Agda-2.5.3/0000755000000000000000000000000013154613124010543 5ustar0000000000000000Agda-2.5.3/stack-8.0.2.yaml0000644000000000000000000000014213154613124013174 0ustar0000000000000000resolver: lts-9.1 # Local packages, usually specified by relative directory name packages: - '.' Agda-2.5.3/Setup.hs0000644000000000000000000000336013154613124012201 0ustar0000000000000000 import Data.List import Distribution.Simple import Distribution.Simple.LocalBuildInfo import Distribution.Simple.Setup import Distribution.Simple.BuildPaths (exeExtension) import Distribution.PackageDescription import System.FilePath import System.Process import System.Exit main = defaultMainWithHooks hooks hooks = simpleUserHooks { regHook = checkAgdaPrimitiveAndRegister } builtins :: [String] builtins = [ "Bool", "Char", "Coinduction", "Equality", "Float" , "FromNat", "FromNeg", "FromString", "IO", "Int", "List" , "Nat", "Reflection", "Size", "Strict", "String" , "TrustMe", "Unit" ] checkAgdaPrimitive :: PackageDescription -> LocalBuildInfo -> RegisterFlags -> IO () checkAgdaPrimitive pkg info flags | regGenPkgConf flags /= NoFlag = return () -- Gets run twice, only do this the second time checkAgdaPrimitive pkg info flags = do let dirs = absoluteInstallDirs pkg info NoCopyDest agda = buildDir info "agda" "agda" <.> exeExtension primMod ms = (ms, datadir dirs "lib" "prim" "Agda" foldr1 () ms <.> "agda") prims = primMod ["Primitive"] : [ primMod ["Builtin", m] | m <- builtins ] checkPrim (ms, file) = do ok <- rawSystem agda [file, "-v0"] case ok of ExitSuccess -> return () ExitFailure _ -> putStrLn $ "WARNING: Failed to typecheck " ++ intercalate "." ("Agda" : ms) ++ "!" putStrLn "Generating Agda library interface files..." mapM_ checkPrim prims checkAgdaPrimitiveAndRegister :: PackageDescription -> LocalBuildInfo -> UserHooks -> RegisterFlags -> IO () checkAgdaPrimitiveAndRegister pkg info hooks flags = do checkAgdaPrimitive pkg info flags regHook simpleUserHooks pkg info hooks flags -- This actually does something useful Agda-2.5.3/stack-7.8.4.yaml0000644000000000000000000000114213154613124013206 0ustar0000000000000000resolver: lts-2.22 flags: QuickCheck: base4: true base4point8: false extra-deps: - EdisonAPI-1.3.1 - EdisonCore-1.3.1.1 - QuickCheck-2.8.2 - STMonadTrans-0.4.3 - binary-0.7.2.1 - boxes-0.1.4 - cpphs-1.20.8 - data-hash-0.2.0.1 - equivalence-0.3.2 - fail-4.9.0.0 - geniplate-mirror-0.7.5 - gitrev-1.2.0 - haskeline-0.7.1.3 - ieee754-0.7.8 - monadplus-1.4.2 - mtl-2.2.1 - murmur-hash-0.1.0.9 - regex-tdfa-1.2.2 - regex-tdfa-text-1.0.0.3 - semigroups-0.18 - transformers-0.4.2.0 - transformers-compat-0.4.0.4 - void-0.7.1 # Local packages, usually specified by relative directory name packages: - '.' Agda-2.5.3/LICENSE0000644000000000000000000001246113154613124011554 0ustar0000000000000000Copyright (c) 2005-2017 remains with the authors. Agda 2 was originally written by Ulf Norell, partially based on code from Agda 1 by Catarina Coquand and Makoto Takeyama, and from Agdalight by Ulf Norell and Andreas Abel. Agda 2 is currently actively developed mainly by Andreas Abel, Guillaume Allais, Jesper Cockx, Nils Anders Danielsson, Philipp Hausmann, Fredrik Nordvall Forsberg, Ulf Norell, Víctor López Juan, Andrés Sicard-Ramírez, and Andrea Vezzosi. Further, Agda 2 has received contributions by, amongst others, Stevan Andjelkovic, Marcin Benke, Jean-Philippe Bernardy, Guillaume Brunerie, James Chapman, Dominique Devriese, Péter Diviánszki, Olle Fredriksson, Adam Gundry, Daniel Gustafsson, Kuen-Bang Hou (favonia), Patrik Jansson, Alan Jeffrey, Wolfram Kahl, Pepijn Kokke, Fredrik Lindblad, Francesco Mazzoli, Stefan Monnier, Darin Morrison, Guilhem Moulin, Nicolas Pouillard, Nobuo Yamashita, Christian Sattler, and Makoto Takeyama. The full list of contributors is available at https://github.com/agda/agda/graphs/contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- The file src/full/Agda/Utils/Parser/ReadP.hs is Copyright (c) The University of Glasgow 2002 and is licensed under a BSD-like license as follows: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW AND THE CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- The file src/full/Agda/Utils/Maybe/Strict.hs (and the following license text?) uses the following license: Copyright (c) Roman Leshchinskiy 2006-2007 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the author nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Agda-2.5.3/Agda.cabal0000644000000000000000000006472513154613124012401 0ustar0000000000000000name: Agda version: 2.5.3 cabal-version: >= 1.10 build-type: Custom license: OtherLicense license-file: LICENSE author: Agda 2 was originally written by Ulf Norell, partially based on code from Agda 1 by Catarina Coquand and Makoto Takeyama, and from Agdalight by Ulf Norell and Andreas Abel. Agda 2 is currently actively developed mainly by Andreas Abel, Guillaume Allais, Jesper Cockx, Nils Anders Danielsson, Philipp Hausmann, Fredrik Nordvall Forsberg, Ulf Norell, Víctor López Juan, Andrés Sicard-Ramírez, and Andrea Vezzosi. Further, Agda 2 has received contributions by, amongst others, Stevan Andjelkovic, Marcin Benke, Jean-Philippe Bernardy, Guillaume Brunerie, James Chapman, Dominique Devriese, Péter Diviánszki, Olle Fredriksson, Adam Gundry, Daniel Gustafsson, Kuen-Bang Hou (favonia), Patrik Jansson, Alan Jeffrey, Wolfram Kahl, Pepijn Kokke, Fredrik Lindblad, Francesco Mazzoli, Stefan Monnier, Darin Morrison, Guilhem Moulin, Nicolas Pouillard, Nobuo Yamashita, Christian Sattler, and Makoto Takeyama and many more. maintainer: Ulf Norell homepage: http://wiki.portal.chalmers.se/agda/ bug-reports: https://github.com/agda/agda/issues category: Dependent types synopsis: A dependently typed functional programming language and proof assistant description: Agda is a dependently typed functional programming language: It has inductive families, which are similar to Haskell's GADTs, but they can be indexed by values and not just types. It also has parameterised modules, mixfix operators, Unicode characters, and an interactive Emacs interface (the type checker can assist in the development of your code). . Agda is also a proof assistant: It is an interactive system for writing and checking proofs. Agda is based on intuitionistic type theory, a foundational system for constructive mathematics developed by the Swedish logician Per Martin-Löf. It has many similarities with other proof assistants based on dependent types, such as Coq, Epigram and NuPRL. . This package includes both a command-line program (agda) and an Emacs mode. If you want to use the Emacs mode you can set it up by running @agda-mode setup@ (see the README). . Note that the Agda package does not follow the package versioning policy, because it is not intended to be used by third-party packages. tested-with: GHC == 7.8.4 GHC == 7.10.3 GHC == 8.0.2 GHC == 8.2.1 extra-source-files: CHANGELOG.md README.md src/full/undefined.h stack-7.8.4.yaml stack-7.10.3.yaml stack-8.0.2.yaml data-dir: src/data data-files: Agda.css agda.sty emacs-mode/*.el JS/agda-rts.js JS/biginteger.js lib/prim/Agda/Builtin/Bool.agda lib/prim/Agda/Builtin/Char.agda lib/prim/Agda/Builtin/Coinduction.agda lib/prim/Agda/Builtin/Equality.agda lib/prim/Agda/Builtin/Float.agda lib/prim/Agda/Builtin/FromNat.agda lib/prim/Agda/Builtin/FromNeg.agda lib/prim/Agda/Builtin/FromString.agda lib/prim/Agda/Builtin/IO.agda lib/prim/Agda/Builtin/Int.agda lib/prim/Agda/Builtin/List.agda lib/prim/Agda/Builtin/Nat.agda lib/prim/Agda/Builtin/Reflection.agda lib/prim/Agda/Builtin/Size.agda lib/prim/Agda/Builtin/Strict.agda lib/prim/Agda/Builtin/String.agda lib/prim/Agda/Builtin/TrustMe.agda lib/prim/Agda/Builtin/Unit.agda lib/prim/Agda/Primitive.agda MAlonzo/src/MAlonzo/*.hs postprocess-latex.pl source-repository head type: git location: https://github.com/agda/agda.git source-repository this type: git location: https://github.com/agda/agda.git tag: v2.5.3 flag cpphs default: True manual: True description: Use cpphs instead of cpp. flag debug default: False manual: True description: Enable debugging features that may slow Agda down. flag enable-cluster-counting default: False description: Enable the --count-clusters flag. (If enable-cluster-counting is False, then the --count-clusters flag triggers an error message.) library hs-source-dirs: src/full include-dirs: src/full if flag(cpphs) -- We don't write an upper bound for cpphs because the -- `build-tools` field can not be modified in Hackage. -- If your change the lower bound of cpphs also change it in the -- `.travis.yml` file [Issue #2315]. build-tools: cpphs >= 1.20.8 ghc-options: -pgmP cpphs -optP --cpp if flag(debug) cpp-options: -DDEBUG if flag(enable-cluster-counting) cpp-options: -DCOUNT_CLUSTERS build-depends: text-icu == 0.7.* if os(windows) build-depends: Win32 >= 2.2 && < 2.6 build-depends: array >= 0.5.0.0 && < 0.6 , async >= 2.0.2 && < 2.2 , base >= 4.7.0.2 && < 4.11 , binary >= 0.7.2.1 && < 0.9 , blaze-html >= 0.8 && < 0.10 , boxes >= 0.1.3 && < 0.2 , bytestring >= 0.10.4.0 && < 0.11 , containers >= 0.5.5.1 && < 0.6 , data-hash >= 0.2.0.0 && < 0.3 , deepseq >= 1.3.0.2 && < 1.5 , directory >= 1.2.0.1 && < 1.4 , EdisonCore >= 1.3.1.1 && < 1.3.2 , edit-distance >= 0.2.1.2 && < 0.3 , equivalence >= 0.3.2 && < 0.4 , filepath >= 1.3.0.1 && < 1.5 , geniplate-mirror >= 0.6.0.6 && < 0.8 , gitrev >= 1.2 && < 2.0 -- hashable 1.2.0.10 makes library-test 10x -- slower. The issue was fixed in hashable 1.2.1.0. -- https://github.com/tibbe/hashable/issues/57. , hashable >= 1.2.1.0 && < 1.3 -- There is a "serious bug" -- (https://hackage.haskell.org/package/hashtables-1.2.0.2/changelog) -- in hashtables 1.2.0.0/1.2.0.1. This bug seems to -- have made Agda much slower (infinitely slower?) in -- some cases. , hashtables >= 1.0.1.8 && < 1.2 || >= 1.2.0.2 && < 1.3 , haskeline >= 0.7.1.3 && < 0.8 , ieee754 >= 0.7.8 && < 0.9 , monadplus >= 1.4 && < 1.5 -- mtl-2.1 contains a severe bug. -- -- mtl >= 2.2 && < 2.2.1 doesn't export Control.Monad.Except. , mtl >= 2.2.1 && < 2.3 , murmur-hash >= 0.1 && < 0.2 , uri-encode >= 1.5.0.4 && < 1.6 , parallel >= 3.2.0.4 && < 3.3 -- pretty 1.1.1.2 and 1.1.1.3 do not follow the -- package versioning policy. , pretty >= 1.1.1.1 && < 1.1.1.2 || >= 1.1.2 && < 1.2 , process >= 1.2.0.0 && < 1.7 , regex-tdfa >= 1.2.2 && < 1.3 , stm >= 2.4.4 && < 2.5 , strict >= 0.3.2 && < 0.4 , template-haskell >= 2.9.0.0 && < 2.13 , text >= 0.11.3.1 && < 1.3 , time >= 1.4.2 && < 1.9 -- In hTags the mtl library must be compiled with the -- version of transformers shipped GHC. The lower -- bound is the version of transformers shipped with -- GHC 7.10.3 (it is not necessary set the lower bound -- to a version shipped with GHC < 7.10.3 because in -- this case Agda and hTags use a different version of -- mtl). , transformers >= 0.4.2.0 && < 0.6 , unordered-containers >= 0.2.5.0 && < 0.3 if impl(ghc < 7.10) build-depends: void >= 0.5.4 && < 0.9 -- zlib >= 0.6.1 is broken with GHC < 7.10.3. See Issue 1518. if impl(ghc < 7.10.3) build-depends: zlib >= 0.4.0.1 && < 0.6.1 else build-depends: zlib >= 0.4.0.1 && < 0.7 if impl(ghc < 8.0) -- provide/emulate `Control.Monad.Fail` and `Data.Semigroups` API -- for pre-GHC8 build-depends: fail == 4.9.* , semigroups == 0.18.* -- We don't write upper bounds for Alex nor Happy because the -- `build-tools` field can not be modified in Hackage. Agda doesn't -- build with Alex 3.2.0. build-tools: alex >= 3.1.0 && < 3.2.0 || >= 3.2.1 , happy >= 1.19.4 exposed-modules: Agda.Auto.Auto Agda.Auto.Options Agda.Auto.CaseSplit Agda.Auto.Convert Agda.Auto.NarrowingSearch Agda.Auto.SearchControl Agda.Auto.Syntax Agda.Auto.Typecheck Agda.Benchmarking Agda.Compiler.Backend Agda.Compiler.CallCompiler Agda.Compiler.Common Agda.Compiler.JS.Compiler Agda.Compiler.JS.Syntax Agda.Compiler.JS.Substitution Agda.Compiler.JS.Pretty Agda.Compiler.MAlonzo.Compiler Agda.Compiler.MAlonzo.Encode Agda.Compiler.MAlonzo.HaskellTypes Agda.Compiler.MAlonzo.Misc Agda.Compiler.MAlonzo.Pragmas Agda.Compiler.MAlonzo.Pretty Agda.Compiler.MAlonzo.Primitives Agda.Compiler.ToTreeless Agda.Compiler.Treeless.AsPatterns Agda.Compiler.Treeless.Builtin Agda.Compiler.Treeless.Compare Agda.Compiler.Treeless.EliminateDefaults Agda.Compiler.Treeless.EliminateLiteralPatterns Agda.Compiler.Treeless.Erase Agda.Compiler.Treeless.GuardsToPrims Agda.Compiler.Treeless.Identity Agda.Compiler.Treeless.NormalizeNames Agda.Compiler.Treeless.Pretty Agda.Compiler.Treeless.Simplify Agda.Compiler.Treeless.Subst Agda.Compiler.Treeless.Uncase Agda.Compiler.Treeless.Unused Agda.ImpossibleTest Agda.Interaction.BasicOps Agda.Interaction.SearchAbout Agda.Interaction.CommandLine Agda.Interaction.EmacsCommand Agda.Interaction.EmacsTop Agda.Interaction.FindFile Agda.Interaction.Highlighting.Dot Agda.Interaction.Highlighting.Emacs Agda.Interaction.Highlighting.Generate Agda.Interaction.Highlighting.HTML Agda.Interaction.Highlighting.Precise Agda.Interaction.Highlighting.Range Agda.Interaction.Highlighting.Vim Agda.Interaction.Highlighting.LaTeX Agda.Interaction.Imports Agda.Interaction.InteractionTop Agda.Interaction.Response Agda.Interaction.MakeCase Agda.Interaction.Monad Agda.Interaction.Library Agda.Interaction.Library.Base Agda.Interaction.Library.Parse Agda.Interaction.Options Agda.Interaction.Options.Lenses Agda.Main Agda.Syntax.Abstract.Copatterns Agda.Syntax.Abstract.Name Agda.Syntax.Abstract.Pattern Agda.Syntax.Abstract.Pretty Agda.Syntax.Abstract.Views Agda.Syntax.Abstract Agda.Syntax.Common Agda.Syntax.Concrete.Definitions Agda.Syntax.Concrete.Generic Agda.Syntax.Concrete.Name Agda.Syntax.Concrete.Operators.Parser Agda.Syntax.Concrete.Operators.Parser.Monad Agda.Syntax.Concrete.Operators Agda.Syntax.Concrete.Pretty Agda.Syntax.Concrete Agda.Syntax.Fixity Agda.Syntax.IdiomBrackets Agda.Syntax.Info Agda.Syntax.Internal Agda.Syntax.Internal.Defs Agda.Syntax.Internal.Generic Agda.Syntax.Internal.Names Agda.Syntax.Internal.Pattern Agda.Syntax.Internal.SanityCheck Agda.Syntax.Literal Agda.Syntax.Notation Agda.Syntax.Parser.Alex Agda.Syntax.Parser.Comments Agda.Syntax.Parser.Layout Agda.Syntax.Parser.LexActions Agda.Syntax.Parser.Lexer Agda.Syntax.Parser.Literate Agda.Syntax.Parser.LookAhead Agda.Syntax.Parser.Monad Agda.Syntax.Parser.Parser Agda.Syntax.Parser.StringLiterals Agda.Syntax.Parser.Tokens Agda.Syntax.Parser Agda.Syntax.Position Agda.Syntax.Reflected Agda.Syntax.Scope.Base Agda.Syntax.Scope.Monad Agda.Syntax.Translation.AbstractToConcrete Agda.Syntax.Translation.ConcreteToAbstract Agda.Syntax.Translation.InternalToAbstract Agda.Syntax.Translation.ReflectedToAbstract Agda.Syntax.Treeless Agda.Termination.CallGraph Agda.Termination.CallMatrix Agda.Termination.CutOff Agda.Termination.Inlining Agda.Termination.Monad Agda.Termination.Order Agda.Termination.RecCheck Agda.Termination.SparseMatrix Agda.Termination.Semiring Agda.Termination.TermCheck Agda.Termination.Termination Agda.TheTypeChecker Agda.TypeChecking.Abstract Agda.TypeChecking.CheckInternal Agda.TypeChecking.CompiledClause Agda.TypeChecking.CompiledClause.Compile Agda.TypeChecking.CompiledClause.Match Agda.TypeChecking.Constraints Agda.TypeChecking.Conversion Agda.TypeChecking.Coverage Agda.TypeChecking.Coverage.Match Agda.TypeChecking.Coverage.SplitTree Agda.TypeChecking.Datatypes Agda.TypeChecking.DeadCode Agda.TypeChecking.DisplayForm Agda.TypeChecking.DropArgs Agda.TypeChecking.Empty Agda.TypeChecking.EtaContract Agda.TypeChecking.Errors Agda.TypeChecking.Free Agda.TypeChecking.Free.Lazy Agda.TypeChecking.Free.Old Agda.TypeChecking.Forcing Agda.TypeChecking.Functions Agda.TypeChecking.Implicit Agda.TypeChecking.Injectivity Agda.TypeChecking.InstanceArguments Agda.TypeChecking.Irrelevance Agda.TypeChecking.Level Agda.TypeChecking.LevelConstraints Agda.TypeChecking.MetaVars Agda.TypeChecking.MetaVars.Mention Agda.TypeChecking.MetaVars.Occurs Agda.TypeChecking.Monad.Base Agda.TypeChecking.Monad.Benchmark Agda.TypeChecking.Monad.Builtin Agda.TypeChecking.Monad.Caching Agda.TypeChecking.Monad.Closure Agda.TypeChecking.Monad.Constraints Agda.TypeChecking.Monad.Context Agda.TypeChecking.Monad.Debug Agda.TypeChecking.Monad.Env Agda.TypeChecking.Monad.Imports Agda.TypeChecking.Monad.Local Agda.TypeChecking.Monad.MetaVars Agda.TypeChecking.Monad.Mutual Agda.TypeChecking.Monad.Open Agda.TypeChecking.Monad.Options Agda.TypeChecking.Monad.Sharing Agda.TypeChecking.Monad.Signature Agda.TypeChecking.Monad.SizedTypes Agda.TypeChecking.Monad.State Agda.TypeChecking.Monad.Statistics Agda.TypeChecking.Monad.Trace Agda.TypeChecking.Monad Agda.TypeChecking.Patterns.Abstract Agda.TypeChecking.Patterns.Match Agda.TypeChecking.Polarity Agda.TypeChecking.Positivity Agda.TypeChecking.Positivity.Occurrence Agda.TypeChecking.Pretty Agda.TypeChecking.Primitive Agda.TypeChecking.ProjectionLike Agda.TypeChecking.Quote Agda.TypeChecking.ReconstructParameters Agda.TypeChecking.RecordPatterns Agda.TypeChecking.Records Agda.TypeChecking.Reduce Agda.TypeChecking.Reduce.Fast Agda.TypeChecking.Reduce.Monad Agda.TypeChecking.Rewriting Agda.TypeChecking.Rewriting.NonLinMatch Agda.TypeChecking.Rules.Builtin Agda.TypeChecking.Rules.Builtin.Coinduction Agda.TypeChecking.Rules.Data Agda.TypeChecking.Rules.Decl Agda.TypeChecking.Rules.Def Agda.TypeChecking.Rules.Display Agda.TypeChecking.Rules.LHS Agda.TypeChecking.Rules.LHS.AsPatterns Agda.TypeChecking.Rules.LHS.Implicit Agda.TypeChecking.Rules.LHS.Instantiate Agda.TypeChecking.Rules.LHS.Problem Agda.TypeChecking.Rules.LHS.ProblemRest Agda.TypeChecking.Rules.LHS.Split Agda.TypeChecking.Rules.LHS.Unify Agda.TypeChecking.Rules.Record Agda.TypeChecking.Rules.Term Agda.TypeChecking.Serialise Agda.TypeChecking.Serialise.Base Agda.TypeChecking.Serialise.Instances Agda.TypeChecking.Serialise.Instances.Abstract Agda.TypeChecking.Serialise.Instances.Common Agda.TypeChecking.Serialise.Instances.Compilers Agda.TypeChecking.Serialise.Instances.Highlighting Agda.TypeChecking.Serialise.Instances.Internal Agda.TypeChecking.Serialise.Instances.Errors Agda.TypeChecking.SizedTypes Agda.TypeChecking.SizedTypes.Solve Agda.TypeChecking.SizedTypes.Syntax Agda.TypeChecking.SizedTypes.Utils Agda.TypeChecking.SizedTypes.WarshallSolver Agda.TypeChecking.Substitute Agda.TypeChecking.Substitute.Class Agda.TypeChecking.Substitute.DeBruijn Agda.TypeChecking.SyntacticEquality Agda.TypeChecking.Telescope Agda.TypeChecking.Unquote Agda.TypeChecking.Warnings Agda.TypeChecking.With Agda.Utils.AssocList Agda.Utils.Bag Agda.Utils.Benchmark Agda.Utils.BiMap Agda.Utils.Char Agda.Utils.Cluster Agda.Utils.Empty Agda.Utils.Environment Agda.Utils.Except Agda.Utils.Either Agda.Utils.Favorites Agda.Utils.FileName Agda.Utils.Functor Agda.Utils.Function Agda.Utils.Geniplate Agda.Utils.Graph.AdjacencyMap.Unidirectional Agda.Utils.Hash Agda.Utils.HashMap Agda.Utils.Haskell.Syntax Agda.Utils.Impossible Agda.Utils.IndexedList Agda.Utils.IO Agda.Utils.IO.Binary Agda.Utils.IO.Directory Agda.Utils.IO.UTF8 Agda.Utils.IORef Agda.Utils.Lens Agda.Utils.Lens.Examples Agda.Utils.List Agda.Utils.ListT Agda.Utils.Map Agda.Utils.Maybe Agda.Utils.Maybe.Strict Agda.Utils.Memo Agda.Utils.Monad Agda.Utils.Monoid Agda.Utils.Null Agda.Utils.Parser.MemoisedCPS Agda.Utils.Parser.ReadP Agda.Utils.PartialOrd Agda.Utils.Permutation Agda.Utils.Pointer Agda.Utils.Pretty Agda.Utils.SemiRing Agda.Utils.Singleton Agda.Utils.Size Agda.Utils.String Agda.Utils.Suffix Agda.Utils.Three Agda.Utils.Time Agda.Utils.Trie Agda.Utils.Tuple Agda.Utils.TypeLevel Agda.Utils.Update Agda.Utils.VarSet Agda.Utils.Warshall Agda.Version Agda.VersionCommit other-modules: Paths_Agda -- Initially, we disable all the warnings. ghc-options: -w -- This option must be the first one after disabling the warnings. See -- Issue #2094. if impl(ghc >= 8.0) ghc-options: -Wunrecognised-warning-flags if impl(ghc >= 7.8) ghc-options: -fwarn-deprecated-flags -fwarn-dodgy-exports -fwarn-dodgy-foreign-imports -fwarn-dodgy-imports -fwarn-duplicate-exports -fwarn-empty-enumerations -fwarn-hi-shadowing -fwarn-identities -fwarn-incomplete-patterns -fwarn-inline-rule-shadowing -fwarn-missing-fields -fwarn-missing-methods -fwarn-missing-signatures -fwarn-tabs -fwarn-typed-holes -fwarn-overflowed-literals -fwarn-overlapping-patterns -fwarn-unrecognised-pragmas -fwarn-unused-do-bind -fwarn-warnings-deprecations -fwarn-wrong-do-bind if impl(ghc >= 7.10) ghc-options: -fwarn-unticked-promoted-constructors -- Enable after removing the support for GHC 7.8. -- -fwarn-deriving-typeable -- This option is deprected in GHC 7.10.1. if impl(ghc >= 7.8) && impl(ghc < 7.10) ghc-options: -fwarn-amp -- These options will be removed in GHC 8.0.1. if impl(ghc >= 7.8) && impl(ghc < 8.0) ghc-options: -fwarn-duplicate-constraints -fwarn-pointless-pragmas -- This option will be deprected in GHC 8.0.1. if impl(ghc >= 7.10) && impl(ghc < 8.0) ghc-options: -fwarn-context-quantification if impl(ghc >= 8.0) ghc-options: -Wmissing-pattern-synonym-signatures -Wnoncanonical-monad-instances -Wnoncanonical-monoid-instances -Wsemigroup -Wunused-foralls if impl(ghc >= 8.2) ghc-options: -Wcpp-undef -- ASR TODO (2017-07-23): `make haddock` fails when -- this flag is on. -- -Wmissing-home-modules -Wsimplifiable-class-constraints -Wunbanged-strict-patterns default-language: Haskell2010 default-extensions: ConstraintKinds , DataKinds , DefaultSignatures , DeriveFoldable , DeriveFunctor , DeriveTraversable , ExistentialQuantification , FlexibleContexts , FlexibleInstances , FunctionalDependencies , LambdaCase , MultiParamTypeClasses , MultiWayIf , NamedFieldPuns , RankNTypes , RecordWildCards , ScopedTypeVariables , StandaloneDeriving , TypeSynonymInstances , TupleSections executable agda hs-source-dirs: src/main main-is: Main.hs build-depends: Agda -- A version range on Agda generates a warning with -- some versions of Cabal and GHC -- (e.g. cabal-install version 1.24.0.2 compiled -- using version 1.24.2.0 of the Cabal library and -- GHC 8.2.1 RC1). -- Nothing is used from the following package, -- except for the prelude. , base >= 4.7.0.2 && < 6 default-language: Haskell2010 if impl(ghc >= 7) -- If someone installs Agda with the setuid bit set, then the -- presence of +RTS may be a security problem (see GHC bug #3910). -- However, we sometimes recommend people to use +RTS to control -- Agda's memory usage, so we want this functionality enabled by -- default. ghc-options: -rtsopts executable agda-mode hs-source-dirs: src/agda-mode main-is: Main.hs other-modules: Paths_Agda build-depends: base >= 4.7.0.2 && < 4.11 , directory >= 1.2.1.0 && < 1.4 , filepath >= 1.3.0.2 && < 1.5 , process >= 1.2.0.0 && < 1.7 default-language: Haskell2010 Agda-2.5.3/CHANGELOG.md0000644000000000000000000106556713154613124012400 0ustar0000000000000000Release notes for Agda version 2.5.3 ==================================== Installation and infrastructure ------------------------------- * Added support for GHC 8.0.2 and 8.2.1. * Removed support for GHC 7.6.3. * Markdown support for literate Agda \[PR [#2357](https://github.com/agda/agda/pull/2357)]. Files ending in `.lagda.md` will be parsed as literate Markdown files. + Code blocks start with ```` ``` ```` or ```` ```agda ```` in its own line, and end with ```` ``` ````, also in its own line. + Code blocks which should be type-checked by Agda but should not be visible when the Markdown is rendered may be enclosed in HTML comment delimiters (``). + Code blocks which should be ignored by Agda, but rendered in the final document may be indented by four spaces. + Note that inline code fragments are not supported due to the difficulty of interpreting their indentation level with respect to the rest of the file. Language -------- ### Pattern matching * Dot patterns. The dot in front of an inaccessible pattern can now be skipped if the pattern consists entirely of constructors or literals. For example: ```agda open import Agda.Builtin.Bool data D : Bool → Set where c : D true f : (x : Bool) → D x → Bool f true c = true ``` Before this change, you had to write `f .true c = true`. * With-clause patterns can be replaced by _ [Issue [#2363](https://github.com/agda/agda/issues/2363)]. Example: ```agda test : Nat → Set test zero with zero test _ | _ = Nat test (suc x) with zero test _ | _ = Nat ``` We do not have to spell out the pattern of the parent clause (`zero` / `suc x`) in the with-clause if we do not need the pattern variables. Note that `x` is not in scope in the with-clause! A more elaborate example, which cannot be reduced to an ellipsis `...`: ```agda record R : Set where coinductive -- disallow matching field f : Bool n : Nat data P (r : R) : Nat → Set where fTrue : R.f r ≡ true → P r zero nSuc : P r (suc (R.n r)) data Q : (b : Bool) (n : Nat) → Set where true! : Q true zero suc! : ∀{b n} → Q b (suc n) test : (r : R) {n : Nat} (p : P r n) → Q (R.f r) n test r nSuc = suc! test r (fTrue p) with R.f r test _ (fTrue ()) | false test _ _ | true = true! -- underscore instead of (isTrue _) ``` * Pattern matching lambdas (also known as extended lambdas) can now be nullary, mirroring the behaviour for ordinary function definitions. [Issue [#2671](https://github.com/agda/agda/issues/2671)] This is useful for case splitting on the result inside an expression: given ```agda record _×_ (A B : Set) : Set where field π₁ : A π₂ : B open _×_ ``` one may case split on the result (C-c C-c RET) in a hole ```agda λ { → {!!}} ``` of type A × B to produce ```agda λ { .π₁ → {!!} ; .π₂ → {!!}} ``` * Records with a field of an empty type are now recognized as empty by Agda. In particular, they can be matched against with an absurd pattern (). For example: ```agda data ⊥ : Set where record Empty : Set where field absurdity : ⊥ magic : Empty → ⊥ magic () ``` * Injective pragmas. Injective pragmas can be used to mark a definition as injective for the pattern matching unifier. This can be used as a version of `--injective-type-constructors` that only applies to specific datatypes. For example: ```agda open import Agda.Builtin.Equality data Fin : Nat → Set where zero : {n : Nat} → Fin (suc n) suc : {n : Nat} → Fin n → Fin (suc n) {-# INJECTIVE Fin #-} Fin-injective : {m n : Nat} → Fin m ≡ Fin n → m ≡ n Fin-injective refl = refl ``` Aside from datatypes, this pragma can also be used to mark other definitions as being injective (for example postulates). * Metavariables can no longer be instantiated during case splitting. This means Agda will refuse to split instead of taking the first constructor it finds. For example: ```agda open import Agda.Builtin.Nat data Vec (A : Set) : Nat → Set where nil : Vec A 0 cons : {n : Nat} → A → Vec A n → Vec A (suc n) foo : Vec Nat _ → Nat foo x = {!x!} ``` In Agda 2.5.2, case splitting on `x` produced the single clause `foo nil = {!!}`, but now Agda refuses to split. ### Reflection * New TC primitive: `debugPrint`. ```agda debugPrint : String → Nat → List ErrorPart → TC ⊤ ``` This maps to the internal function `reportSDoc`. Debug output is enabled with the `-v` flag at the command line, or in an `OPTIONS` pragma. For instance, giving `-v a.b.c:10` enables printing from `debugPrint "a.b.c.d" 10 msg`. In the Emacs mode, debug output ends up in the `*Agda debug*` buffer. ### Built-ins * BUILTIN REFL is now superfluous, subsumed by BUILTIN EQUALITY [Issue [#2389](https://github.com/agda/agda/issues/2389)]. * BUILTIN EQUALITY is now more liberal [Issue [#2386](https://github.com/agda/agda/issues/2386)]. It accepts, among others, the following new definitions of equality: ```agda -- Non-universe polymorphic: data _≡_ {A : Set} (x : A) : A → Set where refl : x ≡ x -- ... with explicit argument to refl; data _≡_ {A : Set} : (x y : A) → Set where refl : {x : A} → x ≡ x -- ... even visible data _≡_ {A : Set} : (x y : A) → Set where refl : (x : A) → x ≡ x -- Equality in a different universe than domain: -- (also with explicit argument to refl) data _≡_ {a} {A : Set a} (x : A) : A → Set where refl : x ≡ x ``` The standard definition is still: ```agda -- Equality in same universe as domain: data _≡_ {a} {A : Set a} (x : A) : A → Set a where refl : x ≡ x ``` ### Miscellaneous * Rule change for omitted top-level module headers. [Issue [#1077](https://github.com/agda/agda/issues/1077)] If your file is named `Bla.agda`, then the following content is rejected. ```agda foo = Set module Bla where bar = Set ``` Before the fix of this issue, Agda would add the missing module header `module Bla where` at the top of the file. However, in this particular case it is more likely the user put the declaration `foo = Set` before the module start in error. Now you get the error ``` Illegal declaration(s) before top-level module ``` if the following conditions are met: 1. There is at least one non-import declaration or non-toplevel pragma before the start of the first module. 2. The module has the same name as the file. 3. The module is the only module at this level (may have submodules, of course). If you should see this error, insert a top-level module before the illegal declarations, or move them inside the existing module. Emacs mode ---------- * New warnings: - Unreachable clauses give rise to a simple warning. They are highlighted in gray. - Incomplete patterns are non-fatal warnings: it is possible to keep interacting with the file (the reduction will simply be stuck on arguments not matching any pattern). The definition with incomplete patterns are highlighted in wheat. * Clauses which do not hold definitionally are now highlighted in white smoke. * Fewer commands have the side effect that the buffer is saved. * Aborting commands. Now one can (try to) abort an Agda command by using `C-c C-x C-a` or a menu entry. The effect is similar to that of restarting Agda (`C-c C-x C-r`), but some state is preserved, which could mean that it takes less time to reload the module. Warning: If a command is aborted while it is writing data to disk (for instance .agdai files or Haskell files generated by the GHC backend), then the resulting files may be corrupted. Note also that external commands (like GHC) are not aborted, and their output may continue to be sent to the Emacs mode. * New bindings for the Agda input method: - All the bold digits are now available. The naming scheme is `\Bx` for digit `x`. - Typing `\:` you can now get a whole slew of colons. (The Agda input method originally only bound the standard unicode colon, which looks deceptively like the normal colon.) * Case splitting now preserves underscores. [Issue [#819](https://github.com/agda/agda/issues/819)] ```agda data ⊥ : Set where test : {A B : Set} → A → ⊥ → B test _ x = {! x !} ``` Splitting on `x` yields ```agda test _ () ``` * Interactively expanding ellipsis. [Issue [#2589](https://github.com/agda/agda/issues/2589)] An ellipsis in a with-clause can be expanded by splitting on "variable" "." (dot). ```agda test0 : Nat → Nat test0 x with zero ... | q = {! . !} -- C-c C-c ``` Splitting on dot here yields: ```agda test0 x | q = ? ``` * New command to check an expression against the type of the hole it is in and see what it elaborates to. [Issue [#2700](https://github.com/agda/agda/issues/2700)] This is useful to determine e.g. what solution typeclass resolution yields. The command is bound to `C-c C-;` and respects the `C-u` modifier. ```agda record Pointed (A : Set) : Set where field point : A it : ∀ {A : Set} {{x : A}} → A it {{x}} = x instance _ = record { point = 3 - 4 } _ : Pointed Nat _ = {! it !} -- C-u C-u C-c C-; ``` yields ```agda Goal: Pointed Nat Elaborates to: record { point = 0 } ``` * If `agda2-give` is called with a prefix, then giving is forced, i.e., the safety checks are skipped, including positivity, termination, and double type-checking. [Issue [#2730](https://github.com/agda/agda/issues/2730)] Invoke forced giving with key sequence `C-u C-c C-SPC`. Library management ------------------ * The `name` field in an `.agda-lib` file is now optional. [Issue [#2708](https://github.com/agda/agda/issues/2708)] This feature is convenient if you just want to specify the dependencies and include pathes for your local project in an `.agda-lib` file. Naturally, libraries without names cannot be depended on. Compiler backends ----------------- * Unified compiler pragmas The compiler pragmas (`COMPILED`, `COMPILED_DATA`, etc.) have been unified across backends into two new pragmas: ``` {-# COMPILE #-} {-# FOREIGN #-} ``` The old pragmas still work, but will emit a warning if used. They will be removed completely in Agda 2.6. The translation of old pragmas into new ones is as follows: Old | New --- | --- `{-# COMPILED f e #-}` | `{-# COMPILE GHC f = e #-}` `{-# COMPILED_TYPE A T #-}` | `{-# COMPILE GHC A = type T #-}` `{-# COMPILED_DATA A D C1 .. CN #-}` | `{-# COMPILE GHC A = data D (C1 | .. | CN) #-}` `{-# COMPILED_DECLARE_DATA #-}` | obsolete, removed `{-# COMPILED_EXPORT f g #-}` | `{-# COMPILE GHC f as g #-}` `{-# IMPORT M #-}` | `{-# FOREIGN GHC import qualified M #-}` `{-# HASKELL code #-}` | `{-# FOREIGN GHC code #-}` `{-# COMPILED_UHC f e #-}` | `{-# COMPILE UHC f = e #-}` `{-# COMPILED_DATA_UHC A D C1 .. CN #-}` | `{-# COMPILE UHC A = data D (C1 | .. | CN) #-}` `{-# IMPORT_UHC M #-}` | `{-# FOREIGN UHC __IMPORT__ M #-}` `{-# COMPILED_JS f e #-}` | `{-# COMPILE JS f = e #-}` * GHC Haskell backend The COMPILED pragma (and the corresponding COMPILE GHC pragma) is now also allowed for functions. This makes it possible to have both an Agda implementation and a native Haskell runtime implementation. The GHC file header pragmas `LANGUAGE`, `OPTIONS_GHC`, and `INCLUDE` inside a `FOREIGN GHC` pragma are recognized and printed correctly at the top of the generated Haskell file. [Issue [#2712](https://github.com/agda/agda/issues/2712)] * UHC compiler backend The UHC backend has been moved to its own repository [https://github.com/agda/agda-uhc] and is no longer part of the Agda distribution. * Haskell imports are no longer transitively inherited from imported modules. The (now deprecated) IMPORT and IMPORT_UHC pragmas no longer cause import statements in modules importing the module containing the pragma. The same is true for the corresponding FOREIGN pragmas. * Support for stand-alone backends. There is a new API in `Agda.Compiler.Backend` for creating stand-alone backends using Agda as a library. This allows prospective backend writers to experiment with new backends without having to change the Agda code base. HTML backend ------------ * Anchors for identifiers (excluding bound variables) are now the identifiers themselves rather than just the file position [Issue [#2604](https://github.com/agda/agda/issues/2604)]. Symbolic anchors look like ```html ``` while other anchors just give the character position in the file: ```html ``` Top-level module names do not get a symbolic anchor, since the position of a top-level module is defined to be the beginning of the file. Example: ```agda module Issue2604 where -- Character position anchor test1 : Set₁ -- Issue2604.html#test1 test1 = bla where bla = Set -- Character position anchor test2 : Set₁ -- Issue2604.html#test2 test2 = bla where bla = Set -- Character position anchor test3 : Set₁ -- Issue2604.html#test3 test3 = bla module M where -- Issue2604.html#M bla = Set -- Issue2604.html#M.bla module NamedModule where -- Issue2604.html#NamedModule test4 : Set₁ -- Issue2604.html#NamedModule.test4 test4 = M.bla module _ where -- Character position anchor test5 : Set₁ -- Character position anchor test5 = M.bla ``` * Some generated HTML files now have different file names [Issue [#2725](https://github.com/agda/agda/issues/2725)]. Agda now uses an encoding that amounts to first converting the module names to UTF-8, and then percent-encoding the resulting bytes. For instance, HTML for the module `Σ` is placed in `%CE%A3.html`. LaTeX backend ------------- * The LaTeX backend now handles indentation in a different way [Issue [#1832](https://github.com/agda/agda/issues/1832)]. A constraint on the indentation of the first token *t* on a line is determined as follows: * Let *T* be the set containing every previous token (in any code block) that is either the initial token on its line or preceded by at least one whitespace character. * Let *S* be the set containing all tokens in *T* that are not *shadowed* by other tokens in *T*. A token *t₁* is shadowed by *t₂* if *t₂* is further down than *t₁* and does not start to the right of *t₁*. * Let *L* be the set containing all tokens in *S* that start to the left of *t*, and *E* be the set containing all tokens in *S* that start in the same column as *t*. * The constraint is that *t* must be indented further than every token in *L*, and aligned with every token in *E*. Note that if any token in *L* or *E* belongs to a previous code block, then the constraint may not be satisfied unless (say) the `AgdaAlign` environment is used in an appropriate way. If custom settings are used, for instance if `\AgdaIndent` is redefined, then the constraint discussed above may not be satisfied. (Note that the meaning of the `\AgdaIndent` command's argument has changed, and that the command is now used in a different way in the generated LaTeX files.) Examples: * Here `C` is indented further than `B`: ```agda postulate A B C : Set ``` * Here `C` is not (necessarily) indented further than `B`, because `X` shadows `B`: ```agda postulate A B : Set X C : Set ``` The new rule is inspired by, but not identical to, the one used by lhs2TeX's poly mode (see Section 8.4 of the [manual for lhs2TeX version 1.17](https://www.andres-loeh.de/lhs2tex/Guide2-1.17.pdf)). * Some spacing issues [[#2353](https://github.com/agda/agda/issues/2353), [#2441](https://github.com/agda/agda/issues/2441), [#2733](https://github.com/agda/agda/issues/2733), [#2740](https://github.com/agda/agda/issues/2740)] have been fixed. * The user can now control the typesetting of (certain) individual tokens by redefining the `\AgdaFormat` command. Example: ```latex \usepackage{ifthen} % Insert extra space before some tokens. \DeclareRobustCommand{\AgdaFormat}[2]{% \ifthenelse{ \equal{#1}{≡⟨} \OR \equal{#1}{≡⟨⟩} \OR \equal{#1}{∎} }{\ }{}#2} ``` Note the use of `\DeclareRobustCommand`. The first argument to `\AgdaFormat` is the token, and the second argument the thing to be typeset. * One can now instruct the agda package not to select any fonts. If the `nofontsetup` option is used, then some font packages are loaded, but specific fonts are not selected: ```latex \usepackage[nofontsetup]{agda} ``` * The height of empty lines is now configurable [[#2734](https://github.com/agda/agda/issues/2734)]. The height is controlled by the length `\AgdaEmptySkip`, which by default is `\baselineskip`. * The alignment feature regards the string `+̲`, containing `+` and a combining character, as having length two. However, it seems more reasonable to treat it as having length one, as it occupies a single column, if displayed "properly" using a monospace font. The new flag `--count-clusters` is an attempt at fixing this. When this flag is enabled the backend counts ["extended grapheme clusters"](http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries) rather than code points. Note that this fix is not perfect: a single extended grapheme cluster might be displayed in different ways by different programs, and might, in some cases, occupy more than one column. Here are some examples of extended grapheme clusters, all of which are treated as a single character by the alignment algorithm: ``` │ │ │+̲│ │Ö̂│ │நி│ │ᄀힰᇹ│ │ᄀᄀᄀᄀᄀᄀힰᇹᇹᇹᇹᇹᇹ│ │ │ ``` Note also that the layout machinery does not count extended grapheme clusters, but code points. The following code is syntactically correct, but if `--count-clusters` is used, then the LaTeX backend does not align the two `field` keywords: ```agda record +̲ : Set₁ where field A : Set field B : Set ``` The `--count-clusters` flag is not enabled in all builds of Agda, because the implementation depends on the [ICU](http://site.icu-project.org) library, the installation of which could cause extra trouble for some users. The presence of this flag is controlled by the Cabal flag `enable-cluster-counting`. * A faster variant of the LaTeX backend: QuickLaTeX. When this variant of the backend is used the top-level module is not type-checked, only scope-checked. This implies that some highlighting information is not available. For instance, overloaded constructors are not resolved. QuickLaTeX can be invoked from the Emacs mode, or using `agda --latex --only-scope-checking`. If the module has already been type-checked successfully, then this information is reused; in this case QuickLaTeX behaves like the regular LaTeX backend. The `--only-scope-checking` flag can also be used independently, but it is perhaps unclear what purpose that would serve. (The flag can currently not be combined with `--html`, `--dependency-graph` or `--vim`.) The flag is not allowed in safe mode. Pragmas and options ------------------- * The `--safe` option is now a valid pragma. This makes it possible to declare a module as being part of the safe subset of the language by stating `{-# OPTIONS --safe #-}` at the top of the corresponding file. Incompatibilities between the `--safe` option and other options or language constructs are non-fatal errors. * The `--no-main` option is now a valid pragma. One can now suppress the compiler warning about a missing main function by putting ```agda {-# OPTIONS --no-main #-} ``` on top of the file. * New command-line option and pragma `--warning=MODE` (or `-W MODE`) for setting the warning mode. Current options are - `warn` for displaying warnings (default) - `error` for turning warnings into errors - `ignore` for not displaying warnings List of fixed issues -------------------- For 2.5.3, the following issues have been fixed (see [bug tracker](https://github.com/agda/agda/issues)): - [#142](https://github.com/agda/agda/issues/142): Inherited dot patterns in with functions are not checked - [#623](https://github.com/agda/agda/issues/623): Error message points to importing module rather than imported module - [#657](https://github.com/agda/agda/issues/657): Yet another display form problem - [#668](https://github.com/agda/agda/issues/668): Ability to stop, or restart, typechecking somehow - [#705](https://github.com/agda/agda/issues/705): confusing error message for ambiguous datatype module name - [#719](https://github.com/agda/agda/issues/719): Error message for duplicate module definition points to external module instead of internal module - [#776](https://github.com/agda/agda/issues/776): Unsolvable constraints should give error - [#819](https://github.com/agda/agda/issues/819): Case-splitting doesn't preserve underscores - [#883](https://github.com/agda/agda/issues/883): Rewrite loses type information - [#899](https://github.com/agda/agda/issues/899): Instance search fails if there are several definitionally equal values in scope - [#1077](https://github.com/agda/agda/issues/1077): problem with module syntax, with parametric module import - [#1126](https://github.com/agda/agda/issues/1126): Port optimizations from the Epic backend - [#1175](https://github.com/agda/agda/issues/1175): Internal Error in Auto - [#1544](https://github.com/agda/agda/issues/1544): Positivity polymorphism needed for compositional positivity analysis - [#1611](https://github.com/agda/agda/issues/1611): Interactive splitting instantiates meta - [#1664](https://github.com/agda/agda/issues/1664): Add Reflection primitives to expose precedence and fixity - [#1817](https://github.com/agda/agda/issues/1817): Solvable size constraints reported as unsolvable - [#1832](https://github.com/agda/agda/issues/1832): Insufficient indentation in LaTeX-rendered Agda code - [#1834](https://github.com/agda/agda/issues/1834): Copattern matching: order of clauses should not matter here - [#1886](https://github.com/agda/agda/issues/1886): Second copies of telescopes not checked? - [#1899](https://github.com/agda/agda/issues/1899): Positivity checker does not treat datatypes and record types in the same way - [#1975](https://github.com/agda/agda/issues/1975): Type-incorrect instantiated overloaded constructor accepted in pattern - [#1976](https://github.com/agda/agda/issues/1976): Type-incorrect instantiated projection accepted in pattern - [#2035](https://github.com/agda/agda/issues/2035): Matching on string causes solver to fail with internal error - [#2146](https://github.com/agda/agda/issues/2146): Unicode syntax for instance arguments - [#2217](https://github.com/agda/agda/issues/2217): Abort Agda without losing state - [#2229](https://github.com/agda/agda/issues/2229): Absence or presence of top-level module header affects scope - [#2253](https://github.com/agda/agda/issues/2253): Wrong scope error for abstract constructors - [#2261](https://github.com/agda/agda/issues/2261): Internal error in Auto/CaseSplit.hs:284 - [#2270](https://github.com/agda/agda/issues/2270): Printer does not use sections. - [#2329](https://github.com/agda/agda/issues/2329): Size solver does not use type `Size< i` to gain the necessary information - [#2354](https://github.com/agda/agda/issues/2354): Interaction between instance search, size solver, and ordinary constraint solver. - [#2355](https://github.com/agda/agda/issues/2355): Literate Agda parser does not recognize TeX comments - [#2360](https://github.com/agda/agda/issues/2360): With clause stripping chokes on ambiguous projection - [#2362](https://github.com/agda/agda/issues/2362): Printing of parent patterns when with-clause does not match - [#2363](https://github.com/agda/agda/issues/2363): Allow underscore in with-clause patterns - [#2366](https://github.com/agda/agda/issues/2366): With-clause patterns renamed in error message - [#2368](https://github.com/agda/agda/issues/2368): Internal error after refining a tactic @ MetaVars.hs:267 - [#2371](https://github.com/agda/agda/issues/2371): Shadowed module parameter crashes interaction - [#2372](https://github.com/agda/agda/issues/2372): problems when instances are declared with inferred types - [#2374](https://github.com/agda/agda/issues/2374): Ambiguous projection pattern could be disambiguated by visibility - [#2376](https://github.com/agda/agda/issues/2376): Termination checking interacts badly with eta-contraction - [#2377](https://github.com/agda/agda/issues/2377): open public is useless before module header - [#2381](https://github.com/agda/agda/issues/2381): Search (`C-c C-z`) panics on pattern synonyms - [#2386](https://github.com/agda/agda/issues/2386): Relax requirements of BUILTIN EQUALITY - [#2389](https://github.com/agda/agda/issues/2389): BUILTIN REFL not needed - [#2400](https://github.com/agda/agda/issues/2400): LaTeX backend error on LaTeX comments - [#2402](https://github.com/agda/agda/issues/2402): Parameters not dropped when reporting incomplete patterns - [#2403](https://github.com/agda/agda/issues/2403): Termination checker should reduce arguments in structural order check - [#2405](https://github.com/agda/agda/issues/2405): instance search failing in parameterized module - [#2408](https://github.com/agda/agda/issues/2408): DLub sorts are not serialized - [#2412](https://github.com/agda/agda/issues/2412): Problem with checking with sized types - [#2413](https://github.com/agda/agda/issues/2413): Agda crashes on x@y pattern - [#2415](https://github.com/agda/agda/issues/2415): Size solver reports "inconsistent upper bound" even though there is a solution - [#2416](https://github.com/agda/agda/issues/2416): Cannot give size as computed by solver - [#2422](https://github.com/agda/agda/issues/2422): Overloaded inherited projections don't resolve - [#2423](https://github.com/agda/agda/issues/2423): Inherited projection on lhs - [#2426](https://github.com/agda/agda/issues/2426): On just warning about missing cases - [#2429](https://github.com/agda/agda/issues/2429): Irrelevant lambda should be accepted when relevant lambda is expected - [#2430](https://github.com/agda/agda/issues/2430): Another regression related to parameter refinement? - [#2433](https://github.com/agda/agda/issues/2433): rebindLocalRewriteRules re-adds global rewrite rules - [#2434](https://github.com/agda/agda/issues/2434): Exact split analysis is too strict when matching on eta record constructor - [#2441](https://github.com/agda/agda/issues/2441): Incorrect alignement in latex using the new ACM format - [#2444](https://github.com/agda/agda/issues/2444): Generalising compiler pragmas - [#2445](https://github.com/agda/agda/issues/2445): The LaTeX backend is slow - [#2447](https://github.com/agda/agda/issues/2447): Cache loaded interfaces even if a type error is encountered - [#2449](https://github.com/agda/agda/issues/2449): Agda depends on additional C library icu - [#2451](https://github.com/agda/agda/issues/2451): Agda panics when attempting to rewrite a typeclass Eq - [#2456](https://github.com/agda/agda/issues/2456): Internal error when postulating instance - [#2458](https://github.com/agda/agda/issues/2458): Regression: Agda-2.5.3 loops where Agda-2.5.2 passes - [#2462](https://github.com/agda/agda/issues/2462): Overloaded postfix projection does not resolve - [#2464](https://github.com/agda/agda/issues/2464): Eta contraction for irrelevant functions breaks subject reduction - [#2466](https://github.com/agda/agda/issues/2466): Case split to make hidden variable visible does not work - [#2467](https://github.com/agda/agda/issues/2467): REWRITE without BUILTIN REWRITE crashes - [#2469](https://github.com/agda/agda/issues/2469): "Partial" pattern match causes segfault at runtime - [#2472](https://github.com/agda/agda/issues/2472): Regression related to the auto command - [#2477](https://github.com/agda/agda/issues/2477): Sized data type analysis brittle, does not reduce size - [#2478](https://github.com/agda/agda/issues/2478): Multiply defined labels on the user manual (pdf) - [#2479](https://github.com/agda/agda/issues/2479): "Occurs check" error in generated Haskell code - [#2480](https://github.com/agda/agda/issues/2480): Agda accepts incorrect (?) code, subject reduction broken - [#2482](https://github.com/agda/agda/issues/2482): Wrong counting of data parameters with new-style mutual blocks - [#2483](https://github.com/agda/agda/issues/2483): Files are sometimes truncated to a size of 201 bytes - [#2486](https://github.com/agda/agda/issues/2486): Imports via FOREIGN are not transitively inherited anymore - [#2488](https://github.com/agda/agda/issues/2488): Instance search inhibits holes for instance fields - [#2493](https://github.com/agda/agda/issues/2493): Regression: Agda seems to loop when expression is given - [#2494](https://github.com/agda/agda/issues/2494): Instance fields sometimes have incorrect goal types - [#2495](https://github.com/agda/agda/issues/2495): Regression: termination checker of Agda-2.5.3 seemingly loops where Agda-2.5.2 passes - [#2500](https://github.com/agda/agda/issues/2500): Adding fields to a record can cause Agda to reject previous definitions - [#2510](https://github.com/agda/agda/issues/2510): Wrong error with --no-pattern-matching - [#2517](https://github.com/agda/agda/issues/2517): "Not a variable error" - [#2518](https://github.com/agda/agda/issues/2518): CopatternReductions in TreeLess - [#2523](https://github.com/agda/agda/issues/2523): The documentation of `--without-K` is outdated - [#2529](https://github.com/agda/agda/issues/2529): Unable to install Agda on Windows. - [#2537](https://github.com/agda/agda/issues/2537): case splitting with 'with' creates {_} instead of replicating the arguments it found. - [#2538](https://github.com/agda/agda/issues/2538): Internal error when parsing as-pattern - [#2543](https://github.com/agda/agda/issues/2543): Case splitting with ellipsis produces spurious parentheses - [#2545](https://github.com/agda/agda/issues/2545): Race condition in api tests - [#2549](https://github.com/agda/agda/issues/2549): Rewrite rule for higher path constructor does not fire - [#2550](https://github.com/agda/agda/issues/2550): Internal error in Agda.TypeChecking.Substitute - [#2552](https://github.com/agda/agda/issues/2552): Let bindings in module telescopes crash Agda.Interaction.BasicOps - [#2553](https://github.com/agda/agda/issues/2553): Internal error in Agda.TypeChecking.CheckInternal - [#2554](https://github.com/agda/agda/issues/2554): More flexible size-assignment in successor style - [#2555](https://github.com/agda/agda/issues/2555): Why does the positivity checker care about non-recursive occurrences? - [#2558](https://github.com/agda/agda/issues/2558): Internal error in Warshall Solver - [#2560](https://github.com/agda/agda/issues/2560): Internal Error in Reduce.Fast - [#2564](https://github.com/agda/agda/issues/2564): Non-exact-split highlighting makes other highlighting disappear - [#2568](https://github.com/agda/agda/issues/2568): agda2-infer-type-maybe-toplevel (in hole) does not respect "single-solution" requirement of instance resolution - [#2571](https://github.com/agda/agda/issues/2571): Record pattern translation does not eta contract - [#2573](https://github.com/agda/agda/issues/2573): Rewrite rules fail depending on unrelated changes - [#2574](https://github.com/agda/agda/issues/2574): No link attached to module without toplevel name - [#2575](https://github.com/agda/agda/issues/2575): Internal error, related to caching - [#2577](https://github.com/agda/agda/issues/2577): deBruijn fail for higher order instance problem - [#2578](https://github.com/agda/agda/issues/2578): Catch-all clause face used incorrectly for parent with pattern - [#2579](https://github.com/agda/agda/issues/2579): Import statements with module instantiation should not trigger an error message - [#2580](https://github.com/agda/agda/issues/2580): Implicit absurd match is NonVariant, explicit not - [#2583](https://github.com/agda/agda/issues/2583): Wrong de Bruijn index introduced by absurd pattern - [#2584](https://github.com/agda/agda/issues/2584): Duplicate warning printing - [#2585](https://github.com/agda/agda/issues/2585): Definition by copatterns not modulo eta - [#2586](https://github.com/agda/agda/issues/2586): "λ where" with single absurd clause not parsed - [#2588](https://github.com/agda/agda/issues/2588): `agda --latex` produces invalid LaTeX when there are block comments - [#2592](https://github.com/agda/agda/issues/2592): Internal Error in Agda/TypeChecking/Serialise/Instances/Common.hs - [#2597](https://github.com/agda/agda/issues/2597): Inline record definitions confuse the reflection API - [#2602](https://github.com/agda/agda/issues/2602): Debug output messes up AgdaInfo buffer - [#2603](https://github.com/agda/agda/issues/2603): Internal error in MetaVars.hs - [#2604](https://github.com/agda/agda/issues/2604): Use QNames as anchors in generated HTML - [#2605](https://github.com/agda/agda/issues/2605): HTML backend generates anchors for whitespace - [#2606](https://github.com/agda/agda/issues/2606): Check that LHS of a rewrite rule doesn't reduce is too strict - [#2612](https://github.com/agda/agda/issues/2612): `exact-split` documentation is outdated and incomplete - [#2613](https://github.com/agda/agda/issues/2613): Parametrised modules, with-abstraction and termination - [#2620](https://github.com/agda/agda/issues/2620): Internal error in auto. - [#2621](https://github.com/agda/agda/issues/2621): Case splitting instantiates meta - [#2626](https://github.com/agda/agda/issues/2626): triggered internal error with sized types in MetaVars module - [#2629](https://github.com/agda/agda/issues/2629): Exact splitting should not complain about absurd clauses - [#2631](https://github.com/agda/agda/issues/2631): docs for auto aren't clear on how to use flags/options - [#2632](https://github.com/agda/agda/issues/2632): some flags to auto dont seem to work in current agda 2.5.2 - [#2637](https://github.com/agda/agda/issues/2637): Internal error in Agda.TypeChecking.Pretty, possibly related to sized types - [#2639](https://github.com/agda/agda/issues/2639): Performance regression, possibly related to the size solver - [#2641](https://github.com/agda/agda/issues/2641): Required instance of FromNat when compiling imported files - [#2642](https://github.com/agda/agda/issues/2642): Records with duplicate fields - [#2644](https://github.com/agda/agda/issues/2644): Wrong substitution in expandRecordVar - [#2645](https://github.com/agda/agda/issues/2645): Agda accepts postulated fields in a record - [#2646](https://github.com/agda/agda/issues/2646): Only warn if fixities for undefined symbols are given - [#2649](https://github.com/agda/agda/issues/2649): Empty list of "previous definition" in duplicate definition error - [#2652](https://github.com/agda/agda/issues/2652): Added a new variant of the colon to the Agda input method - [#2653](https://github.com/agda/agda/issues/2653): agda-mode: "cannot refine" inside instance argument even though term to be refined typechecks there - [#2654](https://github.com/agda/agda/issues/2654): Internal error on result splitting without --postfix-projections - [#2664](https://github.com/agda/agda/issues/2664): Segmentation fault with compiled programs using mutual record - [#2665](https://github.com/agda/agda/issues/2665): Documentation: Record update syntax in wrong location - [#2666](https://github.com/agda/agda/issues/2666): Internal error at Agda/Syntax/Abstract/Name.hs:113 - [#2667](https://github.com/agda/agda/issues/2667): Panic error on unbound variable. - [#2669](https://github.com/agda/agda/issues/2669): Interaction: incorrect field variable name generation - [#2671](https://github.com/agda/agda/issues/2671): Feature request: nullary pattern matching lambdas - [#2679](https://github.com/agda/agda/issues/2679): Internal error at "Typechecking/Abstract.hs:133" and "TypeChecking/Telescope.hs:68" - [#2682](https://github.com/agda/agda/issues/2682): What are the rules for projections of abstract records? - [#2684](https://github.com/agda/agda/issues/2684): Bad error message for abstract constructor - [#2686](https://github.com/agda/agda/issues/2686): Abstract constructors should be ignored when resolving overloading - [#2690](https://github.com/agda/agda/issues/2690): [regression?] Agda engages in deep search instead of immediately failing - [#2700](https://github.com/agda/agda/issues/2700): Add a command to check against goal type (and normalise) - [#2703](https://github.com/agda/agda/issues/2703): Regression: Internal error for underapplied indexed constructor - [#2705](https://github.com/agda/agda/issues/2705): The GHC backend might diverge in infinite file creation - [#2708](https://github.com/agda/agda/issues/2708): Why is the `name` field in .agda-lib files mandatory? - [#2710](https://github.com/agda/agda/issues/2710): Type checker hangs - [#2712](https://github.com/agda/agda/issues/2712): Compiler Pragma for headers - [#2714](https://github.com/agda/agda/issues/2714): Option --no-main should be allowed as file-local option - [#2717](https://github.com/agda/agda/issues/2717): internal error at DisplayForm.hs:197 - [#2718](https://github.com/agda/agda/issues/2718): Interactive 'give' doesn't insert enough parenthesis - [#2721](https://github.com/agda/agda/issues/2721): Without-K doesn't prevent heterogeneous conflict between literals - [#2723](https://github.com/agda/agda/issues/2723): Unreachable clauses in definition by copattern matching trip clause compiler - [#2725](https://github.com/agda/agda/issues/2725): File names for generated HTML files - [#2726](https://github.com/agda/agda/issues/2726): Old regression related to with - [#2727](https://github.com/agda/agda/issues/2727): Internal errors related to rewrite - [#2729](https://github.com/agda/agda/issues/2729): Regression: case splitting uses variable name variants instead of the unused original names - [#2730](https://github.com/agda/agda/issues/2730): Command to give in spite of termination errors - [#2731](https://github.com/agda/agda/issues/2731): Agda fails to build with happy 1.19.6 - [#2733](https://github.com/agda/agda/issues/2733): Avoid some uses of \AgdaIndent? - [#2734](https://github.com/agda/agda/issues/2734): Make height of empty lines configurable - [#2736](https://github.com/agda/agda/issues/2736): Segfault using Alex 3.2.2 and cpphs - [#2740](https://github.com/agda/agda/issues/2740): Indenting every line of code should be a no-op Release notes for Agda version 2.5.2 ==================================== Installation and infrastructure ------------------------------- * Modular support for literate programming Literate programming support has been moved out of the lexer and into the `Agda.Syntax.Parser.Literate` module. Files ending in `.lagda` are still interpreted as literate TeX. The extension `.lagda.tex` may now also be used for literate TeX files. Support for more literate code formats and extensions can be added modularly. By default, `.lagda.*` files are opened in the Emacs mode corresponding to their last extension. One may switch to and from Agda mode manually. * reStructuredText Literate Agda code can now be written in reStructuredText format, using the `.lagda.rst` extension. As a general rule, Agda will parse code following a line ending in `::`, as long as that line does not start with `..`. The module name must match the path of the file in the documentation, and must be given explicitly. Several files have been converted already, for instance: - `language/mixfix-operators.lagda.rst` - `tools/compilers.lagda.rst` Note that: - Code blocks inside an rST comment block will be type-checked by Agda, but not rendered in the documentation. - Code blocks delimited by `.. code-block:: agda` will be rendered in the final documenation, but not type-checked by Agda. - All lines inside a codeblock must be further indented than the first line of the code block. - Indentation must be consistent between code blocks. In other words, the file as a whole must be a valid Agda file if all the literate text is replaced by white space. * Documentation testing All documentation files in the `doc/user-manual` directory that end in `.lagda.rst` can be typechecked by running `make user-manual-test`, and also as part of the general test suite. * Support installation through Stack The Agda sources now also include a configuration for the stack install tool (tested through continuous integration). It should hence be possible to repeatably build any future Agda version (including unreleased commits) from source by checking out that version and running `stack install` from the checkout directory. By using repeatable builds, this should keep selecting the same dependencies in the face of new releases on Hackage. For further motivation, see Issue [#2005](https://github.com/agda/agda/issues/2005). * Removed the `--test` command-line option This option ran the internal test-suite. This test-suite was implemented using Cabal supports for test-suites. [Issue [#2083](https://github.com/agda/agda/issues/2083)]. * The `--no-default-libraries` flag has been split into two flags [Issue [#1937](https://github.com/agda/agda/issues/1937)] - `--no-default-libraries`: Ignore the defaults file but still look for local `.agda-lib` files - `--no-libraries`: Don't use any `.agda-lib` files (the previous behaviour of `--no-default-libraries`). * If `agda` was built inside `git` repository, then the `--version` flag will display the hash of the commit used, and whether the tree was `-dirty` (i.e. there were uncommited changes in the working directory). Otherwise, only the version number is shown. Language -------- * Dot patterns are now optional Consider the following program ```agda data Vec (A : Set) : Nat → Set where [] : Vec A zero cons : ∀ n → A → Vec A n → Vec A (suc n) vmap : ∀ {A B} n → (A → B) → Vec A n → Vec B n vmap .zero f [] = [] vmap .(suc m) f (cons m x xs) = cons m (f x) (vmap m f xs) ``` If we don't care about the dot patterns they can (and could previously) be replaced by wildcards: ```agda vmap : ∀ {A B} n → (A → B) → Vec A n → Vec B n vmap _ f [] = [] vmap _ f (cons m x xs) = cons m (f x) (vmap m f xs) ``` Now it is also allowed to give a variable pattern in place of the dot pattern. In this case the variable will be bound to the value of the dot pattern. For our example: ```agda vmap : ∀ {A B} n → (A → B) → Vec A n → Vec B n vmap n f [] = [] vmap n f (cons m x xs) = cons m (f x) (vmap m f xs) ``` In the first clause `n` reduces to `zero` and in the second clause `n` reduces to `suc m`. * Module parameters can now be refined by pattern matching Previously, pattern matches that would refine a variable outside the current left-hand side was disallowed. For instance, the following would give an error, since matching on the vector would instantiate `n`. ```agda module _ {A : Set} {n : Nat} where f : Vec A n → Vec A n f [] = [] f (x ∷ xs) = x ∷ xs ``` Now this is no longer disallowed. Instead `n` is bound to the appropriate value in each clause. * With-abstraction now abstracts also in module parameters The change that allows pattern matching to refine module parameters also allows with-abstraction to abstract in them. For instance, ```agda module _ (n : Nat) (xs : Vec Nat (n + n)) where f : Nat f with n + n f | nn = ? -- xs : Vec Nat nn ``` Note: Any function argument or lambda-bound variable bound outside a given function counts as a module parameter. To prevent abstraction in a parameter you can hide it inside a definition. In the above example, ```agda module _ (n : Nat) (xs : Vec Nat (n + n)) where ys : Vec Nat (n + n) ys = xs f : Nat f with n + n f | nn = ? -- xs : Vec Nat nn, ys : Vec Nat (n + n) ``` * As-patterns [Issue [#78](https://github.com/agda/agda/issues/78)]. As-patterns (`@`-patterns) are finally working and can be used to name a pattern. The name has the same scope as normal pattern variables (i.e. the right-hand side, where clause, and dot patterns). The name reduces to the value of the named pattern. For example:: ```agda module _ {A : Set} (_<_ : A → A → Bool) where merge : List A → List A → List A merge xs [] = xs merge [] ys = ys merge xs@(x ∷ xs₁) ys@(y ∷ ys₁) = if x < y then x ∷ merge xs₁ ys else y ∷ merge xs ys₁ ``` * Idiom brackets. There is new syntactic sugar for idiom brackets: `(| e a1 .. an |)` expands to `pure e <*> a1 <*> .. <*> an` The desugaring takes place before scope checking and only requires names `pure` and `_<*>_` in scope. Idiom brackets work well with operators, for instance `(| if a then b else c |)` desugars to `pure if_then_else_ <*> a <*> b <*> c` Limitations: - The top-level application inside idiom brackets cannot include implicit applications, so `(| foo {x = e} a b |)` is illegal. In the case `e` is pure you can write `(| (foo {x = e}) a b |)` which desugars to `pure (foo {x = e}) <*> a <*> b` - Binding syntax and operator sections cannot appear immediately inside idiom brackets. * Layout for pattern matching lambdas. You can now write pattern matching lambdas using the syntax ```agda λ where false → true true → false ``` avoiding the need for explicit curly braces and semicolons. * Overloaded projections [Issue [#1944](https://github.com/agda/agda/issues/1944)]. Ambiguous projections are no longer a scope error. Instead they get resolved based on the type of the record value they are eliminating. This corresponds to constructors, which can be overloaded and get disambiguated based on the type they are introducing. Example: ```agda module _ (A : Set) (a : A) where record R B : Set where field f : B open R public record S B : Set where field f : B open S public ``` Exporting `f` twice from both `R` and `S` is now allowed. Then, ```agda r : R A f r = a s : S A f s = f r ``` disambiguates to: ```agda r : R A R.f r = a s : S A S.f s = R.f r ``` If the type of the projection is known, it can also be disambiguated unapplied. ```agda unapplied : R A -> A unapplied = f ``` * Postfix projections [Issue [#1963](https://github.com/agda/agda/issues/1963)]. Agda now supports a postfix syntax for projection application. This style is more in harmony with copatterns. For example: ```agda record Stream (A : Set) : Set where coinductive field head : A tail : Stream A open Stream repeat : ∀{A} (a : A) → Stream A repeat a .head = a repeat a .tail = repeat a zipWith : ∀{A B C} (f : A → B → C) (s : Stream A) (t : Stream B) → Stream C zipWith f s t .head = f (s .head) (t .head) zipWith f s t .tail = zipWith f (s .tail) (t .tail) module Fib (Nat : Set) (zero one : Nat) (plus : Nat → Nat → Nat) where {-# TERMINATING #-} fib : Stream Nat fib .head = zero fib .tail .head = one fib .tail .tail = zipWith plus fib (fib .tail) ``` The thing we eliminate with projection now is visibly the head, i.e., the left-most expression of the sequence (e.g. `repeat` in `repeat a .tail`). The syntax overlaps with dot patterns, but for type correct left hand sides there is no confusion: Dot patterns eliminate function types, while (postfix) projection patterns eliminate record types. By default, Agda prints system-generated projections (such as by eta-expansion or case splitting) prefix. This can be changed with the new option: ```agda {-# OPTIONS --postfix-projections #-} ``` Result splitting in extended lambdas (aka pattern lambdas) always produces postfix projections, as prefix projection pattern do not work here: a prefix projection needs to go left of the head, but the head is omitted in extended lambdas. ```agda dup : ∀{A : Set}(a : A) → A × A dup = λ{ a → ? } ``` Result splitting (`C-c C-c RET`) here will yield: ```agda dup = λ{ a .proj₁ → ? ; a .proj₂ → ? } ``` * Projection parameters [Issue [#1954](https://github.com/agda/agda/issues/1954)]. When copying a module, projection parameters will now stay hidden arguments, even if the module parameters are visible. This matches the situation we had for constructors since long. Example: ```agda module P (A : Set) where record R : Set where field f : A open module Q A = P A ``` Parameter `A` is now hidden in `R.f`: ```agda test : ∀{A} → R A → A test r = R.f r ``` Note that a module parameter that corresponds to the record value argument of a projection will not be hidden. ```agda module M (A : Set) (r : R A) where open R A r public test' : ∀{A} → R A → A test' r = M.f r ``` * Eager insertion of implicit arguments [Issue [#2001](https://github.com/agda/agda/issues/2001)] Implicit arguments are now (again) eagerly inserted in left-hand sides. The previous behaviour of inserting implicits for where blocks, but not right-hand sides was not type safe. * Module applications can now be eta expanded/contracted without changing their behaviour [Issue #[1985](https://github.com/agda/agda/issues/1985)] Previously definitions exported using `open public` got the incorrect type for underapplied module applications. Example: ```agda module A where postulate A : Set module B (X : Set) where open A public module C₁ = B module C₂ (X : Set) = B X ``` Here both `C₁.A` and `C₂.A` have type `(X : Set) → Set`. * Polarity pragmas. Polarity pragmas can be attached to postulates. The polarities express how the postulate's arguments are used. The following polarities are available: `_`: Unused. `++`: Strictly positive. `+`: Positive. `-`: Negative. `*`: Unknown/mixed. Polarity pragmas have the form ``` {-# POLARITY name #-} ``` and can be given wherever fixity declarations can be given. The listed polarities apply to the given postulate's arguments (explicit/implicit/instance), from left to right. Polarities currently cannot be given for module parameters. If the postulate takes n arguments (excluding module parameters), then the number of polarities given must be between 0 and n (inclusive). Polarity pragmas make it possible to use postulated type formers in recursive types in the following way: ```agda postulate ∥_∥ : Set → Set {-# POLARITY ∥_∥ ++ #-} data D : Set where c : ∥ D ∥ → D ``` Note that one can use postulates that may seem benign, together with polarity pragmas, to prove that the empty type is inhabited: ```agda postulate _⇒_ : Set → Set → Set lambda : {A B : Set} → (A → B) → A ⇒ B apply : {A B : Set} → A ⇒ B → A → B {-# POLARITY _⇒_ ++ #-} data ⊥ : Set where data D : Set where c : D ⇒ ⊥ → D not-inhabited : D → ⊥ not-inhabited (c f) = apply f (c f) inhabited : D inhabited = c (lambda not-inhabited) bad : ⊥ bad = not-inhabited inhabited ``` Polarity pragmas are not allowed in safe mode. * Declarations in a `where`-block are now private. [Issue [#2101](https://github.com/agda/agda/issues/2101)] This means that ```agda f ps = body where decls ``` is now equivalent to ```agda f ps = body where private decls ``` This changes little, since the `decls` were anyway not in scope outside `body`. However, it makes a difference for abstract definitions, because private type signatures can see through abstract definitions. Consider: ```agda record Wrap (A : Set) : Set where field unwrap : A postulate P : ∀{A : Set} → A → Set abstract unnamedWhere : (A : Set) → Set unnamedWhere A = A where -- the following definitions are private! B : Set B = Wrap A postulate b : B test : P (Wrap.unwrap b) -- succeeds ``` The `abstract` is inherited in `where`-blocks from the parent (here: function `unnamedWhere`). Thus, the definition of `B` is opaque and the type equation `B = Wrap A` cannot be used to check type signatures, not even of abstract definitions. Thus, checking the type `P (Wrap.unwrap b)` would fail. However, if `test` is private, abstract definitions are translucent in its type, and checking succeeds. With the implemented change, all `where`-definitions are private, in this case `B`, `b`, and `test`, and the example succeeds. Nothing changes for the named forms of `where`, ```agda module M where module _ where ``` For instance, this still fails: ```agda abstract unnamedWhere : (A : Set) → Set unnamedWhere A = A module M where B : Set B = Wrap A postulate b : B test : P (Wrap.unwrap b) -- fails ``` * Private anonymous modules now work as expected [Issue [#2199](https://github.com/agda/agda/issues/2199)] Previously the `private` was ignored for anonymous modules causing its definitions to be visible outside the module containing the anonymous module. This is no longer the case. For instance, ```agda module M where private module _ (A : Set) where Id : Set Id = A foo : Set → Set foo = Id open M bar : Set → Set bar = Id -- Id is no longer in scope here ``` * Pattern synonyms are now expanded on left hand sides of DISPLAY pragmas [Issue [#2132](https://github.com/agda/agda/issues/2132)]. Example: ```agda data D : Set where C c : D g : D → D pattern C′ = C {-# DISPLAY C′ = C′ #-} {-# DISPLAY g C′ = c #-} ``` This now behaves as: ```agda {-# DISPLAY C = C′ #-} {-# DISPLAY g C = c #-} ``` Expected error for ```agda test : C ≡ g C test = refl ``` is thus: ``` C′ != c of type D ``` * The built-in floats have new semantics to fix inconsistencies and to improve cross-platform portability. - Float equality has been split into two primitives. ``primFloatEquality`` is designed to establish decidable propositional equality while ``primFloatNumericalEquality`` is intended for numerical computations. They behave as follows: ``` primFloatEquality NaN NaN = True primFloatEquality 0.0 -0.0 = False primFloatNumericalEquality NaN NaN = False primFloatNumericalEquality 0.0 -0.0 = True ``` This change fixes an inconsistency, see [Issue [#2169](https://github.com/agda/agda/issues/2169)]. For further detail see the [user manual](http://agda.readthedocs.io/en/latest/language/built-ins.html#floats). - Floats now have only one `NaN` value. This is necessary for proper Float support in the JavaScript backend, as JavaScript (and some other platforms) only support one `NaN` value. - The primitive function `primFloatLess` was renamed `primFloatNumericalLess`. * Added new primitives to built-in floats: - `primFloatNegate : Float → Float` [Issue [#2194](https://github.com/agda/agda/issues/2194)] - Trigonometric primitives [Issue [#2200](https://github.com/agda/agda/issues/2200)]: ```agda primCos : Float → Float primTan : Float → Float primASin : Float → Float primACos : Float → Float primATan : Float → Float primATan2 : Float → Float → Float ``` * Anonymous declarations [Issue [#1465](https://github.com/agda/agda/issues/1465)]. A module can contain an arbitrary number of declarations named `_` which will scoped-checked and type-checked but won't be made available in the scope (nor exported). They cannot introduce arguments on the LHS (but one can use lambda-abstractions on the RHS) and they cannot be defined by recursion. ```agda _ : Set → Set _ = λ x → x ``` ### Rewriting * The REWRITE pragma can now handle several names. E.g.: ```agda {-# REWRITE eq1 eq2 #-} ``` ### Reflection * You can now use macros in reflected terms [Issue [#2130](https://github.com/agda/agda/issues/2130)]. For instance, given a macro ```agda macro some-tactic : Term → TC ⊤ some-tactic = ... ``` the term `def (quote some-tactic) []` represents a call to the macro. This makes it a lot easier to compose tactics. * The reflection machinery now uses normalisation less often: * Macros no longer normalise the (automatically quoted) term arguments. * The TC primitives `inferType`, `checkType` and `quoteTC` no longer normalise their arguments. * The following deprecated constructions may also have been changed: `quoteGoal`, `quoteTerm`, `quoteContext` and `tactic`. * New TC primitive: `withNormalisation`. To recover the old normalising behaviour of `inferType`, `checkType`, `quoteTC` and `getContext`, you can wrap them inside a call to `withNormalisation true`: ```agda withNormalisation : ∀ {a} {A : Set a} → Bool → TC A → TC A ``` * New TC primitive: `reduce`. ```agda reduce : Term → TC Term ``` Reduces its argument to weak head normal form. * Added new TC primitive: `isMacro` [Issue [#2182](https://github.com/agda/agda/issues/2182)] ```agda isMacro : Name → TC Bool ``` Returns `true` if the name refers to a macro, otherwise `false`. * The `record-type` constructor now has an extra argument containing information about the record type's fields: ```agda data Definition : Set where … record-type : (c : Name) (fs : List (Arg Name)) → Definition … ``` Type checking ------------- * Files with open metas can be imported now [Issue [#964](https://github.com/agda/agda/issues/964)]. This should make simultaneous interactive development on several modules more pleasant. Requires option: `--allow-unsolved-metas` Internally, before serialization, open metas are turned into postulates named ``` unsolved#meta. ``` where `` is the internal meta variable number. * The performance of the compile-time evaluator has been greatly improved. - Fixed a memory leak in evaluator (Issue [#2147](https://github.com/agda/agda/issues/2147)). - Reduction speed improved by an order of magnitude and is now comparable to the performance of GHCi. Still call-by-name though. * The detection of types that satisfy K added in Agda 2.5.1 has been rolled back (see Issue [#2003](https://github.com/agda/agda/issues/2003)). * Eta-equality for record types is now only on after the positivity checker has confirmed it is safe to have it. Eta-equality for unguarded inductive records previously lead to looping of the type checker. [See Issue [#2197](https://github.com/agda/agda/issues/2197)] ```agda record R : Set where inductive field r : R loops : R loops = ? ``` As a consequence of this change, the following example does not type-check any more: ```agda mutual record ⊤ : Set where test : ∀ {x y : ⊤} → x ≡ y test = refl ``` It fails because the positivity checker is only run after the mutual block, thus, eta-equality for `⊤` is not available when checking test. One can declare eta-equality explicitly, though, to make this example work. ```agda mutual record ⊤ : Set where eta-equality test : ∀ {x y : ⊤} → x ≡ y test = refl ``` * Records with instance fields are now eta expanded before instance search. For instance, assuming `Eq` and `Ord` with boolean functions `_==_` and `_<_` respectively, ```agda record EqAndOrd (A : Set) : Set where field {{eq}} : Eq A {{ord}} : Ord A leq : {A : Set} {{_ : EqAndOrd A}} → A → A → Bool leq x y = x == y || x < y ``` Here the `EqAndOrd` record is automatically unpacked before instance search, revealing the component `Eq` and `Ord` instances. This can be used to simulate superclass dependencies. * Overlappable record instance fields. Instance fields in records can be marked as overlappable using the new `overlap` keyword: ```agda record Ord (A : Set) : Set where field _<_ : A → A → Bool overlap {{eqA}} : Eq A ``` When instance search finds multiple candidates for a given instance goal and they are **all** overlappable it will pick the left-most candidate instead of refusing to solve the instance goal. This can be use to solve the problem arising from shared "superclass" dependencies. For instance, if you have, in addition to `Ord` above, a `Num` record that also has an `Eq` field and want to write a function requiring both `Ord` and `Num`, any `Eq` constraint will be solved by the `Eq` instance from whichever argument that comes first. ```agda record Num (A : Set) : Set where field fromNat : Nat → A overlap {{eqA}} : Eq A lessOrEqualFive : {A : Set} {{NumA : Num A}} {{OrdA : Ord A}} → A → Bool lessOrEqualFive x = x == fromNat 5 || x < fromNat 5 ``` In this example the call to `_==_` will use the `eqA` field from `NumA` rather than the one from `OrdA`. Note that these may well be different. * Instance fields can be left out of copattern matches [Issue [#2288](https://github.com/agda/agda/issues/2288)] Missing cases for instance fields (marked `{{` `}}`) in copattern matches will be solved using instance search. This makes defining instances with superclass fields much nicer. For instance, we can define `Nat` instances of `Eq`, `Ord` and `Num` from above as follows: ```agda instance EqNat : Eq Nat _==_ {{EqNat}} n m = eqNat n m OrdNat : Ord Nat _<_ {{OrdNat}} n m = lessNat n m NumNat : Num Nat fromNat {{NumNat}} n = n ``` The `eqA` fields of `Ord` and `Num` are filled in using instance search (with `EqNat` in this case). * Limited instance search depth [Issue [#2269](https://github.com/agda/agda/issues/2269)] To prevent instance search from looping on bad instances (see [Issue #1743](https://github.com/agda/agda/issues/1743)) the search depth of instance search is now limited. The maximum depth can be set with the `--instance-search-depth` flag and the default value is `500`. Emacs mode ---------- * New command `C-u C-u C-c C-n`: Use `show` to display the result of normalisation. Calling `C-u C-u C-c C-n` on an expression `e` (in a hole or at top level) normalises `show e` and prints the resulting string, or an error message if the expression does not normalise to a literal string. This is useful when working with complex data structures for which you have defined a nice `Show` instance. Note that the name `show` is hardwired into the command. * Changed feature: Interactively split result. Make-case (`C-c C-c`) with no variables will now *either* introduce function arguments *or* do a copattern split (or fail). This is as before: ```agda test : {A B : Set} (a : A) (b : B) → A × B test a b = ? -- expected: -- proj₁ (test a b) = {!!} -- proj₂ (test a b) = {!!} testFun : {A B : Set} (a : A) (b : B) → A × B testFun = ? -- expected: -- testFun a b = {!!} ``` This is has changed: ```agda record FunRec A : Set where field funField : A → A open FunRec testFunRec : ∀{A} → FunRec A testFunRec = ? -- expected (since 2016-05-03): -- funField testFunRec = {!!} -- used to be: -- funField testFunRec x = {!!} ``` * Changed feature: Split on hidden variables. Make-case (`C-c C-c`) will no longer split on the given hidden variables, but only make them visible. (Splitting can then be performed in a second go.) ```agda test : ∀{N M : Nat} → Nat → Nat → Nat test N M = {!.N N .M!} ``` Invoking splitting will result in: ```agda test {N} {M} zero M₁ = ? test {N} {M} (suc N₁) M₁ = ? ``` The hidden `.N` and `.M` have been brought into scope, the visible `N` has been split upon. * Non-fatal errors/warnings. Non-fatal errors and warnings are now displayed in the info buffer and do not interrupt the typechecking of the file. Currently termination errors, unsolved metavariables, unsolved constraints, positivity errors, deprecated BUILTINs, and empty REWRITING pragmas are non-fatal errors. * Highlighting for positivity check failures Negative occurences of a datatype in its definition are now highlighted in a way similar to termination errors. * The abbrev for codata was replaced by an abbrev for code environments. If you type `c C-x '` (on a suitably standard setup), then Emacs will insert the following text: ```agda \begin{code} \end{code}. ``` * The LaTeX backend can now be invoked from the Emacs mode. Using the compilation command (`C-c C-x C-c`). The flag `--latex-dir` can be used to set the output directory (by default: `latex`). Note that if this directory is a relative path, then it is interpreted relative to the "project root". (When the LaTeX backend is invoked from the command line the path is interpreted relative to the current working directory.) Example: If the module `A.B.C` is located in the file `/foo/A/B/C.agda`, then the project root is `/foo/`, and the default output directory is `/foo/latex/`. * The compilation command (`C-c C-x C-c`) now by default asks for a backend. To avoid this question, set the customisation variable `agda2-backend` to an appropriate value. * The command `agda2-measure-load-time` no longer "touches" the file, and the optional argument `DONT-TOUCH` has been removed. * New command `C-u (C-u) C-c C-s`: Simplify or normalise the solution `C-c C-s` produces When writing examples, it is nice to have the hole filled in with a normalised version of the solution. Calling `C-c C-s` on ```agda _ : reverse (0 ∷ 1 ∷ []) ≡ ? _ = refl ``` used to yield the non informative `reverse (0 ∷ 1 ∷ [])` when we would have hopped to get `1 ∷ 0 ∷ []` instead. We can now control finely the degree to which the solution is simplified. * Changed feature: Solving the hole at point Calling `C-c C-s` inside a specific goal does not solve *all* the goals already instantiated internally anymore: it only solves the one at hand (if possible). * New bindings: All the blackboard bold letters are now available [Pull Request [#2305](https://github.com/agda/agda/pull/2305)] The Agda input method only bound a handful of the blackboard bold letters but programmers were actually using more than these. They are now all available: lowercase and uppercase. Some previous bindings had to be modified for consistency. The naming scheme is as follows: * `\bx` for lowercase blackboard bold * `\bX` for uppercase blackboard bold * `\bGx` for lowercase greek blackboard bold (similar to `\Gx` for greeks) * `\bGX` for uppercase greek blackboard bold (similar to `\GX` for uppercase greeks) * Replaced binding for go back Use `M-,` (instead of `M-*`) for go back in Emacs ≥ 25.1 (and continue using `M-*` with previous versions of Emacs). Compiler backends ----------------- * JS compiler backend The JavaScript backend has been (partially) rewritten. The JavaScript backend now supports most Agda features, notably copatterns can now be compiled to JavaScript. Furthermore, the existing optimizations from the other backends now apply to the JavaScript backend as well. * GHC, JS and UHC compiler backends Added new primitives to built-in floats [Issues [#2194](https://github.com/agda/agda/issues/2194) and [#2200](https://github.com/agda/agda/issues/2200)]: ```agda primFloatNegate : Float → Float primCos : Float → Float primTan : Float → Float primASin : Float → Float primACos : Float → Float primATan : Float → Float primATan2 : Float → Float → Float ``` LaTeX backend ------------- * Code blocks are now (by default) surrounded by vertical space. [Issue [#2198](https://github.com/agda/agda/issues/2198)] Use `\AgdaNoSpaceAroundCode{}` to avoid this vertical space, and `\AgdaSpaceAroundCode{}` to reenable it. Note that, if `\AgdaNoSpaceAroundCode{}` is used, then empty lines before or after a code block will not necessarily lead to empty lines in the generated document. However, empty lines *inside* the code block do (by default) lead to empty lines in the output. If you prefer the previous behaviour, then you can use the `agda.sty` file that came with the previous version of Agda. * `\AgdaHide{...}` now eats trailing spaces (using `\ignorespaces`). * New environments: `AgdaAlign`, `AgdaSuppressSpace` and `AgdaMultiCode`. Sometimes one might want to break up a code block into multiple pieces, but keep code in different blocks aligned with respect to each other. Then one can use the `AgdaAlign` environment. Example usage: ```latex \begin{AgdaAlign} \begin{code} code code (more code) \end{code} Explanation... \begin{code} aligned with "code" code (aligned with (more code)) \end{code} \end{AgdaAlign} ``` Note that `AgdaAlign` environments should not be nested. Sometimes one might also want to hide code in the middle of a code block. This can be accomplished in the following way: ```latex \begin{AgdaAlign} \begin{code} visible \end{code} \AgdaHide{ \begin{code} hidden \end{code}} \begin{code} visible \end{code} \end{AgdaAlign} ``` However, the result may be ugly: extra space is perhaps inserted around the code blocks. The `AgdaSuppressSpace` environment ensures that extra space is only inserted before the first code block, and after the last one (but not if `\AgdaNoSpaceAroundCode{}` is used). The environment takes one argument, the number of wrapped code blocks (excluding hidden ones). Example usage: ```latex \begin{AgdaAlign} \begin{code} code more code \end{code} Explanation... \begin{AgdaSuppressSpace}{2} \begin{code} aligned with "code" aligned with "more code" \end{code} \AgdaHide{ \begin{code} hidden code \end{code}} \begin{code} also aligned with "more code" \end{code} \end{AgdaSuppressSpace} \end{AgdaAlign} ``` Note that `AgdaSuppressSpace` environments should not be nested. There is also a combined environment, `AgdaMultiCode`, that combines the effects of `AgdaAlign` and `AgdaSuppressSpace`. Tools ----- ### agda-ghc-names The `agda-ghc-names` now has its own repository at https://github.com/agda/agda-ghc-names and is no longer distributed with Agda. Release notes for Agda version 2.5.1.2 ====================================== * Fixed broken type signatures that were incorrectly accepted due to [GHC #12784](https://ghc.haskell.org/trac/ghc/ticket/12784). Release notes for Agda version 2.5.1.1 ====================================== Installation and infrastructure ------------------------------- * Added support for GHC 8.0.1. * Documentation is now built with Python >=3.3, as done by [readthedocs.org](https://readthedocs.org/). Bug fixes --------- * Fixed a serious performance problem with instance search Issues [#1952](https://github.com/agda/agda/issues/1952) and [#1998](https://github.com/agda/agda/issues/1998). Also related: [#1955](https://github.com/agda/agda/issues/1955) and [#2025](https://github.com/agda/agda/issues/2025) * Interactively splitting variable with `C-c C-c` no longer introduces new trailing patterns. This fixes Issue [#1950](https://github.com/agda/agda/issues/1950). ```agda data Ty : Set where _⇒_ : Ty → Ty → Ty ⟦_⟧ : Ty → Set ⟦ A ⇒ B ⟧ = ⟦ A ⟧ → ⟦ B ⟧ data Term : Ty → Set where K : (A B : Ty) → Term (A ⇒ (B ⇒ A)) test : (A : Ty) (a : Term A) → ⟦ A ⟧ test A a = {!a!} ``` Before change, case splitting on `a` would give ```agda test .(A ⇒ (B ⇒ A)) (K A B) x x₁ = ? ``` Now, it yields ```agda test .(A ⇒ (B ⇒ A)) (K A B) = ? ``` * In literate TeX files, `\begin{code}` and `\end{code}` can be preceded (resp. followed) by TeX code on the same line. This fixes Issue [#2077](https://github.com/agda/agda/issues/2077). * Other issues fixed (see [bug tracker](https://github.com/agda/agda/issues)): [#1951](https://github.com/agda/agda/issues/1951) (mixfix binders not working in 'syntax') [#1967](https://github.com/agda/agda/issues/1967) (too eager insteance search error) [#1974](https://github.com/agda/agda/issues/1974) (lost constraint dependencies) [#1982](https://github.com/agda/agda/issues/1982) (internal error in unifier) [#2034](https://github.com/agda/agda/issues/2034) (function type instance goals) Compiler backends ----------------- * UHC compiler backend Added support for UHC 1.1.9.4. Release notes for Agda version 2.5.1 ==================================== Documentation ------------- * There is now an official Agda User Manual: http://agda.readthedocs.org/en/stable/ Installation and infrastructure ------------------------------- * Builtins and primitives are now defined in a new set of modules available to all users, independent of any particular library. The modules are ```agda Agda.Builtin.Bool Agda.Builtin.Char Agda.Builtin.Coinduction Agda.Builtin.Equality Agda.Builtin.Float Agda.Builtin.FromNat Agda.Builtin.FromNeg Agda.Builtin.FromString Agda.Builtin.IO Agda.Builtin.Int Agda.Builtin.List Agda.Builtin.Nat Agda.Builtin.Reflection Agda.Builtin.Size Agda.Builtin.Strict Agda.Builtin.String Agda.Builtin.TrustMe Agda.Builtin.Unit ``` The standard library reexports the primitives from the new modules. The `Agda.Builtin` modules are installed in the same way as `Agda.Primitive`, but unlike `Agda.Primitive` they are not loaded automatically. Pragmas and options ------------------- * Library management There is a new 'library' concept for managing include paths. A library consists of - a name, - a set of libraries it depends on, and - a set of include paths. A library is defined in a `.agda-lib` file using the following format: ``` name: LIBRARY-NAME -- Comment depend: LIB1 LIB2 LIB3 LIB4 include: PATH1 PATH2 PATH3 ``` Dependencies are library names, not paths to `.agda-lib` files, and include paths are relative to the location of the library-file. To be useable, a library file has to be listed (with its full path) in `AGDA_DIR/libraries` (or `AGDA_DIR/libraries-VERSION`, for a given Agda version). `AGDA_DIR` defaults to `~/.agda` on Unix-like systems and `C:/Users/USERNAME/AppData/Roaming/agda` or similar on Windows, and can be overridden by setting the `AGDA_DIR` environment variable. Environment variables in the paths (of the form `$VAR` or `${VAR}`) are expanded. The location of the libraries file used can be overridden using the `--library-file=FILE` flag, although this is not expected to be very useful. You can find out the precise location of the 'libraries' file by calling `agda -l fjdsk Dummy.agda` and looking at the error message (assuming you don't have a library called fjdsk installed). There are three ways a library gets used: - You supply the `--library=LIB` (or `-l LIB`) option to Agda. This is equivalent to adding a `-iPATH` for each of the include paths of `LIB` and its (transitive) dependencies. - No explicit `--library` flag is given, and the current project root (of the Agda file that is being loaded) or one of its parent directories contains a `.agda-lib` file defining a library `LIB`. This library is used as if a `--librarary=LIB` option had been given, except that it is not necessary for the library to be listed in the `AGDA_DIR/libraries` file. - No explicit `--library` flag, and no `.agda-lib` file in the project root. In this case the file `AGDA_DIR/defaults` is read and all libraries listed are added to the path. The defaults file should contain a list of library names, each on a separate line. In this case the current directory is also added to the path. To disable default libraries, you can give the flag `--no-default-libraries`. Library names can end with a version number (for instance, `mylib-1.2.3`). When resolving a library name (given in a `--library` flag, or listed as a default library or library dependency) the following rules are followed: - If you don't give a version number, any version will do. - If you give a version number an exact match is required. - When there are multiple matches an exact match is preferred, and otherwise the latest matching version is chosen. For example, suppose you have the following libraries installed: `mylib`, `mylib-1.0`, `otherlib-2.1`, and `otherlib-2.3`. In this case, aside from the exact matches you can also say `--library=otherlib` to get `otherlib-2.3`. * New Pragma `COMPILED_DECLARE_DATA` for binding recursively defined Haskell data types to recursively defined Agda data types. If you have a Haskell type like ```haskell {-# LANGUAGE GADTs #-} module Issue223 where data A where BA :: B -> A data B where AB :: A -> B BB :: B ``` You can now bind it to corresponding mutual Agda inductive data types as follows: ```agda {-# IMPORT Issue223 #-} data A : Set {-# COMPILED_DECLARE_DATA A Issue223.A #-} data B : Set {-# COMPILED_DECLARE_DATA B Issue223.B #-} data A where BA : B → A {-# COMPILED_DATA A Issue223.A Issue223.BA #-} data B where AB : A → B BB : B {-# COMPILED_DATA B Issue223.B Issue223.AB Issue223.BB #-} ``` This fixes Issue [#223](https://github.com/agda/agda/issues/223). * New pragma `HASKELL` for adding inline Haskell code (GHC backend only) Arbitrary Haskell code can be added to a module using the `HASKELL` pragma. For instance, ```agda {-# HASKELL echo :: IO () echo = getLine >>= putStrLn #-} postulate echo : IO ⊤ {-# COMPILED echo echo #-} ``` * New option `--exact-split`. The `--exact-split` flag causes Agda to raise an error whenever a clause in a definition by pattern matching cannot be made to hold definitionally (i.e. as a reduction rule). Specific clauses can be excluded from this check by means of the `{-# CATCHALL #-}` pragma. For instance, the following definition will be rejected as the second clause cannot be made to hold definitionally: ```agda min : Nat → Nat → Nat min zero y = zero min x zero = zero min (suc x) (suc y) = suc (min x y ``` Catchall clauses have to be marked as such, for instance: ```agda eq : Nat → Nat → Bool eq zero zero = true eq (suc m) (suc n) = eq m n {-# CATCHALL #-} eq _ _ = false ``` * New option: `--no-exact-split`. This option can be used to override a global `--exact-split` in a file, by adding a pragma `{-# OPTIONS --no-exact-split #-}`. * New options: `--sharing` and `--no-sharing`. These options are used to enable/disable sharing and call-by-need evaluation. The default is `--no-sharing`. Note that they cannot appear in an OPTIONS pragma, but have to be given as command line arguments or added to the Agda Program Args from Emacs with `M-x customize-group agda2`. * New pragma `DISPLAY`. ```agda {-# DISPLAY f e1 .. en = e #-} ``` This causes `f e1 .. en` to be printed in the same way as `e`, where `ei` can bind variables used in `e`. The expressions `ei` and `e` are scope checked, but not type checked. For example this can be used to print overloaded (instance) functions with the overloaded name: ```agda instance NumNat : Num Nat NumNat = record { ..; _+_ = natPlus } {-# DISPLAY natPlus a b = a + b #-} ``` Limitations - Left-hand sides are restricted to variables, constructors, defined functions or types, and literals. In particular, lambdas are not allowed in left-hand sides. - Since `DISPLAY` pragmas are not type checked implicit argument insertion may not work properly if the type of `f` computes to an implicit function space after pattern matching. * Removed pragma `{-# ETA R #-}` The pragma `{-# ETA R #-}` is replaced by the `eta-equality` directive inside record declarations. * New option `--no-eta-equality`. The `--no-eta-equality` flag disables eta rules for declared record types. It has the same effect as `no-eta-equality` inside each declaration of a record type `R`. If used with the OPTIONS pragma it will not affect records defined in other modules. * The semantics of `{-# REWRITE r #-}` pragmas in parametrized modules has changed (see Issue [#1652](https://github.com/agda/agda/issues/1652)). Rewrite rules are no longer lifted to the top context. Instead, they now only apply to terms in (extensions of) the module context. If you want the old behaviour, you should put the `{-# REWRITE r #-}` pragma outside of the module (i.e. unindent it). * New pragma `{-# INLINE f #-}` causes `f` to be inlined during compilation. * The `STATIC` pragma is now taken into account during compilation. Calls to a function marked `STATIC` are normalised before compilation. The typical use case for this is to mark the interpreter of an embedded language as `STATIC`. * Option `--type-in-type` no longer implies `--no-universe-polymorphism`, thus, it can be used with explicit universe levels. [Issue [#1764](https://github.com/agda/agda/issues/1764)] It simply turns off error reporting for any level mismatch now. Examples: ```agda {-# OPTIONS --type-in-type #-} Type : Set Type = Set data D {α} (A : Set α) : Set where d : A → D A data E α β : Set β where e : Set α → E α β ``` * New `NO_POSITIVITY_CHECK` pragma to switch off the positivity checker for data/record definitions and mutual blocks. The pragma must precede a data/record definition or a mutual block. The pragma cannot be used in `--safe` mode. Examples (see `Issue1614*.agda` and `Issue1760*.agda` in `test/Succeed/`): 1. Skipping a single data definition. ```agda {-# NO_POSITIVITY_CHECK #-} data D : Set where lam : (D → D) → D ``` 2. Skipping a single record definition. ```agda {-# NO_POSITIVITY_CHECK #-} record U : Set where field ap : U → U ``` 3. Skipping an old-style mutual block: Somewhere within a `mutual` block before a data/record definition. ```agda mutual data D : Set where lam : (D → D) → D {-# NO_POSITIVITY_CHECK #-} record U : Set where field ap : U → U ``` 4. Skipping an old-style mutual block: Before the `mutual` keyword. ```agda {-# NO_POSITIVITY_CHECK #-} mutual data D : Set where lam : (D → D) → D record U : Set where field ap : U → U ``` 5. Skipping a new-style mutual block: Anywhere before the declaration or the definition of data/record in the block. ```agda record U : Set data D : Set record U where field ap : U → U {-# NO_POSITIVITY_CHECK #-} data D where lam : (D → D) → D ``` * Removed `--no-coverage-check` option. [Issue [#1918](https://github.com/agda/agda/issues/1918)] Language -------- ### Operator syntax * The default fixity for syntax declarations has changed from -666 to 20. * Sections. Operators can be sectioned by replacing arguments with underscores. There must not be any whitespace between these underscores and the adjacent nameparts. Examples: ```agda pred : ℕ → ℕ pred = _∸ 1 T : Bool → Set T = if_then ⊤ else ⊥ if : {A : Set} (b : Bool) → A → A → A if b = if b then_else_ ``` Sections are translated into lambda expressions. Examples: ```agda _∸ 1 ↦ λ section → section ∸ 1 if_then ⊤ else ⊥ ↦ λ section → if section then ⊤ else ⊥ if b then_else_ ↦ λ section section₁ → if b then section else section₁ ``` Operator sections have the same fixity as the underlying operator (except in cases like `if b then_else_`, in which the section is "closed", but the operator is not). Operator sections are not supported in patterns (with the exception of dot patterns), and notations coming from syntax declarations cannot be sectioned. * A long-standing operator fixity bug has been fixed. As a consequence some programs that used to parse no longer do. Previously each precedence level was (incorrectly) split up into five separate ones, ordered as follows, with the earlier ones binding less tightly than the later ones: - Non-associative operators. - Left associative operators. - Right associative operators. - Prefix operators. - Postfix operators. Now this problem has been addressed. It is no longer possible to mix operators of a given precedence level but different associativity. However, prefix and right associative operators are seen as having the same associativity, and similarly for postfix and left associative operators. Examples -------- The following code is no longer accepted: ```agda infixl 6 _+_ infix 6 _∸_ rejected : ℕ rejected = 1 + 0 ∸ 1 ``` However, the following previously rejected code is accepted: ```agda infixr 4 _,_ infix 4 ,_ ,_ : {A : Set} {B : A → Set} {x : A} → B x → Σ A B , y = _ , y accepted : Σ ℕ λ i → Σ ℕ λ j → Σ (i ≡ j) λ _ → Σ ℕ λ k → j ≡ k accepted = 5 , , refl , , refl ``` * The classification of notations with binders into the categories infix, prefix, postfix or closed has changed. [Issue [#1450](https://github.com/agda/agda/issues/1450)] The difference is that, when classifying the notation, only *regular* holes are taken into account, not *binding* ones. Example: The notation ```agda syntax m >>= (λ x → f) = x <- m , f ``` was previously treated as infix, but is now treated as prefix. * Notation can now include wildcard binders. Example: `syntax Σ A (λ _ → B) = A × B` * If an overloaded operator is in scope with several distinct precedence levels, then several instances of this operator will be included in the operator grammar, possibly leading to ambiguity. Previously the operator was given the default fixity [Issue [#1436](https://github.com/agda/agda/issues/1436)]. There is an exception to this rule: If there are multiple precedences, but at most one is explicitly declared, then only one instance will be included in the grammar. If there are no explicitly declared precedences, then this instance will get the default precedence, and otherwise it will get the declared precedence. If multiple occurrences of an operator are "merged" in the grammar, and they have distinct associativities, then they are treated as being non-associative. The three paragraphs above also apply to identical notations (coming from syntax declarations) for a given overloaded name. Examples: ```agda module A where infixr 5 _∷_ infixr 5 _∙_ infixl 3 _+_ infix 1 bind syntax bind c (λ x → d) = x ← c , d module B where infix 5 _∷_ infixr 4 _∙_ -- No fixity declaration for _+_. infixl 2 bind syntax bind c d = c ∙ d module C where infixr 2 bind syntax bind c d = c ∙ d open A open B open C -- _∷_ is infix 5. -- _∙_ has two fixities: infixr 4 and infixr 5. -- _+_ is infixl 3. -- A.bind's notation is infix 1. -- B.bind and C.bind's notations are infix 2. -- There is one instance of "_ ∷ _" in the grammar, and one -- instance of "_ + _". -- There are three instances of "_ ∙ _" in the grammar, one -- corresponding to A._∙_, one corresponding to B._∙_, and one -- corresponding to both B.bind and C.bind. ``` ### Reflection * The reflection framework has received a massive overhaul. A new type of reflected type checking computations supplants most of the old reflection primitives. The `quoteGoal`, `quoteContext` and tactic primitives are deprecated and will be removed in the future, and the `unquoteDecl` and `unquote` primitives have changed behaviour. Furthermore the following primitive functions have been replaced by builtin type checking computations: ```agda - primQNameType --> AGDATCMGETTYPE - primQNameDefinition --> AGDATCMGETDEFINITION - primDataConstructors --> subsumed by AGDATCMGETDEFINITION - primDataNumberOfParameters --> subsumed by AGDATCMGETDEFINITION ``` See below for details. * Types are no longer packaged with a sort. The `AGDATYPE` and `AGDATYPEEL` built-ins have been removed. Reflected types are now simply terms. * Reflected definitions have more information. The type for reflected definitions has changed to ```agda data Definition : Set where fun-def : List Clause → Definition data-type : Nat → List Name → Definition -- parameters and constructors record-type : Name → Definition -- name of the data/record type data-con : Name → Definition -- name of the constructor axiom : Definition prim-fun : Definition ``` Correspondingly the built-ins for function, data and record definitions (`AGDAFUNDEF`, `AGDAFUNDEFCON`, `AGDADATADEF`, `AGDARECORDDEF`) have been removed. * Reflected type checking computations. There is a primitive `TC` monad representing type checking computations. The `unquote`, `unquoteDecl`, and the new `unquoteDef` all expect computations in this monad (see below). The interface to the monad is the following ```agda -- Error messages can contain embedded names and terms. data ErrorPart : Set where strErr : String → ErrorPart termErr : Term → ErrorPart nameErr : Name → ErrorPart {-# BUILTIN AGDAERRORPART ErrorPart #-} {-# BUILTIN AGDAERRORPARTSTRING strErr #-} {-# BUILTIN AGDAERRORPARTTERM termErr #-} {-# BUILTIN AGDAERRORPARTNAME nameErr #-} postulate TC : ∀ {a} → Set a → Set a returnTC : ∀ {a} {A : Set a} → A → TC A bindTC : ∀ {a b} {A : Set a} {B : Set b} → TC A → (A → TC B) → TC B -- Unify two terms, potentially solving metavariables in the process. unify : Term → Term → TC ⊤ -- Throw a type error. Can be caught by catchTC. typeError : ∀ {a} {A : Set a} → List ErrorPart → TC A -- Block a type checking computation on a metavariable. This will abort -- the computation and restart it (from the beginning) when the -- metavariable is solved. blockOnMeta : ∀ {a} {A : Set a} → Meta → TC A -- Backtrack and try the second argument if the first argument throws a -- type error. catchTC : ∀ {a} {A : Set a} → TC A → TC A → TC A -- Infer the type of a given term inferType : Term → TC Type -- Check a term against a given type. This may resolve implicit arguments -- in the term, so a new refined term is returned. Can be used to create -- new metavariables: newMeta t = checkType unknown t checkType : Term → Type → TC Term -- Compute the normal form of a term. normalise : Term → TC Term -- Get the current context. getContext : TC (List (Arg Type)) -- Extend the current context with a variable of the given type. extendContext : ∀ {a} {A : Set a} → Arg Type → TC A → TC A -- Set the current context. inContext : ∀ {a} {A : Set a} → List (Arg Type) → TC A → TC A -- Quote a value, returning the corresponding Term. quoteTC : ∀ {a} {A : Set a} → A → TC Term -- Unquote a Term, returning the corresponding value. unquoteTC : ∀ {a} {A : Set a} → Term → TC A -- Create a fresh name. freshName : String → TC QName -- Declare a new function of the given type. The function must be defined -- later using 'defineFun'. Takes an Arg Name to allow declaring instances -- and irrelevant functions. The Visibility of the Arg must not be hidden. declareDef : Arg QName → Type → TC ⊤ -- Define a declared function. The function may have been declared using -- 'declareDef' or with an explicit type signature in the program. defineFun : QName → List Clause → TC ⊤ -- Get the type of a defined name. Replaces 'primQNameType'. getType : QName → TC Type -- Get the definition of a defined name. Replaces 'primQNameDefinition'. getDefinition : QName → TC Definition {-# BUILTIN AGDATCM TC #-} {-# BUILTIN AGDATCMRETURN returnTC #-} {-# BUILTIN AGDATCMBIND bindTC #-} {-# BUILTIN AGDATCMUNIFY unify #-} {-# BUILTIN AGDATCMNEWMETA newMeta #-} {-# BUILTIN AGDATCMTYPEERROR typeError #-} {-# BUILTIN AGDATCMBLOCKONMETA blockOnMeta #-} {-# BUILTIN AGDATCMCATCHERROR catchTC #-} {-# BUILTIN AGDATCMINFERTYPE inferType #-} {-# BUILTIN AGDATCMCHECKTYPE checkType #-} {-# BUILTIN AGDATCMNORMALISE normalise #-} {-# BUILTIN AGDATCMGETCONTEXT getContext #-} {-# BUILTIN AGDATCMEXTENDCONTEXT extendContext #-} {-# BUILTIN AGDATCMINCONTEXT inContext #-} {-# BUILTIN AGDATCMQUOTETERM quoteTC #-} {-# BUILTIN AGDATCMUNQUOTETERM unquoteTC #-} {-# BUILTIN AGDATCMFRESHNAME freshName #-} {-# BUILTIN AGDATCMDECLAREDEF declareDef #-} {-# BUILTIN AGDATCMDEFINEFUN defineFun #-} {-# BUILTIN AGDATCMGETTYPE getType #-} {-# BUILTIN AGDATCMGETDEFINITION getDefinition #-} ``` * Builtin type for metavariables There is a new builtin type for metavariables used by the new reflection framework. It is declared as follows and comes with primitive equality, ordering and show. ```agda postulate Meta : Set {-# BUILTIN AGDAMETA Meta #-} primitive primMetaEquality : Meta → Meta → Bool primitive primMetaLess : Meta → Meta → Bool primitive primShowMeta : Meta → String ``` There are corresponding new constructors in the `Term` and `Literal` data types: ```agda data Term : Set where ... meta : Meta → List (Arg Term) → Term {-# BUILTIN AGDATERMMETA meta #-} data Literal : Set where ... meta : Meta → Literal {-# BUILTIN AGDALITMETA meta #-} ``` * Builtin unit type The type checker needs to know about the unit type, which you can allow by ```agda record ⊤ : Set where {-# BUILTIN UNIT ⊤ #-} ``` * Changed behaviour of `unquote` The `unquote` primitive now expects a type checking computation instead of a pure term. In particular `unquote e` requires ```agda e : Term → TC ⊤ ``` where the argument is the representation of the hole in which the result should go. The old `unquote` behaviour (where `unquote` expected a `Term` argument) can be recovered by ```agda OLD: unquote v NEW: unquote λ hole → unify hole v ``` * Changed behaviour of `unquoteDecl` The `unquoteDecl` primitive now expects a type checking computation instead of a pure function definition. It is possible to define multiple (mutually recursive) functions at the same time. More specifically ```agda unquoteDecl x₁ .. xₙ = m ``` requires `m : TC ⊤` and that `x₁ .. xₙ` are defined (using `declareDef` and `defineFun`) after executing `m`. As before `x₁ .. xₙ : QName` in `m`, but have their declared types outside the `unquoteDecl`. * New primitive `unquoteDef` There is a new declaration ```agda unquoteDef x₁ .. xₙ = m ``` This works exactly as `unquoteDecl` (see above) with the exception that `x₁ .. xₙ` are required to already be declared. The main advantage of `unquoteDef` over `unquoteDecl` is that `unquoteDef` is allowed in mutual blocks, allowing mutually recursion between generated definitions and hand-written definitions. * The reflection interface now exposes the name hint (as a string) for variables. As before, the actual binding structure is with de Bruijn indices. The String value is just a hint used as a prefix to help display the variable. The type `Abs` is a new builtin type used for the constructors `Term.lam`, `Term.pi`, `Pattern.var` (bultins `AGDATERMLAM`, `AGDATERMPI` and `AGDAPATVAR`). ```agda data Abs (A : Set) : Set where abs : (s : String) (x : A) → Abs A {-# BUILTIN ABS Abs #-} {-# BUILTIN ABSABS abs #-} ``` Updated constructor types: ```agda Term.lam : Hiding → Abs Term → Term Term.pi : Arg Type → Abs Type → Term Pattern.var : String → Pattern ``` * Reflection-based macros Macros are functions of type `t1 → t2 → .. → Term → TC ⊤` that are defined in a `macro` block. Macro application is guided by the type of the macro, where `Term` arguments desugar into the `quoteTerm` syntax and `Name` arguments into the `quote` syntax. Arguments of any other type are preserved as-is. The last `Term` argument is the hole term given to `unquote` computation (see above). For example, the macro application `f u v w` where the macro `f` has the type `Term → Name → Bool → Term → TC ⊤` desugars into `unquote (f (quoteTerm u) (quote v) w)` Limitations: - Macros cannot be recursive. This can be worked around by defining the recursive function outside the macro block and have the macro call the recursive function. Silly example: ```agda macro plus-to-times : Term → Term → TC ⊤ plus-to-times (def (quote _+_) (a ∷ b ∷ [])) hole = unify hole (def (quote _*_) (a ∷ b ∷ [])) plus-to-times v hole = unify hole v thm : (a b : Nat) → plus-to-times (a + b) ≡ a * b thm a b = refl ``` Macros are most useful when writing tactics, since they let you hide the reflection machinery. For instance, suppose you have a solver ```agda magic : Type → Term ``` that takes a reflected goal and outputs a proof (when successful). You can then define the following macro ```agda macro by-magic : Term → TC ⊤ by-magic hole = bindTC (inferType hole) λ goal → unify hole (magic goal) ``` This lets you apply the magic tactic without any syntactic noise at all: ```agda thm : ¬ P ≡ NP thm = by-magic ``` ### Literals and built-ins * Overloaded number literals. You can now overload natural number literals using the new builtin `FROMNAT`: ```agda {-# BUILTIN FROMNAT fromNat #-} ``` The target of the builtin should be a defined name. Typically you would do something like ```agda record Number (A : Set) : Set where field fromNat : Nat → A open Number {{...}} public {-# BUILTIN FROMNAT fromNat #-} ``` This will cause number literals `n` to be desugared to `fromNat n` before type checking. * Negative number literals. Number literals can now be negative. For floating point literals it works as expected. For integer literals there is a new builtin `FROMNEG` that enables negative integer literals: ```agda {-# BUILTIN FROMNEG fromNeg #-} ``` This causes negative literals `-n` to be desugared to `fromNeg n`. * Overloaded string literals. String literals can be overladed using the `FROMSTRING` builtin: ```agda {-# BUILTIN FROMSTRING fromString #-} ``` The will cause string literals `s` to be desugared to `fromString s` before type checking. * Change to builtin integers. The `INTEGER` builtin now needs to be bound to a datatype with two constructors that should be bound to the new builtins `INTEGERPOS` and `INTEGERNEGSUC` as follows: ```agda data Int : Set where pos : Nat -> Int negsuc : Nat -> Int {-# BUILTIN INTEGER Int #-} {-# BUILTIN INTEGERPOS pos #-} {-# BUILTIN INTEGERNEGSUC negsuc #-} ``` where `negsuc n` represents the integer `-n - 1`. For instance, `-5` is represented as `negsuc 4`. All primitive functions on integers except `primShowInteger` have been removed, since these can be defined without too much trouble on the above representation using the corresponding functions on natural numbers. The primitives that have been removed are ```agda primIntegerPlus primIntegerMinus primIntegerTimes primIntegerDiv primIntegerMod primIntegerEquality primIntegerLess primIntegerAbs primNatToInteger ``` * New primitives for strict evaluation ```agda primitive primForce : ∀ {a b} {A : Set a} {B : A → Set b} (x : A) → (∀ x → B x) → B x primForceLemma : ∀ {a b} {A : Set a} {B : A → Set b} (x : A) (f : ∀ x → B x) → primForce x f ≡ f x ``` `primForce x f` evaluates to `f x` if x is in weak head normal form, and `primForceLemma x f` evaluates to `refl` in the same situation. The following values are considered to be in weak head normal form: - constructor applications - literals - lambda abstractions - type constructor (data/record types) applications - function types - Set a ### Modules * Modules in import directives When you use `using`/`hiding`/`renaming` on a name it now automatically applies to any module of the same name, unless you explicitly mention the module. For instance, ```agda open M using (D) ``` is equivalent to ```agda open M using (D; module D) ``` if `M` defines a module `D`. This is most useful for record and data types where you always get a module of the same name as the type. With this feature there is no longer useful to be able to qualify a constructor (or field) by the name of the data type even when it differs from the name of the corresponding module. The follow (weird) code used to work, but doesn't work anymore: ```agda module M where data D where c : D open M using (D) renaming (module D to MD) foo : D foo = D.c ``` If you want to import only the type name and not the module you have to hide it explicitly: ```agda open M using (D) hiding (module D) ``` See discussion on Issue [#836](https://github.com/agda/agda/issues/836). * Private definitions of a module are no longer in scope at the Emacs mode top-level. The reason for this change is that `.agdai-files` are stripped of unused private definitions (which can yield significant performance improvements for module-heavy code). To test private definitions you can create a hole at the bottom of the module, in which private definitions will be visible. ### Records * New record directives `eta-equality`/`no-eta-equality` The keywords `eta-equality`/`no-eta-equality` enable/disable eta rules for the (inductive) record type being declared. ```agda record Σ (A : Set) (B : A -> Set) : Set where no-eta-equality constructor _,_ field fst : A snd : B fst open Σ -- fail : ∀ {A : Set}{B : A -> Set} → (x : Σ A B) → x ≡ (fst x , snd x) -- fail x = refl -- -- x != fst x , snd x of type Σ .A .B -- when checking that the expression refl has type x ≡ (fst x , snd x) ``` * Building records from modules. The `record { }` syntax is now extended to accept module names as well. Fields are thus defined using the corresponding definitions from the given module. For instance assuming this record type `R` and module `M`: ```agda record R : Set where field x : X y : Y z : Z module M where x = {! ... !} y = {! ... !} r : R r = record { M; z = {! ... !} } ``` Previously one had to write `record { x = M.x; y = M.y; z = {! ... !} }`. More precisely this construction now supports any combination of explicit field definitions and applied modules. If a field is both given explicitly and available in one of the modules, then the explicit one takes precedence. If a field is available in more than one module then this is ambiguous and therefore rejected. As a consequence the order of assignments does not matter. The modules can be both applied to arguments and have import directives such as `hiding`, `using`, and `renaming`. In particular this construct subsumes the record update construction. Here is an example of record update: ```agda -- Record update. Same as: record r { y = {! ... !} } r2 : R r2 = record { R r; y = {! ... !} } ``` A contrived example showing the use of `hiding`/`renaming`: ```agda module M2 (a : A) where w = {! ... !} z = {! ... !} r3 : A → R r3 a = record { M hiding (y); M2 a renaming (w to y) } ``` * Record patterns are now accepted. Examples: ```agda swap : {A B : Set} (p : A × B) → B × A swap record{ proj₁ = a; proj₂ = b } = record{ proj₁ = b; proj₂ = a } thd3 : ... thd3 record{ proj₂ = record { proj₂ = c }} = c ``` * Record modules now properly hide all their parameters [Issue [#1759](https://github.com/agda/agda/issues/1759)] Previously parameters to parent modules were not hidden in the record module, resulting in different behaviour between ```agda module M (A : Set) where record R (B : Set) : Set where ``` and ```agda module M where record R (A B : Set) : Set where ``` where in the former case, `A` would be an explicit argument to the module `M.R`, but implicit in the latter case. Now `A` is implicit in both cases. ### Instance search * Performance has been improved, recursive instance search which was previously exponential in the depth is now only quadratic. * Constructors of records and datatypes are not anymore automatically considered as instances, you have to do so explicitely, for instance: ```agda -- only [b] is an instance of D data D : Set where a : D instance b : D c : D -- the constructor is now an instance record tt : Set where instance constructor tt ``` * Lambda-bound variables are no longer automatically considered instances. Lambda-bound variables need to be bound as instance arguments to be considered for instance search. For example, ```agda _==_ : {A : Set} {{_ : Eq A}} → A → A → Bool fails : {A : Set} → Eq A → A → Bool fails eqA x = x == x works : {A : Set} {{_ : Eq A}} → A → Bool works x = x == x ``` * Let-bound variables are no longer automatically considered instances. To make a let-bound variable available as an instance it needs to be declared with the `instance` keyword, just like top-level instances. For example, ```agda mkEq : {A : Set} → (A → A → Bool) → Eq A fails : {A : Set} → (A → A → Bool) → A → Bool fails eq x = let eqA = mkEq eq in x == x works : {A : Set} → (A → A → Bool) → A → Bool works eq x = let instance eqA = mkEq eq in x == x ``` * Record fields can be declared instances. For example, ```agda record EqSet : Set₁ where field set : Set instance eq : Eq set ``` This causes the projection function `eq : (E : EqSet) → Eq (set E)` to be considered for instance search. * Instance search can now find arguments in variable types (but such candidates can only be lambda-bound variables, they can’t be declared as instances) ```agda module _ {A : Set} (P : A → Set) where postulate bla : {x : A} {{_ : P x}} → Set → Set -- Works, the instance argument is found in the context test : {x : A} {{_ : P x}} → Set → Set test B = bla B -- Still forbidden, because [P] could be instantiated later to anything instance postulate forbidden : {x : A} → P x ``` * Instance search now refuses to solve constraints with unconstrained metavariables, since this can lead to non-termination. See [Issue [#1532](https://github.com/agda/agda/issues/1523)] for an example. * Top-level instances are now only considered if they are in scope. [Issue [#1913](https://github.com/agda/agda/issues/1913)] Note that lambda-bound instances need not be in scope. ### Other changes * Unicode ellipsis character is allowed for the ellipsis token `...` in `with` expressions. * `Prop` is no longer a reserved word. Type checking ------------- * Large indices. Force constructor arguments no longer count towards the size of a datatype. For instance, the definition of equality below is accepted. ```agda data _≡_ {a} {A : Set a} : A → A → Set where refl : ∀ x → x ≡ x ``` This gets rid of the asymmetry that the version of equality which indexes only on the second argument could be small, but not the version above which indexes on both arguments. * Detection of datatypes that satisfy K (i.e. sets) Agda will now try to detect datatypes that satisfy K when `--without-K` is enabled. A datatype satisfies K when it follows these three rules: - The types of all non-recursive constructor arguments should satisfy K. - All recursive constructor arguments should be first-order. - The types of all indices should satisfy K. For example, the types `Nat`, `List Nat`, and `x ≡ x` (where `x : Nat`) are all recognized by Agda as satisfying K. * New unifier for case splitting The unifier used by Agda for case splitting has been completely rewritten. The new unifier takes a much more type-directed approach in order to avoid the problems in issues [#1406](https://github.com/agda/agda/issues/1406), [#1408](https://github.com/agda/agda/issues/1408), [#1427](https://github.com/agda/agda/issues/1427), and [#1435](https://github.com/agda/agda/issues/1435). The new unifier also has eta-equality for record types built-in. This should avoid unnecessary case splitting on record constructors and improve the performance of Agda on code that contains deeply nested record patterns (see issues [#473](https://github.com/agda/agda/issues/473), [#635](https://github.com/agda/agda/issues/635), [#1575](https://github.com/agda/agda/issues/1575), [#1603](https://github.com/agda/agda/issues/1603), [#1613](https://github.com/agda/agda/issues/1613), and [#1645](https://github.com/agda/agda/issues/1645)). In some cases, the locations of the dot patterns computed by the unifier did not correspond to the locations given by the user (see Issue [#1608](https://github.com/agda/agda/issues/1608)). This has now been fixed by adding an extra step after case splitting that checks whether the user-written patterns are compatible with the computed ones. In some rare cases, the new unifier is still too restrictive when `--without-K` is enabled because it cannot generalize over the datatype indices (yet). For example, the following code is rejected: ```agda data Bar : Set₁ where bar : Bar baz : (A : Set) → Bar data Foo : Bar → Set where foo : Foo bar test : foo ≡ foo → Set₁ test refl = Set ``` * The aggressive behaviour of `with` introduced in 2.4.2.5 has been rolled back [Issue [#1692](https://github.com/agda/agda/issues/1692)]. With no longer abstracts in the types of variables appearing in the with-expressions. [Issue [#745](https://github.com/agda/agda/issues/745)] This means that the following example no longer works: ```agda fails : (f : (x : A) → a ≡ x) (b : A) → b ≡ a fails f b with a | f b fails f b | .b | refl = f b ``` The `with` no longer abstracts the type of `f` over `a`, since `f` appears in the second with-expression `f b`. You can use a nested `with` to make this example work. This example does work again: ```agda test : ∀{A : Set}{a : A}{f : A → A} (p : f a ≡ a) → f (f a) ≡ a test p rewrite p = p ``` After `rewrite p` the goal has changed to `f a ≡ a`, but the type of `p` has not been rewritten, thus, the final `p` solves the goal. The following, which worked in 2.4.2.5, no longer works: ```agda fails : (f : (x : A) → a ≡ x) (b : A) → b ≡ a fails f b rewrite f b = f b ``` The rewrite with `f b : a ≡ b` is not applied to `f` as the latter is part of the rewrite expression `f b`. Thus, the type of `f` remains untouched, and the changed goal `b ≡ b` is not solved by `f b`. * When using `rewrite` on a term `eq` of type `lhs ≡ rhs`, the `lhs` is no longer abstracted in `rhs` [Issue [#520](https://github.com/agda/agda/issues/520)]. This means that ```agda f pats rewrite eq = body ``` is more than syntactic sugar for ```agda f pats with lhs | eq f pats | _ | refl = body ``` In particular, the following application of `rewrite` is now possible ```agda id : Bool → Bool id true = true id false = false is-id : ∀ x → x ≡ id x is-id true = refl is-id false = refl postulate P : Bool → Set b : Bool p : P (id b) proof : P b proof rewrite is-id b = p ``` Previously, this was desugared to ```agda proof with b | is-id b proof | _ | refl = p ``` which did not type check as `refl` does not have type `b ≡ id b`. Now, Agda gets the task of checking `refl : _ ≡ id b` leading to instantiation of `_` to `id b`. Compiler backends ----------------- * Major Bug Fixes: - Function clauses with different arities are now always compiled correctly by the GHC/UHC backends. (Issue [#727](https://github.com/agda/agda/issues/727)) * Co-patterns - The GHC/UHC backends now support co-patterns. (Issues [#1567](https://github.com/agda/agda/issues/1567), [#1632](https://github.com/agda/agda/issues/1632)) * Optimizations - Builtin naturals are now represented as arbitrary-precision Integers. See the user manual, section "Agda Compilers -> Optimizations" for details. * GHC Haskell backend (MAlonzo) - Pragmas Since builtin naturals are compiled to `Integer` you can no longer give a `{-# COMPILED_DATA #-}` pragma for `Nat`. The same goes for builtin booleans, integers, floats, characters and strings which are now hard-wired to appropriate Haskell types. * UHC compiler backend A new backend targeting the Utrecht Haskell Compiler (UHC) is available. It targets the UHC Core language, and it's design is inspired by the Epic backend. See the user manual, section "Agda Compilers -> UHC Backend" for installation instructions. - FFI The UHC backend has a FFI to Haskell similar to MAlonzo's. The target Haskell code also needs to be compilable using UHC, which does not support the Haskell base library version 4.*. FFI pragmas for the UHC backend are not checked in any way. If the pragmas are wrong, bad things will happen. - Imports Additional Haskell modules can be brought into scope with the `IMPORT_UHC` pragma: ```agda {-# IMPORT_UHC Data.Char #-} ``` The Haskell modules `UHC.Base` and `UHC.Agda.Builtins` are always in scope and don't need to be imported explicitly. - Datatypes Agda datatypes can be bound to Haskell datatypes as follows: Haskell: ```haskell data HsData a = HsCon1 | HsCon2 (HsData a) ``` Agda: ```agda data AgdaData (A : Set) : Set where AgdaCon1 : AgdaData A AgdaCon2 : AgdaData A -> AgdaData A {-# COMPILED_DATA_UHC AgdaData HsData HsCon1 HsCon2 #-} ``` The mapping has to cover all constructors of the used Haskell datatype, else runtime behavior is undefined! There are special reserved names to bind Agda datatypes to certain Haskell datatypes. For example, this binds an Agda datatype to Haskell's list datatype: Agda: ```agda data AgdaList (A : Set) : Set where Nil : AgdaList A Cons : A -> AgdaList A -> AgdaList A {-# COMPILED_DATA_UHC AgdaList __LIST__ __NIL__ __CONS__ #-} ``` The following "magic" datatypes are available: ``` HS Datatype | Datatype Pragma | HS Constructor | Constructor Pragma () __UNIT__ () __UNIT__ List __LIST__ (:) __CONS__ [] __NIL__ Bool __BOOL__ True __TRUE__ False __FALSE__ ``` - Functions Agda postulates can be bound to Haskell functions. Similar as in MAlonzo, all arguments of type `Set` need to be dropped before calling Haskell functions. An example calling the return function: Agda: ```agda postulate hs-return : {A : Set} -> A -> IO A {-# COMPILED_UHC hs-return (\_ -> UHC.Agda.Builtins.primReturn) #-} ``` Emacs mode and interaction -------------------------- * Module contents (`C-c C-o`) now also works for records. [See Issue [#1926](https://github.com/agda/agda/issues/1926) ] If you have an inferable expression of record type in an interaction point, you can invoke `C-c C-o` to see its fields and types. Example ```agda record R : Set where field f : A test : R → R test r = {!r!} -- C-c C-o here ``` * Less aggressive error notification. Previously Emacs could jump to the position of an error even if the type-checking process was not initiated in the current buffer. Now this no longer happens: If the type-checking process was initiated in another buffer, then the cursor is moved to the position of the error in the buffer visiting the file (if any) and in every window displaying the file, but focus should not change from one file to another. In the cases where focus does change from one file to another, one can now use the go-back functionality to return to the previous position. * Removed the `agda-include-dirs` customization parameter. Use `agda-program-args` with `-iDIR` or `-lLIB` instead, or add libraries to `~/.agda/defaults` (`C:/Users/USERNAME/AppData/Roaming/agda/defaults` or similar on Windows). See Library management, above, for more information. Tools ----- ### LaTeX-backend * The default font has been changed to XITS (which is part of TeX Live): http://www.ctan.org/tex-archive/fonts/xits/ This font is more complete with respect to Unicode. ### agda-ghc-names * New tool: The command ``` agda-ghc-names fixprof .prof ``` converts `*.prof` files obtained from profiling runs of MAlonzo-compiled code to `*.agdaIdents.prof`, with the original Agda identifiers replacing the MAlonzo-generated Haskell identifiers. For usage and more details, see `src/agda-ghc-names/README.txt`. Highlighting and textual backends --------------------------------- * Names in import directives are now highlighted and are clickable. [Issue [#1714](https://github.com/agda/agda/issues/1714)] This leads also to nicer printing in the LaTeX and html backends. Fixed issues ------------ See [bug tracker (milestone 2.5.1)](https://github.com/agda/agda/issues?q=milestone%3A2.5.1+is%3Aclosed) Release notes for Agda version 2.4.2.5 ====================================== Installation and infrastructure ------------------------------- * Added support for GHC 7.10.3. * Added `cpphs` Cabal flag Turn on/off this flag to choose cpphs/cpp as the C preprocessor. This flag is turn on by default. (This flag was added in Agda 2.4.2.1 but it was not documented) Pragmas and options ------------------- * Termination pragmas are no longer allowed inside `where` clauses [Issue [#1137](https://github.com/agda/agda/issues/1137)]. Type checking ------------- * `with`-abstraction is more aggressive, abstracts also in types of variables that are used in the `with`-expressions, unless they are also used in the types of the `with`-expressions. [Issue [#1692](https://github.com/agda/agda/issues/1692)] Example: ```agda test : (f : (x : A) → a ≡ x) (b : A) → b ≡ a test f b with a | f b test f b | .b | refl = f b ``` Previously, `with` would not abstract in types of variables that appear in the `with`-expressions, in this case, both `f` and `b`, leaving their types unchanged. Now, it tries to abstract in `f`, as only `b` appears in the types of the `with`-expressions which are `A` (of `a`) and `a ≡ b` (of `f b`). As a result, the type of `f` changes to `(x : A) → b ≡ x` and the type of the goal to `b ≡ b` (as previously). This also affects `rewrite`, which is implemented in terms of `with`. ```agda test : (f : (x : A) → a ≡ x) (b : A) → b ≡ a test f b rewrite f b = f b ``` As the new `with` is not fully backwards-compatible, some parts of your Agda developments using `with` or `rewrite` might need maintenance. Fixed issues ------------ See [bug tracker](https://github.com/agda/agda/issues) [#1407](https://github.com/agda/agda/issues/1497) [#1518](https://github.com/agda/agda/issues/1518) [#1670](https://github.com/agda/agda/issues/1670) [#1677](https://github.com/agda/agda/issues/1677) [#1698](https://github.com/agda/agda/issues/1698) [#1701](https://github.com/agda/agda/issues/1701) [#1710](https://github.com/agda/agda/issues/1710) [#1718](https://github.com/agda/agda/issues/1718) Release notes for Agda version 2.4.2.4 ====================================== Installation and infrastructure ------------------------------- * Removed support for GHC 7.4.2. Pragmas and options ------------------- * Option `--copatterns` is now on by default. To switch off parsing of copatterns, use: ```agda {-# OPTIONS --no-copatterns #-} ``` * Option `--rewriting` is now needed to use `REWRITE` pragmas and rewriting during reduction. Rewriting is not `--safe`. To use rewriting, first specify a relation symbol `R` that will later be used to add rewrite rules. A canonical candidate would be propositional equality ```agda {-# BUILTIN REWRITE _≡_ #-} ``` but any symbol `R` of type `Δ → A → A → Set i` for some `A` and `i` is accepted. Then symbols `q` can be added to rewriting provided their type is of the form `Γ → R ds l r`. This will add a rewrite rule ``` Γ ⊢ l ↦ r : A[ds/Δ] ``` to the signature, which fires whenever a term is an instance of `l`. For example, if ```agda plus0 : ∀ x → x + 0 ≡ x ``` (ideally, there is a proof for `plus0`, but it could be a postulate), then ```agda {-# REWRITE plus0 #-} ``` will prompt Agda to rewrite any well-typed term of the form `t + 0` to `t`. Some caveats: Agda accepts and applies rewrite rules naively, it is very easy to break consistency and termination of type checking. Some examples of rewrite rules that should *not* be added: ```agda refl : ∀ x → x ≡ x -- Agda loops plus-sym : ∀ x y → x + y ≡ y + x -- Agda loops absurd : true ≡ false -- Breaks consistency ``` Adding only proven equations should at least preserve consistency, but this is only a conjecture, so know what you are doing! Using rewriting, you are entering into the wilderness, where you are on your own! Language -------- * `forall` / `∀` now parses like `λ`, i.e., the following parses now [Issue [#1583](https://github.com/agda/agda/issues/1538)]: ```agda ⊤ × ∀ (B : Set) → B → B ``` * The underscore pattern `_` can now also stand for an inaccessible pattern (dot pattern). This alleviates the need for writing `._`. [Issue #[1605](https://github.com/agda/agda/issues/1605)] Instead of ```agda transVOld : ∀{A : Set} (a b c : A) → a ≡ b → b ≡ c → a ≡ c transVOld _ ._ ._ refl refl = refl ``` one can now write ```agda transVNew : ∀{A : Set} (a b c : A) → a ≡ b → b ≡ c → a ≡ c transVNew _ _ _ refl refl = refl ``` and let Agda decide where to put the dots. This was always possible by using hidden arguments ```agda transH : ∀{A : Set}{a b c : A} → a ≡ b → b ≡ c → a ≡ c transH refl refl = refl ``` which is now equivalent to ```agda transHNew : ∀{A : Set}{a b c : A} → a ≡ b → b ≡ c → a ≡ c transHNew {a = _}{b = _}{c = _} refl refl = refl ``` Before, underscore `_` stood for an unnamed variable that could not be instantiated by an inaccessible pattern. If one no wants to prevent Agda from instantiating, one needs to use a variable name other than underscore (however, in practice this situation seems unlikely). Type checking ------------- * Polarity of phantom arguments to data and record types has changed. [Issue [#1596](https://github.com/agda/agda/issues/1596)] Polarity of size arguments is Nonvariant (both monotone and antitone). Polarity of other arguments is Covariant (monotone). Both were Invariant before (neither monotone nor antitone). The following example type-checks now: ```agda open import Common.Size -- List should be monotone in both arguments -- (even when `cons' is missing). data List (i : Size) (A : Set) : Set where [] : List i A castLL : ∀{i A} → List i (List i A) → List ∞ (List ∞ A) castLL x = x -- Stream should be antitone in the first and monotone in the second argument -- (even with field `tail' missing). record Stream (i : Size) (A : Set) : Set where coinductive field head : A castSS : ∀{i A} → Stream ∞ (Stream ∞ A) → Stream i (Stream i A) castSS x = x ``` * `SIZELT` lambdas must be consistent [Issue [#1523](https://github.com/agda/agda/issues/1523), see Abel and Pientka, ICFP 2013]. When lambda-abstracting over type (`Size< size`) then `size` must be non-zero, for any valid instantiation of size variables. - The good: ```agda data Nat (i : Size) : Set where zero : ∀ (j : Size< i) → Nat i suc : ∀ (j : Size< i) → Nat j → Nat i {-# TERMINATING #-} -- This definition is fine, the termination checker is too strict at the moment. fix : ∀ {C : Size → Set} → (∀ i → (∀ (j : Size< i) → Nat j -> C j) → Nat i → C i) → ∀ i → Nat i → C i fix t i (zero j) = t i (λ (k : Size< i) → fix t k) (zero j) fix t i (suc j n) = t i (λ (k : Size< i) → fix t k) (suc j n) ``` The `λ (k : Size< i)` is fine in both cases, as context ```agda i : Size, j : Size< i ``` guarantees that `i` is non-zero. - The bad: ```agda record Stream {i : Size} (A : Set) : Set where coinductive constructor _∷ˢ_ field head : A tail : ∀ {j : Size< i} → Stream {j} A open Stream public _++ˢ_ : ∀ {i A} → List A → Stream {i} A → Stream {i} A [] ++ˢ s = s (a ∷ as) ++ˢ s = a ∷ˢ (as ++ˢ s) ``` This fails, maybe unjustified, at ```agda i : Size, s : Stream {i} A ⊢ a ∷ˢ (λ {j : Size< i} → as ++ˢ s) ``` Fixed by defining the constructor by copattern matching: ```agda record Stream {i : Size} (A : Set) : Set where coinductive field head : A tail : ∀ {j : Size< i} → Stream {j} A open Stream public _∷ˢ_ : ∀ {i A} → A → Stream {i} A → Stream {↑ i} A head (a ∷ˢ as) = a tail (a ∷ˢ as) = as _++ˢ_ : ∀ {i A} → List A → Stream {i} A → Stream {i} A [] ++ˢ s = s (a ∷ as) ++ˢ s = a ∷ˢ (as ++ˢ s) ``` - The ugly: ```agda fix : ∀ {C : Size → Set} → (∀ i → (∀ (j : Size< i) → C j) → C i) → ∀ i → C i fix t i = t i λ (j : Size< i) → fix t j ``` For `i=0`, there is no such `j` at runtime, leading to looping behavior. Interaction ----------- * Issue [#635](https://github.com/agda/agda/issues/635) has been fixed. Case splitting does not spit out implicit record patterns any more. ```agda record Cont : Set₁ where constructor _◃_ field Sh : Set Pos : Sh → Set open Cont data W (C : Cont) : Set where sup : (s : Sh C) (k : Pos C s → W C) → W C bogus : {C : Cont} → W C → Set bogus w = {!w!} ``` Case splitting on `w` yielded, since the fix of Issue [#473](https://github.com/agda/agda/issues/473), ```agda bogus {Sh ◃ Pos} (sup s k) = ? ``` Now it gives, as expected, ```agda bogus (sup s k) = ? ``` Performance ----------- * As one result of the 21st Agda Implementor's Meeting (AIM XXI), serialization of the standard library is 50% faster (time reduced by a third), without using additional disk space for the interface files. Bug fixes --------- Issues fixed (see [bug tracker](https://github.com/agda/agda/issues)): [#1546](https://github.com/agda/agda/issues/1546) (copattern matching and with-clauses) [#1560](https://github.com/agda/agda/issues/1560) (positivity checker inefficiency) [#1584](https://github.com/agda/agda/issues/1548) (let pattern with trailing implicit) Release notes for Agda version 2.4.2.3 ====================================== Installation and infrastructure ------------------------------- * Added support for GHC 7.10.1. * Removed support for GHC 7.0.4. Language -------- * `_ `is no longer a valid name for a definition. The following fails now: [Issue [#1465](https://github.com/agda/agda/issues/1465)] ```agda postulate _ : Set ``` * Typed bindings can now contain hiding information [Issue [#1391](https://github.com/agda/agda/issues/1391)]. This means you can now write ```agda assoc : (xs {ys zs} : List A) → ((xs ++ ys) ++ zs) ≡ (xs ++ (ys ++ zs)) ``` instead of the longer ```agda assoc : (xs : List A) {ys zs : List A} → ... ``` It also works with irrelevance ```agda .(xs {ys zs} : List A) → ... ``` but of course does not make sense if there is hiding information already. Thus, this is (still) a parse error: ```agda {xs {ys zs} : List A} → ... ``` * The builtins for sized types no longer need accompanying postulates. The BUILTIN pragmas for size stuff now also declare the identifiers they bind to. ```agda {-# BUILTIN SIZEUNIV SizeUniv #-} -- SizeUniv : SizeUniv {-# BUILTIN SIZE Size #-} -- Size : SizeUniv {-# BUILTIN SIZELT Size<_ #-} -- Size<_ : ..Size → SizeUniv {-# BUILTIN SIZESUC ↑_ #-} -- ↑_ : Size → Size {-# BUILTIN SIZEINF ∞ #-} -- ∞ : Size ``` `Size` and `Size<` now live in the new universe `SizeUniv`. It is forbidden to build function spaces in this universe, in order to prevent the malicious assumption of a size predecessor ```agda pred : (i : Size) → Size< i ``` [Issue [#1428](https://github.com/agda/agda/issues/1428)]. * Unambiguous notations (coming from syntax declarations) that resolve to ambiguous names are now parsed unambiguously [Issue [#1194](https://github.com/agda/agda/issues/1194)]. * If only some instances of an overloaded name have a given associated notation (coming from syntax declarations), then this name can only be resolved to the given instances of the name, not to other instances [Issue [#1194](https://github.com/agda/agda/issues/1194)]. Previously, if different instances of an overloaded name had *different* associated notations, then none of the notations could be used. Now all of them can be used. Note that notation identity does not only involve the right-hand side of the syntax declaration. For instance, the following notations are not seen as identical, because the implicit argument names are different: ```agda module A where data D : Set where c : {x y : D} → D syntax c {x = a} {y = b} = a ∙ b module B where data D : Set where c : {y x : D} → D syntax c {y = a} {x = b} = a ∙ b ``` * If an overloaded operator is in scope with at least two distinct fixities, then it gets the default fixity [Issue [#1436](https://github.com/agda/agda/issues/1436)]. Similarly, if two or more identical notations for a given overloaded name are in scope, and these notations do not all have the same fixity, then they get the default fixity. Type checking ------------- * Functions of varying arity can now have with-clauses and use rewrite. Example: ```agda NPred : Nat → Set NPred 0 = Bool NPred (suc n) = Nat → NPred n const : Bool → ∀{n} → NPred n const b {0} = b const b {suc n} m = const b {n} allOdd : ∀ n → NPred n allOdd 0 = true allOdd (suc n) m with even m ... | true = const false ... | false = allOdd n ``` * Function defined by copattern matching can now have `with`-clauses and use `rewrite`. Example: ```agda {-# OPTIONS --copatterns #-} record Stream (A : Set) : Set where coinductive constructor delay field force : A × Stream A open Stream map : ∀{A B} → (A → B) → Stream A → Stream B force (map f s) with force s ... | a , as = f a , map f as record Bisim {A B} (R : A → B → Set) (s : Stream A) (t : Stream B) : Set where coinductive constructor ~delay field ~force : let a , as = force s b , bs = force t in R a b × Bisim R as bs open Bisim SEq : ∀{A} (s t : Stream A) → Set SEq = Bisim (_≡_) -- Slightly weird definition of symmetry to demonstrate rewrite. ~sym' : ∀{A} {s t : Stream A} → SEq s t → SEq t s ~force (~sym' {s = s} {t} p) with force s | force t | ~force p ... | a , as | b , bs | r , q rewrite r = refl , ~sym' q ``` * Instances can now be defined by copattern matching. [Issue [#1413](https://github.com/agda/agda/issues/1413)] The following example extends the one in [Abel, Pientka, Thibodeau, Setzer, POPL 2013, Section 2.2]: ```agda {-# OPTIONS --copatterns #-} -- The Monad type class record Monad (M : Set → Set) : Set1 where field return : {A : Set} → A → M A _>>=_ : {A B : Set} → M A → (A → M B) → M B open Monad {{...}} -- The State newtype record State (S A : Set) : Set where field runState : S → A × S open State -- State is an instance of Monad instance stateMonad : {S : Set} → Monad (State S) runState (return {{stateMonad}} a ) s = a , s -- NEW runState (_>>=_ {{stateMonad}} m k) s₀ = -- NEW let a , s₁ = runState m s₀ in runState (k a) s₁ -- stateMonad fulfills the monad laws leftId : {A B S : Set}(a : A)(k : A → State S B) → (return a >>= k) ≡ k a leftId a k = refl rightId : {A B S : Set}(m : State S A) → (m >>= return) ≡ m rightId m = refl assoc : {A B C S : Set}(m : State S A)(k : A → State S B)(l : B → State S C) → ((m >>= k) >>= l) ≡ (m >>= λ a → k a >>= l) assoc m k l = refl ``` Emacs mode ---------- * The new menu option `Switch to another version of Agda` tries to do what it says. * Changed feature: Interactively split result. [ This is as before: ] Make-case (`C-c C-c`) with no variables given tries to split on the result to introduce projection patterns. The hole needs to be of record type, of course. ```agda test : {A B : Set} (a : A) (b : B) → A × B test a b = ? ``` Result-splitting `?` will produce the new clauses: ```agda proj₁ (test a b) = ? proj₂ (test a b) = ? ``` [ This has changed: ] If hole is of function type, `make-case` will introduce only pattern variables (as much as it can). ```agda testFun : {A B : Set} (a : A) (b : B) → A × B testFun = ? ``` Result-splitting `?` will produce the new clause: ```agda testFun a b = ? ``` A second invocation of `make-case` will then introduce projection patterns. Error messages -------------- * Agda now suggests corrections of misspelled options, e.g. ```agda {-# OPTIONS --dont-termination-check --without-k --senf-gurke #-} ``` Unrecognized options: ``` --dont-termination-check (did you mean --no-termination-check ?) --without-k (did you mean --without-K ?) --senf-gurke ``` Nothing close to `--senf-gurke`, I am afraid. Compiler backends ----------------- * The Epic backend has been removed [Issue [#1481](https://github.com/agda/agda/issues/1481)]. Bug fixes --------- * Fixed bug with `unquoteDecl` not working in instance blocks [Issue [#1491](https://github.com/agda/agda/issues/1491)]. * Other issues fixed (see [bug tracker](https://github.com/agda/agda/issues) [#1497](https://github.com/agda/agda/issues/1497) [#1500](https://github.com/agda/agda/issues/1500) Release notes for Agda version 2.4.2.2 ====================================== Bug fixes --------- * Compilation on Windows fixed. * Other issues fixed (see [bug tracker](https://github.com/agda/agda/issues)) [#1332](https://github.com/agda/agda/issues/1322) [#1353](https://github.com/agda/agda/issues/1353) [#1360](https://github.com/agda/agda/issues/1360) [#1366](https://github.com/agda/agda/issues/1366) [#1369](https://github.com/agda/agda/issues/1369) Release notes for Agda version 2.4.2.1 ====================================== Pragmas and options ------------------- * New pragma `{-# TERMINATING #-}` replacing `{-# NO_TERMINATION_CHECK #-}` Complements the existing pragma `{-# NON_TERMINATING #-}`. Skips termination check for the associated definitions and marks them as terminating. Thus, it is a replacement for `{-# NO_TERMINATION_CHECK #-}` with the same semantics. You can no longer use pragma `{-# NO_TERMINATION_CHECK #-}` to skip the termination check, but must label your definitions as either `{-# TERMINATING #-}` or `{-# NON_TERMINATING #-}` instead. Note: `{-# OPTION --no-termination-check #-}` labels all your definitions as `{-# TERMINATING #-}`, putting you in the danger zone of a loop in the type checker. Language -------- * Referring to a local variable shadowed by module opening is now an error. Previous behavior was preferring the local over the imported definitions. [Issue [#1266](https://github.com/agda/agda/issues/1266)] Note that module parameters are locals as well as variables bound by λ, dependent function type, patterns, and let. Example: ```agda module M where A = Set1 test : (A : Set) → let open M in A ``` The last `A` produces an error, since it could refer to the local variable `A` or to the definition imported from module `M`. * `with` on a variable bound by a module telescope or a pattern of a parent function is now forbidden. [Issue [#1342](https://github.com/agda/agda/issues/1342)] ```agda data Unit : Set where unit : Unit id : (A : Set) → A → A id A a = a module M (x : Unit) where dx : Unit → Unit dx unit = x g : ∀ u → x ≡ dx u g with x g | unit = id (∀ u → unit ≡ dx u) ? ``` Even though this code looks right, Agda complains about the type expression `∀ u → unit ≡ dx u`. If you ask Agda what should go there instead, it happily tells you that it wants `∀ u → unit ≡ dx u`. In fact what you do not see and Agda will never show you is that the two expressions actually differ in the invisible first argument to `dx`, which is visible only outside module `M`. What Agda wants is an invisible `unit` after `dx`, but all you can write is an invisible `x` (which is inserted behind the scenes). To avoid those kinds of paradoxes, `with` is now outlawed on module parameters. This should ensure that the invisible arguments are always exactly the module parameters. Since a `where` block is desugared as module with pattern variables of the parent clause as module parameters, the same strikes you for uses of `with` on pattern variables of the parent function. ```agda f : Unit → Unit f x = unit where dx : Unit → Unit dx unit = x g : ∀ u → x ≡ dx u g with x g | unit = id ((u : Unit) → unit ≡ dx u) ? ``` The `with` on pattern variable `x` of the parent clause `f x = unit` is outlawed now. Type checking ------------- * Termination check failure is now a proper error. We no longer continue type checking after termination check failures. Use pragmas `{-# NON_TERMINATING #-}` and `{-# NO_TERMINATION_CHECK #-}` near the offending definitions if you want to do so. Or switch off the termination checker altogether with `{-# OPTIONS --no-termination-check #-}` (at your own risk!). * (Since Agda 2.4.2): Termination checking `--without-K` restricts structural descent to arguments ending in data types or `Size`. Likewise, guardedness is only tracked when result type is data or record type. ```agda mutual data WOne : Set where wrap : FOne → WOne FOne = ⊥ → WOne noo : (X : Set) → (WOne ≡ X) → X → ⊥ noo .WOne refl (wrap f) = noo FOne iso f ``` `noo` is rejected since at type `X` the structural descent `f < wrap f` is discounted `--without-K`. ```agda data Pandora : Set where C : ∞ ⊥ → Pandora loop : (A : Set) → A ≡ Pandora → A loop .Pandora refl = C (♯ (loop ⊥ foo)) ``` `loop` is rejected since guardedness is not tracked at type `A` `--without-K`. See issues [#1023](https://github.com/agda/agda/issues/1023), [#1264](https://github.com/agda/agda/issues/1264), [#1292](https://github.com/agda/agda/issues/1292). Termination checking -------------------- * The termination checker can now recognize simple subterms in dot patterns. ```agda data Subst : (d : Nat) → Set where c₁ : ∀ {d} → Subst d → Subst d c₂ : ∀ {d₁ d₂} → Subst d₁ → Subst d₂ → Subst (suc d₁ + d₂) postulate comp : ∀ {d₁ d₂} → Subst d₁ → Subst d₂ → Subst (d₁ + d₂) lookup : ∀ d → Nat → Subst d → Set₁ lookup d zero (c₁ ρ) = Set lookup d (suc v) (c₁ ρ) = lookup d v ρ lookup .(suc d₁ + d₂) v (c₂ {d₁} {d₂} ρ σ) = lookup (d₁ + d₂) v (comp ρ σ) ``` The dot pattern here is actually normalized, so it is ```agda suc (d₁ + d₂) ``` and the corresponding recursive call argument is `(d₁ + d₂)`. In such simple cases, Agda can now recognize that the pattern is constructor applied to call argument, which is valid descent. Note however, that Agda only looks for syntactic equality when identifying subterms, since it is not allowed to normalize terms on the rhs during termination checking. Actually writing the dot pattern has no effect, this works as well, and looks pretty magical... ;-) ```agda hidden : ∀{d} → Nat → Subst d → Set₁ hidden zero (c₁ ρ) = Set hidden (suc v) (c₁ ρ) = hidden v ρ hidden v (c₂ ρ σ) = hidden v (comp ρ σ) ``` Tools ----- ### LaTeX-backend * Fixed the issue of identifiers containing operators being typeset with excessive math spacing. Bug fixes --------- * Issue [#1194](https://github.com/agda/agda/issues/1194) * Issue [#836](https://github.com/agda/agda/issues/836): Fields and constructors can be qualified by the record/data *type* as well as by their record/data module. This now works also for record/data type imported from parametrized modules: ```agda module M (_ : Set₁) where record R : Set₁ where field X : Set open M Set using (R) -- rather than using (module R) X : R → Set X = R.X ``` Release notes for Agda version 2.4.2 ==================================== Pragmas and options ------------------- * New option: `--with-K` This can be used to override a global `--without-K` in a file, by adding a pragma `{-# OPTIONS --with-K #-}`. * New pragma `{-# NON_TERMINATING #-}` This is a safer version of `NO_TERMINATION_CHECK` which doesn't treat the affected functions as terminating. This means that `NON_TERMINATING` functions do not reduce during type checking. They do reduce at run-time and when invoking `C-c C-n` at top-level (but not in a hole). Language -------- * Instance search is now more efficient and recursive (see Issue [#938](https://github.com/agda/agda/issues/938)) (but without termination check yet). A new keyword `instance` has been introduced (in the style of `abstract` and `private`) which must now be used for every definition/postulate that has to be taken into account during instance resolution. For example: ```agda record RawMonoid (A : Set) : Set where field nil : A _++_ : A -> A -> A open RawMonoid {{...}} instance rawMonoidList : {A : Set} -> RawMonoid (List A) rawMonoidList = record { nil = []; _++_ = List._++_ } rawMonoidMaybe : {A : Set} {{m : RawMonoid A}} -> RawMonoid (Maybe A) rawMonoidMaybe {A} = record { nil = nothing ; _++_ = catMaybe } where catMaybe : Maybe A -> Maybe A -> Maybe A catMaybe nothing mb = mb catMaybe ma nothing = ma catMaybe (just a) (just b) = just (a ++ b) ``` Moreover, each type of an instance must end in (something that reduces to) a named type (e.g. a record, a datatype or a postulate). This allows us to build a simple index structure ``` data/record name --> possible instances ``` that speeds up instance search. Instance search takes into account all local bindings and all global `instance` bindings and the search is recursive. For instance, searching for ```agda ? : RawMonoid (Maybe (List A)) ``` will consider the candidates {`rawMonoidList`, `rawMonoidMaybe`}, fail to unify the first one, succeeding with the second one ```agda ? = rawMonoidMaybe {A = List A} {{m = ?m}} : RawMonoid (Maybe (List A)) ``` and continue with goal ```agda ?m : RawMonoid (List A) ``` This will then find ```agda ?m = rawMonoidList {A = A} ``` and putting together we have the solution. Be careful that there is no termination check for now, you can easily make Agda loop by declaring the identity function as an instance. But it shouldn’t be possible to make Agda loop by only declaring structurally recursive instances (whatever that means). Additionally: - Uniqueness of instances is up to definitional equality (see Issue [#899](https://github.com/agda/agda/issues/899)). - Instances of the following form are allowed: ```agda EqSigma : {A : Set} {B : A → Set} {{EqA : Eq A}} {{EqB : {a : A} → Eq (B a)}} → Eq (Σ A B) ``` When searching recursively for an instance of type `{a : A} → Eq (B a)`, a lambda will automatically be introduced and instance search will search for something of type `Eq (B a)` in the context extended by `a : A`. When searching for an instance, the `a` argument does not have to be implicit, but in the definition of `EqSigma`, instance search will only be able to use `EqB` if `a` is implicit. - There is no longer any attempt to solve irrelevant metas by instance search. - Constructors of records and datatypes are automatically added to the instance table. * You can now use `quote` in patterns. For instance, here is a function that unquotes a (closed) natural number term. ```agda unquoteNat : Term → Maybe Nat unquoteNat (con (quote Nat.zero) []) = just zero unquoteNat (con (quote Nat.suc) (arg _ n ∷ [])) = fmap suc (unquoteNat n) unquoteNat _ = nothing ``` * The builtin constructors `AGDATERMUNSUPPORTED` and `AGDASORTUNSUPPORTED` are now translated to meta variables when unquoting. * New syntactic sugar `tactic e` and `tactic e | e1 | .. | en`. It desugars as follows and makes it less unwieldy to call reflection-based tactics. ```agda tactic e --> quoteGoal g in unquote (e g) tactic e | e1 | .. | en --> quoteGoal g in unquote (e g) e1 .. en ``` Note that in the second form the tactic function should generate a function from a number of new subgoals to the original goal. The type of `e` should be `Term -> Term` in both cases. * New reflection builtins for literals. The term data type `AGDATERM` now needs an additional constructor `AGDATERMLIT` taking a reflected literal defined as follows (with appropriate builtin bindings for the types `Nat`, `Float`, etc). ```agda data Literal : Set where nat : Nat → Literal float : Float → Literal char : Char → Literal string : String → Literal qname : QName → Literal {-# BUILTIN AGDALITERAL Literal #-} {-# BUILTIN AGDALITNAT nat #-} {-# BUILTIN AGDALITFLOAT float #-} {-# BUILTIN AGDALITCHAR char #-} {-# BUILTIN AGDALITSTRING string #-} {-# BUILTIN AGDALITQNAME qname #-} ``` When quoting (`quoteGoal` or `quoteTerm`) literals will be mapped to the `AGDATERMLIT` constructor. Previously natural number literals were quoted to `suc`/`zero` application and other literals were quoted to `AGDATERMUNSUPPORTED`. * New reflection builtins for function definitions. `AGDAFUNDEF` should now map to a data type defined as follows (with ```agda {-# BUILTIN QNAME QName #-} {-# BUILTIN ARG Arg #-} {-# BUILTIN AGDATERM Term #-} {-# BUILTIN AGDATYPE Type #-} {-# BUILTIN AGDALITERAL Literal #-} ``` ). ```agda data Pattern : Set where con : QName → List (Arg Pattern) → Pattern dot : Pattern var : Pattern lit : Literal → Pattern proj : QName → Pattern absurd : Pattern {-# BUILTIN AGDAPATTERN Pattern #-} {-# BUILTIN AGDAPATCON con #-} {-# BUILTIN AGDAPATDOT dot #-} {-# BUILTIN AGDAPATVAR var #-} {-# BUILTIN AGDAPATLIT lit #-} {-# BUILTIN AGDAPATPROJ proj #-} {-# BUILTIN AGDAPATABSURD absurd #-} data Clause : Set where clause : List (Arg Pattern) → Term → Clause absurd-clause : List (Arg Pattern) → Clause {-# BUILTIN AGDACLAUSE Clause #-} {-# BUILTIN AGDACLAUSECLAUSE clause #-} {-# BUILTIN AGDACLAUSEABSURD absurd-clause #-} data FunDef : Set where fun-def : Type → List Clause → FunDef {-# BUILTIN AGDAFUNDEF FunDef #-} {-# BUILTIN AGDAFUNDEFCON fun-def #-} ``` * New reflection builtins for extended (pattern-matching) lambda. The `AGDATERM` data type has been augmented with a constructor ```agda AGDATERMEXTLAM : List AGDACLAUSE → List (ARG AGDATERM) → AGDATERM ``` Absurd lambdas (`λ ()`) are quoted to extended lambdas with an absurd clause. * Unquoting declarations. You can now define (recursive) functions by reflection using the new `unquoteDecl` declaration ```agda unquoteDecl x = e ``` Here e should have type `AGDAFUNDEF` and evaluate to a closed value. This value is then spliced in as the definition of `x`. In the body `e`, `x` has type `QNAME` which lets you splice in recursive definitions. Standard modifiers, such as fixity declarations, can be applied to `x` as expected. * Quoted levels Universe levels are now quoted properly instead of being quoted to `AGDASORTUNSUPPORTED`. `Setω` still gets an unsupported sort, however. * Module applicants can now be operator applications. Example: ```agda postulate [_] : A -> B module M (b : B) where module N (a : A) = M [ a ] ``` [See Issue [#1245](https://github.com/agda/agda/issues/1245)] * Minor change in module application semantics. [Issue [#892](https://github.com/agda/agda/issues/892)] Previously re-exported functions were not redefined when instantiating a module. For instance ```agda module A where f = ... module B (X : Set) where open A public module C = B Nat ``` In this example `C.f` would be an alias for `A.f`, so if both `A` and `C` were opened `f` would not be ambiguous. However, this behaviour is not correct when `A` and `B` share some module parameters (Issue [#892](https://github.com/agda/agda/issues/892)). To fix this `C` now defines its own copy of `f` (which evaluates to `A.f`), which means that opening `A` and `C` results in an ambiguous `f`. Type checking ------------- * Recursive records need to be declared as either `inductive` or `coinductive`. `inductive` is no longer default for recursive records. Examples: ```agda record _×_ (A B : Set) : Set where constructor _,_ field fst : A snd : B record Tree (A : Set) : Set where inductive constructor tree field elem : A subtrees : List (Tree A) record Stream (A : Set) : Set where coinductive constructor _::_ field head : A tail : Stream A ``` If you are using old-style (musical) coinduction, a record may have to be declared as inductive, paradoxically. ```agda record Stream (A : Set) : Set where inductive -- YES, THIS IS INTENDED ! constructor _∷_ field head : A tail : ∞ (Stream A) ``` This is because the "coinduction" happens in the use of `∞` and not in the use of `record`. Tools ----- ### Emacs mode * A new menu option `Display` can be used to display the version of the running Agda process. ### LaTeX-backend * New experimental option `references` has been added. When specified, i.e.: ```latex \usepackage[references]{agda} ``` a new command called `\AgdaRef` is provided, which lets you reference previously typeset commands, e.g.: Let us postulate `\AgdaRef{apa}`. ```agda \begin{code} postulate apa : Set \end{code} ``` Above `apa` will be typeset (highlighted) the same in the text as in the code, provided that the LaTeX output is post-processed using `src/data/postprocess-latex.pl`, e.g.: ``` cp $(dirname $(dirname $(agda-mode locate)))/postprocess-latex.pl . agda -i. --latex Example.lagda cd latex/ perl ../postprocess-latex.pl Example.tex > Example.processed mv Example.processed Example.tex xelatex Example.tex ``` Mix-fix and Unicode should work as expected (Unicode requires XeLaTeX/LuaLaTeX), but there are limitations: - Overloading identifiers should be avoided, if multiples exist `\AgdaRef` will typeset according to the first it finds. - Only the current module is used, should you need to reference identifiers in other modules then you need to specify which other module manually, i.e. `\AgdaRef[module]{identifier}`. Release notes for Agda 2 version 2.4.0.2 ======================================== * The Agda input mode now supports alphabetical super and subscripts, in addition to the numerical ones that were already present. [Issue [#1240](https://github.com/agda/agda/issues/1240)] * New feature: Interactively split result. Make case (`C-c C-c`) with no variables given tries to split on the result to introduce projection patterns. The hole needs to be of record type, of course. ```agda test : {A B : Set} (a : A) (b : B) → A × B test a b = ? ``` Result-splitting `?` will produce the new clauses: ```agda proj₁ (test a b) = ? proj₂ (test a b) = ? ``` If hole is of function type ending in a record type, the necessary pattern variables will be introduced before the split. Thus, the same result can be obtained by starting from: ```agda test : {A B : Set} (a : A) (b : B) → A × B test = ? ``` * The so far undocumented `ETA` pragma now throws an error if applied to definitions that are not records. `ETA` can be used to force eta-equality at recursive record types, for which eta is not enabled automatically by Agda. Here is such an example: ```agda mutual data Colist (A : Set) : Set where [] : Colist A _∷_ : A → ∞Colist A → Colist A record ∞Colist (A : Set) : Set where coinductive constructor delay field force : Colist A open ∞Colist {-# ETA ∞Colist #-} test : {A : Set} (x : ∞Colist A) → x ≡ delay (force x) test x = refl ``` Note: Unsafe use of `ETA` can make Agda loop, e.g. by triggering infinite eta expansion! * Bugs fixed (see [bug tracker](https://github.com/agda/agda/issues)): [#1203](https://github.com/agda/agda/issues/1203) [#1205](https://github.com/agda/agda/issues/1205) [#1209](https://github.com/agda/agda/issues/1209) [#1213](https://github.com/agda/agda/issues/1213) [#1214](https://github.com/agda/agda/issues/1214) [#1216](https://github.com/agda/agda/issues/1216) [#1225](https://github.com/agda/agda/issues/1225) [#1226](https://github.com/agda/agda/issues/1226) [#1231](https://github.com/agda/agda/issues/1231) [#1233](https://github.com/agda/agda/issues/1233) [#1239](https://github.com/agda/agda/issues/1239) [#1241](https://github.com/agda/agda/issues/1241) [#1243](https://github.com/agda/agda/issues/1243) Release notes for Agda 2 version 2.4.0.1 ======================================== * The option `--compile-no-main` has been renamed to `--no-main`. * `COMPILED_DATA` pragmas can now be given for records. * Various bug fixes. Release notes for Agda 2 version 2.4.0 ====================================== Installation and infrastructure ------------------------------- * A new module called `Agda.Primitive` has been introduced. This module is available to all users, even if the standard library is not used. Currently the module contains level primitives and their representation in Haskell when compiling with MAlonzo: ```agda infixl 6 _⊔_ postulate Level : Set lzero : Level lsuc : (ℓ : Level) → Level _⊔_ : (ℓ₁ ℓ₂ : Level) → Level {-# COMPILED_TYPE Level () #-} {-# COMPILED lzero () #-} {-# COMPILED lsuc (\_ -> ()) #-} {-# COMPILED _⊔_ (\_ _ -> ()) #-} {-# BUILTIN LEVEL Level #-} {-# BUILTIN LEVELZERO lzero #-} {-# BUILTIN LEVELSUC lsuc #-} {-# BUILTIN LEVELMAX _⊔_ #-} ``` To bring these declarations into scope you can use a declaration like the following one: ```agda open import Agda.Primitive using (Level; lzero; lsuc; _⊔_) ``` The standard library reexports these primitives (using the names `zero` and `suc` instead of `lzero` and `lsuc`) from the `Level` module. Existing developments using universe polymorphism might now trigger the following error message: ``` Duplicate binding for built-in thing LEVEL, previous binding to .Agda.Primitive.Level ``` To fix this problem, please remove the duplicate bindings. Technical details (perhaps relevant to those who build Agda packages): The include path now always contains a directory `/lib/prim`, and this directory is supposed to contain a subdirectory Agda containing a file `Primitive.agda`. The standard location of `` is system- and installation-specific. E.g., in a Cabal `--user` installation of Agda-2.3.4 on a standard single-ghc Linux system it would be `$HOME/.cabal/share/Agda-2.3.4` or something similar. The location of the `` directory can be configured at compile-time using Cabal flags (`--datadir` and `--datasubdir`). The location can also be set at run-time, using the `Agda_datadir` environment variable. Pragmas and options ------------------- * Pragma `NO_TERMINATION_CHECK` placed within a mutual block is now applied to the whole mutual block (rather than being discarded silently). Adding to the uses 1.-4. outlined in the release notes for 2.3.2 we allow: 3a. Skipping an old-style mutual block: Somewhere within `mutual` block before a type signature or first function clause. ```agda mutual {-# NO_TERMINATION_CHECK #-} c : A c = d d : A d = c ``` * New option `--no-pattern-matching` Disables all forms of pattern matching (for the current file). You can still import files that use pattern matching. * New option `-v profile:7` Prints some stats on which phases Agda spends how much time. (Number might not be very reliable, due to garbage collection interruptions, and maybe due to laziness of Haskell.) * New option `--no-sized-types` Option `--sized-types` is now default. `--no-sized-types` will turn off an extra (inexpensive) analysis on data types used for subtyping of sized types. Language -------- * Experimental feature: `quoteContext` There is a new keyword `quoteContext` that gives users access to the list of names in the current local context. For instance: ```agda open import Data.Nat open import Data.List open import Reflection foo : ℕ → ℕ → ℕ foo 0 m = 0 foo (suc n) m = quoteContext xs in ? ``` In the remaining goal, the list `xs` will consist of two names, `n` and `m`, corresponding to the two local variables. At the moment it is not possible to access let bound variables (this feature may be added in the future). * Experimental feature: Varying arity. Function clauses may now have different arity, e.g., ```agda Sum : ℕ → Set Sum 0 = ℕ Sum (suc n) = ℕ → Sum n sum : (n : ℕ) → ℕ → Sum n sum 0 acc = acc sum (suc n) acc m = sum n (m + acc) ``` or, ```agda T : Bool → Set T true = Bool T false = Bool → Bool f : (b : Bool) → T b f false true = false f false false = true f true = true ``` This feature is experimental. Yet unsupported: - Varying arity and `with`. - Compilation of functions with varying arity to Haskell, JS, or Epic. * Experimental feature: copatterns. (Activated with option `--copatterns`) We can now define a record by explaining what happens if you project the record. For instance: ```agda {-# OPTIONS --copatterns #-} record _×_ (A B : Set) : Set where constructor _,_ field fst : A snd : B open _×_ pair : {A B : Set} → A → B → A × B fst (pair a b) = a snd (pair a b) = b swap : {A B : Set} → A × B → B × A fst (swap p) = snd p snd (swap p) = fst p swap3 : {A B C : Set} → A × (B × C) → C × (B × A) fst (swap3 t) = snd (snd t) fst (snd (swap3 t)) = fst (snd t) snd (snd (swap3 t)) = fst t ``` Taking a projection on the left hand side (lhs) is called a projection pattern, applying to a pattern is called an application pattern. (Alternative terms: projection/application copattern.) In the first example, the symbol `pair`, if applied to variable patterns `a` and `b` and then projected via `fst`, reduces to `a`. `pair` by itself does not reduce. A typical application are coinductive records such as streams: ```agda record Stream (A : Set) : Set where coinductive field head : A tail : Stream A open Stream repeat : {A : Set} (a : A) -> Stream A head (repeat a) = a tail (repeat a) = repeat a ``` Again, `repeat a` by itself will not reduce, but you can take a projection (head or tail) and then it will reduce to the respective rhs. This way, we get the lazy reduction behavior necessary to avoid looping corecursive programs. Application patterns do not need to be trivial (i.e., variable patterns), if we mix with projection patterns. E.g., we can have ```agda nats : Nat -> Stream Nat head (nats zero) = zero tail (nats zero) = nats zero head (nats (suc x)) = x tail (nats (suc x)) = nats x ``` Here is an example (not involving coinduction) which demostrates records with fields of function type: ```agda -- The State monad record State (S A : Set) : Set where constructor state field runState : S → A × S open State -- The Monad type class record Monad (M : Set → Set) : Set1 where constructor monad field return : {A : Set} → A → M A _>>=_ : {A B : Set} → M A → (A → M B) → M B -- State is an instance of Monad -- Demonstrates the interleaving of projection and application patterns stateMonad : {S : Set} → Monad (State S) runState (Monad.return stateMonad a ) s = a , s runState (Monad._>>=_ stateMonad m k) s₀ = let a , s₁ = runState m s₀ in runState (k a) s₁ module MonadLawsForState {S : Set} where open Monad (stateMonad {S}) leftId : {A B : Set}(a : A)(k : A → State S B) → (return a >>= k) ≡ k a leftId a k = refl rightId : {A B : Set}(m : State S A) → (m >>= return) ≡ m rightId m = refl assoc : {A B C : Set}(m : State S A)(k : A → State S B)(l : B → State S C) → ((m >>= k) >>= l) ≡ (m >>= λ a → (k a >>= l)) assoc m k l = refl ``` Copatterns are yet experimental and the following does not work: - Copatterns and `with` clauses. - Compilation of copatterns to Haskell, JS, or Epic. - Projections generated by ```agda open R {{...}} ``` are not handled properly on lhss yet. - Conversion checking is slower in the presence of copatterns, since stuck definitions of record type do no longer count as neutral, since they can become unstuck by applying a projection. Thus, comparing two neutrals currently requires comparing all they projections, which repeats a lot of work. * Top-level module no longer required. The top-level module can be omitted from an Agda file. The module name is then inferred from the file name by dropping the path and the `.agda` extension. So, a module defined in `/A/B/C.agda` would get the name `C`. You can also suppress only the module name of the top-level module by writing ```agda module _ where ``` This works also for parameterised modules. * Module parameters are now always hidden arguments in projections. For instance: ```agda module M (A : Set) where record Prod (B : Set) : Set where constructor _,_ field fst : A snd : B open Prod public open M ``` Now, the types of `fst` and `snd` are ```agda fst : {A : Set}{B : Set} → Prod A B → A snd : {A : Set}{B : Set} → Prod A B → B ``` Until 2.3.2, they were ```agda fst : (A : Set){B : Set} → Prod A B → A snd : (A : Set){B : Set} → Prod A B → B ``` This change is a step towards symmetry of constructors and projections. (Constructors always took the module parameters as hidden arguments). * Telescoping lets: Local bindings are now accepted in telescopes of modules, function types, and lambda-abstractions. The syntax of telescopes as been extended to support `let`: ```agda id : (let ★ = Set) (A : ★) → A → A id A x = x ``` In particular one can now `open` modules inside telescopes: ```agda module Star where ★ : Set₁ ★ = Set module MEndo (let open Star) (A : ★) where Endo : ★ Endo = A → A ``` Finally a shortcut is provided for opening modules: ```agda module N (open Star) (A : ★) (open MEndo A) (f : Endo) where ... ``` The semantics of the latter is ```agda module _ where open Star module _ (A : ★) where open MEndo A module N (f : Endo) where ... ``` The semantics of telescoping lets in function types and lambda abstractions is just expanding them into ordinary lets. * More liberal left-hand sides in lets [Issue [#1028](https://github.com/agda/agda/issues/1028)]: You can now write left-hand sides with arguments also for let bindings without a type signature. For instance, ```agda let f x = suc x in f zero ``` Let bound functions still can't do pattern matching though. * Ambiguous names in patterns are now optimistically resolved in favor of constructors. [Issue [#822](https://github.com/agda/agda/issues/822)] In particular, the following succeeds now: ```agda module M where data D : Set₁ where [_] : Set → D postulate [_] : Set → Set open M Foo : _ → Set Foo [ A ] = A ``` * Anonymous `where`-modules are opened public. [Issue [#848](https://github.com/agda/agda/issues/848)] ``` f args = rhs module _ telescope where body ``` means the following (not proper Agda code, since you cannot put a module in-between clauses) ``` module _ {arg-telescope} telescope where body f args = rhs ``` Example: ```agda A : Set1 A = B module _ where B : Set1 B = Set C : Set1 C = B ``` * Builtin `ZERO` and `SUC` have been merged with `NATURAL`. When binding the `NATURAL` builtin, `ZERO` and `SUC` are bound to the appropriate constructors automatically. This means that instead of writing ```agda {-# BUILTIN NATURAL Nat #-} {-# BUILTIN ZERO zero #-} {-# BUILTIN SUC suc #-} ``` you just write ```agda {-# BUILTIN NATURAL Nat #-} ``` * Pattern synonym can now have implicit arguments. [Issue [#860](https://github.com/agda/agda/issues/860)] For example, ```agda pattern tail=_ {x} xs = x ∷ xs len : ∀ {A} → List A → Nat len [] = 0 len (tail= xs) = 1 + len xs ``` * Syntax declarations can now have implicit arguments. [Issue [#400](https://github.com/agda/agda/issues/400)] For example ```agda id : ∀ {a}{A : Set a} -> A -> A id x = x syntax id {A} x = x ∈ A ``` * Minor syntax changes - `-}` is now parsed as end-comment even if no comment was begun. As a consequence, the following definition gives a parse error ```agda f : {A- : Set} -> Set f {A-} = A- ``` because Agda now sees `ID(f) LBRACE ID(A) END-COMMENT`, and no longer `ID(f) LBRACE ID(A-) RBRACE`. The rational is that the previous lexing was to context-sensitive, attempting to comment-out `f` using `{-` and `-}` lead to a parse error. - Fixities (binding strengths) can now be negative numbers as well. [Issue [#1109](https://github.com/agda/agda/issues/1109)] ```agda infix -1 _myop_ ``` - Postulates are now allowed in mutual blocks. [Issue [#977](https://github.com/agda/agda/issues/977)] - Empty where blocks are now allowed. [Issue [#947](https://github.com/agda/agda/issues/947)] - Pattern synonyms are now allowed in parameterised modules. [Issue [#941](https://github.com/agda/agda/issues/941)] - Empty hiding and renaming lists in module directives are now allowed. - Module directives `using`, `hiding`, `renaming` and `public` can now appear in arbitrary order. Multiple `using`/`hiding`/`renaming` directives are allowed, but you still cannot have both using and `hiding` (because that doesn't make sense). [Issue [#493](https://github.com/agda/agda/issues/493)] Goal and error display ---------------------- * The error message `Refuse to construct infinite term` has been removed, instead one gets unsolved meta variables. Reason: the error was thrown over-eagerly. [Issue [#795](https://github.com/agda/agda/issues/795)] * If an interactive case split fails with message ``` Since goal is solved, further case distinction is not supported; try `Solve constraints' instead ``` then the associated interaction meta is assigned to a solution. Press `C-c C-=` (Show constraints) to view the solution and `C-c C-s` (Solve constraints) to apply it. [Issue [#289](https://github.com/agda/agda/issues/289)] Type checking ------------- * [ Issue [#376](https://github.com/agda/agda/issues/376) ] Implemented expansion of bound record variables during meta assignment. Now Agda can solve for metas X that are applied to projected variables, e.g.: ```agda X (fst z) (snd z) = z X (fst z) = fst z ``` Technically, this is realized by substituting `(x , y)` for `z` with fresh bound variables `x` and `y`. Here the full code for the examples: ```agda record Sigma (A : Set)(B : A -> Set) : Set where constructor _,_ field fst : A snd : B fst open Sigma test : (A : Set) (B : A -> Set) -> let X : (x : A) (y : B x) -> Sigma A B X = _ in (z : Sigma A B) -> X (fst z) (snd z) ≡ z test A B z = refl test' : (A : Set) (B : A -> Set) -> let X : A -> A X = _ in (z : Sigma A B) -> X (fst z) ≡ fst z test' A B z = refl ``` The fresh bound variables are named `fst(z)` and `snd(z)` and can appear in error messages, e.g.: ```agda fail : (A : Set) (B : A -> Set) -> let X : A -> Sigma A B X = _ in (z : Sigma A B) -> X (fst z) ≡ z fail A B z = refl ``` results in error: ``` Cannot instantiate the metavariable _7 to solution fst(z) , snd(z) since it contains the variable snd(z) which is not in scope of the metavariable or irrelevant in the metavariable but relevant in the solution when checking that the expression refl has type _7 A B (fst z) ≡ z ``` * Dependent record types and definitions by copatterns require reduction with previous function clauses while checking the current clause. [Issue [#907](https://github.com/agda/agda/issues/907)] For a simple example, consider ```agda test : ∀ {A} → Σ Nat λ n → Vec A n proj₁ test = zero proj₂ test = [] ``` For the second clause, the lhs and rhs are typed as ```agda proj₂ test : Vec A (proj₁ test) [] : Vec A zero ``` In order for these types to match, we have to reduce the lhs type with the first function clause. Note that termination checking comes after type checking, so be careful to avoid non-termination! Otherwise, the type checker might get into an infinite loop. * The implementation of the primitive `primTrustMe` has changed. It now only reduces to `REFL` if the two arguments `x` and `y` have the same computational normal form. Before, it reduced when `x` and `y` were definitionally equal, which included type-directed equality laws such as eta-equality. Yet because reduction is untyped, calling conversion from reduction lead to Agda crashes [Issue [#882](https://github.com/agda/agda/issues/882)]. The amended description of `primTrustMe` is (cf. release notes for 2.2.6): ```agda primTrustMe : {A : Set} {x y : A} → x ≡ y ``` Here `_≡_` is the builtin equality (see BUILTIN hooks for equality, above). If `x` and `y` have the same computational normal form, then `primTrustMe {x = x} {y = y}` reduces to `refl`. A note on `primTrustMe`'s runtime behavior: The MAlonzo compiler replaces all uses of `primTrustMe` with the `REFL` builtin, without any check for definitional equality. Incorrect uses of `primTrustMe` can potentially lead to segfaults or similar problems of the compiled code. * Implicit patterns of record type are now only eta-expanded if there is a record constructor. [Issues [#473](https://github.com/agda/agda/issues/473), [#635](https://github.com/agda/agda/issues/635)] ```agda data D : Set where d : D data P : D → Set where p : P d record Rc : Set where constructor c field f : D works : {r : Rc} → P (Rc.f r) → Set works p = D ``` This works since the implicit pattern `r` is eta-expanded to `c x` which allows the type of `p` to reduce to `P x` and `x` to be unified with `d`. The corresponding explicit version is: ```agda works' : (r : Rc) → P (Rc.f r) → Set works' (c .d) p = D ``` However, if the record constructor is removed, the same example will fail: ```agda record R : Set where field f : D fails : {r : R} → P (R.f r) → Set fails p = D -- d != R.f r of type D -- when checking that the pattern p has type P (R.f r) ``` The error is justified since there is no pattern we could write down for `r`. It would have to look like ```agda record { f = .d } ``` but anonymous record patterns are not part of the language. * Absurd lambdas at different source locations are no longer different. [Issue [#857](https://github.com/agda/agda/issues/857)] In particular, the following code type-checks now: ```agda absurd-equality : _≡_ {A = ⊥ → ⊥} (λ()) λ() absurd-equality = refl ``` Which is a good thing! * Printing of named implicit function types. When printing terms in a context with bound variables Agda renames new bindings to avoid clashes with the previously bound names. For instance, if `A` is in scope, the type `(A : Set) → A` is printed as `(A₁ : Set) → A₁`. However, for implicit function types the name of the binding matters, since it can be used when giving implicit arguments. For this situation, the following new syntax has been introduced: `{x = y : A} → B` is an implicit function type whose bound variable (in scope in `B`) is `y`, but where the name of the argument is `x` for the purposes of giving it explicitly. For instance, with `A` in scope, the type `{A : Set} → A` is now printed as `{A = A₁ : Set} → A₁`. This syntax is only used when printing and is currently not being parsed. * Changed the semantics of `--without-K`. [Issue [#712](https://github.com/agda/agda/issues/712), Issue [#865](https://github.com/agda/agda/issues/865), Issue [#1025](https://github.com/agda/agda/issues/1025)] New specification of `--without-K`: When `--without-K` is enabled, the unification of indices for pattern matching is restricted in two ways: 1. Reflexive equations of the form `x == x` are no longer solved, instead Agda gives an error when such an equation is encountered. 2. When unifying two same-headed constructor forms `c us` and `c vs` of type `D pars ixs`, the datatype indices `ixs` (but not the parameters) have to be *self-unifiable*, i.e. unification of `ixs` with itself should succeed positively. This is a nontrivial requirement because of point 1. Examples: - The J rule is accepted. ```agda J : {A : Set} (P : {x y : A} → x ≡ y → Set) → (∀ x → P (refl x)) → ∀ {x y} (x≡y : x ≡ y) → P x≡y J P p (refl x) = p x ```agda This definition is accepted since unification of `x` with `y` doesn't require deletion or injectivity. - The K rule is rejected. ```agda K : {A : Set} (P : {x : A} → x ≡ x → Set) → (∀ x → P (refl {x = x})) → ∀ {x} (x≡x : x ≡ x) → P x≡x K P p refl = p _ ``` Definition is rejected with the following error: ``` Cannot eliminate reflexive equation x = x of type A because K has been disabled. when checking that the pattern refl has type x ≡ x ``` - Symmetry of the new criterion. ```agda test₁ : {k l m : ℕ} → k + l ≡ m → ℕ test₁ refl = zero test₂ : {k l m : ℕ} → k ≡ l + m → ℕ test₂ refl = zero ``` Both versions are now accepted (previously only the first one was). - Handling of parameters. ```agda cons-injective : {A : Set} (x y : A) → (x ∷ []) ≡ (y ∷ []) → x ≡ y cons-injective x .x refl = refl ``` Parameters are not unified, so they are ignored by the new criterion. - A larger example: antisymmetry of ≤. ```agda data _≤_ : ℕ → ℕ → Set where lz : (n : ℕ) → zero ≤ n ls : (m n : ℕ) → m ≤ n → suc m ≤ suc n ≤-antisym : (m n : ℕ) → m ≤ n → n ≤ m → m ≡ n ≤-antisym .zero .zero (lz .zero) (lz .zero) = refl ≤-antisym .(suc m) .(suc n) (ls m n p) (ls .n .m q) = cong suc (≤-antisym m n p q) ``` - [ Issue [#1025](https://github.com/agda/agda/issues/1025) ] ```agda postulate mySpace : Set postulate myPoint : mySpace data Foo : myPoint ≡ myPoint → Set where foo : Foo refl test : (i : foo ≡ foo) → i ≡ refl test refl = {!!} ``` When applying injectivity to the equation `foo ≡ foo` of type `Foo refl`, it is checked that the index `refl` of type `myPoint ≡ myPoint` is self-unifiable. The equation `refl ≡ refl` again requires injectivity, so now the index `myPoint` is checked for self-unifiability, hence the error: ``` Cannot eliminate reflexive equation myPoint = myPoint of type mySpace because K has been disabled. when checking that the pattern refl has type foo ≡ foo ``` Termination checking -------------------- * A buggy facility coined "matrix-shaped orders" that supported uncurried functions (which take tuples of arguments instead of one argument after another) has been removed from the termination checker. [Issue [#787](https://github.com/agda/agda/issues/787)] * Definitions which fail the termination checker are not unfolded any longer to avoid loops or stack overflows in Agda. However, the termination checker for a mutual block is only invoked after type-checking, so there can still be loops if you define a non-terminating function. But termination checking now happens before the other supplementary checks: positivity, polarity, injectivity and projection-likeness. Note that with the pragma `{-# NO_TERMINATION_CHECK #-}` you can make Agda treat any function as terminating. * Termination checking of functions defined by `with` has been improved. Cases which previously required `--termination-depth` to pass the termination checker (due to use of `with`) no longer need the flag. For example ```agda merge : List A → List A → List A merge [] ys = ys merge xs [] = xs merge (x ∷ xs) (y ∷ ys) with x ≤ y merge (x ∷ xs) (y ∷ ys) | false = y ∷ merge (x ∷ xs) ys merge (x ∷ xs) (y ∷ ys) | true = x ∷ merge xs (y ∷ ys) ``` This failed to termination check previously, since the `with` expands to an auxiliary function `merge-aux`: ```agda merge-aux x y xs ys false = y ∷ merge (x ∷ xs) ys merge-aux x y xs ys true = x ∷ merge xs (y ∷ ys) ``` This function makes a call to `merge` in which the size of one of the arguments is increasing. To make this pass the termination checker now inlines the definition of `merge-aux` before checking, thus effectively termination checking the original source program. As a result of this transformation doing `with` on a variable no longer preserves termination. For instance, this does not termination check: ```agda bad : Nat → Nat bad n with n ... | zero = zero ... | suc m = bad m ``` * The performance of the termination checker has been improved. For higher `--termination-depth` the improvement is significant. While the default `--termination-depth` is still 1, checking with higher `--termination-depth` should now be feasible. Compiler backends ----------------- * The MAlonzo compiler backend now has support for compiling modules that are not full programs (i.e. don't have a main function). The goal is that you can write part of a program in Agda and the rest in Haskell, and invoke the Agda functions from the Haskell code. The following features were added for this reason: - A new command-line option `--compile-no-main`: the command ``` agda --compile-no-main Test.agda ``` will compile `Test.agda` and all its dependencies to Haskell and compile the resulting Haskell files with `--make`, but (unlike `--compile`) not tell GHC to treat `Test.hs` as the main module. This type of compilation can be invoked from Emacs by customizing the `agda2-backend` variable to value `MAlonzoNoMain` and then calling `C-c C-x C-c` as before. - A new pragma `COMPILED_EXPORT` was added as part of the MAlonzo FFI. If we have an Agda file containing the following: ```agda module A.B where test : SomeType test = someImplementation {-# COMPILED_EXPORT test someHaskellId #-} ``` then test will be compiled to a Haskell function called `someHaskellId` in module `MAlonzo.Code.A.B` that can be invoked from other Haskell code. Its type will be translated according to the normal MAlonzo rules. Tools ----- ### Emacs mode * A new goal command `Helper Function Type` (`C-c C-h`) has been added. If you write an application of an undefined function in a goal, the `Helper Function Type` command will print the type that the function needs to have in order for it to fit the goal. The type is also added to the Emacs kill-ring and can be pasted into the buffer using `C-y`. The application must be of the form `f args` where `f` is the name of the helper function you want to create. The arguments can use all the normal features like named implicits or instance arguments. Example: Here's a start on a naive reverse on vectors: ```agda reverse : ∀ {A n} → Vec A n → Vec A n reverse [] = [] reverse (x ∷ xs) = {!snoc (reverse xs) x!} ``` Calling `C-c C-h` in the goal prints ```agda snoc : ∀ {A} {n} → Vec A n → A → Vec A (suc n) ``` * A new command `Explain why a particular name is in scope` (`C-c C-w`) has been added. [Issue [#207](https://github.com/agda/agda/issues/207)] This command can be called from a goal or from the top-level and will as the name suggests explain why a particular name is in scope. For each definition or module that the given name can refer to a trace is printed of all open statements and module applications leading back to the original definition of the name. For example, given ```agda module A (X : Set₁) where data Foo : Set where mkFoo : Foo module B (Y : Set₁) where open A Y public module C = B Set open C ``` Calling `C-c C-w` on `mkFoo` at the top-level prints ``` mkFoo is in scope as * a constructor Issue207.C._.Foo.mkFoo brought into scope by - the opening of C at Issue207.agda:13,6-7 - the application of B at Issue207.agda:11,12-13 - the application of A at Issue207.agda:9,8-9 - its definition at Issue207.agda:6,5-10 ``` This command is useful if Agda complains about an ambiguous name and you need to figure out how to hide the undesired interpretations. * Improvements to the `make case` command (`C-c C-c`) - One can now also split on hidden variables, using the name (starting with `.`) with which they are printed. Use `C-c C-`, to see all variables in context. - Concerning the printing of generated clauses: * Uses named implicit arguments to improve readability. * Picks explicit occurrences over implicit ones when there is a choice of binding site for a variable. * Avoids binding variables in implicit positions by replacing dot patterns that uses them by wildcards (`._`). * Key bindings for lots of "mathematical" characters (examples: 𝐴𝑨𝒜𝓐𝔄) have been added to the Agda input method. Example: type `\MiA\MIA\McA\MCA\MfA` to get 𝐴𝑨𝒜𝓐𝔄. Note: `\McB` does not exist in Unicode (as well as others in that style), but the `\MC` (bold) alphabet is complete. * Key bindings for "blackboard bold" B (𝔹) and 0-9 (𝟘-𝟡) have been added to the Agda input method (`\bb` and `\b[0-9]`). * Key bindings for controlling simplification/normalisation: Commands like `Goal type and context` (`C-c C-,`) could previously be invoked in two ways. By default the output was normalised, but if a prefix argument was used (for instance via `C-u C-c C-,`), then no explicit normalisation was performed. Now there are three options: - By default (`C-c C-,`) the output is simplified. - If `C-u` is used exactly once (`C-u C-c C-,`), then the result is neither (explicitly) normalised nor simplified. - If `C-u` is used twice (`C-u C-u C-c C-,`), then the result is normalised. ### LaTeX-backend * Two new color scheme options were added to `agda.sty`: `\usepackage[bw]{agda}`, which highlights in black and white; `\usepackage[conor]{agda}`, which highlights using Conor's colors. The default (no options passed) is to use the standard colors. * If `agda.sty` cannot be found by the LateX environment, it is now copied into the LateX output directory (`latex` by default) instead of the working directory. This means that the commands needed to produce a PDF now is ``` agda --latex -i . .lagda cd latex pdflatex .tex ``` * The LaTeX-backend has been made more tool agnostic, in particular XeLaTeX and LuaLaTeX should now work. Here is a small example (`test/LaTeXAndHTML/succeed/UnicodeInput.lagda`): ```latex \documentclass{article} \usepackage{agda} \begin{document} \begin{code} data αβγδεζθικλμνξρστυφχψω : Set₁ where postulate →⇒⇛⇉⇄↦⇨↠⇀⇁ : Set \end{code} \[ ∀X [ ∅ ∉ X ⇒ ∃f:X ⟶ ⋃ X\ ∀A ∈ X (f(A) ∈ A) ] \] \end{document} ``` Compiled as follows, it should produce a nice looking PDF (tested with TeX Live 2012): ``` agda --latex .lagda cd latex xelatex .tex (or lualatex .tex) ``` If symbols are missing or XeLaTeX/LuaLaTeX complains about the font missing, try setting a different font using: ```latex \setmathfont{} ``` Use the `fc-list` tool to list available fonts. * Add experimental support for hyperlinks to identifiers If the `hyperref` LateX package is loaded before the Agda package and the links option is passed to the Agda package, then the Agda package provides a function called `\AgdaTarget`. Identifiers which have been declared targets, by the user, will become clickable hyperlinks in the rest of the document. Here is a small example (`test/LaTeXAndHTML/succeed/Links.lagda`): ```latex \documentclass{article} \usepackage{hyperref} \usepackage[links]{agda} \begin{document} \AgdaTarget{ℕ} \AgdaTarget{zero} \begin{code} data ℕ : Set where zero : ℕ suc : ℕ → ℕ \end{code} See next page for how to define \AgdaFunction{two} (doesn't turn into a link because the target hasn't been defined yet). We could do it manually though; \hyperlink{two}{\AgdaDatatype{two}}. \newpage \AgdaTarget{two} \hypertarget{two}{} \begin{code} two : ℕ two = suc (suc zero) \end{code} \AgdaInductiveConstructor{zero} is of type \AgdaDatatype{ℕ}. \AgdaInductiveConstructor{suc} has not been defined to be a target so it doesn't turn into a link. \newpage Now that the target for \AgdaFunction{two} has been defined the link works automatically. \begin{code} data Bool : Set where true false : Bool \end{code} The AgdaTarget command takes a list as input, enabling several targets to be specified as follows: \AgdaTarget{if, then, else, if\_then\_else\_} \begin{code} if_then_else_ : {A : Set} → Bool → A → A → A if true then t else f = t if false then t else f = f \end{code} \newpage Mixfix identifier need their underscores escaped: \AgdaFunction{if\_then\_else\_}. \end{document} ``` The boarders around the links can be suppressed using hyperref's hidelinks option: ```latex \usepackage[hidelinks]{hyperref} ``` Note that the current approach to links does not keep track of scoping or types, and hence overloaded names might create links which point to the wrong place. Therefore it is recommended to not overload names when using the links option at the moment, this might get fixed in the future. Release notes for Agda 2 version 2.3.2.2 ======================================== * Fixed a bug that sometimes made it tricky to use the Emacs mode on Windows [Issue [#757](https://github.com/agda/agda/issues/757)]. * Made Agda build with newer versions of some libraries. * Fixed a bug that caused ambiguous parse error messages [Issue [#147](https://github.com/agda/agda/issues/147)]. Release notes for Agda 2 version 2.3.2.1 ======================================== Installation ------------ * Made it possible to compile Agda with more recent versions of hashable, QuickCheck and Win32. * Excluded mtl-2.1. Type checking ------------- * Fixed bug in the termination checker (Issue [#754](https://github.com/agda/agda/issues/754)). Release notes for Agda 2 version 2.3.2 ====================================== Installation ------------ * The Agda-executable package has been removed. The executable is now provided as part of the Agda package. * The Emacs mode no longer depends on haskell-mode or GHCi. * Compilation of Emacs mode Lisp files. You can now compile the Emacs mode Lisp files by running `agda-mode compile`. This command is run by `make install`. Compilation can, in some cases, give a noticeable speedup. WARNING: If you reinstall the Agda mode without recompiling the Emacs Lisp files, then Emacs may continue using the old, compiled files. Pragmas and options ------------------- * The `--without-K` check now reconstructs constructor parameters. New specification of `--without-K`: If the flag is activated, then Agda only accepts certain case-splits. If the type of the variable to be split is `D pars ixs`, where `D` is a data (or record) type, `pars` stands for the parameters, and `ixs` the indices, then the following requirements must be satisfied: - The indices `ixs` must be applications of constructors (or literals) to distinct variables. Constructors are usually not applied to parameters, but for the purposes of this check constructor parameters are treated as other arguments. - These distinct variables must not be free in pars. * Irrelevant arguments are printed as `_` by default now. To turn on printing of irrelevant arguments, use option ``` --show-irrelevant ``` * New: Pragma `NO_TERMINATION_CHECK` to switch off termination checker for individual function definitions and mutual blocks. The pragma must precede a function definition or a mutual block. Examples (see `test/Succeed/NoTerminationCheck.agda`): 1. Skipping a single definition: before type signature. ```agda {-# NO_TERMINATION_CHECK #-} a : A a = a ``` 2. Skipping a single definition: before first clause. ```agda b : A {-# NO_TERMINATION_CHECK #-} b = b ``` 3. Skipping an old-style mutual block: Before `mutual` keyword. ```agda {-# NO_TERMINATION_CHECK #-} mutual c : A c = d d : A d = c ``` 4. Skipping a new-style mutual block: Anywhere before a type signature or first function clause in the block ```agda i : A j : A i = j {-# NO_TERMINATION_CHECK #-} j = i ``` The pragma cannot be used in `--safe` mode. Language -------- * Let binding record patterns ```agda record _×_ (A B : Set) : Set where constructor _,_ field fst : A snd : B open _×_ let (x , (y , z)) = t in u ``` will now be interpreted as ```agda let x = fst t y = fst (snd t) z = snd (snd t) in u ``` Note that the type of `t` needs to be inferable. If you need to provide a type signature, you can write the following: ```agda let a : ... a = t (x , (y , z)) = a in u ``` * Pattern synonyms A pattern synonym is a declaration that can be used on the left hand side (when pattern matching) as well as the right hand side (in expressions). For example: ```agda pattern z = zero pattern ss x = suc (suc x) f : ℕ -> ℕ f z = z f (suc z) = ss z f (ss n) = n ``` Pattern synonyms are implemented by substitution on the abstract syntax, so definitions are scope-checked but not type-checked. They are particularly useful for universe constructions. * Qualified mixfix operators It is now possible to use a qualified mixfix operator by qualifying the first part of the name. For instance ```agda import Data.Nat as Nat import Data.Bool as Bool two = Bool.if true then 1 Nat.+ 1 else 0 ``` * Sections [Issue [#735](https://github.com/agda/agda/issues/735)]. Agda now parses anonymous modules as sections: ```agda module _ {a} (A : Set a) where data List : Set a where [] : List _∷_ : (x : A) (xs : List) → List module _ {a} {A : Set a} where _++_ : List A → List A → List A [] ++ ys = ys (x ∷ xs) ++ ys = x ∷ (xs ++ ys) test : List Nat test = (5 ∷ []) ++ (3 ∷ []) ``` In general, now the syntax ```agda module _ parameters where declarations ``` is accepted and has the same effect as ```agda private module M parameters where declarations open M public ``` for a fresh name `M`. * Instantiating a module in an open import statement [Issue [#481](https://github.com/agda/agda/issues/481)]. Now accepted: ```agda open import Path.Module args [using/hiding/renaming (...)] ``` This only brings the imported identifiers from `Path.Module` into scope, not the module itself! Consequently, the following is pointless, and raises an error: ```agda import Path.Module args [using/hiding/renaming (...)] ``` You can give a private name `M` to the instantiated module via ```agda import Path.Module args as M [using/hiding/renaming (...)] open import Path.Module args as M [using/hiding/renaming (...)] ``` Try to avoid `as` as part of the arguments. `as` is not a keyword; the following can be legal, although slightly obfuscated Agda code: ```agda open import as as as as as as ``` * Implicit module parameters can be given by name. E.g. ```agda open M {namedArg = bla} ``` This feature has been introduced in Agda 2.3.0 already. * Multiple type signatures sharing a same type can now be written as a single type signature. ```agda one two : ℕ one = suc zero two = suc one ``` Goal and error display ---------------------- * Meta-variables that were introduced by hidden argument `arg` are now printed as `_arg_number` instead of just `_number`. [Issue [#526](https://github.com/agda/agda/issues/526)] * Agda expands identifiers in anonymous modules when printing. Should make some goals nicer to read. [Issue [#721](https://github.com/agda/agda/issues/721)] * When a module identifier is ambiguous, Agda tells you if one of them is a data type module. [Issues [#318](https://github.com/agda/agda/issues/318), [#705](https://github.com/agda/agda/issues/705)] Type checking ------------- * Improved coverage checker. The coverage checker splits on arguments that have constructor or literal pattern, committing to the left-most split that makes progress. Consider the lookup function for vectors: ```agda data Fin : Nat → Set where zero : {n : Nat} → Fin (suc n) suc : {n : Nat} → Fin n → Fin (suc n) data Vec (A : Set) : Nat → Set where [] : Vec A zero _∷_ : {n : Nat} → A → Vec A n → Vec A (suc n) _!!_ : {A : Set}{n : Nat} → Vec A n → Fin n → A (x ∷ xs) !! zero = x (x ∷ xs) !! suc i = xs !! i ``` In Agda up to 2.3.0, this definition is rejected unless we add an absurd clause ```agda [] !! () ``` This is because the coverage checker committed on splitting on the vector argument, even though this inevitably lead to failed coverage, because a case for the empty vector `[]` is missing. The improvement to the coverage checker consists on committing only on splits that have a chance of covering, since all possible constructor patterns are present. Thus, Agda will now split first on the `Fin` argument, since cases for both `zero` and `suc` are present. Then, it can split on the `Vec` argument, since the empty vector is already ruled out by instantiating `n` to a `suc _`. * Instance arguments resolution will now consider candidates which still expect hidden arguments. For example: ```agda record Eq (A : Set) : Set where field eq : A → A → Bool open Eq {{...}} eqFin : {n : ℕ} → Eq (Fin n) eqFin = record { eq = primEqFin } testFin : Bool testFin = eq fin1 fin2 ``` The type-checker will now resolve the instance argument of the `eq` function to `eqFin {_}`. This is only done for hidden arguments, not instance arguments, so that the instance search stays non-recursive. * Constraint solving: Upgraded Miller patterns to record patterns. [Issue [#456](https://github.com/agda/agda/issues/456)] Agda now solves meta-variables that are applied to record patterns. A typical (but here, artificial) case is: ```agda record Sigma (A : Set)(B : A -> Set) : Set where constructor _,_ field fst : A snd : B fst test : (A : Set)(B : A -> Set) -> let X : Sigma A B -> Sigma A B X = _ in (x : A)(y : B x) -> X (x , y) ≡ (x , y) test A B x y = refl ``` This yields a constraint of the form ``` _X A B (x , y) := t[x,y] ``` (with `t[x,y] = (x, y)`) which is not a Miller pattern. However, Agda now solves this as ``` _X A B z := t[fst z,snd z]. ``` * Changed: solving recursive constraints. [Issue [#585](https://github.com/agda/agda/issues/585)] Until 2.3.0, Agda sometimes inferred values that did not pass the termination checker later, or would even make Agda loop. To prevent this, the occurs check now also looks into the definitions of the current mutual block, to avoid constructing recursive solutions. As a consequence, also terminating recursive solutions are no longer found automatically. This effects a programming pattern where the recursively computed type of a recursive function is left to Agda to solve. ```agda mutual T : D -> Set T pattern1 = _ T pattern2 = _ f : (d : D) -> T d f pattern1 = rhs1 f pattern2 = rhs2 ``` This might no longer work from now on. See examples `test/Fail/Issue585*.agda`. * Less eager introduction of implicit parameters. [Issue [#679](https://github.com/agda/agda/issues/679)] Until Agda 2.3.0, trailing hidden parameters were introduced eagerly on the left hand side of a definition. For instance, one could not write ```agda test : {A : Set} -> Set test = \ {A} -> A ``` because internally, the hidden argument `{A : Set}` was added to the left-hand side, yielding ```agda test {_} = \ {A} -> A ``` which raised a type error. Now, Agda only introduces the trailing implicit parameters it has to, in order to maintain uniform function arity. For instance, in ```agda test : Bool -> {A B C : Set} -> Set test true {A} = A test false {B = B} = B ``` Agda will introduce parameters `A` and `B` in all clauses, but not `C`, resulting in ```agda test : Bool -> {A B C : Set} -> Set test true {A} {_} = A test false {_} {B = B} = B ``` Note that for checking `where`-clauses, still all hidden trailing parameters are in scope. For instance: ```agda id : {i : Level}{A : Set i} -> A -> A id = myId where myId : forall {A} -> A -> A myId x = x ``` To be able to fill in the meta variable `_1` in ```agda myId : {A : Set _1} -> A -> A ``` the hidden parameter `{i : Level}` needs to be in scope. As a result of this more lazy introduction of implicit parameters, the following code now passes. ```agda data Unit : Set where unit : Unit T : Unit → Set T unit = {u : Unit} → Unit test : (u : Unit) → T u test unit with unit ... | _ = λ {v} → v ``` Before, Agda would eagerly introduce the hidden parameter `{v}` as unnamed left-hand side parameter, leaving no way to refer to it. The related Issue [#655](https://github.com/agda/agda/issues/655) has also been addressed. It is now possible to make `synonym' definitions ``` name = expression ``` even when the type of expression begins with a hidden quantifier. Simple example: ``` id2 = id ``` That resulted in unsolved metas until 2.3.0. * Agda detects unused arguments and ignores them during equality checking. [Issue [#691](https://github.com/agda/agda/issues/691), solves also Issue [#44](https://github.com/agda/agda/issues/44)] Agda's polarity checker now assigns 'Nonvariant' to arguments that are not actually used (except for absurd matches). If `f`'s first argument is Nonvariant, then `f x` is definitionally equal to `f y` regardless of `x` and `y`. It is similar to irrelevance, but does not require user annotation. For instance, unused module parameters do no longer get in the way: ```agda module M (x : Bool) where not : Bool → Bool not true = false not false = true open M true open M false renaming (not to not′) test : (y : Bool) → not y ≡ not′ y test y = refl ``` Matching against record or absurd patterns does not count as `use', so we get some form of proof irrelevance: ```agda data ⊥ : Set where record ⊤ : Set where constructor trivial data Bool : Set where true false : Bool True : Bool → Set True true = ⊤ True false = ⊥ fun : (b : Bool) → True b → Bool fun true trivial = true fun false () test : (b : Bool) → (x y : True b) → fun b x ≡ fun b y test b x y = refl ``` More examples in `test/Succeed/NonvariantPolarity.agda`. Phantom arguments: Parameters of record and data types are considered `used' even if they are not actually used. Consider: ```agda False : Nat → Set False zero = ⊥ False (suc n) = False n module Invariant where record Bla (n : Nat)(p : False n) : Set where module Nonvariant where Bla : (n : Nat) → False n → Set Bla n p = ⊤ ``` Even though record `Bla` does not use its parameters `n` and `p`, they are considered as used, allowing "phantom type" techniques. In contrast, the arguments of function `Bla` are recognized as unused. The following code type-checks if we open `Invariant` but leaves unsolved metas if we open `Nonvariant`. ```agda drop-suc : {n : Nat}{p : False n} → Bla (suc n) p → Bla n p drop-suc _ = _ bla : (n : Nat) → {p : False n} → Bla n p → ⊥ bla zero {()} b bla (suc n) b = bla n (drop-suc b) ``` If `Bla` is considered invariant, the hidden argument in the recursive call can be inferred to be `p`. If it is considered non-variant, then `Bla n X = Bla n p` does not entail `X = p` and the hidden argument remains unsolved. Since `bla` does not actually use its hidden argument, its value is not important and it could be searched for. Unfortunately, polarity analysis of `bla` happens only after type checking, thus, the information that `bla` is non-variant in `p` is not available yet when meta-variables are solved. (See `test/Fail/BrokenInferenceDueToNonvariantPolarity.agda`) * Agda now expands simple definitions (one clause, terminating) to check whether a function is constructor headed. [Issue [#747](https://github.com/agda/agda/issues/747)] For instance, the following now also works: ```agda MyPair : Set -> Set -> Set MyPair A B = Pair A B Vec : Set -> Nat -> Set Vec A zero = Unit Vec A (suc n) = MyPair A (Vec A n) ``` Here, `Unit` and `Pair` are data or record types. Compiler backends ----------------- * `-Werror` is now overridable. To enable compilation of Haskell modules containing warnings, the `-Werror` flag for the MAlonzo backend has been made overridable. If, for example, `--ghc-flag=-Wwarn` is passed when compiling, one can get away with things like: ```agda data PartialBool : Set where true : PartialBool {-# COMPILED_DATA PartialBool Bool True #-} ``` The default behavior remains as it used to be and rejects the above program. Tools ----- ### Emacs mode * Asynchronous Emacs mode. One can now use Emacs while a buffer is type-checked. If the buffer is edited while the type-checker runs, then syntax highlighting will not be updated when type-checking is complete. * Interactive syntax highlighting. The syntax highlighting is updated while a buffer is type-checked: - At first the buffer is highlighted in a somewhat crude way (without go-to-definition information for overloaded constructors). - If the highlighting level is "interactive", then the piece of code that is currently being type-checked is highlighted as such. (The default is "non-interactive".) - When a mutual block has been type-checked it is highlighted properly (this highlighting includes warnings for potential non-termination). The highlighting level can be controlled via the new configuration variable `agda2-highlight-level`. * Multiple case-splits can now be performed in one go. Consider the following example: ```agda _==_ : Bool → Bool → Bool b₁ == b₂ = {!!} ``` If you split on `b₁ b₂`, then you get the following code: ```agda _==_ : Bool → Bool → Bool true == true = {!!} true == false = {!!} false == true = {!!} false == false = {!!} ``` The order of the variables matters. Consider the following code: ```agda lookup : ∀ {a n} {A : Set a} → Vec A n → Fin n → A lookup xs i = {!!} ``` If you split on `xs i`, then you get the following code: ```agda lookup : ∀ {a n} {A : Set a} → Vec A n → Fin n → A lookup [] () lookup (x ∷ xs) zero = {!!} lookup (x ∷ xs) (suc i) = {!!} ``` However, if you split on `i xs`, then you get the following code instead: ```agda lookup : ∀ {a n} {A : Set a} → Vec A n → Fin n → A lookup (x ∷ xs) zero = ? lookup (x ∷ xs) (suc i) = ? ``` This code is rejected by Agda 2.3.0, but accepted by 2.3.2 thanks to improved coverage checking (see above). * The Emacs mode now presents information about which module is currently being type-checked. * New global menu entry: `Information about the character at point`. If this entry is selected, then information about the character at point is displayed, including (in many cases) information about how to type the character. * Commenting/uncommenting the rest of the buffer. One can now comment or uncomment the rest of the buffer by typing `C-c C-x M-;` or by selecting the menu entry `Comment/uncomment` the rest of the buffer". * The Emacs mode now uses the Agda executable instead of GHCi. The `*ghci*` buffer has been renamed to `*agda2*`. A new configuration variable has been introduced: `agda2-program-name`, the name of the Agda executable (by default `agda`). The variable `agda2-ghci-options` has been replaced by `agda2-program-args`: extra arguments given to the Agda executable (by default `none`). If you want to limit Agda's memory consumption you can add some arguments to `agda2-program-args`, for instance `+RTS -M1.5G -RTS`. * The Emacs mode no longer depends on haskell-mode. Users who have customised certain haskell-mode variables (such as `haskell-ghci-program-args`) may want to update their configuration. ### LaTeX-backend An experimental LaTeX-backend which does precise highlighting a la the HTML-backend and code alignment a la lhs2TeX has been added. Here is a sample input literate Agda file: ```latex \documentclass{article} \usepackage{agda} \begin{document} The following module declaration will be hidden in the output. \AgdaHide{ \begin{code} module M where \end{code} } Two or more spaces can be used to make the backend align stuff. \begin{code} data ℕ : Set where zero : ℕ suc : ℕ → ℕ _+_ : ℕ → ℕ → ℕ zero + n = n suc m + n = suc (m + n) \end{code} \end{document} ``` To produce an output PDF issue the following commands: ``` agda --latex -i . .lagda pdflatex latex/.tex ``` Only the top-most module is processed, like with lhs2tex and unlike with the HTML-backend. If you want to process imported modules you have to call `agda --latex` manually on each of those modules. There are still issues related to formatting, see the bug tracker for more information: https://code.google.com/p/agda/issues/detail?id=697 The default `agda.sty` might therefore change in backwards-incompatible ways, as work proceeds in trying to resolve those problems. Implemented features: * Two or more spaces can be used to force alignment of things, like with lhs2tex. See example above. * The highlighting information produced by the type checker is used to generate the output. For example, the data declaration in the example above, produces: ```agda \AgdaKeyword{data} \AgdaDatatype{ℕ} \AgdaSymbol{:} \AgdaPrimitiveType{Set} \AgdaKeyword{where} ``` These LaTeX commands are defined in `agda.sty` (which is imported by `\usepackage{agda}`) and cause the highlighting. * The LaTeX-backend checks if `agda.sty` is found by the LaTeX environment, if it isn't a default `agda.sty` is copied from Agda's `data-dir` into the working directory (and thus made available to the LaTeX environment). If the default `agda.sty` isn't satisfactory (colors, fonts, spacing, etc) then the user can modify it and make put it somewhere where the LaTeX environment can find it. Hopefully most aspects should be modifiable via `agda.sty` rather than having to tweak the implementation. * `--latex-dir` can be used to change the default output directory. Release notes for Agda 2 version 2.3.0 ====================================== Language -------- * New more liberal syntax for mutually recursive definitions. It is no longer necessary to use the `mutual` keyword to define mutually recursive functions or datatypes. Instead, it is enough to declare things before they are used. Instead of ```agda mutual f : A f = a[f, g] g : B[f] g = b[f, g] ``` you can now write ```agda f : A g : B[f] f = a[f, g] g = b[f, g]. ``` With the new style you have more freedom in choosing the order in which things are type checked (previously type signatures were always checked before definitions). Furthermore you can mix arbitrary declarations, such as modules and postulates, with mutually recursive definitions. For data types and records the following new syntax is used to separate the declaration from the definition: ```agda -- Declaration. data Vec (A : Set) : Nat → Set -- Note the absence of 'where'. -- Definition. data Vec A where [] : Vec A zero _::_ : {n : Nat} → A → Vec A n → Vec A (suc n) -- Declaration. record Sigma (A : Set) (B : A → Set) : Set -- Definition. record Sigma A B where constructor _,_ field fst : A snd : B fst ``` When making separated declarations/definitions private or abstract you should attach the `private` keyword to the declaration and the `abstract` keyword to the definition. For instance, a private, abstract function can be defined as ```agda private f : A abstract f = e ``` Finally it may be worth noting that the old style of mutually recursive definitions is still supported (it basically desugars into the new style). * Pattern matching lambdas. Anonymous pattern matching functions can be defined using the syntax ``` \ { p11 .. p1n -> e1 ; ... ; pm1 .. pmn -> em } ``` (where, as usual, `\` and `->` can be replaced by `λ` and `→`). Internally this is translated into a function definition of the following form: ``` .extlam p11 .. p1n = e1 ... .extlam pm1 .. pmn = em ``` This means that anonymous pattern matching functions are generative. For instance, `refl` will not be accepted as an inhabitant of the type ```agda (λ { true → true ; false → false }) ≡ (λ { true → true ; false → false }), ``` because this is equivalent to `extlam1 ≡ extlam2` for some distinct fresh names `extlam1` and `extlam2`. Currently the `where` and `with` constructions are not allowed in (the top-level clauses of) anonymous pattern matching functions. Examples: ```agda and : Bool → Bool → Bool and = λ { true x → x ; false _ → false } xor : Bool → Bool → Bool xor = λ { true true → false ; false false → false ; _ _ → true } fst : {A : Set} {B : A → Set} → Σ A B → A fst = λ { (a , b) → a } snd : {A : Set} {B : A → Set} (p : Σ A B) → B (fst p) snd = λ { (a , b) → b } ``` * Record update syntax. Assume that we have a record type and a corresponding value: ```agda record MyRecord : Set where field a b c : ℕ old : MyRecord old = record { a = 1; b = 2; c = 3 } ``` Then we can update (some of) the record value's fields in the following way: ```agda new : MyRecord new = record old { a = 0; c = 5 } ``` Here new normalises to `record { a = 0; b = 2; c = 5 }`. Any expression yielding a value of type `MyRecord` can be used instead of old. Record updating is not allowed to change types: the resulting value must have the same type as the original one, including the record parameters. Thus, the type of a record update can be inferred if the type of the original record can be inferred. The record update syntax is expanded before type checking. When the expression ```agda record old { upd-fields } ``` is checked against a record type `R`, it is expanded to ```agda let r = old in record { new-fields }, ``` where old is required to have type `R` and new-fields is defined as follows: for each field `x` in `R`, - if `x = e` is contained in `upd-fields` then `x = e` is included in `new-fields`, and otherwise - if `x` is an explicit field then `x = R.x r` is included in `new-fields`, and - if `x` is an implicit or instance field, then it is omitted from `new-fields`. (Instance arguments are explained below.) The reason for treating implicit and instance fields specially is to allow code like the following: ```agda record R : Set where field {length} : ℕ vec : Vec ℕ length -- More fields… xs : R xs = record { vec = 0 ∷ 1 ∷ 2 ∷ [] } ys = record xs { vec = 0 ∷ [] } ``` Without the special treatment the last expression would need to include a new binding for length (for instance `length = _`). * Record patterns which do not contain data type patterns, but which do contain dot patterns, are no longer rejected. * When the `--without-K` flag is used literals are now treated as constructors. * Under-applied functions can now reduce. Consider the following definition: ```agda id : {A : Set} → A → A id x = x ``` Previously the expression `id` would not reduce. This has been changed so that it now reduces to `λ x → x`. Usually this makes little difference, but it can be important in conjunction with `with`. See Issue [#365](https://github.com/agda/agda/issues/365) for an example. * Unused AgdaLight legacy syntax `(x y : A; z v : B)` for telescopes has been removed. ### Universe polymorphism * Universe polymorphism is now enabled by default. Use `--no-universe-polymorphism` to disable it. * Universe levels are no longer defined as a data type. The basic level combinators can be introduced in the following way: ```agda postulate Level : Set zero : Level suc : Level → Level max : Level → Level → Level {-# BUILTIN LEVEL Level #-} {-# BUILTIN LEVELZERO zero #-} {-# BUILTIN LEVELSUC suc #-} {-# BUILTIN LEVELMAX max #-} ``` * The BUILTIN equality is now required to be universe-polymorphic. * `trustMe` is now universe-polymorphic. ### Meta-variables and unification * Unsolved meta-variables are now frozen after every mutual block. This means that they cannot be instantiated by subsequent code. For instance, ```agda one : Nat one = _ bla : one ≡ suc zero bla = refl ``` leads to an error now, whereas previously it lead to the instantiation of `_` with `suc zero`. If you want to make use of the old behaviour, put the two definitions in a mutual block. All meta-variables are unfrozen during interactive editing, so that the user can fill holes interactively. Note that type-checking of interactively given terms is not perfect: Agda sometimes refuses to load a file, even though no complaints were raised during the interactive construction of the file. This is because certain checks (for instance, positivity) are only invoked when a file is loaded. * Record types can now be inferred. If there is a unique known record type with fields matching the fields in a record expression, then the type of the expression will be inferred to be the record type applied to unknown parameters. If there is no known record type with the given fields the type checker will give an error instead of producing lots of unsolved meta-variables. Note that "known record type" refers to any record type in any imported module, not just types which are in scope. * The occurrence checker distinguishes rigid and strongly rigid occurrences [Reed, LFMTP 2009; Abel & Pientka, TLCA 2011]. The completeness checker now accepts the following code: ```agda h : (n : Nat) → n ≡ suc n → Nat h n () ``` Internally this generates a constraint `_n = suc _n` where the meta-variable `_n` occurs strongly rigidly, i.e. on a constructor path from the root, in its own defining term tree. This is never solvable. Weakly rigid recursive occurrences may have a solution [Jason Reed's PhD thesis, page 106]: ```agda test : (k : Nat) → let X : (Nat → Nat) → Nat X = _ in (f : Nat → Nat) → X f ≡ suc (f (X (λ x → k))) test k f = refl ``` The constraint `_X k f = suc (f (_X k (λ x → k)))` has the solution `_X k f = suc (f (suc k))`, despite the recursive occurrence of `_X`. Here `_X` is not strongly rigid, because it occurs under the bound variable `f`. Previously Agda rejected this code; now it instead complains about an unsolved meta-variable. * Equation constraints involving the same meta-variable in the head now trigger pruning [Pientka, PhD, Sec. 3.1.2; Abel & Pientka, TLCA 2011]. Example: ```agda same : let X : A → A → A → A × A X = _ in {x y z : A} → X x y y ≡ (x , y) × X x x y ≡ X x y y same = refl , refl ``` The second equation implies that `X` cannot depend on its second argument. After pruning the first equation is linear and can be solved. * Instance arguments. A new type of hidden function arguments has been added: instance arguments. This new feature is based on influences from Scala's implicits and Agda's existing implicit arguments. Plain implicit arguments are marked by single braces: `{…}`. Instance arguments are instead marked by double braces: `{{…}}`. Example: ```agda postulate A : Set B : A → Set a : A f : {{a : A}} → B a ``` Instead of the double braces you can use the symbols `⦃` and `⦄`, but these symbols must in many cases be surrounded by whitespace. (If you are using Emacs and the Agda input method, then you can conjure up the symbols by typing `\{{` and `\}}`, respectively.) Instance arguments behave as ordinary implicit arguments, except for one important aspect: resolution of arguments which are not provided explicitly. For instance, consider the following code: ```agda test = f ``` Here Agda will notice that `f`'s instance argument was not provided explicitly, and try to infer it. All definitions in scope at `f`'s call site, as well as all variables in the context, are considered. If exactly one of these names has the required type `A`, then the instance argument will be instantiated to this name. This feature can be used as an alternative to Haskell type classes. If we define ```agda record Eq (A : Set) : Set where field equal : A → A → Bool, ``` then we can define the following projection: ```agda equal : {A : Set} {{eq : Eq A}} → A → A → Bool equal {{eq}} = Eq.equal eq ``` Now consider the following expression: ```agda equal false false ∨ equal 3 4 ``` If the following `Eq` "instances" for `Bool` and `ℕ` are in scope, and no others, then the expression is accepted: ```agda eq-Bool : Eq Bool eq-Bool = record { equal = … } eq-ℕ : Eq ℕ eq-ℕ = record { equal = … } ``` A shorthand notation is provided to avoid the need to define projection functions manually: ```agda module Eq-with-implicits = Eq {{...}} ``` This notation creates a variant of `Eq`'s record module, where the main `Eq` argument is an instance argument instead of an explicit one. It is equivalent to the following definition: ```agda module Eq-with-implicits {A : Set} {{eq : Eq A}} = Eq eq ``` Note that the short-hand notation allows you to avoid naming the "-with-implicits" module: ```agda open Eq {{...}} ``` Instance argument resolution is not recursive. As an example, consider the following "parametrised instance": ```agda eq-List : {A : Set} → Eq A → Eq (List A) eq-List {A} eq = record { equal = eq-List-A } where eq-List-A : List A → List A → Bool eq-List-A [] [] = true eq-List-A (a ∷ as) (b ∷ bs) = equal a b ∧ eq-List-A as bs eq-List-A _ _ = false ``` Assume that the only `Eq` instances in scope are `eq-List` and `eq-ℕ`. Then the following code does not type-check: ```agda test = equal (1 ∷ 2 ∷ []) (3 ∷ 4 ∷ []) ``` However, we can make the code work by constructing a suitable instance manually: ```agda test′ = equal (1 ∷ 2 ∷ []) (3 ∷ 4 ∷ []) where eq-List-ℕ = eq-List eq-ℕ ``` By restricting the "instance search" to be non-recursive we avoid introducing a new, compile-time-only evaluation model to Agda. For more information about instance arguments, see Devriese & Piessens [ICFP 2011]. Some examples are also available in the examples/instance-arguments subdirectory of the Agda distribution. ### Irrelevance * Dependent irrelevant function types. Some examples illustrating the syntax of dependent irrelevant function types: ``` .(x y : A) → B .{x y z : A} → B ∀ x .y → B ∀ x .{y} {z} .v → B ``` The declaration ``` f : .(x : A) → B[x] f x = t[x] ``` requires that `x` is irrelevant both in `t[x]` and in `B[x]`. This is possible if, for instance, `B[x] = B′ x`, with `B′ : .A → Set`. Dependent irrelevance allows us to define the eliminator for the `Squash` type: ```agda record Squash (A : Set) : Set where constructor squash field .proof : A elim-Squash : {A : Set} (P : Squash A → Set) (ih : .(a : A) → P (squash a)) → (a⁻ : Squash A) → P a⁻ elim-Squash P ih (squash a) = ih a ``` Note that this would not type-check with ```agda (ih : (a : A) -> P (squash a)). ``` * Records with only irrelevant fields. The following now works: ```agda record IsEquivalence {A : Set} (_≈_ : A → A → Set) : Set where field .refl : Reflexive _≈_ .sym : Symmetric _≈_ .trans : Transitive _≈_ record Setoid : Set₁ where infix 4 _≈_ field Carrier : Set _≈_ : Carrier → Carrier → Set .isEquivalence : IsEquivalence _≈_ open IsEquivalence isEquivalence public ``` Previously Agda complained about the application `IsEquivalence isEquivalence`, because `isEquivalence` is irrelevant and the `IsEquivalence` module expected a relevant argument. Now, when record modules are generated for records consisting solely of irrelevant arguments, the record parameter is made irrelevant: ```agda module IsEquivalence {A : Set} {_≈_ : A → A → Set} .(r : IsEquivalence {A = A} _≈_) where … ``` * Irrelevant things are no longer erased internally. This means that they are printed as ordinary terms, not as `_` as before. * The new flag `--experimental-irrelevance` enables irrelevant universe levels and matching on irrelevant data when only one constructor is available. These features are very experimental and likely to change or disappear. ### Reflection * The reflection API has been extended to mirror features like irrelevance, instance arguments and universe polymorphism, and to give (limited) access to definitions. For completeness all the builtins and primitives are listed below: ```agda -- Names. postulate Name : Set {-# BUILTIN QNAME Name #-} primitive -- Equality of names. primQNameEquality : Name → Name → Bool -- Is the argument visible (explicit), hidden (implicit), or an -- instance argument? data Visibility : Set where visible hidden instance : Visibility {-# BUILTIN HIDING Visibility #-} {-# BUILTIN VISIBLE visible #-} {-# BUILTIN HIDDEN hidden #-} {-# BUILTIN INSTANCE instance #-} -- Arguments can be relevant or irrelevant. data Relevance : Set where relevant irrelevant : Relevance {-# BUILTIN RELEVANCE Relevance #-} {-# BUILTIN RELEVANT relevant #-} {-# BUILTIN IRRELEVANT irrelevant #-} -- Arguments. data Arg A : Set where arg : (v : Visibility) (r : Relevance) (x : A) → Arg A {-# BUILTIN ARG Arg #-} {-# BUILTIN ARGARG arg #-} -- Terms. mutual data Term : Set where -- Variable applied to arguments. var : (x : ℕ) (args : List (Arg Term)) → Term -- Constructor applied to arguments. con : (c : Name) (args : List (Arg Term)) → Term -- Identifier applied to arguments. def : (f : Name) (args : List (Arg Term)) → Term -- Different kinds of λ-abstraction. lam : (v : Visibility) (t : Term) → Term -- Pi-type. pi : (t₁ : Arg Type) (t₂ : Type) → Term -- A sort. sort : Sort → Term -- Anything else. unknown : Term data Type : Set where el : (s : Sort) (t : Term) → Type data Sort : Set where -- A Set of a given (possibly neutral) level. set : (t : Term) → Sort -- A Set of a given concrete level. lit : (n : ℕ) → Sort -- Anything else. unknown : Sort {-# BUILTIN AGDASORT Sort #-} {-# BUILTIN AGDATYPE Type #-} {-# BUILTIN AGDATERM Term #-} {-# BUILTIN AGDATERMVAR var #-} {-# BUILTIN AGDATERMCON con #-} {-# BUILTIN AGDATERMDEF def #-} {-# BUILTIN AGDATERMLAM lam #-} {-# BUILTIN AGDATERMPI pi #-} {-# BUILTIN AGDATERMSORT sort #-} {-# BUILTIN AGDATERMUNSUPPORTED unknown #-} {-# BUILTIN AGDATYPEEL el #-} {-# BUILTIN AGDASORTSET set #-} {-# BUILTIN AGDASORTLIT lit #-} {-# BUILTIN AGDASORTUNSUPPORTED unknown #-} postulate -- Function definition. Function : Set -- Data type definition. Data-type : Set -- Record type definition. Record : Set {-# BUILTIN AGDAFUNDEF Function #-} {-# BUILTIN AGDADATADEF Data-type #-} {-# BUILTIN AGDARECORDDEF Record #-} -- Definitions. data Definition : Set where function : Function → Definition data-type : Data-type → Definition record′ : Record → Definition constructor′ : Definition axiom : Definition primitive′ : Definition {-# BUILTIN AGDADEFINITION Definition #-} {-# BUILTIN AGDADEFINITIONFUNDEF function #-} {-# BUILTIN AGDADEFINITIONDATADEF data-type #-} {-# BUILTIN AGDADEFINITIONRECORDDEF record′ #-} {-# BUILTIN AGDADEFINITIONDATACONSTRUCTOR constructor′ #-} {-# BUILTIN AGDADEFINITIONPOSTULATE axiom #-} {-# BUILTIN AGDADEFINITIONPRIMITIVE primitive′ #-} primitive -- The type of the thing with the given name. primQNameType : Name → Type -- The definition of the thing with the given name. primQNameDefinition : Name → Definition -- The constructors of the given data type. primDataConstructors : Data-type → List Name ``` As an example the expression ```agda primQNameType (quote zero) ``` is definitionally equal to ```agda el (lit 0) (def (quote ℕ) []) ``` (if `zero` is a constructor of the data type `ℕ`). * New keyword: `unquote`. The construction `unquote t` converts a representation of an Agda term to actual Agda code in the following way: 1. The argument `t` must have type `Term` (see the reflection API above). 2. The argument is normalised. 3. The entire construction is replaced by the normal form, which is treated as syntax written by the user and type-checked in the usual way. Examples: ```agda test : unquote (def (quote ℕ) []) ≡ ℕ test = refl id : (A : Set) → A → A id = unquote (lam visible (lam visible (var 0 []))) id-ok : id ≡ (λ A (x : A) → x) id-ok = refl ``` * New keyword: `quoteTerm`. The construction `quoteTerm t` is similar to `quote n`, but whereas `quote` is restricted to names `n`, `quoteTerm` accepts terms `t`. The construction is handled in the following way: 1. The type of `t` is inferred. The term `t` must be type-correct. 2. The term `t` is normalised. 3. The construction is replaced by the Term representation (see the reflection API above) of the normal form. Any unsolved metavariables in the term are represented by the `unknown` term constructor. Examples: ```agda test₁ : quoteTerm (λ {A : Set} (x : A) → x) ≡ lam hidden (lam visible (var 0 [])) test₁ = refl -- Local variables are represented as de Bruijn indices. test₂ : (λ {A : Set} (x : A) → quoteTerm x) ≡ (λ x → var 0 []) test₂ = refl -- Terms are normalised before being quoted. test₃ : quoteTerm (0 + 0) ≡ con (quote zero) [] test₃ = refl ``` Compiler backends ----------------- ### MAlonzo * The MAlonzo backend's FFI now handles universe polymorphism in a better way. The translation of Agda types and kinds into Haskell now supports universe-polymorphic postulates. The core changes are that the translation of function types has been changed from ``` T[[ Pi (x : A) B ]] = if A has a Haskell kind then forall x. () -> T[[ B ]] else if x in fv B then undef else T[[ A ]] -> T[[ B ]] ``` into ``` T[[ Pi (x : A) B ]] = if x in fv B then forall x. T[[ A ]] -> T[[ B ]] -- Note: T[[A]] not Unit. else T[[ A ]] -> T[[ B ]], ``` and that the translation of constants (postulates, constructors and literals) has been changed from ``` T[[ k As ]] = if COMPILED_TYPE k T then T T[[ As ]] else undef ``` into ``` T[[ k As ]] = if COMPILED_TYPE k T then T T[[ As ]] else if COMPILED k E then () else undef. ``` For instance, assuming a Haskell definition ```haskell type AgdaIO a b = IO b, ``` we can set up universe-polymorphic `IO` in the following way: ```agda postulate IO : ∀ {ℓ} → Set ℓ → Set ℓ return : ∀ {a} {A : Set a} → A → IO A _>>=_ : ∀ {a b} {A : Set a} {B : Set b} → IO A → (A → IO B) → IO B {-# COMPILED_TYPE IO AgdaIO #-} {-# COMPILED return (\_ _ -> return) #-} {-# COMPILED _>>=_ (\_ _ _ _ -> (>>=)) #-} ``` This is accepted because (assuming that the universe level type is translated to the Haskell unit type `()`) ```haskell (\_ _ -> return) : forall a. () -> forall b. () -> b -> AgdaIO a b = T [[ ∀ {a} {A : Set a} → A → IO A ]] ``` and ```haskell (\_ _ _ _ -> (>>=)) : forall a. () -> forall b. () -> forall c. () -> forall d. () -> AgdaIO a c -> (c -> AgdaIO b d) -> AgdaIO b d = T [[ ∀ {a b} {A : Set a} {B : Set b} → IO A → (A → IO B) → IO B ]]. ``` ### Epic * New Epic backend pragma: `STATIC`. In the Epic backend, functions marked with the `STATIC` pragma will be normalised before compilation. Example usage: ``` {-# STATIC power #-} power : ℕ → ℕ → ℕ power 0 x = 1 power 1 x = x power (suc n) x = power n x * x ``` Occurrences of `power 4 x` will be replaced by `((x * x) * x) * x`. * Some new optimisations have been implemented in the Epic backend: - Removal of unused arguments. A worker/wrapper transformation is performed so that unused arguments can be removed by Epic's inliner. For instance, the map function is transformed in the following way: ```agda map_wrap : (A B : Set) → (A → B) → List A → List B map_wrap A B f xs = map_work f xs map_work f [] = [] map_work f (x ∷ xs) = f x ∷ map_work f xs ``` If `map_wrap` is inlined (which it will be in any saturated call), then `A` and `B` disappear in the generated code. Unused arguments are found using abstract interpretation. The bodies of all functions in a module are inspected to decide which variables are used. The behaviour of postulates is approximated based on their types. Consider `return`, for instance: ```agda postulate return : {A : Set} → A → IO A ``` The first argument of `return` can be removed, because it is of type Set and thus cannot affect the outcome of a program at runtime. - Injection detection. At runtime many functions may turn out to be inefficient variants of the identity function. This is especially true after forcing. Injection detection replaces some of these functions with more efficient versions. Example: ```agda inject : {n : ℕ} → Fin n → Fin (1 + n) inject {suc n} zero = zero inject {suc n} (suc i) = suc (inject {n} i) ``` Forcing removes the `Fin` constructors' `ℕ` arguments, so this function is an inefficient identity function that can be replaced by the following one: ```agda inject {_} x = x ``` To actually find this function, we make the induction hypothesis that inject is an identity function in its second argument and look at the branches of the function to decide if this holds. Injection detection also works over data type barriers. Example: ```agda forget : {A : Set} {n : ℕ} → Vec A n → List A forget [] = [] forget (x ∷ xs) = x ∷ forget xs ``` Given that the constructor tags (in the compiled Epic code) for `Vec.[]` and `List.[]` are the same, and that the tags for `Vec._∷_` and `List._∷_` are also the same, this is also an identity function. We can hence replace the definition with the following one: ```agda forget {_} xs = xs ``` To get this to apply as often as possible, constructor tags are chosen *after* injection detection has been run, in a way to make as many functions as possible injections. Constructor tags are chosen once per source file, so it may be advantageous to define conversion functions like forget in the same module as one of the data types. For instance, if `Vec.agda` imports `List.agda`, then the forget function should be put in `Vec.agda` to ensure that vectors and lists get the same tags (unless some other injection function, which puts different constraints on the tags, is prioritised). - Smashing. This optimisation finds types whose values are inferable at runtime: * A data type with only one constructor where all fields are inferable is itself inferable. * `Set ℓ` is inferable (as it has no runtime representation). A function returning an inferable data type can be smashed, which means that it is replaced by a function which simply returns the inferred value. An important example of an inferable type is the usual propositional equality type (`_≡_`). Any function returning a propositional equality can simply return the reflexivity constructor directly without computing anything. This optimisation makes more arguments unused. It also makes the Epic code size smaller, which in turn speeds up compilation. ### JavaScript * ECMAScript compiler backend. A new compiler backend is being implemented, targetting ECMAScript (also known as JavaScript), with the goal of allowing Agda programs to be run in browsers or other ECMAScript environments. The backend is still at an experimental stage: the core language is implemented, but many features are still missing. The ECMAScript compiler can be invoked from the command line using the flag `--js`: ``` agda --js --compile-dir= .agda ``` Each source `.agda` is compiled into an ECMAScript target `/jAgda..js`. The compiler can also be invoked using the Emacs mode (the variable `agda2-backend` controls which backend is used). Note that ECMAScript is a strict rather than lazy language. Since Agda programs are total, this should not impact program semantics, but it may impact their space or time usage. ECMAScript does not support algebraic datatypes or pattern-matching. These features are translated to a use of the visitor pattern. For instance, the standard library's `List` data type and `null` function are translated into the following code: ```javascript exports["List"] = {}; exports["List"]["[]"] = function (x0) { return x0["[]"](); }; exports["List"]["_∷_"] = function (x0) { return function (x1) { return function (x2) { return x2["_∷_"](x0, x1); }; }; }; exports["null"] = function (x0) { return function (x1) { return function (x2) { return x2({ "[]": function () { return jAgda_Data_Bool["Bool"]["true"]; }, "_∷_": function (x3, x4) { return jAgda_Data_Bool["Bool"]["false"]; } }); }; }; }; ``` Agda records are translated to ECMAScript objects, preserving field names. Top-level Agda modules are translated to ECMAScript modules, following the `common.js` module specification. A top-level Agda module `Foo.Bar` is translated to an ECMAScript module `jAgda.Foo.Bar`. The ECMAScript compiler does not compile to Haskell, so the pragmas related to the Haskell FFI (`IMPORT`, `COMPILED_DATA` and `COMPILED`) are not used by the ECMAScript backend. Instead, there is a `COMPILED_JS` pragma which may be applied to any declaration. For postulates, primitives, functions and values, it gives the ECMAScript code to be emitted by the compiler. For data types, it gives a function which is applied to a value of that type, and a visitor object. For instance, a binding of natural numbers to ECMAScript integers (ignoring overflow errors) is: ```agda data ℕ : Set where zero : ℕ suc : ℕ → ℕ {-# COMPILED_JS ℕ function (x,v) { if (x < 1) { return v.zero(); } else { return v.suc(x-1); } } #-} {-# COMPILED_JS zero 0 #-} {-# COMPILED_JS suc function (x) { return x+1; } #-} _+_ : ℕ → ℕ → ℕ zero + n = n suc m + n = suc (m + n) {-# COMPILED_JS _+_ function (x) { return function (y) { return x+y; }; } #-} ``` To allow FFI code to be optimised, the ECMAScript in a `COMPILED_JS` declaration is parsed, using a simple parser that recognises a pure functional subset of ECMAScript, consisting of functions, function applications, return, if-statements, if-expressions, side-effect-free binary operators (no precedence, left associative), side-effect-free prefix operators, objects (where all member names are quoted), field accesses, and string and integer literals. Modules may be imported using the require (``) syntax: any impure code, or code outside the supported fragment, can be placed in a module and imported. Tools ----- * New flag `--safe`, which can be used to type-check untrusted code. This flag disables postulates, `primTrustMe`, and "unsafe" OPTION pragmas, some of which are known to make Agda inconsistent. Rejected pragmas: ``` --allow-unsolved-metas --experimental-irrelevance --guardedness-preserving-type-construtors --injective-type-constructors --no-coverage-check --no-positivity-check --no-termination-check --sized-types --type-in-type ``` Note that, at the moment, it is not possible to define the universe level or coinduction primitives when `--safe` is used (because they must be introduced as postulates). This can be worked around by type-checking trusted files in a first pass, without using `--safe`, and then using `--saf`e in a second pass. Modules which have already been type-checked are not re-type-checked just because `--safe` is used. * Dependency graphs. The new flag `--dependency-graph=FILE` can be used to generate a DOT file containing a module dependency graph. The generated file (FILE) can be rendered using a tool like dot. * The `--no-unreachable-check` flag has been removed. * Projection functions are highlighted as functions instead of as fields. Field names (in record definitions and record values) are still highlighted as fields. * Support for jumping to positions mentioned in the information buffer has been added. * The `make install` command no longer installs Agda globally (by default). Release notes for Agda 2 version 2.2.10 ======================================= Language -------- * New flag: `--without-K`. This flag makes pattern matching more restricted. If the flag is activated, then Agda only accepts certain case-splits. If the type of the variable to be split is `D pars ixs`, where `D` is a data (or record) type, pars stands for the parameters, and `ixs` the indices, then the following requirements must be satisfied: - The indices `ixs` must be applications of constructors to distinct variables. - These variables must not be free in pars. The intended purpose of `--without-K` is to enable experiments with a propositional equality without the K rule. Let us define propositional equality as follows: ```agda data _≡_ {A : Set} : A → A → Set where refl : ∀ x → x ≡ x ``` Then the obvious implementation of the J rule is accepted: ```agda J : {A : Set} (P : {x y : A} → x ≡ y → Set) → (∀ x → P (refl x)) → ∀ {x y} (x≡y : x ≡ y) → P x≡y J P p (refl x) = p x ``` The same applies to Christine Paulin-Mohring's version of the J rule: ```agda J′ : {A : Set} {x : A} (P : {y : A} → x ≡ y → Set) → P (refl x) → ∀ {y} (x≡y : x ≡ y) → P x≡y J′ P p (refl x) = p ``` On the other hand, the obvious implementation of the K rule is not accepted: ```agda K : {A : Set} (P : {x : A} → x ≡ x → Set) → (∀ x → P (refl x)) → ∀ {x} (x≡x : x ≡ x) → P x≡x K P p (refl x) = p x ``` However, we have *not* proved that activation of `--without-K` ensures that the K rule cannot be proved in some other way. * Irrelevant declarations. Postulates and functions can be marked as irrelevant by prefixing the name with a dot when the name is declared. Example: ```agda postulate .irrelevant : {A : Set} → .A → A ``` Irrelevant names may only be used in irrelevant positions or in definitions of things which have been declared irrelevant. The axiom irrelevant above can be used to define a projection from an irrelevant record field: ```agda data Subset (A : Set) (P : A → Set) : Set where _#_ : (a : A) → .(P a) → Subset A P elem : ∀ {A P} → Subset A P → A elem (a # p) = a .certificate : ∀ {A P} (x : Subset A P) → P (elem x) certificate (a # p) = irrelevant p ``` The right-hand side of certificate is relevant, so we cannot define ```agda certificate (a # p) = p ``` (because `p` is irrelevant). However, certificate is declared to be irrelevant, so it can use the axiom irrelevant. Furthermore the first argument of the axiom is irrelevant, which means that irrelevant `p` is well-formed. As shown above the axiom irrelevant justifies irrelevant projections. Previously no projections were generated for irrelevant record fields, such as the field certificate in the following record type: ```agda record Subset (A : Set) (P : A → Set) : Set where constructor _#_ field elem : A .certificate : P elem ``` Now projections are generated automatically for irrelevant fields (unless the flag `--no-irrelevant-projections` is used). Note that irrelevant projections are highly experimental. * Termination checker recognises projections. Projections now preserve sizes, both in patterns and expressions. Example: ```agda record Wrap (A : Set) : Set where constructor wrap field unwrap : A open Wrap public data WNat : Set where zero : WNat suc : Wrap WNat → WNat id : WNat → WNat id zero = zero id (suc w) = suc (wrap (id (unwrap w))) ``` In the structural ordering `unwrap w` ≤ `w`. This means that ```agda unwrap w ≤ w < suc w, ``` and hence the recursive call to id is accepted. Projections also preserve guardedness. Tools ----- * Hyperlinks for top-level module names now point to the start of the module rather than to the declaration of the module name. This applies both to the Emacs mode and to the output of `agda --html`. * Most occurrences of record field names are now highlighted as "fields". Previously many occurrences were highlighted as "functions". * Emacs mode: It is no longer possible to change the behaviour of the `TAB` key by customising `agda2-indentation`. * Epic compiler backend. A new compiler backend is being implemented. This backend makes use of Edwin Brady's language Epic (http://www.cs.st-andrews.ac.uk/~eb/epic.php) and its compiler. The backend should handle most Agda code, but is still at an experimental stage: more testing is needed, and some things written below may not be entirely true. The Epic compiler can be invoked from the command line using the flag `--epic`: ``` agda --epic --epic-flag= --compile-dir= .agda ``` The `--epic-flag` flag can be given multiple times; each flag is given verbatim to the Epic compiler (in the given order). The resulting executable is named after the main module and placed in the directory specified by the `--compile-dir` flag (default: the project root). Intermediate files are placed in a subdirectory called `Epic`. The backend requires that there is a definition named main. This definition should be a value of type `IO Unit`, but at the moment this is not checked (so it is easy to produce a program which segfaults). Currently the backend represents actions of type `IO A` as functions from `Unit` to `A`, and main is applied to the unit value. The Epic compiler compiles via C, not Haskell, so the pragmas related to the Haskell FFI (`IMPORT`, `COMPILED_DATA` and `COMPILED`) are not used by the Epic backend. Instead there is a new pragma `COMPILED_EPIC`. This pragma is used to give Epic code for postulated definitions (Epic code can in turn call C code). The form of the pragma is `{-# COMPILED_EPIC def code #-}`, where `def` is the name of an Agda postulate and `code` is some Epic code which should include the function arguments, return type and function body. As an example the `IO` monad can be defined as follows: ```agda postulate IO : Set → Set return : ∀ {A} → A → IO A _>>=_ : ∀ {A B} → IO A → (A → IO B) → IO B {-# COMPILED_EPIC return (u : Unit, a : Any) -> Any = ioreturn(a) #-} {-# COMPILED_EPIC _>>=_ (u1 : Unit, u2 : Unit, x : Any, f : Any) -> Any = iobind(x,f) #-} ``` Here `ioreturn` and `iobind` are Epic functions which are defined in the file `AgdaPrelude.e` which is always included. By default the backend will remove so-called forced constructor arguments (and case-splitting on forced variables will be rewritten). This optimisation can be disabled by using the flag `--no-forcing`. All data types which look like unary natural numbers after forced constructor arguments have been removed (i.e. types with two constructors, one nullary and one with a single recursive argument) will be represented as "BigInts". This applies to the standard `Fin` type, for instance. The backend supports Agda's primitive functions and the BUILTIN pragmas. If the BUILTIN pragmas for unary natural numbers are used, then some operations, like addition and multiplication, will use more efficient "BigInt" operations. If you want to make use of the Epic backend you need to install some dependencies, see the README. * The Emacs mode can compile using either the MAlonzo or the Epic backend. The variable `agda2-backend` controls which backend is used. Release notes for Agda 2 version 2.2.8 ====================================== Language -------- * Record pattern matching. It is now possible to pattern match on named record constructors. Example: ```agda record Σ (A : Set) (B : A → Set) : Set where constructor _,_ field proj₁ : A proj₂ : B proj₁ map : {A B : Set} {P : A → Set} {Q : B → Set} (f : A → B) → (∀ {x} → P x → Q (f x)) → Σ A P → Σ B Q map f g (x , y) = (f x , g y) ``` The clause above is internally translated into the following one: ```agda map f g p = (f (Σ.proj₁ p) , g (Σ.proj₂ p)) ``` Record patterns containing data type patterns are not translated. Example: ```agda add : ℕ × ℕ → ℕ add (zero , n) = n add (suc m , n) = suc (add (m , n)) ``` Record patterns which do not contain data type patterns, but which do contain dot patterns, are currently rejected. Example: ```agda Foo : {A : Set} (p₁ p₂ : A × A) → proj₁ p₁ ≡ proj₁ p₂ → Set₁ Foo (x , y) (.x , y′) refl = Set ``` * Proof irrelevant function types. Agda now supports irrelevant non-dependent function types: ```agda f : .A → B ``` This type implies that `f` does not depend computationally on its argument. One intended use case is data structures with embedded proofs, like sorted lists: ```agda postulate _≤_ : ℕ → ℕ → Set p₁ : 0 ≤ 1 p₂ : 0 ≤ 1 data SList (bound : ℕ) : Set where [] : SList bound scons : (head : ℕ) → .(head ≤ bound) → (tail : SList head) → SList bound ``` The effect of the irrelevant type in the signature of `scons` is that `scons`'s second argument is never inspected after Agda has ensured that it has the right type. It is even thrown away, leading to smaller term sizes and hopefully some gain in efficiency. The type-checker ignores irrelevant arguments when checking equality, so two lists can be equal even if they contain different proofs: ```agda l₁ : SList 1 l₁ = scons 0 p₁ [] l₂ : SList 1 l₂ = scons 0 p₂ [] l₁≡l₂ : l₁ ≡ l₂ l₁≡l₂ = refl ``` Irrelevant arguments can only be used in irrelevant contexts. Consider the following subset type: ```agda data Subset (A : Set) (P : A → Set) : Set where _#_ : (elem : A) → .(P elem) → Subset A P ``` The following two uses are fine: ```agda elimSubset : ∀ {A C : Set} {P} → Subset A P → ((a : A) → .(P a) → C) → C elimSubset (a # p) k = k a p elem : {A : Set} {P : A → Set} → Subset A P → A elem (x # p) = x ``` However, if we try to project out the proof component, then Agda complains that `variable p is declared irrelevant, so it cannot be used here`: ```agda prjProof : ∀ {A P} (x : Subset A P) → P (elem x) prjProof (a # p) = p ``` Matching against irrelevant arguments is also forbidden, except in the case of irrefutable matches (record constructor patterns which have been translated away). For instance, the match against the pattern `(p , q)` here is accepted: ```agda elim₂ : ∀ {A C : Set} {P Q : A → Set} → Subset A (λ x → Σ (P x) (λ _ → Q x)) → ((a : A) → .(P a) → .(Q a) → C) → C elim₂ (a # (p , q)) k = k a p q ``` Absurd matches `()` are also allowed. Note that record fields can also be irrelevant. Example: ```agda record Subset (A : Set) (P : A → Set) : Set where constructor _#_ field elem : A .proof : P elem ``` Irrelevant fields are never in scope, neither inside nor outside the record. This means that no record field can depend on an irrelevant field, and furthermore projections are not defined for such fields. Irrelevant fields can only be accessed using pattern matching, as in `elimSubset` above. Irrelevant function types were added very recently, and have not been subjected to much experimentation yet, so do not be surprised if something is changed before the next release. For instance, dependent irrelevant function spaces (`.(x : A) → B`) might be added in the future. * Mixfix binders. It is now possible to declare user-defined syntax that binds identifiers. Example: ```agda postulate State : Set → Set → Set put : ∀ {S} → S → State S ⊤ get : ∀ {S} → State S S return : ∀ {A S} → A → State S A bind : ∀ {A B S} → State S B → (B → State S A) → State S A syntax bind e₁ (λ x → e₂) = x ← e₁ , e₂ increment : State ℕ ⊤ increment = x ← get , put (1 + x) ``` The syntax declaration for `bind` implies that `x` is in scope in `e₂`, but not in `e₁`. You can give fixity declarations along with syntax declarations: ```agda infixr 40 bind syntax bind e₁ (λ x → e₂) = x ← e₁ , e₂ ``` The fixity applies to the syntax, not the name; syntax declarations are also restricted to ordinary, non-operator names. The following declaration is disallowed: ```agda syntax _==_ x y = x === y ```agda Syntax declarations must also be linear; the following declaration is disallowed: ```agda syntax wrong x = x + x ``` Syntax declarations were added very recently, and have not been subjected to much experimentation yet, so do not be surprised if something is changed before the next release. * `Prop` has been removed from the language. The experimental sort `Prop` has been disabled. Any program using `Prop` should typecheck if `Prop` is replaced by `Set₀`. Note that `Prop` is still a keyword. * Injective type constructors off by default. Automatic injectivity of type constructors has been disabled (by default). To enable it, use the flag `--injective-type-constructors`, either on the command line or in an OPTIONS pragma. Note that this flag makes Agda anti-classical and possibly inconsistent: Agda with excluded middle is inconsistent http://thread.gmane.org/gmane.comp.lang.agda/1367 See `test/Succeed/InjectiveTypeConstructors.agda` for an example. * Termination checker can count. There is a new flag `--termination-depth=N` accepting values `N >= 1` (with `N = 1` being the default) which influences the behavior of the termination checker. So far, the termination checker has only distinguished three cases when comparing the argument of a recursive call with the formal parameter of the callee. `<`: the argument is structurally smaller than the parameter `=`: they are equal `?`: the argument is bigger or unrelated to the parameter This behavior, which is still the default (`N = 1`), will not recognise the following functions as terminating. ```agda mutual f : ℕ → ℕ f zero = zero f (suc zero) = zero f (suc (suc n)) = aux n aux : ℕ → ℕ aux m = f (suc m) ``` The call graph ``` f --(<)--> aux --(?)--> f ``` yields a recursive call from `f` to `f` via `aux` where the relation of call argument to callee parameter is computed as "unrelated" (composition of `<` and `?`). Setting `N >= 2` allows a finer analysis: `n` has two constructors less than `suc (suc n)`, and `suc m` has one more than `m`, so we get the call graph: ``` f --(-2)--> aux --(+1)--> f ``` The indirect call `f --> f` is now labeled with `(-1)`, and the termination checker can recognise that the call argument is decreasing on this path. Setting the termination depth to `N` means that the termination checker counts decrease up to `N` and increase up to `N-1`. The default, `N=1`, means that no increase is counted, every increase turns to "unrelated". In practice, examples like the one above sometimes arise when `with` is used. As an example, the program ```agda f : ℕ → ℕ f zero = zero f (suc zero) = zero f (suc (suc n)) with zero ... | _ = f (suc n) ``` is internally represented as ```agda mutual f : ℕ → ℕ f zero = zero f (suc zero) = zero f (suc (suc n)) = aux n zero aux : ℕ → ℕ → ℕ aux m k = f (suc m) ``` Thus, by default, the definition of `f` using `with` is not accepted by the termination checker, even though it looks structural (`suc n` is a subterm of `suc suc n`). Now, the termination checker is satisfied if the option `--termination-depth=2` is used. Caveats: - This is an experimental feature, hopefully being replaced by something smarter in the near future. - Increasing the termination depth will quickly lead to very long termination checking times. So, use with care. Setting termination depth to `100` by habit, just to be on the safe side, is not a good idea! - Increasing termination depth only makes sense for linear data types such as `ℕ` and `Size`. For other types, increase cannot be recognised. For instance, consider a similar example with lists. ```agda data List : Set where nil : List cons : ℕ → List → List mutual f : List → List f nil = nil f (cons x nil) = nil f (cons x (cons y ys)) = aux y ys aux : ℕ → List → List aux z zs = f (cons z zs) ``` Here the termination checker compares `cons z zs` to `z` and also to `zs`. In both cases, the result will be "unrelated", no matter how high we set the termination depth. This is because when comparing `cons z zs` to `zs`, for instance, `z` is unrelated to `zs`, thus, `cons z zs` is also unrelated to `zs`. We cannot say it is just "one larger" since `z` could be a very large term. Note that this points to a weakness of untyped termination checking. To regain the benefit of increased termination depth, we need to index our lists by a linear type such as `ℕ` or `Size`. With termination depth `2`, the above example is accepted for vectors instead of lists. * The `codata` keyword has been removed. To use coinduction, use the following new builtins: `INFINITY`, `SHARP` and `FLAT`. Example: ```agda {-# OPTIONS --universe-polymorphism #-} module Coinduction where open import Level infix 1000 ♯_ postulate ∞ : ∀ {a} (A : Set a) → Set a ♯_ : ∀ {a} {A : Set a} → A → ∞ A ♭ : ∀ {a} {A : Set a} → ∞ A → A {-# BUILTIN INFINITY ∞ #-} {-# BUILTIN SHARP ♯_ #-} {-# BUILTIN FLAT ♭ #-} ``` Note that (non-dependent) pattern matching on `SHARP` is no longer allowed. Note also that strange things might happen if you try to combine the pragmas above with `COMPILED_TYPE`, `COMPILED_DATA` or `COMPILED` pragmas, or if the pragmas do not occur right after the postulates. The compiler compiles the `INFINITY` builtin to nothing (more or less), so that the use of coinduction does not get in the way of FFI declarations: ```agda data Colist (A : Set) : Set where [] : Colist A _∷_ : (x : A) (xs : ∞ (Colist A)) → Colist A {-# COMPILED_DATA Colist [] [] (:) #-} ``` * Infinite types. If the new flag `--guardedness-preserving-type-constructors` is used, then type constructors are treated as inductive constructors when we check productivity (but only in parameters, and only if they are used strictly positively or not at all). This makes examples such as the following possible: ```agda data Rec (A : ∞ Set) : Set where fold : ♭ A → Rec A -- Σ cannot be a record type below. data Σ (A : Set) (B : A → Set) : Set where _,_ : (x : A) → B x → Σ A B syntax Σ A (λ x → B) = Σ[ x ∶ A ] B -- Corecursive definition of the W-type. W : (A : Set) → (A → Set) → Set W A B = Rec (♯ (Σ[ x ∶ A ] (B x → W A B))) syntax W A (λ x → B) = W[ x ∶ A ] B sup : {A : Set} {B : A → Set} (x : A) (f : B x → W A B) → W A B sup x f = fold (x , f) W-rec : {A : Set} {B : A → Set} (P : W A B → Set) → (∀ {x} {f : B x → W A B} → (∀ y → P (f y)) → P (sup x f)) → ∀ x → P x W-rec P h (fold (x , f)) = h (λ y → W-rec P h (f y)) -- Induction-recursion encoded as corecursion-recursion. data Label : Set where ′0 ′1 ′2 ′σ ′π ′w : Label mutual U : Set U = Σ Label U′ U′ : Label → Set U′ ′0 = ⊤ U′ ′1 = ⊤ U′ ′2 = ⊤ U′ ′σ = Rec (♯ (Σ[ a ∶ U ] (El a → U))) U′ ′π = Rec (♯ (Σ[ a ∶ U ] (El a → U))) U′ ′w = Rec (♯ (Σ[ a ∶ U ] (El a → U))) El : U → Set El (′0 , _) = ⊥ El (′1 , _) = ⊤ El (′2 , _) = Bool El (′σ , fold (a , b)) = Σ[ x ∶ El a ] El (b x) El (′π , fold (a , b)) = (x : El a) → El (b x) El (′w , fold (a , b)) = W[ x ∶ El a ] El (b x) U-rec : (P : ∀ u → El u → Set) → P (′1 , _) tt → P (′2 , _) true → P (′2 , _) false → (∀ {a b x y} → P a x → P (b x) y → P (′σ , fold (a , b)) (x , y)) → (∀ {a b f} → (∀ x → P (b x) (f x)) → P (′π , fold (a , b)) f) → (∀ {a b x f} → (∀ y → P (′w , fold (a , b)) (f y)) → P (′w , fold (a , b)) (sup x f)) → ∀ u (x : El u) → P u x U-rec P P1 P2t P2f Pσ Pπ Pw = rec where rec : ∀ u (x : El u) → P u x rec (′0 , _) () rec (′1 , _) _ = P1 rec (′2 , _) true = P2t rec (′2 , _) false = P2f rec (′σ , fold (a , b)) (x , y) = Pσ (rec _ x) (rec _ y) rec (′π , fold (a , b)) f = Pπ (λ x → rec _ (f x)) rec (′w , fold (a , b)) (fold (x , f)) = Pw (λ y → rec _ (f y)) ``` The `--guardedness-preserving-type-constructors` extension is based on a rather operational understanding of `∞`/`♯_`; it's not yet clear if this extension is consistent. * Qualified constructors. Constructors can now be referred to qualified by their data type. For instance, given ```agda data Nat : Set where zero : Nat suc : Nat → Nat data Fin : Nat → Set where zero : ∀ {n} → Fin (suc n) suc : ∀ {n} → Fin n → Fin (suc n) ``` you can refer to the constructors unambiguously as `Nat.zero`, `Nat.suc`, `Fin.zero`, and `Fin.suc` (`Nat` and `Fin` are modules containing the respective constructors). Example: ```agda inj : (n m : Nat) → Nat.suc n ≡ suc m → n ≡ m inj .m m refl = refl ``` Previously you had to write something like ```agda inj : (n m : Nat) → _≡_ {Nat} (suc n) (suc m) → n ≡ m ``` to make the type checker able to figure out that you wanted the natural number suc in this case. * Reflection. There are two new constructs for reflection: - `quoteGoal x in e` In `e` the value of `x` will be a representation of the goal type (the type expected of the whole expression) as an element in a datatype of Agda terms (see below). For instance, ```agda example : ℕ example = quoteGoal x in {! at this point x = def (quote ℕ) [] !} ``` - `quote x : Name` If `x` is the name of a definition (function, datatype, record, or a constructor), `quote x` gives you the representation of `x` as a value in the primitive type `Name` (see below). Quoted terms use the following BUILTINs and primitives (available from the standard library module `Reflection`): ```agda -- The type of Agda names. postulate Name : Set {-# BUILTIN QNAME Name #-} primitive primQNameEquality : Name → Name → Bool -- Arguments. Explicit? = Bool data Arg A : Set where arg : Explicit? → A → Arg A {-# BUILTIN ARG Arg #-} {-# BUILTIN ARGARG arg #-} -- The type of Agda terms. data Term : Set where var : ℕ → List (Arg Term) → Term con : Name → List (Arg Term) → Term def : Name → List (Arg Term) → Term lam : Explicit? → Term → Term pi : Arg Term → Term → Term sort : Term unknown : Term {-# BUILTIN AGDATERM Term #-} {-# BUILTIN AGDATERMVAR var #-} {-# BUILTIN AGDATERMCON con #-} {-# BUILTIN AGDATERMDEF def #-} {-# BUILTIN AGDATERMLAM lam #-} {-# BUILTIN AGDATERMPI pi #-} {-# BUILTIN AGDATERMSORT sort #-} {-# BUILTIN AGDATERMUNSUPPORTED unknown #-} ``` Reflection may be useful when working with internal decision procedures, such as the standard library's ring solver. * Minor record definition improvement. The definition of a record type is now available when type checking record module definitions. This means that you can define things like the following: ```agda record Cat : Set₁ where field Obj : Set _=>_ : Obj → Obj → Set -- ... -- not possible before: op : Cat op = record { Obj = Obj; _=>_ = λ A B → B => A } ``` Tools ----- * The `Goal type and context` command now shows the goal type before the context, and the context is shown in reverse order. The `Goal type, context and inferred type` command has been modified in a similar way. * Show module contents command. Given a module name `M` the Emacs mode can now display all the top-level modules and names inside `M`, along with types for the names. The command is activated using `C-c C-o` or the menus. * Auto command. A command which searches for type inhabitants has been added. The command is invoked by pressing `C-C C-a` (or using the goal menu). There are several flags and parameters, e.g. `-c` which enables case-splitting in the search. For further information, see the Agda wiki: http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.Auto * HTML generation is now possible for a module with unsolved meta-variables, provided that the `--allow-unsolved-metas` flag is used. Release notes for Agda 2 version 2.2.6 ====================================== Language -------- * Universe polymorphism (experimental extension). To enable universe polymorphism give the flag `--universe-polymorphism` on the command line or (recommended) as an OPTIONS pragma. When universe polymorphism is enabled `Set` takes an argument which is the universe level. For instance, the type of universe polymorphic identity is ```agda id : {a : Level} {A : Set a} → A → A. ``` The type Level is isomorphic to the unary natural numbers and should be specified using the BUILTINs `LEVEL`, `LEVELZERO`, and `LEVELSUC`: ```agda data Level : Set where zero : Level suc : Level → Level {-# BUILTIN LEVEL Level #-} {-# BUILTIN LEVELZERO zero #-} {-# BUILTIN LEVELSUC suc #-} ``` There is an additional BUILTIN `LEVELMAX` for taking the maximum of two levels: ```agda max : Level → Level → Level max zero m = m max (suc n) zero = suc n max (suc n) (suc m) = suc (max n m) {-# BUILTIN LEVELMAX max #-} ``` The non-polymorphic universe levels `Set`, `Set₁` and so on are sugar for `Set zero`, `Set (suc zero)`, etc. At present there is no automatic lifting of types from one level to another. It can still be done (rather clumsily) by defining types like the following one: ```agda data Lifted {a} (A : Set a) : Set (suc a) where lift : A → Lifted A ``` However, it is likely that automatic lifting is introduced at some point in the future. * Multiple constructors, record fields, postulates or primitives can be declared using a single type signature: ```agda data Bool : Set where false true : Bool postulate A B : Set ``` * Record fields can be implicit: ```agda record R : Set₁ where field {A} : Set f : A → A {B C} D {E} : Set g : B → C → E ``` By default implicit fields are not printed. * Record constructors can be defined: ```agda record Σ (A : Set) (B : A → Set) : Set where constructor _,_ field proj₁ : A proj₂ : B proj₁ ``` In this example `_,_` gets the type ```agda (proj₁ : A) → B proj₁ → Σ A B. ``` For implicit fields the corresponding constructor arguments become implicit. Note that the constructor is defined in the *outer* scope, so any fixity declaration has to be given outside the record definition. The constructor is not in scope inside the record module. Note also that pattern matching for records has not been implemented yet. * BUILTIN hooks for equality. The data type ```agda data _≡_ {A : Set} (x : A) : A → Set where refl : x ≡ x ``` can be specified as the builtin equality type using the following pragmas: ```agda {-# BUILTIN EQUALITY _≡_ #-} {-# BUILTIN REFL refl #-} ``` The builtin equality is used for the new rewrite construct and the `primTrustMe` primitive described below. * New `rewrite` construct. If `eqn : a ≡ b`, where `_≡_` is the builtin equality (see above) you can now write ```agda f ps rewrite eqn = rhs ``` instead of ```agda f ps with a | eqn ... | ._ | refl = rhs ``` The `rewrite` construct has the effect of rewriting the goal and the context by the given equation (left to right). You can rewrite using several equations (in sequence) by separating them with vertical bars (|): ```agda f ps rewrite eqn₁ | eqn₂ | … = rhs ``` It is also possible to add `with`-clauses after rewriting: ```agda f ps rewrite eqns with e ... | p = rhs ``` Note that pattern matching happens before rewriting—if you want to rewrite and then do pattern matching you can use a with after the rewrite. See `test/Succeed/Rewrite.agda` for some examples. * A new primitive, `primTrustMe`, has been added: ```agda primTrustMe : {A : Set} {x y : A} → x ≡ y ``` Here `_≡_` is the builtin equality (see BUILTIN hooks for equality, above). If `x` and `y` are definitionally equal, then `primTrustMe {x = x} {y = y}` reduces to `refl`. Note that the compiler replaces all uses of `primTrustMe` with the `REFL` builtin, without any check for definitional equality. Incorrect uses of `primTrustMe` can potentially lead to segfaults or similar problems. For an example of the use of `primTrustMe`, see `Data.String` in version 0.3 of the standard library, where it is used to implement decidable equality on strings using the primitive boolean equality. * Changes to the syntax and semantics of IMPORT pragmas, which are used by the Haskell FFI. Such pragmas must now have the following form: ```agda {-# IMPORT #-} ``` These pragmas are interpreted as *qualified* imports, so Haskell names need to be given qualified (unless they come from the Haskell prelude). * The horizontal tab character (U+0009) is no longer treated as white space. * Line pragmas are no longer supported. * The `--include-path` flag can no longer be used as a pragma. * The experimental and incomplete support for proof irrelevance has been disabled. Tools ----- * New `intro` command in the Emacs mode. When there is a canonical way of building something of the goal type (for instance, if the goal type is a pair), the goal can be refined in this way. The command works for the following goal types: - A data type where only one of its constructors can be used to construct an element of the goal type. (For instance, if the goal is a non-empty vector, a `cons` will be introduced.) - A record type. A record value will be introduced. Implicit fields will not be included unless showing of implicit arguments is switched on. - A function type. A lambda binding as many variables as possible will be introduced. The variable names will be chosen from the goal type if its normal form is a dependent function type, otherwise they will be variations on `x`. Implicit lambdas will only be inserted if showing of implicit arguments is switched on. This command can be invoked by using the `refine` command (`C-c C-r`) when the goal is empty. (The old behaviour of the refine command in this situation was to ask for an expression using the minibuffer.) * The Emacs mode displays `Checked` in the mode line if the current file type checked successfully without any warnings. * If a file `F` is loaded, and this file defines the module `M`, it is an error if `F` is not the file which defines `M` according to the include path. Note that the command-line tool and the Emacs mode define the meaning of relative include paths differently: the command-line tool interprets them relative to the current working directory, whereas the Emacs mode interprets them relative to the root directory of the current project. (As an example, if the module `A.B.C` is loaded from the file `/A/B/C.agda`, then the root directory is ``.) * It is an error if there are several files on the include path which match a given module name. * Interface files are relocatable. You can move around source trees as long as the include path is updated in a corresponding way. Note that a module `M` may be re-typechecked if its time stamp is strictly newer than that of the corresponding interface file (`M.agdai`). * Type-checking is no longer done when an up-to-date interface exists. (Previously the initial module was always type-checked.) * Syntax highlighting files for Emacs (`.agda.el`) are no longer used. The `--emacs` flag has been removed. (Syntax highlighting information is cached in the interface files.) * The Agate and Alonzo compilers have been retired. The options `--agate`, `--alonzo` and `--malonzo` have been removed. * The default directory for MAlonzo output is the project's root directory. The `--malonzo-dir` flag has been renamed to `--compile-dir`. * Emacs mode: `C-c C-x C-d` no longer resets the type checking state. `C-c C-x C-r` can be used for a more complete reset. `C-c C-x C-s` (which used to reload the syntax highlighting information) has been removed. `C-c C-l` can be used instead. * The Emacs mode used to define some "abbrevs", unless the user explicitly turned this feature off. The new default is *not* to add any abbrevs. The old default can be obtained by customising `agda2-mode-abbrevs-use-defaults` (a customisation buffer can be obtained by typing `M-x customize-group agda2 RET` after an Agda file has been loaded). Release notes for Agda 2 version 2.2.4 ====================================== Important changes since 2.2.2: * Change to the semantics of `open import` and `open module`. The declaration ```agda open import M ``` now translates to ```agda import A open A ``` instead of ```agda import A open A ``` The same translation is used for `open module M = E …`. Declarations involving the keywords as or public are changed in a corresponding way (`as` always goes with import, and `public` always with open). This change means that import directives do not affect the qualified names when open import/module is used. To get the old behaviour you can use the expanded version above. * Names opened publicly in parameterised modules no longer inherit the module parameters. Example: ```agda module A where postulate X : Set module B (Y : Set) where open A public ``` In Agda 2.2.2 `B.X` has type `(Y : Set) → Set`, whereas in Agda 2.2.4 `B.X` has type Set. * Previously it was not possible to export a given constructor name through two different `open public` statements in the same module. This is now possible. * Unicode subscript digits are now allowed for the hierarchy of universes (`Set₀`, `Set₁`, …): `Set₁` is equivalent to `Set1`. Release notes for Agda 2 version 2.2.2 ====================================== Tools ----- * The `--malonzodir` option has been renamed to `--malonzo-dir`. * The output of `agda --html` is by default placed in a directory called `html`. Infrastructure -------------- * The Emacs mode is included in the Agda Cabal package, and installed by `cabal install`. The recommended way to enable the Emacs mode is to include the following code in `.emacs`: ```elisp (load-file (let ((coding-system-for-read 'utf-8)) (shell-command-to-string "agda-mode locate"))) ``` Release notes for Agda 2 version 2.2.0 ====================================== Important changes since 2.1.2 (which was released 2007-08-16): Language -------- * Exhaustive pattern checking. Agda complains if there are missing clauses in a function definition. * Coinductive types are supported. This feature is under development/evaluation, and may change. http://wiki.portal.chalmers.se/agda/agda.php?n=ReferenceManual.Codatatypes * Another experimental feature: Sized types, which can make it easier to explain why your code is terminating. * Improved constraint solving for functions with constructor headed right hand sides. http://wiki.portal.chalmers.se/agda/agda.php?n=ReferenceManual.FindingTheValuesOfImplicitArguments * A simple, well-typed foreign function interface, which allows use of Haskell functions in Agda code. http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.FFI * The tokens `forall`, `->` and `\` can be written as `∀`, `→` and `λ`. * Absurd lambdas: `λ ()` and `λ {}`. http://thread.gmane.org/gmane.comp.lang.agda/440 * Record fields whose values can be inferred can be omitted. * Agda complains if it spots an unreachable clause, or if a pattern variable "shadows" a hidden constructor of matching type. http://thread.gmane.org/gmane.comp.lang.agda/720 Tools ----- * Case-split: The user interface can replace a pattern variable with the corresponding constructor patterns. You get one new left-hand side for every possible constructor. http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode * The MAlonzo compiler. http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.MAlonzo * A new Emacs input method, which contains bindings for many Unicode symbols, is by default activated in the Emacs mode. http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.UnicodeInput * Highlighted, hyperlinked HTML can be generated from Agda source code. http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.HowToGenerateWebPagesFromSourceCode * The command-line interactive mode (`agda -I`) is no longer supported, but should still work. http://thread.gmane.org/gmane.comp.lang.agda/245 * Reload times when working on large projects are now considerably better. http://thread.gmane.org/gmane.comp.lang.agda/551 Libraries --------- * A standard library is under development. http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Libraries.StandardLibrary Documentation ------------- * The Agda wiki is better organised. It should be easier for a newcomer to find relevant information now. http://wiki.portal.chalmers.se/agda/ Infrastructure -------------- * Easy-to-install packages for Windows and Debian/Ubuntu have been prepared. http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.Download * Agda 2.2.0 is available from Hackage. http://hackage.haskell.org/ Agda-2.5.3/README.md0000644000000000000000000001550413154613124012027 0ustar0000000000000000Agda 2 ====== [![Hackage version](https://img.shields.io/hackage/v/Agda.svg?label=Hackage)](http://hackage.haskell.org/package/Agda) [![Stackage version](https://www.stackage.org/package/Agda/badge/lts?label=Stackage)](https://www.stackage.org/package/Agda) [![Travis Status](https://travis-ci.org/agda/agda.svg?branch=stable-2.5)](https://travis-ci.org/agda/agda) [![Appveyor Status](https://ci.appveyor.com/api/projects/status/x6liln2dol0bg4qw/branch/stable-2.5?svg=true)](https://ci.appveyor.com/project/gallais/agda) [![Documentation Status](https://readthedocs.org/projects/agda/badge/?version=stable-2.5)](http://agda.readthedocs.io/en/stable-2.5/?badge=stable-2.5) Table of contents: * [Documentation](#documentation) * [Prerequisites](#prerequisites) * [Installing Agda](#installing-agda) * [Configuring the Emacs mode](#configuring-the-emacs-mode) * [Installing Emacs under Windows](#installing-emacs-under-windows) Note that this README only discusses installation of Agda, not its standard library. See the [Agda Wiki][agdawiki] for information about the library. Documentation ------------- * [User manual](http://agda.readthedocs.io) * [CHANGELOG](https://github.com/agda/agda/blob/master/CHANGELOG.md) Prerequisites ------------- You need recent versions of the following programs: * GHC: http://www.haskell.org/ghc/ * cabal-install: http://www.haskell.org/cabal/ * Alex: http://www.haskell.org/alex/ * Happy: http://www.haskell.org/happy/ * cpphs: http://projects.haskell.org/cpphs/ * GNU Emacs: http://www.gnu.org/software/emacs/ You should also make sure that programs installed by cabal-install are on your shell's search path. For instructions on installing a suitable version of Emacs under Windows, see [below](#installing-emacs-under-windows). Non-Windows users need to ensure that the development files for the C libraries zlib and ncurses are installed (see http://zlib.net and http://www.gnu.org/software/ncurses/). Your package manager may be able to install these files for you. For instance, on Debian or Ubuntu it should suffice to run apt-get install zlib1g-dev libncurses5-dev as root to get the correct files installed. Optionally one can also install the [ICU](http://site.icu-project.org) library, which is used to implement the `--count-clusters` flag. Under Debian or Ubuntu it may suffice to install `libicu-dev`. Once the ICU library is installed one can hopefully enable the `--count-clusters` flag by giving the `-fenable-cluster-counting` flag to `cabal install`. Note that `make install` by default enables `-fenable-cluster-counting`. ### Note on GHC's CPP language extension Recent versions of Clang's preprocessor don't work well with Haskell. In order to get some dependencies to build, you may need to set up Cabal to have GHC use cpphs by default. You can do this by adding program-default-options ghc-options: -pgmPcpphs -optP--cpp to your .cabal/config file. (You must be using cabal >= 1.18. Note that some packages may not compile with this option set.) You don't need to set this option to install *Agda* from the current development source; Agda.cabal now uses cpphs. Installing Agda --------------- There are several ways to install Agda: ### Using a binary package prepared for your platform Recommended if such a package exists. See the [Agda Wiki][agdawiki]. ### Using a released source package from Hackage Install the prerequisites mentioned above, then run the following commands: cabal update cabal install Agda agda-mode setup The last command tries to set up Emacs for use with Agda. As an alternative you can copy the following text to your .emacs file: (load-file (let ((coding-system-for-read 'utf-8)) (shell-command-to-string "agda-mode locate"))) It is also possible (but not necessary) to compile the Emacs mode's files: agda-mode compile This can, in some cases, give a noticeable speedup. **WARNING**: If you reinstall the Agda mode without recompiling the Emacs Lisp files, then Emacs may continue using the old, compiled files. ### Using the development version of the code You can obtain tarballs of the development version from the [Agda Wiki][agdawiki], or clone the repository. Install the prerequisites discussed in [Prerequisites](#prerequisites). Then, either: *(1a)* Run the following commands in the top-level directory of the Agda source tree to install Agda: cabal update cabal install *(1b)* Run `agda-mode setup` to set up Emacs for use with Agda. Alternatively, add the following text to your .emacs file: (load-file (let ((coding-system-for-read 'utf-8)) (shell-command-to-string "agda-mode locate"))) It is also possible (but not necessary) to compile the Emacs mode's files: agda-mode compile This can, in some cases, give a noticeable speedup. **WARNING**: If you reinstall the Agda mode without recompiling the Emacs Lisp files, then Emacs may continue using the old compiled files. *(2)* Or, you can try to install Agda (including a compiled Emacs mode) by running the following command: make install Configuring the Emacs mode -------------------------- If you want to you can customise the Emacs mode. Just start Emacs and type the following: M-x load-library RET agda2-mode RET M-x customize-group RET agda2 RET This is useful if you want to change the Agda search path, in which case you should change the agda2-include-dirs variable. If you want some specific settings for the Emacs mode you can add them to agda2-mode-hook. For instance, if you do not want to use the Agda input method (for writing various symbols like ∀≥ℕ→π⟦⟧) you can add the following to your .emacs: (add-hook 'agda2-mode-hook '(lambda () ; If you do not want to use any input method: (deactivate-input-method) ; (In some versions of Emacs you should use ; inactivate-input-method instead of ; deactivate-input-method.) ; If you want to use the X input method: (set-input-method "X"))) Note that, on some systems, the Emacs mode changes the default font of the current frame in order to enable many Unicode symbols to be displayed. This only works if the right fonts are available, though. If you want to turn off this feature, then you should customise the agda2-fontset-name variable. ------------------------------------------------------------------------ Installing Emacs under Windows ------------------------------------------------------------------------ A precompiled version of Emacs 24.3, with the necessary mathematical fonts, is available at http://homepage.cs.uiowa.edu/~astump/agda/ [agdawiki]: http://wiki.portal.chalmers.se/agda/pmwiki.php Hacking on Agda --------------- Head to [`HACKING`](https://github.com/agda/agda/blob/master/HACKING) Agda-2.5.3/stack-7.10.3.yaml0000644000000000000000000000025013154613124013255 0ustar0000000000000000resolver: lts-6.35 extra-deps: - STMonadTrans-0.4.3 - cpphs-1.20.8 - equivalence-0.3.2 # Local packages, usually specified by relative directory name packages: - '.' Agda-2.5.3/src/0000755000000000000000000000000013154613124011332 5ustar0000000000000000Agda-2.5.3/src/data/0000755000000000000000000000000013154613124012243 5ustar0000000000000000Agda-2.5.3/src/data/agda.sty0000644000000000000000000005050713154613124013707 0ustar0000000000000000% ---------------------------------------------------------------------- % Some useful commands when doing highlighting of Agda code in LaTeX. % ---------------------------------------------------------------------- \ProvidesPackage{agda} \RequirePackage{ifxetex, ifluatex, xifthen, xcolor, polytable, etoolbox, calc} % https://tex.stackexchange.com/questions/47576/combining-ifxetex-and-ifluatex-with-the-logical-or-operation \newif\ifxetexorluatex \ifxetex \xetexorluatextrue \else \ifluatex \xetexorluatextrue \else \xetexorluatexfalse \fi \fi % ---------------------------------------------------------------------- % Options \DeclareOption{bw} {\newcommand{\AgdaColourScheme}{bw}} \DeclareOption{conor}{\newcommand{\AgdaColourScheme}{conor}} \newif\if@AgdaEnableReferences\@AgdaEnableReferencesfalse \DeclareOption{references}{ \@AgdaEnableReferencestrue } \newif\if@AgdaEnableLinks\@AgdaEnableLinksfalse \DeclareOption{links}{ \@AgdaEnableLinkstrue } % If the "nofontsetup" option is used, then some font packages are % loaded, but specific fonts are not selected. \newif\if@AgdaSetupFonts\@AgdaSetupFontstrue \DeclareOption{nofontsetup}{ \@AgdaSetupFontsfalse } \ProcessOptions\relax % ---------------------------------------------------------------------- % Font setup % XeLaTeX or LuaLaTeX \ifxetexorluatex % Hack to get the amsthm package working. % https://tex.stackexchange.com/questions/130491/xelatex-error-paragraph-ended-before-tempa-was-complete \let\AgdaOpenBracket\[\let\AgdaCloseBracket\] \RequirePackage{fontspec} \let\[\AgdaOpenBracket\let\]\AgdaCloseBracket \RequirePackage{unicode-math} \tracinglostchars=2 % If the font is missing some symbol, then say % so in the compilation output. \if@AgdaSetupFonts \setmainfont [ Ligatures = TeX , BoldItalicFont = xits-bolditalic.otf , BoldFont = xits-bold.otf , ItalicFont = xits-italic.otf ] {xits-regular.otf} \setmathfont{xits-math.otf} \setmonofont[Mapping=tex-text]{FreeMono.otf} % Make mathcal and mathscr appear as different. % https://tex.stackexchange.com/questions/120065/xetex-what-happened-to-mathcal-and-mathscr \setmathfont[range={\mathcal,\mathbfcal},StylisticSet=1]{xits-math.otf} \setmathfont[range=\mathscr]{xits-math.otf} \fi \providecommand{\DeclareUnicodeCharacter}[2]{\relax} % pdfLaTeX \else \RequirePackage{ucs, amsfonts, amssymb} \RequirePackage[safe]{tipa} % See page 12 of the tipa manual for what % safe does. % https://tex.stackexchange.com/questions/1774/how-to-insert-pipe-symbol-in-latex \RequirePackage[T1]{fontenc} \RequirePackage[utf8x]{inputenc} \fi % ---------------------------------------------------------------------- % Colour schemes. \providecommand{\AgdaColourScheme}{standard} % ---------------------------------------------------------------------- % References to code (needs additional post-processing of tex files to % work, see wiki for details). \if@AgdaEnableReferences \RequirePackage{catchfilebetweentags, xstring} \newcommand{\AgdaRef}[2][]{% \StrSubstitute{#2}{\_}{AgdaUnderscore}[\tmp]% \ifthenelse{\isempty{#1}}% {\ExecuteMetaData{AgdaTag-\tmp}}% {\ExecuteMetaData{#1}{AgdaTag-\tmp}} } \fi \providecommand{\AgdaRef}[2][]{#2} % ---------------------------------------------------------------------- % Links (only done if the option is passed and the user has loaded the % hyperref package). \if@AgdaEnableLinks \@ifpackageloaded{hyperref}{ % List that holds added targets. \newcommand{\AgdaList}[0]{} \newtoggle{AgdaIsElem} \newcounter{AgdaIndex} \newcommand{\AgdaLookup}[3]{% \togglefalse{AgdaIsElem}% \setcounter{AgdaIndex}{0}% \renewcommand*{\do}[1]{% \ifstrequal{#1}{##1}% {\toggletrue{AgdaIsElem}\listbreak}% {\stepcounter{AgdaIndex}}}% \dolistloop{\AgdaList}% \iftoggle{AgdaIsElem}{#2}{#3}% } \newcommand*{\AgdaTargetHelper}[1]{% \AgdaLookup{#1}% {\PackageError{agda}{``#1'' used as target more than once}% {Overloaded identifiers and links do not% work well, consider using unique% \MessageBreak identifiers instead.}% }% {\listadd{\AgdaList}{#1}% \hypertarget{Agda\theAgdaIndex}{}% }% } \newcommand{\AgdaTarget}[1]{\forcsvlist{\AgdaTargetHelper}{#1}} \newcommand{\AgdaLink}[1]{% \AgdaLookup{#1}% {\hyperlink{Agda\theAgdaIndex}{#1}}% {#1}% } }{\PackageError{agda}{Load the hyperref package before the agda package}{}} \fi \providecommand{\AgdaTarget}[1]{} \providecommand{\AgdaLink}[1]{#1} % ---------------------------------------------------------------------- % Font styles. \ifxetexorluatex \newcommand{\AgdaFontStyle}[1]{\ensuremath{\mathsf{#1}}} \ifthenelse{\equal{\AgdaColourScheme}{bw}}{ \newcommand{\AgdaKeywordFontStyle}[1]{\underline{#1}} }{ \newcommand{\AgdaKeywordFontStyle}[1]{\ensuremath{\mathsf{#1}}} } \newcommand{\AgdaStringFontStyle}[1]{\ensuremath{\texttt{#1}}} \newcommand{\AgdaCommentFontStyle}[1]{\ensuremath{\texttt{#1}}} \newcommand{\AgdaBoundFontStyle}[1]{\ensuremath{\mathit{#1}}} \else \newcommand{\AgdaFontStyle}[1]{\textsf{#1}} \ifthenelse{\equal{\AgdaColourScheme}{bw}}{ \newcommand{\AgdaKeywordFontStyle}[1]{\underline{#1}} }{ \newcommand{\AgdaKeywordFontStyle}[1]{\textsf{#1}} } \newcommand{\AgdaStringFontStyle}[1]{\texttt{#1}} \newcommand{\AgdaCommentFontStyle}[1]{\texttt{#1}} \newcommand{\AgdaBoundFontStyle}[1]{\textit{#1}} \fi % ---------------------------------------------------------------------- % Colours. % ---------------------------------- % The black and white colour scheme. \ifthenelse{\equal{\AgdaColourScheme}{bw}}{ % Aspect colours. \definecolor{AgdaComment} {HTML}{000000} \definecolor{AgdaOption} {HTML}{000000} \definecolor{AgdaKeyword} {HTML}{000000} \definecolor{AgdaString} {HTML}{000000} \definecolor{AgdaNumber} {HTML}{000000} \definecolor{AgdaSymbol} {HTML}{000000} \definecolor{AgdaPrimitiveType}{HTML}{000000} \definecolor{AgdaOperator} {HTML}{000000} % NameKind colours. \definecolor{AgdaBound} {HTML}{000000} \definecolor{AgdaInductiveConstructor} {HTML}{000000} \definecolor{AgdaCoinductiveConstructor}{HTML}{000000} \definecolor{AgdaDatatype} {HTML}{000000} \definecolor{AgdaField} {HTML}{000000} \definecolor{AgdaFunction} {HTML}{000000} \definecolor{AgdaMacro} {HTML}{000000} \definecolor{AgdaModule} {HTML}{000000} \definecolor{AgdaPostulate} {HTML}{000000} \definecolor{AgdaPrimitive} {HTML}{000000} \definecolor{AgdaRecord} {HTML}{000000} \definecolor{AgdaArgument} {HTML}{000000} % Other aspect colours. \definecolor{AgdaDottedPattern} {HTML}{000000} \definecolor{AgdaUnsolvedMeta} {HTML}{D3D3D3} \definecolor{AgdaTerminationProblem}{HTML}{BEBEBE} \definecolor{AgdaIncompletePattern} {HTML}{D3D3D3} \definecolor{AgdaError} {HTML}{696969} % Misc. \definecolor{AgdaHole} {HTML}{BEBEBE} % ---------------------------------- % Conor McBride's colour scheme. }{ \ifthenelse{\equal{\AgdaColourScheme}{conor}}{ % Aspect colours. \definecolor{AgdaComment} {HTML}{B22222} \definecolor{AgdaOption} {HTML}{000000} \definecolor{AgdaKeyword} {HTML}{000000} \definecolor{AgdaString} {HTML}{000000} \definecolor{AgdaNumber} {HTML}{000000} \definecolor{AgdaSymbol} {HTML}{000000} \definecolor{AgdaPrimitiveType}{HTML}{0000CD} \definecolor{AgdaOperator} {HTML}{000000} % NameKind colours. \definecolor{AgdaBound} {HTML}{A020F0} \definecolor{AgdaInductiveConstructor} {HTML}{8B0000} \definecolor{AgdaCoinductiveConstructor}{HTML}{8B0000} \definecolor{AgdaDatatype} {HTML}{0000CD} \definecolor{AgdaField} {HTML}{8B0000} \definecolor{AgdaFunction} {HTML}{006400} \definecolor{AgdaMacro} {HTML}{006400} \definecolor{AgdaModule} {HTML}{006400} \definecolor{AgdaPostulate} {HTML}{006400} \definecolor{AgdaPrimitive} {HTML}{006400} \definecolor{AgdaRecord} {HTML}{0000CD} \definecolor{AgdaArgument} {HTML}{404040} % Other aspect colours. \definecolor{AgdaDottedPattern} {HTML}{000000} \definecolor{AgdaUnsolvedMeta} {HTML}{FFD700} \definecolor{AgdaTerminationProblem}{HTML}{FF0000} \definecolor{AgdaIncompletePattern} {HTML}{A020F0} \definecolor{AgdaError} {HTML}{F4A460} % Misc. \definecolor{AgdaHole} {HTML}{9DFF9D} % ---------------------------------- % The standard colour scheme. }{ % Aspect colours. \definecolor{AgdaComment} {HTML}{B22222} \definecolor{AgdaOption} {HTML}{000000} \definecolor{AgdaKeyword} {HTML}{CD6600} \definecolor{AgdaString} {HTML}{B22222} \definecolor{AgdaNumber} {HTML}{A020F0} \definecolor{AgdaSymbol} {HTML}{404040} \definecolor{AgdaPrimitiveType}{HTML}{0000CD} \definecolor{AgdaOperator} {HTML}{000000} % NameKind colours. \definecolor{AgdaBound} {HTML}{000000} \definecolor{AgdaInductiveConstructor} {HTML}{008B00} \definecolor{AgdaCoinductiveConstructor}{HTML}{8B7500} \definecolor{AgdaDatatype} {HTML}{0000CD} \definecolor{AgdaField} {HTML}{EE1289} \definecolor{AgdaFunction} {HTML}{0000CD} \definecolor{AgdaMacro} {HTML}{458B74} \definecolor{AgdaModule} {HTML}{A020F0} \definecolor{AgdaPostulate} {HTML}{0000CD} \definecolor{AgdaPrimitive} {HTML}{0000CD} \definecolor{AgdaRecord} {HTML}{0000CD} \definecolor{AgdaArgument} {HTML}{404040} % Other aspect colours. \definecolor{AgdaDottedPattern} {HTML}{000000} \definecolor{AgdaUnsolvedMeta} {HTML}{FFFF00} \definecolor{AgdaTerminationProblem}{HTML}{FFA07A} \definecolor{AgdaIncompletePattern} {HTML}{F5DEB3} \definecolor{AgdaError} {HTML}{FF0000} % Misc. \definecolor{AgdaHole} {HTML}{9DFF9D} }} % ---------------------------------------------------------------------- % Commands. \newcommand{\AgdaNoSpaceMath}[1] {\begingroup\thickmuskip=0mu\medmuskip=0mu#1\endgroup} % Aspect commands. \newcommand{\AgdaComment} [1] {\AgdaNoSpaceMath{\AgdaCommentFontStyle{\textcolor{AgdaComment}{#1}}}} \newcommand{\AgdaOption} [1] {\AgdaNoSpaceMath{\AgdaCommentFontStyle{\textcolor{AgdaOption}{#1}}}} \newcommand{\AgdaKeyword} [1] {\AgdaNoSpaceMath{\AgdaKeywordFontStyle{\textcolor{AgdaKeyword}{#1}}}} \newcommand{\AgdaString} [1] {\AgdaNoSpaceMath{\AgdaStringFontStyle{\textcolor{AgdaString}{#1}}}} \newcommand{\AgdaNumber} [1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaNumber}{#1}}}} \newcommand{\AgdaSymbol} [1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaSymbol}{#1}}}} \newcommand{\AgdaPrimitiveType}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaPrimitiveType}{#1}}}} \newcommand{\AgdaOperator} [1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaOperator}{#1}}}} % NameKind commands. % The user can control the typesetting of (certain) individual tokens % by redefining the following command. The first argument is the token % and the second argument the thing to be typeset (sometimes just the % token, sometimes \AgdaLink{}). Example: % % \usepackage{ifthen} % % % Insert extra space before some tokens. % \DeclareRobustCommand{\AgdaFormat}[2]{% % \ifthenelse{ % \equal{#1}{≡⟨} \OR % \equal{#1}{≡⟨⟩} \OR % \equal{#1}{∎} % }{\ }{}#2} % % Note the use of \DeclareRobustCommand. \newcommand{\AgdaFormat}[2]{#2} \newcommand{\AgdaBound}[1] {\AgdaNoSpaceMath{\AgdaBoundFontStyle{\textcolor{AgdaBound}{\AgdaFormat{#1}{#1}}}}} \newcommand{\AgdaInductiveConstructor}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaInductiveConstructor}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}} \newcommand{\AgdaCoinductiveConstructor}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaCoinductiveConstructor}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}} \newcommand{\AgdaDatatype}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaDatatype}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}} \newcommand{\AgdaField}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaField}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}} \newcommand{\AgdaFunction}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaFunction}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}} \newcommand{\AgdaMacro}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaMacro}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}} \newcommand{\AgdaModule}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaModule}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}} \newcommand{\AgdaPostulate}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaPostulate}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}} \newcommand{\AgdaPrimitive}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaPrimitive}{\AgdaFormat{#1}{#1}}}}} \newcommand{\AgdaRecord}[1] {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaRecord}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}} \newcommand{\AgdaArgument}[1] {\AgdaNoSpaceMath{\AgdaBoundFontStyle{\textcolor{AgdaArgument}{\AgdaFormat{#1}{#1}}}}} % Other aspect commands. \newcommand{\AgdaFixityOp} [1]{\AgdaNoSpaceMath{$#1$}} \newcommand{\AgdaDottedPattern} [1]{\textcolor{AgdaDottedPattern}{#1}} \newcommand{\AgdaUnsolvedMeta} [1] {\AgdaFontStyle{\colorbox{AgdaUnsolvedMeta}{#1}}} \newcommand{\AgdaTerminationProblem}[1] {\AgdaFontStyle{\colorbox{AgdaTerminationProblem}{#1}}} \newcommand{\AgdaIncompletePattern} [1]{\colorbox{AgdaIncompletePattern}{#1}} \newcommand{\AgdaError} [1] {\AgdaFontStyle{\textcolor{AgdaError}{\underline{#1}}}} % Misc. \newcommand{\AgdaHole}[1]{\colorbox{AgdaHole}{#1}} \long\def\AgdaHide#1{\ignorespaces} % Used to hide code from LaTeX. % ---------------------------------------------------------------------- % The code environment. \newcommand{\AgdaCodeStyle}{} % \newcommand{\AgdaCodeStyle}{\tiny} \ifdefined\mathindent {} \else \newdimen\mathindent\mathindent\leftmargini \fi % Adds the given amount of vertical space and starts a new line. % % The implementation comes from lhs2TeX's polycode.fmt, written by % Andres Löh. \newcommand{\AgdaNewlineWithVerticalSpace}[1]{% {\parskip=0pt\parindent=0pt\par\vskip #1\noindent}} % 0: No space around code. 1: Space around code. \newcounter{Agda@SpaceAroundCode} % Use this command to avoid extra space around code blocks. \newcommand{\AgdaNoSpaceAroundCode}{% \setcounter{Agda@SpaceAroundCode}{0}} % Use this command to include extra space around code blocks. \newcommand{\AgdaSpaceAroundCode}{% \setcounter{Agda@SpaceAroundCode}{1}} % By default space is inserted around code blocks. \AgdaSpaceAroundCode{} % Sometimes one might want to break up a code block into multiple % pieces, but keep code in different blocks aligned with respect to % each other. Then one can use the AgdaAlign environment. Example % usage: % % \begin{AgdaAlign} % \begin{code} % code % code (more code) % \end{code} % Explanation... % \begin{code} % aligned with "code" % code (aligned with (more code)) % \end{code} % \end{AgdaAlign} % % Note that AgdaAlign environments should not be nested. % % Sometimes one might also want to hide code in the middle of a code % block. This can be accomplished in the following way: % % \begin{AgdaAlign} % \begin{code} % visible % \end{code} % \AgdaHide{ % \begin{code} % hidden % \end{code}} % \begin{code} % visible % \end{code} % \end{AgdaAlign} % % However, the result may be ugly: extra space is perhaps inserted % around the code blocks. % % The AgdaSuppressSpace environment ensures that extra space is only % inserted before the first code block, and after the last one (but % not if \AgdaNoSpaceAroundCode{} is used). % % The environment takes one argument, the number of wrapped code % blocks (excluding hidden ones). Example usage: % % \begin{AgdaAlign} % \begin{code} % code % more code % \end{code} % Explanation... % \begin{AgdaSuppressSpace}{2} % \begin{code} % aligned with "code" % aligned with "more code" % \end{code} % \AgdaHide{ % \begin{code} % hidden code % \end{code}} % \begin{code} % also aligned with "more code" % \end{code} % \end{AgdaSuppressSpace} % \end{AgdaAlign} % % Note that AgdaSuppressSpace environments should not be nested. % % There is also a combined environment, AgdaMultiCode, that combines % the effects of AgdaAlign and AgdaSuppressSpace. % 0: AgdaAlign is not active. 1: AgdaAlign is active. \newcounter{Agda@Align} \setcounter{Agda@Align}{0} % The current code block. \newcounter{Agda@AlignCurrent} \newcommand{\Agda@AlignStart}{% \setcounter{Agda@Align}{1}% \setcounter{Agda@AlignCurrent}{1}} \newcommand{\Agda@AlignEnd}{\setcounter{Agda@Align}{0}} \newenvironment{AgdaAlign}{% \Agda@AlignStart{}}{% \Agda@AlignEnd{}% \ignorespacesafterend} % 0: AgdaSuppressSpace is not active. 1: AgdaSuppressSpace is active. \newcounter{Agda@Suppress} \setcounter{Agda@Suppress}{0} % The current code block. \newcounter{Agda@SuppressCurrent} % The expected number of code blocks. \newcounter{Agda@SuppressLast} \newcommand{\Agda@SuppressStart}[1]{% \setcounter{Agda@Suppress}{1}% \setcounter{Agda@SuppressLast}{#1}% \setcounter{Agda@SuppressCurrent}{1}} \newcommand{\Agda@SuppressEnd}{\setcounter{Agda@Suppress}{0}} \newenvironment{AgdaSuppressSpace}[1]{% \Agda@SuppressStart{#1}}{% \Agda@SuppressEnd{}% \ignorespacesafterend} \newenvironment{AgdaMultiCode}[1]{% \Agda@AlignStart{}% \Agda@SuppressStart{#1}}{% \Agda@SuppressEnd{}% \Agda@AlignEnd{}% \ignorespacesafterend} % Vertical space used for empty lines. By default \baselineskip. \newlength{\AgdaEmptySkip} \setlength{\AgdaEmptySkip}{\baselineskip} % Extra space to be inserted for empty lines (the difference between % \AgdaEmptySkip and \baselineskip). Used internally. \newlength{\AgdaEmptyExtraSkip} % The code environment. % % The implementation is based on plainhscode in lhs2TeX's % polycode.fmt, written by Andres Löh. \newenvironment{code}{% \ifthenelse{\value{Agda@SpaceAroundCode} = 0 \or% \not \(\value{Agda@Suppress} = 0 \or% \value{Agda@SuppressCurrent} = 1\)}{% \AgdaNewlineWithVerticalSpace{0pt}}{% \AgdaNewlineWithVerticalSpace{\abovedisplayskip}}% \advance\leftskip\mathindent% \AgdaCodeStyle% \setlength{\AgdaEmptyExtraSkip}{\AgdaEmptySkip - \baselineskip}% \pboxed% \ifthenelse{\value{Agda@Align} = 0}{}{% \ifthenelse{\value{Agda@AlignCurrent} = 1}{% \savecolumns}{% \restorecolumns}}}{% \endpboxed% \ifthenelse{\value{Agda@SpaceAroundCode} = 0 \or% \not \(\value{Agda@Suppress} = 0 \or% \value{Agda@SuppressCurrent} =% \value{Agda@SuppressLast}\)}{% \AgdaNewlineWithVerticalSpace{0pt}}{% \AgdaNewlineWithVerticalSpace{\belowdisplayskip}}% \stepcounter{Agda@AlignCurrent}% \stepcounter{Agda@SuppressCurrent}% \ignorespacesafterend} % Space inserted after tokens. \newcommand{\AgdaSpace}{ } % Space inserted to indent something. \newcommand{\AgdaIndentSpace}{\AgdaSpace{}$\;\;$} % Default column for polytable. \defaultcolumn{@{}l@{\AgdaSpace{}}} % \AgdaIndent expects a non-negative integer as its only argument. % This integer should be the distance, in code blocks, to the thing % relative to which the text is indented. % % The default implementation only indents if the thing that the text % is indented relative to exists in the same code block or is wrapped % in the same AgdaAlign or AgdaMultiCode environment. \newcommand{\AgdaIndent}[1]{% \ifthenelse{#1 = 0 \OR \( \value{Agda@Align} = 1 \AND #1 < \value{Agda@AlignCurrent} \)}{\AgdaIndentSpace{}}{}} \endinput Agda-2.5.3/src/data/postprocess-latex.pl0000644000000000000000000000071613154613124016303 0ustar0000000000000000#!/usr/bin/env perl use strict; use warnings; my $tag_prefix = "AgdaTag"; my $underscore = "AgdaUnderscore"; my $commands = qr"(InductiveConstructor|CoinductiveConstructor\ |Datatype|Field|Function|Module|Postulate|Record)"; while (<>) { s|(\\Agda$commands){(.*?)} | my $cmd = $1; my $arg = $3; my $tag = "$tag_prefix-$3" =~ s/\\_/$underscore/gr; $_ = "\n%<*$tag>\n$cmd\{$arg\}\n%\n"; |gxe; print; } Agda-2.5.3/src/data/Agda.css0000644000000000000000000000230313154613124013607 0ustar0000000000000000/* Aspects. */ .Comment { color: #B22222 } .Keyword { color: #CD6600 } .String { color: #B22222 } .Number { color: #A020F0 } .Symbol { color: #404040 } .PrimitiveType { color: #0000CD } .Operator {} /* NameKinds. */ .Bound { color: black } .InductiveConstructor { color: #008B00 } .CoinductiveConstructor { color: #8B7500 } .Datatype { color: #0000CD } .Field { color: #EE1289 } .Function { color: #0000CD } .Module { color: #A020F0 } .Postulate { color: #0000CD } .Primitive { color: #0000CD } .Record { color: #0000CD } /* OtherAspects. */ .DottedPattern {} .UnsolvedMeta { color: black; background: yellow } .UnsolvedConstraint { color: black; background: yellow } .TerminationProblem { color: black; background: #FFA07A } .IncompletePattern { color: black; background: #F5DEB3 } .Error { color: red; text-decoration: underline } .TypeChecks { color: black; background: #ADD8E6 } /* Standard attributes. */ a { text-decoration: none } a[href]:hover { background-color: #B4EEB4 } Agda-2.5.3/src/data/emacs-mode/0000755000000000000000000000000013154613124014255 5ustar0000000000000000Agda-2.5.3/src/data/emacs-mode/agda2-queue.el0000644000000000000000000000264713154613124016710 0ustar0000000000000000;;; agda2-queue.el --- Simple FIFO character queues. (defun agda2-queue-empty () "Creates a new empty FIFO character queue. Queues are represented as pairs. The car contains the queue. If the queue is empty, then the cdr contains the symbol nil, and otherwise it points to the queue's last cons-cell." (cons nil nil)) (defun agda2-queue-is-prefix-of (prefix queue) "Returns a non-nil result iff the string PREFIX is a prefix of QUEUE. Linear in the length of PREFIX." (let ((queue (car queue)) (prefix (append prefix nil))) (while (and (consp queue) (consp prefix) (equal (car queue) (car prefix))) (pop queue) (pop prefix)) (null prefix))) (defun agda2-queue-enqueue (queue string) "Adds the characters in STRING to the end of QUEUE. This function updates QUEUE destructively, and is linear in the length of STRING." (let ((chars (append string nil))) (when (consp chars) (if (null (cdr queue)) (setcar queue chars) (setcdr (cdr queue) chars)) (setcdr queue (last chars)))) queue) (defun agda2-queue-from-string (string) "Creates a new FIFO containing the characters in STRING. Linear in the length of STRING." (agda2-queue-enqueue (agda2-queue-empty) string)) (defun agda2-queue-to-string (queue) "Constructs a string containing all the characters in QUEUE. Linear in the length of QUEUE." (concat "" (car queue))) (provide 'agda2-queue) Agda-2.5.3/src/data/emacs-mode/agda2.el0000644000000000000000000000105613154613124015557 0ustar0000000000000000;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Agda mode code which should run before the first Agda file is ;; loaded (defvar agda2-directory (file-name-directory load-file-name) "Path to the directory that contains agda2.el(c).") (add-to-list 'load-path (or agda2-directory (car load-path))) (autoload 'agda2-mode "agda2-mode" "Major mode for editing Agda files (version ≥ 2)." t) (add-to-list 'auto-mode-alist '("\\.l?agda\\'" . agda2-mode)) (modify-coding-system-alist 'file "\\.l?agda\\'" 'utf-8) (provide 'agda2) Agda-2.5.3/src/data/emacs-mode/agda2-abbrevs.el0000644000000000000000000000507713154613124017210 0ustar0000000000000000;; agda2-abbrevs.el --- Default Agda abbrevs ;;; Commentary: ;;; Code: ;; Skeletons (require 'skeleton) (define-skeleton agda2-abbrevs-module "Inserts a module header template." nil "module " _ " where\n") (define-skeleton agda2-abbrevs-data "Inserts a data template." nil "data " _ " : Set where\n") (define-skeleton agda2-abbrevs-record "Inserts a record type template." nil "record " _ " : Set where\n" " field\n") (define-skeleton agda2-abbrevs-record-value "Inserts a record value template." nil "record {" _ "}") (define-skeleton agda2-abbrevs-using "Inserts a using template." nil "using (" _ ")") (define-skeleton agda2-abbrevs-hiding "Inserts a hiding template." nil "hiding (" _ ")") (define-skeleton agda2-abbrevs-renaming "Inserts a renaming template." nil "renaming (" _ " to " _ ")") (define-skeleton agda2-abbrevs-forall "Inserts a forall template." nil "∀ {" _ "} ") (define-skeleton agda2-abbrevs-code-block "Inserts a code block." nil "\\begin{code}\n " _ "\n\\end{code}\n") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Abbrevs (defvar agda2-abbrevs-defaults '( ("m" "" agda2-abbrevs-module) ("d" "" agda2-abbrevs-data) ("c" "" agda2-abbrevs-code-block) ("re" "" agda2-abbrevs-record) ("rec" "" agda2-abbrevs-record-value) ("u" "" agda2-abbrevs-using) ("h" "" agda2-abbrevs-hiding) ("r" "" agda2-abbrevs-renaming) ("w" "where\n") ("po" "postulate") ("a" "abstract\n") ("pr" "private\n") ("pu" "public") ("mu" "mutual\n") ("f" "" agda2-abbrevs-forall) ("oi" "open import ")) "Abbreviations defined by default in the Agda mode.") (defcustom agda2-mode-abbrevs-use-defaults nil "If non-nil include the default Agda mode abbrevs in `agda2-mode-abbrev-table'. The abbrevs are designed to be expanded explicitly, so users of `abbrev-mode' probably do not want to include them. Restart Emacs in order for this change to take effect." :group 'agda2 :type '(choice (const :tag "Yes" t) (const :tag "No" nil))) (defvar agda2-mode-abbrev-table nil "Agda mode abbrev table.") (define-abbrev-table 'agda2-mode-abbrev-table (if agda2-mode-abbrevs-use-defaults (mapcar (lambda (abbrev) (append abbrev (make-list (- 4 (length abbrev)) nil) '((:system t)))) agda2-abbrevs-defaults))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Administrative details (provide 'agda2-abbrevs) ;;; agda2-abbrevs.el ends here Agda-2.5.3/src/data/emacs-mode/agda2-mode.el0000644000000000000000000024212313154613124016503 0ustar0000000000000000;;; agda2-mode.el --- Major mode for Agda ;;; Commentary: ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; Dependency ;;; Code: (defvar agda2-version "2.5.3" "The version of the Agda mode. Note that the same version of the Agda executable must be used.") (require 'cl) (require 'compile) (require 'pp) (require 'time-date) (require 'eri) (require 'annotation) (require 'fontset) (require 'agda-input) (require 'agda2) (require 'agda2-highlight) (require 'agda2-abbrevs) (require 'agda2-queue) (eval-and-compile ;; Load filladapt, if it is installed. (condition-case nil (require 'filladapt) (error nil)) (unless (fboundp 'overlays-in) (load "overlay")) ; for Xemacs (unless (fboundp 'propertize) ; for Xemacs 21.4 ;; FIXME: XEmacs-21.4 (patch 22) does have `propertize' and so does Emacs-22 ;; (and agda2-mode doesn't work in Emacs-21, AFAICT). (defun propertize (string &rest properties) "Return a copy of STRING with text properties added. First argument is the string to copy. Remaining arguments form a sequence of PROPERTY VALUE pairs for text properties to add to the result." (let ((str (copy-sequence string))) (add-text-properties 0 (length str) properties str) str))) (unless (fboundp 'prog-mode) ;For Emacs<24. (defalias 'prog-mode 'fundamental-mode))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; Utilities (defmacro agda2-let (varbind funcbind &rest body) "Expands to (let* VARBIND (cl-labels FUNCBIND BODY...)). Or possibly (let* VARBIND (labels FUNCBIND BODY...))." (declare (debug ((&rest [&or symbolp (symbolp form)]) (&rest (cl-defun)) body)) (indent 2)) ;; Use cl-labels if available to avoid obsolescence warnings. `(let* ,varbind (,(if (fboundp 'cl-labels) 'cl-labels 'labels) ,funcbind ,@body))) (defun agda2-chunkify (n xs) "Returns a list containing chunks of XS of length at most N. All the elements of XS are included, in their original order." (let ((i 0) (len (length xs)) out) (while (< i len) (let ((new-i (+ i (min n (- len i))))) (setq out (cons (subseq xs i new-i) out)) (setq i new-i))) (nreverse out))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; User options (defgroup agda2 nil "Major mode for interactively developing Agda programs." :group 'languages) (defcustom agda2-program-name "agda" "The name of the Agda executable." :type 'string :group 'agda2) (defcustom agda2-program-args nil "Command-line arguments given to the Agda executable (one per string). Note: Do not give several arguments in the same string. The flag \"--interaction\" is always included as the first argument, and does not need to be listed here." :type '(repeat string) :group 'agda2) (defvar agda2-backends '("GHC" "JS" "LaTeX" "QuickLaTeX") "Compilation backends.") (defcustom agda2-backend "" "The backend used to compile Agda programs (leave blank to ask every time)." :type 'string :group 'agda2) (defcustom agda2-toplevel-module "Agda.Interaction.GhciTop" "The name of the Agda toplevel module." :type 'string :group 'agda2) (defcustom agda2-warning-window-max-height 0.35 "The maximum height of the warning window. A multiple of the frame height." :type 'number :group 'agda2) (defcustom agda2-information-window-max-height 0.35 "The maximum height of the information window. A multiple of the frame height." :type 'number :group 'agda2) (defcustom agda2-fontset-name (unless (or (eq window-system 'mac) ;; Emacs-23 uses a revamped font engine which should ;; make agda2-fontset-name unnecessary in most cases. ;; And if it turns out to be necessary, we should ;; probably use face-remapping-alist rather than ;; set-frame-font so the special font only applies to ;; Agda buffers, and so it applies in all frames where ;; Agda buffers are displayed. (boundp 'face-remapping-alist)) "fontset-agda2") "Default font to use in the selected frame when activating the Agda mode. This is only used if it's non-nil and Emacs is not running in a terminal. Note that this setting (if non-nil) affects non-Agda buffers as well, and that you have to restart Emacs if you want settings to this variable to take effect." :type '(choice (string :tag "Fontset name") (const :tag "Do not change the font" nil)) :group 'agda2) (defcustom agda2-fontset-spec-of-fontset-agda2 "-*-fixed-Medium-r-Normal-*-18-*-*-*-c-*-fontset-agda2, ascii:-Misc-Fixed-Medium-R-Normal--18-120-100-100-C-90-ISO8859-1, latin-iso8859-2:-*-Fixed-*-r-*-*-18-*-*-*-c-*-iso8859-2, latin-iso8859-3:-*-Fixed-*-r-*-*-18-*-*-*-c-*-iso8859-3, latin-iso8859-4:-*-Fixed-*-r-*-*-18-*-*-*-c-*-iso8859-4, cyrillic-iso8859-5:-*-Fixed-*-r-*-*-18-*-*-*-c-*-iso8859-5, greek-iso8859-7:-*-Fixed-*-r-*-*-18-*-*-*-c-*-iso8859-7, latin-iso8859-9:-*-Fixed-*-r-*-*-18-*-*-*-c-*-iso8859-9, mule-unicode-0100-24ff:-Misc-Fixed-Medium-R-Normal--18-120-100-100-C-90-ISO10646-1, mule-unicode-2500-33ff:-Misc-Fixed-Medium-R-Normal--18-120-100-100-C-90-ISO10646-1, mule-unicode-e000-ffff:-Misc-Fixed-Medium-R-Normal--18-120-100-100-C-90-ISO10646-1, japanese-jisx0208:-Misc-Fixed-Medium-R-Normal-ja-18-*-*-*-C-*-JISX0208.1990-0, japanese-jisx0212:-Misc-Fixed-Medium-R-Normal-ja-18-*-*-*-C-*-JISX0212.1990-0, thai-tis620:-Misc-Fixed-Medium-R-Normal--24-240-72-72-C-120-TIS620.2529-1, lao:-Misc-Fixed-Medium-R-Normal--24-240-72-72-C-120-MuleLao-1, tibetan:-TibMdXA-fixed-medium-r-normal--16-160-72-72-m-160-MuleTibetan-0, tibetan-1-column:-TibMdXA-fixed-medium-r-normal--16-160-72-72-m-80-MuleTibetan-1, korean-ksc5601:-Daewoo-Mincho-Medium-R-Normal--16-120-100-100-C-160-KSC5601.1987-0, chinese-gb2312:-ISAS-Fangsong ti-Medium-R-Normal--16-160-72-72-c-160-GB2312.1980-0, chinese-cns11643-1:-HKU-Fixed-Medium-R-Normal--16-160-72-72-C-160-CNS11643.1992.1-0, chinese-big5-1:-ETen-Fixed-Medium-R-Normal--16-150-75-75-C-160-Big5.ETen-0, chinese-big5-2:-ETen-Fixed-Medium-R-Normal--16-150-75-75-C-160-Big5.ETen-0" "Specification of the \"fontset-agda2\" fontset. This fontset is only created if `agda2-fontset-name' is \"fontset-agda2\" and Emacs is not run in a terminal. Note that the text \"fontset-agda2\" has to be part of the string (in a certain way; see the default setting) in order for the agda2 fontset to be created properly. Note also that the default setting may not work unless suitable fonts are installed on your system. Refer to the README file accompanying the Agda distribution for more details. Note finally that you have to restart Emacs if you want settings to this variable to take effect." :group 'agda2 :type 'string) (if (and (equal agda2-fontset-name "fontset-agda2") window-system) (create-fontset-from-fontset-spec agda2-fontset-spec-of-fontset-agda2 t t)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; Global and buffer-local vars, initialization (defvar agda2-mode-syntax-table (let ((tbl (make-syntax-table))) ;; Set the syntax of every char to "w" except for those whose default ;; syntax in `standard-syntax-table' is `paren' or `whitespace'. (map-char-table (lambda (keys val) ;; `keys' here can be a normal char, a generic char ;; (Emacs<23), or a char range (Emacs>=23). (unless (memq (car val) (eval-when-compile (mapcar 'car (list (string-to-syntax "(") (string-to-syntax ")") (string-to-syntax " "))))) (modify-syntax-entry keys "w" tbl))) (standard-syntax-table)) ;; Then override the remaining special cases. (dolist (cs '((?{ . "(}1n") (?} . "){4n") (?- . "w 123b") (?\n . "> b") (?. . ".") (?\; . ".") (?_ . ".") (?! . "."))) (modify-syntax-entry (car cs) (cdr cs) tbl)) tbl) "Syntax table used by the Agda mode: {} | Comment characters, matching parentheses. - | Comment character, word constituent. \n | Comment ender. .;_! | Punctuation. Remaining characters inherit their syntax classes from the standard syntax table if that table treats them as matching parentheses or whitespace. Otherwise they are treated as word constituents.") (defconst agda2-command-table `( (agda2-load "\C-c\C-l" (global) "Load") (agda2-load "\C-c\C-x\C-l") (agda2-compile "\C-c\C-x\C-c" (global) "Compile") (agda2-quit "\C-c\C-x\C-q" (global) "Quit") (agda2-restart "\C-c\C-x\C-r" (global) "Kill and restart Agda") (agda2-abort "\C-c\C-x\C-a" (global) "Abort a command") (agda2-remove-annotations "\C-c\C-x\C-d" (global) "Remove goals and highlighting (\"deactivate\")") (agda2-display-implicit-arguments "\C-c\C-x\C-h" (global) "Toggle display of hidden arguments") (agda2-show-constraints ,(kbd "C-c C-=") (global) "Show constraints") (agda2-solve-maybe-all ,(kbd "C-c C-s") (local global) "Solve constraints") (agda2-show-goals ,(kbd "C-c C-?") (global) "Show goals") (agda2-next-goal "\C-c\C-f" (global) "Next goal") ; Forward. (agda2-previous-goal "\C-c\C-b" (global) "Previous goal") ; Back. (agda2-give ,(kbd "C-c C-SPC") (local) "Give") (agda2-refine "\C-c\C-r" (local) "Refine") (agda2-auto "\C-c\C-a" (local) "Auto") (agda2-make-case "\C-c\C-c" (local) "Case") (agda2-goal-type "\C-c\C-t" (local) "Goal type") (agda2-show-context "\C-c\C-e" (local) "Context (environment)") (agda2-helper-function-type "\C-c\C-h" (local) "Helper function type") (agda2-infer-type-maybe-toplevel "\C-c\C-d" (local global) "Infer (deduce) type") (agda2-why-in-scope-maybe-toplevel "\C-c\C-w" (local global) "Explain why a particular name is in scope") (agda2-goal-and-context ,(kbd "C-c C-,") (local) "Goal type and context") (agda2-goal-and-context-and-inferred ,(kbd "C-c C-.") (local) "Goal type, context and inferred type") (agda2-goal-and-context-and-checked ,(kbd "C-c C-;") (local) "Goal type, context and checked type") (agda2-search-about-toplevel ,(kbd "C-c C-z") (local global) "Search About") (agda2-module-contents-maybe-toplevel ,(kbd "C-c C-o") (local global) "Module contents") (agda2-compute-normalised-maybe-toplevel "\C-c\C-n" (local global) "Evaluate term to normal form") (describe-char nil (global) "Information about the character at point") (agda2-comment-dwim-rest-of-buffer ,(kbd "C-c C-x M-;") (global) "Comment/uncomment the rest of the buffer") (agda2-display-program-version nil (global) "Version") (agda2-set-program-version "\C-c\C-x\C-s" (global) "Switch to another version of Agda") (eri-indent ,(kbd "TAB")) (eri-indent-reverse [S-iso-lefttab]) (eri-indent-reverse [S-lefttab]) (eri-indent-reverse [S-tab]) (agda2-goto-definition-mouse [mouse-2]) (agda2-goto-definition-keyboard "\M-.") (agda2-go-back ,(if (version< emacs-version "25.1") "\M-*" "\M-,")) ) "Table of commands, used to build keymaps and menus. Each element has the form (CMD &optional KEYS WHERE DESC) where CMD is a command; KEYS is its key binding (if any); WHERE is a list which should contain 'local if the command should exist in the goal menu and 'global if the command should exist in the main menu; and DESC is the description of the command used in the menus.") (defvar agda2-mode-map (let ((map (make-sparse-keymap "Agda mode"))) (define-key map [menu-bar Agda] (cons "Agda" (make-sparse-keymap "Agda"))) (define-key map [down-mouse-3] 'agda2-popup-menu-3) (dolist (d (reverse agda2-command-table)) (destructuring-bind (f &optional keys kinds desc) d (if keys (define-key map keys f)) (if (member 'global kinds) (define-key map (vector 'menu-bar 'Agda (intern desc)) (cons desc f))))) map) "Keymap for `agda2-mode'.") (defvar agda2-goal-map (let ((map (make-sparse-keymap "Agda goal"))) (dolist (d (reverse agda2-command-table)) (destructuring-bind (f &optional keys kinds desc) d (if (member 'local kinds) (define-key map (vector (intern desc)) (cons desc f))))) map) "Keymap for agda2 goal menu.") (defvar agda2-info-buffer nil "Agda information buffer.") (defvar agda2-warning-buffer nil "Agda warnings buffer.") (defvar agda2-process-buffer nil "Agda subprocess buffer. Set in `agda2-restart'.") (defvar agda2-process nil "Agda subprocess. Set in `agda2-restart'.") (defvar agda2-in-progress nil "Is the Agda process currently busy?") ;; Some buffer locals (defvar agda2-buffer-external-status "" "External status of an `agda2-mode' buffer (dictated by the Haskell side).") (make-variable-buffer-local 'agda2-buffer-external-status) (defvar agda2-output-prompt "Agda2> " "The Agda2 buffer's prompt.") (defconst agda2-help-address "" "Address accepting submissions of bug reports and questions.") ;; Annotation for a goal ;; {! .... !} ;; ---------- overlay: agda2-gn num, face highlight, after-string num, ;; modification-hooks (agda2-protect-goal-markers) ;; - text-props: category agda2-delim1 ;; - text-props: category agda2-delim2 ;; - text-props: category agda2-delim3 ;; - text-props: category agda2-delim4 ;; ;; Char categories for {! ... !} (defvar agda2-open-brace "{") (defvar agda2-close-brace " }") (setplist 'agda2-delim1 `(display ,agda2-open-brace)) (setplist 'agda2-delim2 `(display ,agda2-open-brace rear-nonsticky t agda2-delim2 t)) (setplist 'agda2-delim3 `(display ,agda2-close-brace agda2-delim3 t)) (setplist 'agda2-delim4 `(display ,agda2-close-brace rear-nonsticky t)) ;; Note that strings used with the display property are compared by ;; reference. If the agda2-*-brace definitions were inlined, then ;; goals would be displayed as "{{ }}n" instead of "{ }n". (defvar agda2-measure-data nil "Used by `agda2-measure-load-time'. This value is either nil or a pair containing a continuation (or nil) and the time at which the measurement was started.") (make-variable-buffer-local 'agda2-measure-data) ;; The following variables are used by the filter process, ;; `agda2-output-filter'. Their values are only modified by the filter ;; process, `agda2-go', `agda2-restart', `agda2-abort-highlighting', ;; and `agda2-abort-done'. (defvar agda2-responses-expected nil "Is the Agda process expected to produce at least one response?") (make-variable-buffer-local 'agda2-responses-expected) (defvar agda2-responses 0 "The number of encountered response commands.") (make-variable-buffer-local 'agda2-responses) (defvar agda2-output-chunk-incomplete (agda2-queue-empty) "Buffer for incomplete lines. \(See `agda2-output-filter'.)") (make-variable-buffer-local 'agda2-output-chunk-incomplete) (defvar agda2-last-responses nil "Response commands which should be run after other commands. The command which arrived last is stored first in the list.") (make-variable-buffer-local 'agda2-last-responses) (defvar agda2-file-buffer nil "The Agda buffer. Note that this variable is not buffer-local.") (defvar agda2-in-agda2-file-buffer nil "Was `agda2-file-buffer' active when `agda2-output-filter' started? Note that this variable is not buffer-local.") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; agda2-mode ;;;###autoload (add-to-list 'auto-mode-alist '("\\.l?agda\\'" . agda2-mode)) ;;;###autoload (modify-coding-system-alist 'file "\\.l?agda\\'" 'utf-8) ;;;###autoload (define-derived-mode agda2-mode prog-mode "Agda" "Major mode for Agda files. The following paragraph does not apply to Emacs 23 or newer. Note that when this mode is activated the default font of the current frame is changed to the fontset `agda2-fontset-name'. The reason is that Agda programs often use mathematical symbols and other Unicode characters, so we try to provide a suitable default font setting, which can display many of the characters encountered. If you prefer to use your own settings, set `agda2-fontset-name' to nil. Special commands: \\{agda2-mode-map}" (if (boundp 'agda2-include-dirs) (display-warning 'agda2 "Note that the variable agda2-include-dirs is no longer used. You may want to update your configuration. You have at least two choices: * Use the library management system. * Set the include path using agda2-program-args. One way to avoid seeing this warning is to make sure that agda2-include-dirs is not bound." :warning)) (setq local-abbrev-table agda2-mode-abbrev-table indent-tabs-mode nil mode-line-process '((:eval (unless (eq 0 (length agda2-buffer-external-status)) (concat ":" agda2-buffer-external-status))))) (let ((l '(max-specpdl-size 2600 max-lisp-eval-depth 2800))) (while l (set (make-local-variable (pop l)) (pop l)))) (if (and window-system agda2-fontset-name) (condition-case nil (set-frame-font agda2-fontset-name) (error (error "Unable to change the font; change agda2-fontset-name or tweak agda2-fontset-spec-of-fontset-agda2")))) ;; Deactivate highlighting if the buffer is edited before ;; typechecking is complete. (add-hook 'first-change-hook 'agda2-abort-highlighting nil 'local) ;; If Agda is not running syntax highlighting does not work properly. (unless (eq 'run (agda2-process-status)) (agda2-restart)) (agda2-highlight-setup) (condition-case err (agda2-highlight-reload) (error (message "Highlighting not loaded: %s" (error-message-string err)))) (agda2-comments-and-paragraphs-setup) (force-mode-line-update) ;; Protect global value of default-input-method from set-input-method. (make-local-variable 'default-input-method) ;; Don't take script into account when determining word boundaries (set (make-local-variable 'word-combining-categories) (cons '(nil . nil) word-combining-categories)) (set-input-method "Agda") ;; Highlighting etc. is removed when we switch from the Agda mode. ;; Use case: When a file M.lagda with a local variables list ;; including "mode: latex" is loaded chances are that the Agda mode ;; is activated before the LaTeX mode, and the LaTeX mode does not ;; seem to remove the text properties set by the Agda mode. (add-hook 'change-major-mode-hook 'agda2-quit nil 'local)) (defun agda2-restart () "Tries to start or restart the Agda process." (interactive) ;; Kill any running instance of the Agda process. (condition-case nil (agda2-term) (error nil)) ;; Check that the right version of Agda is used. (let* ((coding-system-for-read 'utf-8) (output (with-output-to-string (call-process agda2-program-name nil standard-output nil "--version"))) (version (and (string-match "^Agda version \\([0-9.]+\\)" output) (match-string 1 output)))) (unless (equal version agda2-version) (error "The Agda mode's version (%s) does not match that of %s (%s)." agda2-version agda2-program-name (or version "unknown")))) (let ((all-program-args (cons "--interaction" agda2-program-args))) ;; Check that the arguments are not malformed. (let* ((coding-system-for-read 'utf-8) (status) (output (with-output-to-string (setq status (apply 'call-process agda2-program-name nil standard-output nil all-program-args))))) (unless (equal status 0) (error "Failed to start the Agda process:\n%s" output))) ;; Start the Agda process. (let ((agda2-bufname "*agda2*")) (let ((process-connection-type nil)) ; Pipes are faster than PTYs. (setq agda2-process (apply 'start-process "Agda2" agda2-bufname agda2-program-name all-program-args))) (set-process-coding-system agda2-process 'utf-8 'utf-8) (set-process-query-on-exit-flag agda2-process nil) (set-process-filter agda2-process 'agda2-output-filter) (setq agda2-in-progress nil agda2-file-buffer (current-buffer)) (with-current-buffer agda2-bufname (setq agda2-process-buffer (current-buffer) mode-name "Agda executable" agda2-last-responses nil) (set-buffer-file-coding-system 'utf-8)) (agda2-remove-annotations)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; Communicating with Agda (defun agda2-raise-error () "Raises an error. The error message directs the user to the *agda2* buffer." (error "Problem encountered. The *agda2* buffer can perhaps explain why.")) (defun agda2-running-p nil "Does the *agda2* buffer exist, and is the Agda2 process running?" (and (buffer-live-p agda2-process-buffer) (eq (agda2-process-status) 'run))) (defun agda2-send-command (restart &rest args) "Send a command to the Agda process. Sends the list of strings ARGS to the process. If RESTART is non-nil and the process is not running, or the *agda2* buffer does not exist, then an attempt is made to restart the process." (when (and restart (not (agda2-running-p))) ;; Try restarting automatically, but only once, in case there is ;; some major problem. (agda2-restart) (unless (agda2-running-p) (agda2-raise-error))) (let ((command (apply 'concat (agda2-intersperse " " args)))) (with-current-buffer agda2-process-buffer (goto-char (point-max)) (insert command) (insert "\n") (process-send-string agda2-process (concat command "\n"))))) (defun agda2-abort-if-in-progress () "Raise an error if the Agda process is (thought to be) busy." (when agda2-in-progress (error "Another command is currently in progress \(if a command has been aborted you may want to restart Agda)"))) (defun agda2-go (save responses-expected highlight do-abort &rest args) "Executes commands in the Agda2 interpreter. Sends the list of strings ARGS to the Agda2 interpreter, waits for output and executes the responses, if any. If SAVE is 'save, then the buffer is saved first. If no responses are received, and RESPONSES-EXPECTED is non-nil, then an error is raised. If HIGHLIGHT is non-nil, then the buffer's syntax highlighting may be updated." (if do-abort (agda2-abort-if-in-progress)) (setq agda2-in-progress t agda2-highlight-in-progress highlight agda2-responses-expected responses-expected agda2-responses 0 agda2-output-chunk-incomplete (agda2-queue-empty) agda2-file-buffer (current-buffer)) (when (equal save 'save) (save-buffer)) (apply 'agda2-send-command 'restart "IOTCM" (agda2-string-quote (buffer-file-name)) (if highlight (agda2-highlight-level) "None") "Indirect" "(" (append args '(")")))) (defun agda2-abort () "Tries to abort the current computation, if any. May be more efficient than restarting Agda." (interactive) (agda2-send-command nil "IOTCM" (agda2-string-quote (buffer-file-name)) "None" "Indirect" "Cmd_abort")) (defun agda2-abort-done () "Removes annotations, resets certain variables. Intended to be used by the backend if an abort command was successful." (agda2-remove-annotations) (setq agda2-highlight-in-progress nil agda2-responses-expected nil agda2-last-responses nil)) (defun agda2-output-filter (proc chunk) "Evaluate the Agda process's commands. This filter function assumes that every line contains either some kind of error message (which cannot be parsed as a list), or exactly one command. Incomplete lines are stored in a buffer (`agda2-output-chunk-incomplete'). Every command is run by this function, unless it has the form \"(('last . priority) . cmd)\", in which case it is run by `agda2-run-last-commands' at the end, after the Agda2 prompt has reappeared, after all non-last commands, and after all interactive highlighting is complete. The last commands can have different integer priorities; those with the lowest priority are executed first. Non-last commands should not call the Agda process. All commands are echoed to the *agda2* buffer, with the exception of commands of the form \"(agda2-highlight-... ...)\". The non-last commands are run in the order in which they appear. When the prompt has been reached an error is raised if `agda2-responses-expected' is non-nil and no commands have arrived. Otherwise highlighting annotations are reloaded from `agda2-highlighting-file', unless `agda2-highlighting-in-progress' is nil." ;; Beware: the buffer may have been killed in the mean time. E.g. when ;; viewing an attachment containing Agda code in Gnus, Gnus will ;; create a temp buffer, set it in agda2-mode, call font-lock-ensure on it ;; (which won't know that it needs to wait for some process to reply), then ;; extract the fontified text and kill the temp buffer; so when Agda ;; finally answers, the temp buffer is long gone. (when (buffer-live-p agda2-file-buffer) (setq agda2-in-agda2-file-buffer (and agda2-file-buffer (equal (current-buffer) agda2-file-buffer))) (let (;; The input lines in the current chunk. (lines (split-string chunk "\n")) ;; Non-last commands found in the current chunk (reversed). (non-last-commands ()) ;; Last incomplete line, if any. (output-chunk-incomplete "")) (with-current-buffer agda2-file-buffer (when (consp lines) (agda2-queue-enqueue agda2-output-chunk-incomplete (pop lines)) (when (consp lines) ;; The previous uncomplete chunk is now complete. (push (agda2-queue-to-string agda2-output-chunk-incomplete) lines) ;; Stash away the last incomplete line, if any. (Note that ;; (split-string "...\n" "\n") evaluates to (... "").) (setq output-chunk-incomplete (car (last lines)) agda2-output-chunk-incomplete (agda2-queue-from-string output-chunk-incomplete)) ;; Handle every complete line. (dolist (line (butlast lines)) (let* (;; The command. Lines which cannot be parsed as a single ;; list, without any junk, are ignored. (cmd (condition-case nil (let ((result (read-from-string line))) (if (and (listp (car result)) (= (cdr result) (length line))) (car result))) (error nil))) (is-highlighting-command (and cmd (symbolp (car cmd)) (let ((case-fold-search nil)) (string-match "^agda2-highlight-" (symbol-name (car cmd))))))) ;; Do not echo highlighting commands. (unless is-highlighting-command (with-current-buffer agda2-process-buffer (save-excursion (goto-char (point-max)) (insert line) (insert "\n")))) (when cmd (unless is-highlighting-command (incf agda2-responses)) (if (equal 'last (car-safe (car cmd))) (push (cons (cdr (car cmd)) (cdr cmd)) agda2-last-responses) (push cmd non-last-commands))))) ;; Run non-last commands. (mapc 'agda2-exec-response (nreverse non-last-commands))) ;; Check if the prompt has been reached. This function assumes ;; that the prompt does not include any newline characters. (when (agda2-queue-is-prefix-of agda2-output-prompt agda2-output-chunk-incomplete) (with-current-buffer agda2-process-buffer (insert output-chunk-incomplete)) (setq agda2-output-chunk-incomplete (agda2-queue-empty) agda2-in-progress nil agda2-last-responses (nreverse agda2-last-responses)) (when (and agda2-responses-expected (equal agda2-responses 0)) (agda2-raise-error)) (agda2-run-last-commands) (when agda2-measure-data (let ((elapsed (format "%.2fs" (float-time (time-since (cdr agda2-measure-data))))) (continuation (car agda2-measure-data))) (setq agda2-measure-data nil) (message "Load time: %s." elapsed) (when continuation (funcall continuation elapsed)))))))))) (defun agda2-run-last-commands nil "Execute the last commands in the right order. \(After the prompt has reappeared.) See `agda2-output-filter'." ;; with-current-buffer is used repeatedly below, because some last ;; commands may switch the focus to another buffer. (while (with-current-buffer agda2-file-buffer (and (not agda2-in-progress) (consp agda2-last-responses))) (with-current-buffer agda2-file-buffer ;; The list is sorted repeatedly because this function may be ;; called recursively (via `agda2-exec-response'). (setq agda2-last-responses (sort agda2-last-responses (lambda (x y) (<= (car x) (car y))))) (let ((r (pop agda2-last-responses))) (agda2-exec-response (cdr r))))) ;; Unset agda2-highlight-in-progress when all the asynchronous ;; commands have terminated. (unless agda2-in-progress (setq agda2-highlight-in-progress nil))) (defun agda2-abort-highlighting nil "Abort any interactive highlighting. This function should be used in `first-change-hook'." (when agda2-highlight-in-progress (setq agda2-highlight-in-progress nil) (message "\"%s\" has been modified. Interrupting highlighting." (buffer-name (current-buffer))))) (defun agda2-goal-cmd (cmd save &optional want ask &rest args) "Reads input from goal or minibuffer and sends command to Agda. An error is raised if point is not in a goal. The command sent to Agda is CMD ARGS. The user input is computed as follows: * If WANT is nil, then the user input is the empty string. * If WANT is a string, and either ASK is non-nil or the goal only contains whitespace, then the input is taken from the minibuffer. In this case WANT is used as the prompt string. * Otherwise (including if WANT is 'goal) the goal contents are used. If the user input is not taken from the goal, then an empty goal range is given. If SAVE is 'save, then the buffer is saved just before the command is sent to Agda (if it is sent). An error is raised if no responses are received." (multiple-value-bind (o g) (agda2-goal-at (point)) (unless g (error "For this command, please place the cursor in a goal")) (let ((txt (buffer-substring-no-properties (+ (overlay-start o) 2) (- (overlay-end o) 2))) (input-from-goal nil)) (cond ((null want) (setq txt "")) ((and (stringp want) (or ask (string-match "\\`\\s *\\'" txt))) (setq txt (read-string (concat want ": ") nil nil txt t))) (t (setq input-from-goal t))) (apply 'agda2-go save t input-from-goal t cmd (format "%d" g) (if input-from-goal (agda2-goal-Range o) (agda2-mkRange nil)) (agda2-string-quote txt) args)))) ;; Note that the following function is a security risk, since it ;; evaluates code without first inspecting it. The code (supposedly) ;; comes from the Agda backend, but there could be bugs in the backend ;; which can be exploited by an attacker which manages to trick ;; someone into type-checking compromised Agda code. (defun agda2-exec-response (response) "Interprets response." (let ((inhibit-read-only t)) (eval response))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; User commands and response processing (defun agda2-load () "Load current buffer." (interactive) (agda2-go 'save t t t "Cmd_load" (agda2-string-quote (buffer-file-name)) (agda2-list-quote agda2-program-args) )) (defun agda2-measure-load-time (&optional highlighting-level continuation) "Load the current buffer and print how much time it takes. \(Wall-clock time.) The given HIGHLIGHTING-LEVEL is used (if non-nil). If CONTINUATION is non-nil, then CONTINUATION is applied to the resulting time (represented as a string)." (interactive) (agda2-abort-if-in-progress) (let* ((agda2-highlight-level (or highlighting-level agda2-highlight-level))) (setq agda2-measure-data (cons continuation (current-time))) (agda2-load))) (defun agda2-compile () "Compile the current module. The variable `agda2-backend' determines which backend is used." (interactive) (let ((backend (cond ((equal agda2-backend "MAlonzo") "GHC") ((equal agda2-backend "MAlonzoNoMain") "GHCNoMain") ((equal agda2-backend "") (completing-read "Backend: " agda2-backends nil nil nil nil nil 'inherit-input-method)) (t agda2-backend)))) (when (equal backend "") (error "No backend chosen")) (agda2-go 'save t t t "Cmd_compile" backend (agda2-string-quote (buffer-file-name)) (agda2-list-quote agda2-program-args) ))) (defmacro agda2-maybe-forced (name comment cmd save want) "This macro constructs a function NAME which runs CMD. COMMENT is used to build the function's comment. The function NAME takes a prefix argument which tells whether it should apply force or not when running CMD (through `agda2-goal-cmd'; SAVE is used as `agda2-goal-cmd's SAVE argument and WANT is used as `agda2-goal-cmd's WANT argument)." (let ((eval (make-symbol "eval"))) `(defun ,name (&optional prefix) ,(concat comment ". The action depends on the prefix argument: * If the prefix argument is `nil' (i.e., if no prefix argument is given), then no force is applied. * If any other prefix argument is used (for instance, if C-u is typed once or twice right before the command is invoked), then force is applied.") (interactive "P") (let ((,eval (cond ((equal prefix nil) "WithoutForce") ("WithForce")))) (agda2-goal-cmd (concat ,cmd " " ,eval) ,save ,want))))) (agda2-maybe-forced agda2-give "Give to the goal at point the expression in it" "Cmd_give" 'save "expression to give") ;; (defun agda2-give() ;; "Give to the goal at point the expression in it" (interactive) ;; (agda2-goal-cmd "Cmd_give" 'save "expression to give")) (defun agda2-give-action (old-g paren) "Update the goal OLD-G with the expression in it." (let ;; Don't run modification hooks: we don't want this to ;; trigger agda2-abort-highlighting. ((inhibit-modification-hooks t)) (agda2-update old-g paren))) (defun agda2-refine (pmlambda) "Refine the goal at point. If the goal contains an expression e, and some \"suffix\" of the type of e unifies with the goal type, then the goal is replaced by e applied to a suitable number of new goals. PMLAMBDA is only used if the goal has a functional type. When the prefix argument is given a pattern maching lambda will be inserted, otherwise a standard lambda will be used. If the goal is empty, the goal type is a data type, and there is exactly one constructor which unifies with this type, then the goal is replaced by the constructor applied to a suitable number of new goals." (interactive "P") (if pmlambda (agda2-goal-cmd "Cmd_refine_or_intro True" 'save 'goal) (agda2-goal-cmd "Cmd_refine_or_intro False" 'save 'goal))) (defun agda2-auto () "Simple proof search" (interactive) (agda2-goal-cmd "Cmd_auto" 'save 'goal)) (defun agda2-make-case () "Refine the pattern variables given in the goal. Assumes that = {!!} is on one line." (interactive) (agda2-goal-cmd "Cmd_make_case" 'save "pattern variables to case (empty for split on result)")) (defun agda2-make-case-action (newcls) "Replace the line at point with new clauses NEWCLS and reload." (agda2-forget-all-goals);; we reload later anyway. (let* ((p0 (point)) ;; (p1 (goto-char (agda2-decl-beginning))) (p1 (goto-char (+ (current-indentation) (line-beginning-position)))) (indent (current-column)) cl) (goto-char p0) (re-search-forward "!}" (line-end-position) 'noerr) (delete-region p1 (point)) (while (setq cl (pop newcls)) (insert cl) (if newcls (insert "\n" (make-string indent ? )))) (goto-char p1)) (agda2-load)) (defun agda2-make-case-action-extendlam (newcls) "Replace definition of extended lambda with new clauses NEWCLS and reload." (agda2-forget-all-goals);; we reload later anyway. (let* ((p0 (point)) (pmax (re-search-forward "!}")) (bracketCount 0) (p1 (goto-char (+ (current-indentation) (line-beginning-position)))) (indent (current-column)) cl) (goto-char p0) (re-search-backward "{!") (while (and (not (equal (preceding-char) ?\;)) (>= bracketCount 0) (> (point) p1)) (backward-char) (if (equal (preceding-char) ?}) (incf bracketCount)) (if (equal (preceding-char) ?{) (decf bracketCount))) (let* ((is-lambda-where (= (point) p1)) (p (point))) (delete-region (point) pmax) (if (not is-lambda-where) (insert " ")) (while (setq cl (pop newcls)) (insert cl) (if newcls (if is-lambda-where (insert "\n" (make-string indent ? )) (insert " ; ")))) (goto-char p))) (agda2-load)) (defun agda2-status-action (status) "Display the string STATUS in the current buffer's mode line. \(precondition: the current buffer has to use the Agda mode as the major mode)." (setq agda2-buffer-external-status status)) (defmacro agda2-warning-or-info-buffer (buffer kind title) "Used to define the functions agda2-warning-buffer and agda2-info-buffer." `(defun ,buffer nil ,(concat "Creates the Agda " kind " buffer, if it does not already exist. The buffer is returned.") (unless (buffer-live-p ,buffer) (setq ,buffer (generate-new-buffer ,title)) (with-current-buffer ,buffer (compilation-mode "AgdaInfo") ;; Support for jumping to positions mentioned in the text. (set (make-local-variable 'compilation-error-regexp-alist) '(("\\([\\\\/][^[:space:]]*\\):\\([0-9]+\\),\\([0-9]+\\)-\\(\\([0-9]+\\),\\)?\\([0-9]+\\)" 1 (2 . 5) (3 . 6)))) ;; Do not skip errors that start in the same position as the ;; current one. (set (make-local-variable 'compilation-skip-to-next-location) nil) ;; No support for recompilation. The key binding is removed, and ;; attempts to run `recompile' will (hopefully) result in an ;; error. (let ((map (copy-keymap (current-local-map)))) (define-key map (kbd "g") 'undefined) (use-local-map map)) (set (make-local-variable 'compile-command) 'agda2-does-not-support-compilation-via-the-compilation-mode) (set-syntax-table agda2-mode-syntax-table) (set (make-local-variable 'word-combining-categories) (cons '(nil . nil) word-combining-categories)) (set-input-method "Agda"))) ,buffer)) (agda2-warning-or-info-buffer agda2-warning-buffer "warning" "*Agda warnings*") (agda2-warning-or-info-buffer agda2-info-buffer "info" "*Agda information*") (defun agda2-font-syntactic-face (state) (cond ((nth 4 state) ( save-excursion (goto-char (nth 8 state)) (cond ((looking-at "--[[:space:]\n]") 'font-lock-comment-face) ((looking-at "{-[^#]") 'font-lock-comment-face) ))))) (defun agda2-info-action (name text &optional append) "Insert TEXT into the Agda info buffer and display it. NAME is displayed in the buffer's mode line. If APPEND is non-nil, then TEXT is appended at the end of the buffer, and point placed after this text. If APPEND is nil, then any previous text is removed before TEXT is inserted, and point is placed before this text." (interactive) (let ((buf (agda2-info-buffer))) (with-current-buffer buf ;; In some cases the jump-to-position-mentioned-in-text ;; functionality (see compilation-error-regexp-alist above) ;; didn't work: Emacs jumped to the wrong position. However, it ;; seems to work if compilation-forget-errors is used. This ;; problem may be related to Emacs bug #9679 ;; (http://debbugs.gnu.org/cgi/bugreport.cgi?bug=9679). The idea ;; to use compilation-forget-errors comes from a comment due to ;; Oleksandr Manzyuk ;; (https://github.com/haskell/haskell-mode/issues/67). (compilation-forget-errors) (unless append (erase-buffer)) (save-excursion (goto-char (point-max)) (insert text)) (put-text-property 0 (length name) 'face '(:weight bold) name) (setq mode-line-buffer-identification name) (force-mode-line-update)) ;; If the current window displays the information buffer, then the ;; window configuration is left untouched. (unless (equal (window-buffer) buf) (let ((agda-window (and agda2-file-buffer (car-safe ;; All windows, including minibuffers, on any ;; frame on the current terminal, displaying the ;; present Agda file buffer. (get-buffer-window-list agda2-file-buffer t 0))))) (save-selected-window ;; Select a window displaying the Agda file buffer (if such ;; a window exists). With certain configurations of ;; display-buffer this should increase the likelihood that ;; the info buffer will be displayed on the same frame. (when agda-window (select-window agda-window 'no-record)) (let* (;; If there is only one window, then the info window ;; should be created above or below the code window, ;; not to the left or right. (split-width-threshold nil) (window (display-buffer buf ;; Under Emacs 23 the effect of the following ;; argument is only that the current window ;; should not be used. '(nil . (;; Do not use the same window. (inhibit-same-window . t) ;; Do not raise or select another frame. (inhibit-switch-frame . t)))))) (if window (fit-window-to-buffer window (truncate (* (frame-height) agda2-information-window-max-height)))))))) ;; Move point in every window displaying the information buffer. ;; Exception: If we are appending, don't move point in selected ;; windows. (dolist (window (get-buffer-window-list buf 'no-minibuffer t)) (unless (and append (equal window (selected-window))) (with-selected-window window (if append (goto-char (point-max)) (goto-char (point-min)))))))) (defun agda2-close-warning nil (interactive) (when (buffer-live-p agda2-warning-buffer) (delete-windows-on agda2-warning-buffer)) ) (defun agda2-warning-action (name text &optional append) "Insert TEXT into the Agda warning buffer and display it. NAME is displayed in the buffer's mode line. If APPEND is non-nil, then TEXT is appended at the end of the buffer, and point placed after this text. If APPEND is nil, then any previous text is removed before TEXT is inserted, and point is placed before this text." (interactive) (let ((buf (agda2-warning-buffer))) (with-current-buffer buf ;; In some cases the jump-to-position-mentioned-in-text ;; functionality (see compilation-error-regexp-alist above) ;; didn't work: Emacs jumped to the wrong position. However, it ;; seems to work if compilation-forget-errors is used. This ;; problem may be related to Emacs bug #9679 ;; (http://debbugs.gnu.org/cgi/bugreport.cgi?bug=9679). The idea ;; to use compilation-forget-errors comes from a comment due to ;; Oleksandr Manzyuk ;; (https://github.com/haskell/haskell-mode/issues/67). (compilation-forget-errors) (unless append (erase-buffer)) (save-excursion (goto-char (point-max)) (insert text)) (put-text-property 0 (length name) 'face '(:weight bold) name) (setq mode-line-buffer-identification name) (force-mode-line-update)) ;; If the current window displays the information buffer, then the ;; window configuration is left untouched. (unless (equal (window-buffer) buf) (let ((agda-info (and agda2-info-buffer (car-safe ;; All windows, including minibuffers, on any ;; frame on the current terminal, displaying the ;; present Agda file buffer. (get-buffer-window-list agda2-info-buffer t 0))))) (save-selected-window ;; Select a window displaying the Agda file buffer (if such ;; a window exists). With certain configurations of ;; display-buffer this should increase the likelihood that ;; the info buffer will be displayed on the same frame. (when agda-info (select-window agda-info 'no-record)) (let* (;; The warnings window should be displayed below the ;; Agda info one (split-width-threshold nil) (split-height-threshold 1) (window (display-buffer buf ;; Under Emacs 23 the effect of the following ;; argument is only that the current window ;; should not be used. '((display-buffer-below-selected) . (;; Do not use the same window (inhibit-same-window . t) ;; Do not raise or select another frame. (inhibit-switch-frame . t)))))) (if window (fit-window-to-buffer window (truncate (* (frame-height) agda2-warning-window-max-height)))))))) ;; Move point in every window displaying the information buffer. ;; Exception: If we are appending, don't move point in selected ;; windows. (dolist (window (get-buffer-window-list buf 'no-minibuffer t)) (unless (and append (equal window (selected-window))) (with-selected-window window (if append (goto-char (point-max)) (goto-char (point-min)))))))) (defun agda2-info-action-and-copy (name text &optional append) "Same as agda2-info-action but also puts TEXT in the kill ring." (kill-new text) (agda2-info-action name text append)) (defun agda2-show-goals() "Show all goals." (interactive) (agda2-go nil t t t "Cmd_metas")) (defun agda2-show-constraints() "Show constraints." (interactive) (agda2-go nil t t t "Cmd_constraints")) (defun agda2-remove-annotations () "Removes buffer annotations (overlays and text properties)." (interactive) (dolist (o (overlays-in (point-min) (point-max))) (delete-overlay o)) (let ((inhibit-read-only t)) (annotation-preserve-mod-p-and-undo (set-text-properties (point-min) (point-max) '())) (force-mode-line-update))) (defun agda2-next-goal () "Go to the next goal, if any." (interactive) (agda2-mv-goal 'next-single-property-change 'agda2-delim2 1 (point-min))) (defun agda2-previous-goal () "Go to the previous goal, if any." (interactive) (agda2-mv-goal 'previous-single-property-change 'agda2-delim3 0 (point-max))) (defun agda2-mv-goal (change delim adjust wrapped) (agda2-let () ((go (p) (while (and (setq p (funcall change p 'category)) (not (eq (get-text-property p 'category) delim)))) (if p (goto-char (+ adjust p))))) (or (go (point)) (go wrapped) (message "No goals in the buffer")))) (defun agda2-quit () "Quit and clean up after agda2." (interactive) (remove-hook 'first-change-hook 'agda2-abort-highlighting 'local) (agda2-remove-annotations) (agda2-term)) (defun agda2-term () "Send a SIGTERM signal to the Agda2 process, then kill its buffer." (interactive) (when (buffer-live-p agda2-process-buffer) (with-current-buffer agda2-process-buffer (condition-case nil (signal-process agda2-process 'SIGTERM) (error nil)) (kill-buffer)))) (defmacro agda2-maybe-normalised (name comment cmd want) "This macro constructs a function NAME which runs CMD. COMMENT is used to build the function's comment. The function NAME takes a prefix argument which tells whether it should normalise types or not when running CMD (through `agda2-goal-cmd'; WANT is used as `agda2-goal-cmd's WANT argument, and nil as its SAVE argument)." (let ((eval (make-symbol "eval"))) `(defun ,name (&optional prefix) ,(concat comment ". The form of the result depends on the prefix argument: * If the prefix argument is `nil' (i.e., if no prefix argument is given), then the result is simplified. * If the prefix argument is `(4)' (for instance if C-u is typed exactly once right before the command is invoked), then the result is neither explicitly normalised nor simplified. * If any other prefix argument is used (for instance if C-u is typed twice right before the command is invoked), then the result is normalised.") (interactive "P") (let ((,eval (cond ((equal prefix nil) "Simplified") ((equal prefix '(4)) "Instantiated") ("Normalised")))) (agda2-goal-cmd (concat ,cmd " " ,eval) nil ,want))))) (defmacro agda2-maybe-normalised-toplevel (name comment cmd prompt) "This macro constructs a function NAME which runs CMD. COMMENT is used to build the function's comments. The function NAME takes a prefix argument which tells whether it should normalise types or not when running CMD (through `agda2-go' nil t nil t; the string PROMPT is used as the goal command prompt)." (let ((eval (make-symbol "eval"))) `(defun ,name (prefix expr) ,(concat comment ". The form of the result depends on the prefix argument: * If the prefix argument is `nil' (i.e., if no prefix argument is given), then the result is simplified. * If the prefix argument is `(4)' (for instance if C-u is typed exactly once right before the command is invoked), then the result is neither explicitly normalised nor simplified. * If any other prefix argument is used (for instance if C-u is typed twice right before the command is invoked), then the result is normalised.") (interactive ,(concat "P\nM" prompt ": ")) (let ((,eval (cond ((equal prefix nil) "Simplified") ((equal prefix '(4)) "Instantiated") ("Normalised")))) (agda2-go nil t nil t (concat ,cmd " " ,eval " " (agda2-string-quote expr))))))) (defmacro agda2-maybe-normalised-global (name comment cmd) "This macro constructs a function NAME which runs CMD. COMMENT is used to build the function's comments. The function NAME takes a prefix argument which tells whether it should normalise types or not when running CMD (through `agda2-go' nil t nil t;)." (let ((eval (make-symbol "eval"))) `(defun ,name (prefix) ,(concat comment ". The form of the result depends on the prefix argument: * If the prefix argument is `nil' (i.e., if no prefix argument is given), then the result is simplified. * If the prefix argument is `(4)' (for instance if C-u is typed exactly once right before the command is invoked), then the result is neither explicitly normalised nor simplified. * If any other prefix argument is used (for instance if C-u is typed twice right before the command is invoked), then the result is normalised.") (interactive "P") (let ((,eval (cond ((equal prefix nil) "AsIs") ((equal prefix '(4)) "Simplified") ("Normalised")))) (agda2-go nil t nil t (concat ,cmd " " ,eval " " )))))) (agda2-maybe-normalised agda2-goal-type "Show the type of the goal at point" "Cmd_goal_type" nil) (agda2-maybe-normalised agda2-infer-type "Infer the type of the goal at point" "Cmd_infer" "expression to type") (agda2-maybe-normalised-toplevel agda2-infer-type-toplevel "Infers the type of the given expression. The scope used for the expression is that of the last point inside the current top-level module" "Cmd_infer_toplevel" "Expression") (defun agda2-infer-type-maybe-toplevel () "Infers the type of the given expression. Either uses the scope of the current goal or, if point is not in a goal, the top-level scope." (interactive) (call-interactively (if (agda2-goal-at (point)) 'agda2-infer-type 'agda2-infer-type-toplevel))) (defun agda2-why-in-scope () "Explain why something is in scope in a goal." (interactive) (agda2-goal-cmd "Cmd_why_in_scope" nil "Name")) (defun agda2-why-in-scope-toplevel (name) "Explain why something is in scope at the top level." (interactive "MName: ") (agda2-go nil t nil t "Cmd_why_in_scope_toplevel" (agda2-string-quote name))) (defun agda2-why-in-scope-maybe-toplevel () "Explains why a given name is in scope." (interactive) (call-interactively (if (agda2-goal-at (point)) 'agda2-why-in-scope 'agda2-why-in-scope-toplevel))) (agda2-maybe-normalised agda2-goal-and-context "Shows the type of the goal at point and the currect context" "Cmd_goal_type_context" nil) (agda2-maybe-normalised agda2-goal-and-context-and-inferred "Shows the context, the goal and the given expression's inferred type" "Cmd_goal_type_context_infer" "expression to type") (agda2-maybe-normalised agda2-goal-and-context-and-checked "Shows the context, the goal and check the given expression's against the hole's type" "Cmd_goal_type_context_check" "expression to type") (agda2-maybe-normalised agda2-show-context "Show the context of the goal at point" "Cmd_context" nil) (agda2-maybe-normalised agda2-helper-function-type "Compute the type of a hypothetical helper function." "Cmd_helper_function" "Expression") (agda2-maybe-normalised agda2-module-contents "Shows all the top-level names in the given module. Along with their types." "Cmd_show_module_contents" "Module name") (agda2-maybe-normalised-toplevel agda2-module-contents-toplevel "Shows all the top-level names in the given module. Along with their types." "Cmd_show_module_contents_toplevel" "Module name" ) (agda2-maybe-normalised-toplevel agda2-search-about-toplevel "Search About an identifier" "Cmd_search_about_toplevel" "Name" ) (defun agda2-module-contents-maybe-toplevel () "Shows all the top-level names in the given module. Along with their types. Uses either the scope of the current goal or, if point is not in a goal, the top-level scope." (interactive) (call-interactively (if (agda2-goal-at (point)) 'agda2-module-contents 'agda2-module-contents-toplevel))) (defun agda2-solve-maybe-all () "Solves goals that are already instantiated internally. Either only one if point is a goal, or all of them." (interactive) (call-interactively (if (agda2-goal-at (point)) 'agda2-solveOne 'agda2-solveAll)) ) (agda2-maybe-normalised-global agda2-solveAll "Solves all goals that are already instantiated internally." "Cmd_solveAll" ) (agda2-maybe-normalised agda2-solveOne "Solves the goal at point if it is already instantiated internally" "Cmd_solveOne" nil ) (defun agda2-solveAll-action (iss) (while iss (let* ((g (pop iss)) (txt (pop iss)) (cmd (cons 'agda2-solve-action (cons g (cons txt nil))))) (if (null agda2-last-responses) (push (cons 1 cmd) agda2-last-responses) (nconc agda2-last-responses (cons (cons 3 cmd) nil)))))) (defun agda2-solve-action (g txt) (save-excursion (agda2-replace-goal g txt) (agda2-goto-goal g) (agda2-give))) (defun agda2-compute-normalised (&optional arg) "Compute the normal form of the expression in the goal at point. With a prefix argument distinct from `(4)' the normal form of \"show \" is computed, and then the resulting string is printed. With any prefix argument \"abstract\" is ignored during the computation." (interactive "P") (let ((cmd (concat "Cmd_compute" (cond ((equal arg nil) " DefaultCompute") ((equal arg '(4)) " IgnoreAbstract") (" UseShowInstance"))))) (agda2-goal-cmd cmd nil "expression to normalise"))) (defun agda2-compute-normalised-toplevel (expr &optional arg) "Compute the normal form of the given expression. The scope used for the expression is that of the last point inside the current top-level module. With a prefix argument distinct from `(4)' the normal form of \"show \" is computed, and then the resulting string is printed. With any prefix argument \"abstract\" is ignored during the computation." (interactive "MExpression: \nP") (let ((cmd (concat "Cmd_compute_toplevel" (cond ((equal arg nil) " DefaultCompute") ((equal arg '(4)) " IgnoreAbstract") (" UseShowInstance")) " "))) (agda2-go nil t nil t (concat cmd (agda2-string-quote expr))))) (defun agda2-compute-normalised-maybe-toplevel () "Compute the normal form of the given expression. The scope used for the expression is that of the last point inside the current top-level module. With a prefix argument distinct from `(4)' the normal form of \"show \" is computed, and then the resulting string is printed. With any prefix argument \"abstract\" is ignored during the computation." (interactive) (if (agda2-goal-at (point)) (call-interactively 'agda2-compute-normalised) (call-interactively 'agda2-compute-normalised-toplevel))) (defun agda2-display-program-version () "Display version of Agda" (interactive) (agda2-go nil nil nil t "Cmd_show_version")) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; (defun agda2-highlight-reload nil "Loads precomputed syntax highlighting info for the current buffer. Only if the buffer is unmodified, and only if there is anything to load." (unless (buffer-modified-p) (agda2-go nil nil t t "Cmd_load_highlighting_info" (agda2-string-quote (buffer-file-name))))) (defun agda2-literate-p () "Is the current buffer a literate Agda buffer?" (equal (file-name-extension (buffer-name)) "lagda")) (defmacro agda2--case (exp &rest branches) ;FIXME: Use `pcase' instead! (declare (debug t) (indent 1)) (let ((s (make-symbol "v"))) `(let ((,s ,exp)) (cond ,@(mapcar (lambda (branch) `((equal ,s ,(car branch)) ,@(cdr branch))) branches))))) (defun agda2-goals-action (goals) "Annotates the goals in the current buffer with text properties. GOALS is a list of the buffer's goal numbers, in the order in which they appear in the buffer. Note that this function should be run /after/ syntax highlighting information has been loaded, because the two highlighting mechanisms interact in unfortunate ways." (agda2-forget-all-goals) (agda2-let ((literate (agda2-literate-p)) stk top ;; Don't run modification hooks: we don't want this function to ;; trigger agda2-abort-highlighting. (inhibit-modification-hooks t)) ((delims() (re-search-forward "[?]\\|[{][-!]\\|[-!][}]\\|--\\|^%.*\\\\begin{code}\\|\\\\begin{code}\\|\\\\end{code}" nil t)) ;; is-proper checks whether string s (e.g. "?" or "--") is proper ;; i.e., is not part of an identifier. ;; comment-starter is true if s starts a comment (e.g. "--") (is-proper (s comment-starter) (save-excursion (save-match-data (backward-char (length s)) (unless (bolp) (backward-char 1)) ;; bolp = pointer at beginning of line ;; Andreas, 2014-05-17 Issue 1132 ;; A questionmark can also follow immediately after a . ;; for instance to be a place holder for a dot pattern. (looking-at (concat "\\([.{}();]\\|^\\|\\s \\)" ;; \\s = whitespace (regexp-quote s) (unless comment-starter "\\([{}();]\\|$\\|\\s \\)")))))) (make(p) (agda2-make-goal p (point) (pop goals))) (inside-comment() (and stk (null (car stk)))) (inside-goal() (and stk (integerp (car stk)))) (outside-code() (and stk (eq (car stk) 'outside))) (inside-code() (not (outside-code))) ;; inside a multi-line comment ignore everything but the multi-line comment markers (safe-delims() (if (inside-comment) (re-search-forward "{-\\|-}" nil t) (delims)))) (save-excursion ;; In literate mode we should start out in the "outside of code" ;; state. (if literate (push 'outside stk)) (goto-char (point-min)) (while (and goals (safe-delims)) (agda2--case (match-string 0) ("\\begin{code}" (when (outside-code) (pop stk))) ("\\end{code}" (when (not stk) (push 'outside stk))) ("--" (when (and (not stk) (is-proper "--" t)) (end-of-line))) ("{-" (when (and (inside-code) (not (inside-goal))) (push nil stk))) ("-}" (when (inside-comment) (pop stk))) ("{!" (when (and (inside-code) (not (inside-comment))) (push (- (point) 2) stk))) ("!}" (when (inside-goal) (setq top (pop stk)) (unless stk (make top)))) ("?" (progn (when (and (not stk) (is-proper "?" nil)) (delete-char -1) (insert "{!!}") (make (- (point) 4)))))))))) (defun agda2-make-goal (p q n) "Make a goal with number N at

{!...!}. Assume the region is clean." (annotation-preserve-mod-p-and-undo (let ((atp (lambda (x ps) (add-text-properties x (1+ x) ps)))) (funcall atp p '(category agda2-delim1)) (funcall atp (1+ p) '(category agda2-delim2)) (funcall atp (- q 2) '(category agda2-delim3)) (funcall atp (1- q) '(category agda2-delim4))) (let ((o (make-overlay p q nil t nil))) (overlay-put o 'modification-hooks '(agda2-protect-goal-markers)) (overlay-put o 'agda2-gn n) (overlay-put o 'face 'highlight) (overlay-put o 'after-string (propertize (format "%s" n) 'face 'highlight))))) (defun agda2-protect-goal-markers (ol action beg end &optional length) "Ensures that the goal markers cannot be tampered with. Except if `inhibit-read-only' is non-nil or /all/ of the goal is modified." (if action ;; This is the after-change hook. nil ;; This is the before-change hook. (cond ((and (<= beg (overlay-start ol)) (>= end (overlay-end ol))) ;; The user is trying to remove the whole goal: ;; manually evaporate the overlay and add an undo-log entry so ;; it gets re-added if needed. (when (listp buffer-undo-list) (push (list 'apply 0 (overlay-start ol) (overlay-end ol) 'move-overlay ol (overlay-start ol) (overlay-end ol)) buffer-undo-list)) (delete-overlay ol)) ((or (< beg (+ (overlay-start ol) 2)) (> end (- (overlay-end ol) 2))) (unless inhibit-read-only (signal 'text-read-only nil)))))) (defun agda2-update (old-g new-txt) "Update the goal OLD-G. If NEW-TXT is a string, then the goal is replaced by the string, and otherwise the text inside the goal is retained (parenthesised if NEW-TXT is `'paren'). Removes the goal braces, but does not remove the goal overlay or text properties." (multiple-value-bind (p q) (agda2-range-of-goal old-g) (save-excursion (cond ((stringp new-txt) (agda2-replace-goal old-g new-txt)) ((equal new-txt 'paren) (goto-char (- q 2)) (insert ")") (goto-char (+ p 2)) (insert "("))) (multiple-value-bind (p q) (agda2-range-of-goal old-g) (delete-region (- q 2) q) (delete-region p (+ p 2))) ;; Update highlighting (if (and (not (equal new-txt 'paren)) (not (equal new-txt 'no-paren))) (apply 'agda2-go 'save t t nil "Cmd_highlight" (format "%d" old-g) (agda2-mkRange `(,p ,(- q 2))) (agda2-string-quote new-txt) nil)) ))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; Misc (defun agda2-process-status () "Status of `agda2-process-buffer', or \"no process\"." (condition-case nil (process-status agda2-process) (error "no process"))) (defun agda2-intersperse (sep xs) (let(ys)(while xs (push (pop xs) ys)(push sep ys))(pop ys)(nreverse ys))) (defun agda2-goal-Range (o) "The Haskell Range of goal overlay O." (agda2-mkRange `(,(+ (overlay-start o) 2) ,(- (overlay-end o) 2)))) (defun agda2-mkRange (points) "A string representing a range corresponding to POINTS. POINTS must be a list of integers, and its length must be 0 or 2." (if points (format "(intervalsToRange (Just (mkAbsolute %s)) %s)" (agda2-string-quote (file-truename (buffer-file-name))) (format "[Interval %s %s]" (agda2-mkPos (car points)) (agda2-mkPos (cadr points)))) "noRange")) (defun agda2-mkPos (&optional p) "The Haskell PositionWithoutFile corresponding to P or `point'." (save-excursion (save-restriction (widen) (if p (goto-char p)) (format "(Pn () %d %d %d)" (point) (count-lines (point-min) (point)) (1+ (current-column)))))) (defun agda2-char-quote (c) "Convert character C to the notation used in Haskell strings. The non-ASCII characters are actually rendered as \"\\xNNNN\\&\", i.e. followed by a \"null character\", to avoid problems if they are followed by digits. ASCII characters (code points < 128) are converted to singleton strings." (if (< c 128) (list c) ;; FIXME: Why return a list rather than a string? --Stef (append (format "\\x%x\\&" (encode-char c 'ucs)) nil))) (defun agda2-string-quote (s) "Format S as a Haskell string literal. Removes any text properties, escapes newlines, double quotes, etc., adds surrounding double quotes, and converts non-ASCII characters to the \\xNNNN notation used in Haskell strings." (let ((pp-escape-newlines t) (s2 (copy-sequence s))) (set-text-properties 0 (length s2) nil s2) (mapconcat 'agda2-char-quote (pp-to-string s2) ""))) (defun agda2-list-quote (strings) "Convert a list of STRINGS into a string representing it in Haskell syntax." (concat "[" (mapconcat 'agda2-string-quote strings ", ") "]")) (defun agda2-goal-at(pos) "Return (goal overlay, goal number) at POS, or nil." (let ((os (and pos (overlays-at pos))) o g) (while (and os (not(setq g (overlay-get (setq o (pop os)) 'agda2-gn))))) (if g (list o g)))) (defun agda2-goal-overlay (g) "Returns the overlay of goal number G, if any." (car (remove nil (mapcar (lambda (o) (if (equal (overlay-get o 'agda2-gn) g) o)) (overlays-in (point-min) (point-max)))))) (defun agda2-range-of-goal (g) "The range of goal G." (let ((o (agda2-goal-overlay g))) (if o (list (overlay-start o) (overlay-end o))))) (defun agda2-goto-goal (g) (let ((p (+ 2 (car (agda2-range-of-goal g))))) (if p (goto-char p)))) (defun agda2-replace-goal (g newtxt) "Replace the content of goal G with NEWTXT." (interactive) (save-excursion (multiple-value-bind (p q) (agda2-range-of-goal g) (setq p (+ p 2) q (- q 2)) (let ((indent (and (goto-char p) (current-column)))) (delete-region p q) (insert newtxt) (while (re-search-backward "^" p t) (insert-char ? indent) (backward-char (1+ indent))))))) (defun agda2-forget-all-goals () "Remove all goal annotations. \(Including some text properties which might be used by other \(minor) modes.)" (annotation-preserve-mod-p-and-undo (remove-text-properties (point-min) (point-max) '(category nil agda2-delim2 nil agda2-delim3 nil display nil rear-nonsticky nil))) (let ((p (point-min))) (while (< (setq p (next-single-char-property-change p 'agda2-gn)) (point-max)) (delete-overlay (car (agda2-goal-at p)))))) (defun agda2-decl-beginning () "Find the beginning point of the declaration containing the point. To do: dealing with semicolon separated decls." (interactive) (save-excursion (let* ((pEnd (point)) (pDef (progn (goto-char (point-min)) (re-search-forward "\\s *" pEnd t))) (cDef (current-column))) (while (re-search-forward "where\\(\\s +\\)\\S \\|^\\(\\s *\\)\\S " pEnd t) (if (match-end 1) (setq pDef (goto-char (match-end 1)) cDef (current-column)) (goto-char (match-end 2)) (if (>= cDef (current-column)) (setq pDef (point) cDef (current-column)))) (forward-char)) (goto-char pDef) (if (equal (current-word) "mutual") (or (match-end 2) (match-end 1)) pDef)))) (defun agda2-beginning-of-decl () (interactive) (goto-char (agda2-decl-beginning))) (defvar agda2-debug-buffer-name "*Agda debug*" "The name of the buffer used for Agda debug messages.") (defun agda2-verbose (msg) "Appends the string MSG to the `agda2-debug-buffer-name' buffer. Note that this buffer's contents is not erased automatically when a file is loaded." (with-current-buffer (get-buffer-create agda2-debug-buffer-name) (save-excursion (goto-char (point-max)) (insert msg)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Comments and paragraphs (defun agda2-comments-and-paragraphs-setup nil "Set up comment and paragraph handling for Agda mode." ;; Syntax table setup for comments is done elsewhere. ;; Enable highlighting of comments via Font Lock mode (which uses ;; the syntax table). (set (make-local-variable 'font-lock-defaults) '(nil nil nil nil nil (font-lock-syntactic-face-function . agda2-font-syntactic-face))) ;; If the following s-expression is removed, then highlighting of ;; comments stops working. (when font-lock-mode (font-lock-mode t)) ;; Empty lines (all white space according to Emacs) delimit ;; paragraphs. (set (make-local-variable 'paragraph-start) "\\s-*$") (set (make-local-variable 'paragraph-separate) paragraph-start) ;; Support for adding/removing comments. (set (make-local-variable 'comment-start) "-- ") ;; Support for proper filling of text in comments (requires that ;; Filladapt is activated). (when (featurep 'filladapt) (add-to-list (make-local-variable 'filladapt-token-table) '("--" agda2-comment)) (add-to-list (make-local-variable 'filladapt-token-match-table) '(agda2-comment agda2-comment) t) (add-to-list (make-local-variable 'filladapt-token-conversion-table) '(agda2-comment . exact)))) (defun agda2-comment-dwim-rest-of-buffer () "Comment or uncomment the rest of the buffer. From the beginning of the current line to the end of the buffer." (interactive) (save-excursion (forward-line 0) (push-mark (point) 'no-message 'activate-mark) (unwind-protect (progn (goto-char (point-max)) (comment-dwim nil)) (pop-mark)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Go to definition site (defun agda2-goto-definition-keyboard (&optional other-window) "Go to the definition site of the name under point (if any). If this function is invoked with a prefix argument then another window is used to display the given position." (interactive "P") (annotation-goto-indirect (point) other-window)) (defun agda2-goto-definition-mouse (ev) "Go to the definition site of the name clicked on, if any. Otherwise, yank (see `mouse-yank-primary')." (interactive "e") (unless (annotation-goto-indirect ev) ;; FIXME: Shouldn't we use something like ;; (call-interactively (key-binding ev))? --Stef (mouse-yank-primary ev))) (defun agda2-go-back nil "Go back to the previous position in which `agda2-goto-definition-keyboard' or `agda2-goto-definition-mouse' was invoked." (interactive) (annotation-go-back)) (defun agda2-maybe-goto (filepos) "Might move point to the given error. FILEPOS should have the form (FILE . POSITION). If `agda2-highlight-in-progress' is nil, then nothing happens. Otherwise, if the current buffer is the one that is connected to the Agda process, then point is moved to POSITION in FILE (assuming that the FILE is readable). Otherwise point is moved to the given position in the buffer visiting the file, if any, and in every window displaying the buffer, but the window configuration and the selected window are not changed." (when (and agda2-highlight-in-progress (consp filepos) (stringp (car filepos)) (integerp (cdr filepos))) (if agda2-in-agda2-file-buffer (annotation-goto-and-push (current-buffer) (point) filepos) (save-excursion (let ((buffer (find-buffer-visiting (car filepos)))) (when buffer (let ((windows (get-buffer-window-list buffer 'no-minibuffer t))) (if windows (dolist (window windows) (with-selected-window window (annotation-goto-position (cdr filepos)))) (with-current-buffer buffer (annotation-goto-position (cdr filepos))))))))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Implicit arguments (defun agda2-display-implicit-arguments (&optional arg) "Toggle display of implicit arguments. With prefix argument, turn on display of implicit arguments if the argument is a positive number, otherwise turn it off." (interactive "P") (cond ((eq arg nil) (agda2-go nil t t t "ToggleImplicitArgs")) ((and (numberp arg) (> arg 0)) (agda2-go nil t t t "ShowImplicitArgs" "True")) (t (agda2-go nil t t t "ShowImplicitArgs" "False")))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; (defun agda2-popup-menu-3 (ev) "If in a goal, popup the goal menu and call chosen command." (interactive "e") (let (choice) (save-excursion (and (agda2-goal-at (goto-char (posn-point (event-end ev)))) (setq choice (x-popup-menu ev agda2-goal-map)) (call-interactively (lookup-key agda2-goal-map (apply 'vector choice))))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Switching to a different version of Agda (defun get-agda-program-versions () "Get \"version strings\" of executables starting with 'agda-mode' in current path." (delete-dups (mapcar (lambda (path) ;; strip 'agda-mode' prefix (replace-regexp-in-string "^agda-mode-?" "" (file-name-nondirectory path))) (remove-if-not 'file-executable-p ;; concatenate result (reduce 'append ;; for each directory in exec-path, get list of ;; files whose name starts with 'agda-mode' (mapcar (lambda (path) (when (file-accessible-directory-p path) (directory-files path 't "^agda-mode"))) exec-path)))))) ;; Note that other versions of Agda may use different protocols, so ;; this function unloads the Emacs mode. (defun agda2-set-program-version (version) "Tries to switch to Agda version VERSION. This command assumes that the agda and agda-mode executables for Agda version VERSION are called agda-VERSION and agda-mode-VERSION, and that they are located on the PATH. (If VERSION is empty, then agda and agda-mode are used instead.)" (interactive (list (completing-read "Version: " (get-agda-program-versions)))) (let* ((agda-buffers (mapcan (lambda (buf) (with-current-buffer buf (when (equal major-mode 'agda2-mode) (list buf)))) (buffer-list))) (version-suffix (if (or (equal version "") (equal version nil)) "" (concat "-" version))) ;; Run agda-mode and make sure that it returns ;; successfully. (coding-system-for-read 'utf-8) (agda-mode-prog (concat "agda-mode" version-suffix)) (agda-mode-path (condition-case nil (with-temp-buffer (unless (equal 0 (call-process agda-mode-prog nil (current-buffer) nil "locate")) (error "%s" (concat "Error when running " agda-mode-prog))) (buffer-string)) (file-error (error "%s" (concat "Could not find " agda-mode-prog)))))) ;; Make sure that agda-mode returns a valid file. (unless (file-readable-p agda-mode-path) (error "%s" (concat "Could not read " agda-mode-path))) ;; Kill some processes/buffers related to Agda. (when (and agda2-process (process-status agda2-process)) (kill-process agda2-process)) (when (buffer-live-p agda2-process-buffer) (kill-buffer agda2-process-buffer)) (when (buffer-live-p agda2-info-buffer) (kill-buffer agda2-info-buffer)) (when (and agda2-debug-buffer-name (get-buffer agda2-debug-buffer-name)) (kill-buffer agda2-debug-buffer-name)) ;; Remove the Agda mode directory from the load path. (setq load-path (delete agda2-directory load-path)) ;; Unload the Agda mode and its dependencies. (unload-feature 'agda2-mode 'force) (unload-feature 'agda2 'force) (unload-feature 'eri 'force) (unload-feature 'annotation 'force) (unload-feature 'agda-input 'force) (unload-feature 'agda2-highlight 'force) (unload-feature 'agda2-abbrevs 'force) (unload-feature 'agda2-queue 'force) ;; Load the new version of Agda. (load-file agda-mode-path) (require 'agda2-mode) (setq agda2-program-name (concat "agda" version-suffix)) ;; Restart the Agda mode in all former Agda mode buffers. (mapc (lambda (buf) (with-current-buffer buf (agda2-mode))) agda-buffers))) (provide 'agda2-mode) ;;; agda2-mode.el ends here Agda-2.5.3/src/data/emacs-mode/agda-input.el0000644000000000000000000011022313154613124016627 0ustar0000000000000000;;; agda-input.el --- The Agda input method ;;; Commentary: ;; A highly customisable input method which can inherit from other ;; Quail input methods. By default the input method is geared towards ;; the input of mathematical and other symbols in Agda programs. ;; ;; Use M-x customize-group agda-input to customise this input method. ;; Note that the functions defined under "Functions used to tweak ;; translation pairs" below can be used to tweak both the key ;; translations inherited from other input methods as well as the ;; ones added specifically for this one. ;; ;; Use agda-input-show-translations to see all the characters which ;; can be typed using this input method (except for those ;; corresponding to ASCII characters). ;;; Code: (require 'quail) (require 'cl) ;; Quail is quite stateful, so be careful when editing this code. Note ;; that with-temp-buffer is used below whenever buffer-local state is ;; modified. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Utility functions (defun agda-input-concat-map (f xs) "Concat (map F XS)." (apply 'append (mapcar f xs))) (defun agda-input-to-string-list (s) "Convert a string S to a list of one-character strings, after removing all space and newline characters." (agda-input-concat-map (lambda (c) (if (member c (string-to-list " \n")) nil (list (string c)))) (string-to-list s))) (defun agda-input-character-range (from to) "A string consisting of the characters from FROM to TO." (let (seq) (dotimes (i (1+ (- to from))) (setq seq (cons (+ from i) seq))) (concat (nreverse seq)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Functions used to tweak translation pairs ;; lexical-let is used since Elisp lacks lexical scoping. (defun agda-input-compose (f g) "\x -> concatMap F (G x)" (lexical-let ((f1 f) (g1 g)) (lambda (x) (agda-input-concat-map f1 (funcall g1 x))))) (defun agda-input-or (f g) "\x -> F x ++ G x" (lexical-let ((f1 f) (g1 g)) (lambda (x) (append (funcall f1 x) (funcall g1 x))))) (defun agda-input-nonempty () "Only keep pairs with a non-empty first component." (lambda (x) (if (> (length (car x)) 0) (list x)))) (defun agda-input-prepend (prefix) "Prepend PREFIX to all key sequences." (lexical-let ((prefix1 prefix)) (lambda (x) `((,(concat prefix1 (car x)) . ,(cdr x)))))) (defun agda-input-prefix (prefix) "Only keep pairs whose key sequence starts with PREFIX." (lexical-let ((prefix1 prefix)) (lambda (x) (if (equal (substring (car x) 0 (length prefix1)) prefix1) (list x))))) (defun agda-input-suffix (suffix) "Only keep pairs whose key sequence ends with SUFFIX." (lexical-let ((suffix1 suffix)) (lambda (x) (if (equal (substring (car x) (- (length (car x)) (length suffix1))) suffix1) (list x))))) (defun agda-input-drop (ss) "Drop pairs matching one of the given key sequences. SS should be a list of strings." (lexical-let ((ss1 ss)) (lambda (x) (unless (member (car x) ss1) (list x))))) (defun agda-input-drop-beginning (n) "Drop N characters from the beginning of each key sequence." (lexical-let ((n1 n)) (lambda (x) `((,(substring (car x) n1) . ,(cdr x)))))) (defun agda-input-drop-end (n) "Drop N characters from the end of each key sequence." (lexical-let ((n1 n)) (lambda (x) `((,(substring (car x) 0 (- (length (car x)) n1)) . ,(cdr x)))))) (defun agda-input-drop-prefix (prefix) "Only keep pairs whose key sequence starts with PREFIX. This prefix is dropped." (agda-input-compose (agda-input-drop-beginning (length prefix)) (agda-input-prefix prefix))) (defun agda-input-drop-suffix (suffix) "Only keep pairs whose key sequence ends with SUFFIX. This suffix is dropped." (lexical-let ((suffix1 suffix)) (agda-input-compose (agda-input-drop-end (length suffix1)) (agda-input-suffix suffix1)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Customization ;; The :set keyword is set to 'agda-input-incorporate-changed-setting ;; so that the input method gets updated immediately when users ;; customize it. However, the setup functions cannot be run before all ;; variables have been defined. Hence the :initialize keyword is set to ;; 'custom-initialize-default to ensure that the setup is not performed ;; until agda-input-setup is called at the end of this file. (defgroup agda-input nil "The Agda input method. After tweaking these settings you may want to inspect the resulting translations using `agda-input-show-translations'." :group 'agda2 :group 'leim) (defcustom agda-input-tweak-all '(agda-input-compose (agda-input-prepend "\\") (agda-input-nonempty)) "An expression yielding a function which can be used to tweak all translations before they are included in the input method. The resulting function (if non-nil) is applied to every \(KEY-SEQUENCE . TRANSLATION) pair and should return a list of such pairs. (Note that the translations can be anything accepted by `quail-defrule'.) If you change this setting manually (without using the customization buffer) you need to call `agda-input-setup' in order for the change to take effect." :group 'agda-input :set 'agda-input-incorporate-changed-setting :initialize 'custom-initialize-default :type 'sexp) (defcustom agda-input-inherit `(("TeX" . (agda-input-compose (agda-input-drop '("geq" "leq" "bullet" "qed" "par")) (agda-input-or (agda-input-drop-prefix "\\") (agda-input-or (agda-input-compose (agda-input-drop '("^l" "^o" "^r" "^v")) (agda-input-prefix "^")) (agda-input-prefix "_"))))) ) "A list of Quail input methods whose translations should be inherited by the Agda input method (with the exception of translations corresponding to ASCII characters). The list consists of pairs (qp . tweak), where qp is the name of a Quail package, and tweak is an expression of the same kind as `agda-input-tweak-all' which is used to tweak the translation pairs of the input method. The inherited translation pairs are added last, after `agda-input-user-translations' and `agda-input-translations'. If you change this setting manually (without using the customization buffer) you need to call `agda-input-setup' in order for the change to take effect." :group 'agda-input :set 'agda-input-incorporate-changed-setting :initialize 'custom-initialize-default :type '(repeat (cons (string :tag "Quail package") (sexp :tag "Tweaking function")))) (defcustom agda-input-translations (let ((max-lisp-eval-depth 2800)) `( ;; Equality and similar symbols. ("eq" . ,(agda-input-to-string-list "=∼∽≈≋∻∾∿≀≃⋍≂≅ ≌≊≡≣≐≑≒≓≔≕≖≗≘≙≚≛≜≝≞≟≍≎≏≬⋕")) ("eqn" . ,(agda-input-to-string-list "≠≁ ≉ ≄ ≇≆ ≢ ≭ ")) ("=n" . ("≠")) ("~" . ("∼")) ("~n" . ("≁")) ("~~" . ("≈")) ("~~n" . ("≉")) ("~~~" . ("≋")) (":~" . ("∻")) ("~-" . ("≃")) ("~-n" . ("≄")) ("-~" . ("≂")) ("~=" . ("≅")) ("~=n" . ("≇")) ("~~-" . ("≊")) ("==" . ("≡")) ("==n" . ("≢")) ("===" . ("≣")) (".=" . ("≐")) (".=." . ("≑")) (":=" . ("≔")) ("=:" . ("≕")) ("=o" . ("≗")) ("(=" . ("≘")) ("and=" . ("≙")) ("or=" . ("≚")) ("*=" . ("≛")) ("t=" . ("≜")) ("def=" . ("≝")) ("m=" . ("≞")) ("?=" . ("≟")) ;; Inequality and similar symbols. ("leq" . ,(agda-input-to-string-list "<≪⋘≤≦≲ ≶≺≼≾⊂⊆ ⋐⊏⊑ ⊰⊲⊴⋖⋚⋜⋞")) ("leqn" . ,(agda-input-to-string-list "≮ ≰≨≴⋦≸⊀ ⋨⊄⊈⊊ ⋢⋤ ⋪⋬ ⋠")) ("geq" . ,(agda-input-to-string-list ">≫⋙≥≧≳ ≷≻≽≿⊃⊇ ⋑⊐⊒ ⊱⊳⊵⋗⋛⋝⋟")) ("geqn" . ,(agda-input-to-string-list "≯ ≱≩≵⋧≹⊁ ⋩⊅⊉⊋ ⋣⋥ ⋫⋭ ⋡")) ("<=" . ("≤")) (">=" . ("≥")) ("<=n" . ("≰")) (">=n" . ("≱")) ("len" . ("≰")) ("gen" . ("≱")) ("n" . ("≯")) ("<~" . ("≲")) (">~" . ("≳")) ("<~n" . ("⋦")) (">~n" . ("⋧")) ("<~nn" . ("≴")) (">~nn" . ("≵")) ("sub" . ("⊂")) ("sup" . ("⊃")) ("subn" . ("⊄")) ("supn" . ("⊅")) ("sub=" . ("⊆")) ("sup=" . ("⊇")) ("sub=n" . ("⊈")) ("sup=n" . ("⊉")) ("squb" . ("⊏")) ("squp" . ("⊐")) ("squb=" . ("⊑")) ("squp=" . ("⊒")) ("squb=n" . ("⋢")) ("squp=n" . ("⋣")) ;; Set membership etc. ("member" . ,(agda-input-to-string-list "∈∉∊∋∌∍⋲⋳⋴⋵⋶⋷⋸⋹⋺⋻⋼⋽⋾⋿")) ("inn" . ("∉")) ("nin" . ("∌")) ;; Intersections, unions etc. ("intersection" . ,(agda-input-to-string-list "∩⋂∧⋀⋏⨇⊓⨅⋒∏ ⊼ ⨉")) ("union" . ,(agda-input-to-string-list "∪⋃∨⋁⋎⨈⊔⨆⋓∐⨿⊽⊻⊍⨃⊎⨄⊌∑⅀")) ("and" . ("∧")) ("or" . ("∨")) ("And" . ("⋀")) ("Or" . ("⋁")) ("i" . ("∩")) ("un" . ("∪")) ("u+" . ("⊎")) ("u." . ("⊍")) ("I" . ("⋂")) ("Un" . ("⋃")) ("U+" . ("⨄")) ("U." . ("⨃")) ("glb" . ("⊓")) ("lub" . ("⊔")) ("Glb" . ("⨅")) ("Lub" . ("⨆")) ;; Entailment etc. ("entails" . ,(agda-input-to-string-list "⊢⊣⊤⊥⊦⊧⊨⊩⊪⊫⊬⊭⊮⊯")) ("|-" . ("⊢")) ("|-n" . ("⊬")) ("-|" . ("⊣")) ("|=" . ("⊨")) ("|=n" . ("⊭")) ("||-" . ("⊩")) ("||-n" . ("⊮")) ("||=" . ("⊫")) ("||=n" . ("⊯")) ("|||-" . ("⊪")) ;; Divisibility, parallelity. ("|" . ("∣")) ("|n" . ("∤")) ("||" . ("∥")) ("||n" . ("∦")) ;; Some symbols from logic and set theory. ("all" . ("∀")) ("ex" . ("∃")) ("exn" . ("∄")) ("0" . ("∅")) ("C" . ("∁")) ;; Corners, ceilings and floors. ("c" . ,(agda-input-to-string-list "⌜⌝⌞⌟⌈⌉⌊⌋")) ("cu" . ,(agda-input-to-string-list "⌜⌝ ⌈⌉ ")) ("cl" . ,(agda-input-to-string-list " ⌞⌟ ⌊⌋")) ("cul" . ("⌜")) ("cuL" . ("⌈")) ("cur" . ("⌝")) ("cuR" . ("⌉")) ("cll" . ("⌞")) ("clL" . ("⌊")) ("clr" . ("⌟")) ("clR" . ("⌋")) ;; Various operators/symbols. ("qed" . ("∎")) ("x" . ("×")) ("o" . ("∘")) ("comp" . ("∘")) ("." . ("∙")) ("*" . ("⋆")) (".+" . ("∔")) (".-" . ("∸")) (":" . ,(agda-input-to-string-list "∶⦂ː꞉˸፥፦:﹕︓")) ("::" . ("∷")) ("::-" . ("∺")) ("-:" . ("∹")) ("+ " . ("⊹")) ("surd3" . ("∛")) ("surd4" . ("∜")) ("increment" . ("∆")) ("inf" . ("∞")) ("&" . ("⅋")) ;; Circled operators. ("o+" . ("⊕")) ("o--" . ("⊖")) ("ox" . ("⊗")) ("o/" . ("⊘")) ("o." . ("⊙")) ("oo" . ("⊚")) ("o*" . ("⊛")) ("o=" . ("⊜")) ("o-" . ("⊝")) ("O+" . ("⨁")) ("Ox" . ("⨂")) ("O." . ("⨀")) ("O*" . ("⍟")) ;; Boxed operators. ("b+" . ("⊞")) ("b-" . ("⊟")) ("bx" . ("⊠")) ("b." . ("⊡")) ;; Various symbols. ("integral" . ,(agda-input-to-string-list "∫∬∭∮∯∰∱∲∳")) ("angle" . ,(agda-input-to-string-list "∟∡∢⊾⊿")) ("join" . ,(agda-input-to-string-list "⋈⋉⋊⋋⋌⨝⟕⟖⟗")) ;; Arrows. ("l" . ,(agda-input-to-string-list "←⇐⇚⇇⇆↤⇦↞↼↽⇠⇺↜⇽⟵⟸↚⇍⇷ ↹ ↢↩↫⇋⇜⇤⟻⟽⤆↶↺⟲ ")) ("r" . ,(agda-input-to-string-list "→⇒⇛⇉⇄↦⇨↠⇀⇁⇢⇻↝⇾⟶⟹↛⇏⇸⇶ ↴ ↣↪↬⇌⇝⇥⟼⟾⤇↷↻⟳⇰⇴⟴⟿ ➵➸➙➔➛➜➝➞➟➠➡➢➣➤➧➨➩➪➫➬➭➮➯➱➲➳➺➻➼➽➾⊸")) ("u" . ,(agda-input-to-string-list "↑⇑⟰⇈⇅↥⇧↟↿↾⇡⇞ ↰↱➦ ⇪⇫⇬⇭⇮⇯ ")) ("d" . ,(agda-input-to-string-list "↓⇓⟱⇊⇵↧⇩↡⇃⇂⇣⇟ ↵↲↳➥ ↯ ")) ("ud" . ,(agda-input-to-string-list "↕⇕ ↨⇳ ")) ("lr" . ,(agda-input-to-string-list "↔⇔ ⇼↭⇿⟷⟺↮⇎⇹ ")) ("ul" . ,(agda-input-to-string-list "↖⇖ ⇱↸ ")) ("ur" . ,(agda-input-to-string-list "↗⇗ ➶➹➚ ")) ("dr" . ,(agda-input-to-string-list "↘⇘ ⇲ ➴➷➘ ")) ("dl" . ,(agda-input-to-string-list "↙⇙ ")) ("l-" . ("←")) ("<-" . ("←")) ("l=" . ("⇐")) ("r-" . ("→")) ("->" . ("→")) ("r=" . ("⇒")) ("=>" . ("⇒")) ("u-" . ("↑")) ("u=" . ("⇑")) ("d-" . ("↓")) ("d=" . ("⇓")) ("ud-" . ("↕")) ("ud=" . ("⇕")) ("lr-" . ("↔")) ("<->" . ("↔")) ("lr=" . ("⇔")) ("<=>" . ("⇔")) ("ul-" . ("↖")) ("ul=" . ("⇖")) ("ur-" . ("↗")) ("ur=" . ("⇗")) ("dr-" . ("↘")) ("dr=" . ("⇘")) ("dl-" . ("↙")) ("dl=" . ("⇙")) ("l==" . ("⇚")) ("l-2" . ("⇇")) ("l-r-" . ("⇆")) ("r==" . ("⇛")) ("r-2" . ("⇉")) ("r-3" . ("⇶")) ("r-l-" . ("⇄")) ("u==" . ("⟰")) ("u-2" . ("⇈")) ("u-d-" . ("⇅")) ("d==" . ("⟱")) ("d-2" . ("⇊")) ("d-u-" . ("⇵")) ("l--" . ("⟵")) ("<--" . ("⟵")) ("l~" . ("↜" "⇜")) ("r--" . ("⟶")) ("-->" . ("⟶")) ("r~" . ("↝" "⇝" "⟿")) ("lr--" . ("⟷")) ("<-->" . ("⟷")) ("lr~" . ("↭")) ("l-n" . ("↚")) ("<-n" . ("↚")) ("l=n" . ("⇍")) ("r-n" . ("↛")) ("->n" . ("↛")) ("r=n" . ("⇏")) ("=>n" . ("⇏")) ("lr-n" . ("↮")) ("<->n" . ("↮")) ("lr=n" . ("⇎")) ("<=>n" . ("⇎")) ("l-|" . ("↤")) ("ll-" . ("↞")) ("r-|" . ("↦")) ("rr-" . ("↠")) ("u-|" . ("↥")) ("uu-" . ("↟")) ("d-|" . ("↧")) ("dd-" . ("↡")) ("ud-|" . ("↨")) ("l->" . ("↢")) ("r->" . ("↣")) ("r-o" . ("⊸")) ("-o" . ("⊸")) ("dz" . ("↯")) ;; Ellipsis. ("..." . ,(agda-input-to-string-list "⋯⋮⋰⋱")) ;; Box-drawing characters. ("---" . ,(agda-input-to-string-list "─│┌┐└┘├┤┬┼┴╴╵╶╷╭╮╯╰╱╲╳")) ("--=" . ,(agda-input-to-string-list "═║╔╗╚╝╠╣╦╬╩ ╒╕╘╛╞╡╤╪╧ ╓╖╙╜╟╢╥╫╨")) ("--_" . ,(agda-input-to-string-list "━┃┏┓┗┛┣┫┳╋┻╸╹╺╻ ┍┯┑┕┷┙┝┿┥┎┰┒┖┸┚┠╂┨┞╀┦┟╁┧┢╈┪┡╇┩ ┮┭┶┵┾┽┲┱┺┹╊╉╆╅╄╃ ╿╽╼╾")) ("--." . ,(agda-input-to-string-list "╌╎┄┆┈┊ ╍╏┅┇┉┋")) ;; Triangles. ;; Big/small, black/white. ("t" . ,(agda-input-to-string-list "◂◃◄◅▸▹►▻▴▵▾▿◢◿◣◺◤◸◥◹")) ("T" . ,(agda-input-to-string-list "◀◁▶▷▲△▼▽◬◭◮")) ("tb" . ,(agda-input-to-string-list "◂▸▴▾◄►◢◣◤◥")) ("tw" . ,(agda-input-to-string-list "◃▹▵▿◅▻◿◺◸◹")) ("Tb" . ,(agda-input-to-string-list "◀▶▲▼")) ("Tw" . ,(agda-input-to-string-list "◁▷△▽")) ;; Squares. ("sq" . ,(agda-input-to-string-list "■□◼◻◾◽▣▢▤▥▦▧▨▩◧◨◩◪◫◰◱◲◳")) ("sqb" . ,(agda-input-to-string-list "■◼◾")) ("sqw" . ,(agda-input-to-string-list "□◻◽")) ("sq." . ("▣")) ("sqo" . ("▢")) ;; Rectangles. ("re" . ,(agda-input-to-string-list "▬▭▮▯")) ("reb" . ,(agda-input-to-string-list "▬▮")) ("rew" . ,(agda-input-to-string-list "▭▯")) ;; Parallelograms. ("pa" . ,(agda-input-to-string-list "▰▱")) ("pab" . ("▰")) ("paw" . ("▱")) ;; Diamonds. ("di" . ,(agda-input-to-string-list "◆◇◈")) ("dib" . ("◆")) ("diw" . ("◇")) ("di." . ("◈")) ;; Circles. ("ci" . ,(agda-input-to-string-list "●○◎◌◯◍◐◑◒◓◔◕◖◗◠◡◴◵◶◷⚆⚇⚈⚉")) ("cib" . ("●")) ("ciw" . ("○")) ("ci." . ("◎")) ("ci.." . ("◌")) ("ciO" . ("◯")) ;; Stars. ("st" . ,(agda-input-to-string-list "⋆✦✧✶✴✹ ★☆✪✫✯✰✵✷✸")) ("st4" . ,(agda-input-to-string-list "✦✧")) ("st6" . ("✶")) ("st8" . ("✴")) ("st12" . ("✹")) ;; Blackboard bold letters. ("bA" . ("𝔸")) ("bB" . ("𝔹")) ("bC" . ("ℂ")) ("bD" . ("𝔻")) ("bE" . ("𝔼")) ("bF" . ("𝔽")) ("bG" . ("𝔾")) ("bH" . ("ℍ")) ("bI" . ("𝕀")) ("bJ" . ("𝕁")) ("bK" . ("𝕂")) ("bL" . ("𝕃")) ("bM" . ("𝕄")) ("bN" . ("ℕ")) ("bO" . ("𝕆")) ("bP" . ("ℙ")) ("bQ" . ("ℚ")) ("bR" . ("ℝ")) ("bS" . ("𝕊")) ("bT" . ("𝕋")) ("bU" . ("𝕌")) ("bV" . ("𝕍")) ("bW" . ("𝕎")) ("bX" . ("𝕏")) ("bY" . ("𝕐")) ("bZ" . ("ℤ")) ("bGG" . ("ℾ")) ("bGP" . ("ℿ")) ("bGS" . ("⅀")) ("ba" . ("𝕒")) ("bb" . ("𝕓")) ("bc" . ("𝕔")) ("bd" . ("𝕕")) ("be" . ("𝕖")) ("bf" . ("𝕗")) ("bg" . ("𝕘")) ("bh" . ("𝕙")) ("bi" . ("𝕚")) ("bj" . ("𝕛")) ("bk" . ("𝕜")) ("bl" . ("𝕝")) ("bm" . ("𝕞")) ("bn" . ("𝕟")) ("bo" . ("𝕠")) ("bp" . ("𝕡")) ("bq" . ("𝕢")) ("br" . ("𝕣")) ("bs" . ("𝕤")) ("bt" . ("𝕥")) ("bu" . ("𝕦")) ("bv" . ("𝕧")) ("bw" . ("𝕨")) ("bx" . ("𝕩")) ("by" . ("𝕪")) ("bz" . ("𝕫")) ("bGg" . ("ℽ")) ("bGp" . ("ℼ")) ;; Blackboard bold numbers. ("b0" . ("𝟘")) ("b1" . ("𝟙")) ("b2" . ("𝟚")) ("b3" . ("𝟛")) ("b4" . ("𝟜")) ("b5" . ("𝟝")) ("b6" . ("𝟞")) ("b7" . ("𝟟")) ("b8" . ("𝟠")) ("b9" . ("𝟡")) ;; Mathematical bold digits. ("B0" . ("𝟎")) ("B1" . ("𝟏")) ("B2" . ("𝟐")) ("B3" . ("𝟑")) ("B4" . ("𝟒")) ("B5" . ("𝟓")) ("B6" . ("𝟔")) ("B7" . ("𝟕")) ("B8" . ("𝟖")) ("B9" . ("𝟗")) ;; Parentheses. ("(" . ,(agda-input-to-string-list "([{⁅⁽₍〈⎴⟅⟦⟨⟪⦃〈《「『【〔〖〚︵︷︹︻︽︿﹁﹃﹙﹛﹝([{「")) (")" . ,(agda-input-to-string-list ")]}⁆⁾₎〉⎵⟆⟧⟩⟫⦄〉》」』】〕〗〛︶︸︺︼︾﹀﹂﹄﹚﹜﹞)]}」")) ("[[" . ("⟦")) ("]]" . ("⟧")) ("<" . ("⟨")) (">" . ("⟩")) ("<<" . ("⟪")) (">>" . ("⟫")) ("{{" . ("⦃")) ("}}" . ("⦄")) ("(b" . ("⟅")) (")b" . ("⟆")) ("lbag" . ("⟅")) ("rbag" . ("⟆")) ("(|" . ("⦇")) ("|)" . ("⦈")) ;; Primes. ("'" . ,(agda-input-to-string-list "′″‴⁗")) ("`" . ,(agda-input-to-string-list "‵‶‷")) ;; Fractions. ("frac" . ,(agda-input-to-string-list "¼½¾⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞⅟")) ;; Bullets. ("bu" . ,(agda-input-to-string-list "•◦‣⁌⁍")) ("bub" . ("•")) ("buw" . ("◦")) ("but" . ("‣")) ;; Musical symbols. ("note" . ,(agda-input-to-string-list "♩♪♫♬")) ("b" . ("♭")) ("#" . ("♯")) ;; Other punctuation and symbols. ("\\" . ("\\")) ("en" . ("–")) ("em" . ("—")) ("!!" . ("‼")) ("??" . ("⁇")) ("?!" . ("‽" "⁈")) ("!?" . ("⁉")) ("die" . ,(agda-input-to-string-list "⚀⚁⚂⚃⚄⚅")) ("asterisk" . ,(agda-input-to-string-list "⁎⁑⁂✢✣✤✥✱✲✳✺✻✼✽❃❉❊❋")) ("8<" . ("✂" "✄")) ("tie" . ("⁀")) ("undertie" . ("‿")) ("apl" . ,(agda-input-to-string-list "⌶⌷⌸⌹⌺⌻⌼⌽⌾⌿⍀⍁⍂⍃⍄⍅⍆⍇⍈ ⍉⍊⍋⍌⍍⍎⍏⍐⍑⍒⍓⍔⍕⍖⍗⍘⍙⍚⍛ ⍜⍝⍞⍟⍠⍡⍢⍣⍤⍥⍦⍧⍨⍩⍪⍫⍬⍭⍮ ⍯⍰⍱⍲⍳⍴⍵⍶⍷⍸⍹⍺⎕")) ;; Some combining characters. ;; ;; The following combining characters also have (other) ;; translations: ;; ̀ ́ ̂ ̃ ̄ ̆ ̇ ̈ ̋ ̌ ̣ ̧ ̱ ("^--" . ,(agda-input-to-string-list"̅̿")) ("_--" . ,(agda-input-to-string-list"̲̳")) ("^~" . ,(agda-input-to-string-list"̃͌")) ("_~" . ( "̰")) ("^." . ,(agda-input-to-string-list"̇̈⃛⃜")) ("_." . ,(agda-input-to-string-list"̣̤")) ("^l" . ,(agda-input-to-string-list"⃖⃐⃔")) ("^l-" . ( "⃖")) ("^r" . ,(agda-input-to-string-list"⃗⃑⃕")) ("^r-" . ( "⃗")) ("^lr" . ( "⃡")) ("_lr" . ( "͍")) ("^^" . ,(agda-input-to-string-list"̂̑͆")) ("_^" . ,(agda-input-to-string-list"̭̯̪")) ("^v" . ,(agda-input-to-string-list"̌̆")) ("_v" . ,(agda-input-to-string-list"̬̮̺")) ;; Shorter forms of many greek letters plus ƛ. ("Ga" . ("α")) ("GA" . ("Α")) ("Gb" . ("β")) ("GB" . ("Β")) ("Gg" . ("γ")) ("GG" . ("Γ")) ("Gd" . ("δ")) ("GD" . ("Δ")) ("Ge" . ("ε")) ("GE" . ("Ε")) ("Gz" . ("ζ")) ("GZ" . ("Ζ")) ;; \eta \Eta ("Gth" . ("θ")) ("GTH" . ("Θ")) ("Gi" . ("ι")) ("GI" . ("Ι")) ("Gk" . ("κ")) ("GK" . ("Κ")) ("Gl" . ("λ")) ("GL" . ("Λ")) ("Gl-" . ("ƛ")) ("Gm" . ("μ")) ("GM" . ("Μ")) ("Gn" . ("ν")) ("GN" . ("Ν")) ("Gx" . ("ξ")) ("GX" . ("Ξ")) ;; \omicron \Omicron ;; \pi \Pi ("Gr" . ("ρ")) ("GR" . ("Ρ")) ("Gs" . ("σ")) ("GS" . ("Σ")) ("Gt" . ("τ")) ("GT" . ("Τ")) ("Gu" . ("υ")) ("GU" . ("Υ")) ("Gf" . ("φ")) ("GF" . ("Φ")) ("Gc" . ("χ")) ("GC" . ("Χ")) ("Gp" . ("ψ")) ("GP" . ("Ψ")) ("Go" . ("ω")) ("GO" . ("Ω")) ;; Mathematical characters ("MiA" . ("𝐴")) ("MiB" . ("𝐵")) ("MiC" . ("𝐶")) ("MiD" . ("𝐷")) ("MiE" . ("𝐸")) ("MiF" . ("𝐹")) ("MiG" . ("𝐺")) ("MiH" . ("𝐻")) ("MiI" . ("𝐼")) ("MiJ" . ("𝐽")) ("MiK" . ("𝐾")) ("MiL" . ("𝐿")) ("MiM" . ("𝑀")) ("MiN" . ("𝑁")) ("MiO" . ("𝑂")) ("MiP" . ("𝑃")) ("MiQ" . ("𝑄")) ("MiR" . ("𝑅")) ("MiS" . ("𝑆")) ("MiT" . ("𝑇")) ("MiU" . ("𝑈")) ("MiV" . ("𝑉")) ("MiW" . ("𝑊")) ("MiX" . ("𝑋")) ("MiY" . ("𝑌")) ("MiZ" . ("𝑍")) ("Mia" . ("𝑎")) ("Mib" . ("𝑏")) ("Mic" . ("𝑐")) ("Mid" . ("𝑑")) ("Mie" . ("𝑒")) ("Mif" . ("𝑓")) ("Mig" . ("𝑔")) ("Mii" . ("𝑖")) ("Mij" . ("𝑗")) ("Mik" . ("𝑘")) ("Mil" . ("𝑙")) ("Mim" . ("𝑚")) ("Min" . ("𝑛")) ("Mio" . ("𝑜")) ("Mip" . ("𝑝")) ("Miq" . ("𝑞")) ("Mir" . ("𝑟")) ("Mis" . ("𝑠")) ("Mit" . ("𝑡")) ("Miu" . ("𝑢")) ("Miv" . ("𝑣")) ("Miw" . ("𝑤")) ("Mix" . ("𝑥")) ("Miy" . ("𝑦")) ("Miz" . ("𝑧")) ("MIA" . ("𝑨")) ("MIB" . ("𝑩")) ("MIC" . ("𝑪")) ("MID" . ("𝑫")) ("MIE" . ("𝑬")) ("MIF" . ("𝑭")) ("MIG" . ("𝑮")) ("MIH" . ("𝑯")) ("MII" . ("𝑰")) ("MIJ" . ("𝑱")) ("MIK" . ("𝑲")) ("MIL" . ("𝑳")) ("MIM" . ("𝑴")) ("MIN" . ("𝑵")) ("MIO" . ("𝑶")) ("MIP" . ("𝑷")) ("MIQ" . ("𝑸")) ("MIR" . ("𝑹")) ("MIS" . ("𝑺")) ("MIT" . ("𝑻")) ("MIU" . ("𝑼")) ("MIV" . ("𝑽")) ("MIW" . ("𝑾")) ("MIX" . ("𝑿")) ("MIY" . ("𝒀")) ("MIZ" . ("𝒁")) ("MIa" . ("𝒂")) ("MIb" . ("𝒃")) ("MIc" . ("𝒄")) ("MId" . ("𝒅")) ("MIe" . ("𝒆")) ("MIf" . ("𝒇")) ("MIg" . ("𝒈")) ("MIh" . ("𝒉")) ("MIi" . ("𝒊")) ("MIj" . ("𝒋")) ("MIk" . ("𝒌")) ("MIl" . ("𝒍")) ("MIm" . ("𝒎")) ("MIn" . ("𝒏")) ("MIo" . ("𝒐")) ("MIp" . ("𝒑")) ("MIq" . ("𝒒")) ("MIr" . ("𝒓")) ("MIs" . ("𝒔")) ("MIt" . ("𝒕")) ("MIu" . ("𝒖")) ("MIv" . ("𝒗")) ("MIw" . ("𝒘")) ("MIx" . ("𝒙")) ("MIy" . ("𝒚")) ("MIz" . ("𝒛")) ("McA" . ("𝒜")) ("McC" . ("𝒞")) ("McD" . ("𝒟")) ("McG" . ("𝒢")) ("McJ" . ("𝒥")) ("McK" . ("𝒦")) ("McN" . ("𝒩")) ("McO" . ("𝒪")) ("McP" . ("𝒫")) ("McQ" . ("𝒬")) ("McS" . ("𝒮")) ("McT" . ("𝒯")) ("McU" . ("𝒰")) ("McV" . ("𝒱")) ("McW" . ("𝒲")) ("McX" . ("𝒳")) ("McY" . ("𝒴")) ("McZ" . ("𝒵")) ("Mca" . ("𝒶")) ("Mcb" . ("𝒷")) ("Mcc" . ("𝒸")) ("Mcd" . ("𝒹")) ("Mcf" . ("𝒻")) ("Mch" . ("𝒽")) ("Mci" . ("𝒾")) ("Mcj" . ("𝒿")) ("Mck" . ("𝓀")) ("Mcl" . ("𝓁")) ("Mcm" . ("𝓂")) ("Mcn" . ("𝓃")) ("Mcp" . ("𝓅")) ("Mcq" . ("𝓆")) ("Mcr" . ("𝓇")) ("Mcs" . ("𝓈")) ("Mct" . ("𝓉")) ("Mcu" . ("𝓊")) ("Mcv" . ("𝓋")) ("Mcw" . ("𝓌")) ("Mcx" . ("𝓍")) ("Mcy" . ("𝓎")) ("Mcz" . ("𝓏")) ("MCA" . ("𝓐")) ("MCB" . ("𝓑")) ("MCC" . ("𝓒")) ("MCD" . ("𝓓")) ("MCE" . ("𝓔")) ("MCF" . ("𝓕")) ("MCG" . ("𝓖")) ("MCH" . ("𝓗")) ("MCI" . ("𝓘")) ("MCJ" . ("𝓙")) ("MCK" . ("𝓚")) ("MCL" . ("𝓛")) ("MCM" . ("𝓜")) ("MCN" . ("𝓝")) ("MCO" . ("𝓞")) ("MCP" . ("𝓟")) ("MCQ" . ("𝓠")) ("MCR" . ("𝓡")) ("MCS" . ("𝓢")) ("MCT" . ("𝓣")) ("MCU" . ("𝓤")) ("MCV" . ("𝓥")) ("MCW" . ("𝓦")) ("MCX" . ("𝓧")) ("MCY" . ("𝓨")) ("MCZ" . ("𝓩")) ("MCa" . ("𝓪")) ("MCb" . ("𝓫")) ("MCc" . ("𝓬")) ("MCd" . ("𝓭")) ("MCe" . ("𝓮")) ("MCf" . ("𝓯")) ("MCg" . ("𝓰")) ("MCh" . ("𝓱")) ("MCi" . ("𝓲")) ("MCj" . ("𝓳")) ("MCk" . ("𝓴")) ("MCl" . ("𝓵")) ("MCm" . ("𝓶")) ("MCn" . ("𝓷")) ("MCo" . ("𝓸")) ("MCp" . ("𝓹")) ("MCq" . ("𝓺")) ("MCr" . ("𝓻")) ("MCs" . ("𝓼")) ("MCt" . ("𝓽")) ("MCu" . ("𝓾")) ("MCv" . ("𝓿")) ("MCw" . ("𝔀")) ("MCx" . ("𝔁")) ("MCy" . ("𝔂")) ("MCz" . ("𝔃")) ("MfA" . ("𝔄")) ("MfB" . ("𝔅")) ("MfD" . ("𝔇")) ("MfE" . ("𝔈")) ("MfF" . ("𝔉")) ("MfG" . ("𝔊")) ("MfJ" . ("𝔍")) ("MfK" . ("𝔎")) ("MfL" . ("𝔏")) ("MfM" . ("𝔐")) ("MfN" . ("𝔑")) ("MfO" . ("𝔒")) ("MfP" . ("𝔓")) ("MfQ" . ("𝔔")) ("MfS" . ("𝔖")) ("MfT" . ("𝔗")) ("MfU" . ("𝔘")) ("MfV" . ("𝔙")) ("MfW" . ("𝔚")) ("MfX" . ("𝔛")) ("MfY" . ("𝔜")) ("Mfa" . ("𝔞")) ("Mfb" . ("𝔟")) ("Mfc" . ("𝔠")) ("Mfd" . ("𝔡")) ("Mfe" . ("𝔢")) ("Mff" . ("𝔣")) ("Mfg" . ("𝔤")) ("Mfh" . ("𝔥")) ("Mfi" . ("𝔦")) ("Mfj" . ("𝔧")) ("Mfk" . ("𝔨")) ("Mfl" . ("𝔩")) ("Mfm" . ("𝔪")) ("Mfn" . ("𝔫")) ("Mfo" . ("𝔬")) ("Mfp" . ("𝔭")) ("Mfq" . ("𝔮")) ("Mfr" . ("𝔯")) ("Mfs" . ("𝔰")) ("Mft" . ("𝔱")) ("Mfu" . ("𝔲")) ("Mfv" . ("𝔳")) ("Mfw" . ("𝔴")) ("Mfx" . ("𝔵")) ("Mfy" . ("𝔶")) ("Mfz" . ("𝔷")) ;; (Sub / Super) scripts ("_a" . ("ₐ")) ("_e" . ("ₑ")) ("_h" . ("ₕ")) ("_i" . ("ᵢ")) ("_j" . ("ⱼ")) ("_k" . ("ₖ")) ("_l" . ("ₗ")) ("_m" . ("ₘ")) ("_n" . ("ₙ")) ("_o" . ("ₒ")) ("_p" . ("ₚ")) ("_r" . ("ᵣ")) ("_s" . ("ₛ")) ("_t" . ("ₜ")) ("_u" . ("ᵤ")) ("_v" . ("ᵥ")) ("_x" . ("ₓ")) ("^a" . ("ᵃ")) ("^b" . ("ᵇ")) ("^c" . ("ᶜ")) ("^d" . ("ᵈ")) ("^e" . ("ᵉ")) ("^f" . ("ᶠ")) ("^g" . ("ᵍ")) ("^h" . ("ʰ")) ("^i" . ("ⁱ")) ("^j" . ("ʲ")) ("^k" . ("ᵏ")) ("^l" . ("ˡ")) ("^m" . ("ᵐ")) ("^n" . ("ⁿ")) ("^o" . ("ᵒ")) ("^p" . ("ᵖ")) ("^r" . ("ʳ")) ("^s" . ("ˢ")) ("^t" . ("ᵗ")) ("^u" . ("ᵘ")) ("^v" . ("ᵛ")) ("^w" . ("ʷ")) ("^x" . ("ˣ")) ("^y" . ("ʸ")) ("^z" . ("ᶻ")) ("^A" . ("ᴬ")) ("^B" . ("ᴮ")) ("^D" . ("ᴰ")) ("^E" . ("ᴱ")) ("^G" . ("ᴳ")) ("^H" . ("ᴴ")) ("^I" . ("ᴵ")) ("^J" . ("ᴶ")) ("^K" . ("ᴷ")) ("^L" . ("ᴸ")) ("^M" . ("ᴹ")) ("^N" . ("ᴺ")) ("^O" . ("ᴼ")) ("^P" . ("ᴾ")) ("^R" . ("ᴿ")) ("^T" . ("ᵀ")) ("^U" . ("ᵁ")) ("^V" . ("ⱽ")) ("^W" . ("ᵂ")) ;; Some ISO8859-1 characters. (" " . (" ")) ("!" . ("¡")) ("cent" . ("¢")) ("brokenbar" . ("¦")) ("degree" . ("°")) ("?" . ("¿")) ("^a_" . ("ª")) ("^o_" . ("º")) ;; Circled, parenthesised etc. numbers and letters. ( "(0)" . ,(agda-input-to-string-list " ⓪")) ( "(1)" . ,(agda-input-to-string-list "⑴①⒈❶➀➊")) ( "(2)" . ,(agda-input-to-string-list "⑵②⒉❷➁➋")) ( "(3)" . ,(agda-input-to-string-list "⑶③⒊❸➂➌")) ( "(4)" . ,(agda-input-to-string-list "⑷④⒋❹➃➍")) ( "(5)" . ,(agda-input-to-string-list "⑸⑤⒌❺➄➎")) ( "(6)" . ,(agda-input-to-string-list "⑹⑥⒍❻➅➏")) ( "(7)" . ,(agda-input-to-string-list "⑺⑦⒎❼➆➐")) ( "(8)" . ,(agda-input-to-string-list "⑻⑧⒏❽➇➑")) ( "(9)" . ,(agda-input-to-string-list "⑼⑨⒐❾➈➒")) ("(10)" . ,(agda-input-to-string-list "⑽⑩⒑❿➉➓")) ("(11)" . ,(agda-input-to-string-list "⑾⑪⒒")) ("(12)" . ,(agda-input-to-string-list "⑿⑫⒓")) ("(13)" . ,(agda-input-to-string-list "⒀⑬⒔")) ("(14)" . ,(agda-input-to-string-list "⒁⑭⒕")) ("(15)" . ,(agda-input-to-string-list "⒂⑮⒖")) ("(16)" . ,(agda-input-to-string-list "⒃⑯⒗")) ("(17)" . ,(agda-input-to-string-list "⒄⑰⒘")) ("(18)" . ,(agda-input-to-string-list "⒅⑱⒙")) ("(19)" . ,(agda-input-to-string-list "⒆⑲⒚")) ("(20)" . ,(agda-input-to-string-list "⒇⑳⒛")) ("(a)" . ,(agda-input-to-string-list "⒜Ⓐⓐ")) ("(b)" . ,(agda-input-to-string-list "⒝Ⓑⓑ")) ("(c)" . ,(agda-input-to-string-list "⒞Ⓒⓒ")) ("(d)" . ,(agda-input-to-string-list "⒟Ⓓⓓ")) ("(e)" . ,(agda-input-to-string-list "⒠Ⓔⓔ")) ("(f)" . ,(agda-input-to-string-list "⒡Ⓕⓕ")) ("(g)" . ,(agda-input-to-string-list "⒢Ⓖⓖ")) ("(h)" . ,(agda-input-to-string-list "⒣Ⓗⓗ")) ("(i)" . ,(agda-input-to-string-list "⒤Ⓘⓘ")) ("(j)" . ,(agda-input-to-string-list "⒥Ⓙⓙ")) ("(k)" . ,(agda-input-to-string-list "⒦Ⓚⓚ")) ("(l)" . ,(agda-input-to-string-list "⒧Ⓛⓛ")) ("(m)" . ,(agda-input-to-string-list "⒨Ⓜⓜ")) ("(n)" . ,(agda-input-to-string-list "⒩Ⓝⓝ")) ("(o)" . ,(agda-input-to-string-list "⒪Ⓞⓞ")) ("(p)" . ,(agda-input-to-string-list "⒫Ⓟⓟ")) ("(q)" . ,(agda-input-to-string-list "⒬Ⓠⓠ")) ("(r)" . ,(agda-input-to-string-list "⒭Ⓡⓡ")) ("(s)" . ,(agda-input-to-string-list "⒮Ⓢⓢ")) ("(t)" . ,(agda-input-to-string-list "⒯Ⓣⓣ")) ("(u)" . ,(agda-input-to-string-list "⒰Ⓤⓤ")) ("(v)" . ,(agda-input-to-string-list "⒱Ⓥⓥ")) ("(w)" . ,(agda-input-to-string-list "⒲Ⓦⓦ")) ("(x)" . ,(agda-input-to-string-list "⒳Ⓧⓧ")) ("(y)" . ,(agda-input-to-string-list "⒴Ⓨⓨ")) ("(z)" . ,(agda-input-to-string-list "⒵Ⓩⓩ")) )) "A list of translations specific to the Agda input method. Each element is a pair (KEY-SEQUENCE-STRING . LIST-OF-TRANSLATION-STRINGS). All the translation strings are possible translations of the given key sequence; if there is more than one you can choose between them using the arrow keys. Note that if you customize this setting you will not automatically benefit (or suffer) from modifications to its default value when the library is updated. If you just want to add some bindings it is probably a better idea to customize `agda-input-user-translations'. These translation pairs are included after those in `agda-input-user-translations', but before the ones inherited from other input methods (see `agda-input-inherit'). If you change this setting manually (without using the customization buffer) you need to call `agda-input-setup' in order for the change to take effect." :group 'agda-input :set 'agda-input-incorporate-changed-setting :initialize 'custom-initialize-default :type '(repeat (cons (string :tag "Key sequence") (repeat :tag "Translations" string)))) (defcustom agda-input-user-translations nil "Like `agda-input-translations', but more suitable for user customizations since by default it is empty. These translation pairs are included first, before those in `agda-input-translations' and the ones inherited from other input methods." :group 'agda-input :set 'agda-input-incorporate-changed-setting :initialize 'custom-initialize-default :type '(repeat (cons (string :tag "Key sequence") (repeat :tag "Translations" string)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Inspecting and modifying translation maps (defun agda-input-get-translations (qp) "Return a list containing all translations from the Quail package QP (except for those corresponding to ASCII). Each pair in the list has the form (KEY-SEQUENCE . TRANSLATION)." (with-temp-buffer (activate-input-method qp) ; To make sure that the package is loaded. (unless (quail-package qp) (error "%s is not a Quail package." qp)) (let ((decode-map (list 'decode-map))) (quail-build-decode-map (list (quail-map)) "" decode-map 0) (cdr decode-map)))) (defun agda-input-show-translations (qp) "Display all translations used by the Quail package QP (a string). \(Except for those corresponding to ASCII)." (interactive (list (read-input-method-name "Quail input method (default %s): " "Agda"))) (let ((buf (concat "*" qp " input method translations*"))) (with-output-to-temp-buffer buf (with-current-buffer buf (quail-insert-decode-map (cons 'decode-map (agda-input-get-translations qp))))))) (defun agda-input-add-translations (trans) "Add the given translations TRANS to the Agda input method. TRANS is a list of pairs (KEY-SEQUENCE . TRANSLATION). The translations are appended to the current translations." (with-temp-buffer (dolist (tr (agda-input-concat-map (eval agda-input-tweak-all) trans)) (quail-defrule (car tr) (cdr tr) "Agda" t)))) (defun agda-input-inherit-package (qp &optional fun) "Let the Agda input method inherit the translations from the Quail package QP (except for those corresponding to ASCII). The optional function FUN can be used to modify the translations. It is given a pair (KEY-SEQUENCE . TRANSLATION) and should return a list of such pairs." (let ((trans (agda-input-get-translations qp))) (agda-input-add-translations (if fun (agda-input-concat-map fun trans) trans)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Setting up the input method (defun agda-input-setup () "Set up the Agda input method based on the customisable variables and underlying input methods." ;; Create (or reset) the input method. (with-temp-buffer (quail-define-package "Agda" "UTF-8" "∏" t ; guidance "Agda input method. The purpose of this input method is to edit Agda programs, but since it is highly customisable it can be made useful for other tasks as well." nil nil nil nil nil nil t ; maximum-shortest )) (agda-input-add-translations (mapcar (lambda (tr) (cons (car tr) (vconcat (cdr tr)))) (append agda-input-user-translations agda-input-translations))) (dolist (def agda-input-inherit) (agda-input-inherit-package (car def) (eval (cdr def))))) (defun agda-input-incorporate-changed-setting (sym val) "Update the Agda input method based on the customisable variables and underlying input methods. Suitable for use in the :set field of `defcustom'." (set-default sym val) (agda-input-setup)) ;; Set up the input method. (agda-input-setup) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Administrative details (provide 'agda-input) ;;; agda-input.el ends here Agda-2.5.3/src/data/emacs-mode/agda2-mode-pkg.el0000644000000000000000000000020013154613124017246 0ustar0000000000000000(define-package "agda2-mode" "2.5.3" "interactive development for Agda, a dependently typed functional programming language") Agda-2.5.3/src/data/emacs-mode/annotation.el0000644000000000000000000002344013154613124016754 0ustar0000000000000000;;; annotation.el --- Functions for annotating text with faces and help bubbles ;;; Commentary: ;; ;;; Code: (require 'cl) (defconst annotations-offset (- (save-restriction (widen) (point-min)) 1) "Offset between buffer positions and annotations's positions. Annotations's positions are based on 1, so this adjusts it to the base position used by your Emacs.") (defvar annotation-bindings nil "An association list mapping symbols to faces.") (make-variable-buffer-local 'annotation-bindings) (defvar annotation-goto-stack nil "Positions from which `annotation-goto' was invoked.") (defun annotation-goto-indirect (link &optional other-window) "Follow the `annotation-goto' hyperlink pointed to by LINK, if any. LINK should be a buffer position, or an event object (in which case the ending position is used). If the hyperlink exists and the jump is performed successfully, then `t' is returned, and otherwise `nil' (unless an error is raised). If OTHER-WINDOW is non-nil, then another window is used to display the target position." (let (source-pos source-window source-buffer source-file-name target) (cond ((eventp link) (let ((pn (event-end link))) (when (not (posn-area pn)) (setq source-pos (posn-point pn)) (setq source-window (posn-window pn)) (setq source-buffer (window-buffer source-window))))) ((integerp link) (setq source-pos link) (setq source-window (selected-window)) (setq source-buffer (current-buffer))) (t (error "Not an integer or event object: %S" link))) (when (and source-pos source-buffer) (with-current-buffer source-buffer (setq target (get-text-property source-pos 'annotation-goto))) (when target (unless (equal source-window (selected-window)) (select-window source-window)) (annotation-goto-and-push source-buffer source-pos target other-window))))) (defun annotation-go-back nil "Go back to the previous position. The previous position in which `annotation-goto-and-push' was successfully invoked." (when annotation-goto-stack (let ((pos (pop annotation-goto-stack))) (annotation-goto pos)))) (defun annotation-goto-and-push (source-buffer source-pos filepos &optional other-window) "Like `annotation-goto', but pushes a position when successful. The position consists of the file visited by SOURCE-BUFFER, and the position given by SOURCE-POS." (let (source-file-name) (with-current-buffer source-buffer (setq source-file-name buffer-file-name)) (when (annotation-goto filepos other-window) (unless (and (equal source-buffer (current-buffer)) (eq source-pos (point))) (push `(,source-file-name . ,source-pos) annotation-goto-stack)) t))) (defun annotation-goto (filepos &optional other-window) "Go to file position FILEPOS if the file is readable. FILEPOS should have the form (FILE . POS). Return t if successful. If OTHER-WINDOW is non-nil, use another window to display the given position." (when (consp filepos) (let ((file (car filepos))) (if (file-readable-p file) (progn (if other-window (find-file-other-window file) (find-file file)) (annotation-goto-position (cdr filepos)) t) (error "File does not exist or is unreadable: %s." file))))) (defun annotation-goto-position (position) "Move point to POSITION." (goto-char (+ position annotations-offset))) (defun annotation-append-text-property (start end prop values) "Merges VALUES to text property PROP between START and END." (let ((pos start) mid) (while (< pos end) (setq mid (next-single-property-change pos prop nil end)) (let* ((old-values (get-text-property pos prop)) (all-values (union old-values values))) (put-text-property pos mid prop all-values) (setq pos mid))))) (defun annotation-annotate (start end anns &optional info goto) "Annotate text between START and END in the current buffer. Nothing happens if either START or END are out of bounds for the current (possibly narrowed) buffer, or END <= START. If ANNS is nil, then those text properties between START and END that have been set by this function are deleted. Otherwise the following happens. All the symbols in ANNS are looked up in `annotation-bindings', and the font-lock-face text property for the given character range is set to the resulting list of faces. If the string INFO is non-nil, the mouse-face property is set to highlight, and INFO is used as the help-echo string. If GOTO has the form (FILENAME . POSITION), then the mouse-face property is set to highlight, and the given filename/position will be used by `annotation-goto-indirect' when it is invoked with a position in the given range. Note that if a given attribute is defined by several faces, then the first face's setting takes precedence. All characters whose text properties get set also have the annotation-annotated property set to t, and annotation-annotations is set to a list with all the properties that have been set; this ensures that the text properties can later be removed (if the annotation-* properties are not tampered with)." (incf start annotations-offset) (incf end annotations-offset) (when (and (<= (point-min) start) (< start end) (<= end (point-max))) (if (null anns) (annotation-remove-annotations start end) (let ((faces (delq nil (mapcar (lambda (ann) (cdr (assoc ann annotation-bindings))) anns))) (props nil)) (when faces (annotation-append-text-property start end 'font-lock-face faces) (add-to-list 'props 'font-lock-face)) (when (consp goto) (add-text-properties start end `(annotation-goto ,goto mouse-face highlight)) (add-to-list 'props 'annotation-goto) (add-to-list 'props 'mouse-face)) (when info (add-text-properties start end `(mouse-face highlight help-echo ,info)) (add-to-list 'props 'mouse-face) (add-to-list 'props 'help-echo)) (when props (let ((pos start) mid) (while (< pos end) (setq mid (next-single-property-change pos 'annotation-annotations nil end)) (let* ((old-props (get-text-property pos 'annotation-annotations)) (all-props (union old-props props))) (add-text-properties pos mid `(annotation-annotated t annotation-annotations ,all-props)) (setq pos mid))))))))) (defmacro annotation-preserve-mod-p-and-undo (&rest code) "Run CODE preserving both the undo data and the modification bit. Modification hooks are also disabled." (let ((modp (make-symbol "modp"))) `(let ((,modp (buffer-modified-p)) ;; Don't check if the file is being modified by some other process. (buffer-file-name nil) ;; Don't record those changes on the undo-log. (buffer-undo-list t) ;; Don't run modification hooks. (inhibit-modification-hooks t)) (unwind-protect (progn ,@code) (restore-buffer-modified-p ,modp))))) (defun annotation-remove-annotations (&optional start end) "Remove all text properties set by `annotation-annotate'. In the current buffer. If START and END are given, then properties are only removed between these positions. This function preserves the file modification stamp of the current buffer, does not modify the undo list, and temporarily disables all modification hooks. Note: This function may fail if there is read-only text in the buffer." ;; remove-text-properties fails for read-only text. (annotation-preserve-mod-p-and-undo (let ((pos (or start (point-min))) pos2) (while pos (setq pos2 (next-single-property-change pos 'annotation-annotated nil end)) (let ((props (get-text-property pos 'annotation-annotations))) (when props (remove-text-properties pos (or pos2 (point-max)) (mapcan (lambda (prop) (list prop nil)) (append '(annotation-annotated annotation-annotations) props))))) (setq pos (unless (equal pos2 end) pos2)))))) (defun annotation-load (goto-help &rest cmds) "Apply highlighting annotations in CMDS in the current buffer. The argument CMDS should be a list of lists (start end anns &optional info goto). Text between start and end will be annotated with the annotations in the list anns (using `annotation-annotate'). If info and/or goto are present they will be used as the corresponding arguments to `annotation-annotate'. If INFO is nil in a call to `annotation-annotate', and the GOTO argument is a cons-cell, then the INFO argument is set to GOTO-HELP. The intention is that the default help text should inform the user about the \"goto\" facility. This function preserves the file modification stamp of the current buffer, does not modify the undo list, and temporarily disables all modification hooks. Note: This function may fail if there is read-only text in the buffer." (annotation-preserve-mod-p-and-undo (when (listp cmds) (dolist (cmd cmds) (destructuring-bind (start end anns &optional info goto) cmd (let ((info (if (and (not info) (consp goto)) goto-help info))) (annotation-annotate start end anns info goto))))))) (provide 'annotation) ;;; annotation.el ends here Agda-2.5.3/src/data/emacs-mode/eri.el0000644000000000000000000001543513154613124015366 0ustar0000000000000000;;; eri.el --- Enhanced relative indentation (eri) ;;; Commentary: ;;; Code: (require 'cl) (defun eri-current-line-length nil "Calculate length of current line." (- (line-end-position) (line-beginning-position))) (defun eri-current-line-empty nil "Return non-nil if the current line is empty (not counting white space)." (equal (current-indentation) (eri-current-line-length))) (defun eri-maximum (xs) "Calculate maximum element in XS. Returns nil if the list is empty." (if xs (apply 'max xs))) (defun eri-take (n xs) "Return the first N elements of XS." (butlast xs (- (length xs) n))) (defun eri-split (x xs) "Return a pair of lists (XS1 . XS2). If XS is sorted, then XS = (append XS1 XS2), and all elements in XS1 are <= X, whereas all elements in XS2 are > X." (let* ((pos (or (position-if (lambda (y) (> y x)) xs) (length xs))) (xs1 (eri-take pos xs)) (xs2 (nthcdr pos xs))) (cons xs1 xs2))) (defun eri-calculate-indentation-points-on-line (max) "Calculate indentation points on current line. Only points left of column number MAX are included. If MAX is nil, then all points are included. Points are returned in ascending order. Example (positions marked with ^ are returned): f x y = g 3 (Just y) 5 4 ^ ^ ^ ^ ^ ^ ^ ^ | | MAX" (let ((result)) (save-excursion (save-restriction (beginning-of-line) ; To make \\` work in the regexp below: (narrow-to-region (line-beginning-position) (line-end-position)) (while (progn (let ((pos (and (search-forward-regexp "\\(?:\\s-\\|\\`\\)\\(\\S-\\)" nil t) (match-beginning 1)))) (when (not (null pos)) (let ((pos1 (- pos (line-beginning-position)))) (when (or (null max) (< pos1 max)) (add-to-list 'result pos1)))) (and pos (< (point) (line-end-position)) (or (null max) (< (current-column) max)))))) (nreverse result) ; Destructive operation. )))) (defun eri-new-indentation-points () "Calculate new indentation points. Returns a singleton list containing the column number two steps in from the indentation of the first non-empty line (white space excluded) above the current line. If there is no such line, then the empty list is returned." (let ((start (line-beginning-position))) (save-excursion ; Find a non-empty line above the current one, if any. (while (progn (forward-line -1) (not (or (bobp) (not (eri-current-line-empty)))))) (if (or (equal (point) start) (eri-current-line-empty)) nil (list (+ 2 (current-indentation))))))) (defun eri-calculate-indentation-points (reverse) "Calculate points used to indent the current line. The points are given in reverse order if REVERSE is non-nil. See `eri-indent' for a description of how the indentation points are calculated; note that the current indentation is not included in the returned list." ;; First find a bunch of indentations used above the current line. (let ((points) (max) (start (line-beginning-position))) (save-excursion (while (progn (forward-line -1) ; Skip the line we started from and lines with nothing but ; white space. (unless (or (equal (point) start) (eri-current-line-empty)) (setq points (append (eri-calculate-indentation-points-on-line max) points)) (setq max (car points))) ;; Stop after hitting the beginning of the buffer or a ;; non-empty, non-indented line. (not (or (bobp) (and (equal (current-indentation) 0) (> (eri-current-line-length) 0))))))) ;; Add new indentation points, but remove the current indentation. ;; Sort the indentations. Rearrange the points so that the next ;; point is the one after the current one. Reverse if necessary. ;; ;; Note: sort and nreverse are destructive. (let* ((ps0 (remove (current-indentation) (append (eri-new-indentation-points) points))) (ps1 (eri-split (current-indentation) (sort ps0 '<))) (ps2 (append (cdr ps1) (car ps1)))) (if reverse (nreverse ps2) ps2)))) (defun eri-indent (&optional reverse) "Cycle between some possible indentation points. With prefix argument REVERSE, cycle in reverse order. Assume that a file contains the following lines of code, with point on the line with three dots: frob = loooooooooooooooooooooooooong identifier foo = f a b where f (Foo x) y = let bar = x baz = 3 + 5 ... ^ ^ ^ ^ ^ ^ ^ ^ ^ * ^ ^ ^ ^ Then the ^'s and the * mark the indentation points that this function cycles through. The indentation points are selected as follows: * All lines before the current one, up to and including the first non-indented line (or the beginning of the buffer) are considered. foo = f a b where f (Foo x) y = let bar = x baz = 3 + 5 * On these lines, erase all characters that stand to the right of some non-white space character on a lower line. foo whe f (Foo x) y = let b baz = 3 + 5 * Also erase all characters not immediately preceded by white space. f w f ( x y = l b b = 3 + 5 * The columns of all remaining characters are indentation points. f w f ( x y = l b = 3 + 5 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ * A new indentation point is also added, two steps in from the indentation of the first non-empty line (white space excluded) above the current line (if there is such a line). f w f ( x y = l b = 3 + 5 ^ ^ ^ ^ ^ ^ ^ ^ ^ * ^ ^ ^ ^" (interactive "P") (let* ((points (eri-calculate-indentation-points reverse)) (remaining-points (cdr (member (current-indentation) points))) (indentation (if remaining-points (car remaining-points) (car points)))) (when indentation (save-excursion (indent-line-to indentation)) (if (< (current-column) indentation) (indent-line-to indentation))))) (defun eri-indent-reverse nil "Cycle between some possible indentation points (in reverse order). See `eri-indent' for a description of how the indentation points are calculated." (interactive) (eri-indent t)) (provide 'eri) ;;; eri.el ends here Agda-2.5.3/src/data/emacs-mode/agda2-highlight.el0000644000000000000000000004646113154613124017535 0ustar0000000000000000;;; agda2-highlight.el --- Syntax highlighting for Agda (version ≥ 2) ;;; Commentary: ;; Code to apply syntactic highlighting to Agda source code. This uses ;; Agda's own annotations to figure out what is what, so the parsing ;; is always done correctly, but highlighting is not done on the fly. ;;; Code: (require 'annotation) (require 'font-lock) (defgroup agda2-highlight nil "Syntax highlighting for Agda." :group 'agda2) (defcustom agda2-highlight-level 'non-interactive "How much syntax highlighting should be produced? Interactive highlighting includes highlighting of the expression that is currently being type-checked." :type '(choice (const :tag "None" none) (const :tag "Non-interactive" non-interactive) (const :tag "Interactive" interactive)) :group 'agda2-highlight) (defun agda2-highlight-level nil "Formats the highlighting level in a Haskelly way." (cond ((equal agda2-highlight-level 'none) "None") ((equal agda2-highlight-level 'non-interactive) "NonInteractive") ((equal agda2-highlight-level 'interactive) "Interactive") (t "None"))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Functions for setting faces (defun agda2-highlight-set-face-attribute (face attrs) "Reset (globally) all attributes of the face FACE according to ATTRS. If the face does not exist, then it is created first." (make-face face) (set-face-attribute face nil :family 'unspecified :width 'unspecified :height 'unspecified :weight 'unspecified :slant 'unspecified :foreground 'unspecified :background 'unspecified :inverse-video 'unspecified :stipple 'unspecified :underline 'unspecified :overline 'unspecified :strike-through 'unspecified :inherit 'unspecified :box 'unspecified :font 'unspecified) (eval `(set-face-attribute face nil ,@attrs))) (defvar agda2-highlight-face-attributes-list '(:family :width :height :weight :slant :foreground :background :inverse-video :stipple :underline :overline :strike-through :inherit :box :font) "The attributes considered by `agda2-highlight-face-attributes'.") (defun agda2-highlight-face-attributes (face) "The names and values of all attributes in FACE. Only the attributes in `agda2-highlight-face-attributes-list' are considered. The attributes are returned in a flat list of the form (name1 value1 name2 value2...)." (apply 'append (mapcar (lambda (attr) (let ((val (face-attribute face attr))) (if (member val '(unspecified nil)) '() (list attr (if (symbolp val) `',val val))))) agda2-highlight-face-attributes-list))) (defun agda2-highlight-set-faces (variable group) "Set all Agda faces according to the value of GROUP. Also sets the default value of VARIABLE to GROUP." (set-default variable group) (mapc (lambda (face-and-attrs) (agda2-highlight-set-face-attribute (car face-and-attrs) (cdr face-and-attrs))) (cond ((equal group 'conor) '((agda2-highlight-keyword-face :bold t) (agda2-highlight-string-face :foreground "firebrick3") (agda2-highlight-number-face :foreground "firebrick3") (agda2-highlight-symbol-face :foreground "grey25") (agda2-highlight-primitive-type-face :foreground "medium blue") (agda2-highlight-bound-variable-face :foreground "purple") (agda2-highlight-inductive-constructor-face :foreground "firebrick3") (agda2-highlight-coinductive-constructor-face :foreground "firebrick3") (agda2-highlight-datatype-face :foreground "medium blue") (agda2-highlight-field-face :foreground "deeppink") (agda2-highlight-function-face :foreground "darkgreen") (agda2-highlight-module-face :foreground "medium blue") (agda2-highlight-postulate-face :foreground "darkgreen") (agda2-highlight-primitive-face :foreground "darkgreen") (agda2-highlight-macro-face :foreground "aquamarine4") (agda2-highlight-record-face :foreground "medium blue") (agda2-highlight-dotted-face) (agda2-highlight-error-face :foreground "red" :underline t) (agda2-highlight-unsolved-meta-face :foreground "black" :background "yellow") (agda2-highlight-unsolved-constraint-face :foreground "black" :background "yellow") (agda2-highlight-termination-problem-face :foreground "black" :background "light salmon") (agda2-highlight-positivity-problem-face :foreground "black" :background "peru") (agda2-highlight-incomplete-pattern-face :foreground "black" :background "purple") (agda2-highlight-typechecks-face :foreground "black" :background "light blue"))) ((equal group 'default-faces) (list (cons 'agda2-highlight-keyword-face (agda2-highlight-face-attributes font-lock-keyword-face)) (cons 'agda2-highlight-string-face (agda2-highlight-face-attributes font-lock-string-face)) (cons 'agda2-highlight-number-face (agda2-highlight-face-attributes font-lock-constant-face)) (cons 'agda2-highlight-symbol-face (agda2-highlight-face-attributes font-lock-keyword-face)) (cons 'agda2-highlight-primitive-type-face (agda2-highlight-face-attributes font-lock-keyword-face)) (cons 'agda2-highlight-bound-variable-face (agda2-highlight-face-attributes font-lock-variable-name-face)) (cons 'agda2-highlight-inductive-constructor-face (agda2-highlight-face-attributes font-lock-type-face)) (cons 'agda2-highlight-coinductive-constructor-face (agda2-highlight-face-attributes font-lock-type-face)) (cons 'agda2-highlight-datatype-face (agda2-highlight-face-attributes font-lock-type-face)) (cons 'agda2-highlight-field-face (agda2-highlight-face-attributes font-lock-variable-name-face)) (cons 'agda2-highlight-function-face (agda2-highlight-face-attributes font-lock-function-name-face)) (cons 'agda2-highlight-module-face (agda2-highlight-face-attributes font-lock-type-face)) (cons 'agda2-highlight-postulate-face (agda2-highlight-face-attributes font-lock-type-face)) (cons 'agda2-highlight-primitive-face (agda2-highlight-face-attributes font-lock-constant-face)) (cons 'agda2-highlight-macro-face (agda2-highlight-face-attributes font-lock-function-name-face)) (cons 'agda2-highlight-record-face (agda2-highlight-face-attributes font-lock-variable-name-face)) (cons 'agda2-highlight-dotted-face (agda2-highlight-face-attributes font-lock-variable-name-face)) (cons 'agda2-highlight-operator-face (agda2-highlight-face-attributes font-lock-function-name-face)) (cons 'agda2-highlight-error-face (agda2-highlight-face-attributes font-lock-warning-face)) (cons 'agda2-highlight-typechecks-face (agda2-highlight-face-attributes font-lock-type-face)) (cons 'agda2-highlight-typechecking-face (agda2-highlight-face-attributes font-lock-preprocessor-face))))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Faces (defcustom agda2-highlight-face-groups nil "Colour scheme used in Agda buffers. Changes to this variable may not take full effect until you have restarted Emacs. Note also that if you are using the default-faces option and change your colour theme, then the changes may not take effect in Agda buffers until you have restarted Emacs." :type '(choice (const :tag "Use the settings in the \"Agda2 Highlight Faces\" subgroup." nil) (const :tag "Use an approximation of Conor McBride's colour scheme." conor) (const :tag "Use simplified highlighting and default font-lock faces." default-faces)) :group 'agda2-highlight :set 'agda2-highlight-set-faces) (defgroup agda2-highlight-faces nil "Faces used to highlight Agda code. If `agda2-highlight-face-groups' is nil." :group 'agda2-highlight) (defface agda2-highlight-keyword-face '((t (:foreground "DarkOrange3"))) "The face used for keywords." :group 'agda2-highlight-faces) (defface agda2-highlight-string-face '((t (:foreground "firebrick"))) "The face used for strings." :group 'agda2-highlight-faces) (defface agda2-highlight-number-face '((t (:foreground "purple"))) "The face used for numbers." :group 'agda2-highlight-faces) (defface agda2-highlight-symbol-face '((((background light)) (:foreground "gray25")) (((background dark)) (:foreground "gray75"))) "The face used for symbols like forall, =, ->, etc." :group 'agda2-highlight-faces) (defface agda2-highlight-primitive-type-face '((t (:foreground "medium blue"))) "The face used for primitive types (like Set and Prop)." :group 'agda2-highlight-faces) (defface agda2-highlight-bound-variable-face '((t nil)) "The face used for bound variables." :group 'agda2-highlight-faces) (defface agda2-highlight-inductive-constructor-face '((t (:foreground "green4"))) "The face used for inductive constructors." :group 'agda2-highlight-faces) (defface agda2-highlight-coinductive-constructor-face '((t (:foreground "gold4"))) "The face used for coinductive constructors." :group 'agda2-highlight-faces) (defface agda2-highlight-datatype-face '((t (:foreground "medium blue"))) "The face used for datatypes." :group 'agda2-highlight-faces) (defface agda2-highlight-field-face '((t (:foreground "DeepPink2"))) "The face used for record fields." :group 'agda2-highlight-faces) (defface agda2-highlight-function-face '((t (:foreground "medium blue"))) "The face used for functions." :group 'agda2-highlight-faces) (defface agda2-highlight-module-face '((t (:foreground "purple"))) "The face used for module names." :group 'agda2-highlight-faces) (defface agda2-highlight-postulate-face '((t (:foreground "medium blue"))) "The face used for postulates." :group 'agda2-highlight-faces) (defface agda2-highlight-primitive-face '((t (:foreground "medium blue"))) "The face used for primitive functions." :group 'agda2-highlight-faces) (defface agda2-highlight-macro-face '((t (:foreground "aquamarine4"))) "The face used for macros." :group 'agda2-highlight-faces) (defface agda2-highlight-record-face '((t (:foreground "medium blue"))) "The face used for record types." :group 'agda2-highlight-faces) (defface agda2-highlight-dotted-face '((t nil)) "The face used for dotted patterns." :group 'agda2-highlight-faces) (defface agda2-highlight-operator-face '((t nil)) "The face used for operators." :group 'agda2-highlight-faces) (defface agda2-highlight-error-face '((t (:foreground "red" :underline t))) "The face used for errors." :group 'agda2-highlight-faces) (defface agda2-highlight-unsolved-meta-face '((t (:background "yellow"))) "The face used for unsolved meta variables." :group 'agda2-highlight-faces) (defface agda2-highlight-unsolved-constraint-face '((t (:background "yellow"))) "The face used for unsolved constraints which are not connected to metas." :group 'agda2-highlight-faces) (defface agda2-highlight-termination-problem-face '((t (:background "light salmon"))) "The face used for termination problems." :group 'agda2-highlight-faces) (defface agda2-highlight-positivity-problem-face '((t (:background "peru"))) "The face used for positivity problems." :group 'agda2-highlight-faces) (defface agda2-highlight-reachability-problem-face '((t (:background "dark gray"))) "The face used for reachability problems." :group 'agda2-highlight-faces) (defface agda2-highlight-coverage-problem-face '((t (:background "wheat"))) "The face used for coverage problems." :group 'agda2-highlight-faces) (defface agda2-highlight-catchall-clause-face '((t (:background "white smoke"))) "The face used for catchall clauses." :group 'agda2-highlight-faces) (defface agda2-highlight-typechecks-face '((t (:background "light blue" :foreground "black"))) "The face used for code which is being type-checked." :group 'agda2-highlight-faces) (defvar agda2-highlight-faces '((keyword . agda2-highlight-keyword-face) (comment . font-lock-comment-face) (string . agda2-highlight-string-face) (number . agda2-highlight-number-face) (symbol . agda2-highlight-symbol-face) (primitivetype . agda2-highlight-primitive-type-face) (bound . agda2-highlight-bound-variable-face) (inductiveconstructor . agda2-highlight-inductive-constructor-face) (coinductiveconstructor . agda2-highlight-coinductive-constructor-face) (datatype . agda2-highlight-datatype-face) (field . agda2-highlight-field-face) (function . agda2-highlight-function-face) (module . agda2-highlight-module-face) (postulate . agda2-highlight-postulate-face) (primitive . agda2-highlight-primitive-face) (macro . agda2-highlight-macro-face) (record . agda2-highlight-record-face) (dotted . agda2-highlight-dotted-face) (operator . agda2-highlight-operator-face) (error . agda2-highlight-error-face) (unsolvedmeta . agda2-highlight-unsolved-meta-face) (unsolvedconstraint . agda2-highlight-unsolved-constraint-face) (terminationproblem . agda2-highlight-termination-problem-face) (reachabilityproblem . agda2-highlight-reachability-problem-face) (coverageproblem . agda2-highlight-coverage-problem-face) (positivityproblem . agda2-highlight-positivity-problem-face) (incompletepattern . agda2-highlight-incomplete-pattern-face) (catchallclause . agda2-highlight-catchall-clause-face) (typechecks . agda2-highlight-typechecks-face)) "Alist mapping code aspects to the face used when displaying them. The aspects currently recognised are the following: `bound' Bound variables. `coinductiveconstructor' Coinductive constructors. `datatype' Data types. `dotted' Dotted patterns. `error' Errors. `field' Record fields. `function' Functions. `incompletepattern' Incomplete patterns. `inductiveconstructor' Inductive constructors. `keyword' Keywords. `module' Module names. `number' Numbers. `operator' Operators. `postulate' Postulates. `primitive' Primitive functions. `primitivetype' Primitive types (like Set and Prop). `macro' Macros. `record' Record types. `string' Strings. `symbol' Symbols like forall, =, ->, etc. `terminationproblem' Termination problems. `positivityproblem' Positivity problems. `reachabilityproblem' Reachability problems. `coverageproblem' Coverage problems. `catchallclause' Clause not holding definitionally. `typechecks' Code which is being type-checked. `unsolvedconstraint' Unsolved constraints, not connected to meta variables. `unsolvedmeta' Unsolved meta variables. `comment' Comments.") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Variables (defvar agda2-highlight-in-progress nil "If nil, then highlighting annotations are not applied.") (make-variable-buffer-local 'agda2-highlight-in-progress) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Functions (defun agda2-highlight-setup nil "Set up the `annotation' library for use with `agda2-mode'." (setq annotation-bindings agda2-highlight-faces)) (defun agda2-highlight-apply (&rest cmds) "Adds the syntax highlighting information in the annotation list CMDS. Old syntax highlighting information is not removed." (let (;; Ignore read-only status, otherwise this function may fail. (inhibit-read-only t)) (apply 'annotation-load "Click mouse-2 to jump to definition" cmds))) (defun agda2-highlight-add-annotations (&rest cmds) "Like `agda2-highlight-apply'. But only if `agda2-highlight-in-progress' is non-nil." (if agda2-highlight-in-progress (apply 'agda2-highlight-apply cmds))) (defun agda2-highlight-load (file) "Load syntax highlighting information from FILE. Old syntax highlighting information is not removed." (let* ((coding-system-for-read 'utf-8) (cmds (with-temp-buffer (insert-file-contents file) (goto-char (point-min)) (read (current-buffer))))) (apply 'agda2-highlight-apply cmds))) (defun agda2-highlight-load-and-delete-action (file) "Like `agda2-highlight-load', but deletes FILE when done. And highlighting is only updated if `agda2-highlight-in-progress' is non-nil." (unwind-protect (if agda2-highlight-in-progress (agda2-highlight-load file)) (delete-file file))) (defun agda2-highlight-clear nil "Remove all syntax highlighting added by `agda2-highlight-reload'." (interactive) (let ((inhibit-read-only t)) ; Ignore read-only status, otherwise this function may fail. (annotation-remove-annotations))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Administrative details (provide 'agda2-highlight) ;;; agda2-highlight.el ends here Agda-2.5.3/src/data/lib/0000755000000000000000000000000013154613124013011 5ustar0000000000000000Agda-2.5.3/src/data/lib/prim/0000755000000000000000000000000013154613124013760 5ustar0000000000000000Agda-2.5.3/src/data/lib/prim/Agda/0000755000000000000000000000000013154613124014614 5ustar0000000000000000Agda-2.5.3/src/data/lib/prim/Agda/Primitive.agda0000644000000000000000000000144513154613124017406 0ustar0000000000000000-- The Agda primitives (preloaded). {-# OPTIONS --without-K #-} module Agda.Primitive where ------------------------------------------------------------------------ -- Universe levels ------------------------------------------------------------------------ infixl 6 _⊔_ -- Level is the first thing we need to define. -- The other postulates can only be checked if built-in Level is known. postulate Level : Set -- MAlonzo compiles Level to (). This should be safe, because it is -- not possible to pattern match on levels. {-# COMPILE GHC Level = type () #-} {-# BUILTIN LEVEL Level #-} postulate lzero : Level lsuc : (ℓ : Level) → Level _⊔_ : (ℓ₁ ℓ₂ : Level) → Level {-# BUILTIN LEVELZERO lzero #-} {-# BUILTIN LEVELSUC lsuc #-} {-# BUILTIN LEVELMAX _⊔_ #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/0000755000000000000000000000000013154613124016222 5ustar0000000000000000Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Equality.agda0000644000000000000000000000030013154613124020626 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Equality where infix 4 _≡_ data _≡_ {a} {A : Set a} (x : A) : A → Set a where instance refl : x ≡ x {-# BUILTIN EQUALITY _≡_ #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Coinduction.agda0000644000000000000000000000047313154613124021322 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Coinduction where infix 1000 ♯_ postulate ∞ : ∀ {a} (A : Set a) → Set a ♯_ : ∀ {a} {A : Set a} → A → ∞ A ♭ : ∀ {a} {A : Set a} → ∞ A → A {-# BUILTIN INFINITY ∞ #-} {-# BUILTIN SHARP ♯_ #-} {-# BUILTIN FLAT ♭ #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/IO.agda0000644000000000000000000000031513154613124017346 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.IO where postulate IO : ∀ {a} → Set a → Set a {-# BUILTIN IO IO #-} {-# FOREIGN GHC type AgdaIO a b = IO b #-} {-# COMPILE GHC IO = type AgdaIO #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Char.agda0000644000000000000000000000070113154613124017713 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Char where open import Agda.Builtin.Nat open import Agda.Builtin.Bool postulate Char : Set {-# BUILTIN CHAR Char #-} primitive primIsLower primIsDigit primIsAlpha primIsSpace primIsAscii primIsLatin1 primIsPrint primIsHexDigit : Char → Bool primToUpper primToLower : Char → Char primCharToNat : Char → Nat primNatToChar : Nat → Char primCharEquality : Char → Char → Bool Agda-2.5.3/src/data/lib/prim/Agda/Builtin/String.agda0000644000000000000000000000172013154613124020306 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.String where open import Agda.Builtin.Bool open import Agda.Builtin.List open import Agda.Builtin.Char postulate String : Set {-# BUILTIN STRING String #-} primitive primStringToList : String → List Char primStringFromList : List Char → String primStringAppend : String → String → String primStringEquality : String → String → Bool primShowChar : Char → String primShowString : String → String {-# COMPILE JS primStringToList = function(x) { return x.split(""); } #-} {-# COMPILE JS primStringFromList = function(x) { return x.join(""); } #-} {-# COMPILE JS primStringAppend = function(x) { return function(y) { return x+y; }; } #-} {-# COMPILE JS primStringEquality = function(x) { return function(y) { return x===y; }; } #-} {-# COMPILE JS primShowChar = function(x) { return JSON.stringify(x); } #-} {-# COMPILE JS primShowString = function(x) { return JSON.stringify(x); } #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Nat.agda0000644000000000000000000000235513154613124017567 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Nat where open import Agda.Builtin.Bool data Nat : Set where zero : Nat suc : (n : Nat) → Nat {-# BUILTIN NATURAL Nat #-} infix 4 _==_ _<_ infixl 6 _+_ _-_ infixl 7 _*_ _+_ : Nat → Nat → Nat zero + m = m suc n + m = suc (n + m) {-# BUILTIN NATPLUS _+_ #-} _-_ : Nat → Nat → Nat n - zero = n zero - suc m = zero suc n - suc m = n - m {-# BUILTIN NATMINUS _-_ #-} _*_ : Nat → Nat → Nat zero * m = zero suc n * m = m + n * m {-# BUILTIN NATTIMES _*_ #-} _==_ : Nat → Nat → Bool zero == zero = true suc n == suc m = n == m _ == _ = false {-# BUILTIN NATEQUALS _==_ #-} _<_ : Nat → Nat → Bool _ < zero = false zero < suc _ = true suc n < suc m = n < m {-# BUILTIN NATLESS _<_ #-} div-helper : Nat → Nat → Nat → Nat → Nat div-helper k m zero j = k div-helper k m (suc n) zero = div-helper (suc k) m n m div-helper k m (suc n) (suc j) = div-helper k m n j {-# BUILTIN NATDIVSUCAUX div-helper #-} mod-helper : Nat → Nat → Nat → Nat → Nat mod-helper k m zero j = k mod-helper k m (suc n) zero = mod-helper 0 m n m mod-helper k m (suc n) (suc j) = mod-helper (suc k) m n j {-# BUILTIN NATMODSUCAUX mod-helper #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Size.agda0000644000000000000000000000040113154613124017745 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Size where {-# BUILTIN SIZEUNIV SizeU #-} {-# BUILTIN SIZE Size #-} {-# BUILTIN SIZELT Size<_ #-} {-# BUILTIN SIZESUC ↑_ #-} {-# BUILTIN SIZEINF ω #-} {-# BUILTIN SIZEMAX _⊔ˢ_ #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/TrustMe.agda0000644000000000000000000000024513154613124020444 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.TrustMe where open import Agda.Builtin.Equality primitive primTrustMe : ∀ {a} {A : Set a} {x y : A} → x ≡ y Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Strict.agda0000644000000000000000000000047413154613124020315 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Strict where open import Agda.Builtin.Equality primitive primForce : ∀ {a b} {A : Set a} {B : A → Set b} (x : A) → (∀ x → B x) → B x primForceLemma : ∀ {a b} {A : Set a} {B : A → Set b} (x : A) (f : ∀ x → B x) → primForce x f ≡ f x Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Bool.agda0000644000000000000000000000063013154613124017732 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Bool where data Bool : Set where false true : Bool {-# BUILTIN BOOL Bool #-} {-# BUILTIN FALSE false #-} {-# BUILTIN TRUE true #-} {-# COMPILE UHC Bool = data __BOOL__ (__FALSE__ | __TRUE__) #-} {-# COMPILE JS Bool = function (x,v) { return ((x)? v["true"]() : v["false"]()); } #-} {-# COMPILE JS false = false #-} {-# COMPILE JS true = true #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/FromNat.agda0000644000000000000000000000060213154613124020404 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.FromNat where open import Agda.Primitive open import Agda.Builtin.Nat record Number {a} (A : Set a) : Set (lsuc a) where field Constraint : Nat → Set a fromNat : ∀ n → {{_ : Constraint n}} → A open Number {{...}} public using (fromNat) {-# BUILTIN FROMNAT fromNat #-} {-# DISPLAY Number.fromNat _ n = fromNat n #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Reflection.agda0000644000000000000000000002335013154613124021135 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Reflection where open import Agda.Builtin.Unit open import Agda.Builtin.Bool open import Agda.Builtin.Nat open import Agda.Builtin.List open import Agda.Builtin.String open import Agda.Builtin.Char open import Agda.Builtin.Float open import Agda.Builtin.Int -- Names -- postulate Name : Set {-# BUILTIN QNAME Name #-} primitive primQNameEquality : Name → Name → Bool primQNameLess : Name → Name → Bool primShowQName : Name → String -- Fixity -- data Associativity : Set where left-assoc : Associativity right-assoc : Associativity non-assoc : Associativity data Precedence : Set where related : Int → Precedence unrelated : Precedence data Fixity : Set where fixity : Associativity → Precedence → Fixity {-# BUILTIN ASSOC Associativity #-} {-# BUILTIN ASSOCLEFT left-assoc #-} {-# BUILTIN ASSOCRIGHT right-assoc #-} {-# BUILTIN ASSOCNON non-assoc #-} {-# BUILTIN PRECEDENCE Precedence #-} {-# BUILTIN PRECRELATED related #-} {-# BUILTIN PRECUNRELATED unrelated #-} {-# BUILTIN FIXITY Fixity #-} {-# BUILTIN FIXITYFIXITY fixity #-} {-# COMPILE GHC Associativity = data MAlonzo.RTE.Assoc (MAlonzo.RTE.LeftAssoc | MAlonzo.RTE.RightAssoc | MAlonzo.RTE.NonAssoc) #-} {-# COMPILE GHC Precedence = data MAlonzo.RTE.Precedence (MAlonzo.RTE.Related | MAlonzo.RTE.Unrelated) #-} {-# COMPILE GHC Fixity = data MAlonzo.RTE.Fixity (MAlonzo.RTE.Fixity) #-} {-# COMPILE JS Associativity = function (x,v) { return v[x](); } #-} {-# COMPILE JS left-assoc = "left-assoc" #-} {-# COMPILE JS right-assoc = "right-assoc" #-} {-# COMPILE JS non-assoc = "non-assoc" #-} {-# COMPILE JS Precedence = function (x,v) { if (x === "unrelated") { return v[x](); } else { return v["related"](x); }} #-} {-# COMPILE JS related = function(x) { return x; } #-} {-# COMPILE JS unrelated = "unrelated" #-} {-# COMPILE JS Fixity = function (x,v) { return v["fixity"](x["assoc"], x["prec"]); } #-} {-# COMPILE JS fixity = function (x) { return function (y) { return { "assoc": x, "prec": y}; }; } #-} primitive primQNameFixity : Name → Fixity -- Metavariables -- postulate Meta : Set {-# BUILTIN AGDAMETA Meta #-} primitive primMetaEquality : Meta → Meta → Bool primMetaLess : Meta → Meta → Bool primShowMeta : Meta → String -- Arguments -- -- Arguments can be (visible), {hidden}, or {{instance}}. data Visibility : Set where visible hidden instance′ : Visibility {-# BUILTIN HIDING Visibility #-} {-# BUILTIN VISIBLE visible #-} {-# BUILTIN HIDDEN hidden #-} {-# BUILTIN INSTANCE instance′ #-} -- Arguments can be relevant or irrelevant. data Relevance : Set where relevant irrelevant : Relevance {-# BUILTIN RELEVANCE Relevance #-} {-# BUILTIN RELEVANT relevant #-} {-# BUILTIN IRRELEVANT irrelevant #-} data ArgInfo : Set where arg-info : (v : Visibility) (r : Relevance) → ArgInfo data Arg (A : Set) : Set where arg : (i : ArgInfo) (x : A) → Arg A {-# BUILTIN ARGINFO ArgInfo #-} {-# BUILTIN ARGARGINFO arg-info #-} {-# BUILTIN ARG Arg #-} {-# BUILTIN ARGARG arg #-} -- Name abstraction -- data Abs (A : Set) : Set where abs : (s : String) (x : A) → Abs A {-# BUILTIN ABS Abs #-} {-# BUILTIN ABSABS abs #-} -- Literals -- data Literal : Set where nat : (n : Nat) → Literal float : (x : Float) → Literal char : (c : Char) → Literal string : (s : String) → Literal name : (x : Name) → Literal meta : (x : Meta) → Literal {-# BUILTIN AGDALITERAL Literal #-} {-# BUILTIN AGDALITNAT nat #-} {-# BUILTIN AGDALITFLOAT float #-} {-# BUILTIN AGDALITCHAR char #-} {-# BUILTIN AGDALITSTRING string #-} {-# BUILTIN AGDALITQNAME name #-} {-# BUILTIN AGDALITMETA meta #-} -- Patterns -- data Pattern : Set where con : (c : Name) (ps : List (Arg Pattern)) → Pattern dot : Pattern var : (s : String) → Pattern lit : (l : Literal) → Pattern proj : (f : Name) → Pattern absurd : Pattern {-# BUILTIN AGDAPATTERN Pattern #-} {-# BUILTIN AGDAPATCON con #-} {-# BUILTIN AGDAPATDOT dot #-} {-# BUILTIN AGDAPATVAR var #-} {-# BUILTIN AGDAPATLIT lit #-} {-# BUILTIN AGDAPATPROJ proj #-} {-# BUILTIN AGDAPATABSURD absurd #-} -- Terms -- data Sort : Set data Clause : Set data Term : Set Type = Term data Term where var : (x : Nat) (args : List (Arg Term)) → Term con : (c : Name) (args : List (Arg Term)) → Term def : (f : Name) (args : List (Arg Term)) → Term lam : (v : Visibility) (t : Abs Term) → Term pat-lam : (cs : List Clause) (args : List (Arg Term)) → Term pi : (a : Arg Type) (b : Abs Type) → Term agda-sort : (s : Sort) → Term lit : (l : Literal) → Term meta : (x : Meta) → List (Arg Term) → Term unknown : Term data Sort where set : (t : Term) → Sort lit : (n : Nat) → Sort unknown : Sort data Clause where clause : (ps : List (Arg Pattern)) (t : Term) → Clause absurd-clause : (ps : List (Arg Pattern)) → Clause {-# BUILTIN AGDASORT Sort #-} {-# BUILTIN AGDATERM Term #-} {-# BUILTIN AGDACLAUSE Clause #-} {-# BUILTIN AGDATERMVAR var #-} {-# BUILTIN AGDATERMCON con #-} {-# BUILTIN AGDATERMDEF def #-} {-# BUILTIN AGDATERMMETA meta #-} {-# BUILTIN AGDATERMLAM lam #-} {-# BUILTIN AGDATERMEXTLAM pat-lam #-} {-# BUILTIN AGDATERMPI pi #-} {-# BUILTIN AGDATERMSORT agda-sort #-} {-# BUILTIN AGDATERMLIT lit #-} {-# BUILTIN AGDATERMUNSUPPORTED unknown #-} {-# BUILTIN AGDASORTSET set #-} {-# BUILTIN AGDASORTLIT lit #-} {-# BUILTIN AGDASORTUNSUPPORTED unknown #-} {-# BUILTIN AGDACLAUSECLAUSE clause #-} {-# BUILTIN AGDACLAUSEABSURD absurd-clause #-} -- Definitions -- data Definition : Set where function : (cs : List Clause) → Definition data-type : (pars : Nat) (cs : List Name) → Definition record-type : (c : Name) (fs : List (Arg Name)) → Definition data-cons : (d : Name) → Definition axiom : Definition prim-fun : Definition {-# BUILTIN AGDADEFINITION Definition #-} {-# BUILTIN AGDADEFINITIONFUNDEF function #-} {-# BUILTIN AGDADEFINITIONDATADEF data-type #-} {-# BUILTIN AGDADEFINITIONRECORDDEF record-type #-} {-# BUILTIN AGDADEFINITIONDATACONSTRUCTOR data-cons #-} {-# BUILTIN AGDADEFINITIONPOSTULATE axiom #-} {-# BUILTIN AGDADEFINITIONPRIMITIVE prim-fun #-} -- Errors -- data ErrorPart : Set where strErr : String → ErrorPart termErr : Term → ErrorPart nameErr : Name → ErrorPart {-# BUILTIN AGDAERRORPART ErrorPart #-} {-# BUILTIN AGDAERRORPARTSTRING strErr #-} {-# BUILTIN AGDAERRORPARTTERM termErr #-} {-# BUILTIN AGDAERRORPARTNAME nameErr #-} -- TC monad -- postulate TC : ∀ {a} → Set a → Set a returnTC : ∀ {a} {A : Set a} → A → TC A bindTC : ∀ {a b} {A : Set a} {B : Set b} → TC A → (A → TC B) → TC B unify : Term → Term → TC ⊤ typeError : ∀ {a} {A : Set a} → List ErrorPart → TC A inferType : Term → TC Type checkType : Term → Type → TC Term normalise : Term → TC Term reduce : Term → TC Term catchTC : ∀ {a} {A : Set a} → TC A → TC A → TC A quoteTC : ∀ {a} {A : Set a} → A → TC Term unquoteTC : ∀ {a} {A : Set a} → Term → TC A getContext : TC (List (Arg Type)) extendContext : ∀ {a} {A : Set a} → Arg Type → TC A → TC A inContext : ∀ {a} {A : Set a} → List (Arg Type) → TC A → TC A freshName : String → TC Name declareDef : Arg Name → Type → TC ⊤ defineFun : Name → List Clause → TC ⊤ getType : Name → TC Type getDefinition : Name → TC Definition blockOnMeta : ∀ {a} {A : Set a} → Meta → TC A commitTC : TC ⊤ isMacro : Name → TC Bool -- If the argument is 'true' makes the following primitives also normalise -- their results: inferType, checkType, quoteTC, getType, and getContext withNormalisation : ∀ {a} {A : Set a} → Bool → TC A → TC A -- Prints the third argument if the corresponding verbosity level is turned -- on (with the -v flag to Agda). debugPrint : String → Nat → List ErrorPart → TC ⊤ {-# BUILTIN AGDATCM TC #-} {-# BUILTIN AGDATCMRETURN returnTC #-} {-# BUILTIN AGDATCMBIND bindTC #-} {-# BUILTIN AGDATCMUNIFY unify #-} {-# BUILTIN AGDATCMTYPEERROR typeError #-} {-# BUILTIN AGDATCMINFERTYPE inferType #-} {-# BUILTIN AGDATCMCHECKTYPE checkType #-} {-# BUILTIN AGDATCMNORMALISE normalise #-} {-# BUILTIN AGDATCMREDUCE reduce #-} {-# BUILTIN AGDATCMCATCHERROR catchTC #-} {-# BUILTIN AGDATCMQUOTETERM quoteTC #-} {-# BUILTIN AGDATCMUNQUOTETERM unquoteTC #-} {-# BUILTIN AGDATCMGETCONTEXT getContext #-} {-# BUILTIN AGDATCMEXTENDCONTEXT extendContext #-} {-# BUILTIN AGDATCMINCONTEXT inContext #-} {-# BUILTIN AGDATCMFRESHNAME freshName #-} {-# BUILTIN AGDATCMDECLAREDEF declareDef #-} {-# BUILTIN AGDATCMDEFINEFUN defineFun #-} {-# BUILTIN AGDATCMGETTYPE getType #-} {-# BUILTIN AGDATCMGETDEFINITION getDefinition #-} {-# BUILTIN AGDATCMBLOCKONMETA blockOnMeta #-} {-# BUILTIN AGDATCMCOMMIT commitTC #-} {-# BUILTIN AGDATCMISMACRO isMacro #-} {-# BUILTIN AGDATCMWITHNORMALISATION withNormalisation #-} {-# BUILTIN AGDATCMDEBUGPRINT debugPrint #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/FromNeg.agda0000644000000000000000000000061013154613124020372 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.FromNeg where open import Agda.Primitive open import Agda.Builtin.Nat record Negative {a} (A : Set a) : Set (lsuc a) where field Constraint : Nat → Set a fromNeg : ∀ n → {{_ : Constraint n}} → A open Negative {{...}} public using (fromNeg) {-# BUILTIN FROMNEG fromNeg #-} {-# DISPLAY Negative.fromNeg _ n = fromNeg n #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Float.agda0000644000000000000000000000226613154613124020113 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Float where open import Agda.Builtin.Bool open import Agda.Builtin.Nat open import Agda.Builtin.Int open import Agda.Builtin.String postulate Float : Set {-# BUILTIN FLOAT Float #-} primitive primFloatEquality : Float → Float → Bool primFloatNumericalEquality : Float → Float → Bool primFloatNumericalLess : Float → Float → Bool primNatToFloat : Nat → Float primFloatPlus : Float → Float → Float primFloatMinus : Float → Float → Float primFloatTimes : Float → Float → Float primFloatNegate : Float → Float primFloatDiv : Float → Float → Float primFloatSqrt : Float → Float primRound : Float → Int primFloor : Float → Int primCeiling : Float → Int primExp : Float → Float primLog : Float → Float primSin : Float → Float primCos : Float → Float primTan : Float → Float primASin : Float → Float primACos : Float → Float primATan : Float → Float primATan2 : Float → Float → Float primShowFloat : Float → String Agda-2.5.3/src/data/lib/prim/Agda/Builtin/FromString.agda0000644000000000000000000000064613154613124021140 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.FromString where open import Agda.Primitive open import Agda.Builtin.String record IsString {a} (A : Set a) : Set (lsuc a) where field Constraint : String → Set a fromString : (s : String) {{_ : Constraint s}} → A open IsString {{...}} public using (fromString) {-# BUILTIN FROMSTRING fromString #-} {-# DISPLAY IsString.fromString _ s = fromString s #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Unit.agda0000644000000000000000000000025713154613124017763 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Unit where record ⊤ : Set where instance constructor tt {-# BUILTIN UNIT ⊤ #-} {-# COMPILE GHC ⊤ = data () (()) #-} Agda-2.5.3/src/data/lib/prim/Agda/Builtin/Int.agda0000644000000000000000000000062713154613124017577 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.Int where open import Agda.Builtin.Nat open import Agda.Builtin.String infix 8 pos -- Standard library uses this as +_ data Int : Set where pos : (n : Nat) → Int negsuc : (n : Nat) → Int {-# BUILTIN INTEGER Int #-} {-# BUILTIN INTEGERPOS pos #-} {-# BUILTIN INTEGERNEGSUC negsuc #-} primitive primShowInteger : Int → String Agda-2.5.3/src/data/lib/prim/Agda/Builtin/List.agda0000644000000000000000000000124313154613124017753 0ustar0000000000000000{-# OPTIONS --without-K #-} module Agda.Builtin.List where infixr 5 _∷_ data List {a} (A : Set a) : Set a where [] : List A _∷_ : (x : A) (xs : List A) → List A {-# BUILTIN LIST List #-} {-# BUILTIN NIL [] #-} {-# BUILTIN CONS _∷_ #-} {-# FOREIGN GHC type AgdaList a b = [b] #-} {-# COMPILE GHC List = data AgdaList ([] | (:)) #-} {-# COMPILE UHC List = data __LIST__ (__NIL__ | __CONS__) #-} {-# COMPILE JS List = function(x,v) { if (x.length < 1) { return v["[]"](); } else { return v["_∷_"](x[0], x.slice(1)); } } #-} {-# COMPILE JS [] = Array() #-} {-# COMPILE JS _∷_ = function (x) { return function(y) { return Array(x).concat(y); }; } #-} Agda-2.5.3/src/data/MAlonzo/0000755000000000000000000000000013154613124013622 5ustar0000000000000000Agda-2.5.3/src/data/MAlonzo/src/0000755000000000000000000000000013154613124014411 5ustar0000000000000000Agda-2.5.3/src/data/MAlonzo/src/MAlonzo/0000755000000000000000000000000013154613124015770 5ustar0000000000000000Agda-2.5.3/src/data/MAlonzo/src/MAlonzo/RTE.hs0000644000000000000000000000443513154613124016764 0ustar0000000000000000module MAlonzo.RTE where import Unsafe.Coerce import GHC.Prim import Numeric.IEEE ( IEEE(identicalIEEE) ) -- Special version of coerce that plays well with rules. {-# INLINE [1] coe #-} coe = unsafeCoerce {-# RULES "coerce-id" forall (x :: a) . coe x = x #-} -- Builtin QNames. data QName = QName { nameId, moduleId :: Integer, qnameString :: String, qnameFixity :: Fixity } data Assoc = NonAssoc | LeftAssoc | RightAssoc data Precedence = Unrelated | Related Integer data Fixity = Fixity Assoc Precedence instance Eq QName where QName a b _ _ == QName c d _ _ = (a, b) == (c, d) instance Ord QName where compare (QName a b _ _) (QName c d _ _) = compare (a, b) (c, d) erased :: a erased = coe (\ _ -> erased) mazIncompleteMatch :: String -> a mazIncompleteMatch s = error ("Agda: incomplete pattern matching: " ++ s) mazUnreachableError :: a mazUnreachableError = error ("Agda: unreachable code reached.") addInt :: Integer -> Integer -> Integer addInt = (+) subInt :: Integer -> Integer -> Integer subInt = (-) mulInt :: Integer -> Integer -> Integer mulInt = (*) geqInt :: Integer -> Integer -> Bool geqInt = (>=) ltInt :: Integer -> Integer -> Bool ltInt = (<) eqInt :: Integer -> Integer -> Bool eqInt = (==) quotInt :: Integer -> Integer -> Integer quotInt = quot remInt :: Integer -> Integer -> Integer remInt = rem eqFloat :: Double -> Double -> Bool eqFloat x y = identicalIEEE x y || (isNaN x && isNaN y) eqNumFloat :: Double -> Double -> Bool eqNumFloat = (==) negativeZero :: Double negativeZero = -0.0 positiveInfinity :: Double positiveInfinity = 1.0 / 0.0 negativeInfinity :: Double negativeInfinity = -positiveInfinity positiveNaN :: Double positiveNaN = 0.0 / 0.0 negativeNaN :: Double negativeNaN = -positiveNaN -- Adapted from the same function on Agda.Syntax.Literal. compareFloat :: Double -> Double -> Ordering compareFloat x y | identicalIEEE x y = EQ | isNegInf x = LT | isNegInf y = GT | isNaN x && isNaN y = EQ | isNaN x = LT | isNaN y = GT | otherwise = compare x y where isNegInf z = z < 0 && isInfinite z ltNumFloat :: Double -> Double -> Bool ltNumFloat x y = case compareFloat x y of LT -> True _ -> False Agda-2.5.3/src/data/JS/0000755000000000000000000000000013154613124012557 5ustar0000000000000000Agda-2.5.3/src/data/JS/agda-rts.js0000644000000000000000000000726313154613124014627 0ustar0000000000000000// NOTE: // Some of the primitives here are curried, some are not. All uncurried primitives are prefixed by 'u'. var biginteger = require("biginteger") // Integers exports.primIntegerFromString = function(x) { return biginteger.BigInteger(x); }; exports.primShowInteger = function(x) { return x.toString(); }; exports.uprimIntegerPlus = function(x,y) { return x.add(y); }; exports.uprimIntegerMinus = function(x,y) { return x.subtract(y); }; exports.uprimIntegerMultiply = function(x,y) { return x.multiply(y); }; exports.uprimIntegerRem = function(x, y) { return x.remainder(y); }; exports.uprimIntegerQuot = function(x, y) { return x.quotient(y); }; exports.uprimIntegerEqual = function(x,y) { return x.compare(y) == 0; }; exports.uprimIntegerGreaterOrEqualThan = function(x,y) { return x.compare(y) >= 0; }; exports.uprimIntegerLessThan = function(x,y) { return x.compare(y) == -1; }; exports.primNatMinus = function(x) { return function(y) { var z = x.subtract(y); if (z.isNegative()) { return biginteger.ZERO; } else { return z; } }; }; // Floats exports.primShowFloat = function(x) { // See Issue #2192. if (Number.isInteger(x)) { if (Object.is(x,-0.0)) { return ("-0.0"); } else { var a = x.toString(); return (a + ".0"); } } else { return x.toString(); } }; exports.primFloatEquality = function(x) { return function(y) { return Object.is(x,y); }; }; exports.primFloatNumericalEquality = function(x) { return function(y) { return x==y; }; }; exports.uprimFloatEquality = function(x, y) { return Object.is(x,y); }; exports.primFloatNumericalLess = function(x) { return function(y) { if(x == Number.NEGATIVE_INFINITY) { return y != Number.NEGATIVE_INFINITY; } else if(y == Number.NEGATIVE_INFINITY) { return false; } else if(isNaN(x)) { return !isNaN(y); } else if(isNaN(y)) { return false; } else { return x Copyright (c) 2010,2011 by John Tobey Licensed under the MIT license. Support for arbitrary internal representation base was added by Vitaly Magerya. */ /* File: biginteger.js Exports: */ (function(exports) { "use strict"; /* Class: BigInteger An arbitrarily-large integer. objects should be considered immutable. None of the "built-in" methods modify *this* or their arguments. All properties should be considered private. All the methods of instances can be called "statically". The static versions are convenient if you don't already have a object. As an example, these calls are equivalent. > BigInteger(4).multiply(5); // returns BigInteger(20); > BigInteger.multiply(4, 5); // returns BigInteger(20); > var a = 42; > var a = BigInteger.toJSValue("0b101010"); // Not completely useless... */ var CONSTRUCT = {}; // Unique token to call "private" version of constructor /* Constructor: BigInteger() Convert a value to a . Although is the constructor for objects, it is best not to call it as a constructor. If *n* is a object, it is simply returned as-is. Otherwise, is equivalent to without a radix argument. > var n0 = BigInteger(); // Same as > var n1 = BigInteger("123"); // Create a new with value 123 > var n2 = BigInteger(123); // Create a new with value 123 > var n3 = BigInteger(n2); // Return n2, unchanged The constructor form only takes an array and a sign. *n* must be an array of numbers in little-endian order, where each digit is between 0 and BigInteger.base. The second parameter sets the sign: -1 for negative, +1 for positive, or 0 for zero. The array is *not copied and may be modified*. If the array contains only zeros, the sign parameter is ignored and is forced to zero. > new BigInteger([5], -1): create a new BigInteger with value -5 Parameters: n - Value to convert to a . Returns: A value. See Also: , */ function BigInteger(n, s, token) { if (token !== CONSTRUCT) { if (n instanceof BigInteger) { return n; } else if (typeof n === "undefined") { return ZERO; } return BigInteger.parse(n); } n = n || []; // Provide the nullary constructor for subclasses. while (n.length && !n[n.length - 1]) { --n.length; } this._d = n; this._s = n.length ? (s || 1) : 0; } BigInteger._construct = function(n, s) { return new BigInteger(n, s, CONSTRUCT); }; // Base-10 speedup hacks in parse, toString, exp10 and log functions // require base to be a power of 10. 10^7 is the largest such power // that won't cause a precision loss when digits are multiplied. var BigInteger_base = 10000000; var BigInteger_base_log10 = 7; BigInteger.base = BigInteger_base; BigInteger.base_log10 = BigInteger_base_log10; var ZERO = new BigInteger([], 0, CONSTRUCT); // Constant: ZERO // 0. BigInteger.ZERO = ZERO; var ONE = new BigInteger([1], 1, CONSTRUCT); // Constant: ONE // 1. BigInteger.ONE = ONE; var M_ONE = new BigInteger(ONE._d, -1, CONSTRUCT); // Constant: M_ONE // -1. BigInteger.M_ONE = M_ONE; // Constant: _0 // Shortcut for . BigInteger._0 = ZERO; // Constant: _1 // Shortcut for . BigInteger._1 = ONE; /* Constant: small Array of from 0 to 36. These are used internally for parsing, but useful when you need a "small" . See Also: , , <_0>, <_1> */ BigInteger.small = [ ZERO, ONE, /* Assuming BigInteger_base > 36 */ new BigInteger( [2], 1, CONSTRUCT), new BigInteger( [3], 1, CONSTRUCT), new BigInteger( [4], 1, CONSTRUCT), new BigInteger( [5], 1, CONSTRUCT), new BigInteger( [6], 1, CONSTRUCT), new BigInteger( [7], 1, CONSTRUCT), new BigInteger( [8], 1, CONSTRUCT), new BigInteger( [9], 1, CONSTRUCT), new BigInteger([10], 1, CONSTRUCT), new BigInteger([11], 1, CONSTRUCT), new BigInteger([12], 1, CONSTRUCT), new BigInteger([13], 1, CONSTRUCT), new BigInteger([14], 1, CONSTRUCT), new BigInteger([15], 1, CONSTRUCT), new BigInteger([16], 1, CONSTRUCT), new BigInteger([17], 1, CONSTRUCT), new BigInteger([18], 1, CONSTRUCT), new BigInteger([19], 1, CONSTRUCT), new BigInteger([20], 1, CONSTRUCT), new BigInteger([21], 1, CONSTRUCT), new BigInteger([22], 1, CONSTRUCT), new BigInteger([23], 1, CONSTRUCT), new BigInteger([24], 1, CONSTRUCT), new BigInteger([25], 1, CONSTRUCT), new BigInteger([26], 1, CONSTRUCT), new BigInteger([27], 1, CONSTRUCT), new BigInteger([28], 1, CONSTRUCT), new BigInteger([29], 1, CONSTRUCT), new BigInteger([30], 1, CONSTRUCT), new BigInteger([31], 1, CONSTRUCT), new BigInteger([32], 1, CONSTRUCT), new BigInteger([33], 1, CONSTRUCT), new BigInteger([34], 1, CONSTRUCT), new BigInteger([35], 1, CONSTRUCT), new BigInteger([36], 1, CONSTRUCT) ]; // Used for parsing/radix conversion BigInteger.digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""); /* Method: toString Convert a to a string. When *base* is greater than 10, letters are upper case. Parameters: base - Optional base to represent the number in (default is base 10). Must be between 2 and 36 inclusive, or an Error will be thrown. Returns: The string representation of the . */ BigInteger.prototype.toString = function(base) { base = +base || 10; if (base < 2 || base > 36) { throw new Error("illegal radix " + base + "."); } if (this._s === 0) { return "0"; } if (base === 10) { var str = this._s < 0 ? "-" : ""; str += this._d[this._d.length - 1].toString(); for (var i = this._d.length - 2; i >= 0; i--) { var group = this._d[i].toString(); while (group.length < BigInteger_base_log10) group = '0' + group; str += group; } return str; } else { var numerals = BigInteger.digits; base = BigInteger.small[base]; var sign = this._s; var n = this.abs(); var digits = []; var digit; while (n._s !== 0) { var divmod = n.divRem(base); n = divmod[0]; digit = divmod[1]; // TODO: This could be changed to unshift instead of reversing at the end. // Benchmark both to compare speeds. digits.push(numerals[digit.valueOf()]); } return (sign < 0 ? "-" : "") + digits.reverse().join(""); } }; // Verify strings for parsing BigInteger.radixRegex = [ /^$/, /^$/, /^[01]*$/, /^[012]*$/, /^[0-3]*$/, /^[0-4]*$/, /^[0-5]*$/, /^[0-6]*$/, /^[0-7]*$/, /^[0-8]*$/, /^[0-9]*$/, /^[0-9aA]*$/, /^[0-9abAB]*$/, /^[0-9abcABC]*$/, /^[0-9a-dA-D]*$/, /^[0-9a-eA-E]*$/, /^[0-9a-fA-F]*$/, /^[0-9a-gA-G]*$/, /^[0-9a-hA-H]*$/, /^[0-9a-iA-I]*$/, /^[0-9a-jA-J]*$/, /^[0-9a-kA-K]*$/, /^[0-9a-lA-L]*$/, /^[0-9a-mA-M]*$/, /^[0-9a-nA-N]*$/, /^[0-9a-oA-O]*$/, /^[0-9a-pA-P]*$/, /^[0-9a-qA-Q]*$/, /^[0-9a-rA-R]*$/, /^[0-9a-sA-S]*$/, /^[0-9a-tA-T]*$/, /^[0-9a-uA-U]*$/, /^[0-9a-vA-V]*$/, /^[0-9a-wA-W]*$/, /^[0-9a-xA-X]*$/, /^[0-9a-yA-Y]*$/, /^[0-9a-zA-Z]*$/ ]; /* Function: parse Parse a string into a . *base* is optional but, if provided, must be from 2 to 36 inclusive. If *base* is not provided, it will be guessed based on the leading characters of *s* as follows: - "0x" or "0X": *base* = 16 - "0c" or "0C": *base* = 8 - "0b" or "0B": *base* = 2 - else: *base* = 10 If no base is provided, or *base* is 10, the number can be in exponential form. For example, these are all valid: > BigInteger.parse("1e9"); // Same as "1000000000" > BigInteger.parse("1.234*10^3"); // Same as 1234 > BigInteger.parse("56789 * 10 ** -2"); // Same as 567 If any characters fall outside the range defined by the radix, an exception will be thrown. Parameters: s - The string to parse. base - Optional radix (default is to guess based on *s*). Returns: a instance. */ BigInteger.parse = function(s, base) { // Expands a number in exponential form to decimal form. // expandExponential("-13.441*10^5") === "1344100"; // expandExponential("1.12300e-1") === "0.112300"; // expandExponential(1000000000000000000000000000000) === "1000000000000000000000000000000"; function expandExponential(str) { str = str.replace(/\s*[*xX]\s*10\s*(\^|\*\*)\s*/, "e"); return str.replace(/^([+\-])?(\d+)\.?(\d*)[eE]([+\-]?\d+)$/, function(x, s, n, f, c) { c = +c; var l = c < 0; var i = n.length + c; x = (l ? n : f).length; c = ((c = Math.abs(c)) >= x ? c - x + l : 0); var z = (new Array(c + 1)).join("0"); var r = n + f; return (s || "") + (l ? r = z + r : r += z).substr(0, i += l ? z.length : 0) + (i < r.length ? "." + r.substr(i) : ""); }); } s = s.toString(); if (typeof base === "undefined" || +base === 10) { s = expandExponential(s); } var prefixRE; if (typeof base === "undefined") { prefixRE = '0[xcb]'; } else if (base == 16) { prefixRE = '0x'; } else if (base == 8) { prefixRE = '0c'; } else if (base == 2) { prefixRE = '0b'; } else { prefixRE = ''; } var parts = new RegExp('^([+\\-]?)(' + prefixRE + ')?([0-9a-z]*)(?:\\.\\d*)?$', 'i').exec(s); if (parts) { var sign = parts[1] || "+"; var baseSection = parts[2] || ""; var digits = parts[3] || ""; if (typeof base === "undefined") { // Guess base if (baseSection === "0x" || baseSection === "0X") { // Hex base = 16; } else if (baseSection === "0c" || baseSection === "0C") { // Octal base = 8; } else if (baseSection === "0b" || baseSection === "0B") { // Binary base = 2; } else { base = 10; } } else if (base < 2 || base > 36) { throw new Error("Illegal radix " + base + "."); } base = +base; // Check for digits outside the range if (!(BigInteger.radixRegex[base].test(digits))) { throw new Error("Bad digit for radix " + base); } // Strip leading zeros, and convert to array digits = digits.replace(/^0+/, "").split(""); if (digits.length === 0) { return ZERO; } // Get the sign (we know it's not zero) sign = (sign === "-") ? -1 : 1; // Optimize 10 if (base == 10) { var d = []; while (digits.length >= BigInteger_base_log10) { d.push(parseInt(digits.splice(digits.length-BigInteger.base_log10, BigInteger.base_log10).join(''), 10)); } d.push(parseInt(digits.join(''), 10)); return new BigInteger(d, sign, CONSTRUCT); } // Do the conversion var d = ZERO; base = BigInteger.small[base]; var small = BigInteger.small; for (var i = 0; i < digits.length; i++) { d = d.multiply(base).add(small[parseInt(digits[i], 36)]); } return new BigInteger(d._d, sign, CONSTRUCT); } else { throw new Error("Invalid BigInteger format: " + s); } }; /* Function: add Add two . Parameters: n - The number to add to *this*. Will be converted to a . Returns: The numbers added together. See Also: , , , */ BigInteger.prototype.add = function(n) { if (this._s === 0) { return BigInteger(n); } n = BigInteger(n); if (n._s === 0) { return this; } if (this._s !== n._s) { n = n.negate(); return this.subtract(n); } var a = this._d; var b = n._d; var al = a.length; var bl = b.length; var sum = new Array(Math.max(al, bl) + 1); var size = Math.min(al, bl); var carry = 0; var digit; for (var i = 0; i < size; i++) { digit = a[i] + b[i] + carry; sum[i] = digit % BigInteger_base; carry = (digit / BigInteger_base) | 0; } if (bl > al) { a = b; al = bl; } for (i = size; carry && i < al; i++) { digit = a[i] + carry; sum[i] = digit % BigInteger_base; carry = (digit / BigInteger_base) | 0; } if (carry) { sum[i] = carry; } for ( ; i < al; i++) { sum[i] = a[i]; } return new BigInteger(sum, this._s, CONSTRUCT); }; /* Function: negate Get the additive inverse of a . Returns: A with the same magnatude, but with the opposite sign. See Also: */ BigInteger.prototype.negate = function() { return new BigInteger(this._d, (-this._s) | 0, CONSTRUCT); }; /* Function: abs Get the absolute value of a . Returns: A with the same magnatude, but always positive (or zero). See Also: */ BigInteger.prototype.abs = function() { return (this._s < 0) ? this.negate() : this; }; /* Function: subtract Subtract two . Parameters: n - The number to subtract from *this*. Will be converted to a . Returns: The *n* subtracted from *this*. See Also: , , , */ BigInteger.prototype.subtract = function(n) { if (this._s === 0) { return BigInteger(n).negate(); } n = BigInteger(n); if (n._s === 0) { return this; } if (this._s !== n._s) { n = n.negate(); return this.add(n); } var m = this; // negative - negative => -|a| - -|b| => -|a| + |b| => |b| - |a| if (this._s < 0) { m = new BigInteger(n._d, 1, CONSTRUCT); n = new BigInteger(this._d, 1, CONSTRUCT); } // Both are positive => a - b var sign = m.compareAbs(n); if (sign === 0) { return ZERO; } else if (sign < 0) { // swap m and n var t = n; n = m; m = t; } // a > b var a = m._d; var b = n._d; var al = a.length; var bl = b.length; var diff = new Array(al); // al >= bl since a > b var borrow = 0; var i; var digit; for (i = 0; i < bl; i++) { digit = a[i] - borrow - b[i]; if (digit < 0) { digit += BigInteger_base; borrow = 1; } else { borrow = 0; } diff[i] = digit; } for (i = bl; i < al; i++) { digit = a[i] - borrow; if (digit < 0) { digit += BigInteger_base; } else { diff[i++] = digit; break; } diff[i] = digit; } for ( ; i < al; i++) { diff[i] = a[i]; } return new BigInteger(diff, sign, CONSTRUCT); }; (function() { function addOne(n, sign) { var a = n._d; var sum = a.slice(); var carry = true; var i = 0; while (true) { var digit = (a[i] || 0) + 1; sum[i] = digit % BigInteger_base; if (digit <= BigInteger_base - 1) { break; } ++i; } return new BigInteger(sum, sign, CONSTRUCT); } function subtractOne(n, sign) { var a = n._d; var sum = a.slice(); var borrow = true; var i = 0; while (true) { var digit = (a[i] || 0) - 1; if (digit < 0) { sum[i] = digit + BigInteger_base; } else { sum[i] = digit; break; } ++i; } return new BigInteger(sum, sign, CONSTRUCT); } /* Function: next Get the next (add one). Returns: *this* + 1. See Also: , */ BigInteger.prototype.next = function() { switch (this._s) { case 0: return ONE; case -1: return subtractOne(this, -1); // case 1: default: return addOne(this, 1); } }; /* Function: prev Get the previous (subtract one). Returns: *this* - 1. See Also: , */ BigInteger.prototype.prev = function() { switch (this._s) { case 0: return M_ONE; case -1: return addOne(this, -1); // case 1: default: return subtractOne(this, 1); } }; })(); /* Function: compareAbs Compare the absolute value of two . Calling is faster than calling twice, then . Parameters: n - The number to compare to *this*. Will be converted to a . Returns: -1, 0, or +1 if *|this|* is less than, equal to, or greater than *|n|*. See Also: , */ BigInteger.prototype.compareAbs = function(n) { if (this === n) { return 0; } if (!(n instanceof BigInteger)) { if (!isFinite(n)) { return(isNaN(n) ? n : -1); } n = BigInteger(n); } if (this._s === 0) { return (n._s !== 0) ? -1 : 0; } if (n._s === 0) { return 1; } var l = this._d.length; var nl = n._d.length; if (l < nl) { return -1; } else if (l > nl) { return 1; } var a = this._d; var b = n._d; for (var i = l-1; i >= 0; i--) { if (a[i] !== b[i]) { return a[i] < b[i] ? -1 : 1; } } return 0; }; /* Function: compare Compare two . Parameters: n - The number to compare to *this*. Will be converted to a . Returns: -1, 0, or +1 if *this* is less than, equal to, or greater than *n*. See Also: , , , */ BigInteger.prototype.compare = function(n) { if (this === n) { return 0; } n = BigInteger(n); if (this._s === 0) { return -n._s; } if (this._s === n._s) { // both positive or both negative var cmp = this.compareAbs(n); return cmp * this._s; } else { return this._s; } }; /* Function: isUnit Return true iff *this* is either 1 or -1. Returns: true if *this* compares equal to or . See Also: , , , , , , */ BigInteger.prototype.isUnit = function() { return this === ONE || this === M_ONE || (this._d.length === 1 && this._d[0] === 1); }; /* Function: multiply Multiply two . Parameters: n - The number to multiply *this* by. Will be converted to a . Returns: The numbers multiplied together. See Also: , , , */ BigInteger.prototype.multiply = function(n) { // TODO: Consider adding Karatsuba multiplication for large numbers if (this._s === 0) { return ZERO; } n = BigInteger(n); if (n._s === 0) { return ZERO; } if (this.isUnit()) { if (this._s < 0) { return n.negate(); } return n; } if (n.isUnit()) { if (n._s < 0) { return this.negate(); } return this; } if (this === n) { return this.square(); } var r = (this._d.length >= n._d.length); var a = (r ? this : n)._d; // a will be longer than b var b = (r ? n : this)._d; var al = a.length; var bl = b.length; var pl = al + bl; var partial = new Array(pl); var i; for (i = 0; i < pl; i++) { partial[i] = 0; } for (i = 0; i < bl; i++) { var carry = 0; var bi = b[i]; var jlimit = al + i; var digit; for (var j = i; j < jlimit; j++) { digit = partial[j] + bi * a[j - i] + carry; carry = (digit / BigInteger_base) | 0; partial[j] = (digit % BigInteger_base) | 0; } if (carry) { digit = partial[j] + carry; carry = (digit / BigInteger_base) | 0; partial[j] = digit % BigInteger_base; } } return new BigInteger(partial, this._s * n._s, CONSTRUCT); }; // Multiply a BigInteger by a single-digit native number // Assumes that this and n are >= 0 // This is not really intended to be used outside the library itself BigInteger.prototype.multiplySingleDigit = function(n) { if (n === 0 || this._s === 0) { return ZERO; } if (n === 1) { return this; } var digit; if (this._d.length === 1) { digit = this._d[0] * n; if (digit >= BigInteger_base) { return new BigInteger([(digit % BigInteger_base)|0, (digit / BigInteger_base)|0], 1, CONSTRUCT); } return new BigInteger([digit], 1, CONSTRUCT); } if (n === 2) { return this.add(this); } if (this.isUnit()) { return new BigInteger([n], 1, CONSTRUCT); } var a = this._d; var al = a.length; var pl = al + 1; var partial = new Array(pl); for (var i = 0; i < pl; i++) { partial[i] = 0; } var carry = 0; for (var j = 0; j < al; j++) { digit = n * a[j] + carry; carry = (digit / BigInteger_base) | 0; partial[j] = (digit % BigInteger_base) | 0; } if (carry) { partial[j] = carry; } return new BigInteger(partial, 1, CONSTRUCT); }; /* Function: square Multiply a by itself. This is slightly faster than regular multiplication, since it removes the duplicated multiplcations. Returns: > this.multiply(this) See Also: */ BigInteger.prototype.square = function() { // Normally, squaring a 10-digit number would take 100 multiplications. // Of these 10 are unique diagonals, of the remaining 90 (100-10), 45 are repeated. // This procedure saves (N*(N-1))/2 multiplications, (e.g., 45 of 100 multiplies). // Based on code by Gary Darby, Intellitech Systems Inc., www.DelphiForFun.org if (this._s === 0) { return ZERO; } if (this.isUnit()) { return ONE; } var digits = this._d; var length = digits.length; var imult1 = new Array(length + length + 1); var product, carry, k; var i; // Calculate diagonal for (i = 0; i < length; i++) { k = i * 2; product = digits[i] * digits[i]; carry = (product / BigInteger_base) | 0; imult1[k] = product % BigInteger_base; imult1[k + 1] = carry; } // Calculate repeating part for (i = 0; i < length; i++) { carry = 0; k = i * 2 + 1; for (var j = i + 1; j < length; j++, k++) { product = digits[j] * digits[i] * 2 + imult1[k] + carry; carry = (product / BigInteger_base) | 0; imult1[k] = product % BigInteger_base; } k = length + i; var digit = carry + imult1[k]; carry = (digit / BigInteger_base) | 0; imult1[k] = digit % BigInteger_base; imult1[k + 1] += carry; } return new BigInteger(imult1, 1, CONSTRUCT); }; /* Function: quotient Divide two and truncate towards zero. throws an exception if *n* is zero. Parameters: n - The number to divide *this* by. Will be converted to a . Returns: The *this* / *n*, truncated to an integer. See Also: , , , , */ BigInteger.prototype.quotient = function(n) { return this.divRem(n)[0]; }; /* Function: divide Deprecated synonym for . */ BigInteger.prototype.divide = BigInteger.prototype.quotient; /* Function: remainder Calculate the remainder of two . throws an exception if *n* is zero. Parameters: n - The remainder after *this* is divided *this* by *n*. Will be converted to a . Returns: *this* % *n*. See Also: , */ BigInteger.prototype.remainder = function(n) { return this.divRem(n)[1]; }; /* Function: divRem Calculate the integer quotient and remainder of two . throws an exception if *n* is zero. Parameters: n - The number to divide *this* by. Will be converted to a . Returns: A two-element array containing the quotient and the remainder. > a.divRem(b) is exactly equivalent to > [a.quotient(b), a.remainder(b)] except it is faster, because they are calculated at the same time. See Also: , */ BigInteger.prototype.divRem = function(n) { n = BigInteger(n); if (n._s === 0) { throw new Error("Divide by zero"); } if (this._s === 0) { return [ZERO, ZERO]; } if (n._d.length === 1) { return this.divRemSmall(n._s * n._d[0]); } // Test for easy cases -- |n1| <= |n2| switch (this.compareAbs(n)) { case 0: // n1 == n2 return [this._s === n._s ? ONE : M_ONE, ZERO]; case -1: // |n1| < |n2| return [ZERO, this]; } var sign = this._s * n._s; var a = n.abs(); var b_digits = this._d; var b_index = b_digits.length; var digits = n._d.length; var quot = []; var guess; var part = new BigInteger([], 0, CONSTRUCT); while (b_index) { part._d.unshift(b_digits[--b_index]); part = new BigInteger(part._d, 1, CONSTRUCT); if (part.compareAbs(n) < 0) { quot.push(0); continue; } if (part._s === 0) { guess = 0; } else { var xlen = part._d.length, ylen = a._d.length; var highx = part._d[xlen-1]*BigInteger_base + part._d[xlen-2]; var highy = a._d[ylen-1]*BigInteger_base + a._d[ylen-2]; if (part._d.length > a._d.length) { // The length of part._d can either match a._d length, // or exceed it by one. highx = (highx+1)*BigInteger_base; } guess = Math.ceil(highx/highy); } do { var check = a.multiplySingleDigit(guess); if (check.compareAbs(part) <= 0) { break; } guess--; } while (guess); quot.push(guess); if (!guess) { continue; } var diff = part.subtract(check); part._d = diff._d.slice(); } return [new BigInteger(quot.reverse(), sign, CONSTRUCT), new BigInteger(part._d, this._s, CONSTRUCT)]; }; // Throws an exception if n is outside of (-BigInteger.base, -1] or // [1, BigInteger.base). It's not necessary to call this, since the // other division functions will call it if they are able to. BigInteger.prototype.divRemSmall = function(n) { var r; n = +n; if (n === 0) { throw new Error("Divide by zero"); } var n_s = n < 0 ? -1 : 1; var sign = this._s * n_s; n = Math.abs(n); if (n < 1 || n >= BigInteger_base) { throw new Error("Argument out of range"); } if (this._s === 0) { return [ZERO, ZERO]; } if (n === 1 || n === -1) { return [(sign === 1) ? this.abs() : new BigInteger(this._d, sign, CONSTRUCT), ZERO]; } // 2 <= n < BigInteger_base // divide a single digit by a single digit if (this._d.length === 1) { var q = new BigInteger([(this._d[0] / n) | 0], 1, CONSTRUCT); r = new BigInteger([(this._d[0] % n) | 0], 1, CONSTRUCT); if (sign < 0) { q = q.negate(); } if (this._s < 0) { r = r.negate(); } return [q, r]; } var digits = this._d.slice(); var quot = new Array(digits.length); var part = 0; var diff = 0; var i = 0; var guess; while (digits.length) { part = part * BigInteger_base + digits[digits.length - 1]; if (part < n) { quot[i++] = 0; digits.pop(); diff = BigInteger_base * diff + part; continue; } if (part === 0) { guess = 0; } else { guess = (part / n) | 0; } var check = n * guess; diff = part - check; quot[i++] = guess; if (!guess) { digits.pop(); continue; } digits.pop(); part = diff; } r = new BigInteger([diff], 1, CONSTRUCT); if (this._s < 0) { r = r.negate(); } return [new BigInteger(quot.reverse(), sign, CONSTRUCT), r]; }; /* Function: isEven Return true iff *this* is divisible by two. Note that is even. Returns: true if *this* is even, false otherwise. See Also: */ BigInteger.prototype.isEven = function() { var digits = this._d; return this._s === 0 || digits.length === 0 || (digits[0] % 2) === 0; }; /* Function: isOdd Return true iff *this* is not divisible by two. Returns: true if *this* is odd, false otherwise. See Also: */ BigInteger.prototype.isOdd = function() { return !this.isEven(); }; /* Function: sign Get the sign of a . Returns: * -1 if *this* < 0 * 0 if *this* == 0 * +1 if *this* > 0 See Also: , , , , */ BigInteger.prototype.sign = function() { return this._s; }; /* Function: isPositive Return true iff *this* > 0. Returns: true if *this*.compare() == 1. See Also: , , , , , */ BigInteger.prototype.isPositive = function() { return this._s > 0; }; /* Function: isNegative Return true iff *this* < 0. Returns: true if *this*.compare() == -1. See Also: , , , , , */ BigInteger.prototype.isNegative = function() { return this._s < 0; }; /* Function: isZero Return true iff *this* == 0. Returns: true if *this*.compare() == 0. See Also: , , , , */ BigInteger.prototype.isZero = function() { return this._s === 0; }; /* Function: exp10 Multiply a by a power of 10. This is equivalent to, but faster than > if (n >= 0) { > return this.multiply(BigInteger("1e" + n)); > } > else { // n <= 0 > return this.quotient(BigInteger("1e" + -n)); > } Parameters: n - The power of 10 to multiply *this* by. *n* is converted to a javascipt number and must be no greater than (0x7FFFFFFF), or an exception will be thrown. Returns: *this* * (10 ** *n*), truncated to an integer if necessary. See Also: , */ BigInteger.prototype.exp10 = function(n) { n = +n; if (n === 0) { return this; } if (Math.abs(n) > Number(MAX_EXP)) { throw new Error("exponent too large in BigInteger.exp10"); } // Optimization for this == 0. This also keeps us from having to trim zeros in the positive n case if (this._s === 0) { return ZERO; } if (n > 0) { var k = new BigInteger(this._d.slice(), this._s, CONSTRUCT); for (; n >= BigInteger_base_log10; n -= BigInteger_base_log10) { k._d.unshift(0); } if (n == 0) return k; k._s = 1; k = k.multiplySingleDigit(Math.pow(10, n)); return (this._s < 0 ? k.negate() : k); } else if (-n >= this._d.length*BigInteger_base_log10) { return ZERO; } else { var k = new BigInteger(this._d.slice(), this._s, CONSTRUCT); for (n = -n; n >= BigInteger_base_log10; n -= BigInteger_base_log10) { k._d.shift(); } return (n == 0) ? k : k.divRemSmall(Math.pow(10, n))[0]; } }; /* Function: pow Raise a to a power. In this implementation, 0**0 is 1. Parameters: n - The exponent to raise *this* by. *n* must be no greater than (0x7FFFFFFF), or an exception will be thrown. Returns: *this* raised to the *nth* power. See Also: */ BigInteger.prototype.pow = function(n) { if (this.isUnit()) { if (this._s > 0) { return this; } else { return BigInteger(n).isOdd() ? this : this.negate(); } } n = BigInteger(n); if (n._s === 0) { return ONE; } else if (n._s < 0) { if (this._s === 0) { throw new Error("Divide by zero"); } else { return ZERO; } } if (this._s === 0) { return ZERO; } if (n.isUnit()) { return this; } if (n.compareAbs(MAX_EXP) > 0) { throw new Error("exponent too large in BigInteger.pow"); } var x = this; var aux = ONE; var two = BigInteger.small[2]; while (n.isPositive()) { if (n.isOdd()) { aux = aux.multiply(x); if (n.isUnit()) { return aux; } } x = x.square(); n = n.quotient(two); } return aux; }; /* Function: modPow Raise a to a power (mod m). Because it is reduced by a modulus, is not limited by like . Parameters: exponent - The exponent to raise *this* by. Must be positive. modulus - The modulus. Returns: *this* ^ *exponent* (mod *modulus*). See Also: , */ BigInteger.prototype.modPow = function(exponent, modulus) { var result = ONE; var base = this; while (exponent.isPositive()) { if (exponent.isOdd()) { result = result.multiply(base).remainder(modulus); } exponent = exponent.quotient(BigInteger.small[2]); if (exponent.isPositive()) { base = base.square().remainder(modulus); } } return result; }; /* Function: log Get the natural logarithm of a as a native JavaScript number. This is equivalent to > Math.log(this.toJSValue()) but handles values outside of the native number range. Returns: log( *this* ) See Also: */ BigInteger.prototype.log = function() { switch (this._s) { case 0: return -Infinity; case -1: return NaN; default: // Fall through. } var l = this._d.length; if (l*BigInteger_base_log10 < 30) { return Math.log(this.valueOf()); } var N = Math.ceil(30/BigInteger_base_log10); var firstNdigits = this._d.slice(l - N); return Math.log((new BigInteger(firstNdigits, 1, CONSTRUCT)).valueOf()) + (l - N) * Math.log(BigInteger_base); }; /* Function: valueOf Convert a to a native JavaScript integer. This is called automatically by JavaScipt to convert a to a native value. Returns: > parseInt(this.toString(), 10) See Also: , */ BigInteger.prototype.valueOf = function() { return parseInt(this.toString(), 10); }; /* Function: toJSValue Convert a to a native JavaScript integer. This is the same as valueOf, but more explicitly named. Returns: > parseInt(this.toString(), 10) See Also: , */ BigInteger.prototype.toJSValue = function() { return parseInt(this.toString(), 10); }; var MAX_EXP = BigInteger(0x7FFFFFFF); // Constant: MAX_EXP // The largest exponent allowed in and (0x7FFFFFFF or 2147483647). BigInteger.MAX_EXP = MAX_EXP; (function() { function makeUnary(fn) { return function(a) { return fn.call(BigInteger(a)); }; } function makeBinary(fn) { return function(a, b) { return fn.call(BigInteger(a), BigInteger(b)); }; } function makeTrinary(fn) { return function(a, b, c) { return fn.call(BigInteger(a), BigInteger(b), BigInteger(c)); }; } (function() { var i, fn; var unary = "toJSValue,isEven,isOdd,sign,isZero,isNegative,abs,isUnit,square,negate,isPositive,toString,next,prev,log".split(","); var binary = "compare,remainder,divRem,subtract,add,quotient,divide,multiply,pow,compareAbs".split(","); var trinary = ["modPow"]; for (i = 0; i < unary.length; i++) { fn = unary[i]; BigInteger[fn] = makeUnary(BigInteger.prototype[fn]); } for (i = 0; i < binary.length; i++) { fn = binary[i]; BigInteger[fn] = makeBinary(BigInteger.prototype[fn]); } for (i = 0; i < trinary.length; i++) { fn = trinary[i]; BigInteger[fn] = makeTrinary(BigInteger.prototype[fn]); } BigInteger.exp10 = function(x, n) { return BigInteger(x).exp10(n); }; })(); })(); exports.BigInteger = BigInteger; })(typeof exports !== 'undefined' ? exports : this); Agda-2.5.3/src/main/0000755000000000000000000000000013154613124012256 5ustar0000000000000000Agda-2.5.3/src/main/Main.hs0000644000000000000000000000035613154613124013502 0ustar0000000000000000-- | Wrapper for "Agda.Main". -- -- Agda is installed as a library. This module is used to build the -- executable. module Main (main) where import qualified Agda.Main ( main ) import Prelude ( IO ) main :: IO () main = Agda.Main.main Agda-2.5.3/src/agda-mode/0000755000000000000000000000000013154613124013150 5ustar0000000000000000Agda-2.5.3/src/agda-mode/Main.hs0000644000000000000000000001673013154613124014377 0ustar0000000000000000-- | A program which either tries to add setup code for Agda's Emacs -- mode to the users .emacs file, or provides information to Emacs -- about where the Emacs mode is installed. module Main (main) where import Control.Applicative import Control.Exception import Control.Monad import Data.Char import Data.List import Data.Maybe import Data.Version import Numeric import System.Directory import System.Environment import System.Exit import System.FilePath import System.IO import System.Process import Paths_Agda (getDataDir, version) -- | The program. main :: IO () main = do prog <- getProgName args <- getArgs case args of [arg] | arg == locateFlag -> printEmacsModeFile | arg == setupFlag -> do dotEmacs <- findDotEmacs setupDotEmacs (Files { thisProgram = prog , dotEmacs = dotEmacs }) | arg == compileFlag -> compileElispFiles _ -> do inform usage exitFailure -- Command line options. setupFlag = "setup" locateFlag = "locate" compileFlag = "compile" -- | Usage information. usage :: String usage = unlines [ "This program, which is part of Agda version " ++ ver ++ ", can be run" , "in three modes, depending on which option it is invoked with:" , "" , setupFlag , "" , " The program tries to add setup code for Agda's Emacs mode to the" , " current user's .emacs file. It is assumed that the .emacs file" , " uses the character encoding specified by the locale." , "" , locateFlag , "" , " The path to the Emacs mode's main file is printed on standard" , " output (using the UTF-8 character encoding and no trailing" , " newline)." , "" , compileFlag , "" , " The program tries to compile Agda's Emacs mode's source files." , "" , " WARNING: If you reinstall the Agda mode without recompiling the Emacs" , " Lisp files, then Emacs may continue using the old, compiled files." ] -- | The current version of Agda. ver :: String ver = intercalate "." $ map show $ versionBranch version ------------------------------------------------------------------------ -- Locating the Agda mode -- | Prints out the path to the Agda mode's main file (using UTF-8 and -- without any trailing newline). printEmacsModeFile :: IO () printEmacsModeFile = do dataDir <- getDataDir let path = dataDir "emacs-mode" "agda2.el" hSetEncoding stdout utf8 putStr path ------------------------------------------------------------------------ -- Setting up the .emacs file data Files = Files { dotEmacs :: FilePath -- ^ The .emacs file. , thisProgram :: FilePath -- ^ The name of the current program. } -- | Tries to set up the Agda mode in the given .emacs file. setupDotEmacs :: Files -> IO () setupDotEmacs files = do informLn $ "The .emacs file used: " ++ dotEmacs files already <- alreadyInstalled files if already then informLn "It seems as if setup has already been performed." else do appendFile (dotEmacs files) (setupString files) inform $ unlines $ [ "Setup done. Try to (re)start Emacs and open an Agda file." , "The following text was appended to the .emacs file:" ] ++ lines (setupString files) -- | Tries to find the user's .emacs file by querying Emacs. findDotEmacs :: IO FilePath findDotEmacs = askEmacs "(insert (expand-file-name user-init-file))" -- | Has the Agda mode already been set up? alreadyInstalled :: Files -> IO Bool alreadyInstalled files = do exists <- doesFileExist (dotEmacs files) if not exists then return False else withFile (dotEmacs files) ReadMode $ \h -> evaluate . (identifier files `isInfixOf`) =<< hGetContents h -- Uses evaluate to ensure that the file is not closed -- prematurely. -- | If this string occurs in the .emacs file, then it is assumed that -- setup has already been performed. identifier :: Files -> String identifier files = takeFileName (thisProgram files) ++ " " ++ locateFlag -- | The string appended to the end of the .emacs file. setupString :: Files -> String setupString files = unlines [ "" , "(load-file (let ((coding-system-for-read 'utf-8))" , " (shell-command-to-string \"" ++ identifier files ++ "\")))" ] ------------------------------------------------------------------------ -- Querying Emacs -- | Evaluates the given Elisp command using Emacs. The output of the -- command (whatever was written into the current buffer) is returned. -- -- Note: The input is not checked. The input is assumed to come from a -- trusted source. askEmacs :: String -> IO String askEmacs query = do tempDir <- getTemporaryDirectory bracket (openTempFile tempDir "askEmacs") (removeFile . fst) $ \(file, h) -> do hClose h exit <- rawSystem "emacs" [ "--no-desktop", "--no-window-system", "--no-splash" -- Andreas, 2014-01-11: ^ try a leaner startup of emacs , "--eval" , "(with-temp-file " ++ escape file ++ " " ++ query ++ ")" , "--kill" ] unless (exit == ExitSuccess) $ do informLn "Unable to query Emacs." exitFailure withFile file ReadMode $ \h -> do result <- hGetContents h evaluate (length result) -- Uses evaluate to ensure that the file is not closed -- prematurely. return result -- | Escapes the string so that Emacs can parse it as an Elisp string. escape :: FilePath -> FilePath escape s = "\"" ++ concatMap esc s ++ "\"" where esc c | c `elem` ['\\', '"'] = '\\' : [c] | isAscii c && isPrint c = [c] | otherwise = "\\x" ++ showHex (fromEnum c) "\\ " ------------------------------------------------------------------------ -- Compiling Emacs Lisp files -- | The Agda mode's Emacs Lisp files, given in the order in which -- they should be compiled. emacsLispFiles :: [FilePath] emacsLispFiles = [ "agda2-abbrevs.el" , "annotation.el" , "agda2-queue.el" , "eri.el" , "agda2.el" , "agda-input.el" , "agda2-highlight.el" , "agda2-mode.el" ] -- | Tries to compile the Agda mode's Emacs Lisp files. compileElispFiles :: IO () compileElispFiles = do dataDir <- ( "emacs-mode") <$> getDataDir let elFiles = map (dataDir ) emacsLispFiles elFiles <- filterM doesFileExist elFiles results <- mapM (compile dataDir) elFiles case catMaybes results of [] -> return () fs -> do informLn "Unable to compile the following Emacs Lisp files:" mapM_ (informLn . (" " ++)) fs exitFailure where compile dataDir f = do exit <- rawSystem "emacs" $ [ "--no-init-file", "--no-site-file" , "--directory", dataDir , "--batch" , "--eval" , "(progn \ \(setq byte-compile-error-on-warn t) \ \(byte-compile-disable-warning 'cl-functions) \ \(batch-byte-compile))" , f ] return $ if exit == ExitSuccess then Nothing else Just f ------------------------------------------------------------------------ -- Helper functions -- These functions inform the user about something by printing on -- stderr. inform = hPutStr stderr informLn = hPutStrLn stderr Agda-2.5.3/src/full/0000755000000000000000000000000013154613124012274 5ustar0000000000000000Agda-2.5.3/src/full/undefined.h0000644000000000000000000000057013154613124014410 0ustar0000000000000000#define __IMPOSSIBLE__ (throwImpossible (Impossible __FILE__ __LINE__)) #define __IMPOSSIBLE_TERM__ (impossibleTerm __FILE__ __LINE__) #define __IMPOSSIBLE_VERBOSE__ (\ s -> do { reportSLn "impossible" 10 s ; __IMPOSSIBLE__ }) #define __UNREACHABLE__ (throwImpossible (Unreachable __FILE__ __LINE__)) #define __CRASH_WHEN__ (\ k n -> whenExactVerbosity k n __UNREACHABLE__) Agda-2.5.3/src/full/Agda/0000755000000000000000000000000013154613124013130 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Version.hs0000644000000000000000000000036213154613124015112 0ustar0000000000000000module Agda.Version where import Data.Version import Data.List ( intercalate, map ) import qualified Paths_Agda as PA -- | The version of Agda. version :: String version = intercalate "." $ map show $ versionBranch PA.version Agda-2.5.3/src/full/Agda/TheTypeChecker.hs0000644000000000000000000000026213154613124016333 0ustar0000000000000000module Agda.TheTypeChecker ( checkDecls, checkDecl, checkDeclCached , inferExpr, checkExpr ) where import Agda.TypeChecking.Rules.Decl import Agda.TypeChecking.Rules.Term Agda-2.5.3/src/full/Agda/ImpossibleTest.hs0000644000000000000000000000024113154613124016427 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.ImpossibleTest where #include "undefined.h" import Agda.Utils.Impossible impossibleTest :: a impossibleTest = __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/VersionCommit.hs0000644000000000000000000000131313154613124016260 0ustar0000000000000000{-# LANGUAGE TemplateHaskell #-} module Agda.VersionCommit where import Development.GitRev import Agda.Version versionWithCommitInfo :: String versionWithCommitInfo = version ++ case commitInfo of Nothing -> "" Just info -> "-" ++ info -- | Information about current git commit, generated at compile time commitInfo :: Maybe String commitInfo = case $(gitHash) of "UNKNOWN" -> Nothing hash -> Just $ abbrev hash ++ dirty where -- | Check if there are uncommitted changes dirty | $(gitDirty) = "-dirty" | otherwise = "" -- | Abbreviate a commit hash while keeping it unambiguous abbrev = take 7 Agda-2.5.3/src/full/Agda/Benchmarking.hs0000644000000000000000000000756613154613124016072 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE IncoherentInstances #-} {-# LANGUAGE NoMonomorphismRestriction #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE OverlappingInstances #-} #endif {-# OPTIONS_GHC -fno-warn-orphans #-} -- | Agda-specific benchmarking structure. module Agda.Benchmarking where import qualified Control.Exception as E import Data.IORef import System.IO.Unsafe import Agda.Syntax.Concrete.Name (TopLevelModuleName) import Agda.Syntax.Concrete.Pretty import Agda.Syntax.Abstract.Name import Agda.Utils.Benchmark (MonadBench(..)) import qualified Agda.Utils.Benchmark as B import Agda.Utils.Null import Agda.Utils.Pretty -- | Phases to allocate CPU time to. data Phase = Parsing -- ^ Happy parsing and operator parsing. | Import -- ^ Import chasing. | Deserialization -- ^ Reading interface files. | Scoping -- ^ Scope checking and translation to abstract syntax. | Typing -- ^ Type checking and translation to internal syntax. | Termination -- ^ Termination checking. | Positivity -- ^ Positivity checking and polarity computation. | Injectivity -- ^ Injectivity checking. | ProjectionLikeness -- ^ Checking for projection likeness. | Coverage -- ^ Coverage checking and compilation to case trees. | Highlighting -- ^ Generating highlighting info. | Serialization -- ^ Writing interface files. | DeadCode -- ^ Deac code elimination. | Graph -- ^ Subphase for 'Termination'. | RecCheck -- ^ Subphase for 'Termination'. | Reduce -- ^ Subphase for 'Termination'. | Level -- ^ Subphase for 'Termination'. | Compare -- ^ Subphase for 'Termination'. | With -- ^ Subphase for 'Termination'. | ModuleName -- ^ Subphase for 'Import'. | BuildInterface -- ^ Subphase for 'Serialization'. | Sort -- ^ Subphase for 'Serialization'. | BinaryEncode -- ^ Subphase for 'Serialization'. | Compress -- ^ Subphase for 'Serialization'. | OperatorsExpr -- ^ Subphase for 'Parsing'. | OperatorsPattern -- ^ Subphase for 'Parsing'. | Free -- ^ Subphase for 'Typing': free variable computation. | OccursCheck -- ^ Subphase for 'Typing': occurs check for solving metas. | CheckLHS -- ^ Subphase for 'Typing': checking the LHS | CheckRHS -- ^ Subphase for 'Typing': checking the RHS | TypeSig -- ^ Subphase for 'Typing': checking a type signature | UnifyIndices -- ^ Subphase for 'CheckLHS': unification of the indices | InverseScopeLookup -- ^ Pretty printing names. | TopModule TopLevelModuleName | Definition QName deriving (Eq, Ord, Show) instance Pretty Phase where pretty (TopModule m) = pretty m pretty (Definition q) = pretty q pretty a = text (show a) type Benchmark = B.Benchmark Phase type Account = B.Account Phase isModuleAccount :: Account -> Bool isModuleAccount [] = True isModuleAccount (TopModule{} : _) = True isModuleAccount _ = False isDefAccount :: Account -> Bool isDefAccount [] = True isDefAccount (Definition{} : _) = True isDefAccount _ = False isInternalAccount :: Account -> Bool isInternalAccount (TopModule{} : _) = False isInternalAccount (Definition{} : _) = False isInternalAccount _ = True -- * Benchmarking in the IO monad. -- | Global variable to store benchmark statistics. {-# NOINLINE benchmarks #-} benchmarks :: IORef Benchmark benchmarks = unsafePerformIO $ newIORef empty instance MonadBench Phase IO where getBenchmark = readIORef benchmarks putBenchmark = writeIORef benchmarks finally = E.finally -- | Benchmark an IO computation and bill it to the given account. billToIO :: Account -> IO a -> IO a billToIO = B.billTo -- | Benchmark a pure computation and bill it to the given account. billToPure :: Account -> a -> a billToPure acc a = unsafePerformIO $ billToIO acc $ return a Agda-2.5.3/src/full/Agda/Main.hs0000644000000000000000000001633213154613124014355 0ustar0000000000000000{-# LANGUAGE CPP #-} {-| Agda main module. -} module Agda.Main where import Control.Monad.State import Control.Applicative import Data.Maybe import System.Environment import System.Exit import System.Console.GetOpt import Agda.Syntax.Position (Range) import Agda.Syntax.Concrete.Pretty () import Agda.Syntax.Abstract.Name (toTopLevelModuleName) import Agda.Interaction.CommandLine import Agda.Interaction.Options import Agda.Interaction.Monad import Agda.Interaction.EmacsTop (mimicGHCi) import Agda.Interaction.Imports (MaybeWarnings'(..)) import qualified Agda.Interaction.Imports as Imp import qualified Agda.Interaction.Highlighting.Dot as Dot import qualified Agda.Interaction.Highlighting.LaTeX as LaTeX import Agda.Interaction.Highlighting.HTML import Agda.TypeChecking.Monad import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TypeChecking.Errors import Agda.TypeChecking.Warnings import Agda.TypeChecking.Pretty import Agda.Compiler.Common (IsMain (..)) import Agda.Compiler.MAlonzo.Compiler (ghcBackend) import Agda.Compiler.JS.Compiler (jsBackend) import Agda.Compiler.Backend import Agda.Utils.Lens import Agda.Utils.Monad import Agda.Utils.String import Agda.VersionCommit import qualified Agda.Utils.Benchmark as UtilsBench import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.Impossible import Agda.Utils.Lens #include "undefined.h" builtinBackends :: [Backend] builtinBackends = [ ghcBackend, jsBackend ] -- | The main function runAgda :: [Backend] -> IO () runAgda backends = runAgda' $ builtinBackends ++ backends runAgda' :: [Backend] -> IO () runAgda' backends = runTCMPrettyErrors $ do progName <- liftIO getProgName argv <- liftIO getArgs opts <- liftIO $ runOptM $ parseBackendOptions backends argv case opts of Left err -> liftIO $ optionError err Right (bs, opts) -> do stBackends .= bs let enabled (Backend b) = isEnabled b (options b) bs' = filter enabled bs () <$ runAgdaWithOptions backends generateHTML (interaction bs') progName opts where interaction bs = backendInteraction bs $ defaultInteraction opts defaultInteraction :: CommandLineOptions -> TCM (Maybe Interface) -> TCM () defaultInteraction opts | i = runIM . interactionLoop | ghci = mimicGHCi . (failIfInt =<<) | otherwise = (() <$) where i = optInteractive opts ghci = optGHCiInteraction opts failIfInt Nothing = return () failIfInt (Just _) = __IMPOSSIBLE__ -- | Run Agda with parsed command line options and with a custom HTML generator runAgdaWithOptions :: [Backend] -- ^ Backends only for printing usage and version information -> TCM () -- ^ HTML generating action -> (TCM (Maybe Interface) -> TCM a) -- ^ Backend interaction -> String -- ^ program name -> CommandLineOptions -- ^ parsed command line options -> TCM (Maybe a) runAgdaWithOptions backends generateHTML interaction progName opts | optShowHelp opts = Nothing <$ liftIO (printUsage backends) | optShowVersion opts = Nothing <$ liftIO (printVersion backends) | isNothing (optInputFile opts) && not (optInteractive opts) && not (optGHCiInteraction opts) = Nothing <$ liftIO (printUsage backends) | otherwise = do -- Main function. -- Bill everything to root of Benchmark trie. UtilsBench.setBenchmarking UtilsBench.BenchmarkOn -- Andreas, Nisse, 2016-10-11 AIM XXIV -- Turn benchmarking on provisionally, otherwise we lose track of time spent -- on e.g. LaTeX-code generation. -- Benchmarking might be turned off later by setCommandlineOptions Bench.billTo [] checkFile `finally_` do -- Print benchmarks. Bench.print -- Print accumulated statistics. printStatistics 20 Nothing =<< use lensAccumStatistics where checkFile = Just <$> do when (optInteractive opts) $ liftIO $ putStr splashScreen interaction $ do setCommandLineOptions opts hasFile <- hasInputFile -- Andreas, 2013-10-30 The following 'resetState' kills the -- verbosity options. That does not make sense (see fail/Issue641). -- 'resetState' here does not seem to serve any purpose, -- thus, I am removing it. -- resetState if not hasFile then return Nothing else do let mode = if optOnlyScopeChecking opts then Imp.ScopeCheck else Imp.TypeCheck file <- getInputFile (i, mw) <- Imp.typeCheckMain file mode -- An interface is only generated if the mode is -- Imp.TypeCheck and there are no warnings. result <- case (mode, mw) of (Imp.ScopeCheck, _) -> return Nothing (_, NoWarnings) -> return $ Just i (_, SomeWarnings ws) -> do ws' <- applyFlagsToTCWarnings RespectFlags ws case ws' of [] -> return Nothing cuws -> tcWarningsToError cuws reportSDoc "main" 50 $ pretty i whenM (optGenerateHTML <$> commandLineOptions) $ generateHTML whenM (isJust . optDependencyGraph <$> commandLineOptions) $ Dot.generateDot $ i whenM (optGenerateLaTeX <$> commandLineOptions) $ LaTeX.generateLaTeX i -- Print accumulated warnings ws <- (snd . classifyWarnings) <$> Imp.getAllWarnings' AllWarnings RespectFlags unless (null ws) $ do let banner = text $ "\n" ++ delimiter "All done; warnings encountered" reportSDoc "warning" 1 $ vcat $ punctuate (text "\n") $ banner : (prettyTCM <$> ws) return result -- | Print usage information. printUsage :: [Backend] -> IO () printUsage backends = do progName <- getProgName putStr $ usage standardOptions_ progName mapM_ (putStr . backendUsage) backends backendUsage :: Backend -> String backendUsage (Backend b) = usageInfo ("\n" ++ backendName b ++ " backend options") $ map (fmap $ const ()) (commandLineFlags b) -- | Print version information. printVersion :: [Backend] -> IO () printVersion backends = do putStrLn $ "Agda version " ++ versionWithCommitInfo mapM_ putStrLn [ " - " ++ name ++ " backend version " ++ ver | Backend Backend'{ backendName = name, backendVersion = Just ver } <- backends ] -- | What to do for bad options. optionError :: String -> IO () optionError err = do prog <- getProgName putStrLn $ "Error: " ++ err ++ "\nRun '" ++ prog ++ " --help' for help on command line options." exitFailure -- | Run a TCM action in IO; catch and pretty print errors. runTCMPrettyErrors :: TCM () -> IO () runTCMPrettyErrors tcm = do r <- runTCMTop $ tcm `catchError` \err -> do s2s <- prettyTCWarnings' =<< Imp.errorWarningsOfTCErr err s1 <- prettyError err let ss = filter (not . null) $ s2s ++ [s1] unless (null s1) (liftIO $ putStr $ unlines ss) throwError err case r of Right _ -> exitSuccess Left _ -> exitFailure `catchImpossible` \e -> do putStr $ show e exitFailure -- | Main main :: IO () main = runAgda [] Agda-2.5.3/src/full/Agda/Auto/0000755000000000000000000000000013154613124014040 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Auto/Auto.hs0000644000000000000000000005547613154613124015325 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Auto.Auto (auto , AutoResult(..) , AutoProgress(..) ) where import Prelude hiding (null) #if __GLASGOW_HASKELL__ <= 708 import Control.Applicative ( (<$), pure ) #endif import Data.Functor import Control.Monad.State import qualified Data.List as List import qualified Data.Map as Map import Data.IORef import qualified System.Timeout import Data.Maybe import qualified Data.Traversable as Trav import Agda.Utils.Permutation (permute, takeP) import Agda.TypeChecking.Monad hiding (withCurrentModule) import Agda.TypeChecking.Telescope import Agda.Syntax.Common (Hiding(..)) import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Pretty (prettyA) import qualified Text.PrettyPrint as PP import qualified Agda.TypeChecking.Pretty as TCM import Agda.Syntax.Position import qualified Agda.Syntax.Internal as I import Agda.Syntax.Translation.InternalToAbstract import Agda.Syntax.Translation.AbstractToConcrete (abstractToConcreteEnv, abstractToConcrete_, makeEnv, runAbsToCon, toConcrete) import Agda.Interaction.BasicOps hiding (refine) import Agda.TypeChecking.Reduce (normalise) import Agda.Syntax.Common import qualified Agda.Syntax.Scope.Base as Scope import Agda.Syntax.Scope.Monad (withCurrentModule) import qualified Agda.Syntax.Abstract.Name as AN import qualified Agda.TypeChecking.Monad.Base as TCM import Agda.TypeChecking.EtaContract (etaContract) import qualified Agda.Utils.HashMap as HMap import Agda.Auto.Options import Agda.Auto.Convert import Agda.Auto.NarrowingSearch import Agda.Auto.Syntax import Agda.Auto.SearchControl import Agda.Auto.Typecheck import Agda.Auto.CaseSplit import Agda.Utils.Except ( runExceptT, MonadError(catchError) ) import Agda.Utils.Functor import Agda.Utils.Impossible import Agda.Utils.Lens import Agda.Utils.Maybe import Agda.Utils.Null import Agda.Utils.Tuple #include "undefined.h" insertAbsurdPattern :: String -> String insertAbsurdPattern [] = [] insertAbsurdPattern s@(_:_) | take (length abspatvarname) s == abspatvarname = "()" ++ drop (length abspatvarname) s insertAbsurdPattern (c:s) = c : insertAbsurdPattern s getHeadAsHint :: A.Expr -> Maybe Hint getHeadAsHint (A.ScopedExpr _ e) = getHeadAsHint e getHeadAsHint (A.Def qname) = Just $ Hint False qname getHeadAsHint (A.Proj _ qname) = Just $ Hint False $ head $ I.unAmbQ qname getHeadAsHint (A.Con qname) = Just $ Hint True $ head $ I.unAmbQ qname getHeadAsHint _ = Nothing -- | Result type: Progress & potential Message for the user -- -- The of the Auto tactic can be one of the following three: -- -- 1. @Solutions [(ii,s)]@ -- A list of solutions @s@ for interaction ids @ii@. -- In particular, @Solutions []@ means Agsy found no solution. -- -- 2. @FunClauses cs@ -- A list of clauses for the interaction id @ii@ in which Auto -- was invoked with case-splitting turned on. -- -- 3. @Refinement s@ -- A refinement for the interaction id @ii@ in which Auto was invoked. data AutoProgress = Solutions [(InteractionId, String)] | FunClauses [String] | Refinement String data AutoResult = AutoResult { autoProgress :: AutoProgress , autoMessage :: Maybe String } stopWithMsg :: String -> TCM AutoResult stopWithMsg msg = return $ AutoResult (Solutions []) (Just msg) -- | Entry point for Auto tactic (Agsy). -- -- If the @autoMessage@ part of the result is set to @Just msg@, the -- message @msg@ produced by Agsy should be displayed to the user. auto :: InteractionId -> Range -> String -> TCM AutoResult auto ii rng argstr = do -- Parse hints and other configuration. let autoOptions = parseArgs argstr let hints = autoOptions ^. aoHints let timeout = autoOptions ^. aoTimeOut let pick = autoOptions ^. aoPick let mode = autoOptions ^. aoMode let hintmode = autoOptions ^. aoHintMode ahints <- case mode of MRefine{} -> return [] _ -> mapM (parseExprIn ii rng) hints let failHints = stopWithMsg "Hints must be a list of constant names" eqstuff <- getEqCombinators ii rng caseMaybe (mapM getHeadAsHint ahints) failHints $ \ ehints -> do -- Get the meta variable for the interaction point we are trying to fill. -- Add the @autohints@ for that meta to the hints collection. mi <- lookupInteractionId ii thisdefinfo <- findClauseDeep ii ehints <- (ehints ++) <$> do autohints hintmode mi $ fmap fst3 thisdefinfo -- If @thisdefinfo /= Nothing@ get the its type (normalized). mrectyp <- maybeToList <$> do Trav.forM thisdefinfo $ \ (def, _, _) -> do normalise =<< do TCM.defType <$> getConstInfo def (myhints', mymrectyp, tccons, eqcons, cmap) <- tomy mi (ehints ++ eqstuff) mrectyp let (myhints, c1to6) = splitAt (length myhints' - length eqstuff) myhints' meqr = ifNull eqstuff Nothing $ \ _ -> {- else -} let [c1, c2, c3, c4, c5, c6] = c1to6 in Just $ EqReasoningConsts c1 c2 c3 c4 c5 c6 let tcSearchSC isdep ctx typ trm = caseMaybe meqr a $ \ eqr -> mpret $ Sidecondition (calcEqRState eqr trm) a where a = tcSearch isdep ctx typ trm let (mainm, _, _, _) = tccons Map.! mi case mode of MNormal listmode disprove -> do let numsols = if listmode then 10 else 1 -- Andreas, 2015-05-17 Issue 1504: -- wish to produce several solutions, as -- the first one might be ill-typed. -- However, currently changing the 1 to something higher makes Agsy loop. sols <- liftIO $ newIORef ([] :: [[I.Term]]) nsol <- liftIO $ newIORef $ pick + numsols let hsol = do nsol' <- readIORef nsol let cond = nsol' <= numsols when cond $ do trms <- runExceptT $ mapM (\ (m , _, _, _) -> convert (Meta m) :: MOT I.Term) $ Map.elems tccons case trms of Left{} -> writeIORef nsol $! nsol' + 1 Right trms -> modifyIORef sols (trms :) -- Right trms -> if listmode then modifyIORef sols (trms :) -- else writeIORef sols [trms] ticks <- liftIO $ newIORef 0 let exsearch initprop recinfo defdfv = liftIO $ System.Timeout.timeout (getTimeOut timeout * 1000000) $ loop 0 where loop d = do let rechint x = case recinfo of Nothing -> x Just (_, recdef) -> (recdef, HMRecCall) : x env = RIEnv { rieHints = rechint $ map (,HMNormal) myhints , rieDefFreeVars = defdfv , rieEqReasoningConsts = meqr } depreached <- topSearch ticks nsol hsol env (initprop) d costIncrease nsol' <- readIORef nsol if nsol' /= 0 && depreached then loop (d + costIncrease) else return depreached let getsols sol = do exprs <- forM (zip (Map.keys tccons) sol) $ \ (mi, e) -> do mv <- lookupMeta mi e <- etaContract e expr <- modifyAbstractExpr <$> do withMetaInfo (getMetaInfo mv) $ reify e return (mi, expr) let loop :: I.MetaId -> StateT [I.MetaId] TCM [(I.MetaId, A.Expr)] loop midx = do let (m, _, _, deps) = tccons Map.! midx asolss <- mapM loop deps dones <- get asols <- if midx `elem` dones then return [] else do put (midx : dones) return [(midx, fromMaybe __IMPOSSIBLE__ $ lookup midx exprs)] return $ concat asolss ++ asols (asols, _) <- runStateT (loop mi) [] return asols if disprove then case eqcons of [] -> case Map.elems tccons of (m, mytype, mylocalVars, _) : [] -> do defdfv <- case thisdefinfo of Just (def, _, _) -> getdfv mi def Nothing -> return 0 ee <- liftIO $ newIORef $ ConstDef {cdname = "T", cdorigin = __IMPOSSIBLE__, cdtype = NotM $ Sort (Set 0), cdcont = Postulate, cddeffreevars = 0} let (restargs, modargs) = splitAt (length mylocalVars - defdfv) mylocalVars mytype' = foldl (\x y -> NotM $ Pi Nothing NotHidden (freeIn 0 y) y (Abs NoId x)) mytype restargs htyp = negtype ee mytype' sctx = (Id "h", closify htyp) : map (\x -> (NoId, closify x)) modargs ntt = closify (NotM $ App Nothing (NotM OKVal) (Const ee) (NotM ALNil)) res <- exsearch (tcSearchSC False sctx ntt (Meta m)) Nothing defdfv rsols <- liftM reverse $ liftIO $ readIORef sols if null rsols then do nsol' <- liftIO $ readIORef nsol stopWithMsg $ insuffsols (pick + numsols - nsol') else do aexprss <- mapM getsols rsols cexprss <- forM aexprss $ mapM $ \(mi, e) -> do mv <- lookupMeta mi withMetaInfo (getMetaInfo mv) $ do (mi,) <$> abstractToConcrete_ e let ss = dropWhile (== ' ') . dropWhile (/= ' ') . show disp [(_, cexpr)] = ss cexpr disp cexprs = concat $ map (\ (mi, cexpr) -> ss cexpr ++ " ") cexprs ticks <- liftIO $ readIORef ticks stopWithMsg $ unlines $ ("Listing disproof(s) " ++ show pick ++ "-" ++ show (pick + length rsols - 1)) : for (zip cexprss [pick..]) (\ (x, y) -> show y ++ " " ++ disp x) _ -> stopWithMsg "Metavariable dependencies not allowed in disprove mode" _ -> stopWithMsg "Metavariable dependencies not allowed in disprove mode" else do (recinfo, defdfv) <- case thisdefinfo of Just (def, clause, _) -> do let [rectyp'] = mymrectyp defdfv <- getdfv mi def myrecdef <- liftIO $ newIORef $ ConstDef {cdname = "", cdorigin = (Nothing, def), cdtype = rectyp', cdcont = Postulate, cddeffreevars = defdfv} (_, pats) <- constructPats cmap mi clause defdfv <- getdfv mi def return $ if contains_constructor pats then (Just (pats, myrecdef), defdfv) else (Nothing, defdfv) Nothing -> return (Nothing, 0) let tc (m, mytype, mylocalVars) isdep = tcSearchSC isdep (map (\x -> (NoId, closify x)) mylocalVars) (closify mytype) (Meta m) initprop = foldl (\x (ineq, e, i) -> mpret $ And Nothing x (comp' ineq (closify e) (closify i))) (foldl (\x (m, mt, mlv, _) -> if hequalMetavar m mainm then case recinfo of Just (recpats, recdef) -> mpret $ Sidecondition (localTerminationSidecond (localTerminationEnv recpats) recdef (Meta m)) (tc (m, mt, mlv) False) Nothing -> mpret $ And Nothing x (tc (m, mt, mlv) False) else mpret $ And Nothing x (tc (m, mt, mlv) True) ) (mpret OK) (Map.elems tccons) ) eqcons res <- exsearch initprop recinfo defdfv riis <- map swap <$> getInteractionIdsAndMetas let timeoutString | isNothing res = " after timeout (" ++ show timeout ++ "s)" | otherwise = "" if listmode then do rsols <- liftM reverse $ liftIO $ readIORef sols if null rsols then do nsol' <- liftIO $ readIORef nsol stopWithMsg $ insuffsols (pick + numsols - nsol') ++ timeoutString else do aexprss <- mapM getsols rsols -- cexprss <- mapM (mapM (\(mi, e) -> lookupMeta mi >>= \mv -> withMetaInfo (getMetaInfo mv) $ abstractToConcrete_ e >>= \e' -> return (mi, e'))) aexprss cexprss <- forM aexprss $ do mapM $ \ (mi, e) -> do mv <- lookupMeta mi withMetaInfo (getMetaInfo mv) $ do e' <- abstractToConcrete_ e return (mi, e') let disp [(_, cexpr)] = show cexpr disp cexprs = concat $ for cexprs $ \ (mi, cexpr) -> maybe (show mi) show (lookup mi riis) ++ " := " ++ show cexpr ++ " " ticks <- liftIO $ readIORef ticks stopWithMsg $ "Listing solution(s) " ++ show pick ++ "-" ++ show (pick + length rsols - 1) ++ timeoutString ++ "\n" ++ unlines (map (\(x, y) -> show y ++ " " ++ disp x) $ zip cexprss [pick..]) else {- not listmode -} case res of Nothing -> do nsol' <- liftIO $ readIORef nsol stopWithMsg $ insuffsols (pick + numsols - nsol') ++ timeoutString Just depthreached -> do ticks <- liftIO $ readIORef ticks rsols <- liftIO $ readIORef sols case rsols of [] -> do nsol' <- liftIO $ readIORef nsol stopWithMsg $ insuffsols (pick + numsols - nsol') terms -> loop terms where -- Andreas, 2015-05-17 Issue 1504 -- If giving a solution failed (e.g. ill-typed) -- we could try the next one. -- However, currently @terms@ is always a singleton list. -- Thus, the following @loop@ is not doing something very -- meaningful. loop :: [[I.Term]] -> TCM AutoResult loop [] = return $ AutoResult (Solutions []) (Just "") loop (term : terms') = do -- On exception, try next solution flip catchError (const $ loop terms') $ do exprs <- getsols term reportSDoc "auto" 20 $ TCM.text "Trying solution " TCM.<+> TCM.prettyTCM exprs giveress <- forM exprs $ \ (mi, expr0) -> do let expr = killRange expr0 case lookup mi riis of Nothing -> -- catchError (giveExpr WithoutForce Nothing mi expr >> return (Nothing, Nothing)) -- (const retry) -- (\_ -> return (Nothing, Just ("Failed to give expr for side solution of " ++ show mi))) Just ii' -> do ae <- give WithoutForce ii' Nothing expr mv <- lookupMeta mi let scope = getMetaScope mv ce <- abstractToConcreteEnv (makeEnv scope) ae let cmnt = if ii' == ii then agsyinfo ticks else "" return (Just (ii', show ce ++ cmnt), Nothing) -- Andreas, 2015-05-17, Issue 1504 -- When Agsy produces an ill-typed solution, return nothing. -- TODO: try other solution. -- `catchError` const retry -- (return (Nothing, Nothing)) let msg = if length exprs == 1 then Nothing else Just $ "Also gave solution(s) for hole(s)" ++ concatMap (\(mi', _) -> if mi' == mi then "" else (" " ++ case lookup mi' riis of {Nothing -> show mi'; Just ii -> show ii}) ) exprs let msgs = catMaybes $ msg : map snd giveress msg' = unlines msgs <$ guard (not $ null msgs) return $ AutoResult (Solutions $ catMaybes $ map fst giveress) msg' MCaseSplit -> do case thisdefinfo of Just (def, clause, True) -> case Map.elems tccons of [(m, mytype, mylocalVars, _)] | null eqcons -> do (ids, pats) <- constructPats cmap mi clause let ctx = map (\((hid, id), t) -> HI hid (id, t)) (zip ids mylocalVars) ticks <- liftIO $ newIORef 0 let [rectyp'] = mymrectyp defdfv <- getdfv mi def myrecdef <- liftIO $ newIORef $ ConstDef {cdname = "", cdorigin = (Nothing, def), cdtype = rectyp', cdcont = Postulate, cddeffreevars = defdfv} sols <- liftIO $ System.Timeout.timeout (getTimeOut timeout * 1000000) ( let r d = do sols <- liftIO $ caseSplitSearch ticks __IMPOSSIBLE__ myhints meqr __IMPOSSIBLE__ d myrecdef ctx mytype pats case sols of [] -> r (d + costIncrease) (_:_) -> return sols in r 0) case sols of Just (cls : _) -> withInteractionId ii $ do cls' <- liftIO $ runExceptT (mapM frommyClause cls) case cls' of Left{} -> stopWithMsg "No solution found" Right cls' -> do cls'' <- forM cls' $ \ (I.Clause _ _ tel ps body t catchall reachable) -> do withCurrentModule (AN.qnameModule def) $ do -- Normalise the dot patterns ps <- addContext tel $ normalise ps body <- etaContract body liftM modifyAbstractClause $ inTopContext $ reify $ AN.QNamed def $ I.Clause noRange noRange tel ps body t catchall reachable moduleTel <- lookupSection (AN.qnameModule def) pcs <- withInteractionId ii $ inTopContext $ addContext moduleTel $ mapM prettyA cls'' ticks <- liftIO $ readIORef ticks return $ AutoResult (FunClauses $ map (insertAbsurdPattern . PP.renderStyle (PP.style { PP.mode = PP.OneLineMode })) pcs) Nothing Just [] -> stopWithMsg "No solution found" -- case not possible at the moment because case split doesnt care about search exhaustiveness Nothing -> stopWithMsg $ "No solution found at time out (" ++ show timeout ++ "s)" _ -> stopWithMsg "Metavariable dependencies not allowed in case split mode" _ -> stopWithMsg "Metavariable is not at top level of clause RHS" MRefine listmode -> do mv <- lookupMeta mi let tt = jMetaType $ mvJudgement mv minfo = getMetaInfo mv targettyp <- withMetaInfo minfo $ do vs <- getContextArgs targettype <- tt `piApplyM` permute (takeP (length vs) $ mvPermutation mv) vs normalise targettype let tctx = length $ envContext $ clEnv minfo hits <- if elem "-a" hints then do st <- liftTCM $ join $ pureTCM $ \st _ -> return st let defs = st^.stSignature.sigDefinitions idefs = st^.stImports.sigDefinitions alldefs = HMap.keys defs ++ HMap.keys idefs liftM catMaybes $ mapM (\n -> case thisdefinfo of Just (def, _, _) | def == n -> return Nothing _ -> do cn <- withMetaInfo minfo $ runAbsToCon $ toConcrete n if head (show cn) == '.' then -- not in scope return Nothing else do c <- getConstInfo n ctyp <- normalise $ defType c cdfv <- withMetaInfo minfo $ getDefFreeVars n return $ case matchType cdfv tctx ctyp targettyp of Nothing -> Nothing Just score -> Just (show cn, score) ) alldefs else do let scopeinfo = clScope (getMetaInfo mv) namespace = Scope.everythingInScope scopeinfo names = Scope.nsNames namespace qnames = map (\(x, y) -> (x, Scope.anameName $ head y)) $ Map.toList names modnames = case thisdefinfo of Just (def, _, _) -> filter (\(_, n) -> n /= def) qnames Nothing -> qnames liftM catMaybes $ mapM (\(cn, n) -> do c <- getConstInfo n ctyp <- normalise $ defType c cdfv <- withMetaInfo minfo $ getDefFreeVars n return $ case matchType cdfv tctx ctyp targettyp of Nothing -> Nothing Just score -> Just (show cn, score) ) modnames let sorthits = List.sortBy (\(_, (pa1, pb1)) (_, (pa2, pb2)) -> case compare pa2 pa1 of {EQ -> compare pb1 pb2; o -> o}) hits if listmode || pick == (-1) then let pick' = max 0 pick in if pick' >= length sorthits then stopWithMsg $ insuffcands $ length sorthits else let showhits = take 10 $ drop pick' sorthits in stopWithMsg $ "Listing candidate(s) " ++ show pick' ++ "-" ++ show (pick' + length showhits - 1) ++ " (found " ++ show (length sorthits) ++ " in total)\n" ++ unlines (map (\(i, (cn, _)) -> show i ++ " " ++ cn) (zip [pick'..pick' + length showhits - 1] showhits)) else if pick >= length sorthits then stopWithMsg $ insuffcands $ length sorthits else return $ AutoResult (Refinement $ fst $ sorthits !! pick) Nothing where agsyinfo ticks = "" -- Get the functions and axioms defined in the same module as @def@. autohints :: AutoHintMode -> I.MetaId -> Maybe AN.QName -> TCM [Hint] autohints AHMModule mi (Just def) = do scope <- clScope . getMetaInfo <$> lookupMeta mi let names = Scope.nsNames $ Scope.everythingInScope scope qnames = map (Scope.anameName . head) $ Map.elems names modnames = filter (\n -> AN.qnameModule n == AN.qnameModule def && n /= def) qnames map (Hint False) <$> do (`filterM` modnames) $ \ n -> do c <- getConstInfo n case theDef c of Axiom{} -> return True AbstractDefn{} -> return True Function{} -> return True _ -> return False autohints _ _ _ = return [] -- | Names for the equality reasoning combinators -- Empty if any of these names is not defined. getEqCombinators :: InteractionId -> Range -> TCM [Hint] getEqCombinators ii rng = do let eqCombinators = ["_≡_", "begin_", "_≡⟨_⟩_", "_∎", "sym", "cong"] raw <- mapM (parseExprIn ii rng) eqCombinators `catchError` const (pure []) return $ fromMaybe [] $ mapM getHeadAsHint raw -- | Templates for error messages genericNotEnough :: String -> Int -> String genericNotEnough str n = List.intercalate " " $ case n of 0 -> [ "No" , str, "found"] 1 -> [ "Only 1", str, "found" ] _ -> [ "Only", show n, str ++ "s", "found" ] insuffsols :: Int -> String insuffsols = genericNotEnough "solution" insuffcands :: Int -> String insuffcands = genericNotEnough "candidate" Agda-2.5.3/src/full/Agda/Auto/CaseSplit.hs0000644000000000000000000005701313154613124016271 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE UndecidableInstances #-} module Agda.Auto.CaseSplit where #if __GLASGOW_HASKELL__ <= 708 import Control.Applicative ( (<$>), (<*>), pure ) #endif import Data.IORef import Data.Tuple (swap) import Data.List (findIndex, union) import Data.Monoid ((<>), Sum(..)) import Data.Foldable (foldMap) import qualified Data.Set as Set import qualified Data.IntMap as IntMap import Control.Monad.State as St hiding (lift) import Control.Monad.Reader as Rd hiding (lift) import qualified Control.Monad.State as St import Data.Function import Agda.Syntax.Common (Hiding(..)) import Agda.Auto.NarrowingSearch import Agda.Auto.Syntax import Agda.Auto.SearchControl import Agda.Auto.Typecheck #include "undefined.h" import Agda.Utils.Impossible import Agda.Utils.Monad (or2M) abspatvarname :: String abspatvarname = "\0absurdPattern" costCaseSplitVeryHigh, costCaseSplitHigh, costCaseSplitLow, costAddVarDepth :: Cost costCaseSplitVeryHigh = 10000 costCaseSplitHigh = 5000 costCaseSplitLow = 2000 costAddVarDepth = 1000 data HI a = HI Hiding a drophid :: [HI a] -> [a] drophid = map (\(HI _ x) -> x) type CSPat o = HI (CSPatI o) type CSCtx o = [HI (MId, MExp o)] data CSPatI o = CSPatConApp (ConstRef o) [CSPat o] | CSPatVar Nat | CSPatExp (MExp o) | CSWith (MExp o) -- always an App | CSAbsurd | CSOmittedArg type Sol o = [(CSCtx o, [CSPat o], Maybe (MExp o))] caseSplitSearch :: forall o . IORef Int -> Int -> [ConstRef o] -> Maybe (EqReasoningConsts o) -> Int -> Cost -> ConstRef o -> CSCtx o -> MExp o -> [CSPat o] -> IO [Sol o] caseSplitSearch ticks nsolwanted chints meqr depthinterval depth recdef ctx tt pats = do let branchsearch :: Cost -> CSCtx o -> MExp o -> ([Nat], Nat, [Nat]) -> IO (Maybe (MExp o)) branchsearch depth ctx tt termcheckenv = do nsol <- newIORef 1 m <- initMeta sol <- newIORef Nothing let trm = Meta m hsol = do trm' <- expandMetas trm writeIORef sol (Just trm') initcon = mpret $ Sidecondition (localTerminationSidecond termcheckenv recdef trm) $ (case meqr of Nothing -> id Just eqr -> mpret . Sidecondition (calcEqRState eqr trm) ) $ tcSearch False (map (fmap closify) (drophid ctx)) (closify tt) trm recdefd <- readIORef recdef let env = RIEnv { rieHints = (recdef, HMRecCall) : map (, HMNormal) chints , rieDefFreeVars = cddeffreevars recdefd , rieEqReasoningConsts = meqr } depreached <- topSearch ticks nsol hsol env initcon depth (depth + 1) rsol <- readIORef sol return rsol ctx' = ff 1 ctx ff _ [] = [] ff n (HI hid (id, t) : ctx) = HI hid (id, lift n t) : ff (n + 1) ctx caseSplitSearch' branchsearch depthinterval depth recdef ctx' tt pats caseSplitSearch' :: forall o . (Cost -> CSCtx o -> MExp o -> ([Nat], Nat, [Nat]) -> IO (Maybe (MExp o))) -> Int -> Cost -> ConstRef o -> CSCtx o -> MExp o -> [CSPat o] -> IO [Sol o] caseSplitSearch' branchsearch depthinterval depth recdef ctx tt pats = do recdefd <- readIORef recdef sols <- rc depth (cddeffreevars recdefd) ctx tt pats return sols where rc :: Cost -> Int -> CSCtx o -> MExp o -> [CSPat o] -> IO [Sol o] rc depth _ _ _ _ | depth < 0 = return [] rc depth nscrutavoid ctx tt pats = do mblkvar <- getblks tt fork mblkvar where fork :: [Nat] -> IO [Sol o] fork mblkvar = do sols1 <- dobody case sols1 of (_:_) -> return sols1 [] -> do let r :: [Nat] -> IO [Sol o] r [] = return [] r (v:vs) = do sols2 <- splitvar mblkvar v case sols2 of (_:_) -> return sols2 [] -> r vs r [nv - x | x <- [0..nv]] -- [0..length ctx - 1 - nscrutavoid] where nv = length ctx - 1 dobody :: IO [Sol o] dobody = do case findperm (map snd (drophid ctx)) of Just perm -> do let (ctx', tt', pats') = applyperm perm ctx tt pats res <- branchsearch depth ctx' tt' (localTerminationEnv pats') return $ case res of Just trm -> [[(ctx', pats', Just trm)]] Nothing -> [] Nothing -> __IMPOSSIBLE__ -- no permutation found splitvar :: [Nat] -> Nat -> IO [Sol o] splitvar mblkvar scrut = do let scruttype = infertypevar ctx scrut case rm __IMPOSSIBLE__ scruttype of App _ _ (Const c) _ -> do cd <- readIORef c case cdcont cd of Datatype cons _ -> do sols <- dobranches cons return $ map (\sol -> case sol of [] -> case findperm (map snd (drophid ctx)) of Just perm -> let HI scrhid(_, scrt) = ctx !! scrut ctx1 = take scrut ctx ++ (HI scrhid (Id abspatvarname, scrt)) : drop (scrut + 1) ctx (ctx', _, pats') = applyperm perm ctx1 tt ({-map (replacep scrut 1 CSAbsurd __IMPOSSIBLE__) -}pats) in [(ctx', pats', Nothing)] Nothing -> __IMPOSSIBLE__ -- no permutation found _ -> sol ) sols where dobranches :: [ConstRef o] -> IO [Sol o] dobranches [] = return [[]] dobranches (con : cons) = do cond <- readIORef con let ff t = case rm __IMPOSSIBLE__ t of Pi _ h _ it (Abs id ot) -> let (xs, inft) = ff ot in (((h, scrut + length xs), id, lift (scrut + length xs + 1) it) : xs, inft) _ -> ([], lift scrut t) (newvars, inftype) = ff (cdtype cond) constrapp = NotM $ App Nothing (NotM OKVal) (Const con) (foldl (\xs ((h, v), _, _) -> NotM $ ALCons h (NotM $ App Nothing (NotM OKVal) (Var v) (NotM ALNil)) xs) (NotM ALNil) (reverse newvars)) pconstrapp = CSPatConApp con (map (\((hid, v), _, _) -> HI hid (CSPatVar v)) newvars) thesub = replace scrut (length newvars) constrapp Id newvarprefix = fst $ (drophid ctx) !! scrut ctx1 = map (\(HI hid (id, t)) -> HI hid (id, thesub t)) (take scrut ctx) ++ reverse (map (\(((hid, _), id, t), i) -> HI hid (Id (case id of {NoId -> newvarprefix{- ++ show i-}; Id id -> id}), t) ) (zip newvars [0..])) ++ map (\(HI hid (id, t)) -> HI hid (id, thesub t)) (drop (scrut + 1) ctx) tt' = thesub tt pats' = map (replacep scrut (length newvars) pconstrapp constrapp) pats scruttype' = thesub scruttype -- scruttype shouldn't really refer to scrutvar so lift is enough, but what if circular ref has been created and this is not detected until case split is done case unifyexp inftype scruttype' of Nothing -> do res <- notequal scrut (length newvars) scruttype' inftype if res then -- branch absurd dobranches cons else -- branch dont know return [] Just unif -> do let (ctx2, tt2, pats2) = removevar ctx1 tt' pats' unif --cost = if elem scrut mblkvar then costCaseSplit - (costCaseSplit - costCaseSplitFollow) `div` (length mblkvar) else costCaseSplit cost = if null mblkvar then if scrut < length ctx - nscrutavoid && nothid then costCaseSplitLow + costAddVarDepth * Cost (depthofvar scrut pats) else costCaseSplitVeryHigh else if elem scrut mblkvar then costCaseSplitLow else (if scrut < length ctx - nscrutavoid && nothid then costCaseSplitHigh else costCaseSplitVeryHigh) nothid = let HI hid _ = ctx !! scrut in hid == NotHidden sols <- rc (depth - cost) (length ctx - 1 - scrut) ctx2 tt2 pats2 case sols of [] -> return [] _ -> do sols2 <- dobranches cons return $ concat (map (\sol -> map (\sol2 -> sol ++ sol2) sols2) sols) _ -> return [] -- split failed "scrut type is not datatype" _ -> return [] -- split failed "scrut type is not datatype" infertypevar :: CSCtx o -> Nat -> MExp o infertypevar ctx v = snd $ (drophid ctx) !! v class Replace o t u | t u -> o where replace' :: Nat -> MExp o -> t -> Reader (Nat, Nat) u replace :: Replace o t u => Nat -> Nat -> MExp o -> t -> u replace sv nnew e t = replace' 0 e t `runReader` (sv, nnew) instance Replace o t u => Replace o (Abs t) (Abs u) where replace' n re (Abs mid b) = Abs mid <$> replace' (n + 1) re b instance Replace o (Exp o) (MExp o) where replace' n re e = case e of App uid ok elr@(Var v) args -> do ih <- NotM <$> replace' n re args (sv, nnew) <- ask return $ if v >= n then if v - n == sv then betareduce (lift n re) ih else if v - n > sv then NotM $ App uid ok (Var (v + nnew - 1)) ih else NotM $ App uid ok elr ih else NotM $ App uid ok elr ih App uid ok elr@Const{} args -> NotM . App uid ok elr . NotM <$> replace' n re args Lam hid b -> NotM . Lam hid <$> replace' (n + 1) re b Pi uid hid possdep it b -> fmap NotM $ Pi uid hid possdep <$> replace' n re it <*> replace' n re b Sort{} -> return $ NotM e AbsurdLambda{} -> return $ NotM e instance Replace o t u => Replace o (MM t (RefInfo o)) u where replace' n re = replace' n re . rm __IMPOSSIBLE__ instance Replace o (ArgList o) (ArgList o) where replace' n re args = case args of ALNil -> return ALNil ALCons hid a as -> ALCons hid <$> replace' n re a <*> (NotM <$> replace' n re as) ALProj{} -> __IMPOSSIBLE__ ALConPar as -> ALConPar . NotM <$> replace' n re as betareduce :: MExp o -> MArgList o -> MExp o betareduce e args = case rm __IMPOSSIBLE__ args of ALNil -> e ALCons _ a rargs -> case rm __IMPOSSIBLE__ e of App uid ok elr eargs -> NotM $ App uid ok elr (concatargs eargs args) Lam _ (Abs _ b) -> betareduce (replace 0 0 a b) rargs _ -> __IMPOSSIBLE__ -- not type correct if this happens ALProj{} -> __IMPOSSIBLE__ ALConPar as -> __IMPOSSIBLE__ concatargs :: MArgList o -> MArgList o -> MArgList o concatargs xs ys = case rm __IMPOSSIBLE__ xs of ALNil -> ys ALCons hid x xs -> NotM $ ALCons hid x (concatargs xs ys) ALProj{} -> __IMPOSSIBLE__ ALConPar as -> NotM $ ALConPar (concatargs xs ys) replacep :: forall o. Nat -> Nat -> CSPatI o -> MExp o -> CSPat o -> CSPat o replacep sv nnew rp re = r where r :: CSPat o -> CSPat o r (HI hid (CSPatConApp c ps)) = HI hid (CSPatConApp c (map r ps)) r (HI hid (CSPatVar v)) = if v == sv then HI hid rp else if v > sv then HI hid (CSPatVar (v + nnew - 1)) else HI hid (CSPatVar v) r (HI hid (CSPatExp e)) = HI hid (CSPatExp $ replace sv nnew re e) r p@(HI _ CSOmittedArg) = p r _ = __IMPOSSIBLE__ -- other constructors dont appear in indata Pats -- Unification takes two values of the same type and generates a list -- of assignments making the two terms equal. type Assignments o = [(Nat, Exp o)] class Unify o t | t -> o where unify' :: t -> t -> StateT (Assignments o) Maybe () notequal' :: t -> t -> ReaderT (Nat, Nat) (StateT (Assignments o) IO) Bool unify :: Unify o t => t -> t -> Maybe (Assignments o) unify t u = unify' t u `execStateT` [] notequal :: Unify o t => Nat -> Nat -> t -> t -> IO Bool notequal fstnew nbnew t1 t2 = notequal' t1 t2 `runReaderT` (fstnew, nbnew) `evalStateT` [] instance Unify o t => Unify o (MM t (RefInfo o)) where unify' = unify' `on` rm __IMPOSSIBLE__ notequal' = notequal' `on` rm __IMPOSSIBLE__ unifyVar :: Nat -> Exp o -> StateT (Assignments o) Maybe () unifyVar v e = do unif <- get case lookup v unif of Nothing -> modify ((v, e) :) Just e' -> unify' e e' instance Unify o t => Unify o (Abs t) where unify' (Abs _ b1) (Abs _ b2) = unify' b1 b2 notequal' (Abs _ b1) (Abs _ b2) = notequal' b1 b2 instance Unify o (Exp o) where unify' e1 e2 = case (e1, e2) of (App _ _ elr1 args1, App _ _ elr2 args2) | elr1 == elr2 -> unify' args1 args2 (Lam hid1 b1, Lam hid2 b2) | hid1 == hid2 -> unify' b1 b2 (Pi _ hid1 _ a1 b1, Pi _ hid2 _ a2 b2) | hid1 == hid2 -> unify' a1 a2 >> unify' b1 b2 (Sort _, Sort _) -> return () -- a bit sloppy (App _ _ (Var v) (NotM ALNil), _) | elem v (freevars e2) -> St.lift Nothing -- Occurs check (_, App _ _ (Var v) (NotM ALNil)) | elem v (freevars e1) -> St.lift Nothing -- Occurs check (App _ _ (Var v) (NotM ALNil), _) -> unifyVar v e2 (_, App _ _ (Var v) (NotM ALNil)) -> unifyVar v e1 _ -> St.lift Nothing notequal' e1 e2 = do (fstnew, nbnew) <- ask unifier <- get case (e1, e2) of (App _ _ elr1 es1, App _ _ elr2 es2) | elr1 == elr2 -> notequal' es1 es2 (_, App _ _ (Var v2) (NotM ALNil)) -- why is this not symmetric?! | fstnew <= v2 && v2 < fstnew + nbnew -> case lookup v2 unifier of Nothing -> modify ((v2, e1):) >> return False Just e2' -> notequal' e1 e2' {- GA: Skipped these: Not sure why we'd claim they're impossible (_, App _ _ (Var v2) (NotM ALProj{})) -> __IMPOSSIBLE__ (_, App _ _ (Var v2) (NotM ALConPar{})) -> __IMPOSSIBLE__ -} (App _ _ (Const c1) es1, App _ _ (Const c2) es2) -> do cd1 <- liftIO $ readIORef c1 cd2 <- liftIO $ readIORef c2 case (cdcont cd1, cdcont cd2) of (Constructor{}, Constructor{}) -> if c1 == c2 then notequal' es1 es2 else return True _ -> return False {- GA: Why don't we have a case for distinct heads after all these unification cases for vars with no spines & metas that can be looked up? (App _ _ elr1 _, App _ _ elr2 _) | elr1 <> elr2 -> return True -} _ -> return False instance Unify o (ArgList o) where unify' args1 args2 = case (args1, args2) of (ALNil, ALNil) -> pure () (ALCons hid1 a1 as1, ALCons hid2 a2 as2) | hid1 == hid2 -> unify' a1 a2 >> unify' as1 as2 (ALConPar as1, ALCons _ _ as2) -> unify' as1 as2 (ALCons _ _ as1, ALConPar as2) -> unify' as1 as2 (ALConPar as1, ALConPar as2) -> unify' as1 as2 _ -> St.lift Nothing notequal' args1 args2 = case (args1, args2) of (ALCons _ e es, ALCons _ f fs) -> notequal' e f `or2M` notequal' es fs (ALConPar es1, ALConPar es2) -> notequal' es1 es2 _ -> return False -- This definition is only here to respect the previous interface. unifyexp :: MExp o -> MExp o -> Maybe ([(Nat, MExp o)]) unifyexp e1 e2 = fmap (NotM <$>) <$> unify e1 e2 class Lift t where lift' :: Nat -> Nat -> t -> t lift :: Lift t => Nat -> t -> t lift 0 = id lift n = lift' n 0 instance Lift t => Lift (Abs t) where lift' n j (Abs mid b) = Abs mid (lift' n (j + 1) b) instance Lift t => Lift (MM t r) where lift' n j = NotM . lift' n j . rm __IMPOSSIBLE__ instance Lift (Exp o) where lift' n j e = case e of App uid ok elr args -> case elr of Var v | v >= j -> App uid ok (Var (v + n)) (lift' n j args) _ -> App uid ok elr (lift' n j args) Lam hid b -> Lam hid (lift' n j b) Pi uid hid possdep it b -> Pi uid hid possdep (lift' n j it) (lift' n j b) Sort{} -> e AbsurdLambda{} -> e instance Lift (ArgList o) where lift' n j args = case args of ALNil -> ALNil ALCons hid a as -> ALCons hid (lift' n j a) (lift' n j as) ALProj{} -> __IMPOSSIBLE__ ALConPar as -> ALConPar (lift' n j as) removevar :: CSCtx o -> MExp o -> [CSPat o] -> [(Nat, MExp o)] -> (CSCtx o, MExp o, [CSPat o]) removevar ctx tt pats [] = (ctx, tt, pats) removevar ctx tt pats ((v, e) : unif) = let e2 = replace v 0 __IMPOSSIBLE__ {- occurs check failed -} e thesub = replace v 0 e2 ctx1 = map (\(HI hid (id, t)) -> HI hid (id, thesub t)) (take v ctx) ++ map (\(HI hid (id, t)) -> HI hid (id, thesub t)) (drop (v + 1) ctx) tt' = thesub tt pats' = map (replacep v 0 (CSPatExp e2) e2) pats unif' = map (\(uv, ue) -> (if uv > v then uv - 1 else uv, thesub ue)) unif in removevar ctx1 tt' pats' unif' findperm :: [MExp o] -> Maybe [Nat] findperm ts = let frees = map freevars ts m = IntMap.fromList $ map (\i -> (i, length (filter (elem i) frees))) [0..length ts - 1] r _ perm 0 = Just $ reverse perm r m perm n = case lookup 0 (map swap (IntMap.toList m)) of Nothing -> Nothing Just i -> r (foldl (flip $ IntMap.adjust (subtract 1)) (IntMap.insert i (-1) m) (frees !! i)) (i : perm) (n - 1) in r m [] (length ts) freevars :: FreeVars t => t -> [Nat] freevars = Set.toList . freeVars applyperm :: [Nat] -> CSCtx o -> MExp o -> [CSPat o] -> (CSCtx o, MExp o, [CSPat o]) applyperm perm ctx tt pats = let ctx1 = map (\(HI hid (id, t)) -> HI hid (id, rename (ren perm) t)) ctx ctx2 = map (\i -> ctx1 !! i) perm ctx3 = seqctx ctx2 tt' = rename (ren perm) tt pats' = map (rename (ren perm)) pats in (ctx3, tt', pats') ren :: [Nat] -> Nat -> Int ren n i = let Just j = findIndex (== i) n in j instance Renaming t => Renaming (HI t) where renameOffset j ren (HI hid t) = HI hid $ renameOffset j ren t instance Renaming (CSPatI o) where renameOffset j ren e = case e of CSPatConApp c pats -> CSPatConApp c $ map (renameOffset j ren) pats CSPatVar i -> CSPatVar $ j + ren i CSPatExp e -> CSPatExp $ renameOffset j ren e CSOmittedArg -> e _ -> __IMPOSSIBLE__ seqctx :: CSCtx o -> CSCtx o seqctx = r (-1) where r _ [] = [] r n (HI hid (id, t) : ctx) = HI hid (id, lift n t) : r (n - 1) ctx -- -------------------- depthofvar :: Nat -> [CSPat o] -> Nat depthofvar v pats = let [depth] = concatMap (f 0) (drophid pats) f d (CSPatConApp _ pats) = concatMap (f (d + 1)) (drophid pats) f d (CSPatVar v') = if v == v' then [d] else [] f _ _ = [] in depth -- -------------------- -- | Speculation: Type class computing the size (?) of a pattern -- and collecting the vars it introduces class LocalTerminationEnv a where sizeAndBoundVars :: a -> (Sum Nat, [Nat]) instance LocalTerminationEnv a => LocalTerminationEnv (HI a) where sizeAndBoundVars (HI _ p) = sizeAndBoundVars p instance LocalTerminationEnv (CSPatI o) where sizeAndBoundVars p = case p of CSPatConApp _ ps -> (1, []) <> sizeAndBoundVars ps CSPatVar n -> (0, [n]) CSPatExp e -> sizeAndBoundVars e _ -> (0, []) instance LocalTerminationEnv a => LocalTerminationEnv [a] where sizeAndBoundVars = foldMap sizeAndBoundVars instance LocalTerminationEnv (MExp o) where -- sizeAndBoundVars e = case rm __IMPOSSIBLE__ e of -- GA: 2017 06 27: Not actually impossible! (cf. #2620) sizeAndBoundVars Meta{} = (0, []) -- Does this default behaviour even make sense? The catchall in the -- following match seems to suggest it does sizeAndBoundVars (NotM e) = case e of App _ _ (Var v) _ -> (0, [v]) App _ _ (Const _) args -> (1, []) <> sizeAndBoundVars args _ -> (0, []) instance (LocalTerminationEnv a, LocalTerminationEnv b) => LocalTerminationEnv (a, b) where sizeAndBoundVars (a, b) = sizeAndBoundVars a <> sizeAndBoundVars b instance LocalTerminationEnv (MArgList o) where sizeAndBoundVars as = case rm __IMPOSSIBLE__ as of ALNil -> (0, []) ALCons _ a as -> sizeAndBoundVars (a, as) ALProj{} -> __IMPOSSIBLE__ ALConPar as -> sizeAndBoundVars as -- | Take a list of patterns and returns (is, size, vars) where (speculation): --- * the is are the pattern indices the vars are contained in -- * size is total number of constructors removed (?) to access vars localTerminationEnv :: [CSPat o] -> ([Nat], Nat, [Nat]) localTerminationEnv pats = (is, getSum s, vs) where (is , s , vs) = g 0 pats g :: Nat -> [CSPat o] -> ([Nat], Sum Nat, [Nat]) g _ [] = ([], 0, []) g i (hp@(HI _ p) : ps) = case p of CSPatConApp{} -> let (size, vars) = sizeAndBoundVars hp in ([i], size, vars) <> g (i + 1) ps _ -> g (i + 1) ps localTerminationSidecond :: ([Nat], Nat, [Nat]) -> ConstRef o -> MExp o -> EE (MyPB o) localTerminationSidecond (is, size, vars) reccallc b = ok b where ok e = mmpcase (False, prioNo, Nothing) e $ \e -> case e of App _ _ elr args -> mpret $ Sidecondition (oks args) (case elr of Const c | c == reccallc -> if size == 0 then mpret (Error "localTerminationSidecond: no size to decrement") else okcall 0 size vars args _ -> mpret OK ) Lam _ (Abs _ e) -> ok e Pi _ _ _ it (Abs _ ot) -> mpret $ Sidecondition (ok it) (ok ot) Sort{} -> mpret OK AbsurdLambda{} -> mpret OK oks as = mmpcase (False, prioNo, Nothing) as $ \as -> case as of ALNil -> mpret OK ALCons _ a as -> mpret $ Sidecondition (ok a) (oks as) ALProj eas _ _ as -> mpret $ Sidecondition (oks eas) (oks as) ALConPar as -> oks as okcall i size vars as = mmpcase (False, prioNo, Nothing) as $ \as -> case as of ALNil -> mpret OK ALCons _ a as | elem i is -> mbpcase prioNo Nothing (he size vars a) $ \x -> case x of Nothing -> mpret $ Error "localTerminationSidecond: reccall not ok" Just (size', vars') -> okcall (i + 1) size' vars' as ALCons _ a as -> okcall (i + 1) size vars as ALProj{} -> mpret OK ALConPar as -> __IMPOSSIBLE__ he size vars e = mmcase e $ \e -> case e of App _ _ (Var v) _ -> case remove v vars of Nothing -> mbret Nothing Just vars' -> mbret $ Just (size, vars') App _ _ (Const c) args -> do cd <- readIORef c case cdcont cd of Constructor{} -> if size == 1 then mbret Nothing else hes (size - 1) vars args _ -> mbret Nothing _ -> mbret Nothing hes size vars as = mmcase as $ \as -> case as of ALNil -> mbret $ Just (size, vars) ALCons _ a as -> mbcase (he size vars a) $ \x -> case x of Nothing -> mbret Nothing Just (size', vars') -> hes size' vars' as ALProj{} -> __IMPOSSIBLE__ ALConPar as -> __IMPOSSIBLE__ remove _ [] = Nothing remove x (y : ys) | x == y = Just ys remove x (y : ys) = case remove x ys of {Nothing -> Nothing; Just ys' -> Just (y : ys')} -- --------------------------- getblks :: MExp o -> IO [Nat] getblks tt = do NotB (hntt, blks) <- hnn_blks (Clos [] tt) case f blks of Just v -> return [v] Nothing -> case rawValue hntt of HNApp (Const c) args -> do cd <- readIORef c case cdcont cd of Datatype{} -> g [] args _ -> return [] _ -> return [] where f blks = case blks of (_:_) -> case rawValue (last blks) of HNApp (Var v) _ -> Just v _ -> Nothing _ -> Nothing g vs args = do NotB hnargs <- hnarglist args case hnargs of HNALCons _ a as -> do NotB (_, blks) <- hnn_blks a let vs' = case f blks of Just v | v `notElem` vs -> v : vs _ -> vs g vs' as _ -> return vs -- --------------------------- Agda-2.5.3/src/full/Agda/Auto/Typecheck.hs0000644000000000000000000007632613154613124016331 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Auto.Typecheck where import Data.IORef import Control.Monad (liftM) import Agda.Syntax.Common (Hiding (..)) import Agda.Auto.NarrowingSearch import Agda.Auto.Syntax import Agda.Auto.SearchControl #include "undefined.h" import Agda.Utils.Impossible -- --------------------------------- -- | Typechecker drives the solution of metas. tcExp :: Bool -> Ctx o -> CExp o -> MExp o -> EE (MyPB o) tcExp isdep ctx typ@(TrBr typtrs ityp@(Clos _ itypexp)) trm = mbpcase prioTypeUnknown Nothing (hnn_checkstep ityp) $ \(hntyp, iotastepdone) -> mmpcase (True, prioTypecheck isdep, Just (RIMainInfo (length ctx) hntyp iotastepdone)) trm $ \trm -> case trm of App _ okh elr args -> case rawValue hntyp of HNPi{} | isdep -> mpret $ Error "tcExp, dep terms should be eta-long" _ -> do (ityp, sc) <- case elr of Var v -> -- assuming within scope return (weak (v + 1) (snd $ ctx !! v), id) Const c -> do cdef <- readIORef c return (closify (cdtype cdef), \x -> mpret $ And (Just [Term args]) (noiotastep_term c args) x) ndfv <- case elr of Var{} -> return 0 Const c -> readIORef c >>= \cd -> return (cddeffreevars cd) isconstructor <- case elr of Var{} -> return False Const c -> do cdef <- readIORef c return $ case cdcont cdef of {Constructor{} -> True; _ -> False} sc $ tcargs ndfv isdep ctx ityp args (NotM $ App Nothing (NotM OKVal) elr (NotM ALNil)) isconstructor $ \ityp _ -> mpret $ ConnectHandle okh (comp' True typ ityp) Lam hid (Abs id1 b) -> case rawValue hntyp of HNPi hid2 _ it (Abs id2 ot) | hid == hid2 -> tcExp isdep ((pickid id1 id2, t it) : ctx) (t ot) b _ -> mpret $ Error "tcExp, type of lam should be fun or pi (and same hid)" Pi _ _ _ it (Abs id ot) -> case rawValue hntyp of HNSort s -> mpret $ And (Just [Term ctx, Term it]) (tcExp True ctx (closify (NotM $ Sort s)) it) (tcExp isdep ((id, closify it) : ctx) (closify (NotM $ Sort s)) ot) _ -> mpret $ Error "tcExp, type of pi should be set" Sort (Set i) -> case rawValue hntyp of HNSort s2 -> case s2 of Set j -> mpret $ if i < j then OK else Error "tcExp, type of set should be larger set" UnknownSort -> mpret OK -- mpret $ Error "tcExp, type of set i unknown sort" -- OK instead? (prev __IMPOSSIBLE__) Type -> mpret OK _ -> mpret $ Error "tcExp, type of set should be set" Sort UnknownSort -> __IMPOSSIBLE__ Sort Type -> __IMPOSSIBLE__ AbsurdLambda hid -> case rawValue hntyp of HNPi hid2 _ it _ | hid == hid2 -> mbpcase prioAbsurdLambda Nothing (getDatatype it) $ \res -> case res of Just (indeces, cons) -> foldl (\p con -> mpret $ And Nothing p ( constructorImpossible indeces con )) (mpret OK) cons Nothing -> mpret $ Error "tcExp, absurd lambda, datatype needed" _ -> mpret $ Error "tcExp, type of absurd lam should be fun or pi (and same hid)" where t = TrBr typtrs getDatatype :: ICExp o -> EE (MyMB (Maybe (ICArgList o, [ConstRef o])) o) getDatatype t = mbcase (hnn t) $ \hnt -> case rawValue hnt of HNApp (Const c) args -> do cd <- readIORef c case cdcont cd of Datatype cons _ -> mbret $ Just (args, cons) -- ?? check that lenth args corresponds to type of datatype _ -> mbret Nothing _ -> mbret Nothing constructorImpossible :: ICArgList o -> ConstRef o -> EE (MyPB o) constructorImpossible args c = do cd <- readIORef c mbpcase prioAbsurdLambda Nothing (traversePi (-1) (Clos [] $ cdtype cd)) $ \hnot -> case rawValue hnot of HNApp _ args2 -> unequals args args2 (\_ -> mpret $ Error "not unequal") [] _ -> mpret $ Error "constructorImpossible 1" unequals :: ICArgList o -> ICArgList o -> ([(Nat, HNExp o)] -> EE (MyPB o)) -> [(Nat, HNExp o)] -> EE (MyPB o) unequals es1 es2 cont unifier2 = mbpcase prioAbsurdLambda Nothing (hnarglist es1) $ \hnes1 -> mbpcase prioAbsurdLambda Nothing (hnarglist es2) $ \hnes2 -> case (hnes1, hnes2) of (HNALCons _ e1 es1, HNALCons _ e2 es2) -> unequal e1 e2 (unequals es1 es2 cont) unifier2 (HNALConPar es1, HNALConPar es2) -> unequals es1 es2 cont unifier2 _ -> cont unifier2 unequal :: ICExp o -> ICExp o -> ([(Nat, HNExp o)] -> EE (MyPB o)) -> [(Nat, HNExp o)] -> EE (MyPB o) unequal e1 e2 cont unifier2 = mbpcase prioAbsurdLambda Nothing (hnn e1) $ \hne1 -> mbpcase prioAbsurdLambda Nothing (hnn e2) $ \hne2 -> case rawValue hne2 of HNApp (Var v2) es2 | v2 < 0 -> mbpcase prioAbsurdLambda Nothing (hnarglist es2) $ \hnes2 -> case hnes2 of HNALNil -> case lookup v2 unifier2 of Nothing -> cont ((v2, hne1) : unifier2) Just hne2' -> cc hne1 hne2' HNALCons{} -> cont unifier2 HNALConPar{} -> __IMPOSSIBLE__ _ -> cc hne1 hne2 where cc hne1 hne2 = case (rawValue hne1, rawValue hne2) of (HNApp (Const c1) es1, HNApp (Const c2) es2) -> do cd1 <- readIORef c1 cd2 <- readIORef c2 case (cdcont cd1, cdcont cd2) of (Constructor{}, Constructor{}) -> if c1 == c2 then unequals es1 es2 cont unifier2 else mpret OK _ -> cont unifier2 _ -> cont unifier2 traversePi :: Int -> ICExp o -> EE (MyMB (HNExp o) o) traversePi v t = mbcase (hnn t) $ \hnt -> case rawValue hnt of HNPi _ _ _ (Abs _ ot) -> traversePi (v - 1) $ subi (NotM $ App Nothing (NotM OKVal) (Var v) (NotM ALNil)) ot _ -> mbret hnt tcargs :: Nat -> Bool -> Ctx o -> CExp o -> MArgList o -> MExp o -> Bool -> (CExp o -> MExp o -> EE (MyPB o)) -> EE (MyPB o) tcargs ndfv isdep ctx ityp@(TrBr ityptrs iityp) args elimtrm isconstructor cont = mmpcase (True, prioTypecheckArgList, (Just $ RICheckElim $ isdep || isconstructor)) args $ \args' -> case args' of ALNil -> cont ityp elimtrm ALCons hid a as -> mbpcase prioInferredTypeUnknown (Just RIInferredTypeUnknown) (hnn iityp) $ \hnityp -> case rawValue hnityp of HNPi hid2 possdep it (Abs _ ot) | ndfv > 0 || copyarg a || hid == hid2 -> mpret $ And (Just ((if possdep then [Term a] else []) ++ [Term ctx, Term ityptrs])) (if ndfv > 0 then mpret OK else (tcExp (isdep || possdep) ctx (t it) a)) (tcargs (ndfv - 1) isdep ctx (sub a (t ot)) as (addend hid a elimtrm) isconstructor cont) _ -> mpret $ Error "tcargs, inf type should be fun or pi (and same hid)" ALProj{} | ndfv > 0 -> __IMPOSSIBLE__ ALProj preas projidx hid as -> mbpcase prioInferredTypeUnknown (Just RIInferredTypeUnknown) (hnn iityp) $ \hnityp -> case rawValue hnityp of HNApp (Const dd) _ -> do dddef <- readIORef dd case cdcont dddef of Datatype _ projs -> mmpcase (True, prioProjIndex, Just (RICheckProjIndex projs)) projidx $ \projidx -> do projd <- readIORef projidx tcargs (cddeffreevars projd) isdep ctx (closify $ cdtype projd) preas (NotM $ App Nothing (NotM OKVal) (Const projidx) (NotM ALNil)) True $ \ityp2@(TrBr ityp2trs iityp2) elimtrm2 -> case iityp2 of Clos _ (NotM (Pi _ _ _ (NotM (App _ _ (Const dd2) _)) _)) | dd2 == dd -> mbpcase prioInferredTypeUnknown (Just RIInferredTypeUnknown) (hnn iityp2) $ \hnityp2 -> case rawValue hnityp2 of HNPi hid2 possdep it (Abs _ ot) | hid == hid2 -> mpret $ And Nothing (comp' True (TrBr ityp2trs it) ityp) (tcargs 0 isdep ctx (sub elimtrm (t ot)) as (addend hid elimtrm elimtrm2) isconstructor cont) _ -> mpret $ Error "proj function type is not a Pi" _ -> mpret $ Error "proj function type is not correct" _ -> mpret $ Error "proj, not a datatype" _ -> mpret $ Error "proj, not a const app" ALConPar _ -> __IMPOSSIBLE__ where t = TrBr ityptrs addend :: Hiding -> MExp o -> MM (Exp o) blk -> MM (Exp o) blk addend hid a (NotM (App uid okh elr as)) = NotM $ App uid okh elr (f as) where f (NotM ALNil) = NotM $ ALCons hid a (NotM $ ALNil) f (NotM (ALCons hid a as)) = NotM $ ALCons hid a (f as) f _ = __IMPOSSIBLE__ addend _ _ _ = __IMPOSSIBLE__ copyarg :: MExp o -> Bool copyarg _ = False -- --------------------------------- type HNNBlks o = [HNExp o] noblks :: HNNBlks o noblks = [] addblk :: HNExp o -> HNNBlks o -> HNNBlks o addblk = (:) hnn :: ICExp o -> EE (MyMB (HNExp o) o) hnn e = mbcase (hnn_blks e) $ \(hne, _) -> mbret hne hnn_blks :: ICExp o -> EE (MyMB (HNExp o, HNNBlks o) o) hnn_blks e = hnn' e CALNil hnn_checkstep :: ICExp o -> EE (MyMB (HNExp o, Bool) o) hnn_checkstep e = mbcase (hnb e CALNil) $ \hne -> mbcase (iotastep True hne) $ \res -> case res of Right _ -> mbret (hne, False) Left (e, as) -> mbcase (hnn' e as) $ \(hne, _) -> mbret (hne, True) hnn' :: ICExp o -> ICArgList o -> EE (MyMB (HNExp o, HNNBlks o) o) hnn' e as = mbcase (hnb e as) $ \hne -> mbcase (iotastep True hne) $ \res -> case res of Right blks -> mbret (hne, blks) Left (e, as) -> hnn' e as hnb :: ICExp o -> ICArgList o -> EE (MyMB (HNExp o) o) hnb e as = mbcase (hnc False e as []) $ \res -> case res of HNDone _ hne -> mbret hne HNMeta{} -> __IMPOSSIBLE__ data HNRes o = HNDone (Maybe (Metavar (Exp o) (RefInfo o))) (HNExp o) | HNMeta (ICExp o) (ICArgList o) [Maybe (UId o)] hnc :: Bool -> ICExp o -> ICArgList o -> [Maybe (UId o)] -> EE (MyMB (HNRes o) o) hnc haltmeta = loop where loop ce@(Clos cl e) cargs seenuids = (if haltmeta then mmmcase e (mbret $ HNMeta ce cargs seenuids) else mmcase e) $ \ee -> case ee of App uid okh elr args -> let ncargs = CALConcat (Clos cl args) cargs in case elr of Var v -> case doclos cl v of Left v' -> mbret $ HNDone expmeta $ WithSeenUIds (uid : seenuids) $ HNApp (Var v') ncargs Right f -> loop f ncargs (uid : seenuids) Const _ -> mbret $ HNDone expmeta $ WithSeenUIds (uid : seenuids) $ HNApp elr ncargs Lam hid (Abs id b) -> mbcase (hnarglist cargs) $ \hncargs -> case hncargs of HNALNil -> mbret $ HNDone expmeta $ WithSeenUIds seenuids $ HNLam hid (Abs id (Clos (Skip : cl) b)) HNALCons _ arg cargs' -> loop (Clos (Sub arg : cl) b) cargs' seenuids HNALConPar{} -> __IMPOSSIBLE__ Pi uid hid possdep it (Abs id ot) -> checkNoArgs cargs $ mbret $ HNDone expmeta $ WithSeenUIds (uid : seenuids) $ HNPi hid possdep (Clos cl it) (Abs id (Clos (Skip : cl) ot)) Sort s -> checkNoArgs cargs $ mbret $ HNDone expmeta $ WithSeenUIds [] $ HNSort s AbsurdLambda{} -> mbfailed "hnc: encountered absurdlambda" where expmeta = case e of {Meta m -> Just m; NotM _ -> Nothing} checkNoArgs cargs c = mbcase (hnarglist cargs) $ \hncargs -> case hncargs of HNALNil -> c HNALCons{} -> mbfailed "hnc: there should be no args" HNALConPar{} -> __IMPOSSIBLE__ hnarglist :: ICArgList o -> EE (MyMB (HNArgList o) o) hnarglist args = case args of CALNil -> mbret HNALNil CALConcat (Clos cl args) args2 -> mmcase args $ \args -> case args of ALNil -> hnarglist args2 ALCons hid arg argsb -> mbret $ HNALCons hid (Clos cl arg) (CALConcat (Clos cl argsb) args2) ALProj{} -> mbret HNALNil -- dirty hack to make check of no-iota in term work ALConPar args -> mbret $ HNALConPar (CALConcat (Clos cl args) args2) -- ----------------------------- getNArgs :: Nat -> ICArgList o -> EE (MyMB (Maybe ([ICExp o], ICArgList o)) o) getNArgs 0 args = mbret $ Just ([], args) getNArgs narg args = mbcase (hnarglist args) $ \hnargs -> case hnargs of HNALNil -> mbret Nothing HNALCons _ arg args' -> mbcase (getNArgs (narg - 1) args') $ \res -> case res of Nothing -> mbret Nothing Just (pargs, rargs) -> mbret $ Just (arg : pargs, rargs) HNALConPar{} -> __IMPOSSIBLE__ getAllArgs :: ICArgList o -> EE (MyMB [ICExp o] o) getAllArgs args = mbcase (hnarglist args) $ \hnargs -> case hnargs of HNALNil -> mbret [] HNALCons _ arg args' -> mbcase (getAllArgs args') $ \args'' -> mbret (arg : args'') HNALConPar args2 -> mbcase (getAllArgs args2) $ \args3 -> mbret (__IMPOSSIBLE__ : args3) data PEval o = PENo (ICExp o) | PEConApp (ICExp o) (ConstRef o) [PEval o] iotastep :: Bool -> HNExp o -> EE (MyMB (Either (ICExp o, ICArgList o) (HNNBlks o)) o) iotastep smartcheck e = case rawValue e of HNApp (Const c) args -> do cd <- readIORef c case cdcont cd of Def narg cls _ _ -> mbcase (getNArgs narg args) $ \res -> case res of Nothing -> mbret (Right noblks) Just (pargs, rargs) -> mbcase (dorules cls (map PENo pargs)) $ \res -> case res of Right blks -> mbret (Right blks) Left rhs -> mbret $ Left (rhs, rargs) _ -> mbret $ Right noblks _ -> mbret $ Right noblks where dorules :: [Clause o] -> [PEval o] -> EE (MyMB (Either (ICExp o) (HNNBlks o)) o) dorules [] _ = mbret $ Right noblks dorules (rule:rules') as = mbcase (dorule rule as) $ \x -> case x of Left (Left as') -> dorules rules' as' Left (Right blks) -> mbret (Right blks) Right rhs -> mbret $ Left rhs dorule :: Clause o -> [PEval o] -> EE (MyMB (Either (Either [PEval o] (HNNBlks o)) (ICExp o)) o) dorule (pats, rhs) as = mbcase (dopats pats as) $ \x -> case x of Right (_, ss) -> mbret $ Right (Clos (map Sub ss) rhs) Left hnas -> mbret $ Left hnas dopats :: [Pat o] -> [PEval o] -> EE (MyMB (Either (Either [PEval o] (HNNBlks o)) ([PEval o], [ICExp o])) o) dopats [] [] = mbret $ Right ([], []) dopats (p:ps') (a:as') = mbcase (dopat p a) $ \x -> case x of Right (hna, ss) -> mbcase (dopats ps' as') $ \x -> case x of Right (hnas, ss2) -> mbret $ Right (hna : hnas, ss2 ++ ss) Left (Right blks) -> mbret $ Left (Right blks) Left (Left hnas) -> mbret $ Left $ Left (hna : hnas) Left (Right blks) -> mbret $ Left (Right blks) Left (Left hna) -> mbret $ Left $ Left (hna : as') dopats _ _ = __IMPOSSIBLE__ dopat :: Pat o -> PEval o -> EE (MyMB (Either (Either (PEval o) (HNNBlks o)) (PEval o, [ICExp o])) o) dopat (PatConApp c pas) a = case a of PENo a -> if smartcheck then mbcase (meta_not_constructor a) $ \notcon -> if notcon then mbret $ Left $ Right noblks else qq -- to know more often if iota step is possible else qq where qq = mbcase (hnn_blks a) $ \(hna, blks) -> case rawValue hna of HNApp (Const c') as -> if c == c' then mbcase (getAllArgs as) $ \as' -> if length as' == length pas then mbcase (dopats pas (map PENo as')) $ \x -> case x of Right (hnas, ss) -> mbret $ Right (PEConApp a c' hnas, ss) Left (Right blks) -> mbret $ Left (Right blks) Left (Left hnas) -> mbret $ Left $ Left (PEConApp a c' hnas) else mbfailed "dopat: wrong amount of args" else do cd <- readIORef c' case cdcont cd of Constructor{} -> mbcase (getAllArgs as) $ \as' -> mbret $ Left (Left (PEConApp a c' (map PENo as'))) _ -> mbret $ Left (Right (addblk hna blks)) _ -> mbret $ Left (Right (addblk hna blks)) aa@(PEConApp a c' as) -> if c == c' then if length as == length pas then mbcase (dopats pas as) $ \x -> case x of Right (hnas, ss) -> mbret $ Right (PEConApp a c' hnas, ss) Left (Right blks) -> mbret $ Left (Right blks) Left (Left hnas) -> mbret $ Left $ Left (PEConApp a c' hnas) else mbfailed "dopat: wrong amount of args" else mbret $ Left (Left aa) dopat PatVar{} a@(PENo a') = mbret $ Right (a, [a']) dopat PatVar{} a@(PEConApp a' _ _) = mbret $ Right (a, [a']) dopat PatExp a = mbret $ Right (a, []) -- ----------------------------- noiotastep :: HNExp o -> EE (MyPB o) noiotastep hne = mbpcase prioNoIota Nothing (iotastep False hne) $ \res -> case res of Left _ -> mpret $ Error "iota step possible contrary to assumed" Right _ -> mpret OK noiotastep_term :: ConstRef o -> MArgList o -> EE (MyPB o) noiotastep_term c args = do cd <- readIORef c case cdcont cd of Def _ [(pats, _)] _ _ -> mpret OK -- all (\pat -> case pat of {PatConApp{} -> False; _ -> True}) pats _ -> noiotastep $ WithSeenUIds [] $ HNApp (Const c) $ CALConcat (Clos [] args) CALNil data CMode o = CMRigid (Maybe (Metavar (Exp o) (RefInfo o))) (HNExp o) | forall b . Refinable b (RefInfo o) => CMFlex (MM b (RefInfo o)) (CMFlex o) data CMFlex o = CMFFlex (ICExp o) (ICArgList o) [Maybe (UId o)] | CMFSemi (Maybe (Metavar (Exp o) (RefInfo o))) (HNExp o) | CMFBlocked (Maybe (Metavar (Exp o) (RefInfo o))) (HNExp o) comp' :: forall o . Bool -> CExp o -> CExp o -> EE (MyPB o) comp' ineq lhs@(TrBr trs1 e1) rhs@(TrBr trs2 e2) = comp ineq e1 e2 where comp :: Bool -> ICExp o -> ICExp o -> EE (MyPB o) comp ineq e1 e2 = proc e1 e2 where proc e1 e2 = f True e1 CALNil [] $ \res1 -> f True e2 CALNil [] $ \res2 -> g res1 res2 f semifok e as seenuids cont = mbpcase prioCompBeta Nothing (hnc True e as seenuids) $ \res -> case res of HNDone mexpmeta hne -> fhn semifok mexpmeta hne cont HNMeta ce@(Clos cl m) cargs seenuids -> do b1 <- boringClos cl b2 <- boringArgs cargs if b1 && b2 then cont $ CMFlex m (CMFFlex ce cargs seenuids) else mbpcase prioCompBetaStructured Nothing (hnc False ce cargs seenuids) $ \res -> case res of HNDone mexpmeta hne -> cont $ CMFlex m (CMFBlocked mexpmeta hne) HNMeta{} -> __IMPOSSIBLE__ fhn semifok mexpmeta hne cont = mmbpcase (iotastep True hne) (\m -> do sf <- return False {- semiflex hne -} if semifok && sf then cont (CMFlex m (CMFSemi mexpmeta hne)) else cont (CMFlex m (CMFBlocked mexpmeta hne)) ) (\res -> case res of Right _ -> cont (CMRigid mexpmeta hne) Left (e, as) -> f semifok e as [] cont ) g res1 res2 = case (res1, res2) of (CMRigid mexpmeta1 hne1, CMRigid mexpmeta2 hne2) -> comphn ineq mexpmeta1 hne1 mexpmeta2 hne2 (CMFlex m1 (CMFBlocked mexpmeta1 hne1), _) -> mstp False mexpmeta1 hne1 $ \res1 -> g res1 res2 (_, CMFlex m2 (CMFBlocked mexpmeta2 hne2)) -> mstp False mexpmeta2 hne2 $ \res2 -> g res1 res2 (CMRigid mexpmeta1 hne1, CMFlex _ fl2) -> unif True mexpmeta1 hne1 fl2 (CMFlex _ fl1, CMRigid mexpmeta2 hne2) -> unif False mexpmeta2 hne2 fl1 (CMFlex m1 fl1, CMFlex m2 fl2) -> doubleblock m1 m2 $ fcm fl1 $ \res1 -> fcm fl2 $ \res2 -> g res1 res2 fcm (CMFFlex ce cargs seenuids) = f True ce cargs seenuids fcm (CMFSemi mexpmeta hne) = fhn True mexpmeta hne fcm (CMFBlocked _ hne) = __IMPOSSIBLE__ -- not used. if so should be: fhn False hne mstp semif mexpmeta hne cont = mpret $ Or prioCompChoice (mpret $ And (Just [Term lhs, Term rhs]) (noiotastep hne) (cont (CMRigid mexpmeta hne)) ) (stp semif hne cont) stp semif hne cont = mbpcase prioCompIota (Just $ RIIotaStep semif) (iotastep True hne) $ \res -> case res of Right _ -> mpret $ Error "no iota step possible, contrary to assumed" Left (e, as) -> f semif e as [] cont unif oppis1 oppmexpmeta opphne res = let iter res = if oppis1 then g (CMRigid oppmexpmeta opphne) res else g res (CMRigid oppmexpmeta opphne) in case res of CMFFlex ce cargs seenuids -> do poss <- iotapossmeta ce cargs maybeor poss prioCompChoice (loop ce cargs seenuids) -- (mbpcase prioCompBeta (Just $ RIIotaStep False) (hnb ce cargs) $ \hne -> (mbpcase prioCompBeta (Just $ RIIotaStep False) (hnc False ce cargs seenuids) $ \res -> -- RIIotaStep here on beta-norm to make cost high when guessing elim const in type par case res of HNDone mexpmeta hne -> stp False hne iter HNMeta{} -> __IMPOSSIBLE__ ) where loop ce@(Clos cl m) cargs seenuids = mmpcase (False, prioCompUnif, Just (RIUnifInfo cl opphne)) m $ \_ -> mbpcase prioCompBeta Nothing (hnc True ce cargs seenuids) $ \res -> case res of HNDone mexpmeta hne -> mpret $ And (Just [Term lhs, Term rhs]) (noiotastep hne) (iter (CMRigid mexpmeta hne)) HNMeta ce cargs seenuids -> loop ce cargs seenuids CMFSemi _ hne -> __IMPOSSIBLE__ -- CMFSemi disabled, if used should be: stp True hne iter CMFBlocked{} -> __IMPOSSIBLE__ comphn :: Bool -> Maybe (Metavar (Exp o) (RefInfo o)) -> HNExp o -> Maybe (Metavar (Exp o) (RefInfo o)) -> HNExp o -> EE (MyPB o) comphn ineq mexpmeta1 hne1 mexpmeta2 hne2 = case (rawValue hne1, rawValue hne2) of (HNApp elr1 args1, HNApp elr2 args2) -> let ce = case (elr1, elr2) of (Var v1, Var v2) -> if v1 == v2 then Nothing else Just "comphn, elr, vars not equal" (Const c1, Const c2) -> if c1 == c2 then Nothing else Just "comphn, elr, consts not equal" (_, _) -> Just "comphn, elrs not equal" in case ce of Nothing -> compargs args1 args2 Just msg -> mpret $ Error msg (HNLam hid1 (Abs id1 b1), HNLam hid2 (Abs id2 b2)) -> comp False b1 b2 (HNLam _ (Abs _ b1), HNApp elr2 args2) -> f True b1 CALNil (seenUIds hne1) $ \res1 -> fhn True mexpmeta2 (WithSeenUIds (seenUIds hne2) $ HNApp (weak 1 elr2) (addtrailingargs (Clos [] $ NotM $ ALCons NotHidden{- arbitrary -} (NotM $ App Nothing (NotM OKVal) (Var 0) (NotM ALNil)) (NotM ALNil)) (weak 1 args2))) $ \res2 -> g res1 res2 (HNApp elr1 args1, HNLam _ (Abs _ b2)) -> fhn True mexpmeta1 (WithSeenUIds (seenUIds hne1) $ HNApp (weak 1 elr1) (addtrailingargs (Clos [] $ NotM $ ALCons NotHidden{- arbitrary -} (NotM $ App Nothing (NotM OKVal) (Var 0) (NotM ALNil)) (NotM ALNil)) (weak 1 args1))) $ \res1 -> f True b2 CALNil (seenUIds hne2) $ \res2 -> g res1 res2 {- (HNLam _ (Abs _ b1), HNApp uid2 elr2 args2) -> f True b1 CALNil $ \res1 -> g res1 (CMRigid mexpmeta2 (HNApp uid2 (weak 1 elr2) (addtrailingargs (Clos [] $ NotM $ ALCons NotHidden{- arbitrary -} (NotM $ App Nothing (NotM OKVal) (Var 0) (NotM ALNil)) (NotM ALNil)) (weak 1 args2)))) (HNApp uid1 elr1 args1, HNLam _ (Abs _ b2)) -> f True b2 CALNil $ \res2 -> g (CMRigid mexpmeta1 (HNApp uid1 (weak 1 elr1) (addtrailingargs (Clos [] $ NotM $ ALCons NotHidden{- arbitrary -} (NotM $ App Nothing (NotM OKVal) (Var 0) (NotM ALNil)) (NotM ALNil)) (weak 1 args1)))) res2 -} (HNPi hid1 _ it1 (Abs id1 ot1), HNPi hid2 _ it2 (Abs id2 ot2)) -> mpret $ And (Just [Term trs1, Term trs2]) (comp False it1 it2) (comp ineq ot1 ot2) (HNSort s1, HNSort s2) -> mpret $ case (s1, s2) of (Set i1, Set i2) -> if i1 == i2 || ineq && i1 > i2 then OK else Error "comphn, set levels not matching" (Set _, UnknownSort) -> OK (UnknownSort, Set _) -> OK (UnknownSort, UnknownSort) -> OK (Type, Set _) | ineq -> OK (Type, UnknownSort) | ineq -> OK _ -> __IMPOSSIBLE__ (HNApp (Const c1) _, _) -> case mexpmeta2 of Nothing -> mpret $ Error "comphn, not equal (2)" Just m2 -> mpret $ AddExtraRef "comphn: not equal, adding extra ref" m2 (extraref m2 (seenUIds hne1) c1) (_, HNApp (Const c2) _) -> case mexpmeta1 of Nothing -> mpret $ Error "comphn, not equal (3)" Just m1 -> mpret $ AddExtraRef "comphn: not equal, adding extra ref" m1 (extraref m1 (seenUIds hne2) c2) (_, _) -> mpret $ Error "comphn, not equal" compargs :: ICArgList o -> ICArgList o -> EE (MyPB o) compargs args1 args2 = mbpcase prioCompareArgList Nothing (hnarglist args1) $ \hnargs1 -> mbpcase prioCompareArgList Nothing (hnarglist args2) $ \hnargs2 -> case (hnargs1, hnargs2) of (HNALNil, HNALNil) -> mpret OK (HNALCons hid1 arg1 args1b, HNALCons hid2 arg2 args2b) -> mpret $ And (Just [Term trs1, Term trs2]) (comp False arg1 arg2) (compargs args1b args2b) (HNALConPar args1b, HNALCons _ _ args2b) -> compargs args1b args2b (HNALCons _ _ args1b, HNALConPar args2b) -> compargs args1b args2b (HNALConPar args1', HNALConPar args2') -> compargs args1' args2' (_, _) -> mpret $ Error $ "comphnargs, not equal" boringExp :: ICExp o -> EE Bool boringExp (Clos cl e) = do e <- expandbind e case e of Meta{} -> boringClos cl NotM e -> case e of App _ _ (Var v) as -> do as <- expandbind as case as of Meta{} -> return False NotM as -> case as of ALNil -> case doclos cl v of Left _ -> return True Right e -> boringExp e ALCons{} -> return False ALProj{} -> return False ALConPar{} -> return False _ -> return False boringClos :: [CAction o] -> EE Bool boringClos cl = liftM (all id) $ mapM f cl where f (Sub e) = boringExp e f Skip = return True f (Weak _) = return True boringArgs :: ICArgList o -> EE Bool boringArgs CALNil = return True boringArgs (CALConcat (Clos cl as) as2) = do b1 <- f cl as b2 <- boringArgs as2 return $ b1 && b2 where f cl as = do as <- expandbind as case as of Meta{} -> return False NotM as -> case as of ALNil -> return True ALCons _ a as -> do b1 <- boringExp (Clos cl a) b2 <- f cl as return $ b1 && b2 ALProj{} -> __IMPOSSIBLE__ ALConPar as -> f cl as -- --------------------------------- checkeliminand :: MExp o -> EE (MyPB o) checkeliminand = f [] [] where f uids used e = mmpcase (False, prioNo, Just (RIUsedVars uids used)) e $ \e -> case e of App uid _ elr@(Var{}) args -> fs (adduid uid uids) (elr : used) args App uid _ elr@(Const c) args -> do cd <- readIORef c case cdcont cd of Def _ _ (Just i) _ -> mpret $ Sidecondition (fs (adduid uid uids) (elr : used) args) (g i args) where g i as = mmpcase (False, prioNo, Nothing) as $ \as -> case as of ALNil -> mpret OK ALCons _ a as -> case i of 0 -> mmpcase (False, prioNo, Just RINotConstructor) a $ \_ -> mpret OK _ -> g (i - 1) as ALProj eas _ _ as -> mpret OK ALConPar as -> case i of 0 -> __IMPOSSIBLE__ _ -> g (i - 1) as _ -> fs (adduid uid uids) (elr : used) args Lam _ (Abs _ e) -> f uids (w used) e Pi uid _ _ e1 (Abs _ e2) -> mpret $ Sidecondition (f (adduid uid uids) used e1) (f (adduid uid uids) (w used) e2) Sort _ -> mpret OK AbsurdLambda{} -> mpret OK fs uids used as = mmpcase (False, prioNo, Nothing) as $ \as -> case as of ALNil -> mpret OK ALCons _ a as -> mpret $ Sidecondition (f uids used a) (fs uids used as) ALProj eas _ _ as -> mpret $ Sidecondition (fs uids used eas) (fs uids used as) ALConPar as -> fs uids used as w = map (\x -> case x of {Var v -> Var (v + 1); Const{} -> x}) adduid (Just uid) uids = uid : uids adduid Nothing uids = uids -- --------------------------------- maybeor :: Bool -> Prio -> IO (PB (RefInfo o)) -> IO (PB (RefInfo o)) -> IO (PB (RefInfo o)) maybeor _ _ mainalt _ = mainalt iotapossmeta :: ICExp o -> ICArgList o -> EE Bool iotapossmeta ce@(Clos cl _) cargs = do xs <- mapM ncaction cl y <- nccargs cargs return $ not (all id xs && y) where ncaction (Sub ce) = nonconstructor ce ncaction Skip = return True ncaction (Weak{}) = return True nccargs CALNil = return True nccargs (CALConcat (Clos cl margs) cargs) = do x <- ncmargs cl margs y <- nccargs cargs return $ x && y ncmargs cl (Meta m) = do mb <- readIORef (mbind m) case mb of Nothing -> return False Just x -> ncargs cl x ncmargs cl (NotM args) = ncargs cl args ncargs cl ALNil = return True ncargs cl (ALCons _ a args) = do x <- nonconstructor (Clos cl a) y <- ncmargs cl args return $ x && y ncargs _ (ALProj{}) = __IMPOSSIBLE__ ncargs cl (ALConPar args) = ncmargs cl args nonconstructor :: ICExp o -> EE Bool nonconstructor ce = do res <- hnc True ce CALNil [] case res of Blocked{} -> return False Failed{} -> return False NotB res -> case res of HNMeta ce _ _ -> do let (Clos _ (Meta m)) = ce infos <- extractblkinfos m if any (\info -> case info of {RINotConstructor -> True; _ -> False}) infos then do return True else return False -- return False -- return True -- ?? removes completeness - Yes, in DavidW1.additionRight HNDone{} -> do res <- hnn ce case res of NotB hne -> case rawValue hne of HNApp (Const c) _ -> do cd <- readIORef c case cdcont cd of Constructor{} -> return False _ -> return True _ -> return True Blocked m _ -> return False -- not necessary to do check here because already done by hnn (!! if it's known that m stands for an eliminator then it cannot be constructor so True instead) Failed _ -> return False meta_not_constructor :: ICExp o -> EE (MB Bool (RefInfo o)) meta_not_constructor a = mbcase (hnc True a CALNil []) $ \res -> case res of HNMeta ce _ _ -> do let (Clos _ (Meta m)) = ce infos <- extractblkinfos m if any (\info -> case info of {RINotConstructor -> True; _ -> False}) infos then do b <- iotapossmeta ce CALNil mbret $ not b else mbret False HNDone{} -> mbret False -- --------------------------------- calcEqRState :: EqReasoningConsts o -> MExp o -> EE (MyPB o) calcEqRState cs = f EqRSNone where f s e = mmpcase (False, prioNo, Just (RIEqRState s)) e $ \e -> case e of App _ _ (Const c) args -> case () of _ | c == eqrcBegin cs -> fs [EqRSNone, EqRSNone, EqRSNone, EqRSNone, EqRSChain] args _ | c == eqrcStep cs -> fs [EqRSNone, EqRSNone, EqRSNone, EqRSNone, EqRSNone, EqRSPrf1, EqRSChain] args _ | c == eqrcSym cs -> fs [EqRSNone, EqRSNone, EqRSNone, EqRSNone, EqRSPrf2] args _ | c == eqrcCong cs -> fs [EqRSNone, EqRSNone, EqRSNone, EqRSNone, EqRSNone, EqRSNone, EqRSNone, EqRSPrf3] args _ -> fs [] args App _ _ (Var{}) args -> fs [] args Lam _ (Abs _ b) -> f EqRSNone b Pi _ _ _ it (Abs _ ot) -> mpret $ Sidecondition (f EqRSNone it) (f EqRSNone ot) Sort{} -> mpret OK AbsurdLambda{} -> mpret OK fs ss args = mmpcase (False, prioNo, Nothing) args $ \args -> case (ss, args) of (_, ALNil) -> mpret OK (s : ss, ALCons _ a args) -> mpret $ Sidecondition (f s a) (fs ss args) ([], ALCons _ a args) -> mpret $ Sidecondition (f EqRSNone a) (fs [] args) (_, ALProj eas _ _ as) -> mpret $ Sidecondition (fs [] eas) (fs [] as) -- when eqr-hint is given manually, ss can be non-empty here (_ : ss, ALConPar args) -> fs ss args ([], ALConPar args) -> fs [] args -- --------------------------------- pickid :: MId -> MId -> MId pickid mid1@(Id _) _ = mid1 pickid _ mid2 = mid2 -- --------------------------------- tcSearch :: Bool -> Ctx o -> CExp o -> MExp o -> EE (MyPB o) tcSearch isdep ctx typ trm = mpret $ Sidecondition (checkeliminand trm) (tcExp isdep ctx typ trm) -- ---------------------------- Agda-2.5.3/src/full/Agda/Auto/Syntax.hs0000644000000000000000000003241513154613124015667 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Auto.Syntax where import Data.IORef import qualified Data.Set as Set #if __GLASGOW_HASKELL__ <= 708 import Control.Applicative ( (<$>), (<*>) ) #endif import Agda.Syntax.Common (Hiding) import Agda.Auto.NarrowingSearch #include "undefined.h" import Agda.Utils.Impossible -- | Unique identifiers for variable occurrences in unification. type UId o = Metavar (Exp o) (RefInfo o) data HintMode = HMNormal | HMRecCall data EqReasoningConsts o = EqReasoningConsts { eqrcId -- "_≡_" , eqrcBegin -- "begin_" , eqrcStep -- "_≡⟨_⟩_" , eqrcEnd -- "_∎" , eqrcSym -- "sym" , eqrcCong -- "cong" :: ConstRef o } data EqReasoningState = EqRSNone | EqRSChain | EqRSPrf1 | EqRSPrf2 | EqRSPrf3 deriving (Eq, Show) -- | The concrete instance of the 'blk' parameter in 'Metavar'. -- I.e., the information passed to the search control. data RefInfo o = RIEnv { rieHints :: [(ConstRef o, HintMode)] , rieDefFreeVars :: Nat -- ^ Nat - deffreevars -- (to make cost of using module parameters correspond to that of hints). , rieEqReasoningConsts :: Maybe (EqReasoningConsts o) } | RIMainInfo { riMainCxtLength :: Nat -- ^ Size of typing context in which meta was created. , riMainType :: HNExp o -- ^ Head normal form of type of meta. , riMainIota :: Bool -- ^ True if iota steps performed when normalising target type -- (used to put cost when traversing a definition -- by construction instantiation). } | RIUnifInfo [CAction o] (HNExp o) -- meta environment, opp hne | RICopyInfo (ICExp o) | RIIotaStep Bool -- True - semiflex | RIInferredTypeUnknown | RINotConstructor | RIUsedVars [UId o] [Elr o] | RIPickSubsvar | RIEqRState EqReasoningState | RICheckElim Bool -- isdep | RICheckProjIndex [ConstRef o] -- noof proj functions type MyPB o = PB (RefInfo o) type MyMB a o = MB a (RefInfo o) type Nat = Int data MId = Id String | NoId -- | Abstraction with maybe a name. -- -- Different from Agda, where there is also info -- whether function is constant. data Abs a = Abs MId a -- | Constant signatures. data ConstDef o = ConstDef { cdname :: String -- ^ For debug printing. , cdorigin :: o -- ^ Reference to the Agda constant. , cdtype :: MExp o -- ^ Type of constant. , cdcont :: DeclCont o -- ^ Constant definition. , cddeffreevars :: Nat -- ^ Free vars of the module where the constant is defined.. } -- contains no metas -- | Constant definitions. data DeclCont o = Def Nat [Clause o] (Maybe Nat) -- maybe an index to elimand argument (Maybe Nat) -- maybe index to elim arg if semiflex | Datatype [ConstRef o] -- constructors [ConstRef o] -- projection functions (in case it is a record) | Constructor Nat -- number of omitted args | Postulate type Clause o = ([Pat o], MExp o) data Pat o = PatConApp (ConstRef o) [Pat o] | PatVar String | PatExp -- ^ Dot pattern. {- TODO: projection patterns. | PatProj (ConstRef o) -- ^ Projection pattern. -} type ConstRef o = IORef (ConstDef o) -- | Head of application (elimination). data Elr o = Var Nat | Const (ConstRef o) deriving (Eq) getVar :: Elr o -> Maybe Nat getVar (Var n) = Just n getVar Const{} = Nothing getConst :: Elr o -> Maybe (ConstRef o) getConst (Const c) = Just c getConst Var{} = Nothing data Sort = Set Nat | UnknownSort | Type -- | Agsy's internal syntax. data Exp o = App { appUId :: Maybe (UId o) -- ^ Unique identifier of the head. , appOK :: OKHandle (RefInfo o) -- ^ This application has been type-checked. , appHead :: Elr o -- ^ Head. , appElims :: MArgList o -- ^ Arguments. } | Lam Hiding (Abs (MExp o)) -- ^ Lambda with hiding information. | Pi (Maybe (UId o)) Hiding Bool (MExp o) (Abs (MExp o)) -- ^ @True@ if possibly dependent (var not known to not occur). -- @False@ if non-dependent. | Sort Sort | AbsurdLambda Hiding -- ^ Absurd lambda with hiding information. dontCare :: Exp o dontCare = Sort UnknownSort -- | "Maybe expression": Expression or reference to meta variable. type MExp o = MM (Exp o) (RefInfo o) data ArgList o = ALNil -- ^ No more eliminations. | ALCons Hiding (MExp o) (MArgList o) -- ^ Application and tail. | ALProj (MArgList o) (MM (ConstRef o) (RefInfo o)) Hiding (MArgList o) -- ^ proj pre args, projfcn idx, tail | ALConPar (MArgList o) -- ^ Constructor parameter (missing in Agda). -- Agsy has monomorphic constructors. -- Inserted to cover glitch of polymorphic constructor -- applications coming from Agda type MArgList o = MM (ArgList o) (RefInfo o) data WithSeenUIds a o = WithSeenUIds { seenUIds :: [Maybe (UId o)] , rawValue :: a } type HNExp o = WithSeenUIds (HNExp' o) o data HNExp' o = HNApp (Elr o) (ICArgList o) | HNLam Hiding (Abs (ICExp o)) | HNPi Hiding Bool (ICExp o) (Abs (ICExp o)) | HNSort Sort -- | Head-normal form of 'ICArgList'. First entry is exposed. -- -- Q: Why are there no projection eliminations? data HNArgList o = HNALNil | HNALCons Hiding (ICExp o) (ICArgList o) | HNALConPar (ICArgList o) -- | Lazy concatenation of argument lists under explicit substitutions. data ICArgList o = CALNil | CALConcat (Clos (MArgList o) o) (ICArgList o) -- | An expression @a@ in an explicit substitution @[CAction a]@. type ICExp o = Clos (MExp o) o data Clos a o = Clos [CAction o] a type CExp o = TrBr (ICExp o) o data TrBr a o = TrBr [MExp o] a -- | Entry of an explicit substitution. -- -- An explicit substitution is a list of @CAction@s. -- This is isomorphic to the usual presentation where -- @Skip@ and @Weak@ would be constructors of exp. substs. data CAction o = Sub (ICExp o) -- ^ Instantation of variable. | Skip -- ^ For going under a binder, often called "Lift". | Weak Nat -- ^ Shifting substitution (going to a larger context). type Ctx o = [(MId, CExp o)] type EE = IO -- ------------------------------------------- detecteliminand :: [Clause o] -> Maybe Nat detecteliminand cls = case map cleli cls of [] -> Nothing (i:is) -> if all (i ==) is then i else Nothing where cleli (pats, _) = pateli 0 pats pateli i (PatConApp _ args : pats) = if all notcon (args ++ pats) then Just i else Nothing pateli i (_ : pats) = pateli (i + 1) pats pateli i [] = Nothing notcon PatConApp{} = False notcon _ = True detectsemiflex :: ConstRef o -> [Clause o] -> IO Bool detectsemiflex _ _ = return False -- disabled categorizedecl :: ConstRef o -> IO () categorizedecl c = do cd <- readIORef c case cdcont cd of Def narg cls _ _ -> do semif <- detectsemiflex c cls let elim = detecteliminand cls semifb = case (semif, elim) of (True, Just i) -> Just i -- just copying val of elim arg. this should be changed (_, _) -> Nothing writeIORef c (cd {cdcont = Def narg cls elim semifb}) _ -> return () -- ------------------------------------------- class MetaliseOKH t where metaliseOKH :: t -> IO t instance MetaliseOKH t => MetaliseOKH (MM t a) where metaliseOKH e = case e of Meta m -> return $ Meta m NotM e -> NotM <$> metaliseOKH e instance MetaliseOKH t => MetaliseOKH (Abs t) where metaliseOKH (Abs id b) = Abs id <$> metaliseOKH b instance MetaliseOKH (Exp o) where metaliseOKH e = case e of App uid okh elr args -> (\ m -> App uid m elr) <$> (Meta <$> initMeta) <*> metaliseOKH args Lam hid b -> Lam hid <$> metaliseOKH b Pi uid hid dep it ot -> Pi uid hid dep <$> metaliseOKH it <*> metaliseOKH ot Sort{} -> return e AbsurdLambda{} -> return e instance MetaliseOKH (ArgList o) where metaliseOKH e = case e of ALNil -> return ALNil ALCons hid a as -> ALCons hid <$> metaliseOKH a <*> metaliseOKH as ALProj eas idx hid as -> (\ eas -> ALProj eas idx hid) <$> metaliseOKH eas <*> metaliseOKH as ALConPar as -> ALConPar <$> metaliseOKH as metaliseokh :: MExp o -> IO (MExp o) metaliseokh = metaliseOKH -- ------------------------------------------- class ExpandMetas t where expandMetas :: t -> IO t instance ExpandMetas t => ExpandMetas (MM t a) where expandMetas t = case t of NotM e -> NotM <$> expandMetas e Meta m -> do mb <- readIORef (mbind m) case mb of Nothing -> return $ Meta m Just e -> NotM <$> expandMetas e instance ExpandMetas t => ExpandMetas (Abs t) where expandMetas (Abs id b) = Abs id <$> expandMetas b instance ExpandMetas (Exp o) where expandMetas t = case t of App uid okh elr args -> App uid okh elr <$> expandMetas args Lam hid b -> Lam hid <$> expandMetas b Pi uid hid dep it ot -> Pi uid hid dep <$> expandMetas it <*> expandMetas ot Sort{} -> return t AbsurdLambda{} -> return t instance ExpandMetas (ArgList o) where expandMetas e = case e of ALNil -> return ALNil ALCons hid a as -> ALCons hid <$> expandMetas a <*> expandMetas as ALProj eas idx hid as -> (\ a b -> ALProj a b hid) <$> expandMetas eas <*> expandbind idx <*> expandMetas as ALConPar as -> ALConPar <$> expandMetas as -- --------------------------------- addtrailingargs :: Clos (MArgList o) o -> ICArgList o -> ICArgList o addtrailingargs newargs CALNil = CALConcat newargs CALNil addtrailingargs newargs (CALConcat x xs) = CALConcat x (addtrailingargs newargs xs) -- --------------------------------- closify :: MExp o -> CExp o closify e = TrBr [e] (Clos [] e) sub :: MExp o -> CExp o -> CExp o -- sub e (Clos [] x) = Clos [Sub e] x sub e (TrBr trs (Clos (Skip : as) x)) = TrBr (e : trs) (Clos (Sub (Clos [] e) : as) x) {-sub e (Clos (Weak n : as) x) = if n == 1 then Clos as x else Clos (Weak (n - 1) : as) x-} sub _ _ = __IMPOSSIBLE__ subi :: MExp o -> ICExp o -> ICExp o subi e (Clos (Skip : as) x) = Clos (Sub (Clos [] e) : as) x subi _ _ = __IMPOSSIBLE__ weak :: Weakening t => Nat -> t -> t weak 0 = id weak n = weak' n class Weakening t where weak' :: Nat -> t -> t instance Weakening a => Weakening (TrBr a o) where weak' n (TrBr trs e) = TrBr trs (weak' n e) instance Weakening (Clos a o) where weak' n (Clos as x) = Clos (Weak n : as) x instance Weakening (ICArgList o) where weak' n e = case e of CALNil -> CALNil CALConcat a as -> CALConcat (weak' n a) (weak' n as) instance Weakening (Elr o) where weak' n = rename (n+) -- | Substituting for a variable. doclos :: [CAction o] -> Nat -> Either Nat (ICExp o) doclos = f 0 where -- ns is the number of weakenings f ns [] i = Left (ns + i) f ns (Weak n : xs) i = f (ns + n) xs i f ns (Sub s : _ ) 0 = Right (weak ns s) f ns (Skip : _ ) 0 = Left ns f ns (Skip : xs) i = f (ns + 1) xs (i - 1) f ns (Sub _ : xs) i = f ns xs (i - 1) -- | FreeVars class and instances freeVars :: FreeVars t => t -> Set.Set Nat freeVars = freeVarsOffset 0 class FreeVars t where freeVarsOffset :: Nat -> t -> Set.Set Nat instance (FreeVars a, FreeVars b) => FreeVars (a, b) where freeVarsOffset n (a, b) = Set.union (freeVarsOffset n a) (freeVarsOffset n b) instance FreeVars t => FreeVars (MM t a) where freeVarsOffset n e = freeVarsOffset n (rm __IMPOSSIBLE__ e) instance FreeVars t => FreeVars (Abs t) where freeVarsOffset n (Abs id e) = freeVarsOffset (n + 1) e instance FreeVars (Elr o) where freeVarsOffset n e = case e of Var v -> Set.singleton (v - n) Const{} -> Set.empty instance FreeVars (Exp o) where freeVarsOffset n e = case e of App _ _ elr args -> freeVarsOffset n (elr, args) Lam _ b -> freeVarsOffset n b Pi _ _ _ it ot -> freeVarsOffset n (it, ot) Sort{} -> Set.empty AbsurdLambda{} -> Set.empty instance FreeVars (ArgList o) where freeVarsOffset n es = case es of ALNil -> Set.empty ALCons _ e es -> freeVarsOffset n (e, es) ALConPar es -> freeVarsOffset n es ALProj{} -> __IMPOSSIBLE__ -- | Renaming Typeclass and instances rename :: Renaming t => (Nat -> Nat) -> t -> t rename = renameOffset 0 class Renaming t where renameOffset :: Nat -> (Nat -> Nat) -> t -> t instance (Renaming a, Renaming b) => Renaming (a, b) where renameOffset j ren (a, b) = (renameOffset j ren a, renameOffset j ren b) instance Renaming t => Renaming (MM t a) where renameOffset j ren e = NotM $ renameOffset j ren (rm __IMPOSSIBLE__ e) instance Renaming t => Renaming (Abs t) where renameOffset j ren (Abs id e) = Abs id $ renameOffset (j + 1) ren e instance Renaming (Elr o) where renameOffset j ren e = case e of Var v | v >= j -> Var (ren (v - j) + j) _ -> e instance Renaming (Exp o) where renameOffset j ren e = case e of App uid ok elr args -> uncurry (App uid ok) $ renameOffset j ren (elr, args) Lam hid e -> Lam hid (renameOffset j ren e) Pi a b c it ot -> uncurry (Pi a b c) $ renameOffset j ren (it, ot) Sort{} -> e AbsurdLambda{} -> e instance Renaming (ArgList o) where renameOffset j ren e = case e of ALNil -> ALNil ALCons hid a as -> uncurry (ALCons hid) $ renameOffset j ren (a, as) ALConPar as -> ALConPar (renameOffset j ren as) ALProj{} -> __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/Auto/NarrowingSearch.hs0000644000000000000000000004633013154613124017476 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} module Agda.Auto.NarrowingSearch where import Data.IORef hiding (writeIORef, modifyIORef) import qualified Data.IORef as NoUndo (writeIORef, modifyIORef) import Control.Monad.State import Control.Applicative hiding (Const(..), getConst) #include "undefined.h" import Agda.Utils.Impossible import Agda.Utils.Empty newtype Prio = Prio { getPrio :: Int } deriving (Eq, Ord, Num) class Trav a blk | a -> blk where trav :: Monad m => (forall b . Trav b blk => MM b blk -> m ()) -> a -> m () instance Trav a blk => Trav (MM a blk) blk where trav f me = f me data Term blk = forall a . Trav a blk => Term a -- | Result of type-checking. data Prop blk = OK -- ^ Success. | Error String -- ^ Definite failure. | forall a . AddExtraRef String (Metavar a blk) (Move' blk a) -- ^ Experimental. | And (Maybe [Term blk]) (MetaEnv (PB blk)) (MetaEnv (PB blk)) -- ^ Parallel conjunction of constraints. | Sidecondition (MetaEnv (PB blk)) (MetaEnv (PB blk)) -- ^ Experimental, related to 'mcompoint'. -- First arg is sidecondition. | Or Prio (MetaEnv (PB blk)) (MetaEnv (PB blk)) -- ^ Forking proof on something that is not part of the term language. -- E.g. whether a term will reduce or not. | ConnectHandle (OKHandle blk) (MetaEnv (PB blk)) -- ^ Obsolete. data OKVal = OKVal type OKHandle blk = MM OKVal blk type OKMeta blk = Metavar OKVal blk -- | Agsy's meta variables. -- -- @a@ the type of the metavariable (what it can be instantiated with). -- @blk@ the search control information (e.g. the scope of the meta). data Metavar a blk = Metavar { mbind :: IORef (Maybe a) -- ^ Maybe an instantiation (refinement). It is usually shallow, -- i.e., just one construct(or) with arguments again being metas. , mprincipalpresent :: IORef Bool -- ^ Does this meta block a principal constraint -- (i.e., a type-checking constraint). , mobs :: IORef [(QPB a blk, Maybe (CTree blk))] -- ^ List of observers, i.e., constraints blocked by this meta. , mcompoint :: IORef [SubConstraints blk] -- ^ Used for experiments with independence of subproofs. , mextrarefs :: IORef [Move' blk a] -- ^ Experimental. } hequalMetavar :: Metavar a1 blk1 -> Metavar a2 bkl2 -> Bool hequalMetavar m1 m2 = mprincipalpresent m1 == mprincipalpresent m2 instance Eq (Metavar a blk) where x == y = hequalMetavar x y newMeta :: IORef [SubConstraints blk] -> IO (Metavar a blk) newMeta mcompoint = do bind <- newIORef Nothing pp <- newIORef False obs <- newIORef [] erefs <- newIORef [] return $ Metavar bind pp obs mcompoint erefs initMeta :: IO (Metavar a blk) initMeta = do cp <- newIORef [] newMeta cp data CTree blk = CTree {ctpriometa :: IORef (PrioMeta blk), ctsub :: IORef (Maybe (SubConstraints blk)), ctparent :: IORef (Maybe (CTree blk)), -- Nothing - root cthandles :: IORef [OKMeta blk] } data SubConstraints blk = SubConstraints {scflip :: IORef Bool, sccomcount :: IORef Int, scsub1 :: CTree blk, scsub2 :: CTree blk } newCTree :: Maybe (CTree blk) -> IO (CTree blk) newCTree parent = do priometa <- newIORef (NoPrio False) sub <- newIORef Nothing rparent <- newIORef parent handles <- newIORef [] return $ CTree priometa sub rparent handles newSubConstraints :: CTree blk -> IO (SubConstraints blk) newSubConstraints node = do flip <- newIORef True -- False -- initially (and always) True, trying out prefer rightmost subterm when none have priority comcount <- newIORef 0 sub1 <- newCTree $ Just node sub2 <- newCTree $ Just node return $ SubConstraints flip comcount sub1 sub2 data PrioMeta blk = forall a . Refinable a blk => PrioMeta Prio (Metavar a blk) | NoPrio Bool -- True if subconstraint is done (all OK) instance Eq (PrioMeta blk) where NoPrio d1 == NoPrio d2 = d1 == d2 PrioMeta p1 m1 == PrioMeta p2 m2 = p1 == p2 && hequalMetavar m1 m2 _ == _ = False -- ----------------------- data Restore = forall a . Restore (IORef a) a type Undo = StateT [Restore] IO ureadIORef :: IORef a -> Undo a ureadIORef ptr = lift $ readIORef ptr uwriteIORef :: IORef a -> a -> Undo () uwriteIORef ptr newval = do oldval <- ureadIORef ptr modify (Restore ptr oldval :) lift $ NoUndo.writeIORef ptr newval umodifyIORef :: IORef a -> (a -> a) -> Undo () umodifyIORef ptr f = do oldval <- ureadIORef ptr modify (Restore ptr oldval :) lift $ NoUndo.writeIORef ptr (f oldval) ureadmodifyIORef :: IORef a -> (a -> a) -> Undo a ureadmodifyIORef ptr f = do oldval <- ureadIORef ptr modify (Restore ptr oldval :) lift $ NoUndo.writeIORef ptr (f oldval) return oldval runUndo :: Undo a -> IO a runUndo x = do (res, restores) <- runStateT x [] mapM_ (\(Restore ptr oldval) -> NoUndo.writeIORef ptr oldval) restores return res -- ----------------------- newtype RefCreateEnv blk a = RefCreateEnv { runRefCreateEnv :: StateT ((IORef [SubConstraints blk]), Int) IO a } instance Functor (RefCreateEnv blk) where fmap f = RefCreateEnv . fmap f . runRefCreateEnv instance Applicative (RefCreateEnv blk) where pure = RefCreateEnv . pure f <*> t = RefCreateEnv $ runRefCreateEnv f <*> runRefCreateEnv t instance Monad (RefCreateEnv blk) where return = pure t >>= f = RefCreateEnv $ runRefCreateEnv t >>= runRefCreateEnv . f newtype Cost = Cost { getCost :: Int } deriving (Num, Eq, Ord) data Move' blk a = Move { moveCost :: Cost , moveNext :: RefCreateEnv blk a } class Refinable a blk where refinements :: blk -> [blk] -> Metavar a blk -> IO [Move' blk a] newPlaceholder :: RefCreateEnv blk (MM a blk) newPlaceholder = RefCreateEnv $ do (mcompoint, c) <- get m <- lift $ newMeta mcompoint put (mcompoint, (c + 1)) return $ Meta m newOKHandle :: RefCreateEnv blk (OKHandle blk) newOKHandle = RefCreateEnv $ do (e, c) <- get cp <- lift $ newIORef [] m <- lift $ newMeta cp put (e, (c + 1)) return $ Meta m dryInstantiate :: RefCreateEnv blk a -> IO a dryInstantiate bind = evalStateT (runRefCreateEnv bind) (__IMPOSSIBLE__, 0) type BlkInfo blk = (Bool, Prio, Maybe blk) -- Bool - is principal data MM a blk = NotM a | Meta (Metavar a blk) rm :: Empty -> MM a b -> a rm e (NotM x) = x rm e Meta{} = absurd e type MetaEnv = IO data MB a blk = NotB a | forall b . Refinable b blk => Blocked (Metavar b blk) (MetaEnv (MB a blk)) | Failed String data PB blk = NotPB (Prop blk) | forall b . Refinable b blk => PBlocked (Metavar b blk) (BlkInfo blk) (MetaEnv (PB blk)) | forall b1 b2 . (Refinable b1 blk, Refinable b2 blk) => PDoubleBlocked (Metavar b1 blk) (Metavar b2 blk) (MetaEnv (PB blk)) data QPB b blk = QPBlocked (BlkInfo blk) (MetaEnv (PB blk)) | QPDoubleBlocked (IORef Bool) (MetaEnv (PB blk)) -- flag set True by first observer that continues mmcase :: Refinable a blk => MM a blk -> (a -> MetaEnv (MB b blk)) -> MetaEnv (MB b blk) mmcase x f = case x of NotM x -> f x x@(Meta m) -> do bind <- readIORef $ mbind m case bind of Just x -> f x Nothing -> return $ Blocked m (mmcase x f) mmmcase :: MM a blk -> MetaEnv (MB b blk) -> (a -> MetaEnv (MB b blk)) -> MetaEnv (MB b blk) mmmcase x fm f = case x of NotM x -> f x Meta m -> do bind <- readIORef $ mbind m case bind of Just x -> f x Nothing -> fm mmpcase :: Refinable a blk => BlkInfo blk -> MM a blk -> (a -> MetaEnv (PB blk)) -> MetaEnv (PB blk) mmpcase blkinfo x f = case x of NotM x -> f x x@(Meta m) -> do bind <- readIORef $ mbind m case bind of Just x -> f x Nothing -> return $ PBlocked m blkinfo (mmpcase __IMPOSSIBLE__ x f) -- blkinfo not needed because will be notb next time doubleblock :: (Refinable a blk, Refinable b blk) => MM a blk -> MM b blk -> MetaEnv (PB blk) -> MetaEnv (PB blk) doubleblock (Meta m1) (Meta m2) cont = return $ PDoubleBlocked m1 m2 cont doubleblock _ _ _ = __IMPOSSIBLE__ mbcase :: MetaEnv (MB a blk) -> (a -> MetaEnv (MB b blk)) -> MetaEnv (MB b blk) mbcase x f = do x' <- x case x' of NotB x -> f x Blocked m x -> return $ Blocked m (mbcase x f) Failed msg -> return $ Failed msg mbpcase :: Prio -> Maybe blk -> MetaEnv (MB a blk) -> (a -> MetaEnv (PB blk)) -> MetaEnv (PB blk) mbpcase prio bi x f = do x' <- x case x' of NotB x -> f x Blocked m x -> return $ PBlocked m (False, prio, bi) (mbpcase prio bi x f) Failed msg -> return $ NotPB $ Error msg mmbpcase :: MetaEnv (MB a blk) -> (forall b . Refinable b blk => MM b blk -> MetaEnv (PB blk)) -> (a -> MetaEnv (PB blk)) -> MetaEnv (PB blk) mmbpcase x fm f = do x' <- x case x' of NotB x -> f x Blocked m x -> fm (Meta m) Failed msg -> return $ NotPB $ Error msg waitok :: OKHandle blk -> MetaEnv (MB b blk) -> MetaEnv (MB b blk) waitok okh f = mmcase okh $ \b -> case b of -- principle constraint is never present for okhandle so it will not be refined OKVal -> f mbret :: a -> MetaEnv (MB a blk) mbret x = return $ NotB x mbfailed :: String -> MetaEnv (MB a blk) mbfailed msg = return $ Failed msg mpret :: Prop blk -> MetaEnv (PB blk) mpret p = return $ NotPB p expandbind :: MM a blk -> MetaEnv (MM a blk) expandbind x = case x of NotM{} -> return x Meta m -> do bind <- readIORef $ mbind m case bind of Just x -> return $ NotM x Nothing -> return x -- ----------------------- type HandleSol = IO () type SRes = Either Bool Int topSearch :: forall blk . IORef Int -> IORef Int -> HandleSol -> blk -> MetaEnv (PB blk) -> Cost -> Cost -> IO Bool topSearch ticks nsol hsol envinfo p searchdepth depthinterval = do depthreached <- newIORef False mainroot <- newCTree Nothing let searchSubProb :: [(CTree blk, Maybe (IORef Bool))] -> Cost -> IO SRes searchSubProb [] depth = do when (depth < depthinterval) $ do hsol n <- readIORef nsol NoUndo.writeIORef nsol $! n - 1 return $ Left True searchSubProb ((root, firstdone) : restprobs) depth = let search :: Cost -> IO SRes search depth = do pm <- readIORef $ ctpriometa root case pm of NoPrio False -> return $ Left False -- nothing to refine but not done, this can happen when eq constraints are passed along with main constraint in agdaplugin NoPrio True -> searchSubProb restprobs depth -- ?? what should depth be PrioMeta _ m -> do let carryon = fork m depth sub <- readIORef $ ctsub root case sub of Nothing -> carryon Just sc -> do let sub1 = scsub1 sc sub2 = scsub2 sc pm1 <- readIORef $ ctpriometa sub1 pm2 <- readIORef $ ctpriometa sub2 let split = carryon -- split disabled case pm1 of NoPrio True -> split _ -> case pm2 of NoPrio True -> split _ -> do comc <- readIORef $ sccomcount sc case comc of 0 -> split _ -> carryon fork :: forall a. Refinable a blk => Metavar a blk -> Cost -> IO SRes fork m depth = do blkinfos <- extractblkinfos m refs <- refinements envinfo blkinfos m f refs where f :: [Move' blk a] -> IO SRes f [] = do erefs <- readIORef $ mextrarefs m case erefs of [] -> return (Left False) _ -> do NoUndo.writeIORef (mextrarefs m) [] f erefs f (Move cost bind : binds) = hsres (refine m bind (depth - cost)) (f binds) hsres :: IO SRes -> IO SRes -> IO SRes hsres x1 x2 = do res <- x1 case res of Right _ -> return res Left found -> do n <- readIORef nsol if n == 0 then return res else do res2 <- x2 case res2 of Right _ -> if found then __IMPOSSIBLE__ else return res2 Left found2 -> return $ Left (found || found2) refine :: Metavar a blk -> RefCreateEnv blk a -> Cost -> IO SRes refine _ _ depthleft | depthleft < 0 = do NoUndo.writeIORef depthreached True return $ Left False refine m bind depthleft = runUndo $ do t <- ureadIORef ticks lift $ NoUndo.writeIORef ticks $! t + 1 (bind, (_, nnewmeta)) <- lift $ runStateT (runRefCreateEnv bind) (mcompoint m, 0) uwriteIORef (mbind m) (Just bind) mcomptr <- ureadIORef $ mcompoint m mapM_ (\comptr -> umodifyIORef (sccomcount comptr) (+ (nnewmeta - 1)) -- umodifyIORef (scflip comptr) not -- don't flip now since trying prefer rightmost subterm if non have prio ) mcomptr obs <- ureadIORef (mobs m) res <- recalcs obs case res of True -> -- failed return $ Left False False -> lift $ search depthleft -- succeeded doit = do res <- search depth return $ case res of Right n -> case firstdone of Nothing -> if n == 0 then Left False else Right (n - 1) Just _ -> Right (n + 1) res@(Left True) -> res res@(Left False) -> case firstdone of Nothing -> res Just _ -> Right 0 in case firstdone of Nothing -> doit Just rdone -> do done <- readIORef rdone if done then searchSubProb restprobs depth else do NoUndo.writeIORef rdone True doit runUndo $ do res <- reccalc p (Just mainroot) case res of True -> -- failed immediately return False False -> do Left solfound <- lift $ searchSubProb [(mainroot, Nothing)] searchdepth dr <- lift $ readIORef depthreached return dr extractblkinfos :: Metavar a blk -> IO [blk] extractblkinfos m = do obs <- readIORef $ mobs m return $ f obs where f [] = [] f ((QPBlocked (_,_,mblkinfo) _, _) : cs) = case mblkinfo of Nothing -> f cs Just blkinfo -> blkinfo : f cs f ((QPDoubleBlocked{}, _) : cs) = f cs recalcs :: [(QPB a blk, Maybe (CTree blk))] -> Undo Bool recalcs [] = return False recalcs (c : cs) = seqc (recalc c) (recalcs cs) seqc :: Undo Bool -> Undo Bool -> Undo Bool seqc x y = do res1 <- x case res1 of res1@True -> return res1 False -> y recalc :: (QPB a blk, Maybe (CTree blk)) -> Undo Bool recalc (con, node) = case con of QPBlocked _ cont -> reccalc cont node QPDoubleBlocked flag cont -> do fl <- ureadIORef flag if fl then return False else do uwriteIORef flag True reccalc cont node reccalc :: MetaEnv (PB blk) -> Maybe (CTree blk) -> Undo Bool reccalc cont node = do res <- calc cont node case res of Nothing -> return True Just pendhandles -> foldM (\res1 h -> case res1 of True -> return res1 False -> do uwriteIORef (mbind h) $ Just OKVal obs <- ureadIORef (mobs h) recalcs obs ) False pendhandles calc :: forall blk . MetaEnv (PB blk) -> Maybe (CTree blk) -> Undo (Maybe [OKMeta blk]) calc cont node = do res <- donewp node cont case res of Just (_, pendhandles) -> do pendhandles2 <- case node of Just node -> propagatePrio node Nothing -> return [] return $ Just (pendhandles ++ pendhandles2) Nothing -> return Nothing where storeprio (Just node) pm pendhandles = do pendhandles' <- case pm of NoPrio True -> do handles <- ureadIORef (cthandles node) return $ handles ++ pendhandles _ -> return pendhandles uwriteIORef (ctpriometa node) pm return $ Just (pm, pendhandles') storeprio Nothing _ _ = return $ Just (NoPrio False, []) donewp node p = do bp <- lift p case bp of NotPB p -> doprop node p PBlocked m blkinfo cont -> do oldobs <- ureadmodifyIORef (mobs m) ((QPBlocked blkinfo cont, node) :) let (princ, prio, _) = blkinfo pp <- ureadIORef (mprincipalpresent m) when (princ && not pp) $ do uwriteIORef (mprincipalpresent m) True mapM_ (\(qpb, node) -> case node of Just node -> case qpb of QPBlocked (_, prio, _) _ -> do uwriteIORef (ctpriometa node) (PrioMeta prio m) propagatePrio node QPDoubleBlocked flag _ -> return [] Nothing -> return [] ) oldobs if pp || princ then storeprio node (PrioMeta prio m) [] else storeprio node (NoPrio False) [] PDoubleBlocked m1 m2 cont -> do flag <- lift $ newIORef False let newobs = ((QPDoubleBlocked flag cont, node) :) umodifyIORef (mobs m1) newobs umodifyIORef (mobs m2) newobs storeprio node (NoPrio False) [] doprop node p = case p of OK -> storeprio node (NoPrio True) [] Error _ -> return Nothing AddExtraRef _ m eref -> do lift $ NoUndo.modifyIORef (mextrarefs m) (eref :) return Nothing And coms p1 p2 -> do let Just jnode = node sc <- lift $ newSubConstraints jnode uwriteIORef (ctsub jnode) $ Just sc ndep <- case coms of Nothing -> return 1 -- no metas pointing to it so will never decrement to 0 Just coms -> return 1 -- dito lift $ NoUndo.writeIORef (sccomcount sc) ndep -- OK since sc was just created resp1 <- donewp (Just $ scsub1 sc) p1 case resp1 of Just (pm1, phs1) -> do resp2 <- donewp (Just $ scsub2 sc) p2 case resp2 of Just (pm2, phs2) -> storeprio node (choosePrioMeta False pm1 pm2) (phs1 ++ phs2) resp2@Nothing -> return resp2 resp1@Nothing -> return resp1 Sidecondition sidep mainp -> do resp1 <- donewp Nothing sidep case resp1 of Just{} -> do resp2 <- donewp node mainp case resp2 of Just (pm2, phs2) -> storeprio node pm2 phs2 resp2@Nothing -> return resp2 resp1@Nothing -> return resp1 Or prio p1 p2 -> do cm <- lift $ initMeta donewp node (choose (Meta cm) prio p1 p2) ConnectHandle (Meta handle) p' -> do let Just jnode = node umodifyIORef (cthandles jnode) (handle :) donewp node p' ConnectHandle (NotM _) _ -> __IMPOSSIBLE__ choosePrioMeta :: Bool -> PrioMeta blk -> PrioMeta blk -> PrioMeta blk choosePrioMeta flip pm1@(PrioMeta p1 _) pm2@(PrioMeta p2 _) = if p1 > p2 then pm1 else if p2 > p1 then pm2 else if flip then pm2 else pm1 choosePrioMeta _ pm@(PrioMeta _ _) (NoPrio _) = pm choosePrioMeta _ (NoPrio _) pm@(PrioMeta _ _) = pm choosePrioMeta _ (NoPrio d1) (NoPrio d2) = NoPrio (d1 && d2) propagatePrio :: CTree blk -> Undo [OKMeta blk] propagatePrio node = do parent <- lift $ readIORef $ ctparent node case parent of Nothing -> return [] Just parent -> do Just sc <- ureadIORef (ctsub parent) pm1 <- ureadIORef $ ctpriometa $ scsub1 sc pm2 <- ureadIORef $ ctpriometa $ scsub2 sc flip <- ureadIORef $ scflip sc let pm = choosePrioMeta flip pm1 pm2 opm <- ureadIORef (ctpriometa parent) if (not (pm == opm)) then do uwriteIORef (ctpriometa parent) pm phs <- case pm of NoPrio True -> ureadIORef (cthandles parent) _ -> return [] phs2 <- propagatePrio parent return $ phs ++ phs2 else return [] data Choice = LeftDisjunct | RightDisjunct choose :: MM Choice blk -> Prio -> MetaEnv (PB blk) -> MetaEnv (PB blk) -> MetaEnv (PB blk) choose c prio p1 p2 = mmpcase (True, prio, Nothing) c $ \c -> case c of LeftDisjunct -> p1 RightDisjunct -> p2 instance Refinable Choice blk where refinements _ x _ = return $ Move 0 . return <$> [LeftDisjunct, RightDisjunct] instance Refinable OKVal blk where refinements _ _ _ = __IMPOSSIBLE__ -- OKVal should never be refined -- ------------------------------------ Agda-2.5.3/src/full/Agda/Auto/Convert.hs0000644000000000000000000007367313154613124016034 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Auto.Convert where import Control.Applicative hiding (getConst, Const(..)) import Data.IORef import Data.Maybe (catMaybes) import Data.Map (Map) import qualified Data.Map as Map import Data.Traversable (traverse) import Control.Monad.State import Agda.Syntax.Common (Hiding(..), getHiding) import Agda.Syntax.Concrete (exprFieldA) import qualified Agda.Syntax.Internal as I import qualified Agda.Syntax.Internal.Pattern as IP import qualified Agda.Syntax.Common as Cm import qualified Agda.Syntax.Abstract.Name as AN import qualified Agda.Syntax.Abstract as A import qualified Agda.Syntax.Position as SP import qualified Agda.TypeChecking.Monad.Base as MB import Agda.TypeChecking.Monad.Signature (getConstInfo, getDefFreeVars, ignoreAbstractMode) import Agda.Utils.Permutation (Permutation(Perm), permute, takeP, compactP) import Agda.TypeChecking.Level (reallyUnLevelView) import Agda.TypeChecking.Monad.Base (mvJudgement, mvPermutation, getMetaInfo, ctxEntry, envContext, clEnv) import Agda.TypeChecking.Monad.MetaVars (lookupMeta, withMetaInfo, lookupInteractionPoint) import Agda.TypeChecking.Monad.Context (getContextArgs) import Agda.TypeChecking.Monad.Constraints (getAllConstraints) import Agda.TypeChecking.Substitute (applySubst, renamingR) import Agda.TypeChecking.Telescope (piApplyM) import qualified Agda.TypeChecking.Substitute as I (absBody) import Agda.TypeChecking.Reduce (normalise, instantiate) import Agda.TypeChecking.EtaContract (etaContract) import Agda.TypeChecking.Monad.Builtin (constructorForm) import Agda.TypeChecking.Free as Free (freeIn) import Agda.TypeChecking.Errors ( stringTCErr ) import Agda.Interaction.MakeCase (getClauseForIP) import Agda.Auto.NarrowingSearch import Agda.Auto.Syntax hiding (getConst) import Agda.Auto.CaseSplit hiding (lift) import Agda.Utils.Either import Agda.Utils.Except ( ExceptT , MonadError(throwError) ) import Agda.Utils.Lens import Agda.Utils.Pretty ( prettyShow ) import Agda.Utils.Impossible #include "undefined.h" data Hint = Hint { hintIsConstructor :: Bool , hintQName :: I.QName } type O = (Maybe Int, AN.QName) -- Nothing - Def -- Just npar - Con with npar parameters which don't appear in Agda data TMode = TMAll -- can be extended to distinguish between different modes (all, only def) deriving Eq type MapS a b = (Map a b, [a]) initMapS :: MapS a b initMapS = (Map.empty, []) popMapS :: (S -> (a, [b])) -> ((a, [b]) -> S -> S) -> TOM (Maybe b) popMapS r w = do (m, xs) <- gets r case xs of [] -> return Nothing (x:xs) -> do modify (w (m, xs)) return $ Just x data S = S {sConsts :: MapS AN.QName (TMode, ConstRef O), sMetas :: MapS I.MetaId (Metavar (Exp O) (RefInfo O), Maybe (MExp O, [MExp O]), [I.MetaId]), sEqs :: MapS Int (Maybe (Bool, MExp O, MExp O)), sCurMeta :: Maybe I.MetaId, sMainMeta :: I.MetaId } type TOM = StateT S MB.TCM type MOT = ExceptT String IO tomy :: I.MetaId -> [Hint] -> [I.Type] -> MB.TCM ([ConstRef O] , [MExp O] , Map I.MetaId (Metavar (Exp O) (RefInfo O), MExp O, [MExp O], [I.MetaId]) , [(Bool, MExp O, MExp O)] , Map AN.QName (TMode, ConstRef O)) tomy imi icns typs = do eqs <- getEqs let r :: [AN.QName] -> TOM [AN.QName] r projfcns = do nxt <- popMapS sConsts (\x y -> y {sConsts = x}) case nxt of Just cn -> do cmap <- fst `liftM` gets sConsts let (mode, c) = cmap Map.! cn def <- lift $ getConstInfo cn let typ = MB.defType def defn = MB.theDef def typ <- lift $ normalise typ typ' <- convert typ let clausesToDef clauses = do clauses' <- convert clauses let narg = case clauses of [] -> 0 I.Clause {I.namedClausePats = xs} : _ -> length xs return (Def narg clauses' Nothing Nothing, []) (cont, projfcns2) <- case defn of MB.Axiom {} -> return (Postulate, []) MB.AbstractDefn{} -> return (Postulate, []) MB.Function {MB.funClauses = clauses} -> clausesToDef clauses -- MB.Primitive {MB.primClauses = []} -> throwError $ strMsg "Auto: Primitive functions are not supported" -- Andreas, 2013-06-17 breaks interaction/AutoMisc MB.Primitive {MB.primClauses = clauses} -> clausesToDef clauses MB.Datatype {MB.dataCons = cons} -> do cons2 <- mapM (\con -> getConst True con TMAll) cons return (Datatype cons2 [], []) MB.Record {MB.recFields = fields, MB.recTel = tel} -> do -- the value of recPars seems unreliable or don't know what it signifies let pars n (I.El _ (I.Pi it typ)) = Cm.Arg (Cm.domInfo it) (I.var n) : pars (n - 1) (I.unAbs typ) pars n (I.El s (I.Shared p)) = pars n (I.El s (I.derefPtr p)) pars _ (I.El _ _) = [] contyp npar I.EmptyTel = I.El (I.mkType 0 {- arbitrary -}) $ I.Def cn $ map I.Apply $ pars (npar - 1) typ contyp npar (I.ExtendTel it (I.Abs v tel)) = I.El (I.mkType 0 {- arbitrary -}) (I.Pi it (I.Abs v (contyp (npar + 1) tel))) contyp npar (I.ExtendTel it I.NoAbs{}) = __IMPOSSIBLE__ contyp' <- convert $ contyp 0 tel cc <- lift $ liftIO $ readIORef c let Datatype [con] [] = cdcont cc lift $ liftIO $ modifyIORef con (\cdef -> cdef {cdtype = contyp'}) projfcns <- mapM (\name -> getConst False name TMAll) (map Cm.unArg fields) return (Datatype [con] projfcns, []{-map snd fields-}) MB.Constructor {MB.conData = dt} -> do _ <- getConst False dt TMAll -- make sure that datatype is included cc <- lift $ liftIO $ readIORef c let (Just nomi, _) = cdorigin cc return (Constructor (nomi - cddeffreevars cc), []) lift $ liftIO $ modifyIORef c (\cdef -> cdef {cdtype = typ', cdcont = cont}) r $ projfcns2 ++ projfcns Nothing -> do nxt <- popMapS sMetas (\x y -> y {sMetas = x}) case nxt of Just mi -> do mapM_ (\((_, e, i), eqi) -> do when (fmExp mi e || fmExp mi i) $ do (eqsm, eqsl) <- gets sEqs when (Map.notMember eqi eqsm) $ do modify $ \s -> s {sEqs = (Map.insert eqi Nothing eqsm, eqi : eqsl)} ) (zip eqs [0..]) mv <- lift $ lookupMeta mi msol <- case MB.mvInstantiation mv of MB.InstV{} -> lift $ withMetaInfo (getMetaInfo mv) $ do args <- getContextArgs --sol <- norm (I.MetaV mi args) sol <- instantiate $ I.MetaV mi $ map I.Apply $ permute (takeP (length args) $ mvPermutation mv) args return $ Just sol _ -> return Nothing case msol of Nothing -> return () Just sol -> do m <- getMeta mi sol' <- convert sol modify $ \s -> s {sEqs = (Map.insert (Map.size (fst $ sEqs s)) (Just (False, Meta m, sol')) (fst $ sEqs s), snd $ sEqs s)} let tt = MB.jMetaType $ mvJudgement mv minfo = getMetaInfo mv localVars = map (snd . Cm.unDom . ctxEntry) . envContext . clEnv $ minfo (targettype, localVars) <- lift $ withMetaInfo minfo $ do vs <- getContextArgs targettype <- tt `piApplyM` permute (takeP (length vs) $ mvPermutation mv) vs targettype <- normalise targettype localVars <- mapM normalise localVars return (targettype, localVars) modify (\s -> s {sCurMeta = Just mi}) typ' <- convert targettype ctx' <- mapM convert localVars modify (\s -> s {sCurMeta = Nothing}) modify (\s -> s {sMetas = (Map.adjust (\(m, _, deps) -> (m, Just (typ', ctx'), deps)) mi (fst $ sMetas s), snd $ sMetas s)}) r projfcns Nothing -> do nxt <- popMapS sEqs (\x y -> y {sEqs = x}) case nxt of Just eqi -> do let (ineq, e, i) = eqs !! eqi e' <- convert e i' <- convert i modify (\s -> s {sEqs = (Map.adjust (\_ -> Just (ineq, e', i')) eqi (fst $ sEqs s), snd $ sEqs s)}) r projfcns Nothing -> return projfcns ((icns', typs'), s) <- runStateT (do _ <- getMeta imi icns' <- mapM (\ (Hint iscon name) -> getConst iscon name TMAll) icns typs' <- mapM convert typs projfcns <- r [] projfcns' <- mapM (\name -> getConst False name TMAll) projfcns [] <- r [] return (projfcns' ++ icns', typs') ) (S {sConsts = initMapS, sMetas = initMapS, sEqs = initMapS, sCurMeta = Nothing, sMainMeta = imi}) lift $ liftIO $ mapM_ categorizedecl icns' return (icns', typs', Map.map flatten (fst (sMetas s)), map fromJust $ Map.elems (fst (sEqs s)), fst (sConsts s)) where flatten (x, Just (y, z), w) = (x, y, z, w) flatten (x, Nothing, w) = __IMPOSSIBLE__ fromJust (Just x) = x fromJust Nothing = __IMPOSSIBLE__ getConst :: Bool -> AN.QName -> TMode -> TOM (ConstRef O) getConst iscon name mode = do def <- lift $ getConstInfo name case MB.theDef def of MB.Record {MB.recConHead = con} -> do let conname = I.conName con cmap <- fst `liftM` gets sConsts case Map.lookup name cmap of Just (mode', c) -> if iscon then do cd <- lift $ liftIO $ readIORef c let Datatype [con] _ = cdcont cd return con else return c Nothing -> do mainm <- gets sMainMeta dfv <- lift $ getdfv mainm name let nomi = I.arity (MB.defType def) ccon <- lift $ liftIO $ newIORef (ConstDef {cdname = prettyShow name ++ ".CONS", cdorigin = (Just nomi, conname), cdtype = __IMPOSSIBLE__, cdcont = Constructor (nomi - dfv), cddeffreevars = dfv}) -- ?? correct value of deffreevars for records? c <- lift $ liftIO $ newIORef (ConstDef {cdname = prettyShow name, cdorigin = (Nothing, name), cdtype = __IMPOSSIBLE__, cdcont = Datatype [ccon] [], cddeffreevars = dfv}) -- ?? correct value of deffreevars for records? modify (\s -> s {sConsts = (Map.insert name (mode, c) cmap, name : snd (sConsts s))}) return $ if iscon then ccon else c _ -> do cmap <- fst `liftM` gets sConsts case Map.lookup name cmap of Just (mode', c) -> return c Nothing -> do (miscon, sname) <- if iscon then do let MB.Constructor {MB.conPars = npar, MB.conData = dname} = MB.theDef def return (Just npar, prettyShow dname ++ "." ++ prettyShow (I.qnameName name)) else return (Nothing, prettyShow name) mainm <- gets sMainMeta dfv <- lift $ getdfv mainm name c <- lift $ liftIO $ newIORef (ConstDef {cdname = sname, cdorigin = (miscon, name), cdtype = __IMPOSSIBLE__, cdcont = __IMPOSSIBLE__, cddeffreevars = dfv}) modify (\s -> s {sConsts = (Map.insert name (mode, c) cmap, name : snd (sConsts s))}) return c getdfv :: I.MetaId -> A.QName -> MB.TCM Cm.Nat getdfv mainm name = do mv <- lookupMeta mainm withMetaInfo (getMetaInfo mv) $ getDefFreeVars name getMeta :: I.MetaId -> TOM (Metavar (Exp O) (RefInfo O)) getMeta name = do mmap <- fst `liftM` gets sMetas case Map.lookup name mmap of Just (m, _, _) -> return m Nothing -> do m <- lift $ liftIO initMeta modify (\s -> s {sMetas = (Map.insert name (m, Nothing, []) mmap, name : snd (sMetas s))}) return m getEqs :: MB.TCM [(Bool, I.Term, I.Term)] getEqs = do eqs <- getAllConstraints let r = mapM (\eqc -> do neqc <- normalise eqc case MB.clValue $ MB.theConstraint neqc of MB.ValueCmp ineq _ i e -> do ei <- etaContract i ee <- etaContract e return [(tomyIneq ineq, ee, ei)] MB.TypeCmp ineq i e -> do I.El _ ei <- etaContract i I.El _ ee <- etaContract e return [(tomyIneq ineq, ee, ei)] MB.Guarded (MB.UnBlock _) pid -> return [] _ -> return [] ) eqs' <- r eqs return $ concat eqs' copatternsNotImplemented :: MB.TCM a copatternsNotImplemented = MB.typeError $ MB.NotImplemented $ "The Agda synthesizer (Agsy) does not support copatterns yet" literalsNotImplemented :: MB.TCM a literalsNotImplemented = MB.typeError $ MB.NotImplemented $ "The Agda synthesizer (Agsy) does not support literals yet" class Conversion m a b where convert :: a -> m b instance Conversion TOM [I.Clause] [([Pat O], MExp O)] where convert = fmap catMaybes . mapM convert instance Conversion TOM I.Clause (Maybe ([Pat O], MExp O)) where convert cl = do let -- Jesper, 2016-07-28: -- I can't figure out if this should be the old or new -- clause body (i.e. relative to the positions of pattern variables or -- relative to the clauseTel). Both options pass the test suite, so I -- have the impression it doesn't actually matter. -- ALTERNATIVE CODE: -- perm = fromMaybe __IMPOSSIBLE__ $ IP.clausePerm cl -- body = applySubst (renamingR perm) $ I.clauseBody cl body = I.clauseBody cl pats = I.clausePats cl pats' <- mapM convert (IP.unnumberPatVars pats :: [Cm.Arg I.Pattern]) body' <- traverse convert =<< lift (normalise body) return $ (pats',) <$> body' instance Conversion TOM (Cm.Arg I.Pattern) (Pat O) where convert p = case Cm.unArg p of I.VarP n -> return $ PatVar (show n) I.DotP _ -> return $ PatVar "_" -- because Agda includes these when referring to variables in the body I.AbsurdP{} -> return $ PatVar I.absurdPatternName I.ConP con _ pats -> do let n = I.conName con c <- getConst True n TMAll pats' <- mapM (convert . fmap Cm.namedThing) pats def <- lift $ getConstInfo n cc <- lift $ liftIO $ readIORef c let Just npar = fst $ cdorigin cc return $ PatConApp c (replicate npar PatExp ++ pats') -- UNSUPPORTED CASES I.ProjP{} -> lift copatternsNotImplemented I.LitP _ -> lift literalsNotImplemented instance Conversion TOM I.Type (MExp O) where convert (I.El _ t) = convert t -- sort info is thrown away instance Conversion TOM I.Term (MExp O) where convert v0 = case I.unSpine v0 of I.Var v es -> do let Just as = I.allApplyElims es as' <- convert as return $ NotM $ App Nothing (NotM OKVal) (Var v) as' I.Lam info b -> do b' <- convert (I.absBody b) return $ NotM $ Lam (getHiding info) (Abs (Id $ I.absName b) b') t@I.Lit{} -> do t <- lift $ constructorForm t case t of I.Lit{} -> lift literalsNotImplemented _ -> convert t I.Level l -> convert =<< lift (reallyUnLevelView l) I.Def name es -> do let Just as = I.allApplyElims es c <- getConst False name TMAll as' <- convert as return $ NotM $ App Nothing (NotM OKVal) (Const c) as' I.Con con ci as -> do let name = I.conName con c <- getConst True name TMAll as' <- convert as def <- lift $ getConstInfo name cc <- lift $ liftIO $ readIORef c let Just npar = fst $ cdorigin cc return $ NotM $ App Nothing (NotM OKVal) (Const c) (foldl (\x _ -> NotM $ ALConPar x) as' [1..npar]) I.Pi (Cm.Dom{domInfo = info, unDom = x}) b -> do let y = I.absBody b name = I.absName b x' <- convert x y' <- convert y return $ NotM $ Pi Nothing (getHiding info) (Free.freeIn 0 y) x' (Abs (Id name) y') I.Sort (I.Type (I.Max [I.ClosedLevel l])) -> return $ NotM $ Sort $ Set $ fromIntegral l I.Sort _ -> return $ NotM $ Sort UnknownSort t@I.MetaV{} -> do t <- lift $ instantiate t case t of I.MetaV mid _ -> do mcurmeta <- gets sCurMeta case mcurmeta of Nothing -> return () Just curmeta -> modify $ \ s -> s { sMetas = ( Map.adjust (\(m, x, deps) -> (m, x, mid : deps)) curmeta (fst $ sMetas s) , snd $ sMetas s ) } m <- getMeta mid return $ Meta m _ -> convert t I.DontCare _ -> return $ NotM dontCare I.Shared p -> convert $ I.derefPtr p instance Conversion TOM a b => Conversion TOM (Cm.Arg a) (Hiding, b) where convert (Cm.Arg info a) = (getHiding info,) <$> convert a instance Conversion TOM I.Args (MM (ArgList O) (RefInfo O)) where convert as = NotM . foldr (\ (hid,t) -> ALCons hid t . NotM) ALNil <$> mapM convert as tomyIneq :: MB.Comparison -> Bool tomyIneq MB.CmpEq = False tomyIneq MB.CmpLeq = True -- --------------------------------------------- fmType :: I.MetaId -> I.Type -> Bool fmType m (I.El _ t) = fmExp m t fmExp :: I.MetaId -> I.Term -> Bool fmExp m (I.Var _ as) = fmExps m $ I.argsFromElims as fmExp m (I.Lam _ b) = fmExp m (I.unAbs b) fmExp m (I.Lit _) = False fmExp m (I.Level (I.Max as)) = any (fmLevel m) as fmExp m (I.Def _ as) = fmExps m $ I.argsFromElims as fmExp m (I.Con _ ci as) = fmExps m as fmExp m (I.Pi x y) = fmType m (Cm.unDom x) || fmType m (I.unAbs y) fmExp m (I.Sort _) = False fmExp m (I.MetaV mid _) = mid == m fmExp m (I.DontCare _) = False fmExp m (I.Shared p) = fmExp m $ I.derefPtr p fmExps :: I.MetaId -> I.Args -> Bool fmExps m [] = False fmExps m (a : as) = fmExp m (Cm.unArg a) || fmExps m as fmLevel :: I.MetaId -> I.PlusLevel -> Bool fmLevel m I.ClosedLevel{} = False fmLevel m (I.Plus _ l) = case l of I.MetaLevel m' _ -> m == m' I.NeutralLevel _ v -> fmExp m v I.BlockedLevel _ v -> fmExp m v I.UnreducedLevel v -> fmExp m v -- --------------------------------------------- icnvh :: Hiding -> Cm.ArgInfo icnvh h = Cm.setHiding h $ Cm.setOrigin o $ Cm.defaultArgInfo where -- Andreas, 2017-01-18, issue #819. -- Visible arguments are made UserWritten, -- otherwise they might not be printed in patterns. o = case h of NotHidden -> Cm.UserWritten Instance{} -> Cm.Inserted Hidden -> Cm.Inserted -- --------------------------------------------- instance Conversion MOT a b => Conversion MOT (MM a (RefInfo O)) b where convert meta = case meta of NotM a -> convert a Meta m -> do ma <- lift $ readIORef $ mbind m case ma of Nothing -> throwError "meta not bound" Just a -> convert a instance Conversion MOT a b => Conversion MOT (Abs a) (I.Abs b) where convert (Abs mid t) = I.Abs id <$> convert t where id = case mid of NoId -> "x" Id id -> id instance Conversion MOT (Exp O) I.Type where convert e = I.El (I.mkType 0) <$> convert e -- 0 is arbitrary, sort not read by Agda when reifying instance Conversion MOT (Exp O) I.Term where convert e = case e of App _ _ (Var v) as -> frommyExps 0 as (I.Var v []) App _ _ (Const c) as -> do cdef <- lift $ readIORef c let (iscon, name) = cdorigin cdef {- case iscon of Just n -> do v <- getConTerm name -- We are not in TCM frommyExps n as v -} (ndrop, h) = case iscon of Just n -> (n, \ q -> I.Con (I.ConHead q Cm.Inductive []) Cm.ConOSystem) -- TODO: restore fields Nothing -> (0, \ f vs -> I.Def f $ map I.Apply vs) frommyExps ndrop as (h name []) Lam hid t -> I.Lam (icnvh hid) <$> convert t Pi _ hid _ x y -> I.Pi . Cm.Dom (icnvh hid) <$> convert x <*> convert y -- maybe have case for Pi where possdep is False which produces Fun (and has to unweaken y), return $ I.Fun (Cm.Arg (icnvh hid) x') y' Sort (Set l) -> return $ I.Sort (I.mkType (fromIntegral l)) Sort Type -> __IMPOSSIBLE__ Sort UnknownSort -> return $ I.Sort (I.mkType 0) -- hoping it's thrown away AbsurdLambda hid -> return $ I.Lam (icnvh hid) $ I.Abs abslamvarname (I.Var 0 []) frommyExps :: Nat -> MArgList O -> I.Term -> ExceptT String IO I.Term frommyExps ndrop (Meta m) trm = do bind <- lift $ readIORef $ mbind m case bind of Nothing -> throwError "meta not bound" Just e -> frommyExps ndrop (NotM e) trm frommyExps ndrop (NotM as) trm = case as of ALNil -> return trm ALCons _ _ xs | ndrop > 0 -> frommyExps (ndrop - 1) xs trm ALCons hid x xs -> do x' <- convert x frommyExps ndrop xs (addend (Cm.Arg (icnvh hid) x') trm) -- Andreas, 2013-10-19 TODO: restore postfix projections ALProj eas idx hid xs -> do idx <- lift $ expandbind idx c <- case idx of NotM c -> return c Meta{} -> throwError "meta not bound" cdef <- lift $ readIORef c let name = snd $ cdorigin cdef trm2 <- frommyExps 0 eas (I.Def name []) frommyExps 0 xs (addend (Cm.Arg (icnvh hid) trm) trm2) ALConPar xs | ndrop > 0 -> frommyExps (ndrop - 1) xs trm ALConPar _ -> __IMPOSSIBLE__ where addend x (I.Var h xs) = I.Var h (xs ++ [I.Apply x]) addend x (I.Con h ci xs) = I.Con h ci (xs ++ [x]) addend x (I.Def h xs) = I.Def h (xs ++ [I.Apply x]) addend x (I.Shared p) = addend x (I.derefPtr p) addend _ _ = __IMPOSSIBLE__ -- -------------------------------- abslamvarname :: String abslamvarname = "\0absurdlambda" modifyAbstractExpr :: A.Expr -> A.Expr modifyAbstractExpr = f where f (A.App i e1 (Cm.Arg info (Cm.Named n e2))) = A.App i (f e1) (Cm.Arg info (Cm.Named n (f e2))) f (A.Lam i (A.DomainFree info n) _) | prettyShow (A.nameConcrete n) == abslamvarname = A.AbsurdLam i $ Cm.argInfoHiding info f (A.Lam i b e) = A.Lam i b (f e) f (A.Rec i xs) = A.Rec i (map (mapLeft (over exprFieldA f)) xs) f (A.RecUpdate i e xs) = A.RecUpdate i (f e) (map (over exprFieldA f) xs) f (A.ScopedExpr i e) = A.ScopedExpr i (f e) f e = e modifyAbstractClause :: A.Clause -> A.Clause modifyAbstractClause (A.Clause lhs dots sdots (A.RHS e mc) decls catchall) = A.Clause lhs dots sdots (A.RHS (modifyAbstractExpr e) mc) decls catchall modifyAbstractClause cl = cl -- --------------------------------- constructPats :: Map AN.QName (TMode, ConstRef O) -> I.MetaId -> I.Clause -> MB.TCM ([(Hiding, MId)], [CSPat O]) constructPats cmap mainm clause = do let cnvps ns [] = return (ns, []) cnvps ns (p : ps) = do (ns', ps') <- cnvps ns ps (ns'', p') <- cnvp ns' p return (ns'', p' : ps') cnvp ns p = let hid = getHiding $ Cm.argInfo p in case Cm.namedArg p of I.VarP n -> return ((hid, Id n) : ns, HI hid (CSPatVar $ length ns)) I.ConP con _ ps -> do let c = I.conName con (c2, _) <- runStateT (getConst True c TMAll) (S {sConsts = (cmap, []), sMetas = initMapS, sEqs = initMapS, sCurMeta = Nothing, sMainMeta = mainm}) (ns', ps') <- cnvps ns ps cc <- liftIO $ readIORef c2 let Just npar = fst $ cdorigin cc return (ns', HI hid (CSPatConApp c2 (replicate npar (HI Hidden CSOmittedArg) ++ ps'))) I.DotP t -> do (t2, _) <- runStateT (convert t) (S {sConsts = (cmap, []), sMetas = initMapS, sEqs = initMapS, sCurMeta = Nothing, sMainMeta = mainm}) return (ns, HI hid (CSPatExp t2)) I.AbsurdP{} -> return ((hid, Id I.absurdPatternName) : ns, HI hid (CSPatVar $ length ns)) I.ProjP{} -> copatternsNotImplemented I.LitP{} -> literalsNotImplemented (names, pats) <- cnvps [] (IP.unnumberPatVars $ I.namedClausePats clause) return (reverse names, pats) frommyClause :: (CSCtx O, [CSPat O], Maybe (MExp O)) -> ExceptT String IO I.Clause frommyClause (ids, pats, mrhs) = do let ctel [] = return I.EmptyTel ctel (HI hid (mid, t) : ctx) = do let Id id = mid tel <- ctel ctx t' <- convert t return $ I.ExtendTel (Cm.Dom (icnvh hid) t') (I.Abs id tel) tel <- ctel $ reverse ids let getperms 0 [] perm nv = return (perm, nv) getperms n [] _ _ = __IMPOSSIBLE__ getperms 0 (p : ps) perm nv = do (perm, nv) <- getperm p perm nv getperms 0 ps perm nv getperms n (HI _ CSPatExp{} : ps) perm nv = getperms (n - 1) ps perm nv getperms n (HI _ CSOmittedArg{} : ps) perm nv = getperms (n - 1) ps perm nv getperms n (_ : _) _ _ = __IMPOSSIBLE__ getperm (HI _ p) perm nv = case p of --CSPatVar v -> return (length ids + nv - 1 - v : perm, nv) CSPatVar v -> return ((length ids - 1 - v, nv) : perm, nv + 1) CSPatConApp c ps -> do cdef <- lift $ readIORef c let (Just ndrop, _) = cdorigin cdef getperms ndrop ps perm nv CSPatExp e -> return (perm, nv + 1) _ -> __IMPOSSIBLE__ (rperm, nv) <- getperms 0 pats [] 0 let --perm = reverse rperm perm = map (\i -> let Just x = lookup i rperm in x) [0..length ids - 1] --renperm = map (\i -> length ids + nv - 1 - i) rperm --renm = rename (\i -> renperm !! i) cnvps 0 [] = return [] cnvps n [] = __IMPOSSIBLE__ cnvps 0 (p : ps) = do p' <- cnvp p ps' <- cnvps 0 ps return (p' : ps') cnvps n (HI _ CSPatExp{} : ps) = cnvps (n - 1) ps cnvps n (HI _ CSOmittedArg{} : ps) = cnvps (n - 1) ps cnvps n (_ : _) = __IMPOSSIBLE__ cnvp (HI hid p) = do p' <- case p of CSPatVar v -> return (I.varP $ let HI _ (Id n, _) = ids !! v in n) CSPatConApp c ps -> do cdef <- lift $ readIORef c let (Just ndrop, name) = cdorigin cdef ps' <- cnvps ndrop ps let con = I.ConHead name Cm.Inductive [] -- TODO: restore record fields! return (I.ConP con I.noConPatternInfo ps') CSPatExp e -> do e' <- convert e {- renm e -} -- renaming before adding to clause below return (I.DotP e') CSAbsurd -> __IMPOSSIBLE__ -- CSAbsurd not used _ -> __IMPOSSIBLE__ return $ Cm.Arg (icnvh hid) $ Cm.unnamed p' -- TODO: recover names ps <- cnvps 0 pats body <- case mrhs of Nothing -> return $ Nothing Just e -> Just <$> convert e let cperm = Perm nv perm return $ I.Clause { I.clauseLHSRange = SP.noRange , I.clauseFullRange = SP.noRange , I.clauseTel = tel , I.namedClausePats = IP.numberPatVars __IMPOSSIBLE__ cperm $ applySubst (renamingR $ compactP cperm) ps , I.clauseBody = body , I.clauseType = Nothing -- TODO: compute clause type , I.clauseCatchall = False , I.clauseUnreachable = Nothing -- TODO: Don't know here whether reachable or not !? } contains_constructor :: [CSPat O] -> Bool contains_constructor = any f where f (HI _ p) = case p of CSPatConApp{} -> True _ -> False -- --------------------------------- freeIn :: Nat -> MExp o -> Bool freeIn = f where mr x = let NotM x' = x in x' f v e = case mr e of App _ _ elr args -> case elr of Var v' | v' == v -> False _ -> fs v args Lam _ (Abs _ b) -> f (v + 1) b Pi _ _ _ it (Abs _ ot) -> f v it && f (v + 1) ot Sort{} -> True AbsurdLambda{} -> True fs v es = case mr es of ALNil -> True ALCons _ a as -> f v a && fs v as ALProj{} -> __IMPOSSIBLE__ ALConPar as -> fs v as negtype :: ConstRef o -> MExp o -> MExp o negtype ee = f (0 :: Int) where mr x = let NotM x' = x in x' f n e = case mr e of Pi uid hid possdep it (Abs id ot) -> NotM $ Pi uid hid possdep it (Abs id (f (n + 1) ot)) _ -> NotM $ Pi Nothing NotHidden False (NotM $ Pi Nothing NotHidden False e (Abs NoId (NotM $ Pi Nothing NotHidden True (NotM $ Sort (Set 0)) (Abs NoId (NotM $ App Nothing (NotM OKVal) (Var 0) (NotM ALNil)))))) (Abs NoId (NotM $ App Nothing (NotM OKVal) (Const ee) (NotM ALNil))) -- --------------------------------------- findClauseDeep :: Cm.InteractionId -> MB.TCM (Maybe (AN.QName, I.Clause, Bool)) findClauseDeep ii = ignoreAbstractMode $ do -- Andreas, 2016-09-04, issue #2162 MB.InteractionPoint { MB.ipClause = ipCl} <- lookupInteractionPoint ii case ipCl of MB.IPNoClause -> return Nothing MB.IPClause f clauseNo _ -> do (_, c, _) <- getClauseForIP f clauseNo return $ Just (f, c, maybe __IMPOSSIBLE__ toplevel $ I.clauseBody c) where toplevel e = case I.ignoreSharing e of I.MetaV{} -> True _ -> False -- --------------------------------------- matchType :: Int -> Int -> I.Type -> I.Type -> Maybe (Nat, Nat) -- Nat is deffreevars of const, Nat is ctx length of target type, left arg is const type, right is target type matchType cdfv tctx ctyp ttyp = trmodps cdfv ctyp where trmodps 0 ctyp = tr 0 0 ctyp trmodps n ctyp = case I.ignoreSharing $ I.unEl ctyp of I.Pi _ ot -> trmodps (n - 1) (I.absBody ot) _ -> __IMPOSSIBLE__ tr narg na ctyp = case ft 0 0 Just ctyp ttyp of Just n -> Just (n, narg) Nothing -> case I.ignoreSharing $ I.unEl ctyp of I.Pi _ (I.Abs _ ot) -> tr (narg + 1) (na + 1) ot I.Pi _ (I.NoAbs _ ot) -> tr (narg + 1) na ot _ -> Nothing where ft nl n c (I.El _ e1) (I.El _ e2) = f nl n c e1 e2 f nl n c e1 e2 = case I.ignoreSharing e1 of I.Var v1 as1 | v1 < nl -> case e2 of I.Var v2 as2 | v1 == v2 -> fes nl (n + 1) c as1 as2 _ -> Nothing I.Var v1 _ | v1 < nl + na -> c n -- unify vars with no args? I.Var v1 as1 -> case e2 of I.Var v2 as2 | cdfv + na + nl - v1 == tctx + nl - v2 -> fes nl (n + 1) c as1 as2 _ -> Nothing _ -> case (I.ignoreSharing e1, I.ignoreSharing e2) of (I.MetaV{}, _) -> c n (_, I.MetaV{}) -> c n (I.Lam hid1 b1, I.Lam hid2 b2) | hid1 == hid2 -> f (nl + 1) n c (I.absBody b1) (I.absBody b2) (I.Lit lit1, I.Lit lit2) | lit1 == lit2 -> c (n + 1) (I.Def n1 as1, I.Def n2 as2) | n1 == n2 -> fes nl (n + 1) c as1 as2 (I.Con n1 _ as1, I.Con n2 _ as2) | n1 == n2 -> fs nl (n + 1) c as1 as2 (I.Pi (Cm.Dom{domInfo = info1, unDom = it1}) ot1, I.Pi (Cm.Dom{domInfo = info2, unDom = it2}) ot2) | Cm.argInfoHiding info1 == Cm.argInfoHiding info2 -> ft nl n (\n -> ft (nl + 1) n c (I.absBody ot1) (I.absBody ot2)) it1 it2 (I.Sort{}, I.Sort{}) -> c n -- sloppy _ -> Nothing fs nl n c es1 es2 = case (es1, es2) of ([], []) -> c n (Cm.Arg info1 e1 : es1, Cm.Arg info2 e2 : es2) | Cm.argInfoHiding info1 == Cm.argInfoHiding info2 -> f nl n (\n -> fs nl n c es1 es2) e1 e2 _ -> Nothing fes nl n c es1 es2 = case (es1, es2) of ([], []) -> c n (I.Proj _ f : es1, I.Proj _ f' : es2) | f == f' -> fes nl n c es1 es2 (I.Apply (Cm.Arg info1 e1) : es1, I.Apply (Cm.Arg info2 e2) : es2) | Cm.argInfoHiding info1 == Cm.argInfoHiding info2 -> f nl n (\n -> fes nl n c es1 es2) e1 e2 _ -> Nothing Agda-2.5.3/src/full/Agda/Auto/Options.hs0000644000000000000000000000527213154613124016035 0ustar0000000000000000module Agda.Auto.Options where import Control.Monad.State import Agda.Utils.Lens data Mode = MNormal Bool Bool -- true if list mode, true if disprove | MCaseSplit | MRefine Bool -- true if list mode data AutoHintMode = AHMNone | AHMModule type Hints = [String] newtype TimeOut = TimeOut { getTimeOut :: Int } instance Show TimeOut where show = show . getTimeOut -- | Options for Auto, default value and lenses data AutoOptions = AutoOptions { autoHints :: Hints , autoTimeOut :: TimeOut , autoPick :: Int , autoMode :: Mode , autoHintMode :: AutoHintMode } initAutoOptions :: AutoOptions initAutoOptions = AutoOptions { autoHints = [] , autoTimeOut = TimeOut 1 , autoPick = 0 , autoMode = MNormal False False , autoHintMode = AHMNone } aoHints :: Lens' Hints AutoOptions aoHints f s = f (autoHints s) <&> \x -> s {autoHints = x} aoTimeOut :: Lens' TimeOut AutoOptions aoTimeOut f s = f (autoTimeOut s) <&> \x -> s {autoTimeOut = x} aoPick :: Lens' Int AutoOptions aoPick f s = f (autoPick s) <&> \x -> s {autoPick = x} aoMode :: Lens' Mode AutoOptions aoMode f s = f (autoMode s) <&> \x -> s {autoMode = x} aoHintMode :: Lens' AutoHintMode AutoOptions aoHintMode f s = f (autoHintMode s) <&> \x -> s {autoHintMode = x} -- | Tokenising the input (makes `parseArgs` cleaner) data AutoToken = M | C | R | D | L | T Int | S Int | H String autoTokens :: [String] -> [AutoToken] autoTokens [] = [] autoTokens ("-t" : t : ws) = T (read t) : autoTokens ws autoTokens ("-s" : s : ws) = S (read s) : autoTokens ws autoTokens ("-l" : ws) = L : autoTokens ws autoTokens ("-d" : ws) = D : autoTokens ws autoTokens ("-m" : ws) = M : autoTokens ws autoTokens ("-c" : ws) = C : autoTokens ws autoTokens ("-r" : ws) = R : autoTokens ws autoTokens (h : ws) = H h : autoTokens ws parseArgs :: String -> AutoOptions parseArgs s = mapM_ step (autoTokens $ words s) `execState` initAutoOptions where step :: AutoToken -> State AutoOptions () step M = aoHintMode .= AHMModule step C = aoMode .= MCaseSplit step R = aoPick .= (-1) >> aoMode .= MRefine False step (T t) = aoTimeOut .= TimeOut t step (S p) = aoPick .= p step (H h) = aoHints %= (h :) step D = do mode <- use aoMode case mode of MNormal lm _ -> aoMode .= MNormal lm True _ -> return () step L = do mode <- use aoMode case mode of MNormal _ dp -> aoMode .= MNormal True dp MRefine _ -> aoMode .= MRefine True _ -> return () Agda-2.5.3/src/full/Agda/Auto/SearchControl.hs0000644000000000000000000003650713154613124017155 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE UndecidableInstances #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.Auto.SearchControl where import Control.Applicative hiding (getConst, Const(..)) import Control.Monad import Data.IORef import Control.Monad.State import Data.Maybe (mapMaybe, fromMaybe) import Agda.Syntax.Common (Hiding(..)) import Agda.Auto.NarrowingSearch import Agda.Auto.Syntax #include "undefined.h" import Agda.Utils.Impossible instance Refinable (ArgList o) (RefInfo o) where refinements _ infos _ = return $ fmap (Move 0) $ [ return ALNil, cons NotHidden, cons Hidden ] ++ if getIsDep infos then [] else [ proj NotHidden, proj Hidden ] where getIsDep :: [RefInfo o] -> Bool getIsDep (x : xs) = case x of RICheckElim isDep -> isDep _ -> getIsDep xs getIsDep _ = __IMPOSSIBLE__ proj :: Hiding -> RefCreateEnv (RefInfo o) (ArgList o) proj hid = ALProj <$> newPlaceholder <*> newPlaceholder <*> return hid <*> newPlaceholder cons :: Hiding -> RefCreateEnv (RefInfo o) (ArgList o) cons hid = ALCons hid <$> newPlaceholder <*> newPlaceholder data ExpRefInfo o = ExpRefInfo { eriMain :: Maybe (RefInfo o) , eriUnifs :: [RefInfo o] , eriInfTypeUnknown :: Bool , eriIsEliminand :: Bool , eriUsedVars :: Maybe ([UId o], [Elr o]) , eriIotaStep :: Maybe Bool , eriPickSubsVar :: Bool , eriEqRState :: Maybe EqReasoningState } initExpRefInfo :: ExpRefInfo o initExpRefInfo = ExpRefInfo { eriMain = Nothing , eriUnifs = [] , eriInfTypeUnknown = False , eriIsEliminand = False , eriUsedVars = Nothing , eriIotaStep = Nothing , eriPickSubsVar = False , eriEqRState = Nothing } getinfo :: [RefInfo o] -> ExpRefInfo o getinfo = foldl step initExpRefInfo where step :: ExpRefInfo o -> RefInfo o -> ExpRefInfo o step eri x@RIMainInfo{} = eri { eriMain = Just x } step eri x@RIUnifInfo{} = eri { eriUnifs = x : eriUnifs eri } step eri RIInferredTypeUnknown = eri { eriInfTypeUnknown = True } step eri RINotConstructor = eri { eriIsEliminand = True } step eri (RIUsedVars nuids nused) = eri { eriUsedVars = Just (nuids, nused) } step eri (RIIotaStep semif) = eri { eriIotaStep = Just iota' } where iota' = semif || fromMaybe False (eriIotaStep eri) step eri RIPickSubsvar = eri { eriPickSubsVar = True } step eri (RIEqRState s) = eri { eriEqRState = Just s } step eri _ = __IMPOSSIBLE__ -- | @univar sub v@ figures out what the name of @v@ "outside" of -- the substitution @sub@ ought to be, if anything. univar :: [CAction o] -> Nat -> Maybe Nat univar cl v = getOutsideName cl v 0 where getOutsideName :: [CAction o] -> Nat -> Nat -> Maybe Nat -- @v@ is offset by @v'@ binders getOutsideName [] v v' = Just (v' + v) -- @v@ was introduced by the weakening: disappears getOutsideName (Weak n : _) v v' | v < n = Nothing -- @v@ was introduced before the weakening: strengthened getOutsideName (Weak n : xs) v v' = getOutsideName xs (v - n) v' -- Name of @v@ before the substitution was pushed in -- had to be offset by 1 getOutsideName (Sub _ : xs) v v' = getOutsideName xs v (v' + 1) -- If this is the place where @v@ was bound, it used to -- be called 0 + offset of all the vars substituted for getOutsideName (Skip : _) 0 v' = Just v' -- Going over a binder: de Bruijn name of @v@ decreased -- but offset increased getOutsideName (Skip : xs) v v' = getOutsideName xs (v - 1) (v' + 1) -- | List of the variables instantiated by the substitution subsvars :: [CAction o] -> [Nat] subsvars = f 0 where f :: Nat -> [CAction o] -> [Nat] f n [] = [] f n (Weak _ : xs) = f n xs -- why? f n (Sub _ : xs) = n : f (n + 1) xs f n (Skip : xs) = f (n + 1) xs -- | Moves -- A move is composed of a @Cost@ together with an action -- computing the refined problem. type Move o = Move' (RefInfo o) (Exp o) -- | New constructors -- Taking a step towards a solution consists in picking a -- constructor and filling in the missing parts with -- placeholders to be discharged later on. newAbs :: MId -> RefCreateEnv blk (Abs (MM a blk)) newAbs mid = Abs mid <$> newPlaceholder newLam :: Hiding -> MId -> RefCreateEnv (RefInfo o) (Exp o) newLam hid mid = Lam hid <$> newAbs mid newPi :: UId o -> Bool -> Hiding -> RefCreateEnv (RefInfo o) (Exp o) newPi uid dep hid = Pi (Just uid) hid dep <$> newPlaceholder <*> newAbs NoId foldArgs :: [(Hiding, MExp o)] -> MArgList o foldArgs = foldr (\ (h, a) sp -> NotM $ ALCons h a sp) (NotM ALNil) -- | New spine of arguments potentially using placeholders newArgs' :: [Hiding] -> [MExp o] -> RefCreateEnv (RefInfo o) (MArgList o) newArgs' h tms = foldArgs . zip h . (++ tms) <$> replicateM size newPlaceholder where size = length h - length tms newArgs :: [Hiding] -> RefCreateEnv (RefInfo o) (MArgList o) newArgs h = newArgs' h [] -- | New @App@lication node using a new spine of arguments -- respecting the @Hiding@ annotation newApp' :: UId o -> ConstRef o -> [Hiding] -> [MExp o] -> RefCreateEnv (RefInfo o) (Exp o) newApp' meta cst hds tms = App (Just meta) <$> newOKHandle <*> return (Const cst) <*> newArgs' hds tms newApp :: UId o -> ConstRef o -> [Hiding] -> RefCreateEnv (RefInfo o) (Exp o) newApp meta cst hds = newApp' meta cst hds [] -- | Equality reasoning steps -- The begin token is accompanied by two steps because -- it does not make sense to have a derivation any shorter -- than that. eqStep :: UId o -> EqReasoningConsts o -> Move o eqStep meta eqrc = Move costEqStep $ newApp meta (eqrcStep eqrc) [Hidden, Hidden, NotHidden, Hidden, Hidden, NotHidden, NotHidden] eqEnd :: UId o -> EqReasoningConsts o -> Move o eqEnd meta eqrc = Move costEqEnd $ newApp meta (eqrcEnd eqrc) [Hidden, Hidden, NotHidden] eqCong :: UId o -> EqReasoningConsts o -> Move o eqCong meta eqrc = Move costEqCong $ newApp meta (eqrcCong eqrc) [Hidden, Hidden, Hidden, Hidden, NotHidden, Hidden, Hidden, NotHidden] eqSym :: UId o -> EqReasoningConsts o -> Move o eqSym meta eqrc = Move costEqSym $ newApp meta (eqrcSym eqrc) [Hidden, Hidden, Hidden, Hidden, NotHidden] eqBeginStep2 :: UId o -> EqReasoningConsts o -> Move o eqBeginStep2 meta eqrc = Move costEqStep $ do e1 <- newApp meta (eqrcStep eqrc) [Hidden, Hidden, NotHidden, Hidden, Hidden, NotHidden, NotHidden] e2 <- newApp' meta (eqrcStep eqrc) [Hidden, Hidden, NotHidden, Hidden, Hidden, NotHidden, NotHidden] [NotM e1] newApp' meta (eqrcBegin eqrc) [Hidden, Hidden, Hidden, Hidden, NotHidden] [NotM e2] -- | Pick the first unused UId amongst the ones you have seen (GA: ??) -- Defaults to the head of the seen ones. pickUid :: forall o. [UId o] -> [Maybe (UId o)] -> (Maybe (UId o), Bool) pickUid used seen = maybe (head seen, False) (, True) $ firstUnused seen where {- ?? which uid to pick -} firstUnused :: [Maybe (UId o)] -> Maybe (Maybe (UId o)) firstUnused [] = Nothing firstUnused (Nothing : _) = Just Nothing firstUnused (mu@(Just u) : us) = if u `elem` used then firstUnused us else Just mu instance Refinable (Exp o) (RefInfo o) where refinements envinfo infos meta = let hints = rieHints envinfo deffreevars = rieDefFreeVars envinfo meqr = rieEqReasoningConsts envinfo ExpRefInfo { eriMain = Just (RIMainInfo n tt iotastepdone) , eriUnifs = unis , eriInfTypeUnknown = inftypeunknown , eriIsEliminand = iseliminand , eriUsedVars = Just (uids, usedvars) , eriIotaStep = iotastep , eriPickSubsVar = picksubsvar , eriEqRState = meqrstate } = getinfo infos eqrstate = fromMaybe EqRSNone meqrstate set l = return $ Sort (Set l) in case unis of [] -> let eqr = fromMaybe __IMPOSSIBLE__ meqr eq_end = eqEnd meta eqr eq_step = eqStep meta eqr eq_cong = eqCong meta eqr eq_sym = eqSym meta eqr eq_begin_step2 = eqBeginStep2 meta eqr adjustCost i = if inftypeunknown then costInferredTypeUnkown else i varcost v | v < n - deffreevars = adjustCost $ if elem v (mapMaybe getVar usedvars) then costAppVarUsed else costAppVar varcost v | otherwise = adjustCost costAppHint varapps = map (\ v -> Move (varcost v) $ app n meta Nothing (Var v)) [0..n - 1] hintapps = map (\(c, hm) -> Move (cost c hm) (app n meta Nothing (Const c))) hints where cost :: ConstRef o -> HintMode -> Cost cost c hm = adjustCost $ case (iotastep , hm) of (Just _ , _ ) -> costIotaStep (Nothing , HMNormal) -> if elem c (mapMaybe getConst usedvars) then costAppHintUsed else costAppHint (Nothing , HMRecCall) -> if elem c (mapMaybe getConst usedvars) then costAppRecCallUsed else costAppRecCall generics = varapps ++ hintapps in case rawValue tt of _ | eqrstate == EqRSChain -> return [eq_end, eq_step] HNPi hid _ _ (Abs id _) -> return $ (Move (adjustCost (if iotastepdone then costLamUnfold else costLam)) $ newLam hid id) : (Move costAbsurdLam $ return $ AbsurdLambda hid) : generics HNSort (Set l) -> return $ map (Move (adjustCost costSort) . set) [0..l - 1] ++ map (Move (adjustCost costPi) . newPi meta True) [NotHidden, Hidden] ++ generics HNApp (Const c) _ -> do cd <- readIORef c return $ case cdcont cd of Datatype cons _ | eqrstate == EqRSNone -> map (\c -> Move (adjustCost $ case iotastep of Just True -> costUnification _ -> if length cons <= 1 then costAppConstructorSingle else costAppConstructor) $ app n meta Nothing (Const c)) cons ++ generics ++ (guard (maybe False ((c ==) . eqrcId) meqr) *> [eq_sym, eq_cong, eq_begin_step2]) _ | eqrstate == EqRSPrf1 -> generics ++ [eq_sym, eq_cong] _ | eqrstate == EqRSPrf2 -> generics ++ [eq_cong] _ -> generics _ -> return generics (RIUnifInfo cl hne : _) -> let subsvarapps = map (Move costUnification . app n meta Nothing . Var) (subsvars cl) mlam = case rawValue tt of HNPi hid _ _ (Abs id _) -> [Move costUnification (newLam hid id)] _ -> [] generics = mlam ++ subsvarapps in return $ case rawValue hne of HNApp (Var v) _ -> let (uid, isunique) = pickUid uids $ seenUIds hne uni = case univar cl v of Just v | v < n -> [Move (costUnificationIf isunique) $ app n meta uid (Var v)] _ -> [] in uni ++ generics HNApp (Const c) _ -> let (uid, isunique) = pickUid uids $ seenUIds hne in (Move (costUnificationIf isunique) $ app n meta uid (Const c)) : generics HNLam{} -> generics HNPi hid possdep _ _ -> let (uid, isunique) = pickUid uids $ seenUIds hne in (Move (costUnificationIf isunique) $ newPi (fromMaybe meta uid) possdep hid) : generics HNSort (Set l) -> map (Move costUnification . set) [0..l] ++ generics HNSort _ -> generics _ -> __IMPOSSIBLE__ where app :: Nat -> UId o -> Maybe (UId o) -> Elr o -> RefCreateEnv (RefInfo o) (Exp o) app n meta muid elr = do p <- newPlaceholder p <- case elr of Var{} -> return p Const c -> do cd <- RefCreateEnv $ lift $ readIORef c let dfvapp 0 _ = p dfvapp i n = NotM $ ALCons NotHidden (NotM $ App Nothing (NotM $ OKVal) (Var n) (NotM ALNil)) (dfvapp (i - 1) (n - 1)) -- NotHidden is ok because agda reification throws these arguments -- away and agsy skips typechecking them return $ dfvapp (cddeffreevars cd) (n - 1) okh <- newOKHandle return $ App (Just $ fromMaybe meta muid) okh elr p extraref :: UId o -> [Maybe (UId o)] -> ConstRef o -> Move o extraref meta seenuids c = Move costAppExtraRef $ app (head seenuids) (Const c) where app muid elr = App (Just $ fromMaybe meta muid) <$> newOKHandle <*> return elr <*> newPlaceholder instance Refinable (ICExp o) (RefInfo o) where refinements _ infos _ = let (RICopyInfo e : _) = infos in return [Move 0 (return e)] instance Refinable (ConstRef o) (RefInfo o) where refinements _ [RICheckProjIndex projs] _ = return $ map (Move 0 . return) projs refinements _ _ _ = __IMPOSSIBLE__ -- --------------------------------- costIncrease, costUnificationOccurs, costUnification, costAppVar, costAppVarUsed, costAppHint, costAppHintUsed, costAppRecCall, costAppRecCallUsed, costAppConstructor, costAppConstructorSingle, costAppExtraRef, costLam, costLamUnfold, costPi, costSort, costIotaStep, costInferredTypeUnkown, costAbsurdLam :: Cost costUnificationIf :: Bool -> Cost costUnificationIf b = if b then costUnification else costUnificationOccurs costIncrease = 1000 costUnificationOccurs = 100 -- 1000001 -- 1 -- 100 costUnification = 0000 costAppVar = 0000 -- 0, 1 costAppVarUsed = 1000 -- 5 costAppHint = 3000 -- 2, 5 costAppHintUsed = 5000 costAppRecCall = 0 -- 1000? costAppRecCallUsed = 10000 -- 1000? costAppConstructor = 1000 costAppConstructorSingle = 0000 costAppExtraRef = 1000 costLam = 0000 -- 1, 0 costLamUnfold = 1000 -- 1, 0 costPi = 1000003 -- 100 -- 5 costSort = 1000004 -- 0 costIotaStep = 3000 -- 1000005 -- 2 -- 100 costInferredTypeUnkown = 1000006 -- 100 costAbsurdLam = 0 costEqStep, costEqEnd, costEqSym, costEqCong :: Cost costEqStep = 2000 costEqEnd = 0 costEqSym = 0 costEqCong = 500 prioNo, prioTypeUnknown, prioTypecheckArgList, prioInferredTypeUnknown, prioCompBeta, prioCompBetaStructured, prioCompareArgList, prioCompIota, prioCompChoice, prioCompUnif, prioCompCopy, prioNoIota, prioAbsurdLambda, prioProjIndex :: Prio prioNo = (-1) prioTypeUnknown = 0 prioTypecheckArgList = 3000 prioInferredTypeUnknown = 4000 prioCompBeta = 4000 prioCompBetaStructured = 4000 prioCompIota = 4000 prioCompChoice = 5000 -- 700 -- 5000 prioCompUnif = 6000 -- 2 prioCompCopy = 8000 prioCompareArgList = 7000 -- 5 -- 2 prioNoIota = 500 -- 500 prioAbsurdLambda = 1000 prioProjIndex = 3000 prioTypecheck :: Bool -> Prio prioTypecheck False = 1000 prioTypecheck True = 0 -- --------------------------------- instance Trav a blk => Trav [a] blk where trav _ [] = return () trav f (x:xs) = trav f x >> trav f xs instance Trav (MId, CExp o) (RefInfo o) where trav f (_, ce) = trav f ce instance Trav (TrBr a o) (RefInfo o) where trav f (TrBr es _) = trav f es instance Trav (Exp o) (RefInfo o) where trav f e = case e of App _ _ _ args -> trav f args Lam _ (Abs _ b) -> trav f b Pi _ _ _ it (Abs _ ot) -> trav f it >> trav f ot Sort _ -> return () AbsurdLambda{} -> return () instance Trav (ArgList o) (RefInfo o) where trav _ ALNil = return () trav f (ALCons _ arg args) = trav f arg >> trav f args trav f (ALProj eas _ _ as) = trav f eas >> trav f as trav f (ALConPar args) = trav f args -- --------------------------------- Agda-2.5.3/src/full/Agda/Compiler/0000755000000000000000000000000013154613124014702 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Compiler/Backend.hs-boot0000644000000000000000000000006213154613124017524 0ustar0000000000000000 module Agda.Compiler.Backend where data Backend Agda-2.5.3/src/full/Agda/Compiler/Backend.hs0000644000000000000000000001707713154613124016601 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE TypeOperators #-} -- | Interface for compiler backend writers. module Agda.Compiler.Backend ( Backend(..), Backend'(..), Recompile(..), IsMain(..) , Flag , toTreeless , module Agda.Syntax.Treeless , module Agda.TypeChecking.Monad -- For Agda.Main , backendInteraction , parseBackendOptions -- For InteractionTop , callBackend ) where import Control.Monad.State import qualified Data.List as List import Data.Functor import Data.Map (Map) import qualified Data.Map as Map import System.Environment import System.Exit import System.Console.GetOpt import Agda.Syntax.Treeless import Agda.TypeChecking.Monad import Agda.TypeChecking.Reduce import Agda.TypeChecking.Pretty as P import Agda.Interaction.Options import Agda.Interaction.FindFile import Agda.Interaction.Highlighting.HTML import Agda.Interaction.Imports (getAllWarnings') import Agda.TypeChecking.Warnings import Agda.Utils.FileName import Agda.Utils.Functor import Agda.Utils.IndexedList import Agda.Utils.Lens import Agda.Utils.Monad import Agda.Utils.Pretty import Agda.Compiler.ToTreeless import Agda.Compiler.Common #include "undefined.h" import Agda.Utils.Impossible -- Public interface ------------------------------------------------------- data Backend where Backend :: Backend' opts env menv mod def -> Backend data Backend' opts env menv mod def = Backend' { backendName :: String , backendVersion :: Maybe String -- ^ Optional version information to be printed with @--version@. , options :: opts -- ^ Default options , commandLineFlags :: [OptDescr (Flag opts)] -- ^ Backend-specific command-line flags. Should at minimum contain a -- flag to enable the backend. , isEnabled :: opts -> Bool -- ^ Unless the backend has been enabled, @runAgda@ will fall back to -- vanilla Agda behaviour. , preCompile :: opts -> TCM env -- ^ Called after type checking completes, but before compilation starts. , postCompile :: env -> IsMain -> Map ModuleName mod -> TCM () -- ^ Called after module compilation has completed. The @IsMain@ argument -- is @NotMain@ if the @--no-main@ flag is present. , preModule :: env -> ModuleName -> FilePath -> TCM (Recompile menv mod) -- ^ Called before compilation of each module. Gets the path to the -- @.agdai@ file to allow up-to-date checking of previously written -- compilation results. Should return @Skip m@ if compilation is not -- required. , postModule :: env -> menv -> IsMain -> ModuleName -> [def] -> TCM mod -- ^ Called after all definitions of a module has been compiled. , compileDef :: env -> menv -> Definition -> TCM def -- ^ Compile a single definition. , scopeCheckingSuffices :: Bool -- ^ True if the backend works if @--only-scope-checking@ is used. } data Recompile menv mod = Recompile menv | Skip mod callBackend :: String -> IsMain -> Interface -> TCM () callBackend name iMain i = do backends <- use stBackends case [ b | b@(Backend b') <- backends, backendName b' == name ] of Backend b : _ -> compilerMain b iMain i [] -> genericError $ "No backend called '" ++ name ++ "' " ++ "(installed backends: " ++ List.intercalate ", " [ backendName b | Backend b <- backends ] ++ ")" -- Internals -------------------------------------------------------------- data BackendWithOpts opts where BackendWithOpts :: Backend' opts env menv mod def -> BackendWithOpts opts backendWithOpts :: Backend -> Some BackendWithOpts backendWithOpts (Backend backend) = Some (BackendWithOpts backend) forgetOpts :: BackendWithOpts opts -> Backend forgetOpts (BackendWithOpts backend) = Backend backend bOptions :: Lens' opts (BackendWithOpts opts) bOptions f (BackendWithOpts b) = f (options b) <&> \ opts -> BackendWithOpts b{ options = opts } embedFlag :: Lens' a b -> Flag a -> Flag b embedFlag l flag = l flag embedOpt :: Lens' a b -> OptDescr (Flag a) -> OptDescr (Flag b) embedOpt l = fmap (embedFlag l) parseBackendOptions :: [Backend] -> [String] -> OptM ([Backend], CommandLineOptions) parseBackendOptions backends argv = case makeAll backendWithOpts backends of Some bs -> do let agdaFlags = map (embedOpt lSnd) standardOptions backendFlags = do Some i <- forgetAll Some $ allIndices bs BackendWithOpts b <- [lookupIndex bs i] opt <- commandLineFlags b return $ embedOpt (lFst . lIndex i . bOptions) opt (backends, opts) <- getOptSimple argv (agdaFlags ++ backendFlags) (embedFlag lSnd . inputFlag) (bs, defaultOptions) opts <- checkOpts opts return (forgetAll forgetOpts backends, opts) backendInteraction :: [Backend] -> (TCM (Maybe Interface) -> TCM ()) -> TCM (Maybe Interface) -> TCM () backendInteraction [] fallback check = fallback check backendInteraction backends _ check = do opts <- commandLineOptions let backendNames = [ backendName b | Backend b <- backends ] err flag = genericError $ "Cannot mix --" ++ flag ++ " and backends (" ++ List.intercalate ", " backendNames ++ ")" when (optInteractive opts) $ err "interactive" when (optGHCiInteraction opts) $ err "interaction" mi <- check -- reset warnings stTCWarnings .= [] noMain <- optCompileNoMain <$> pragmaOptions let isMain | noMain = NotMain | otherwise = IsMain case mi of Nothing -> genericError $ "You can only compile modules without unsolved metavariables." Just i -> sequence_ [ compilerMain backend isMain i | Backend backend <- backends ] -- print warnings that might have accumulated during compilation ws <- filter (not . isUnsolvedWarning . tcWarning) <$> getAllWarnings' AllWarnings RespectFlags unless (null ws) $ reportSDoc "warning" 1 $ P.vcat $ P.prettyTCM <$> ws compilerMain :: Backend' opts env menv mod def -> IsMain -> Interface -> TCM () compilerMain backend isMain0 i = inCompilerEnv i $ do onlyScoping <- optOnlyScopeChecking <$> commandLineOptions when (not (scopeCheckingSuffices backend) && onlyScoping) $ genericError $ "The --only-scope-checking flag cannot be combined with " ++ backendName backend ++ "." -- Andreas, 2017-08-23, issue #2714 -- If the backend is invoked from Emacs, we can only get the --no-main -- pragma option now, coming from the interface file. isMain <- ifM (optCompileNoMain <$> pragmaOptions) {-then-} (return NotMain) {-else-} (return isMain0) env <- preCompile backend (options backend) mods <- doCompile isMain i $ \ isMain i -> Map.singleton (iModuleName i) <$> compileModule backend env isMain i setInterface i postCompile backend env isMain mods compileModule :: Backend' opts env menv mod def -> env -> IsMain -> Interface -> TCM mod compileModule backend env isMain i = do ifile <- maybe __IMPOSSIBLE__ filePath <$> (findInterfaceFile . toTopLevelModuleName =<< curMName) r <- preModule backend env (iModuleName i) ifile case r of Skip m -> return m Recompile menv -> do defs <- map snd . sortDefs <$> curDefs res <- mapM (compileDef' backend env menv <=< instantiateFull) defs postModule backend env menv isMain (iModuleName i) res compileDef' :: Backend' opts env menv mod def -> env -> menv -> Definition -> TCM def compileDef' backend env menv def = setCurrentRange (defName def) $ compileDef backend env menv def Agda-2.5.3/src/full/Agda/Compiler/ToTreeless.hs0000644000000000000000000003600413154613124017332 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Compiler.ToTreeless ( toTreeless , closedTermToTreeless ) where import Control.Applicative import Control.Monad.Reader import Control.Monad.State import Data.Maybe import Data.Map (Map) import qualified Data.Map as Map import Data.Traversable (traverse) import Agda.Syntax.Common import Agda.Syntax.Internal (QName) import qualified Agda.Syntax.Treeless as C import qualified Agda.Syntax.Internal as I import Agda.Syntax.Literal import qualified Agda.TypeChecking.CompiledClause as CC import Agda.TypeChecking.Records (getRecordConstructor) import Agda.TypeChecking.Pretty import Agda.TypeChecking.CompiledClause import Agda.Compiler.Treeless.Builtin import Agda.Compiler.Treeless.Simplify import Agda.Compiler.Treeless.Erase import Agda.Compiler.Treeless.Uncase import Agda.Compiler.Treeless.Pretty import Agda.Compiler.Treeless.Unused import Agda.Compiler.Treeless.AsPatterns import Agda.Compiler.Treeless.Identity import Agda.Syntax.Common import Agda.TypeChecking.Monad as TCM import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.Utils.Functor import qualified Agda.Utils.HashMap as HMap import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Lens import Agda.Utils.Pretty (prettyShow) import qualified Agda.Utils.Pretty as P #include "undefined.h" import Agda.Utils.Impossible prettyPure :: P.Pretty a => a -> TCM Doc prettyPure = return . P.pretty -- | Converts compiled clauses to treeless syntax. -- -- Note: Do not use any of the concrete names in the returned -- term for identification purposes! If you wish to do so, -- first apply the Agda.Compiler.Treeless.NormalizeNames -- transformation. toTreeless :: QName -> TCM (Maybe C.TTerm) toTreeless q = ifM (alwaysInline q) (pure Nothing) $ Just <$> toTreeless' q toTreeless' :: QName -> TCM C.TTerm toTreeless' q = flip fromMaybeM (getTreeless q) $ verboseBracket "treeless.convert" 20 ("compiling " ++ prettyShow q) $ do Just cc <- defCompiled <$> getConstInfo q unlessM (alwaysInline q) $ setTreeless q (C.TDef q) -- so recursive inlining doesn't loop, but not for always inlined -- functions, since that would risk inlining to fail. ccToTreeless q cc -- | Does not require the name to refer to a function. cacheTreeless :: QName -> TCM () cacheTreeless q = do def <- theDef <$> getConstInfo q case def of Function{} -> () <$ toTreeless' q _ -> return () ccToTreeless :: QName -> CC.CompiledClauses -> TCM C.TTerm ccToTreeless q cc = do let pbody b = pbody' "" b pbody' suf b = sep [ text (prettyShow q ++ suf) <+> text "=", nest 2 $ prettyPure b ] v <- ifM (alwaysInline q) (return 20) (return 0) reportSDoc "treeless.convert" (30 + v) $ text "-- compiled clauses of" <+> prettyTCM q $$ nest 2 (prettyPure cc) body <- casetreeTop cc reportSDoc "treeless.opt.converted" (30 + v) $ text "-- converted" $$ pbody body body <- simplifyTTerm body reportSDoc "treeless.opt.simpl" (35 + v) $ text "-- after first simplification" $$ pbody body body <- translateBuiltins body reportSDoc "treeless.opt.builtin" (30 + v) $ text "-- after builtin translation" $$ pbody body body <- simplifyTTerm body reportSDoc "treeless.opt.simpl" (30 + v) $ text "-- after second simplification" $$ pbody body body <- eraseTerms q body reportSDoc "treeless.opt.erase" (30 + v) $ text "-- after erasure" $$ pbody body body <- caseToSeq body reportSDoc "treeless.opt.uncase" (30 + v) $ text "-- after uncase" $$ pbody body body <- recoverAsPatterns body reportSDoc "treeless.opt.aspat" (30 + v) $ text "-- after @-pattern recovery" $$ pbody body body <- detectIdentityFunctions q body reportSDoc "treeless.opt.id" (30 + v) $ text "-- after identity function detection" $$ pbody body body <- simplifyTTerm body reportSDoc "treeless.opt.simpl" (30 + v) $ text "-- after third simplification" $$ pbody body body <- eraseTerms q body reportSDoc "treeless.opt.erase" (30 + v) $ text "-- after second erasure" $$ pbody body used <- usedArguments q body when (any not used) $ reportSDoc "treeless.opt.unused" (30 + v) $ text "-- used args:" <+> hsep [ if u then text [x] else text "_" | (x, u) <- zip ['a'..] used ] $$ pbody' "[stripped]" (stripUnusedArguments used body) reportSDoc "treeless.opt.final" (20 + v) $ pbody body setTreeless q body setCompiledArgUse q used return body closedTermToTreeless :: I.Term -> TCM C.TTerm closedTermToTreeless t = do substTerm t `runReaderT` initCCEnv alwaysInline :: QName -> TCM Bool alwaysInline q = do def <- theDef <$> getConstInfo q pure $ case def of -- always inline with functions and pattern lambdas Function{} -> isJust (funExtLam def) || isJust (funWith def) _ -> False -- | Initial environment for expression generation. initCCEnv :: CCEnv initCCEnv = CCEnv { ccCxt = [] , ccCatchAll = Nothing } -- | Environment for naming of local variables. data CCEnv = CCEnv { ccCxt :: CCContext -- ^ Maps case tree de-bruijn indices to TTerm de-bruijn indices , ccCatchAll :: Maybe Int -- ^ TTerm de-bruijn index of the current catch all -- If an inner case has no catch-all clause, we use the one from its parent. } type CCContext = [Int] type CC = ReaderT CCEnv TCM shift :: Int -> CCContext -> CCContext shift n = map (+n) -- | Term variables are de Bruijn indices. lookupIndex :: Int -- ^ Case tree de bruijn index. -> CCContext -> Int -- ^ TTerm de bruijn index. lookupIndex i xs = fromMaybe __IMPOSSIBLE__ $ xs !!! i -- | Case variables are de Bruijn levels. lookupLevel :: Int -- ^ case tree de bruijn level -> CCContext -> Int -- ^ TTerm de bruijn index lookupLevel l xs = fromMaybe __IMPOSSIBLE__ $ xs !!! (length xs - 1 - l) -- | Compile a case tree into nested case and record expressions. casetreeTop :: CC.CompiledClauses -> TCM C.TTerm casetreeTop cc = flip runReaderT initCCEnv $ do let a = commonArity cc lift $ reportSLn "treeless.convert.arity" 40 $ "-- common arity: " ++ show a lambdasUpTo a $ casetree cc casetree :: CC.CompiledClauses -> CC C.TTerm casetree cc = do case cc of CC.Fail -> return C.tUnreachable CC.Done xs v -> withContextSize (length xs) $ do -- Issue 2469: Body context size (`length xs`) may be smaller than current context size -- if some arguments are not used in the body. v <- lift (putAllowedReductions [ProjectionReductions, CopatternReductions] $ normalise v) substTerm v CC.Case _ (CC.Branches True _ _ Just{}) -> __IMPOSSIBLE__ -- Andreas, 2016-06-03, issue #1986: Ulf: "no catch-all for copatterns!" -- lift $ do -- typeError . GenericDocError =<< do -- text "Not yet implemented: compilation of copattern matching with catch-all clause" CC.Case (Arg _ n) (CC.Branches True conBrs _ Nothing) -> lambdasUpTo n $ do mkRecord =<< traverse casetree (CC.content <$> conBrs) CC.Case (Arg _ n) (CC.Branches False conBrs litBrs catchAll) -> lambdasUpTo (n + 1) $ do if Map.null conBrs && Map.null litBrs then do -- there are no branches, just return default fromCatchAll else do caseTy <- case (Map.keys conBrs, Map.keys litBrs) of ((c:_), []) -> do c' <- lift (canonicalName c) dtNm <- conData . theDef <$> lift (getConstInfo c') return $ C.CTData dtNm ([], (LitChar _ _):_) -> return C.CTChar ([], (LitString _ _):_) -> return C.CTString ([], (LitFloat _ _):_) -> return C.CTFloat ([], (LitQName _ _):_) -> return C.CTQName _ -> __IMPOSSIBLE__ updateCatchAll catchAll $ do x <- lookupLevel n <$> asks ccCxt def <- fromCatchAll C.TCase x caseTy def <$> do br1 <- conAlts n conBrs br2 <- litAlts n litBrs return (br1 ++ br2) where -- normally, Agda should make sure that a pattern match is total, -- so we set the default to unreachable if no default has been provided. fromCatchAll :: CC C.TTerm fromCatchAll = maybe C.tUnreachable C.TVar <$> asks ccCatchAll commonArity :: CC.CompiledClauses -> Int commonArity cc = case arities 0 cc of [] -> 0 as -> minimum as where arities cxt (Case (Arg _ x) (Branches False cons lits def)) = concatMap (wArities cxt') (Map.elems cons) ++ concatMap (wArities cxt' . WithArity 0) (Map.elems lits) ++ concat [ arities cxt' c | Just c <- [def] ] -- ?? where cxt' = max (x + 1) cxt arities cxt (Case _ (Branches True _ _ _)) = [cxt] arities cxt (Done xs _) = [max cxt (length xs)] arities _ Fail = [] wArities cxt (WithArity k c) = map (\ x -> x - k + 1) $ arities (cxt - 1 + k) c updateCatchAll :: Maybe CC.CompiledClauses -> (CC C.TTerm -> CC C.TTerm) updateCatchAll Nothing cont = cont updateCatchAll (Just cc) cont = do def <- casetree cc local (\e -> e { ccCatchAll = Just 0, ccCxt = shift 1 (ccCxt e) }) $ do C.mkLet def <$> cont -- | Shrinks or grows the context to the given size. -- Does not update the catchAll expression, the catchAll expression -- MUST NOT be used inside `cont`. withContextSize :: Int -> CC C.TTerm -> CC C.TTerm withContextSize n cont = do diff <- (n -) . length <$> asks ccCxt if diff <= 0 then do local (\e -> e { ccCxt = shift diff $ drop (-diff) (ccCxt e)}) $ C.mkTApp <$> cont <*> pure [C.TVar i | i <- reverse [0..(-diff - 1)]] else do local (\e -> e { ccCxt = [0..(diff - 1)] ++ shift diff (ccCxt e)}) $ do createLambdas diff <$> do cont where createLambdas :: Int -> C.TTerm -> C.TTerm createLambdas 0 cont' = cont' createLambdas i cont' | i > 0 = C.TLam (createLambdas (i - 1) cont') createLambdas _ _ = __IMPOSSIBLE__ -- | Adds lambdas until the context has at least the given size. -- Updates the catchAll expression to take the additional lambdas into account. lambdasUpTo :: Int -> CC C.TTerm -> CC C.TTerm lambdasUpTo n cont = do diff <- (n -) . length <$> asks ccCxt if diff <= 0 then cont -- no new lambdas needed else do catchAll <- asks ccCatchAll withContextSize n $ do case catchAll of Just catchAll' -> do -- the catch all doesn't know about the additional lambdas, so just directly -- apply it again to the newly introduced lambda arguments. -- we also bind the catch all to a let, to avoid code duplication local (\e -> e { ccCatchAll = Just 0 , ccCxt = shift 1 (ccCxt e)}) $ do let catchAllArgs = map C.TVar $ reverse [0..(diff - 1)] C.mkLet (C.mkTApp (C.TVar $ catchAll' + diff) catchAllArgs) <$> cont Nothing -> cont conAlts :: Int -> Map QName (CC.WithArity CC.CompiledClauses) -> CC [C.TAlt] conAlts x br = forM (Map.toList br) $ \ (c, CC.WithArity n cc) -> do c' <- lift $ canonicalName c replaceVar x n $ do branch (C.TACon c' n) cc litAlts :: Int -> Map Literal CC.CompiledClauses -> CC [C.TAlt] litAlts x br = forM (Map.toList br) $ \ (l, cc) -> -- Issue1624: we need to drop the case scrutinee from the environment here! replaceVar x 0 $ do branch (C.TALit l ) cc branch :: (C.TTerm -> C.TAlt) -> CC.CompiledClauses -> CC C.TAlt branch alt cc = do alt <$> casetree cc -- | Replace de Bruijn Level @x@ by @n@ new variables. replaceVar :: Int -> Int -> CC a -> CC a replaceVar x n cont = do let upd cxt = shift n ys ++ ixs ++ shift n zs where -- compute the de Bruijn index i = length cxt - 1 - x -- discard index i (ys, _:zs) = splitAt i cxt -- compute the de-bruijn indexes of the newly inserted variables ixs = [0..(n - 1)] local (\e -> e { ccCxt = upd (ccCxt e) , ccCatchAll = (+n) <$> ccCatchAll e }) $ cont -- | Precondition: Map not empty. mkRecord :: Map QName C.TTerm -> CC C.TTerm mkRecord fs = lift $ do -- Get the name of the first field let p1 = fst $ fromMaybe __IMPOSSIBLE__ $ headMaybe $ Map.toList fs -- Use the field name to get the record constructor and the field names. I.ConHead c _ind xs <- conSrcCon . theDef <$> (getConstInfo =<< canonicalName . I.conName =<< recConFromProj p1) -- Convert the constructor let (args :: [C.TTerm]) = for xs $ \ x -> fromMaybe __IMPOSSIBLE__ $ Map.lookup x fs return $ C.mkTApp (C.TCon c) args recConFromProj :: QName -> TCM I.ConHead recConFromProj q = do caseMaybeM (isProjection q) __IMPOSSIBLE__ $ \ proj -> do let d = unArg $ projFromType proj getRecordConstructor d -- | Translate the actual Agda terms, with an environment of all the bound variables -- from patternmatching. Agda terms are in de Bruijn indices, but the expected -- TTerm de bruijn indexes may differ. This is due to additional let-bindings -- introduced by the catch-all machinery, so we need to lookup casetree de bruijn -- indices in the environment as well. substTerm :: I.Term -> CC C.TTerm substTerm term = normaliseStatic term >>= \ term -> case I.ignoreSharing $ I.unSpine term of I.Var ind es -> do ind' <- lookupIndex ind <$> asks ccCxt let args = fromMaybe __IMPOSSIBLE__ $ I.allApplyElims es C.mkTApp (C.TVar ind') <$> substArgs args I.Lam _ ab -> C.TLam <$> local (\e -> e { ccCxt = 0 : (shift 1 $ ccCxt e) }) (substTerm $ I.unAbs ab) I.Lit l -> return $ C.TLit l I.Level _ -> return C.TUnit I.Def q es -> do let args = fromMaybe __IMPOSSIBLE__ $ I.allApplyElims es maybeInlineDef q args I.Con c ci args -> do c' <- lift $ canonicalName $ I.conName c C.mkTApp (C.TCon c') <$> substArgs args I.Shared _ -> __IMPOSSIBLE__ -- the ignoreSharing fun should already take care of this I.Pi _ _ -> return C.TUnit I.Sort _ -> return C.TSort I.MetaV _ _ -> __IMPOSSIBLE__ -- we don't compiled if unsolved metas I.DontCare _ -> return C.TErased normaliseStatic :: I.Term -> CC I.Term normaliseStatic v@(I.Def f es) = lift $ do static <- isStaticFun . theDef <$> getConstInfo f if static then normalise v else pure v normaliseStatic v = pure v maybeInlineDef :: I.QName -> I.Args -> CC C.TTerm maybeInlineDef q vs = ifM (lift $ alwaysInline q) doinline $ do lift $ cacheTreeless q def <- lift $ getConstInfo q case theDef def of fun@Function{} | fun ^. funInline -> doinline | otherwise -> do used <- lift $ getCompiledArgUse q let substUsed False _ = pure C.TErased substUsed True arg = substArg arg C.mkTApp (C.TDef q) <$> sequence [ substUsed u arg | (arg, u) <- zip vs $ used ++ repeat True ] _ -> C.mkTApp (C.TDef q) <$> substArgs vs where doinline = C.mkTApp <$> inline q <*> substArgs vs inline q = lift $ toTreeless' q substArgs :: [Arg I.Term] -> CC [C.TTerm] substArgs = traverse substArg substArg :: Arg I.Term -> CC C.TTerm substArg x | erasable x = return C.TErased | otherwise = substTerm (unArg x) where erasable x = case getRelevance x of Irrelevant -> True NonStrict -> True Forced{} -> False -- TODO: would like this to be True Relevant -> False Agda-2.5.3/src/full/Agda/Compiler/Common.hs0000644000000000000000000001471013154613124016471 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE ScopedTypeVariables #-} module Agda.Compiler.Common where #if __GLASGOW_HASKELL__ <= 708 import Prelude hiding (foldl, mapM_, mapM, sequence, concat) #endif import Data.List as List import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import Data.Char import Data.Function import Data.Semigroup import Data.Monoid hiding ((<>)) import Control.Monad import Control.Monad.State hiding (mapM_, forM_, mapM, forM, sequence) import Agda.Syntax.Common import qualified Agda.Syntax.Abstract.Name as A import qualified Agda.Syntax.Concrete.Name as C import Agda.Syntax.Internal as I import Agda.Interaction.FindFile import Agda.Interaction.Imports import Agda.Interaction.Options import Agda.TypeChecking.Monad import Agda.TypeChecking.Pretty hiding ((<>)) import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.Utils.FileName import qualified Agda.Utils.HashMap as HMap import Agda.Utils.Lens import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Pretty hiding ((<>)) #include "undefined.h" import Agda.Utils.Impossible data IsMain = IsMain | NotMain deriving (Eq, Show) instance Semigroup IsMain where NotMain <> _ = NotMain _ <> NotMain = NotMain IsMain <> IsMain = IsMain instance Monoid IsMain where mempty = IsMain mappend = (<>) doCompile :: forall r. Monoid r => IsMain -> Interface -> (IsMain -> Interface -> TCM r) -> TCM r doCompile isMain i f = do -- The Agda.Primitive module is implicitly assumed to be always imported, -- even though it not necesseraly occurs in iImportedModules. -- TODO: there should be a better way to get hold of Agda.Primitive? [agdaPrimInter] <- filter (("Agda.Primitive"==) . prettyShow . iModuleName) . map miInterface . Map.elems <$> getVisitedModules flip evalStateT Set.empty $ mappend <$> comp NotMain agdaPrimInter <*> comp isMain i where comp :: IsMain -> Interface -> StateT (Set ModuleName) TCM r comp isMain i = do alreadyDone <- Set.member (iModuleName i) <$> get if alreadyDone then return mempty else do imps <- lift $ map miInterface . catMaybes <$> mapM (getVisitedModule . toTopLevelModuleName . fst) (iImportedModules i) ri <- mconcat <$> mapM (comp NotMain) imps lift $ setInterface i r <- lift $ f isMain i modify (Set.insert $ iModuleName i) return $ mappend ri r setInterface :: Interface -> TCM () setInterface i = do opts <- gets (stPersistentOptions . stPersistentState) setCommandLineOptions opts mapM_ setOptionsFromPragma (iPragmaOptions i) stImportedModules .= Set.fromList (map fst $ iImportedModules i) stCurrentModule .= Just (iModuleName i) curIF :: TCM Interface curIF = do mName <- use stCurrentModule case mName of Nothing -> __IMPOSSIBLE__ Just name -> do mm <- getVisitedModule (toTopLevelModuleName name) case mm of Nothing -> __IMPOSSIBLE__ Just mi -> return $ miInterface mi curSig :: TCM Signature curSig = iSignature <$> curIF curMName :: TCM ModuleName curMName = sigMName <$> curSig curDefs :: TCM Definitions curDefs = (^. sigDefinitions) <$> curSig sortDefs :: Definitions -> [(QName, Definition)] sortDefs defs = -- The list is sorted to ensure that the order of the generated -- definitions does not depend on things like the number of bits -- in an Int (see Issue 1900). List.sortBy (compare `on` fst) $ HMap.toList defs sigMName :: Signature -> ModuleName sigMName sig = case Map.keys (sig ^. sigSections) of [] -> __IMPOSSIBLE__ m : _ -> m compileDir :: TCM FilePath compileDir = do mdir <- optCompileDir <$> commandLineOptions case mdir of Just dir -> return dir Nothing -> __IMPOSSIBLE__ repl :: [String] -> String -> String repl subs = go where go ('<':'<':c:'>':'>':s) | 0 <= i && i < length subs = subs !! i ++ go s where i = ord c - ord '0' go (c:s) = c : go s go [] = [] -- | Copy pasted from MAlonzo.... -- Move somewhere else! conArityAndPars :: QName -> TCM (Nat, Nat) conArityAndPars q = do def <- getConstInfo q n <- typeArity (defType def) let Constructor{ conPars = np } = theDef def return (n - np, np) -- | Sets up the compilation environment. inCompilerEnv :: Interface -> TCM a -> TCM a inCompilerEnv mainI cont = do -- Preserve the state (the compiler modifies the state). -- Andreas, 2014-03-23 But we might want to collect Benchmark info, -- so use localTCState. -- FNF, 2017-02-22 we also want to keep the warnings we have encountered, -- so use localTCStateSaving and pick them out. (a , s) <- localTCStateSaving $ do -- Compute the output directory. Note: using commandLineOptions would make -- the current pragma options persistent when we setCommandLineOptions -- below. opts <- gets $ stPersistentOptions . stPersistentState compileDir <- case optCompileDir opts of Just dir -> return dir Nothing -> do -- The default output directory is the project root. let tm = toTopLevelModuleName $ iModuleName mainI f <- findFile tm return $ filePath $ C.projectRoot f tm setCommandLineOptions $ opts { optCompileDir = Just compileDir } -- Andreas, 2017-08-23, issue #2714 recover pragma option --no-main -- Unfortunately, a pragma option is stored in the interface file as -- just a list of strings, thus, the solution is a bit of hack: -- We match on whether @["--no-main"]@ is one of the stored options. when (["--no-main"] `elem` iPragmaOptions mainI) $ stPragmaOptions %= \ o -> o { optCompileNoMain = True } setScope (iInsideScope mainI) -- so that compiler errors don't use overly qualified names ignoreAbstractMode $ do cont -- keep generated warnings let newWarnings = stPostTCWarnings $ stPostScopeState $ s stTCWarnings .= newWarnings return a topLevelModuleName :: ModuleName -> TCM ModuleName topLevelModuleName m = do -- get the names of the visited modules visited <- List.map (iModuleName . miInterface) . Map.elems <$> getVisitedModules -- find the module with the longest matching prefix to m let ms = sortBy (compare `on` (length . mnameToList)) $ List.filter (\ m' -> mnameToList m' `isPrefixOf` mnameToList m) visited case ms of (m' : _) -> return m' -- if we did not get anything, it may be because m is a section -- (a module _ ), see e.g. #1866 [] -> curMName Agda-2.5.3/src/full/Agda/Compiler/CallCompiler.hs0000644000000000000000000000506613154613124017613 0ustar0000000000000000{-# LANGUAGE CPP #-} ------------------------------------------------------------------------ -- | A command which calls a compiler ------------------------------------------------------------------------ module Agda.Compiler.CallCompiler where import qualified Control.Exception as E import Control.Monad.Trans import Data.List ( intercalate ) import qualified Data.List as List import System.Exit import System.IO import System.Process import Agda.TypeChecking.Monad #include "undefined.h" import Agda.Utils.Impossible -- | Calls a compiler: -- -- * Checks the exit code to see if the compiler exits successfully. -- If not, then an exception is raised, containing the text the -- compiler printed to stderr (if any). -- -- * Uses the debug printout machinery to relay any progress -- information the compiler prints to stdout. callCompiler :: Bool -- ^ Should we actually call the compiler -> FilePath -- ^ The path to the compiler -> [String] -- ^ Command-line arguments. -> TCM () callCompiler doCall cmd args = if doCall then do merrors <- callCompiler' cmd args case merrors of Nothing -> return () Just errors -> typeError (CompilationError errors) else reportSLn "compile.cmd" 1 $ "NOT calling: " ++ intercalate " " (cmd : args) -- | Generalisation of @callCompiler@ where the raised exception is -- returned. callCompiler' :: FilePath -- ^ The path to the compiler -> [String] -- ^ Command-line arguments. -> TCM (Maybe String) callCompiler' cmd args = do reportSLn "compile.cmd" 1 $ "Calling: " ++ intercalate " " (cmd : args) (_, out, err, p) <- liftIO $ createProcess (proc cmd args) { std_err = CreatePipe , std_out = CreatePipe } -- In -v0 mode we throw away any progress information printed to -- stdout. case out of Nothing -> __IMPOSSIBLE__ Just out -> forkTCM $ do -- The handle should be in text mode. liftIO $ hSetBinaryMode out False progressInfo <- liftIO $ hGetContents out mapM_ (reportSLn "compile.output" 1) $ lines progressInfo errors <- liftIO $ case err of Nothing -> __IMPOSSIBLE__ Just err -> do -- The handle should be in text mode. hSetBinaryMode err False hGetContents err exitcode <- liftIO $ do -- Ensure that the output has been read before waiting for the -- process. _ <- E.evaluate (length errors) waitForProcess p case exitcode of ExitFailure _ -> return $ Just errors _ -> return Nothing Agda-2.5.3/src/full/Agda/Compiler/Treeless/0000755000000000000000000000000013154613124016470 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Compiler/Treeless/EliminateLiteralPatterns.hs0000644000000000000000000000443113154613124023773 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Converts case matches on literals to if cascades with equality comparisons. module Agda.Compiler.Treeless.EliminateLiteralPatterns where import Control.Applicative import Data.Maybe import Agda.Syntax.Abstract.Name (QName) import Agda.Syntax.Treeless import Agda.Syntax.Literal import qualified Agda.Syntax.Internal as I import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Primitive import Agda.TypeChecking.Substitute import Agda.Compiler.Treeless.Subst import Agda.Utils.Impossible #include "undefined.h" eliminateLiteralPatterns :: TTerm -> TCM TTerm eliminateLiteralPatterns t = do kit <- BuiltinKit <$> getBuiltinName builtinNat <*> getBuiltinName builtinInteger return $ transform kit t data BuiltinKit = BuiltinKit { nat :: Maybe QName , int :: Maybe QName } transform :: BuiltinKit -> TTerm -> TTerm transform kit = tr where tr :: TTerm -> TTerm tr t = case t of TCase sc t def alts | t `elem` [CTChar, CTString, CTQName, CTNat, CTInt, CTFloat] -> foldr litAlt (tr def) alts where litAlt :: TAlt -> TTerm -> TTerm litAlt (TALit l body) cont = tIfThenElse (tOp (eqFromLit l) (TLit l) (TVar sc)) (tr body) cont litAlt _ _ = __IMPOSSIBLE__ TCase sc t@(CTData dt) def alts -> TCase sc t (tr def) (map trAlt alts) where trAlt a = case a of TAGuard g b -> TAGuard (tr g) (tr b) TACon q a b -> TACon q a (tr b) TALit l b -> TALit l (tr b) TCase _ _ _ _ -> __IMPOSSIBLE__ TVar{} -> t TDef{} -> t TCon{} -> t TPrim{} -> t TLit{} -> t TUnit{} -> t TSort{} -> t TErased{} -> t TError{} -> t TLam b -> TLam (tr b) TApp a bs -> TApp (tr a) (map tr bs) TLet e b -> TLet (tr e) (tr b) isCaseOn (CTData dt) xs = dt `elem` catMaybes (map ($ kit) xs) isCaseOn _ _ = False eqFromLit :: Literal -> TPrim eqFromLit x = case x of LitNat _ _ -> PEqI LitFloat _ _ -> PEqF LitString _ _ -> PEqS LitChar _ _ -> PEqC LitQName _ _ -> PEqQ _ -> __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/Compiler/Treeless/Subst.hs0000644000000000000000000000736213154613124020134 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.Compiler.Treeless.Subst where import Control.Applicative import qualified Data.Map as Map import Data.Map (Map) import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend, All(..), Any(..)) import Data.Maybe import Agda.Syntax.Treeless import Agda.Syntax.Internal (Substitution'(..)) import Agda.TypeChecking.Substitute instance DeBruijn TTerm where deBruijnVar = TVar deBruijnView (TVar i) = Just i deBruijnView _ = Nothing instance Subst TTerm TTerm where applySubst IdS t = t applySubst rho t = case t of TDef{} -> t TLit{} -> t TCon{} -> t TPrim{} -> t TUnit{} -> t TSort{} -> t TErased{} -> t TError{} -> t TVar i -> lookupS rho i TApp f ts -> tApp (applySubst rho f) (applySubst rho ts) TLam b -> TLam (applySubst (liftS 1 rho) b) TLet e b -> TLet (applySubst rho e) (applySubst (liftS 1 rho) b) TCase i t d bs -> case applySubst rho (TVar i) of TVar j -> TCase j t (applySubst rho d) (applySubst rho bs) e -> TLet e $ TCase 0 t (applySubst rho' d) (applySubst rho' bs) where rho' = wkS 1 rho where tApp (TPrim PSeq) [TErased, b] = b tApp f ts = TApp f ts instance Subst TTerm TAlt where applySubst rho (TACon c i b) = TACon c i (applySubst (liftS i rho) b) applySubst rho (TALit l b) = TALit l (applySubst rho b) applySubst rho (TAGuard g b) = TAGuard (applySubst rho g) (applySubst rho b) newtype UnderLambda = UnderLambda Any deriving (Eq, Ord, Show, Semigroup, Monoid) newtype SeqArg = SeqArg All deriving (Eq, Ord, Show, Semigroup, Monoid) data Occurs = Occurs Int UnderLambda SeqArg deriving (Eq, Ord, Show) once :: Occurs once = Occurs 1 mempty (SeqArg $ All False) inSeq :: Occurs -> Occurs inSeq (Occurs n l _) = Occurs n l mempty underLambda :: Occurs -> Occurs underLambda o = o <> Occurs 0 (UnderLambda $ Any True) mempty instance Semigroup Occurs where Occurs a k s <> Occurs b l t = Occurs (a + b) (k <> l) (s <> t) instance Monoid Occurs where mempty = Occurs 0 mempty mempty mappend = (<>) class HasFree a where freeVars :: a -> Map Int Occurs freeIn :: HasFree a => Int -> a -> Bool freeIn i x = Map.member i (freeVars x) occursIn :: HasFree a => Int -> a -> Occurs occursIn i x = fromMaybe mempty $ Map.lookup i (freeVars x) instance HasFree Int where freeVars x = Map.singleton x once instance HasFree a => HasFree [a] where freeVars xs = Map.unionsWith mappend $ map freeVars xs instance (HasFree a, HasFree b) => HasFree (a, b) where freeVars (x, y) = Map.unionWith mappend (freeVars x) (freeVars y) data Binder a = Binder Int a instance HasFree a => HasFree (Binder a) where freeVars (Binder 0 x) = freeVars x freeVars (Binder k x) = Map.filterWithKey (\ k _ -> k >= 0) $ Map.mapKeysMonotonic (subtract k) $ freeVars x newtype InSeq a = InSeq a instance HasFree a => HasFree (InSeq a) where freeVars (InSeq x) = inSeq <$> freeVars x instance HasFree TTerm where freeVars t = case t of TDef{} -> Map.empty TLit{} -> Map.empty TCon{} -> Map.empty TPrim{} -> Map.empty TUnit{} -> Map.empty TSort{} -> Map.empty TErased{} -> Map.empty TError{} -> Map.empty TVar i -> freeVars i TApp (TPrim PSeq) [TVar x, b] -> freeVars (InSeq x, b) TApp f ts -> freeVars (f, ts) TLam b -> underLambda <$> freeVars (Binder 1 b) TLet e b -> freeVars (e, Binder 1 b) TCase i _ d bs -> freeVars (i, (d, bs)) instance HasFree TAlt where freeVars a = case a of TACon _ i b -> freeVars (Binder i b) TALit _ b -> freeVars b TAGuard g b -> freeVars (g, b) Agda-2.5.3/src/full/Agda/Compiler/Treeless/NormalizeNames.hs0000644000000000000000000000276713154613124021764 0ustar0000000000000000-- | Ensures that all occurences of an abstract name share -- the same concrete name. -- -- Apply this transformation if your backend uses concrete names -- for identification purposes! -- -- The identity of an abstract name is only the nameId, the concrete -- name is only a naming suggestion. If renaming imports are used, -- the concrete name may change. This transformation makes sure -- that all occurences of an abstract name share the same -- concrete name. -- -- This transfomation should be run as the last transformation. {-# LANGUAGE CPP #-} module Agda.Compiler.Treeless.NormalizeNames ( normalizeNames ) where import Control.Applicative import Agda.TypeChecking.Monad import Agda.Syntax.Treeless normalizeNames :: TTerm -> TCM TTerm normalizeNames = tr where tr t = case t of TDef q -> do q' <- defName <$> getConstInfo q return $ TDef q' TVar{} -> done TCon{} -> done TPrim{} -> done TLit{} -> done TUnit{} -> done TSort{} -> done TErased{} -> done TError{} -> done TLam b -> TLam <$> tr b TApp a bs -> TApp <$> tr a <*> mapM tr bs TLet e b -> TLet <$> tr e <*> tr b TCase sc t def alts -> TCase sc t <$> tr def <*> mapM trAlt alts where done :: TCM TTerm done = return t trAlt a = case a of TAGuard g b -> TAGuard <$> tr g <*> tr b TACon q a b -> TACon q a <$> tr b TALit l b -> TALit l <$> tr b Agda-2.5.3/src/full/Agda/Compiler/Treeless/Pretty.hs0000644000000000000000000001072313154613124020316 0ustar0000000000000000 {-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.Compiler.Treeless.Pretty () where import Control.Arrow ((&&&), (***), first, second) import Control.Applicative import Control.Monad.Reader import Data.Maybe import Agda.Syntax.Treeless import Agda.Utils.Pretty data PEnv = PEnv { pPrec :: Int , pFresh :: [String] , pBound :: [String] } type P = Reader PEnv withName :: (String -> P a) -> P a withName k = withNames 1 $ \[x] -> k x withNames :: Int -> ([String] -> P a) -> P a withNames n k = do (xs, ys) <- asks $ splitAt n . pFresh local (\ e -> e { pFresh = ys }) (k xs) bindName :: String -> P a -> P a bindName x = local $ \ e -> e { pBound = x : pBound e } bindNames :: [String] -> P a -> P a bindNames xs p = foldr bindName p xs paren :: Int -> P Doc -> P Doc paren p doc = do n <- asks pPrec (if p < n then parens else id) <$> doc prec :: Int -> P a -> P a prec p = local $ \ e -> e { pPrec = p } name :: Int -> P String name x = asks $ (!! x) . (++ map (("^" ++) . show) [1..]) . pBound runP :: P a -> a runP p = runReader p PEnv{ pPrec = 0, pFresh = names, pBound = [] } where names = [ x ++ i | i <- "" : map show [1..], x <- map (:[]) ['a'..'z'] ] instance Pretty TTerm where prettyPrec p t = runP $ prec p (pTerm t) opName :: TPrim -> String opName PAdd = "+" opName PSub = "-" opName PMul = "*" opName PQuot = "quot" opName PRem = "rem" opName PGeq = ">=" opName PLt = "<" opName PEqI = "==I" opName PEqF = "==F" opName PEqS = "==S" opName PEqC = "==C" opName PEqQ = "==Q" opName PIf = "if_then_else_" opName PSeq = "seq" isInfix :: TPrim -> Maybe (Int, Int, Int) isInfix op = case op of PMul -> l 7 PAdd -> l 6 PSub -> l 6 PGeq -> non 4 PLt -> non 4 p | isPrimEq p -> non 4 _ -> Nothing where l n = Just (n, n, n + 1) r n = Just (n, n + 1, n) non n = Just (n, n + 1, n + 1) pTerm' :: Int -> TTerm -> P Doc pTerm' p = prec p . pTerm pTerm :: TTerm -> P Doc pTerm t = case t of TVar x -> text <$> name x TApp (TPrim op) [a, b] | Just (c, l, r) <- isInfix op -> paren c $ sep <$> sequence [ pTerm' l a , pure $ text $ opName op , pTerm' r b ] TApp (TPrim PIf) [a, b, c] -> paren 0 $ (\ a b c -> sep [ text "if" <+> a , nest 2 $ text "then" <+> b , nest 2 $ text "else" <+> c ]) <$> pTerm' 0 a <*> pTerm' 0 b <*> pTerm c TDef f -> pure $ pretty f TCon c -> pure $ pretty c TLit l -> pure $ pretty l TPrim op | isJust (isInfix op) -> pure $ text ("_" ++ opName op ++ "_") | otherwise -> pure $ text (opName op) TApp f es -> paren 9 $ (\a bs -> sep [a, nest 2 $ fsep bs]) <$> pTerm' 9 f <*> mapM (pTerm' 10) es TLam _ -> paren 0 $ withNames n $ \xs -> bindNames xs $ (\b -> sep [ text ("λ " ++ unwords xs ++ " →") , nest 2 b ]) <$> pTerm' 0 b where (n, b) = lamV t lamV (TLam b) = first succ $ lamV b lamV t = (0, t) TLet{} -> paren 0 $ withNames (length es) $ \ xs -> (\ (binds, b) -> sep [ text "let" <+> vcat [ sep [ text x <+> text "=" , nest 2 e ] | (x, e) <- binds ] <+> text "in", b ]) <$> pLets (zip xs es) b where (es, b) = letV t letV (TLet e b) = first (e :) $ letV b letV t = ([], t) pLets [] b = ([],) <$> pTerm' 0 b pLets ((x, e) : bs) b = do e <- pTerm' 0 e first ((x, e) :) <$> bindName x (pLets bs b) TCase x _ def alts -> paren 0 $ (\ sc alts defd -> sep [ text "case" <+> sc <+> text "of" , nest 2 $ vcat (alts ++ [ text "_ →" <+> defd | null alts || def /= TError TUnreachable ]) ] ) <$> pTerm' 0 (TVar x) <*> mapM pAlt alts <*> pTerm' 0 def where pAlt (TALit l b) = pAlt' <$> pTerm' 0 (TLit l) <*> pTerm' 0 b pAlt (TAGuard g b) = pAlt' <$> ((text "_" <+> text "|" <+>) <$> pTerm' 0 g) <*> (pTerm' 0 b) pAlt (TACon c a b) = withNames a $ \ xs -> bindNames xs $ pAlt' <$> pTerm' 0 (TApp (TCon c) [TVar i | i <- reverse [0..a - 1]]) <*> pTerm' 0 b pAlt' p b = sep [p <+> text "→", nest 2 b] TUnit -> pure $ text "()" TSort -> pure $ text "Set" TErased -> pure $ text "_" TError err -> paren 9 $ pure $ text "error" <+> text (show (show err)) Agda-2.5.3/src/full/Agda/Compiler/Treeless/AsPatterns.hs0000644000000000000000000000445613154613124021121 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Compiler.Treeless.AsPatterns (recoverAsPatterns) where import Control.Applicative import Control.Monad.Reader import Data.Monoid import Agda.Syntax.Treeless import Agda.Syntax.Literal import Agda.TypeChecking.Substitute import Agda.Compiler.Treeless.Subst import Agda.Compiler.Treeless.Compare import Agda.Utils.Impossible #include "undefined.h" data AsPat = AsPat Int QName [Int] -- x@(c ys) deriving (Show) wk :: Int -> AsPat -> AsPat wk n (AsPat x c ys) = AsPat (n + x) c (map (n +) ys) type S = Reader [AsPat] runS :: S a -> a runS m = runReader m [] underBinds :: Int -> S a -> S a underBinds 0 = id underBinds n = local (map $ wk n) bindAsPattern :: AsPat -> S a -> S a bindAsPattern p = local (p :) lookupAsPattern :: QName -> [TTerm] -> S TTerm lookupAsPattern c vs | Just xs <- allVars vs = do ps <- ask case [ x | AsPat x c' ys <- ps, c == c', xs == ys ] of x : _ -> pure $ TVar x _ -> pure $ mkTApp (TCon c) vs | otherwise = pure $ mkTApp (TCon c) vs where allVars = mapM getVar getVar (TVar x) = Just x getVar _ = Nothing -- what about erased? -- | We lose track of @-patterns in the internal syntax. This pass puts them -- back. recoverAsPatterns :: Monad m => TTerm -> m TTerm recoverAsPatterns t = return $ runS (recover t) recover :: TTerm -> S TTerm recover t = case t of TApp f vs -> do f <- recover f vs <- mapM recover vs tApp f vs TLam b -> TLam <$> underBinds 1 (recover b) TCon{} -> tApp t [] -- need to recover nullary constructors as well (to make deep @-patterns work) TLet v b -> TLet <$> recover v <*> underBinds 1 (recover b) TCase x ct d bs -> TCase x ct <$> recover d <*> mapM (recoverAlt x) bs TLit{} -> pure t TVar{} -> pure t TPrim{} -> pure t TDef{} -> pure t TUnit{} -> pure t TSort{} -> pure t TErased{} -> pure t TError{} -> pure t recoverAlt :: Int -> TAlt -> S TAlt recoverAlt x b = case b of TACon c n b -> TACon c n <$> underBinds n (bindAsPattern (AsPat (x + n) c [n - 1, n - 2..0]) $ recover b) TAGuard g b -> TAGuard <$> recover g <*> recover b TALit l b -> TALit l <$> recover b tApp :: TTerm -> [TTerm] -> S TTerm tApp (TCon c) vs = lookupAsPattern c vs tApp f vs = pure $ mkTApp f vs Agda-2.5.3/src/full/Agda/Compiler/Treeless/Unused.hs0000644000000000000000000000460213154613124020271 0ustar0000000000000000 module Agda.Compiler.Treeless.Unused ( usedArguments , stripUnusedArguments ) where import Control.Arrow (first) import Control.Applicative import qualified Data.Set as Set import Data.Maybe import Agda.Syntax.Treeless import Agda.TypeChecking.Monad import Agda.TypeChecking.Substitute import Agda.Compiler.Treeless.Subst import Agda.Compiler.Treeless.Pretty import Agda.Utils.Pretty (prettyShow) usedArguments :: QName -> TTerm -> TCM [Bool] usedArguments q t = computeUnused q b (replicate n False) where (n, b) = tLamView t computeUnused :: QName -> TTerm -> [Bool] -> TCM [Bool] computeUnused q t used = do reportSLn "treeless.opt.unused" 50 $ "Unused approximation for " ++ prettyShow q ++ ": " ++ unwords [ if u then [x] else "_" | (x, u) <- zip ['a'..] used ] setCompiledArgUse q used fv <- go t let used' = [ Set.member i fv | (i, _) <- reverse $ zip [0..] used ] if used == used' then return used' else computeUnused q t used' where go t = case t of TVar x -> pure $ Set.singleton x TPrim{} -> pure Set.empty TDef{} -> pure Set.empty TLit{} -> pure Set.empty TCon{} -> pure Set.empty TApp (TDef f) ts -> do used <- getCompiledArgUse f Set.unions <$> sequence [ go t | (t, True) <- zip ts $ used ++ repeat True ] TApp f ts -> Set.unions <$> mapM go (f : ts) TLam b -> underBinder <$> go b TLet e b -> Set.union <$> go e <*> (underBinder <$> go b) TCase x _ d bs -> Set.insert x . Set.unions <$> ((:) <$> go d <*> mapM goAlt bs) TUnit{} -> pure Set.empty TSort{} -> pure Set.empty TErased{} -> pure Set.empty TError{} -> pure Set.empty goAlt (TALit _ b) = go b goAlt (TAGuard g b) = Set.union <$> go g <*> go b goAlt (TACon _ a b) = underBinders a <$> go b underBinder = underBinders 1 underBinders 0 = id underBinders n = Set.filter (>= 0) . Set.mapMonotonic (subtract n) stripUnusedArguments :: [Bool] -> TTerm -> TTerm stripUnusedArguments used t = mkTLam m $ applySubst rho b where (n, b) = tLamView t m = length $ filter id used' used' = reverse $ take n $ used ++ repeat True rho = computeSubst used' computeSubst (False : bs) = TErased :# computeSubst bs computeSubst (True : bs) = liftS 1 $ computeSubst bs computeSubst [] = idS Agda-2.5.3/src/full/Agda/Compiler/Treeless/Compare.hs0000644000000000000000000000405013154613124020411 0ustar0000000000000000module Agda.Compiler.Treeless.Compare (equalTerms) where import Agda.Syntax.Treeless import Agda.TypeChecking.Substitute import Agda.Compiler.Treeless.Subst equalTerms :: TTerm -> TTerm -> Bool equalTerms u v = case (evalPrims u, evalPrims v) of (TLet s u@(TCase 0 _ _ _), TLet t v@(TCase 0 _ _ _)) -> equalTerms s t && equalTerms u v (TLet _ (TCase 0 _ _ _), _) -> False (_, TLet _ (TCase 0 _ _ _)) -> False (TLet t u, v) -> equalTerms (subst 0 t u) v (u, TLet t v) -> equalTerms u (subst 0 t v) (u, v) | u == v -> True (TApp f us, TApp g vs) -> eqList equalTerms (f : us) (g : vs) (TCase x _ d as, TCase y _ e bs) -> x == y && equalTerms d e && eqList equalAlts as bs (TLam u, TLam v) -> equalTerms u v _ -> False equalAlts :: TAlt -> TAlt -> Bool equalAlts (TACon c a b) (TACon c1 a1 b1) = (c, a) == (c1, a1) && equalTerms b b1 equalAlts (TALit l b) (TALit l1 b1) = l == l1 && equalTerms b b1 equalAlts (TAGuard g b) (TAGuard g1 b1) = equalTerms g g1 && equalTerms b b1 equalAlts _ _ = False eqList :: (a -> a -> Bool) -> [a] -> [a] -> Bool eqList eq xs ys = length xs == length ys && and (zipWith eq xs ys) evalPrims :: TTerm -> TTerm evalPrims (TApp (TPrim op) [a, b]) | Just n <- intView (evalPrims a), Just m <- intView (evalPrims b), Just r <- applyPrim op n m = tInt r evalPrims t = t applyPrim :: TPrim -> Integer -> Integer -> Maybe Integer applyPrim PAdd a b = Just (a + b) applyPrim PSub a b = Just (a - b) applyPrim PMul a b = Just (a * b) applyPrim PQuot a b | b /= 0 = Just (quot a b) | otherwise = Nothing applyPrim PRem a b | b /= 0 = Just (rem a b) | otherwise = Nothing applyPrim PGeq _ _ = Nothing applyPrim PLt _ _ = Nothing applyPrim PEqI _ _ = Nothing applyPrim PEqF _ _ = Nothing applyPrim PEqC _ _ = Nothing applyPrim PEqS _ _ = Nothing applyPrim PEqQ _ _ = Nothing applyPrim PIf _ _ = Nothing applyPrim PSeq _ _ = Nothing Agda-2.5.3/src/full/Agda/Compiler/Treeless/Erase.hs0000644000000000000000000002033513154613124020066 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Compiler.Treeless.Erase (eraseTerms, computeErasedConstructorArgs) where import Control.Arrow ((&&&), (***), first, second) import Control.Applicative import Control.Monad import Control.Monad.State import Data.Map (Map) import qualified Data.Map as Map import Agda.Syntax.Common import Agda.Syntax.Internal as I import Agda.Syntax.Abstract.Name (QName) import Agda.Syntax.Position import Agda.Syntax.Treeless import Agda.Syntax.Literal import Agda.TypeChecking.Substitute import Agda.TypeChecking.Monad as I import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Telescope import Agda.TypeChecking.Reduce import Agda.TypeChecking.Datatypes import Agda.TypeChecking.Pretty import Agda.Compiler.Treeless.Subst import Agda.Compiler.Treeless.Pretty import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.Maybe import Agda.Utils.Memo import Agda.Utils.Monad import Agda.Utils.Pretty (prettyShow) import qualified Agda.Utils.Pretty as P #include "undefined.h" import Agda.Utils.Impossible data ESt = ESt { _funMap :: Map QName FunInfo , _typeMap :: Map QName TypeInfo } funMap :: Lens' (Map QName FunInfo) ESt funMap f r = f (_funMap r) <&> \ a -> r { _funMap = a } typeMap :: Lens' (Map QName TypeInfo) ESt typeMap f r = f (_typeMap r) <&> \ a -> r { _typeMap = a } type E = StateT ESt TCM runE :: E a -> TCM a runE m = evalStateT m (ESt Map.empty Map.empty) -- | Takes the name of the data/record type. computeErasedConstructorArgs :: QName -> TCM () computeErasedConstructorArgs d = do cs <- getConstructors d runE $ mapM_ getFunInfo cs eraseTerms :: QName -> TTerm -> TCM TTerm eraseTerms q = runE . eraseTop q where eraseTop q t = do (_, h) <- getFunInfo q case h of Erasable -> pure TErased Empty -> pure TErased _ -> erase t erase t = case tAppView t of TCon c : vs -> do (rs, h) <- getFunInfo c when (length rs < length vs) __IMPOSSIBLE__ case h of Erasable -> pure TErased Empty -> pure TErased _ -> tApp (TCon c) <$> zipWithM eraseRel rs vs TDef f : vs -> do (rs, h) <- getFunInfo f case h of Erasable -> pure TErased Empty -> pure TErased _ -> tApp (TDef f) <$> zipWithM eraseRel (rs ++ repeat Relevant) vs _ -> case t of TVar{} -> pure t TDef{} -> pure t TPrim{} -> pure t TLit{} -> pure t TCon{} -> pure t TApp f es -> tApp <$> erase f <*> mapM erase es TLam b -> tLam <$> erase b TLet e b -> do e <- erase e if isErased e then case b of TCase 0 _ _ _ -> tLet TErased <$> erase b _ -> erase $ subst 0 TErased b else tLet e <$> erase b TCase x t d bs -> do d <- ifM (isComplete t bs) (pure tUnreachable) (erase d) bs <- mapM eraseAlt bs tCase x t d bs TUnit -> pure t TSort -> pure t TErased -> pure t TError{} -> pure t tLam TErased = TErased tLam t = TLam t tLet e b | freeIn 0 b = TLet e b | otherwise = strengthen __IMPOSSIBLE__ b tApp f [] = f tApp TErased _ = TErased tApp f _ | isUnreachable f = tUnreachable tApp f es = TApp f es tCase x t d bs | isErased d && all (isErased . aBody) bs = pure TErased | otherwise = case bs of [TACon c a b] -> do h <- snd <$> getFunInfo c case h of NotErasable -> noerase Empty -> pure TErased Erasable -> (if a == 0 then pure else erase) $ applySubst (replicate a TErased ++# idS) b -- might enable more erasure _ -> noerase where noerase = pure $ TCase x t d bs isErased t = t == TErased || isUnreachable t eraseRel r t | erasableR r = pure TErased | otherwise = erase t eraseAlt a = case a of TALit l b -> TALit l <$> erase b TACon c a b -> do rs <- map erasableR . fst <$> getFunInfo c let sub = foldr (\ e -> if e then (TErased :#) . wkS 1 else liftS 1) idS $ reverse rs TACon c a <$> erase (applySubst sub b) TAGuard g b -> TAGuard <$> erase g <*> erase b -- | Doesn't have any type information (other than the name of the data type), -- so we can't do better than checking if all constructors are present. isComplete :: CaseType -> [TAlt] -> E Bool isComplete (CTData d) bs = do cs <- lift $ getConstructors d return $ length cs == length [ b | b@TACon{} <- bs ] isComplete _ _ = pure False data TypeInfo = Empty | Erasable | NotErasable deriving (Eq, Show) sumTypeInfo :: [TypeInfo] -> TypeInfo sumTypeInfo is = foldr plus Empty is where plus Empty r = r plus r Empty = r plus Erasable r = r plus r Erasable = r plus NotErasable NotErasable = NotErasable erasableR :: Relevance -> Bool erasableR Relevant = False erasableR Forced{} = False -- TODO: should be True but need to transform clauses erasableR NonStrict = True erasableR Irrelevant = True erasable :: TypeInfo -> Bool erasable Erasable = True erasable Empty = True erasable NotErasable = False type FunInfo = ([Relevance], TypeInfo) getFunInfo :: QName -> E FunInfo getFunInfo q = memo (funMap . key q) $ getInfo q where getInfo q = do (rs, t) <- do (tel, t) <- lift $ typeWithoutParams q is <- mapM (getTypeInfo . snd . dget) tel return (zipWith (mkR . getRelevance) tel is, t) h <- if isAbsurdLambdaName q then pure Erasable else getTypeInfo t lift $ reportSLn "treeless.opt.erase.info" 50 $ "type info for " ++ prettyShow q ++ ": " ++ show rs ++ " -> " ++ show h lift $ setErasedConArgs q $ map erasableR rs return (rs, h) -- Treat empty or erasable arguments as NonStrict (and thus erasable) mkR :: Relevance -> TypeInfo -> Relevance mkR Irrelevant _ = Irrelevant mkR r NotErasable = r mkR _ Empty = NonStrict mkR _ Erasable = NonStrict telListView :: Type -> TCM (ListTel, Type) telListView t = do TelV tel t <- telView t return (telToList tel, t) typeWithoutParams :: QName -> TCM (ListTel, Type) typeWithoutParams q = do def <- getConstInfo q let d = case I.theDef def of Function{ funProjection = Just Projection{ projIndex = i } } -> i - 1 Constructor{ conPars = n } -> n _ -> 0 first (drop d) <$> telListView (defType def) getTypeInfo :: Type -> E TypeInfo getTypeInfo t0 = do (tel, t) <- lift $ telListView t0 et <- case ignoreSharing $ I.unEl t of I.Def d _ -> typeInfo d Sort{} -> return Erasable _ -> return NotErasable is <- mapM (getTypeInfo . snd . dget) tel let e | any (== Empty) is = Erasable | null is = et -- TODO: guard should really be "all inhabited is" | et == Empty = Erasable | otherwise = et lift $ reportSDoc "treeless.opt.erase.type" 50 $ prettyTCM t0 <+> text ("is " ++ show e) return e where typeInfo :: QName -> E TypeInfo typeInfo q = memoRec (typeMap . key q) Erasable $ do -- assume recursive occurrences are erasable def <- lift $ getConstInfo q mcs <- return $ case I.theDef def of I.Datatype{ dataCons = cs } -> Just cs I.Record{ recConHead = c } -> Just [conName c] _ -> Nothing case mcs of Just [c] -> do (ts, _) <- lift $ typeWithoutParams c let rs = map getRelevance ts is <- mapM (getTypeInfo . snd . dget) ts let er = and [ erasable i || erasableR r | (i, r) <- zip is rs ] return $ if er then Erasable else NotErasable Just [] -> return Empty Just (_:_:_) -> return NotErasable Nothing -> case I.theDef def of I.Function{ funClauses = cs } -> sumTypeInfo <$> mapM (maybe (return Empty) (getTypeInfo . El Prop) . clauseBody) cs _ -> return NotErasable Agda-2.5.3/src/full/Agda/Compiler/Treeless/Identity.hs0000644000000000000000000000631113154613124020616 0ustar0000000000000000 module Agda.Compiler.Treeless.Identity ( detectIdentityFunctions ) where import Control.Applicative import Data.Foldable (foldMap) import Data.Semigroup import Data.List.NonEmpty (NonEmpty(..)) import qualified Data.List as List import Agda.Syntax.Treeless import Agda.TypeChecking.Substitute import Agda.TypeChecking.Monad import Agda.Utils.Lens detectIdentityFunctions :: QName -> TTerm -> TCM TTerm detectIdentityFunctions q t = case isIdentity q t of Nothing -> return t Just (n, k) -> do markInline q def <- theDef <$> getConstInfo q return $ mkTLam n $ TVar k -- If isIdentity f t = Just (n, k) then -- f = t is equivalent to f = λ xₙ₋₁ .. x₀ → xk isIdentity :: QName -> TTerm -> Maybe (Int, Int) isIdentity q t = trivialIdentity q t <|> recursiveIdentity q t -- Does the function recurse on an argument, rebuilding the same value again. recursiveIdentity :: QName -> TTerm -> Maybe (Int, Int) recursiveIdentity q t = case b of TCase x _ (TError TUnreachable) bs | all (identityBranch x) bs -> pure (n, x) _ -> empty -- TODO: lets? where (n, b) = tLamView t identityBranch _ TALit{} = False identityBranch _ TAGuard{} = False identityBranch x (TACon c a b) = case b of TApp (TCon c') args -> c == c' && identityArgs a args TVar y -> y == x + a -- from @-pattern recovery _ -> False -- TODO: nested cases where identityArgs a args = length args == a && and (zipWith match (reverse args) [0..]) proj x args = reverse args !! x match TErased _ = True match (TVar z) y = z == y match (TApp (TDef f) args) y = f == q && length args == n && match (proj x args) y match _ _ = False data IdentityIn = IdIn [Int] notId :: IdentityIn notId = IdIn [] instance Semigroup IdentityIn where IdIn xs <> IdIn ys = IdIn $ List.intersect xs ys -- Does the function always return one of its arguments unchanged (possibly -- through recursive calls). trivialIdentity :: QName -> TTerm -> Maybe (Int, Int) trivialIdentity q t = case go 0 b of IdIn [x] -> pure (n, x) IdIn [] -> Nothing IdIn (_:_:_) -> Nothing -- only happens for empty functions (which will never be called) where (n, b) = tLamView t go :: Int -> TTerm -> IdentityIn go k t = case t of TVar x | x >= k -> IdIn [x - k] | otherwise -> notId TLet _ b -> go (k + 1) b TCase _ _ d bs -> sconcat (go k d :| map (goAlt k) bs) TApp (TDef f) args | f == q -> IdIn [ y | (TVar x, y) <- zip (reverse args) [0..], y + k == x ] TApp{} -> notId TLam{} -> notId TLit{} -> notId TDef{} -> notId TCon{} -> notId TPrim{} -> notId TUnit{} -> notId TSort{} -> notId TErased{} -> notId TError{} -> notId goAlt :: Int -> TAlt -> IdentityIn goAlt k (TALit _ b) = go k b goAlt k (TAGuard _ b) = go k b goAlt k (TACon _ n b) = go (k + n) b Agda-2.5.3/src/full/Agda/Compiler/Treeless/GuardsToPrims.hs0000644000000000000000000000315413154613124021572 0ustar0000000000000000-- | Translates guard alternatives to if-then-else cascades. -- -- The builtin translation must be run before this transformation. {-# LANGUAGE CPP #-} module Agda.Compiler.Treeless.GuardsToPrims ( convertGuards ) where import qualified Data.List as List import Agda.Syntax.Abstract.Name (QName) import Agda.Syntax.Treeless import Agda.Syntax.Literal import Agda.TypeChecking.Substitute import Agda.Compiler.Treeless.Subst import Agda.Utils.Impossible #include "undefined.h" convertGuards :: TTerm -> TTerm convertGuards = tr where tr t = case t of TCase sc t def alts -> if null otherAlts then def' else TCase sc t def' (fmap trAlt otherAlts) where (plusAlts, otherAlts) = splitAlts alts guardedAlt :: TAlt -> TTerm -> TTerm guardedAlt (TAGuard g body) cont = tIfThenElse (tr g) (tr body) (tr cont) guardedAlt _ _ = __IMPOSSIBLE__ def' = foldr guardedAlt (tr def) plusAlts trAlt (TAGuard{}) = __IMPOSSIBLE__ trAlt a = a { aBody = tr (aBody a) } TVar{} -> t TDef{} -> t TCon{} -> t TPrim{} -> t TLit{} -> t TUnit{} -> t TSort{} -> t TErased{} -> t TError{} -> t TLam b -> TLam (tr b) TApp a bs -> TApp (tr a) (map tr bs) TLet e b -> TLet (tr e) (tr b) -- | Split alts into TAGuard alts and other alts. splitAlts :: [TAlt] -> ([TAlt], [TAlt]) splitAlts = List.partition isGuardAlt where isGuardAlt (TAGuard _ _) = True isGuardAlt _ = False Agda-2.5.3/src/full/Agda/Compiler/Treeless/Builtin.hs0000644000000000000000000001521513154613124020436 0ustar0000000000000000-- | Translates the Agda builtin nat datatype to arbitrary-precision integers. -- -- Philipp, 20150921: -- At the moment, this optimization is the reason that there is a -- TAPlus alternative. For Haskell, this can easily be translated to guards. However, in -- the long term it would be easier for the backends if these things were translated -- directly to a less-than primitive and if-then-else expressions or similar. This would -- require us to add some internal Bool-datatype as compiler-internal type and -- a primitive less-than function, which will be much easier once Treeless -- is used for whole modules. -- -- Ulf, 2015-09-21: No, actually we need the n+k patterns, or at least guards. -- Representing them with if-then-else would make it a lot harder to do -- optimisations that analyse case tree, like impossible case elimination. -- -- Ulf, 2015-10-30: Guards are actually a better primitive. Fixed that. {-# LANGUAGE CPP #-} module Agda.Compiler.Treeless.Builtin (translateBuiltins) where import Control.Applicative import qualified Agda.Syntax.Internal as I import Agda.Syntax.Abstract.Name (QName) import Agda.Syntax.Position import Agda.Syntax.Treeless import Agda.Syntax.Literal import Agda.TypeChecking.Substitute import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.Compiler.Treeless.Subst import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.Maybe import Agda.Utils.Impossible #include "undefined.h" data BuiltinKit = BuiltinKit { isZero :: QName -> Bool , isSuc :: QName -> Bool , isPos :: QName -> Bool , isNegSuc :: QName -> Bool , isPlus :: QName -> Bool , isTimes :: QName -> Bool , isLess :: QName -> Bool , isEqual :: QName -> Bool , isForce :: QName -> Bool } builtinKit :: TCM BuiltinKit builtinKit = BuiltinKit <$> isB con builtinZero <*> isB con builtinSuc <*> isB con builtinIntegerPos <*> isB con builtinIntegerNegSuc <*> isB def builtinNatPlus <*> isB def builtinNatTimes <*> isB def builtinNatLess <*> isB def builtinNatEquals <*> isP pf "primForce" where con (I.Con c _ _) = pure $ I.conName c con _ = Nothing def (I.Def d _) = pure d def _ = Nothing pf = Just . primFunName is a b = maybe (const False) (==) . (a =<<) <$> b isB a b = is a (getBuiltin' b) isP a p = is a (getPrimitive' p) translateBuiltins :: TTerm -> TCM TTerm translateBuiltins t = do kit <- builtinKit return $ transform kit t transform :: BuiltinKit -> TTerm -> TTerm transform BuiltinKit{..} = tr where tr t = case t of TCon c | isZero c -> tInt 0 | isSuc c -> TLam (tPlusK 1 (TVar 0)) | isPos c -> TLam (TVar 0) | isNegSuc c -> TLam $ tNegPlusK 1 (TVar 0) TDef f | isPlus f -> TPrim PAdd | isTimes f -> TPrim PMul | isLess f -> TPrim PLt | isEqual f -> TPrim PEqI -- Note: Don't do this for builtinNatMinus! PSub is integer minus and -- builtin minus is monus. The simplifier will do it if it can see -- that it won't underflow. TApp (TDef q) [_, _, _, _, e, f] | isForce q -> tr $ TLet e $ tOp PSeq (TVar 0) $ mkTApp (raise 1 f) [TVar 0] TApp (TCon s) [e] | isSuc s -> case tr e of TLit (LitNat r n) -> tInt (n + 1) e | Just (i, e) <- plusKView e -> tPlusK (i + 1) e e -> tPlusK 1 e TApp (TCon c) [e] | isPos c -> tr e | isNegSuc c -> case tr e of TLit (LitNat _ n) -> tInt (-n - 1) e | Just (i, e) <- plusKView e -> tNegPlusK (i + 1) e e -> tNegPlusK 1 e TCase e t d bs -> TCase e (caseType t bs) (tr d) $ concatMap trAlt bs where trAlt b = case b of TACon c 0 b | isZero c -> [TALit (LitNat noRange 0) (tr b)] TACon c 1 b | isSuc c -> case tr b of -- Collapse nested n+k patterns TCase 0 _ d bs' -> map sucBranch bs' ++ [nPlusKAlt 1 d] b -> [nPlusKAlt 1 b] where sucBranch (TALit (LitNat r i) b) = TALit (LitNat r (i + 1)) $ TLet (tInt i) b sucBranch alt | Just (k, b) <- nPlusKView alt = nPlusKAlt (k + 1) $ TLet (tOp PAdd (TVar 0) (tInt 1)) $ applySubst ([TVar 1, TVar 0] ++# wkS 2 idS) b sucBranch _ = __IMPOSSIBLE__ nPlusKAlt k b = TAGuard (tOp PGeq (TVar e) (tInt k)) $ TLet (tOp PSub (TVar e) (tInt k)) b str err = compactS err [Nothing] TACon c 1 b | isPos c -> case tr b of -- collapse nested nat patterns TCase 0 _ d bs -> map sub bs ++ [posAlt d] b -> [posAlt b] where -- subst scrutinee for the pos argument sub :: Subst TTerm a => a -> a sub = applySubst (TVar e :# IdS) posAlt b = TAGuard (tOp PGeq (TVar e) (tInt 0)) $ sub b TACon c 1 b | isNegSuc c -> case tr b of -- collapse nested nat patterns TCase 0 _ d bs -> map negsucBranch bs ++ [negAlt d] b -> [negAlt b] where body b = TLet (tNegPlusK 1 (TVar e)) b negAlt b = TAGuard (tOp PLt (TVar e) (tInt 0)) $ body b negsucBranch (TALit (LitNat r i) b) = TALit (LitNat r (-i - 1)) $ body b negsucBranch alt | Just (k, b) <- nPlusKView alt = TAGuard (tOp PLt (TVar e) (tInt (-k))) $ body $ TLet (tNegPlusK (k + 1) (TVar $ e + 1)) b negsucBranch _ = __IMPOSSIBLE__ TACon c a b -> [TACon c a (tr b)] TALit l b -> [TALit l (tr b)] TAGuard g b -> [TAGuard (tr g) (tr b)] TVar{} -> t TDef{} -> t TCon{} -> t TPrim{} -> t TLit{} -> t TUnit{} -> t TSort{} -> t TErased{} -> t TError{} -> t TLam b -> TLam (tr b) TApp a bs -> TApp (tr a) (map tr bs) TLet e b -> TLet (tr e) (tr b) caseType t (TACon c _ _ : _) | isZero c = CTNat | isSuc c = CTNat | isPos c = CTInt | isNegSuc c = CTInt caseType t _ = t nPlusKView (TAGuard (TApp (TPrim PGeq) [TVar 0, (TLit (LitNat _ k))]) (TLet (TApp (TPrim PSub) [TVar 0, (TLit (LitNat _ j))]) b)) | k == j = Just (k, b) nPlusKView _ = Nothing Agda-2.5.3/src/full/Agda/Compiler/Treeless/Simplify.hs0000644000000000000000000003427613154613124020634 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Compiler.Treeless.Simplify (simplifyTTerm) where import Control.Arrow (first, second, (***)) import Control.Applicative import Control.Monad.Reader import Control.Monad.Writer import Data.Traversable (traverse) import qualified Data.List as List import Agda.Syntax.Treeless import Agda.Syntax.Internal (Substitution'(..)) import Agda.Syntax.Literal import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Primitive import Agda.TypeChecking.Substitute import Agda.Utils.Maybe import Agda.Compiler.Treeless.Subst import Agda.Compiler.Treeless.Pretty import Agda.Compiler.Treeless.Compare import Agda.Utils.Pretty import Agda.Utils.Impossible #include "undefined.h" data SEnv = SEnv { envSubst :: Substitution' TTerm , envRewrite :: [(TTerm, TTerm)] } type S = Reader SEnv runS :: S a -> a runS m = runReader m $ SEnv IdS [] lookupVar :: Int -> S TTerm lookupVar i = asks $ (`lookupS` i) . envSubst onSubst :: (Substitution' TTerm -> Substitution' TTerm) -> S a -> S a onSubst f = local $ \ env -> env { envSubst = f (envSubst env) } onRewrite :: Substitution' TTerm -> S a -> S a onRewrite rho = local $ \ env -> env { envRewrite = map (applySubst rho *** applySubst rho) (envRewrite env) } addRewrite :: TTerm -> TTerm -> S a -> S a addRewrite lhs rhs = local $ \ env -> env { envRewrite = (lhs, rhs) : envRewrite env } underLams :: Int -> S a -> S a underLams i = onRewrite (raiseS i) . onSubst (liftS i) underLam :: S a -> S a underLam = underLams 1 underLet :: TTerm -> S a -> S a underLet u = onRewrite (raiseS 1) . onSubst (\rho -> wkS 1 $ u :# rho) bindVar :: Int -> TTerm -> S a -> S a bindVar x u = onSubst (inplaceS x u `composeS`) rewrite :: TTerm -> S TTerm rewrite t = do rules <- asks envRewrite case [ rhs | (lhs, rhs) <- rules, equalTerms t lhs ] of rhs : _ -> pure rhs [] -> pure t data FunctionKit = FunctionKit { modAux, divAux, natMinus, true, false :: Maybe QName } simplifyTTerm :: TTerm -> TCM TTerm simplifyTTerm t = do kit <- FunctionKit <$> getBuiltinName builtinNatModSucAux <*> getBuiltinName builtinNatDivSucAux <*> getBuiltinName builtinNatMinus <*> getBuiltinName builtinTrue <*> getBuiltinName builtinFalse return $ runS $ simplify kit t simplify :: FunctionKit -> TTerm -> S TTerm simplify FunctionKit{..} = simpl where simpl = rewrite' >=> unchainCase >=> \ t -> case t of TDef{} -> pure t TPrim{} -> pure t TVar x -> do v <- lookupVar x pure $ if isAtomic v then v else t TApp (TDef f) [TLit (LitNat _ 0), m, n, m'] -- div/mod are equivalent to quot/rem on natural numbers. | m == m', Just f == divAux -> simpl $ tOp PQuot n (tPlusK 1 m) | m == m', Just f == modAux -> simpl $ tOp PRem n (tPlusK 1 m) TApp (TPrim _) _ -> pure t -- taken care of by rewrite' TApp f es -> do f <- simpl f es <- traverse simpl es maybeMinusToPrim f es TLam b -> TLam <$> underLam (simpl b) TLit{} -> pure t TCon{} -> pure t TLet e b -> do e <- simpl e tLet e <$> underLet e (simpl b) TCase x t d bs -> do v <- lookupVar x let (lets, u) = tLetView v case u of -- TODO: also for literals _ | Just (c, as) <- conView u -> simpl $ matchCon lets c as d bs | Just (k, TVar y) <- plusKView u -> simpl . mkLets lets . TCase y t d =<< mapM (matchPlusK y x k) bs TCase y t1 d1 bs1 -> simpl $ mkLets lets $ TCase y t1 (distrDef case1 d1) $ map (distrCase case1) bs1 where -- Γ x Δ -> Γ _ Δ Θ y, where x maps to y and Θ are the lets n = length lets rho = liftS (x + n + 1) (raiseS 1) `composeS` singletonS (x + n + 1) (TVar 0) `composeS` raiseS (n + 1) case1 = applySubst rho (TCase x t d bs) distrDef v d | isUnreachable d = tUnreachable | otherwise = tLet d v distrCase v (TACon c a b) = TACon c a $ TLet b $ raiseFrom 1 a v distrCase v (TALit l b) = TALit l $ TLet b v distrCase v (TAGuard g b) = TAGuard g $ TLet b v _ -> do d <- simpl d bs <- traverse (simplAlt x) bs tCase x t d bs TUnit -> pure t TSort -> pure t TErased -> pure t TError{} -> pure t conView (TCon c) = Just (c, []) conView (TApp (TCon c) as) = Just (c, as) conView e = Nothing -- Collapse chained cases (case x of bs -> vs; _ -> case x of bs' -> vs' ==> -- case x of bs -> vs; bs' -> vs') unchainCase :: TTerm -> S TTerm unchainCase e@(TCase x t d bs) = do let (lets, u) = tLetView d k = length lets return $ case u of TCase y _ d' bs' | x + k == y -> mkLets lets $ TCase y t d' $ raise k bs ++ bs' _ -> e unchainCase e = return e mkLets es b = foldr TLet b es matchCon _ _ _ d [] = d matchCon lets c as d (TALit{} : bs) = matchCon lets c as d bs matchCon lets c as d (TAGuard{} : bs) = matchCon lets c as d bs matchCon lets c as d (TACon c' a b : bs) | c == c' = flip (foldr TLet) lets $ mkLet 0 as (raiseFrom a (length lets) b) | otherwise = matchCon lets c as d bs where mkLet _ [] b = b mkLet i (a : as) b = TLet (raise i a) $ mkLet (i + 1) as b -- Simplify let y = x + k in case y of j -> u; _ | g[y] -> v -- to let y = x + k in case x of j - k -> u; _ | g[x + k] -> v matchPlusK :: Int -> Int -> Integer -> TAlt -> S TAlt matchPlusK x y k (TALit (LitNat r j) b) = return $ TALit (LitNat r (j - k)) b matchPlusK x y k (TAGuard g b) = flip TAGuard b <$> simpl (applySubst (inplaceS y (tPlusK k (TVar x))) g) matchPlusK x y k TACon{} = __IMPOSSIBLE__ matchPlusK x y k TALit{} = __IMPOSSIBLE__ simplPrim (TApp f@TPrim{} args) = do args <- mapM simpl args inlined <- mapM inline args let u = TApp f args v = simplPrim' (TApp f inlined) pure $ if v `betterThan` u then v else u where inline (TVar x) = do v <- lookupVar x if v == TVar x then pure v else inline v inline (TApp f@TPrim{} args) = TApp f <$> mapM inline args inline u@(TLet _ (TCase 0 _ _ _)) = pure u inline (TLet e b) = inline (subst 0 e b) inline u = pure u simplPrim t = pure t simplPrim' :: TTerm -> TTerm simplPrim' (TApp (TPrim PLt) [u, v]) | Just (PAdd, k, u) <- constArithView u, Just (PAdd, j, v) <- constArithView v, k == j = tOp PLt u v simplPrim' (TApp (TPrim op) [u, v]) | elem op [PGeq, PLt, PEqI] , Just (PAdd, k, u) <- constArithView u , Just j <- intView v = TApp (TPrim op) [u, tInt (j - k)] simplPrim' (TApp (TPrim PEqI) [u, v]) | Just (op1, k, u) <- constArithView u, Just (op2, j, v) <- constArithView v, op1 == op2, k == j, elem op1 [PAdd, PSub] = tOp PEqI u v simplPrim' (TApp (TPrim PMul) [u, v]) | Just 0 <- intView u = tInt 0 | Just 0 <- intView v = tInt 0 simplPrim' (TApp (TPrim op) [u, v]) | Just u <- negView u, Just v <- negView v, elem op [PMul, PQuot] = tOp op u v | Just u <- negView u, elem op [PMul, PQuot] = simplArith $ tOp PSub (tInt 0) (tOp op u v) | Just v <- negView v, elem op [PMul, PQuot] = simplArith $ tOp PSub (tInt 0) (tOp op u v) simplPrim' (TApp (TPrim PRem) [u, v]) | Just u <- negView u = simplArith $ tOp PSub (tInt 0) (tOp PRem u (unNeg v)) | Just v <- negView v = tOp PRem u v simplPrim' (TApp f@(TPrim op) [u, v]) = simplArith $ TApp f [simplPrim' u, simplPrim' v] simplPrim' u = u unNeg u | Just v <- negView u = v | otherwise = u negView (TApp (TPrim PSub) [a, b]) | Just 0 <- intView a = Just b negView _ = Nothing -- Count arithmetic operations betterThan u v = operations u <= operations v where operations (TApp (TPrim _) [a, b]) = 1 + operations a + operations b operations TVar{} = 0 operations TLit{} = 0 operations _ = 1000 rewrite' t = rewrite =<< simplPrim t constArithView :: TTerm -> Maybe (TPrim, Integer, TTerm) constArithView (TApp (TPrim op) [TLit (LitNat _ k), u]) | elem op [PAdd, PSub] = Just (op, k, u) constArithView (TApp (TPrim op) [u, TLit (LitNat _ k)]) | op == PAdd = Just (op, k, u) | op == PSub = Just (PAdd, -k, u) constArithView _ = Nothing simplAlt x (TACon c a b) = TACon c a <$> underLams a (maybeAddRewrite (x + a) conTerm $ simpl b) where conTerm = mkTApp (TCon c) [TVar i | i <- reverse $ take a [0..]] simplAlt x (TALit l b) = TALit l <$> maybeAddRewrite x (TLit l) (simpl b) simplAlt x (TAGuard g b) = TAGuard <$> simpl g <*> simpl b -- If x is already bound we add a rewrite, otherwise we bind x to rhs. maybeAddRewrite x rhs cont = do v <- lookupVar x case v of TVar y | x == y -> bindVar x rhs $ cont _ -> addRewrite v rhs cont isTrue (TCon c) = Just c == true isTrue _ = False isFalse (TCon c) = Just c == false isFalse _ = False maybeMinusToPrim f@(TDef minus) es@[a, b] | Just minus == natMinus = do b_a <- rewrite' (tOp PLt b a) b_sa <- rewrite' (tOp PLt b (tOp PAdd (tInt 1) a)) a_b <- rewrite' (tOp PLt a b) if isTrue b_a || isTrue b_sa || isFalse a_b then pure $ tOp PSub a b else tApp f es maybeMinusToPrim f es = tApp f es tLet (TVar x) b = subst 0 (TVar x) b tLet e (TVar 0) = e tLet e b = TLet e b tCase :: Int -> CaseType -> TTerm -> [TAlt] -> S TTerm tCase x t d [] = pure d tCase x t d bs | isUnreachable d = case reverse bs' of [] -> pure d TALit _ b : as -> tCase x t b (reverse as) TAGuard _ b : as -> tCase x t b (reverse as) TACon c a b : _ -> tCase' x t d bs' | otherwise = do d' <- lookupIfVar d case d' of TCase y _ d bs'' | x == y -> tCase x t d (bs' ++ filter noOverlap bs'') _ -> tCase' x t d bs' where bs' = filter (not . isUnreachable) bs lookupIfVar (TVar i) = lookupVar i lookupIfVar t = pure t noOverlap b = not $ any (overlapped b) bs' overlapped (TACon c _ _) (TACon c' _ _) = c == c' overlapped (TALit l _) (TALit l' _) = l == l' overlapped _ _ = False -- | Drop unreachable cases for Nat and Int cases. pruneLitCases :: Int -> CaseType -> TTerm -> [TAlt] -> S TTerm pruneLitCases x CTNat d bs = case complete bs [] Nothing of Just bs' -> tCase x CTNat tUnreachable bs' Nothing -> return $ TCase x CTNat d bs where complete bs small (Just upper) | null $ [0..upper - 1] List.\\ small = Just [] complete (b@(TALit (LitNat _ n) _) : bs) small upper = (b :) <$> complete bs (n : small) upper complete (b@(TAGuard (TApp (TPrim PGeq) [TVar y, TLit (LitNat _ j)]) _) : bs) small upper | x == y = (b :) <$> complete bs small (Just $ maybe j (min j) upper) complete _ _ _ = Nothing pruneLitCases x CTInt d bs = return $ TCase x CTInt d bs -- TODO pruneLitCases x t d bs = return $ TCase x t d bs tCase' x t d [] = return d tCase' x t d bs = pruneLitCases x t d bs tApp :: TTerm -> [TTerm] -> S TTerm tApp (TLet e b) es = TLet e <$> underLet e (tApp b (raise 1 es)) tApp (TCase x t d bs) es = do d <- tApp d es bs <- mapM (`tAppAlt` es) bs simpl $ TCase x t d bs -- will resimplify branches tApp (TVar x) es = do v <- lookupVar x case v of _ | v /= TVar x && isAtomic v -> tApp v es TLam{} -> tApp v es -- could blow up the code _ -> pure $ mkTApp (TVar x) es tApp f [] = pure f tApp (TLam b) (TVar i : es) = tApp (subst 0 (TVar i) b) es tApp (TLam b) (e : es) = tApp (TLet e b) es tApp f es = pure $ TApp f es tAppAlt (TACon c a b) es = TACon c a <$> underLams a (tApp b (raise a es)) tAppAlt (TALit l b) es = TALit l <$> tApp b es tAppAlt (TAGuard g b) es = TAGuard g <$> tApp b es isAtomic v = case v of TVar{} -> True TCon{} -> True TPrim{} -> True TDef{} -> True TLit{} -> True TSort{} -> True TErased{} -> True TError{} -> True _ -> False type Arith = (Integer, [Atom]) data Atom = Pos TTerm | Neg TTerm deriving (Show, Eq, Ord) aNeg :: Atom -> Atom aNeg (Pos a) = Neg a aNeg (Neg a) = Pos a aCancel :: [Atom] -> [Atom] aCancel (a : as) | elem (aNeg a) as = aCancel (List.delete (aNeg a) as) | otherwise = a : aCancel as aCancel [] = [] sortR :: Ord a => [a] -> [a] sortR = List.sortBy (flip compare) aAdd :: Arith -> Arith -> Arith aAdd (a, xs) (b, ys) = (a + b, aCancel $ sortR $ xs ++ ys) aSub :: Arith -> Arith -> Arith aSub (a, xs) (b, ys) = (a - b, aCancel $ sortR $ xs ++ map aNeg ys) fromArith :: Arith -> TTerm fromArith (n, []) = tInt n fromArith (0, xs) | (ys, Pos a : zs) <- break isPos xs = foldl addAtom a (ys ++ zs) fromArith (n, xs) | n < 0, (ys, Pos a : zs) <- break isPos xs = tOp PSub (foldl addAtom a (ys ++ zs)) (tInt (-n)) fromArith (n, xs) = foldl addAtom (tInt n) xs isPos :: Atom -> Bool isPos Pos{} = True isPos Neg{} = False addAtom :: TTerm -> Atom -> TTerm addAtom t (Pos a) = tOp PAdd t a addAtom t (Neg a) = tOp PSub t a toArith :: TTerm -> Arith toArith t | Just n <- intView t = (n, []) toArith (TApp (TPrim PAdd) [a, b]) = aAdd (toArith a) (toArith b) toArith (TApp (TPrim PSub) [a, b]) = aSub (toArith a) (toArith b) toArith t = (0, [Pos t]) simplArith :: TTerm -> TTerm simplArith = fromArith . toArith Agda-2.5.3/src/full/Agda/Compiler/Treeless/Uncase.hs0000644000000000000000000000426713154613124020253 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Compiler.Treeless.Uncase (caseToSeq) where import Control.Applicative import Data.Monoid import Agda.Syntax.Treeless import Agda.Syntax.Literal import Agda.TypeChecking.Substitute import Agda.Compiler.Treeless.Subst import Agda.Compiler.Treeless.Compare import Agda.Utils.Impossible #include "undefined.h" caseToSeq :: Monad m => TTerm -> m TTerm caseToSeq t = return $ uncase t uncase :: TTerm -> TTerm uncase t = case t of TVar{} -> t TPrim{} -> t TDef{} -> t TApp f es -> tApp (uncase f) (map uncase es) TLam b -> TLam $ uncase b TLit{} -> t TCon{} -> t TLet e b -> tLet (uncase e) (uncase b) TCase x t d bs -> doCase x t (uncase d) (map uncaseAlt bs) TUnit{} -> t TSort{} -> t TErased{} -> t TError{} -> t where uncaseAlt (TACon c a b) = TACon c a $ uncase b uncaseAlt (TALit l b) = TALit l $ uncase b uncaseAlt (TAGuard g b) = TAGuard (uncase g) (uncase b) doCase x t d bs | fv > 0 = fallback -- can't do it for constructors with arguments | all (equalTo x u) bs = tApp (TPrim PSeq) [TVar x, u] | otherwise = fallback where fallback = TCase x t d bs (fv, u) | isUnreachable d = case last bs of TACon _ a b -> (a, b) TALit l b -> (0, b) TAGuard _ b -> (0, b) | otherwise = (0, d) equalTo :: Int -> TTerm -> TAlt -> Bool equalTo x t (TACon c a b) = a == 0 && equalTerms (subst x (TCon c) t) (subst x (TCon c) b) equalTo x t (TALit l b) = equalTerms (subst x (TLit l) t) (subst x (TLit l) b) equalTo x t (TAGuard _ b) = equalTerms t b -- There's no sense binding an expression just to seq on it. tLet e b = case occursIn 0 b of Occurs 0 _ _ -> strengthen __IMPOSSIBLE__ b Occurs _ _ (SeqArg (All True)) -> subst 0 TErased b -- this will get rid of the seq _ -> TLet e b -- Primitive operations are already strict tApp (TPrim PSeq) [_, b@(TApp (TPrim op) _)] | op `elem` [PAdd, PSub, PMul, PLt, PGeq, PRem, PQuot] || isPrimEq op = b tApp f es = TApp f es Agda-2.5.3/src/full/Agda/Compiler/Treeless/EliminateDefaults.hs0000644000000000000000000000363513154613124022432 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Eliminates case defaults by adding an alternative for all possible -- constructors. Literal cases are preserved as-is. module Agda.Compiler.Treeless.EliminateDefaults where import Control.Applicative import Control.Monad import qualified Data.List as List import Data.Maybe import Agda.Syntax.Abstract.Name (QName) import Agda.Syntax.Treeless import Agda.Syntax.Literal import qualified Agda.Syntax.Internal as I import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Primitive import Agda.TypeChecking.Substitute import Agda.Compiler.Treeless.Subst import Agda.Utils.Impossible #include "undefined.h" eliminateCaseDefaults :: TTerm -> TCM TTerm eliminateCaseDefaults = tr where tr :: TTerm -> TCM TTerm tr t = case t of TCase sc ct@(CTData qn) def alts | not (isUnreachable def) -> do dtCons <- defConstructors . theDef <$> getConstInfo qn let missingCons = dtCons List.\\ map aCon alts def <- tr def newAlts <- forM missingCons $ \con -> do Constructor {conArity = ar} <- theDef <$> getConstInfo con return $ TACon con ar (TVar ar) alts' <- (++ newAlts) <$> mapM (trAlt . raise 1) alts return $ TLet def $ TCase (sc + 1) ct tUnreachable alts' TCase sc ct def alts -> TCase sc ct <$> tr def <*> mapM trAlt alts TVar{} -> tt TDef{} -> tt TCon{} -> tt TPrim{} -> tt TLit{} -> tt TUnit{} -> tt TSort{} -> tt TErased{} -> tt TError{} -> tt TLam b -> TLam <$> tr b TApp a bs -> TApp <$> tr a <*> mapM tr bs TLet e b -> TLet <$> tr e <*> tr b where tt = return t trAlt :: TAlt -> TCM TAlt trAlt a = case a of TAGuard g b -> TAGuard <$> tr g <*> tr b TACon q a b -> TACon q a <$> tr b TALit l b -> TALit l <$> tr b Agda-2.5.3/src/full/Agda/Compiler/MAlonzo/0000755000000000000000000000000013154613124016261 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Compiler/MAlonzo/HaskellTypes.hs0000644000000000000000000001276513154613124021240 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Translating Agda types to Haskell types. Used to ensure that imported -- Haskell functions have the right type. module Agda.Compiler.MAlonzo.HaskellTypes where import Control.Applicative import Data.Maybe (fromMaybe) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Pretty import Agda.TypeChecking.Primitive (getBuiltinName) import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Free import Agda.Compiler.MAlonzo.Pragmas import Agda.Compiler.MAlonzo.Misc import Agda.Compiler.MAlonzo.Pretty import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.Pretty (prettyShow) #include "undefined.h" import Agda.Utils.Impossible type HaskellKind = String hsStar :: HaskellKind hsStar = "*" hsKFun :: HaskellKind -> HaskellKind -> HaskellKind hsKFun k l = "(" ++ k ++ " -> " ++ l ++ ")" hsFun :: HaskellKind -> HaskellKind -> HaskellKind hsFun a b = "(" ++ a ++ " -> " ++ b ++ ")" hsUnit :: HaskellType hsUnit = "()" hsVar :: Name -> HaskellType hsVar x = "x" ++ concatMap encode (prettyShow x) where okChars = ['a'..'z'] ++ ['A'..'Y'] ++ "_'" encode 'Z' = "ZZ" encode c | c `elem` okChars = [c] | otherwise = "Z" ++ show (fromEnum c) hsApp :: String -> [HaskellType] -> HaskellType hsApp d [] = d hsApp d as = "(" ++ unwords (d : as) ++ ")" hsForall :: String -> HaskellType -> HaskellType hsForall x a = "(forall " ++ x ++ ". " ++ a ++ ")" notAHaskellType :: Type -> TCM a notAHaskellType a = typeError . GenericDocError =<< do fsep $ pwords "The type" ++ [prettyTCM a] ++ pwords "cannot be translated to a Haskell type." getHsType :: QName -> TCM HaskellType getHsType x = do d <- getHaskellPragma x let namedType = do -- For these builtin types, the type name (xhqn ...) refers to the -- generated, but unused, datatype and not the primitive type. nat <- getBuiltinName builtinNat int <- getBuiltinName builtinInteger bool <- getBuiltinName builtinBool case () of _ | Just x `elem` [nat, int] -> return "Integer" | Just x == bool -> return "Bool" | otherwise -> prettyShow <$> xhqn "T" x setCurrentRange d $ case d of Just HsDefn{} -> return hsUnit Just HsType{} -> namedType Just HsData{} -> namedType _ -> notAHaskellType (El Prop $ Def x []) getHsVar :: Nat -> TCM HaskellCode getHsVar i = hsVar <$> nameOfBV i -- | Note that @Inf a b@, where @Inf@ is the INFINITY builtin, is -- translated to @@ (assuming that all coinductive -- builtins are defined). -- -- Note that if @haskellType@ supported universe polymorphism then the -- special treatment of INFINITY might not be needed. haskellType' :: Type -> TCM HaskellType haskellType' t = fromType t where err = notAHaskellType t fromArgs = mapM (fromTerm . unArg) fromType = fromTerm . unEl fromTerm v = do v <- unSpine <$> reduce v reportSLn "compile.haskell.type" 50 $ "toHaskellType " ++ show v kit <- liftTCM coinductionKit case v of Var x es -> do let args = fromMaybe __IMPOSSIBLE__ $ allApplyElims es hsApp <$> getHsVar x <*> fromArgs args Def d es | Just d == (nameOfInf <$> kit) -> case es of [Apply a, Apply b] -> fromTerm (unArg b) _ -> err Def d es -> do let args = fromMaybe __IMPOSSIBLE__ $ allApplyElims es hsApp <$> getHsType d <*> fromArgs args Pi a b -> if isBinderUsed b -- Andreas, 2012-04-03. Q: could we rely on Abs/NoAbs instead of again checking freeness of variable? then do hsA <- fromType (unDom a) underAbstraction a b $ \b -> hsForall <$> getHsVar 0 <*> (hsFun hsA <$> fromType b) else hsFun <$> fromType (unDom a) <*> fromType (noabsApp __IMPOSSIBLE__ b) Con c ci args -> hsApp <$> getHsType (conName c) <*> fromArgs args Lam{} -> err Level{} -> return hsUnit Lit{} -> return hsUnit Sort{} -> return hsUnit Shared p -> fromTerm $ derefPtr p MetaV{} -> err DontCare{} -> err haskellType :: QName -> TCM HaskellType haskellType q = do def <- getConstInfo q let np = case theDef def of Constructor{ conPars = np } -> np _ -> 0 underPars 0 a = haskellType' a underPars n a = do a <- reduce a case unEl a of Pi a (NoAbs _ b) -> underPars (n - 1) b Pi a b -> underAbstraction a b $ \b -> hsForall <$> getHsVar 0 <*> underPars (n - 1) b _ -> __IMPOSSIBLE__ ty <- underPars np $ defType def reportSLn "tc.pragma.compile" 10 $ "Haskell type for " ++ prettyShow q ++ ": " ++ ty return ty checkConstructorCount :: QName -> [QName] -> [HaskellCode] -> TCM () checkConstructorCount d cs hsCons | n == hn = return () | otherwise = do let n_forms_are = case hn of 1 -> "1 Haskell constructor is" n -> show n ++ " Haskell constructors are" only | hn == 0 = "" | hn < n = "only " | otherwise = "" genericDocError =<< fsep ([prettyTCM d] ++ pwords ("has " ++ show n ++ " constructors, but " ++ only ++ n_forms_are ++ " given [" ++ unwords hsCons ++ "]")) where n = length cs hn = length hsCons Agda-2.5.3/src/full/Agda/Compiler/MAlonzo/Pretty.hs0000644000000000000000000001637713154613124020122 0ustar0000000000000000{-# LANGUAGE CPP #-} ------------------------------------------------------------------------ -- Pretty-printing of Haskell modules ------------------------------------------------------------------------ module Agda.Compiler.MAlonzo.Pretty where import Data.Generics.Geniplate import qualified Agda.Utils.Haskell.Syntax as HS import Text.PrettyPrint (empty) import Agda.Compiler.MAlonzo.Encode import Agda.Utils.Pretty import Agda.Utils.Impossible #include "undefined.h" prettyPrint :: Pretty a => a -> String prettyPrint = show . pretty instance Pretty HS.Module where pretty (HS.Module m pragmas imps decls) = vcat [ vcat $ map pretty pragmas , text "module" <+> pretty m <+> text "where" , text "" , vcat $ map pretty imps , text "" , vcat $ map pretty decls ] instance Pretty HS.ModulePragma where pretty (HS.LanguagePragma ps) = text "{-#" <+> text "LANGUAGE" <+> fsep (punctuate comma $ map pretty ps) <+> text "#-}" pretty (HS.OtherPragma p) = text p instance Pretty HS.ImportDecl where pretty HS.ImportDecl{ HS.importModule = m , HS.importQualified = q , HS.importSpecs = specs } = hsep [ text "import" , if q then text "qualified" else empty , pretty m , maybe empty prSpecs specs ] where prSpecs (hide, specs) = hsep [ if hide then text "hiding" else empty , parens $ fsep $ punctuate comma $ map pretty specs ] instance Pretty HS.ImportSpec where pretty (HS.IVar x) = pretty x instance Pretty HS.Decl where pretty d = case d of HS.TypeDecl f xs t -> sep [ text "type" <+> pretty f <+> fsep (map pretty xs) <+> text "=" , nest 2 $ pretty t ] HS.DataDecl newt d xs cons derv -> sep [ pretty newt <+> pretty d <+> fsep (map pretty xs) , nest 2 $ if null cons then empty else text "=" <+> fsep (punctuate (text " |") $ map pretty cons) , nest 2 $ prDeriving derv ] where prDeriving [] = empty prDeriving ds = text "deriving" <+> parens (fsep $ punctuate comma $ map prDer ds) prDer (d, ts) = pretty (foldl HS.TyApp (HS.TyCon d) ts) HS.TypeSig fs t -> sep [ hsep (punctuate comma (map pretty fs)) <+> text "::" , nest 2 $ pretty t ] HS.FunBind ms -> vcat $ map pretty ms HS.PatSyn p1 p2 -> sep [ text "pattern" <+> pretty p1 <+> text "=" <+> pretty p2 ] HS.FakeDecl s -> text s instance Pretty HS.ConDecl where pretty (HS.ConDecl c ts) = pretty c <+> fsep (map (prettyPrec 10) ts) instance Pretty HS.Match where pretty (HS.Match f ps rhs wh) = prettyWhere wh $ sep [ pretty f <+> fsep (map (prettyPrec 10) ps) , nest 2 $ prettyRhs "=" rhs ] prettyWhere :: Maybe HS.Binds -> Doc -> Doc prettyWhere Nothing doc = doc prettyWhere (Just b) doc = vcat [ doc, nest 2 $ sep [ text "where", nest 2 $ pretty b ] ] instance Pretty HS.Pat where prettyPrec pr pat = case pat of HS.PVar x -> pretty x HS.PLit l -> pretty l HS.PAsPat x p -> mparens (pr > 10) $ pretty x <> text "@" <> prettyPrec 11 p HS.PWildCard -> text "_" HS.PBangPat p -> text "!" <> prettyPrec 11 p HS.PApp c ps -> mparens (pr > 9) $ pretty c <+> hsep (map (prettyPrec 10) ps) HS.PatTypeSig p t -> mparens (pr > 0) $ sep [ pretty p <+> text "::", nest 2 $ pretty t ] HS.PIrrPat p -> mparens (pr > 10) $ text "~" <> prettyPrec 11 p prettyRhs :: String -> HS.Rhs -> Doc prettyRhs eq (HS.UnGuardedRhs e) = text eq <+> pretty e prettyRhs eq (HS.GuardedRhss rhss) = vcat $ map (prettyGuardedRhs eq) rhss prettyGuardedRhs :: String -> HS.GuardedRhs -> Doc prettyGuardedRhs eq (HS.GuardedRhs ss e) = sep [ text "|" <+> sep (punctuate comma $ map pretty ss) <+> text eq , nest 2 $ pretty e ] instance Pretty HS.Binds where pretty (HS.BDecls ds) = vcat $ map pretty ds instance Pretty HS.DataOrNew where pretty HS.DataType = text "data" pretty HS.NewType = text "newtype" instance Pretty HS.TyVarBind where pretty (HS.UnkindedVar x) = pretty x instance Pretty HS.Type where prettyPrec pr t = case t of HS.TyForall xs t -> mparens (pr > 0) $ sep [ text "forall" <+> fsep (map pretty xs) <> text "." , nest 2 $ pretty t ] HS.TyFun a b -> mparens (pr > 4) $ sep [ prettyPrec 5 a <+> text "->", prettyPrec 4 b ] HS.TyCon c -> pretty c HS.TyVar x -> pretty x t@HS.TyApp{} -> sep [ prettyPrec 9 f , nest 2 $ fsep $ map (prettyPrec 10) ts ] where f : ts = appView t [] appView (HS.TyApp a b) as = appView a (b : as) appView t as = t : as HS.FakeType s -> text s instance Pretty HS.Stmt where pretty (HS.Qualifier e) = pretty e pretty (HS.Generator p e) = sep [ pretty p <+> text "<-", nest 2 $ pretty e ] instance Pretty HS.Literal where pretty (HS.Int n) = integer n pretty (HS.Frac x) = double (fromRational x) pretty (HS.Char c) = text (show c) pretty (HS.String s) = text (show s) instance Pretty HS.Exp where prettyPrec pr e = case e of HS.Var x -> pretty x HS.Con c -> pretty c HS.Lit l -> pretty l HS.InfixApp a qop b -> mparens (pr > 0) $ sep [ prettyPrec 1 a , pretty qop <+> prettyPrec 1 b ] HS.App{} -> mparens (pr > 9) $ sep [ prettyPrec 9 f , nest 2 $ fsep $ map (prettyPrec 10) es ] where f : es = appView e [] appView (HS.App f e) es = appView f (e : es) appView f es = f : es HS.Lambda ps e -> mparens (pr > 0) $ sep [ text "\\" <+> fsep (map (prettyPrec 10) ps) <+> text "->" , nest 2 $ pretty e ] HS.Let bs e -> mparens (pr > 0) $ sep [ text "let" <+> pretty bs <+> text "in" , pretty e ] HS.If a b c -> mparens (pr > 0) $ sep [ text "if" <+> pretty a , nest 2 $ text "then" <+> pretty b , nest 2 $ text "else" <+> prettyPrec 1 c ] HS.Case e bs -> mparens (pr > 0) $ vcat [ text "case" <+> pretty e <+> text "of" , nest 2 $ vcat $ map pretty bs ] HS.ExpTypeSig e t -> mparens (pr > 0) $ sep [ pretty e <+> text "::" , nest 2 $ pretty t ] HS.NegApp exp -> parens $ text "-" <> pretty exp HS.FakeExp s -> text s instance Pretty HS.Alt where pretty (HS.Alt pat rhs wh) = prettyWhere wh $ sep [ pretty pat, nest 2 $ prettyRhs "->" rhs ] instance Pretty HS.ModuleName where pretty m = text s where HS.ModuleName s = encodeModuleName m instance Pretty HS.QName where pretty q = mparens (isOperator q) (prettyQName q) instance Pretty HS.Name where pretty (HS.Ident s) = text s pretty (HS.Symbol s) = text s instance Pretty HS.QOp where pretty (HS.QVarOp x) | isOperator x = prettyQName x | otherwise = text "`" <> prettyQName x <> text "`" isOperator :: HS.QName -> Bool isOperator q = case q of HS.Qual _ x -> isOp x HS.UnQual x -> isOp x where isOp HS.Symbol{} = True isOp HS.Ident{} = False prettyQName :: HS.QName -> Doc prettyQName (HS.Qual m x) = pretty m <> text "." <> pretty x prettyQName (HS.UnQual x) = pretty x Agda-2.5.3/src/full/Agda/Compiler/MAlonzo/Primitives.hs0000644000000000000000000002432713154613124020760 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Compiler.MAlonzo.Primitives where import Control.Monad.State import Data.Char import qualified Data.List as List import Data.Map as M import Data.Maybe import Agda.Compiler.Common import Agda.Compiler.ToTreeless import {-# SOURCE #-} Agda.Compiler.MAlonzo.Compiler (closedTerm) import Agda.Compiler.MAlonzo.Misc import Agda.Compiler.MAlonzo.Pretty import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Treeless import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Primitive import Agda.TypeChecking.Reduce import Agda.TypeChecking.Pretty import Agda.Utils.Either import Agda.Utils.Except import Agda.Utils.Lens import Agda.Utils.Monad import qualified Agda.Utils.HashMap as HMap import qualified Agda.Utils.Haskell.Syntax as HS #include "undefined.h" import Agda.Utils.Impossible isMainFunction :: QName -> Bool isMainFunction q = "main" == show (nameConcrete $ qnameName q) hasMainFunction :: Interface -> IsMain hasMainFunction i | List.any isMainFunction names = IsMain | otherwise = NotMain where names = HMap.keys $ iSignature i ^. sigDefinitions -- | Check that the main function has type IO a, for some a. checkTypeOfMain :: QName -> Type -> TCM [HS.Decl] -> TCM [HS.Decl] checkTypeOfMain q ty ret | not (isMainFunction q) = ret | otherwise = do Def io _ <- ignoreSharing <$> primIO ty <- normalise ty case ignoreSharing $ unEl ty of Def d _ | d == io -> (mainAlias :) <$> ret _ -> do err <- fsep $ pwords "The type of main should be" ++ [prettyTCM io] ++ pwords " A, for some A. The given type is" ++ [prettyTCM ty] typeError $ GenericError $ show err where mainAlias = HS.FunBind [HS.Match mainLHS [] mainRHS emptyBinds ] mainLHS = HS.Ident "main" mainRHS = HS.UnGuardedRhs $ HS.Var $ HS.UnQual $ unqhname "d" q treelessPrimName :: TPrim -> String treelessPrimName p = case p of PQuot -> "quotInt" PRem -> "remInt" PSub -> "subInt" PAdd -> "addInt" PMul -> "mulInt" PGeq -> "geqInt" PLt -> "ltInt" PEqI -> "eqInt" PEqF -> "eqFloat" -- MAlonzo uses literal patterns, so we don't need equality for the other primitive types PEqC -> __IMPOSSIBLE__ PEqS -> __IMPOSSIBLE__ PEqQ -> __IMPOSSIBLE__ PSeq -> "seq" -- primitives only used by GuardsToPrims transformation, which MAlonzo doesn't use PIf -> __IMPOSSIBLE__ -- | Haskell modules to be imported for BUILT-INs importsForPrim :: TCM [HS.ModuleName] importsForPrim = fmap (++ [HS.ModuleName "Data.Text"]) $ xForPrim $ List.map (\(s, ms) -> (s, return (List.map HS.ModuleName ms))) $ [ "CHAR" |-> ["Data.Char"] , "primIsAlpha" |-> ["Data.Char"] , "primIsAscii" |-> ["Data.Char"] , "primIsDigit" |-> ["Data.Char"] , "primIsHexDigit" |-> ["Data.Char"] , "primIsLatin1" |-> ["Data.Char"] , "primIsLower" |-> ["Data.Char"] , "primIsPrint" |-> ["Data.Char"] , "primIsSpace" |-> ["Data.Char"] , "primToLower" |-> ["Data.Char"] , "primToUpper" |-> ["Data.Char"] ] where (|->) = (,) -------------- xForPrim :: [(String, TCM [a])] -> TCM [a] xForPrim table = do qs <- HMap.keys <$> curDefs bs <- toList <$> gets stBuiltinThings let getName (Builtin (Def q _)) = q getName (Builtin (Con q _ _)) = conName q getName (Builtin (Shared p)) = getName (Builtin $ derefPtr p) getName (Builtin (Lam _ b)) = getName (Builtin $ unAbs b) getName (Builtin _) = __IMPOSSIBLE__ getName (Prim (PrimFun q _ _)) = q concat <$> sequence [ fromMaybe (return []) $ List.lookup s table | (s, def) <- bs, getName def `elem` qs ] -- | Definition bodies for primitive functions primBody :: String -> TCM HS.Exp primBody s = maybe unimplemented (fromRight (hsVarUQ . HS.Ident) <$>) $ List.lookup s $ [ -- Integer functions "primIntegerPlus" |-> binAsis "(+)" "Integer" , "primIntegerMinus" |-> binAsis "(-)" "Integer" , "primIntegerTimes" |-> binAsis "(*)" "Integer" , "primIntegerDiv" |-> binAsis "div" "Integer" , "primIntegerMod" |-> binAsis "mod" "Integer" , "primIntegerEquality"|-> rel "(==)" "Integer" , "primIntegerLess" |-> rel "(<)" "Integer" , "primIntegerAbs" |-> return "(abs :: Integer -> Integer)" , "primNatToInteger" |-> return "(id :: Integer -> Integer)" , "primShowInteger" |-> return "(Data.Text.pack . show :: Integer -> Data.Text.Text)" -- Levels , "primLevelZero" |-> return "()" , "primLevelSuc" |-> return "(\\ _ -> ())" , "primLevelMax" |-> return "(\\ _ _ -> ())" -- Natural number functions , "primNatPlus" |-> binNat "(+)" , "primNatMinus" |-> binNat "(\\ x y -> max 0 (x - y))" , "primNatTimes" |-> binNat "(*)" , "primNatDivSucAux" |-> binNat4 "(\\ k m n j -> k + div (max 0 $ n + m - j) (m + 1))" , "primNatModSucAux" |-> binNat4 "(\\ k m n j -> if n > j then mod (n - j - 1) (m + 1) else (k + n))" , "primNatEquality" |-> relNat "(==)" , "primNatLess" |-> relNat "(<)" -- Floating point functions , "primNatToFloat" |-> return "(fromIntegral :: Integer -> Double)" , "primFloatPlus" |-> return "((+) :: Double -> Double -> Double)" , "primFloatMinus" |-> return "((-) :: Double -> Double -> Double)" , "primFloatTimes" |-> return "((*) :: Double -> Double -> Double)" , "primFloatNegate" |-> return "(negate :: Double -> Double)" , "primFloatDiv" |-> return "((/) :: Double -> Double -> Double)" -- ASR (2016-09-14). We use bitwise equality for comparing Double -- because Haskell's Eq, which equates 0.0 and -0.0, allows to prove -- a contradiction (see Issue #2169). , "primFloatEquality" |-> return "MAlonzo.RTE.eqFloat" , "primFloatNumericalEquality" |-> return "MAlonzo.RTE.eqNumFloat" , "primFloatNumericalLess" |-> return "MAlonzo.RTE.ltNumFloat" , "primFloatSqrt" |-> return "(sqrt :: Double -> Double)" , "primRound" |-> return "(round :: Double -> Integer)" , "primFloor" |-> return "(floor :: Double -> Integer)" , "primCeiling" |-> return "(ceiling :: Double -> Integer)" , "primExp" |-> return "(exp :: Double -> Double)" , "primLog" |-> return "(log :: Double -> Double)" , "primSin" |-> return "(sin :: Double -> Double)" , "primCos" |-> return "(cos :: Double -> Double)" , "primTan" |-> return "(tan :: Double -> Double)" , "primASin" |-> return "(asin :: Double -> Double)" , "primACos" |-> return "(acos :: Double -> Double)" , "primATan" |-> return "(atan :: Double -> Double)" , "primATan2" |-> return "(atan2 :: Double -> Double -> Double)" , "primShowFloat" |-> return "(Data.Text.pack . show :: Double -> Data.Text.Text)" -- Character functions , "primCharEquality" |-> rel "(==)" "Char" , "primIsLower" |-> return "Data.Char.isLower" , "primIsDigit" |-> return "Data.Char.isDigit" , "primIsAlpha" |-> return "Data.Char.isAlpha" , "primIsSpace" |-> return "Data.Char.isSpace" , "primIsAscii" |-> return "Data.Char.isAscii" , "primIsLatin1" |-> return "Data.Char.isLatin1" , "primIsPrint" |-> return "Data.Char.isPrint" , "primIsHexDigit" |-> return "Data.Char.isHexDigit" , "primToUpper" |-> return "Data.Char.toUpper" , "primToLower" |-> return "Data.Char.toLower" , "primCharToNat" |-> return "(fromIntegral . fromEnum :: Char -> Integer)" , "primNatToChar" |-> return "(toEnum . fromIntegral :: Integer -> Char)" , "primShowChar" |-> return "(Data.Text.pack . show :: Char -> Data.Text.Text)" -- String functions , "primStringToList" |-> return "Data.Text.unpack" , "primStringFromList" |-> return "Data.Text.pack" , "primStringAppend" |-> binAsis "Data.Text.append" "Data.Text.Text" , "primStringEquality" |-> rel "(==)" "Data.Text.Text" , "primShowString" |-> return "(Data.Text.pack . show :: Data.Text.Text -> Data.Text.Text)" -- Reflection , "primQNameEquality" |-> rel "(==)" "MAlonzo.RTE.QName" , "primQNameLess" |-> rel "(<)" "MAlonzo.RTE.QName" , "primShowQName" |-> return "Data.Text.pack . MAlonzo.RTE.qnameString" , "primQNameFixity" |-> return "MAlonzo.RTE.qnameFixity" , "primMetaEquality" |-> rel "(==)" "Integer" , "primMetaLess" |-> rel "(<)" "Integer" , "primShowMeta" |-> return "\\ x -> Data.Text.pack (\"_\" ++ show (x :: Integer))" -- Seq , "primForce" |-> return "\\ _ _ _ _ x f -> f $! x" , "primForceLemma" |-> return "erased" -- Trust me , ("primTrustMe" , Right <$> do refl <- primRefl closedTerm =<< (closedTermToTreeless $ lam "a" (lam "A" (lam "x" (lam "y" refl))))) ] where x |-> s = (x, Left <$> s) bin blt op ty from to = do from' <- bltQual' blt from to' <- bltQual' blt to return $ repl [op, opty ty, from', to'] $ "\\ x y -> <<3>> ((<<0>> :: <<1>>) (<<2>> x) (<<2>> y))" binNat op = return $ repl [op] "(<<0>> :: Integer -> Integer -> Integer)" binNat4 op = return $ repl [op] "(<<0>> :: Integer -> Integer -> Integer -> Integer -> Integer)" binAsis op ty = return $ repl [op, opty ty] $ "((<<0>>) :: <<1>>)" rel' toTy op ty = do return $ repl [op, ty, toTy] $ "(\\ x y -> (<<0>> :: <<1>> -> <<1>> -> Bool) (<<2>> x) (<<2>> y))" relNat op = do return $ repl [op] $ "(<<0>> :: Integer -> Integer -> Bool)" rel op ty = rel' "" op ty opty t = t ++ "->" ++ t ++ "->" ++ t unimplemented = typeError $ NotImplemented s lam x t = Lam (setHiding Hidden defaultArgInfo) (Abs x t) noCheckCover :: QName -> TCM Bool noCheckCover q = (||) <$> isBuiltin q builtinNat <*> isBuiltin q builtinInteger ---------------------- pconName :: String -> TCM String pconName s = toS . ignoreSharing =<< getBuiltin s where toS (Con q _ _) = prettyPrint <$> conhqn (conName q) toS (Lam _ t) = toS (unAbs t) toS _ = mazerror $ "pconName" ++ s bltQual' :: String -> String -> TCM String bltQual' b s = prettyPrint <$> bltQual b s Agda-2.5.3/src/full/Agda/Compiler/MAlonzo/Compiler.hs-boot0000644000000000000000000000031313154613124021325 0ustar0000000000000000module Agda.Compiler.MAlonzo.Compiler where import qualified Agda.Utils.Haskell.Syntax as HS import Agda.Syntax.Treeless (TTerm) import Agda.TypeChecking.Monad (TCM) closedTerm :: TTerm -> TCM HS.Exp Agda-2.5.3/src/full/Agda/Compiler/MAlonzo/Misc.hs0000644000000000000000000001535413154613124017520 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Compiler.MAlonzo.Misc where import Control.Monad.State (gets) import Data.Char import qualified Data.List as List import Data.Map as Map import Data.Set as Set import Data.Function import qualified Agda.Utils.Haskell.Syntax as HS import Agda.Compiler.Common import Agda.Compiler.MAlonzo.Pragmas import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.Utils.Lens import Agda.Utils.Monad import Agda.Utils.Pretty import Agda.Utils.Maybe #include "undefined.h" import Agda.Utils.Impossible -------------------------------------------------- -- Setting up Interface before compile -------------------------------------------------- curHsMod :: TCM HS.ModuleName curHsMod = mazMod <$> curMName -------------------------------------------------- -- utilities for haskell names -------------------------------------------------- -- The following naming scheme seems to be used: -- -- * Types coming from Agda are named "T\". -- -- * Other definitions coming from Agda are named "d\". -- Exception: the main function is named "main". -- -- * Names coming from Haskell must always be used qualified. -- Exception: names from the Prelude. ihname :: String -> Nat -> HS.Name ihname s i = HS.Ident $ s ++ show i unqhname :: String -> QName -> HS.Name {- NOT A GOOD IDEA, see Issue 728 unqhname s q | ("d", "main") == (s, show(qnameName q)) = HS.Ident "main" | otherwise = ihname s (idnum $ nameId $ qnameName $ q) -} unqhname s q = ihname s (idnum $ nameId $ qnameName $ q) where idnum (NameId x _) = fromIntegral x -- the toplevel module containing the given one tlmodOf :: ModuleName -> TCM HS.ModuleName tlmodOf = fmap mazMod . topLevelModuleName -- qualify HS.Name n by the module of QName q, if necessary; -- accumulates the used module in stImportedModules at the same time. xqual :: QName -> HS.Name -> TCM HS.QName xqual q n = do m1 <- topLevelModuleName (qnameModule q) m2 <- curMName if m1 == m2 then return (HS.UnQual n) else addImport m1 >> return (HS.Qual (mazMod m1) n) xhqn :: String -> QName -> TCM HS.QName xhqn s q = xqual q (unqhname s q) hsName :: String -> HS.QName hsName s = HS.UnQual (HS.Ident s) -- always use the original name for a constructor even when it's redefined. conhqn :: QName -> TCM HS.QName conhqn q = do cq <- canonicalName q def <- getConstInfo cq xhqn "C" cq -- qualify name s by the module of builtin b bltQual :: String -> String -> TCM HS.QName bltQual b s = do Def q _ <- ignoreSharing <$> getBuiltin b xqual q (HS.Ident s) dname :: QName -> HS.Name dname q = unqhname "d" q -- | Name for definition stripped of unused arguments duname :: QName -> HS.Name duname q = unqhname "du" q hsPrimOp :: String -> HS.QOp hsPrimOp s = HS.QVarOp $ HS.UnQual $ HS.Symbol s hsPrimOpApp :: String -> HS.Exp -> HS.Exp -> HS.Exp hsPrimOpApp op e e1 = HS.InfixApp e (hsPrimOp op) e1 hsInt :: Integer -> HS.Exp hsInt n = HS.Lit (HS.Int n) hsTypedInt :: Integral a => a -> HS.Exp hsTypedInt n = HS.ExpTypeSig (HS.Lit (HS.Int $ fromIntegral n)) (HS.TyCon (hsName "Integer")) hsLet :: HS.Name -> HS.Exp -> HS.Exp -> HS.Exp hsLet x e b = HS.Let (HS.BDecls [HS.FunBind [HS.Match x [] (HS.UnGuardedRhs e) emptyBinds]]) b hsVarUQ :: HS.Name -> HS.Exp hsVarUQ = HS.Var . HS.UnQual hsAppView :: HS.Exp -> [HS.Exp] hsAppView = reverse . view where view (HS.App e e1) = e1 : view e view (HS.InfixApp e1 op e2) = [e2, e1, hsOpToExp op] view e = [e] hsOpToExp :: HS.QOp -> HS.Exp hsOpToExp (HS.QVarOp x) = HS.Var x hsLambda :: [HS.Pat] -> HS.Exp -> HS.Exp hsLambda ps (HS.Lambda ps1 e) = HS.Lambda (ps ++ ps1) e hsLambda ps e = HS.Lambda ps e hsMapAlt :: (HS.Exp -> HS.Exp) -> HS.Alt -> HS.Alt hsMapAlt f (HS.Alt p rhs wh) = HS.Alt p (hsMapRHS f rhs) wh hsMapRHS :: (HS.Exp -> HS.Exp) -> HS.Rhs -> HS.Rhs hsMapRHS f (HS.UnGuardedRhs def) = HS.UnGuardedRhs (f def) hsMapRHS f (HS.GuardedRhss es) = HS.GuardedRhss [ HS.GuardedRhs g (f e) | HS.GuardedRhs g e <- es ] -------------------------------------------------- -- Hard coded module names -------------------------------------------------- mazstr :: String mazstr = "MAlonzo.Code" mazName :: Name mazName = mkName_ __IMPOSSIBLE__ mazstr mazMod' :: String -> HS.ModuleName mazMod' s = HS.ModuleName $ mazstr ++ "." ++ s mazMod :: ModuleName -> HS.ModuleName mazMod = mazMod' . prettyShow mazerror :: String -> a mazerror msg = error $ mazstr ++ ": " ++ msg mazCoerceName :: String mazCoerceName = "coe" mazErasedName :: String mazErasedName = "erased" mazCoerce :: HS.Exp -- mazCoerce = HS.Var $ HS.Qual unsafeCoerceMod (HS.Ident "unsafeCoerce") -- mazCoerce = HS.Var $ HS.Qual mazRTE $ HS.Ident mazCoerceName mazCoerce = HS.Var $ HS.UnQual $ HS.Ident mazCoerceName -- Andreas, 2011-11-16: error incomplete match now RTE-call mazIncompleteMatch :: HS.Exp mazIncompleteMatch = HS.Var $ HS.Qual mazRTE $ HS.Ident "mazIncompleteMatch" rtmIncompleteMatch :: QName -> HS.Exp rtmIncompleteMatch q = mazIncompleteMatch `HS.App` hsVarUQ (unqhname "name" q) mazUnreachableError :: HS.Exp mazUnreachableError = HS.Var $ HS.Qual mazRTE $ HS.Ident "mazUnreachableError" rtmUnreachableError :: HS.Exp rtmUnreachableError = mazUnreachableError mazRTE :: HS.ModuleName mazRTE = HS.ModuleName "MAlonzo.RTE" rtmQual :: String -> HS.QName rtmQual = HS.UnQual . HS.Ident rtmVar :: String -> HS.Exp rtmVar = HS.Var . rtmQual rtmError :: String -> HS.Exp rtmError s = rtmVar "error" `HS.App` (HS.Lit $ HS.String $ "MAlonzo Runtime Error: " ++ s) unsafeCoerceMod :: HS.ModuleName unsafeCoerceMod = HS.ModuleName "Unsafe.Coerce" -------------------------------------------------- -- Sloppy ways to declare = -------------------------------------------------- fakeD :: HS.Name -> String -> HS.Decl fakeD v s = HS.FunBind [HS.Match v [] (HS.UnGuardedRhs $ fakeExp s) emptyBinds] fakeDS :: String -> String -> HS.Decl fakeDS = fakeD . HS.Ident fakeDQ :: QName -> String -> HS.Decl fakeDQ = fakeD . unqhname "d" fakeType :: String -> HS.Type fakeType = HS.FakeType fakeExp :: String -> HS.Exp fakeExp = HS.FakeExp fakeDecl :: String -> HS.Decl fakeDecl = HS.FakeDecl -------------------------------------------------- -- Auxiliary definitions -------------------------------------------------- emptyBinds :: Maybe HS.Binds emptyBinds = Nothing -------------------------------------------------- -- Utilities for Haskell modules names -------------------------------------------------- -- | Can the character be used in a Haskell module name part -- (@conid@)? This function is more restrictive than what the Haskell -- report allows. isModChar :: Char -> Bool isModChar c = isLower c || isUpper c || isDigit c || c == '_' || c == '\'' Agda-2.5.3/src/full/Agda/Compiler/MAlonzo/Encode.hs0000644000000000000000000000432713154613124020020 0ustar0000000000000000------------------------------------------------------------------------ -- Module name encoding ------------------------------------------------------------------------ module Agda.Compiler.MAlonzo.Encode ( encodeModuleName ) where import Data.Char import Data.Function import qualified Data.List as List import qualified Agda.Utils.Haskell.Syntax as HS import Agda.Compiler.MAlonzo.Misc -- | Haskell module names have to satisfy the Haskell (including the -- hierarchical module namespace extension) lexical syntax: -- -- @modid -> [modid.] large {small | large | digit | ' }@ -- -- 'encodeModuleName' is an injective function into the set of module -- names defined by @modid@. The function preserves @.@s, and it also -- preserves module names whose first name part is not 'mazstr'. -- -- Precondition: The input must not start or end with @.@, and no two -- @.@s may be adjacent. encodeModuleName :: HS.ModuleName -> HS.ModuleName encodeModuleName (HS.ModuleName s) = HS.ModuleName $ case List.stripPrefix mazstr s of Just s' -> mazstr ++ foldr encNamePart "" (splitUp' s') Nothing -> s where -- splitUp ".apa.bepa." == [".","apa",".","bepa","."] -- splitUp = groupBy ((&&) `on` (/= '.')) -- Since comparison against "." is wasteful, and modules name components are nonempty, -- we can use "" as the separator. -- Since modules name components are nonempty, -- this is more efficient than adding a Maybe wrapper: -- We are effectively using ``String = Maybe NEString''. -- -- splitUp' ".apa.bepa." == ["","apa","","bepa",""] splitUp' :: String -> [String] splitUp' = h where h [] = [] h (c : cs) = case c of '.' -> "" : h cs _ -> g (c :) cs g acc [] = [acc []] g acc (c : cs) = case c of '.' -> acc [] : "" : h cs _ -> g (acc . (c :)) cs encNamePart "" r = '.' : r encNamePart s r = ensureFirstCharLarge s $ foldr enc r s ensureFirstCharLarge s r = case s of c : cs | isUpper c && c /= largeChar -> r _ -> largeChar : r largeChar = 'Q' escapeChar = 'Z' isOK c = c /= escapeChar && isModChar c enc c r | isOK c = c : r | otherwise = escapeChar : shows (fromEnum c) (escapeChar : r) Agda-2.5.3/src/full/Agda/Compiler/MAlonzo/Compiler.hs0000644000000000000000000007251713154613124020403 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Compiler.MAlonzo.Compiler where #if __GLASGOW_HASKELL__ <= 708 import Prelude hiding (foldl, mapM_, mapM, sequence, concat) #endif import Control.Applicative import Control.Monad.Reader hiding (mapM_, forM_, mapM, forM, sequence) import Control.Monad.State hiding (mapM_, forM_, mapM, forM, sequence) import Data.Generics.Geniplate import Data.Foldable hiding (any, all, foldr, sequence_) import Data.Function import qualified Data.List as List import Data.Map (Map) import qualified Data.Map as Map import Data.Maybe import Data.Set (Set) import qualified Data.Set as Set import Data.Traversable hiding (for) import Data.Monoid hiding ((<>)) import Numeric.IEEE import qualified Agda.Utils.Haskell.Syntax as HS import System.Directory (createDirectoryIfMissing) import System.FilePath hiding (normalise) import Agda.Compiler.CallCompiler import Agda.Compiler.Common import Agda.Compiler.MAlonzo.Misc import Agda.Compiler.MAlonzo.Pretty import Agda.Compiler.MAlonzo.Primitives import Agda.Compiler.MAlonzo.HaskellTypes import Agda.Compiler.MAlonzo.Pragmas import Agda.Compiler.ToTreeless import Agda.Compiler.Treeless.Unused import Agda.Compiler.Treeless.Erase import Agda.Compiler.Backend import Agda.Interaction.FindFile import Agda.Interaction.Imports import Agda.Interaction.Options import Agda.Syntax.Common import Agda.Syntax.Fixity import qualified Agda.Syntax.Abstract.Name as A import qualified Agda.Syntax.Concrete.Name as C import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Names (namesIn) import qualified Agda.Syntax.Treeless as T import Agda.Syntax.Literal import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Datatypes import Agda.TypeChecking.Records import Agda.TypeChecking.Reduce import Agda.TypeChecking.Pretty import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Level (reallyUnLevelView) import Agda.TypeChecking.Warnings import Agda.TypeChecking.CompiledClause import Agda.Utils.FileName import Agda.Utils.Functor import Agda.Utils.IO.Directory import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Pretty (prettyShow, Pretty) import qualified Agda.Utils.IO.UTF8 as UTF8 import qualified Agda.Utils.HashMap as HMap import Agda.Utils.Singleton import Agda.Utils.Size import Agda.Utils.Tuple import Paths_Agda #include "undefined.h" import Agda.Utils.Impossible -- The backend callbacks -------------------------------------------------- ghcBackend :: Backend ghcBackend = Backend ghcBackend' ghcBackend' :: Backend' GHCOptions GHCOptions GHCModuleEnv IsMain [HS.Decl] ghcBackend' = Backend' { backendName = "GHC" , backendVersion = Nothing , options = defaultGHCOptions , commandLineFlags = ghcCommandLineFlags , isEnabled = optGhcCompile , preCompile = ghcPreCompile , postCompile = ghcPostCompile , preModule = ghcPreModule , postModule = ghcPostModule , compileDef = ghcCompileDef , scopeCheckingSuffices = False } --- Options --- data GHCOptions = GHCOptions { optGhcCompile :: Bool , optGhcCallGhc :: Bool , optGhcFlags :: [String] } defaultGHCOptions :: GHCOptions defaultGHCOptions = GHCOptions { optGhcCompile = False , optGhcCallGhc = True , optGhcFlags = [] } ghcCommandLineFlags :: [OptDescr (Flag GHCOptions)] ghcCommandLineFlags = [ Option ['c'] ["compile", "ghc"] (NoArg enable) "compile program using the GHC backend" , Option [] ["ghc-dont-call-ghc"] (NoArg dontCallGHC) "don't call GHC, just write the GHC Haskell files." , Option [] ["ghc-flag"] (ReqArg ghcFlag "GHC-FLAG") "give the flag GHC-FLAG to GHC" ] where enable o = pure o{ optGhcCompile = True } dontCallGHC o = pure o{ optGhcCallGhc = False } ghcFlag f o = pure o{ optGhcFlags = optGhcFlags o ++ [f] } --- Top-level compilation --- ghcPreCompile :: GHCOptions -> TCM GHCOptions ghcPreCompile ghcOpts = do allowUnsolved <- optAllowUnsolved <$> pragmaOptions when allowUnsolved $ genericError $ "Unsolved meta variables are not allowed when compiling." return ghcOpts ghcPostCompile :: GHCOptions -> IsMain -> Map ModuleName IsMain -> TCM () ghcPostCompile opts isMain mods = copyRTEModules >> callGHC opts isMain mods --- Module compilation --- type GHCModuleEnv = Maybe CoinductionKit ghcPreModule :: GHCOptions -> ModuleName -> FilePath -> TCM (Recompile GHCModuleEnv IsMain) ghcPreModule _ m ifile = ifM uptodate noComp yesComp where uptodate = liftIO =<< isNewerThan <$> outFile_ <*> pure ifile noComp = do reportSLn "compile.ghc" 2 . (++ " : no compilation is needed.") . show . A.mnameToConcrete =<< curMName Skip . hasMainFunction <$> curIF yesComp = do m <- show . A.mnameToConcrete <$> curMName out <- outFile_ reportSLn "compile.ghc" 1 $ repl [m, ifile, out] "Compiling <<0>> in <<1>> to <<2>>" stImportedModules .= Set.empty -- we use stImportedModules to accumulate the required Haskell imports Recompile <$> coinductionKit ghcPostModule :: GHCOptions -> GHCModuleEnv -> IsMain -> ModuleName -> [[HS.Decl]] -> TCM IsMain ghcPostModule _ _ _ _ defs = do m <- curHsMod imps <- imports -- Get content of FOREIGN pragmas. (headerPragmas, hsImps, code) <- foreignHaskell writeModule $ HS.Module m (map HS.OtherPragma headerPragmas) imps (map fakeDecl (hsImps ++ code) ++ concat defs) hasMainFunction <$> curIF ghcCompileDef :: GHCOptions -> GHCModuleEnv -> Definition -> TCM [HS.Decl] ghcCompileDef _ = definition -- Compilation ------------------------------------------------------------ -------------------------------------------------- -- imported modules -- I use stImportedModules in a non-standard way, -- accumulating in it what are acutally used in Misc.xqual -------------------------------------------------- imports :: TCM [HS.ImportDecl] imports = (hsImps ++) <$> imps where hsImps :: [HS.ImportDecl] hsImps = [unqualRTE, decl mazRTE] unqualRTE :: HS.ImportDecl unqualRTE = HS.ImportDecl mazRTE False $ Just $ (False, [ HS.IVar $ HS.Ident x | x <- [mazCoerceName, mazErasedName] ++ map treelessPrimName [T.PAdd, T.PSub, T.PMul, T.PQuot, T.PRem, T.PGeq, T.PLt, T.PEqI, T.PEqF] ]) imps :: TCM [HS.ImportDecl] imps = List.map decl . uniq <$> ((++) <$> importsForPrim <*> (List.map mazMod <$> mnames)) decl :: HS.ModuleName -> HS.ImportDecl decl m = HS.ImportDecl m True Nothing mnames :: TCM [ModuleName] mnames = Set.elems <$> use stImportedModules uniq :: [HS.ModuleName] -> [HS.ModuleName] uniq = List.map head . List.group . List.sort -------------------------------------------------- -- Main compiling clauses -------------------------------------------------- -- | Note that the INFINITY, SHARP and FLAT builtins are translated as -- follows (if a 'CoinductionKit' is given): -- -- @ -- type Infinity a b = b -- -- sharp :: a -> a -- sharp x = x -- -- flat :: a -> a -- flat x = x -- @ definition :: Maybe CoinductionKit -> Definition -> TCM [HS.Decl] -- ignore irrelevant definitions {- Andreas, 2012-10-02: Invariant no longer holds definition kit (Defn Forced{} _ _ _ _ _ _ _ _) = __IMPOSSIBLE__ definition kit (Defn UnusedArg _ _ _ _ _ _ _ _) = __IMPOSSIBLE__ definition kit (Defn NonStrict _ _ _ _ _ _ _ _) = __IMPOSSIBLE__ -} definition kit Defn{defArgInfo = info, defName = q} | isIrrelevant info = do reportSDoc "compile.ghc.definition" 10 $ text "Not compiling" <+> prettyTCM q <> text "." return [] definition kit Defn{defName = q, defType = ty, theDef = d} = do reportSDoc "compile.ghc.definition" 10 $ vcat [ text "Compiling" <+> prettyTCM q <> text ":" , nest 2 $ text (show d) ] pragma <- getHaskellPragma q checkTypeOfMain q ty $ do infodecl q <$> case d of _ | Just (HsDefn r hs) <- pragma -> setCurrentRange r $ do -- Make sure we have imports for all names mentioned in the type. hsty <- haskellType q ty <- normalise ty sequence_ [ xqual x (HS.Ident "_") | x <- Set.toList (namesIn ty) ] -- Check that the function isn't INLINE (since that will make this -- definition pointless). inline <- (^. funInline) . theDef <$> getConstInfo q when inline $ warning $ UselessInline q return $ fbWithType hsty (fakeExp hs) -- Special treatment of coinductive builtins. Datatype{} | Just q == (nameOfInf <$> kit) -> do let infT = unqhname "T" q infV = unqhname "d" q a = ihname "a" 0 b = ihname "a" 1 vars = [a, b] return [ HS.TypeDecl infT (List.map HS.UnkindedVar vars) (HS.TyVar b) , HS.FunBind [HS.Match infV (List.map HS.PVar vars) (HS.UnGuardedRhs HS.unit_con) emptyBinds] ] Constructor{} | Just q == (nameOfSharp <$> kit) -> do let sharp = unqhname "d" q x = ihname "x" 0 return $ [ HS.TypeSig [sharp] $ fakeType $ "forall a. a -> a" , HS.FunBind [HS.Match sharp [HS.PVar x] (HS.UnGuardedRhs (HS.Var (HS.UnQual x))) emptyBinds] ] Function{} | Just q == (nameOfFlat <$> kit) -> do let flat = unqhname "d" q x = ihname "x" 0 return $ [ HS.TypeSig [flat] $ fakeType $ "forall a. a -> a" , HS.FunBind [HS.Match flat [HS.PVar x] (HS.UnGuardedRhs (HS.Var (HS.UnQual x))) emptyBinds] ] Axiom{} -> do ar <- typeArity ty return $ [ compiledTypeSynonym q ty ar | Just (HsType r ty) <- [pragma] ] ++ fb axiomErr Primitive{ primName = s } -> fb <$> primBody s Function{} -> function pragma $ functionViaTreeless q Datatype{ dataPars = np, dataIxs = ni, dataClause = cl, dataCons = cs } | Just (HsData r ty hsCons) <- pragma -> setCurrentRange r $ do computeErasedConstructorArgs q ccscov <- constructorCoverageCode q (np + ni) cs ty hsCons cds <- mapM compiledcondecl cs return $ tvaldecl q (dataInduction d) 0 (np + ni) [] (Just __IMPOSSIBLE__) ++ [compiledTypeSynonym q ty np] ++ cds ++ ccscov Datatype{ dataPars = np, dataIxs = ni, dataClause = cl, dataCons = cs } -> do computeErasedConstructorArgs q (ars, cds) <- unzip <$> mapM condecl cs return $ tvaldecl q (dataInduction d) (List.maximum (np:ars) - np) (np + ni) cds cl Constructor{} -> return [] Record{ recPars = np, recClause = cl, recConHead = con } | Just (HsData r ty hsCons) <- pragma -> setCurrentRange r $ do let cs = [conName con] computeErasedConstructorArgs q ccscov <- constructorCoverageCode q np cs ty hsCons cds <- mapM compiledcondecl cs return $ tvaldecl q Inductive 0 np [] (Just __IMPOSSIBLE__) ++ [compiledTypeSynonym q ty np] ++ cds ++ ccscov Record{ recClause = cl, recConHead = con, recFields = flds } -> do computeErasedConstructorArgs q let c = conName con let noFields = length flds let ar = I.arity ty cd <- snd <$> condecl c return $ tvaldecl q Inductive noFields ar [cd] cl AbstractDefn{} -> __IMPOSSIBLE__ where function :: Maybe HaskellPragma -> TCM [HS.Decl] -> TCM [HS.Decl] function mhe fun = do ccls <- mkwhere <$> fun case mhe of Just (HsExport r name) -> do t <- setCurrentRange r $ haskellType q let tsig :: HS.Decl tsig = HS.TypeSig [HS.Ident name] (fakeType t) def :: HS.Decl def = HS.FunBind [HS.Match (HS.Ident name) [] (HS.UnGuardedRhs (hsVarUQ $ dname q)) emptyBinds] return ([tsig,def] ++ ccls) _ -> return ccls functionViaTreeless :: QName -> TCM [HS.Decl] functionViaTreeless q = caseMaybeM (toTreeless q) (pure []) $ \ treeless -> do used <- getCompiledArgUse q let dostrip = any not used e <- if dostrip then closedTerm (stripUnusedArguments used treeless) else closedTerm treeless let (ps, b) = lamView e lamView e = case stripTopCoerce e of HS.Lambda ps b -> (ps, b) b -> ([], b) stripTopCoerce (HS.Lambda ps b) = HS.Lambda ps $ stripTopCoerce b stripTopCoerce e = case hsAppView e of [c, e] | c == mazCoerce -> e _ -> e funbind f ps b = HS.FunBind [HS.Match f ps (HS.UnGuardedRhs b) emptyBinds] -- The definition of the non-stripped function (ps0, _) <- lamView <$> closedTerm (foldr ($) T.TErased $ replicate (length used) T.TLam) let b0 = foldl HS.App (hsVarUQ $ duname q) [ hsVarUQ x | (~(HS.PVar x), True) <- zip ps0 used ] return $ if dostrip then [ funbind (dname q) ps0 b0 , funbind (duname q) ps b ] else [ funbind (dname q) ps b ] mkwhere :: [HS.Decl] -> [HS.Decl] mkwhere (HS.FunBind [m0, HS.Match dn ps rhs emptyBinds] : fbs@(_:_)) = [HS.FunBind [m0, HS.Match dn ps rhs bindsAux]] where bindsAux :: Maybe HS.Binds bindsAux = Just $ HS.BDecls fbs mkwhere fbs = fbs fbWithType :: HaskellType -> HS.Exp -> [HS.Decl] fbWithType ty e = [ HS.TypeSig [unqhname "d" q] $ fakeType ty ] ++ fb e fb :: HS.Exp -> [HS.Decl] fb e = [HS.FunBind [HS.Match (unqhname "d" q) [] (HS.UnGuardedRhs $ e) emptyBinds]] axiomErr :: HS.Exp axiomErr = rtmError $ "postulate evaluated: " ++ prettyShow q constructorCoverageCode :: QName -> Int -> [QName] -> HaskellType -> [HaskellCode] -> TCM [HS.Decl] constructorCoverageCode q np cs hsTy hsCons = do checkConstructorCount q cs hsCons ifM (noCheckCover q) (return []) $ do ccs <- List.concat <$> zipWithM checkConstructorType cs hsCons cov <- checkCover q hsTy np cs hsCons return $ ccs ++ cov -- | Environment for naming of local variables. -- Invariant: @reverse ccCxt ++ ccNameSupply@ data CCEnv = CCEnv { ccNameSupply :: NameSupply -- ^ Supply of fresh names , ccCxt :: CCContext -- ^ Names currently in scope } type NameSupply = [HS.Name] type CCContext = [HS.Name] mapNameSupply :: (NameSupply -> NameSupply) -> CCEnv -> CCEnv mapNameSupply f e = e { ccNameSupply = f (ccNameSupply e) } mapContext :: (CCContext -> CCContext) -> CCEnv -> CCEnv mapContext f e = e { ccCxt = f (ccCxt e) } -- | Initial environment for expression generation. initCCEnv :: CCEnv initCCEnv = CCEnv { ccNameSupply = map (ihname "v") [0..] -- DON'T CHANGE THESE NAMES! , ccCxt = [] } -- | Term variables are de Bruijn indices. lookupIndex :: Int -> CCContext -> HS.Name lookupIndex i xs = fromMaybe __IMPOSSIBLE__ $ xs !!! i type CC = ReaderT CCEnv TCM freshNames :: Int -> ([HS.Name] -> CC a) -> CC a freshNames n _ | n < 0 = __IMPOSSIBLE__ freshNames n cont = do (xs, rest) <- splitAt n <$> asks ccNameSupply local (mapNameSupply (const rest)) $ cont xs -- | Introduce n variables into the context. intros :: Int -> ([HS.Name] -> CC a) -> CC a intros n cont = freshNames n $ \xs -> local (mapContext (reverse xs ++)) $ cont xs checkConstructorType :: QName -> HaskellCode -> TCM [HS.Decl] checkConstructorType q hs = do ty <- haskellType q return [ HS.TypeSig [unqhname "check" q] $ fakeType ty , HS.FunBind [HS.Match (unqhname "check" q) [] (HS.UnGuardedRhs $ fakeExp hs) emptyBinds] ] checkCover :: QName -> HaskellType -> Nat -> [QName] -> [HaskellCode] -> TCM [HS.Decl] checkCover q ty n cs hsCons = do let tvs = [ "a" ++ show i | i <- [1..n] ] makeClause c hsc = do a <- erasedArity c let pat = HS.PApp (HS.UnQual $ HS.Ident hsc) $ replicate a HS.PWildCard return $ HS.Alt pat (HS.UnGuardedRhs $ HS.unit_con) emptyBinds cs <- zipWithM makeClause cs hsCons let rhs = case cs of [] -> fakeExp "()" -- There is no empty case statement in Haskell _ -> HS.Case (HS.Var $ HS.UnQual $ HS.Ident "x") cs return [ HS.TypeSig [unqhname "cover" q] $ fakeType $ unwords (ty : tvs) ++ " -> ()" , HS.FunBind [HS.Match (unqhname "cover" q) [HS.PVar $ HS.Ident "x"] (HS.UnGuardedRhs rhs) emptyBinds] ] closedTerm :: T.TTerm -> TCM HS.Exp closedTerm v = hsCast <$> term v `runReaderT` initCCEnv -- | Extract Agda term to Haskell expression. -- Erased arguments are extracted as @()@. -- Types are extracted as @()@. term :: T.TTerm -> CC HS.Exp term tm0 = case tm0 of T.TVar i -> do x <- lookupIndex i <$> asks ccCxt return $ hsVarUQ x T.TApp (T.TDef f) ts -> do used <- lift $ getCompiledArgUse f isCompiled <- lift $ isJust <$> getHaskellPragma f -- #2248: no unused argument pruning for COMPILE'd functions let given = length ts needed = length used missing = drop given used if not isCompiled && any not used then if any not missing then term (etaExpand (needed - given) tm0) else do f <- lift $ HS.Var <$> xhqn "du" f -- used stripped function f `apps` [ t | (t, True) <- zip ts $ used ++ repeat True ] else do t' <- term (T.TDef f) t' `apps` ts T.TApp (T.TCon c) ts -> do kit <- lift coinductionKit if Just c == (nameOfSharp <$> kit) then do t' <- HS.Var <$> lift (xhqn "d" c) apps t' ts else do (ar, _) <- lift $ conArityAndPars c erased <- lift $ getErasedConArgs c let missing = drop (length ts) erased notErased = not case all notErased missing of False -> term $ etaExpand (length missing) tm0 True -> do f <- lift $ HS.Con <$> conhqn c f `apps` [ t | (t, False) <- zip ts erased ] T.TApp t ts -> do t' <- term t t' `apps` ts T.TLam at -> do (nm:_) <- asks ccNameSupply intros 1 $ \ [x] -> hsLambda [HS.PVar x] <$> term at T.TLet t1 t2 -> do t1' <- term t1 intros 1 $ \[x] -> do t2' <- term t2 return $ hsLet x (hsCast t1') t2' T.TCase sc ct def alts -> do sc' <- term (T.TVar sc) alts' <- traverse (alt sc) alts def' <- term def let defAlt = HS.Alt HS.PWildCard (HS.UnGuardedRhs def') emptyBinds return $ HS.Case (hsCast sc') (alts' ++ [defAlt]) T.TLit l -> return $ literal l T.TDef q -> do HS.Var <$> (lift $ xhqn "d" q) T.TCon q -> term (T.TApp (T.TCon q) []) T.TPrim p -> return $ compilePrim p T.TUnit -> return HS.unit_con T.TSort -> return HS.unit_con T.TErased -> return $ hsVarUQ $ HS.Ident mazErasedName T.TError e -> return $ case e of T.TUnreachable -> rtmUnreachableError where apps = foldM (\ h a -> HS.App h <$> term a) etaExpand n t = foldr (const T.TLam) (T.mkTApp (raise n t) [T.TVar i | i <- [n - 1, n - 2..0]]) (replicate n ()) compilePrim :: T.TPrim -> HS.Exp compilePrim s = HS.Var $ hsName $ treelessPrimName s alt :: Int -> T.TAlt -> CC HS.Alt alt sc a = do case a of T.TACon {T.aCon = c} -> do intros (T.aArity a) $ \ xs -> do erased <- lift $ getErasedConArgs c hConNm <- lift $ conhqn c mkAlt (HS.PApp hConNm $ map HS.PVar [ x | (x, False) <- zip xs erased ]) T.TAGuard g b -> do g <- term g b <- term b return $ HS.Alt HS.PWildCard (HS.GuardedRhss [HS.GuardedRhs [HS.Qualifier g] b]) emptyBinds T.TALit { T.aLit = LitQName _ q } -> mkAlt (litqnamepat q) T.TALit { T.aLit = l@LitFloat{}, T.aBody = b } -> mkGuarded (treelessPrimName T.PEqF) (literal l) b T.TALit { T.aLit = LitString _ s , T.aBody = b } -> mkGuarded "(==)" (litString s) b T.TALit {} -> mkAlt (HS.PLit $ hslit $ T.aLit a) where mkGuarded eq lit b = do b <- term b sc <- term (T.TVar sc) let guard = HS.Var (HS.UnQual (HS.Ident eq)) `HS.App` sc `HS.App` lit return $ HS.Alt HS.PWildCard (HS.GuardedRhss [HS.GuardedRhs [HS.Qualifier guard] b]) emptyBinds mkAlt :: HS.Pat -> CC HS.Alt mkAlt pat = do body' <- term $ T.aBody a return $ HS.Alt pat (HS.UnGuardedRhs $ hsCast body') emptyBinds literal :: Literal -> HS.Exp literal l = case l of LitNat _ _ -> typed "Integer" LitFloat _ x -> floatExp x "Double" LitQName _ x -> litqname x LitString _ s -> litString s _ -> l' where l' = HS.Lit $ hslit l typed = HS.ExpTypeSig l' . HS.TyCon . rtmQual -- ASR (2016-09-14): See Issue #2169. -- Ulf, 2016-09-28: and #2218. floatExp :: Double -> String -> HS.Exp floatExp x s | isNegativeZero x = rte "negativeZero" | isNegativeInf x = rte "negativeInfinity" | isInfinite x = rte "positiveInfinity" | isNegativeNaN x = rte "negativeNaN" | isNaN x = rte "positiveNaN" | otherwise = typed s rte = HS.Var . HS.Qual mazRTE . HS.Ident isNegativeInf x = isInfinite x && x < 0.0 isNegativeNaN x = isNaN x && not (identicalIEEE x (0.0 / 0.0)) hslit :: Literal -> HS.Literal hslit l = case l of LitNat _ x -> HS.Int x LitFloat _ x -> HS.Frac (toRational x) LitChar _ x -> HS.Char x LitQName _ x -> __IMPOSSIBLE__ LitString _ _ -> __IMPOSSIBLE__ LitMeta{} -> __IMPOSSIBLE__ litString :: String -> HS.Exp litString s = HS.Var (HS.Qual (HS.ModuleName "Data.Text") (HS.Ident "pack")) `HS.App` (HS.Lit $ HS.String s) litqname :: QName -> HS.Exp litqname x = rteCon "QName" `apps` [ hsTypedInt n , hsTypedInt m , HS.Lit $ HS.String $ prettyShow x , rteCon "Fixity" `apps` [ litAssoc (fixityAssoc fx) , litPrec (fixityLevel fx) ] ] where apps = foldl HS.App rteCon name = HS.Con $ HS.Qual mazRTE $ HS.Ident name NameId n m = nameId $ qnameName x fx = theFixity $ nameFixity $ qnameName x litAssoc NonAssoc = rteCon "NonAssoc" litAssoc LeftAssoc = rteCon "LeftAssoc" litAssoc RightAssoc = rteCon "RightAssoc" litPrec Unrelated = rteCon "Unrelated" litPrec (Related l) = rteCon "Related" `HS.App` hsTypedInt l litqnamepat :: QName -> HS.Pat litqnamepat x = HS.PApp (HS.Qual mazRTE $ HS.Ident "QName") [ HS.PLit (HS.Int $ fromIntegral n) , HS.PLit (HS.Int $ fromIntegral m) , HS.PWildCard, HS.PWildCard ] where NameId n m = nameId $ qnameName x erasedArity :: QName -> TCM Nat erasedArity q = do (ar, _) <- conArityAndPars q erased <- length . filter id <$> getErasedConArgs q return (ar - erased) condecl :: QName -> TCM (Nat, HS.ConDecl) condecl q = do (ar, np) <- conArityAndPars q erased <- length . filter id <$> getErasedConArgs q let ar' = ar - erased return $ (ar' + np, cdecl q ar') cdecl :: QName -> Nat -> HS.ConDecl cdecl q n = HS.ConDecl (unqhname "C" q) [ HS.TyVar $ ihname "a" i | i <- [0 .. n - 1] ] compiledcondecl :: QName -> TCM HS.Decl compiledcondecl q = do (ar, np) <- conArityAndPars q hsCon <- fromMaybe __IMPOSSIBLE__ <$> getHaskellConstructor q let patVars = map (HS.PVar . ihname "a") [0 .. ar - 1] return $ HS.PatSyn (HS.PApp (HS.UnQual $ unqhname "C" q) patVars) (HS.PApp (hsName hsCon) patVars) compiledTypeSynonym :: QName -> String -> Nat -> HS.Decl compiledTypeSynonym q hsT arity = HS.TypeDecl (unqhname "T" q) (map HS.UnkindedVar vs) (foldl HS.TyApp (HS.FakeType hsT) $ map HS.TyVar vs) where vs = [ ihname "a" i | i <- [0 .. arity - 1]] tvaldecl :: QName -> Induction -- ^ Is the type inductive or coinductive? -> Nat -> Nat -> [HS.ConDecl] -> Maybe Clause -> [HS.Decl] tvaldecl q ind ntv npar cds cl = HS.FunBind [HS.Match vn pvs (HS.UnGuardedRhs HS.unit_con) emptyBinds] : maybe [HS.DataDecl kind tn tvs cds []] (const []) cl where (tn, vn) = (unqhname "T" q, unqhname "d" q) tvs = [ HS.UnkindedVar $ ihname "a" i | i <- [0 .. ntv - 1]] pvs = [ HS.PVar $ ihname "a" i | i <- [0 .. npar - 1]] -- Inductive data types consisting of a single constructor with a -- single argument are translated into newtypes. kind = case (ind, cds) of (Inductive, [HS.ConDecl _ [_]]) -> HS.NewType _ -> HS.DataType infodecl :: QName -> [HS.Decl] -> [HS.Decl] infodecl _ [] = [] infodecl q ds = fakeD (unqhname "name" q) (show $ prettyShow q) : ds -------------------------------------------------- -- Inserting unsafeCoerce -------------------------------------------------- hsCast :: HS.Exp -> HS.Exp {- hsCast = addcast . go where addcast [e@(HS.Var(HS.UnQual(HS.Ident(c:ns))))] | c == 'v' && all isDigit ns = e addcast es = foldl HS.App mazCoerce es -- this need to be extended if you generate other kinds of exps. go (HS.App e1 e2 ) = go e1 ++ [hsCast e2] go (HS.Lambda _ ps e) = [ HS.Lambda ps (hsCast e) ] go e = [e] -} -- TODO: what's the specification for hsCast, hsCast' and hsCoerce??? hsCast e = hsCoerce (hsCast' e) hsCast' :: HS.Exp -> HS.Exp hsCast' (HS.InfixApp e1 op e2) = hsCoerce $ HS.InfixApp (hsCast' e1) op (hsCast' e2) hsCast' (HS.Lambda ps e) = HS.Lambda ps $ hsCast' e hsCast' (HS.Let bs e) = HS.Let bs $ hsCast' e hsCast' (HS.Case sc alts) = HS.Case (hsCast' sc) (map (hsMapAlt hsCast') alts) hsCast' e = case hsAppView e of f : es -> foldl HS.App (hsCoerce f) (map hsCastApp es) _ -> __IMPOSSIBLE__ -- We still have to coerce function applications in arguments to coerced -- functions. hsCastApp :: HS.Exp -> HS.Exp hsCastApp (HS.Lambda ps b) = HS.Lambda ps (hsCastApp b) hsCastApp (HS.Let bs e) = HS.Let bs $ hsCastApp e hsCastApp (HS.Case sc bs) = HS.Case (hsCastApp sc) (map (hsMapAlt hsCastApp) bs) hsCastApp (HS.InfixApp e1 op e2) = HS.InfixApp (hsCastApp e1) op (hsCastApp e2) hsCastApp e = case hsAppView e of f : es@(_:_) -> foldl HS.App (hsCoerce f) $ map hsCastApp es _ -> e -- No coercion for literal integers hsCoerce :: HS.Exp -> HS.Exp hsCoerce e@(HS.ExpTypeSig (HS.Lit (HS.Int{})) _) = e hsCoerce (HS.Case sc alts) = HS.Case sc (map (hsMapAlt hsCoerce) alts) hsCoerce (HS.Let bs e) = HS.Let bs $ hsCoerce e hsCoerce e = case hsAppView e of c : _ | c == mazCoerce || c == mazIncompleteMatch -> e _ -> mazCoerce `HS.App` e -------------------------------------------------- -- Writing out a haskell module -------------------------------------------------- copyRTEModules :: TCM () copyRTEModules = do dataDir <- lift getDataDir let srcDir = dataDir "MAlonzo" "src" (lift . copyDirContent srcDir) =<< compileDir writeModule :: HS.Module -> TCM () writeModule (HS.Module m ps imp ds) = do -- Note that GHC assumes that sources use ASCII or UTF-8. out <- outFile m liftIO $ UTF8.writeFile out $ prettyPrint $ HS.Module m (p : ps) imp ds where p = HS.LanguagePragma $ List.map HS.Ident $ [ "EmptyDataDecls" , "ExistentialQuantification" , "ScopedTypeVariables" , "NoMonomorphismRestriction" , "Rank2Types" , "PatternSynonyms" ] outFile' :: Pretty a => a -> TCM (FilePath, FilePath) outFile' m = do mdir <- compileDir let (fdir, fn) = splitFileName $ repldot pathSeparator $ prettyPrint m let dir = mdir fdir fp = dir replaceExtension fn "hs" liftIO $ createDirectoryIfMissing True dir return (mdir, fp) where repldot c = List.map $ \ c' -> if c' == '.' then c else c' outFile :: HS.ModuleName -> TCM FilePath outFile m = snd <$> outFile' m outFile_ :: TCM FilePath outFile_ = outFile =<< curHsMod callGHC :: GHCOptions -> IsMain -> Map ModuleName IsMain -> TCM () callGHC opts modIsMain mods = do mdir <- compileDir hsmod <- prettyPrint <$> curHsMod agdaMod <- curMName let outputName = case mnameToList agdaMod of [] -> __IMPOSSIBLE__ ms -> last ms (mdir, fp) <- outFile' =<< curHsMod let ghcopts = optGhcFlags opts let modIsReallyMain = fromMaybe __IMPOSSIBLE__ $ Map.lookup agdaMod mods isMain = mappend modIsMain modIsReallyMain -- both need to be IsMain -- Warn if no main function and not --no-main when (modIsMain /= isMain) $ genericWarning =<< fsep (pwords "No main function defined in" ++ [prettyTCM agdaMod <> text "."] ++ pwords "Use --no-main to suppress this warning.") let overridableArgs = [ "-O"] ++ (if isMain == IsMain then ["-o", mdir show (nameConcrete outputName)] else []) ++ [ "-Werror"] otherArgs = [ "-i" ++ mdir] ++ (if isMain == IsMain then ["-main-is", hsmod] else []) ++ [ fp , "--make" , "-fwarn-incomplete-patterns" , "-fno-warn-overlapping-patterns" ] args = overridableArgs ++ ghcopts ++ otherArgs compiler = "ghc" -- Note: Some versions of GHC use stderr for progress reports. For -- those versions of GHC we don't print any progress information -- unless an error is encountered. let doCall = optGhcCallGhc opts callCompiler doCall compiler args Agda-2.5.3/src/full/Agda/Compiler/MAlonzo/Pragmas.hs0000644000000000000000000001605013154613124020211 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Compiler.MAlonzo.Pragmas where import Control.Applicative import Control.Monad import Data.Maybe import Data.Char import qualified Data.List as List import Data.Traversable (traverse) import Data.Map (Map) import qualified Data.Map as Map import Agda.Syntax.Position import Agda.Syntax.Abstract.Name import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Primitive import Agda.Utils.Lens import Agda.Utils.Parser.ReadP import Agda.Utils.Pretty hiding (char) import Agda.Utils.String ( ltrim ) import Agda.Utils.Three import Agda.Compiler.Common import Agda.Utils.Impossible #include "undefined.h" data HaskellPragma = HsDefn Range HaskellCode | HsType Range HaskellType | HsData Range HaskellType [HaskellCode] | HsExport Range HaskellCode deriving (Show, Eq) instance HasRange HaskellPragma where getRange (HsDefn r _) = r getRange (HsType r _) = r getRange (HsData r _ _) = r getRange (HsExport r _) = r -- Syntax for Haskell pragmas: -- HsDefn CODE "= CODE" -- HsType TYPE "= type TYPE" -- HsData NAME CONS "= data NAME (CON₁ | .. | CONₙ)" -- HsExport NAME "as NAME" parsePragma :: CompilerPragma -> Either String HaskellPragma parsePragma (CompilerPragma r s) = case parse pragmaP s of [] -> Left $ "Failed to parse GHC pragma '" ++ s ++ "'" [p] -> Right p ps -> Left $ "Ambiguous parse of pragma '" ++ s ++ "':\n" ++ unlines (map show ps) -- shouldn't happen where pragmaP :: ReadP Char HaskellPragma pragmaP = choice [ exportP, typeP, dataP, defnP ] whitespace = many1 (satisfy isSpace) wordsP [] = return () wordsP (w:ws) = skipSpaces *> string w *> wordsP ws barP = skipSpaces *> char '|' -- quite liberal isIdent c = isAlphaNum c || elem c "_.':[]" isOp c = not $ isSpace c || elem c "()" hsIdent = fst <$> gather (choice [ string "()" , many1 (satisfy isIdent) , between (char '(') (char ')') (many1 (satisfy isOp)) ]) hsCode = many1 get -- very liberal paren = between (skipSpaces *> char '(') (skipSpaces *> char ')') notTypeOrData = do s <- look guard $ not $ any (`List.isPrefixOf` s) ["type", "data"] exportP = HsExport r <$ wordsP ["as"] <* whitespace <*> hsIdent <* skipSpaces typeP = HsType r <$ wordsP ["=", "type"] <* whitespace <*> hsCode dataP = HsData r <$ wordsP ["=", "data"] <* whitespace <*> hsIdent <*> paren (sepBy (skipSpaces *> hsIdent) barP) <* skipSpaces defnP = HsDefn r <$ wordsP ["="] <* whitespace <* notTypeOrData <*> hsCode parseHaskellPragma :: CompilerPragma -> TCM HaskellPragma parseHaskellPragma p = setCurrentRange p $ case parsePragma p of Left err -> genericError err Right p -> return p getHaskellPragma :: QName -> TCM (Maybe HaskellPragma) getHaskellPragma q = do pragma <- traverse parseHaskellPragma =<< getUniqueCompilerPragma ghcBackendName q def <- getConstInfo q setCurrentRange pragma $ pragma <$ sanityCheckPragma def pragma sanityCheckPragma :: Definition -> Maybe HaskellPragma -> TCM () sanityCheckPragma _ Nothing = return () sanityCheckPragma def (Just HsDefn{}) = case theDef def of Axiom{} -> return () Function{} -> return () AbstractDefn{} -> __IMPOSSIBLE__ Datatype{} -> recOrDataErr "data" Record{} -> recOrDataErr "record" _ -> typeError $ GenericError "Haskell definitions can only be given for postulates and functions." where recOrDataErr which = typeError $ GenericDocError $ sep [ text $ "Bad COMPILE GHC pragma for " ++ which ++ " type. Use" , text "{-# COMPILE GHC = data ( | .. | ) #-}" ] sanityCheckPragma def (Just HsData{}) = case theDef def of Datatype{} -> return () Record{} -> return () _ -> typeError $ GenericError "Haskell data types can only be given for data or record types." sanityCheckPragma def (Just HsType{}) = case theDef def of Axiom{} -> return () Datatype{} -> do -- We use HsType pragmas for Nat, Int and Bool nat <- getBuiltinName builtinNat int <- getBuiltinName builtinInteger bool <- getBuiltinName builtinBool unless (Just (defName def) `elem` [nat, int, bool]) err _ -> err where err = typeError $ GenericError "Haskell types can only be given for postulates." sanityCheckPragma def (Just HsExport{}) = case theDef def of Function{} -> return () _ -> typeError $ GenericError "Only functions can be exported to Haskell using {-# COMPILE GHC as #-}" -- TODO: cache this to avoid parsing the pragma for every constructor -- occurrence! getHaskellConstructor :: QName -> TCM (Maybe HaskellCode) getHaskellConstructor c = do c <- canonicalName c cDef <- theDef <$> getConstInfo c true <- getBuiltinName builtinTrue false <- getBuiltinName builtinFalse case cDef of _ | Just c == true -> return $ Just "True" | Just c == false -> return $ Just "False" Constructor{conData = d} -> do mp <- getHaskellPragma d case mp of Just (HsData _ _ hsCons) -> do cons <- defConstructors . theDef <$> getConstInfo d return $ Just $ fromMaybe __IMPOSSIBLE__ $ lookup c $ zip cons hsCons _ -> return Nothing _ -> return Nothing -- | Get content of @FOREIGN GHC@ pragmas, sorted by 'KindOfForeignCode': -- file header pragmas, import statements, rest. foreignHaskell :: TCM ([String], [String], [String]) foreignHaskell = partitionByKindOfForeignCode classifyForeign . map getCode . fromMaybe [] . Map.lookup ghcBackendName . iForeignCode <$> curIF where getCode (ForeignCode _ code) = code -- | Classify @FOREIGN@ Haskell code. data KindOfForeignCode = ForeignFileHeaderPragma -- ^ A pragma that must appear before the module header. | ForeignImport -- ^ An import statement. Must appear right after the module header. | ForeignOther -- ^ The rest. To appear after the import statements. -- | Classify a @FOREIGN GHC@ declaration. classifyForeign :: String -> KindOfForeignCode classifyForeign s0 = case ltrim s0 of s | List.isPrefixOf "import " s -> ForeignImport s | List.isPrefixOf "{-#" s -> classifyPragma $ drop 3 s _ -> ForeignOther -- | Classify a Haskell pragma into whether it is a file header pragma or not. classifyPragma :: String -> KindOfForeignCode classifyPragma s0 = case ltrim s0 of s | any (`List.isPrefixOf` s) fileHeaderPragmas -> ForeignFileHeaderPragma _ -> ForeignOther where fileHeaderPragmas = [ "LANGUAGE" , "OPTIONS_GHC" , "INCLUDE" ] -- | Partition a list by 'KindOfForeignCode' attribute. partitionByKindOfForeignCode :: (a -> KindOfForeignCode) -> [a] -> ([a], [a], [a]) partitionByKindOfForeignCode f = partition3 $ toThree . f where toThree = \case ForeignFileHeaderPragma -> One ForeignImport -> Two ForeignOther -> Three Agda-2.5.3/src/full/Agda/Compiler/JS/0000755000000000000000000000000013154613124015216 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Compiler/JS/Pretty.hs0000644000000000000000000001153213154613124017043 0ustar0000000000000000module Agda.Compiler.JS.Pretty where import Prelude hiding ( null ) import Data.List ( intercalate ) import Data.Set ( Set, toList, singleton, insert, member ) import Data.Map ( Map, toAscList, empty, null ) import Text.Regex.TDFA (makeRegex, matchTest, Regex) import Agda.Syntax.Common ( Nat ) import Agda.Utils.Hash import Agda.Compiler.JS.Syntax hiding (exports) -- Pretty-print a lambda-calculus expression as ECMAScript. -- Since ECMAScript is C-like rather than Haskell-like, it's easier to -- do the pretty-printing directly than use the Pretty library, which -- assumes Haskell-like indentation. br :: Int -> String br i = "\n" ++ take (2*i) (repeat ' ') unescape :: Char -> String unescape '"' = "\\\"" unescape '\\' = "\\\\" unescape '\n' = "\\n" unescape '\r' = "\\r" unescape '\x2028' = "\\u2028" unescape '\x2029' = "\\u2029" unescape c = [c] unescapes :: String -> String unescapes s = concat (map unescape s) -- pretty n i e pretty-prints e, under n levels of de Bruijn binding, -- with i levels of indentation. class Pretty a where pretty :: Nat -> Int -> a -> String instance (Pretty a, Pretty b) => Pretty (a,b) where pretty n i (x,y) = pretty n i x ++ ": " ++ pretty n (i+1) y -- Pretty-print collections class Pretties a where pretties :: Nat -> Int -> a -> [String] instance Pretty a => Pretties [a] where pretties n i = map (pretty n i) instance (Pretty a, Pretty b) => Pretties (Map a b) where pretties n i o = pretties n i (toAscList o) -- Pretty print identifiers instance Pretty LocalId where pretty n i (LocalId x) = "x" ++ show (n - x - 1) instance Pretty GlobalId where pretty n i (GlobalId m) = variableName $ intercalate "_" m instance Pretty MemberId where pretty n i (MemberId s) = "\"" ++ unescapes s ++ "\"" -- Pretty print expressions instance Pretty Exp where pretty n i (Self) = "exports" pretty n i (Local x) = pretty n i x pretty n i (Global m) = pretty n i m pretty n i (Undefined) = "undefined" pretty n i (String s) = "\"" ++ unescapes s ++ "\"" pretty n i (Char c) = "\"" ++ unescape c ++ "\"" pretty n i (Integer x) = "agdaRTS.primIntegerFromString(\"" ++ show x ++ "\")" pretty n i (Double x) = show x pretty n i (Lambda x e) = "function (" ++ intercalate ", " (pretties (n+x) i (map LocalId [x-1, x-2 .. 0])) ++ ") " ++ block (n+x) i e pretty n i (Object o) | null o = "{}" pretty n i (Object o) | otherwise = "{" ++ br (i+1) ++ intercalate ("," ++ br (i+1)) (pretties n i o) ++ br i ++ "}" pretty n i (Apply f es) = pretty n i f ++ "(" ++ intercalate ", " (pretties n i es) ++ ")" pretty n i (Lookup e l) = pretty n i e ++ "[" ++ pretty n i l ++ "]" pretty n i (If e f g) = "(" ++ pretty n i e ++ "? " ++ pretty n i f ++ ": " ++ pretty n i g ++ ")" pretty n i (PreOp op e) = "(" ++ op ++ " " ++ pretty n i e ++ ")" pretty n i (BinOp e op f) = "(" ++ pretty n i e ++ " " ++ op ++ " " ++ pretty n i f ++ ")" pretty n i (Const c) = c pretty n i (PlainJS js) = "(" ++ js ++ ")" block :: Nat -> Int -> Exp -> String block n i (If e f g) = "{" ++ br (i+1) ++ block' n (i+1) (If e f g) ++ br i ++ "}" block n i e = "{" ++ br (i+1) ++ "return " ++ pretty n (i+1) e ++ ";" ++ br i ++ "}" block' :: Nat -> Int -> Exp -> String block' n i (If e f g) = "if (" ++ pretty n i e ++ ") " ++ block n i f ++ " else " ++ block' n i g block' n i e = block n i e modname :: GlobalId -> String modname (GlobalId ms) = "\"" ++ intercalate "." ms ++ "\"" exports :: Nat -> Int -> Set [MemberId] -> [Export] -> String exports n i lss [] = "" exports n i lss (Export ls e : es) | member (init ls) lss = "exports[" ++ intercalate "][" (pretties n i ls) ++ "] = " ++ pretty n (i+1) e ++ ";" ++ br i ++ exports n i (insert ls lss) es exports n i lss (Export ls e : es) | otherwise = exports n i lss (Export (init ls) (Object empty) : Export ls e : es) instance Pretty Module where pretty n i (Module m es ex) = imports ++ br i ++ exports n i (singleton []) es ++ br i ++ maybe "" (pretty n i) ex where js = toList (globals es) imports = unlines $ ["var agdaRTS = require(\"agda-rts\");"] ++ ["var " ++ pretty n (i+1) e ++ " = require(" ++ modname e ++ ");" | e <- js] variableName :: String -> String variableName s = if isValidJSIdent s then "z_" ++ s else "h_" ++ show (hashString s) -- | Check if a string is a valid JS identifier. The check ignores keywords -- as we prepend z_ to our identifiers. The check -- is conservative and may not admit all valid JS identifiers. isValidJSIdent :: String -> Bool isValidJSIdent s = matchTest regex s where regex :: Regex regex = makeRegex "^[a-zA-Z_$][0-9a-zA-Z_$]*$" Agda-2.5.3/src/full/Agda/Compiler/JS/Substitution.hs0000644000000000000000000001001713154613124020265 0ustar0000000000000000module Agda.Compiler.JS.Substitution where import Prelude hiding ( map, lookup ) import Data.Map ( empty, unionWith, singleton, findWithDefault ) import qualified Data.Map as Map import Data.List ( genericIndex ) import qualified Data.List as List import Agda.Syntax.Common ( Nat ) import Agda.Compiler.JS.Syntax ( Exp(Self,Undefined,Local,Lambda,Object,Apply,Lookup,If,BinOp,PreOp), MemberId, LocalId(LocalId) ) import Agda.Utils.Function ( iterate' ) -- Map for expressions map :: Nat -> (Nat -> LocalId -> Exp) -> Exp -> Exp map m f (Local i) = f m i map m f (Lambda i e) = Lambda i (map (m + i) f e) map m f (Object o) = Object (Map.map (map m f) o) map m f (Apply e es) = Apply (map m f e) (List.map (map m f) es) map m f (Lookup e l) = Lookup (map m f e) l map m f (If e e' e'') = If (map m f e) (map m f e') (map m f e'') map m f (PreOp op e) = PreOp op (map m f e) map m f (BinOp e op e') = BinOp (map m f e) op (map m f e') map m f e = e -- Shifting shift :: Nat -> Exp -> Exp shift = shiftFrom 0 shiftFrom :: Nat -> Nat -> Exp -> Exp shiftFrom m 0 e = e shiftFrom m n e = map m (shifter n) e shifter :: Nat -> Nat -> LocalId -> Exp shifter n m (LocalId i) | i < m = Local (LocalId i) shifter n m (LocalId i) | otherwise = Local (LocalId (i + n)) -- Substitution subst :: Nat -> [Exp] -> Exp -> Exp subst 0 es e = e subst n es e = map 0 (substituter n es) e substituter :: Nat -> [Exp] -> Nat -> LocalId -> Exp substituter n es m (LocalId i) | i < m = Local (LocalId i) substituter n es m (LocalId i) | (i - m) < n = shift m (genericIndex (es ++ repeat Undefined) (n - (i + 1 - m))) substituter n es m (LocalId i) | otherwise = Local (LocalId (i - n)) -- A variant on substitution which performs beta-reduction map' :: Nat -> (Nat -> LocalId -> Exp) -> Exp -> Exp map' m f (Local i) = f m i map' m f (Lambda i e) = Lambda i (map' (m + i) f e) map' m f (Object o) = Object (Map.map (map' m f) o) map' m f (Apply e es) = apply (map' m f e) (List.map (map' m f) es) map' m f (Lookup e l) = lookup (map' m f e) l map' m f (If e e' e'') = If (map' m f e) (map' m f e') (map' m f e'') map' m f (PreOp op e) = PreOp op (map' m f e) map' m f (BinOp e op e') = BinOp (map' m f e) op (map' m f e') map' m f e = e subst' :: Nat -> [Exp] -> Exp -> Exp subst' 0 es e = e subst' n es e = map' 0 (substituter n es) e -- Beta-reducing application and field access apply :: Exp -> [Exp] -> Exp apply (Lambda i e) es = subst' i es e apply e es = Apply e es lookup :: Exp -> MemberId -> Exp lookup (Object o) l = findWithDefault Undefined l o lookup e l = Lookup e l -- Replace any top-level occurrences of self -- (needed because JS is a cbv language, so any top-level -- recursions would evaluate before the module has been defined, -- e.g. exports = { x: 1, y: exports.x } results in an exception, -- as exports is undefined at the point that exports.x is evaluated), self :: Exp -> Exp -> Exp self e (Self) = e self e (Object o) = Object (Map.map (self e) o) self e (Apply f es) = case (self e f) of (Lambda n g) -> self e (subst' n es g) g -> Apply g (List.map (self e) es) self e (Lookup f l) = lookup (self e f) l self e (If f g h) = If (self e f) (self e g) (self e h) self e (BinOp f op g) = BinOp (self e f) op (self e g) self e (PreOp op f) = PreOp op (self e f) self e f = f -- Find the fixed point of an expression, with no top-level occurrences -- of self. fix :: Exp -> Exp fix f = e where e = self e f -- Some helper functions curriedApply :: Exp -> [Exp] -> Exp curriedApply = foldl (\ f e -> apply f [e]) curriedLambda :: Nat -> Exp -> Exp curriedLambda n = iterate' n (Lambda 1) emp :: Exp emp = Object (empty) union :: Exp -> Exp -> Exp union (Object o) (Object p) = Object (unionWith union o p) union e f = e vine :: [MemberId] -> Exp -> Exp vine ls e = foldr (\ l e -> Object (singleton l e)) e ls object :: [([MemberId],Exp)] -> Exp object = foldr (\ (ls,e) -> (union (vine ls e))) emp Agda-2.5.3/src/full/Agda/Compiler/JS/Syntax.hs0000644000000000000000000000604213154613124017042 0ustar0000000000000000{-# LANGUAGE DeriveDataTypeable #-} module Agda.Compiler.JS.Syntax where import Data.Map ( Map ) import qualified Data.Map as Map import Data.Typeable ( Typeable ) import Data.Set ( Set, empty, singleton, union ) import Agda.Syntax.Common ( Nat ) -- An untyped lambda calculus with records, -- and a special self-binder for recursive declarations data Exp = Self | Local LocalId | Global GlobalId | Undefined | String String | Char Char | Integer Integer | Double Double | Lambda Nat Exp | Object (Map MemberId Exp) | Apply Exp [Exp] | Lookup Exp MemberId | If Exp Exp Exp | BinOp Exp String Exp | PreOp String Exp | Const String | PlainJS String -- ^ Arbitrary JS code. deriving (Typeable, Show) -- Local identifiers are named by De Bruijn indices. -- Global identifiers are named by string lists. -- Object members are named by strings. newtype LocalId = LocalId Nat deriving (Typeable, Eq, Ord, Show) newtype GlobalId = GlobalId [String] deriving (Typeable, Eq, Ord, Show) newtype MemberId = MemberId String deriving (Typeable, Eq, Ord, Show) -- The top-level compilation unit is a module, which names -- the GId of its exports, and a list of definitions data Export = Export { expName :: [MemberId], defn :: Exp } deriving (Typeable, Show) data Module = Module { modName :: GlobalId, exports :: [Export], postscript :: Maybe Exp } deriving (Typeable, Show) -- Note that modules are allowed to be recursive, via the Self expression, -- which is bound to the exported module. -- Top-level uses of the form exports.l1....lN. class Uses a where uses :: a -> Set [MemberId] instance Uses a => Uses [a] where uses = foldr (union . uses) empty instance Uses a => Uses (Map k a) where uses = Map.foldr (union . uses) empty instance Uses Exp where uses (Object o) = Map.foldr (union . uses) empty o uses (Apply e es) = foldr (union . uses) (uses e) es uses (Lookup e l) = uses' e [l] where uses' Self ls = singleton ls uses' (Lookup e l) ls = uses' e (l : ls) uses' e ls = uses e uses (If e f g) = uses e `union` uses f `union` uses g uses (BinOp e op f) = uses e `union` uses f uses (PreOp op e) = uses e uses e = empty instance Uses Export where uses (Export _ e) = uses e -- All global ids class Globals a where globals :: a -> Set GlobalId instance Globals a => Globals [a] where globals = foldr (union . globals) empty instance Globals a => Globals (Map k a) where globals = Map.foldr (union . globals) empty instance Globals Exp where globals (Global i) = singleton i globals (Lambda n e) = globals e globals (Object o) = globals o globals (Apply e es) = globals e `union` globals es globals (Lookup e l) = globals e globals (If e f g) = globals e `union` globals f `union` globals g globals (BinOp e op f) = globals e `union` globals f globals (PreOp op e) = globals e globals _ = empty instance Globals Export where globals (Export _ e) = globals e instance Globals Module where globals (Module m es _) = globals es Agda-2.5.3/src/full/Agda/Compiler/JS/Compiler.hs0000644000000000000000000004363613154613124017340 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Compiler.JS.Compiler where import Prelude hiding ( null, writeFile ) import Control.Applicative import Control.Monad.Reader ( liftIO ) import Control.Monad.Trans import Data.Char ( isSpace ) import Data.List ( intercalate, genericLength, partition ) import Data.Maybe ( isJust ) import Data.Set ( Set, null, insert, difference, delete ) import Data.Traversable (traverse) import Data.Map ( fromList, elems ) import qualified Data.Set as Set import qualified Data.Map as Map import System.Directory ( createDirectoryIfMissing ) import System.FilePath ( splitFileName, () ) import Agda.Interaction.FindFile ( findFile, findInterfaceFile ) import Agda.Interaction.Imports ( isNewerThan ) import Agda.Interaction.Options ( optCompileDir ) import Agda.Syntax.Common ( Nat, unArg, namedArg, NameId(..) ) import Agda.Syntax.Concrete.Name ( projectRoot , isNoName ) import Agda.Syntax.Abstract.Name ( ModuleName(MName), QName, mnameToConcrete, mnameToList, qnameName, qnameModule, isInModule, nameId ) import Agda.Syntax.Internal ( Name, Args, Type, conName, toTopLevelModuleName, arity, unEl, unAbs, nameFixity ) import Agda.Syntax.Position import Agda.Syntax.Literal ( Literal(LitNat,LitFloat,LitString,LitChar,LitQName,LitMeta) ) import Agda.Syntax.Fixity import qualified Agda.Syntax.Treeless as T import Agda.TypeChecking.Substitute ( absBody ) import Agda.TypeChecking.Level ( reallyUnLevelView ) import Agda.TypeChecking.Monad hiding (Global, Local) import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Monad.Debug ( reportSLn ) import Agda.TypeChecking.Monad.Options ( setCommandLineOptions ) import Agda.TypeChecking.Reduce ( instantiateFull, normalise ) import Agda.TypeChecking.Substitute (TelV(..)) import Agda.TypeChecking.Telescope import Agda.TypeChecking.Pretty import Agda.Utils.FileName ( filePath ) import Agda.Utils.Function ( iterate' ) import Agda.Utils.Maybe import Agda.Utils.Monad ( (<$>), (<*>), ifM ) import Agda.Utils.Pretty (prettyShow) import qualified Agda.Utils.Pretty as P import Agda.Utils.IO.Directory import Agda.Utils.IO.UTF8 ( writeFile ) import qualified Agda.Utils.HashMap as HMap import Agda.Compiler.Common import Agda.Compiler.ToTreeless import Agda.Compiler.Treeless.EliminateDefaults import Agda.Compiler.Treeless.EliminateLiteralPatterns import Agda.Compiler.Treeless.GuardsToPrims import Agda.Compiler.Backend (Backend(..), Backend'(..), Recompile(..)) import Agda.Compiler.JS.Syntax ( Exp(Self,Local,Global,Undefined,String,Char,Integer,Double,Lambda,Object,Apply,Lookup,If,BinOp,PlainJS), LocalId(LocalId), GlobalId(GlobalId), MemberId(MemberId), Export(Export), Module(Module), modName, expName, uses ) import Agda.Compiler.JS.Substitution ( curriedLambda, curriedApply, emp, subst, apply ) import qualified Agda.Compiler.JS.Pretty as JSPretty import Agda.Interaction.Options import Paths_Agda #include "undefined.h" import Agda.Utils.Impossible ( Impossible(Impossible), throwImpossible ) -------------------------------------------------- -- Entry point into the compiler -------------------------------------------------- jsBackend :: Backend jsBackend = Backend jsBackend' jsBackend' :: Backend' JSOptions JSOptions JSModuleEnv () (Maybe Export) jsBackend' = Backend' { backendName = jsBackendName , backendVersion = Nothing , options = defaultJSOptions , commandLineFlags = jsCommandLineFlags , isEnabled = optJSCompile , preCompile = jsPreCompile , postCompile = jsPostCompile , preModule = jsPreModule , postModule = jsPostModule , compileDef = jsCompileDef , scopeCheckingSuffices = False } --- Options --- data JSOptions = JSOptions { optJSCompile :: Bool } defaultJSOptions :: JSOptions defaultJSOptions = JSOptions { optJSCompile = False } jsCommandLineFlags :: [OptDescr (Flag JSOptions)] jsCommandLineFlags = [ Option [] ["js"] (NoArg enable) "compile program using the JS backend" ] where enable o = pure o{ optJSCompile = True } --- Top-level compilation --- jsPreCompile :: JSOptions -> TCM JSOptions jsPreCompile opts = return opts jsPostCompile :: JSOptions -> IsMain -> a -> TCM () jsPostCompile _ _ _ = copyRTEModules --- Module compilation --- type JSModuleEnv = Maybe CoinductionKit jsPreModule :: JSOptions -> ModuleName -> FilePath -> TCM (Recompile JSModuleEnv ()) jsPreModule _ m ifile = ifM uptodate noComp yesComp where uptodate = liftIO =<< isNewerThan <$> outFile_ <*> pure ifile noComp = do reportSLn "compile.js" 2 . (++ " : no compilation is needed.") . prettyShow =<< curMName return $ Skip () yesComp = do m <- prettyShow <$> curMName out <- outFile_ reportSLn "compile.js" 1 $ repl [m, ifile, out] "Compiling <<0>> in <<1>> to <<2>>" Recompile <$> coinductionKit jsPostModule :: JSOptions -> JSModuleEnv -> IsMain -> ModuleName -> [Maybe Export] -> TCM () jsPostModule _ _ isMain _ defs = do m <- jsMod <$> curMName is <- map (jsMod . fst) . iImportedModules <$> curIF let es = catMaybes defs writeModule $ Module m (reorder es) main where main = case isMain of IsMain -> Just $ Apply (Lookup Self $ MemberId "main") [Lambda 1 emp] NotMain -> Nothing jsCompileDef :: JSOptions -> JSModuleEnv -> Definition -> TCM (Maybe Export) jsCompileDef _ kit def = definition kit (defName def, def) -------------------------------------------------- -- Naming -------------------------------------------------- prefix :: [Char] prefix = "jAgda" jsMod :: ModuleName -> GlobalId jsMod m = GlobalId (prefix : map prettyShow (mnameToList m)) jsFileName :: GlobalId -> String jsFileName (GlobalId ms) = intercalate "." ms ++ ".js" jsMember :: Name -> MemberId jsMember n -- Anonymous fields are used for where clauses, -- and they're all given the concrete name "_", -- so we disambiguate them using their name id. | isNoName n = MemberId ("_" ++ show (nameId n)) | otherwise = MemberId $ prettyShow n -- Rather annoyingly, the anonymous construtor of a record R in module M -- is given the name M.recCon, but a named constructor C -- is given the name M.R.C, sigh. This causes a lot of hoop-jumping -- in the map from Agda names to JS names, which we patch by renaming -- anonymous constructors to M.R.record. global' :: QName -> TCM (Exp,[MemberId]) global' q = do i <- iModuleName <$> curIF modNm <- topLevelModuleName (qnameModule q) let qms = mnameToList $ qnameModule q nm = map jsMember (drop (length $ mnameToList modNm) qms ++ [qnameName q]) if modNm == i then return (Self, nm) else return (Global (jsMod modNm), nm) global :: QName -> TCM (Exp,[MemberId]) global q = do d <- getConstInfo q case d of Defn { theDef = Constructor { conData = p } } -> do e <- getConstInfo p case e of Defn { theDef = Record { recNamedCon = False } } -> do (m,ls) <- global' p return (m, ls ++ [MemberId "record"]) _ -> global' (defName d) _ -> global' (defName d) -- Reorder a list of exports to ensure def-before-use. -- Note that this can diverge in the case when there is no such reordering. -- Only top-level values are evaluated before definitions are added to the -- module, so we put those last, ordered in dependency order. There can't be -- any recursion between top-level values (unless termination checking has been -- disabled and someone's written a non-sensical program), so reordering will -- terminate. reorder :: [Export] -> [Export] reorder es = datas ++ funs ++ reorder' (Set.fromList $ map expName $ datas ++ funs) vals where (vs, funs) = partition isTopLevelValue es (datas, vals) = partition isEmptyObject vs reorder' :: Set [MemberId] -> [Export] -> [Export] reorder' defs [] = [] reorder' defs (e : es) = let us = uses e `difference` defs in case null us of True -> e : (reorder' (insert (expName e) defs) es) False -> reorder' defs (insertAfter us e es) isTopLevelValue :: Export -> Bool isTopLevelValue (Export _ e) = case e of Object m | flatName `Map.member` m -> False Lambda{} -> False _ -> True isEmptyObject :: Export -> Bool isEmptyObject (Export _ e) = case e of Object m -> Map.null m Lambda{} -> True _ -> False insertAfter :: Set [MemberId] -> Export -> [Export] -> [Export] insertAfter us e [] = [e] insertAfter us e (f:fs) | null us = e : f : fs insertAfter us e (f:fs) | otherwise = f : insertAfter (delete (expName f) us) e fs -------------------------------------------------- -- Main compiling clauses -------------------------------------------------- curModule :: IsMain -> TCM Module curModule isMain = do kit <- coinductionKit m <- (jsMod <$> curMName) is <- map jsMod <$> (map fst . iImportedModules <$> curIF) es <- catMaybes <$> (mapM (definition kit) =<< (sortDefs <$> curDefs)) return $ Module m (reorder es) main where main = case isMain of IsMain -> Just $ Apply (Lookup Self $ MemberId "main") [Lambda 1 emp] NotMain -> Nothing definition :: Maybe CoinductionKit -> (QName,Definition) -> TCM (Maybe Export) definition kit (q,d) = do reportSDoc "compile.js" 10 $ text "compiling def:" <+> prettyTCM q (_,ls) <- global q d <- instantiateFull d definition' kit q d (defType d) ls -- | Ensure that there is at most one pragma for a name. checkCompilerPragmas :: QName -> TCM () checkCompilerPragmas q = caseMaybeM (getUniqueCompilerPragma jsBackendName q) (return ()) $ \ (CompilerPragma r s) -> setCurrentRange r $ case words s of "=" : _ -> return () _ -> genericDocError $ P.sep [ P.text "Badly formed COMPILE JS pragma. Expected", P.text "{-# COMPILE JS = #-}" ] defJSDef :: Definition -> Maybe String defJSDef def = case defCompilerPragmas jsBackendName def of [CompilerPragma _ s] -> Just (dropEquals s) [] -> Nothing _:_:_ -> __IMPOSSIBLE__ where dropEquals = dropWhile $ \ c -> isSpace c || c == '=' definition' :: Maybe CoinductionKit -> QName -> Definition -> Type -> [MemberId] -> TCM (Maybe Export) definition' kit q d t ls = do checkCompilerPragmas q case theDef d of -- coinduction Constructor{} | Just q == (nameOfSharp <$> kit) -> do return Nothing Function{} | Just q == (nameOfFlat <$> kit) -> do ret $ Lambda 1 $ Apply (Lookup (local 0) flatName) [] Axiom | Just e <- defJSDef d -> plainJS e Axiom | otherwise -> ret Undefined Function{} | Just e <- defJSDef d -> plainJS e Function{} | otherwise -> do reportSDoc "compile.js" 5 $ text "compiling fun:" <+> prettyTCM q caseMaybeM (toTreeless q) (pure Nothing) $ \ treeless -> do funBody <- eliminateCaseDefaults =<< eliminateLiteralPatterns (convertGuards treeless) reportSDoc "compile.js" 30 $ text " compiled treeless fun:" <+> pretty funBody funBody' <- compileTerm funBody reportSDoc "compile.js" 30 $ text " compiled JS fun:" <+> (text . show) funBody' return $ Just $ Export ls funBody' Primitive{primName = p} | p `Set.member` primitives -> plainJS $ "agdaRTS." ++ p Primitive{} | Just e <- defJSDef d -> plainJS e Primitive{} | otherwise -> ret Undefined Datatype{} -> ret emp Record{} -> return Nothing Constructor{} | Just e <- defJSDef d -> plainJS e Constructor{conData = p, conPars = nc} | otherwise -> do np <- return (arity t - nc) d <- getConstInfo p case theDef d of Record { recFields = flds } -> ret (curriedLambda np (Object (fromList ( (last ls , Lambda 1 (Apply (Lookup (Local (LocalId 0)) (last ls)) [ Local (LocalId (np - i)) | i <- [0 .. np-1] ])) : (zip [ jsMember (qnameName (unArg fld)) | fld <- flds ] [ Local (LocalId (np - i)) | i <- [1 .. np] ]))))) _ -> ret (curriedLambda (np + 1) (Apply (Lookup (Local (LocalId 0)) (last ls)) [ Local (LocalId (np - i)) | i <- [0 .. np-1] ])) AbstractDefn{} -> __IMPOSSIBLE__ where ret = return . Just . Export ls plainJS = return . Just . Export ls . PlainJS compileTerm :: T.TTerm -> TCM Exp compileTerm t = do kit <- coinductionKit compileTerm' kit t compileTerm' :: Maybe CoinductionKit -> T.TTerm -> TCM Exp compileTerm' kit t = go t where go :: T.TTerm -> TCM Exp go t = case t of T.TVar x -> return $ Local $ LocalId x T.TDef q -> do d <- getConstInfo q case theDef d of -- Datatypes and records are erased Datatype {} -> return (String "*") Record {} -> return (String "*") _ -> qname q T.TApp (T.TCon q) [x] | Just q == (nameOfSharp <$> kit) -> do x <- go x let evalThunk = unlines [ "function() {" , " delete this.flat;" , " var result = this.__flat_helper();" , " delete this.__flat_helper;" , " this.flat = function() { return result; };" , " return result;" , "}" ] return $ Object $ Map.fromList [(flatName, PlainJS evalThunk) ,(MemberId "__flat_helper", Lambda 0 x)] T.TApp t xs -> curriedApply <$> go t <*> mapM go xs T.TLam t -> Lambda 1 <$> go t -- TODO This is not a lazy let, but it should be... T.TLet t e -> apply <$> (Lambda 1 <$> go e) <*> traverse go [t] T.TLit l -> return $ literal l T.TCon q -> do d <- getConstInfo q qname q T.TCase sc (T.CTData dt) def alts -> do dt <- getConstInfo dt alts' <- traverse compileAlt alts let obj = Object $ Map.fromList alts' case (theDef dt, defJSDef dt) of (_, Just e) -> do return $ apply (PlainJS e) [Local (LocalId sc), obj] (Record{}, _) -> do memId <- visitorName $ recCon $ theDef dt return $ apply (Lookup (Local $ LocalId sc) memId) [obj] (Datatype{}, _) -> do return $ curriedApply (Local (LocalId sc)) [obj] _ -> __IMPOSSIBLE__ T.TCase _ _ _ _ -> __IMPOSSIBLE__ T.TPrim p -> return $ compilePrim p T.TUnit -> unit T.TSort -> unit T.TErased -> unit T.TError T.TUnreachable -> return Undefined unit = return $ Integer 0 compilePrim :: T.TPrim -> Exp compilePrim p = case p of T.PIf -> curriedLambda 3 $ If (local 2) (local 1) (local 0) T.PEqI -> binOp "agdaRTS.uprimIntegerEqual" T.PEqF -> binOp "agdaRTS.uprimFloatEquality" T.PEqQ -> binOp "agdaRTS.uprimQNameEquality" p | T.isPrimEq p -> curriedLambda 2 $ BinOp (local 1) "===" (local 0) T.PGeq -> binOp "agdaRTS.uprimIntegerGreaterOrEqualThan" T.PLt -> binOp "agdaRTS.uprimIntegerLessThan" T.PAdd -> binOp "agdaRTS.uprimIntegerPlus" T.PSub -> binOp "agdaRTS.uprimIntegerMinus" T.PMul -> binOp "agdaRTS.uprimIntegerMultiply" T.PRem -> binOp "agdaRTS.uprimIntegerRem" T.PQuot -> binOp "agdaRTS.uprimIntegerQuot" T.PSeq -> binOp "agdaRTS.primSeq" _ -> __IMPOSSIBLE__ where binOp js = curriedLambda 2 $ apply (PlainJS js) [local 1, local 0] compileAlt :: T.TAlt -> TCM (MemberId, Exp) compileAlt a = case a of T.TACon con ar body -> do memId <- visitorName con body <- Lambda ar <$> compileTerm body return (memId, body) _ -> __IMPOSSIBLE__ visitorName :: QName -> TCM MemberId visitorName q = do (m,ls) <- global q; return (last ls) flatName :: MemberId flatName = MemberId "flat" local :: Nat -> Exp local = Local . LocalId qname :: QName -> TCM Exp qname q = do (e,ls) <- global q return (foldl Lookup e ls) literal :: Literal -> Exp literal l = case l of (LitNat _ x) -> Integer x (LitFloat _ x) -> Double x (LitString _ x) -> String x (LitChar _ x) -> Char x (LitQName _ x) -> litqname x LitMeta{} -> __IMPOSSIBLE__ litqname :: QName -> Exp litqname q = Object $ Map.fromList [ (mem "id", Integer $ fromIntegral n) , (mem "moduleId", Integer $ fromIntegral m) , (mem "name", String $ prettyShow q) , (mem "fixity", litfixity fx)] where mem = MemberId NameId n m = nameId $ qnameName q fx = theFixity $ nameFixity $ qnameName q litfixity :: Fixity -> Exp litfixity fx = Object $ Map.fromList [ (mem "assoc", litAssoc $ fixityAssoc fx) , (mem "prec", litPrec $ fixityLevel fx)] -- TODO this will probably not work well together with the necessary FFI bindings litAssoc NonAssoc = String "non-assoc" litAssoc LeftAssoc = String "left-assoc" litAssoc RightAssoc = String "right-assoc" litPrec Unrelated = String "unrelated" litPrec (Related l) = Integer l -------------------------------------------------- -- Writing out an ECMAScript module -------------------------------------------------- writeModule :: Module -> TCM () writeModule m = do out <- outFile (modName m) liftIO (writeFile out (JSPretty.pretty 0 0 m)) outFile :: GlobalId -> TCM FilePath outFile m = do mdir <- compileDir let (fdir, fn) = splitFileName (jsFileName m) let dir = mdir fdir fp = dir fn liftIO $ createDirectoryIfMissing True dir return fp outFile_ :: TCM FilePath outFile_ = do m <- curMName outFile (jsMod m) copyRTEModules :: TCM () copyRTEModules = do dataDir <- lift getDataDir let srcDir = dataDir "JS" (lift . copyDirContent srcDir) =<< compileDir -- | Primitives implemented in the JS Agda RTS. primitives :: Set String primitives = Set.fromList [ "primExp" , "primFloatDiv" , "primFloatEquality" , "primFloatNumericalEquality" , "primFloatNumericalLess" , "primFloatNegate" , "primFloatMinus" , "primFloatPlus" , "primFloatSqrt" , "primFloatTimes" , "primNatMinus" , "primShowFloat" , "primShowInteger" , "primSin" , "primCos" , "primTan" , "primASin" , "primACos" , "primATan" , "primATan2" , "primShowQName" , "primQNameEquality" , "primQNameLess" , "primQNameFixity" ] Agda-2.5.3/src/full/Agda/Termination/0000755000000000000000000000000013154613124015421 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Termination/TermCheck.hs0000644000000000000000000014072013154613124017626 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE ImplicitParams #-} {-# LANGUAGE NondecreasingIndentation #-} {- Checking for Structural recursion Authors: Andreas Abel, Nils Anders Danielsson, Ulf Norell, Karl Mehltretter and others Created: 2007-05-28 Source : TypeCheck.Rules.Decl -} module Agda.Termination.TermCheck ( termDecl , termMutual , Result ) where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad.Reader import Control.Monad.State import Data.Foldable (toList) import qualified Data.List as List import Data.Monoid hiding ((<>)) import qualified Data.Set as Set import Data.Traversable (Traversable, traverse) import Agda.Syntax.Abstract (IsProjP(..), AllNames(..)) import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Pattern as I import Agda.Syntax.Internal.Generic import qualified Agda.Syntax.Info as Info import Agda.Syntax.Position import Agda.Syntax.Common import Agda.Syntax.Translation.InternalToAbstract ( reifyPatterns ) import Agda.Termination.CutOff import Agda.Termination.Monad import Agda.Termination.CallGraph hiding (toList) import qualified Agda.Termination.CallGraph as CallGraph import Agda.Termination.CallMatrix hiding (toList) import Agda.Termination.Order as Order import qualified Agda.Termination.SparseMatrix as Matrix import Agda.Termination.Termination (endos, idempotent) import qualified Agda.Termination.Termination as Term import Agda.Termination.RecCheck import Agda.Termination.Inlining import Agda.TypeChecking.Datatypes import Agda.TypeChecking.EtaContract import Agda.TypeChecking.Functions import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Pretty import Agda.TypeChecking.Records -- (isRecordConstructor, isInductiveRecord) import Agda.TypeChecking.Reduce (reduce, normalise, instantiate, instantiateFull) import Agda.TypeChecking.SizedTypes import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import qualified Agda.Benchmarking as Benchmark import Agda.TypeChecking.Monad.Benchmark (billTo, billPureTo) import Agda.Interaction.Options import Agda.Utils.Either import Agda.Utils.Function import Agda.Utils.Functor (($>), (<.>)) import Agda.Utils.List import Agda.Utils.Size import Agda.Utils.Maybe import Agda.Utils.Monad -- (mapM', forM', ifM, or2M, and2M) import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Pretty (prettyShow) import Agda.Utils.Singleton import qualified Agda.Utils.VarSet as VarSet #include "undefined.h" import Agda.Utils.Impossible -- | Call graph with call info for composed calls. type Calls = CallGraph CallPath -- | The result of termination checking a module. -- Must be a 'Monoid' and have 'Singleton'. type Result = [TerminationError] -- | Entry point: Termination check a single declaration. -- -- Precondition: 'envMutualBlock' must be set correctly. termDecl :: A.Declaration -> TCM Result termDecl d = inTopContext $ termDecl' d -- | Termination check a single declaration -- (without necessarily ignoring @abstract@). termDecl' :: A.Declaration -> TCM Result termDecl' d = case d of A.Axiom {} -> return mempty A.Field {} -> return mempty A.Primitive {} -> return mempty A.Mutual _ ds | [A.RecSig{}, A.RecDef _ _ _ _ _ _ _ rds] <- unscopeDefs ds -> termDecls rds A.Mutual i ds -> termMutual $ getNames ds A.Section _ _ _ ds -> termDecls ds -- section structure can be ignored as we are termination checking -- definitions lifted to the top-level A.Apply {} -> return mempty A.Import {} -> return mempty A.Pragma {} -> return mempty A.Open {} -> return mempty A.PatternSynDef {} -> return mempty -- open and pattern synonym defs are just artifacts from the concrete syntax A.ScopedDecl scope ds -> {- withScope_ scope $ -} termDecls ds -- scope is irrelevant as we are termination checking Syntax.Internal A.RecSig{} -> return mempty A.RecDef _ r _ _ _ _ _ ds -> termDecls ds -- These should all be wrapped in mutual blocks A.FunDef{} -> __IMPOSSIBLE__ A.DataSig{} -> __IMPOSSIBLE__ A.DataDef{} -> __IMPOSSIBLE__ A.UnquoteDecl{} -> __IMPOSSIBLE__ A.UnquoteDef{} -> __IMPOSSIBLE__ where termDecls ds = concat <$> mapM termDecl' ds unscopeDefs = concatMap unscopeDef unscopeDef (A.ScopedDecl _ ds) = unscopeDefs ds unscopeDef d = [d] -- The mutual names mentioned in the abstract syntax -- for symbols that need to be termination-checked. getNames = concatMap getName getName (A.FunDef i x delayed cs) = [x] getName (A.RecDef _ _ _ _ _ _ _ ds) = getNames ds getName (A.Mutual _ ds) = getNames ds getName (A.Section _ _ _ ds) = getNames ds getName (A.ScopedDecl _ ds) = getNames ds getName (A.UnquoteDecl _ _ xs _) = xs getName (A.UnquoteDef _ xs _) = xs getName _ = [] -- | Entry point: Termination check the current mutual block. termMutual :: [QName] -- ^ The function names defined in this block on top-level. -- (For error-reporting only.) -> TCM Result termMutual names0 = ifNotM (optTerminationCheck <$> pragmaOptions) (return mempty) $ {-else-} inTopContext $ disableDestructiveUpdate $ do -- Get set of mutually defined names from the TCM. -- This includes local and auxiliary functions introduced -- during type-checking. mid <- fromMaybe __IMPOSSIBLE__ <$> asks envMutualBlock mutualBlock <- lookupMutualBlock mid let allNames = Set.elems $ mutualNames mutualBlock names = if null names0 then allNames else names0 i = mutualInfo mutualBlock -- Andreas, 2014-03-26 -- Keeping recursion check after experiments on the standard lib. -- Seems still to save 1s. -- skip = return False -- No need to term-check if the declarations are acyclic! skip = not <$> do -- Andreas, 2016-10-01 issue #2231 -- Recursivity checker has to see through abstract definitions! ignoreAbstractMode $ do billTo [Benchmark.Termination, Benchmark.RecCheck] $ recursive allNames -- -- Andreas, 2017-03-24, use positivity info to skip non-recursive functions -- skip = ignoreAbstractMode $ allM allNames $ \ x -> do -- null <$> getMutual x -- PROBLEMS with test/Succeed/AbstractCoinduction.agda -- We set the range to avoid panics when printing error messages. setCurrentRange i $ do reportSLn "term.mutual" 10 $ "Termination checking " ++ prettyShow allNames -- NO_TERMINATION_CHECK if (Info.mutualTermCheck i `elem` [ NoTerminationCheck, Terminating ]) then do reportSLn "term.warn.yes" 2 $ "Skipping termination check for " ++ prettyShow names forM_ allNames $ \ q -> setTerminates q True -- considered terminating! return mempty -- NON_TERMINATING else if (Info.mutualTermCheck i == NonTerminating) then do reportSLn "term.warn.yes" 2 $ "Considering as non-terminating: " ++ prettyShow names forM_ allNames $ \ q -> setTerminates q False return mempty -- Trivially terminating (non-recursive) else ifM skip (do reportSLn "term.warn.yes" 2 $ "Trivially terminating: " ++ prettyShow names forM_ allNames $ \ q -> setTerminates q True return mempty) $ {- else -} do -- Set the mutual names in the termination environment. let setNames e = e { terMutual = allNames , terUserNames = names } runTerm cont = runTerDefault $ do cutoff <- terGetCutOff reportSLn "term.top" 10 $ "Termination checking " ++ prettyShow names ++ " with cutoff=" ++ show cutoff ++ "..." terLocal setNames cont -- New check currently only makes a difference for copatterns. -- Since it is slow, only invoke it if -- any of the definitions uses copatterns. res <- ifM (orM $ map usesCopatterns allNames) -- Then: New check, one after another. (runTerm $ forM' allNames $ termFunction) -- Else: Old check, all at once. (runTerm $ termMutual') -- Record result of termination check in signature. -- If there are some termination errors, we collect them in -- the state and mark the definition as non-terminating so -- that it does not get unfolded let terminates = null res forM_ allNames $ \ q -> setTerminates q terminates return res -- | @termMutual'@ checks all names of the current mutual block, -- henceforth called @allNames@, for termination. -- -- @allNames@ is taken from 'Internal' syntax, it contains also -- the definitions created by the type checker (e.g., with-functions). termMutual' :: TerM Result termMutual' = do -- collect all recursive calls in the block allNames <- terGetMutual let collect = forM' allNames termDef -- first try to termination check ignoring the dot patterns calls1 <- collect reportCalls "no " calls1 cutoff <- terGetCutOff let ?cutoff = cutoff r <- billToTerGraph $ Term.terminates calls1 r <- case r of r@Right{} -> return r Left{} -> do -- Try again, but include the dot patterns this time. calls2 <- terSetUseDotPatterns True $ collect reportCalls "" calls2 billToTerGraph $ Term.terminates calls2 -- @names@ is taken from the 'Abstract' syntax, so it contains only -- the names the user has declared. This is for error reporting. names <- terGetUserNames case r of Left calls -> return $ singleton $ terminationError names $ callInfos calls Right{} -> do liftTCM $ reportSLn "term.warn.yes" 2 $ prettyShow (names) ++ " does termination check" return mempty -- | Smart constructor for 'TerminationError'. -- Removes 'termErrFunctions' that are not mentioned in 'termErrCalls'. terminationError :: [QName] -> [CallInfo] -> TerminationError terminationError names calls = TerminationError names' calls where names' = names `List.intersect` toList (allNames calls) billToTerGraph :: a -> TerM a billToTerGraph a = liftTCM $ billPureTo [Benchmark.Termination, Benchmark.Graph] a -- | @reportCalls@ for debug printing. -- -- Replays the call graph completion for debugging. reportCalls :: String -> Calls -> TerM () reportCalls no calls = do cutoff <- terGetCutOff let ?cutoff = cutoff -- We work in TCM exclusively. liftTCM $ do reportS "term.lex" 20 $ unlines [ "Calls (" ++ no ++ "dot patterns): " ++ prettyShow calls ] -- Print the whole completion phase. verboseS "term.matrices" 40 $ do let header s = unlines [ replicate n '=' , replicate k '=' ++ s ++ replicate k' '=' , replicate n '=' ] where n = 70 r = n - length s k = r `div` 2 k' = r - k let report s cs = reportSDoc "term.matrices" 40 $ vcat [ text $ header s , nest 2 $ pretty cs ] cs0 = calls step cs = do let (new, cs') = completionStep cs0 cs report " New call matrices " new return $ if null new then Left () else Right cs' report " Initial call matrices " cs0 trampolineM step cs0 -- Print the result of completion. let calls' = CallGraph.complete calls idems = filter idempotent $ endos $ CallGraph.toList calls' -- TODO -- reportSDoc "term.behaviours" 20 $ vcat -- [ text $ "Recursion behaviours (" ++ no ++ "dot patterns):" -- , nest 2 $ return $ Term.prettyBehaviour calls' -- ] reportSDoc "term.matrices" 30 $ vcat [ text $ "Idempotent call matrices (" ++ no ++ "dot patterns):\n" , nest 2 $ vcat $ punctuate (text "\n") $ map pretty idems ] -- reportSDoc "term.matrices" 30 $ vcat -- [ text $ "Other call matrices (" ++ no ++ "dot patterns):" -- , nest 2 $ pretty $ CallGraph.fromList others -- ] return () -- | @termFunction name@ checks @name@ for termination. termFunction :: QName -> TerM Result termFunction name = do -- Function @name@ is henceforth referred to by its @index@ -- in the list of @allNames@ of the mutual block. allNames <- terGetMutual let index = fromMaybe __IMPOSSIBLE__ $ List.elemIndex name allNames -- Retrieve the target type of the function to check. target <- liftTCM $ do typeEndsInDef =<< typeOfConst name reportTarget target terSetTarget target $ do -- Collect the recursive calls in the block which (transitively) -- involve @name@, -- taking the target of @name@ into account for computing guardedness. let collect = (`trampolineM` (Set.singleton index, mempty, mempty)) $ \ (todo, done, calls) -> do if null todo then return $ Left calls else do -- Extract calls originating from indices in @todo@. new <- forM' todo $ \ i -> termDef $ fromMaybe __IMPOSSIBLE__ $ allNames !!! i -- Mark those functions as processed and add the calls to the result. let done' = done `mappend` todo calls' = new `mappend` calls -- Compute the new todo list: todo' = CallGraph.targetNodes new Set.\\ done' -- Jump the trampoline. return $ Right (todo', done', calls') -- First try to termination check ignoring the dot patterns calls1 <- terSetUseDotPatterns False $ collect reportCalls "no " calls1 r <- do cutoff <- terGetCutOff let ?cutoff = cutoff r <- billToTerGraph $ Term.terminatesFilter (== index) calls1 case r of Right () -> return $ Right () Left{} -> do -- Try again, but include the dot patterns this time. calls2 <- terSetUseDotPatterns True $ collect reportCalls "" calls2 billToTerGraph $ mapLeft callInfos $ Term.terminatesFilter (== index) calls2 names <- terGetUserNames case r of Left calls -> return $ singleton $ terminationError ([name] `List.intersect` names) calls Right () -> do liftTCM $ reportSLn "term.warn.yes" 2 $ prettyShow name ++ " does termination check" return mempty where reportTarget r = liftTCM $ reportSLn "term.target" 20 $ " target type " ++ caseMaybe r "not recognized" (\ q -> "ends in " ++ prettyShow q) -- | To process the target type. typeEndsInDef :: MonadTCM tcm => Type -> tcm (Maybe QName) typeEndsInDef t = liftTCM $ do TelV _ core <- telView t case ignoreSharing $ unEl core of Def d vs -> return $ Just d _ -> return Nothing -- | Termination check a definition by pattern matching. -- -- TODO: Refactor! -- As this function may be called twice, -- once disregarding dot patterns, -- the second time regarding dot patterns, -- it is better if we separated bare call extraction -- from computing the change in structural order. -- Only the latter depends on the choice whether we -- consider dot patterns or not. termDef :: QName -> TerM Calls termDef name = terSetCurrent name $ inConcreteOrAbstractMode name $ \ def -> do -- Retrieve definition let t = defType def liftTCM $ reportSDoc "term.def.fun" 5 $ sep [ text "termination checking body of" <+> prettyTCM name , nest 2 $ text ":" <+> prettyTCM t ] -- If --without-K, we disregard all arguments (and result) -- which are not of data or record type. withoutKEnabled <- liftTCM $ optWithoutK <$> pragmaOptions applyWhen withoutKEnabled (setMasks t) $ do -- If the result should be disregarded, set all calls to unguarded. applyWhenM terGetMaskResult terUnguarded $ do case theDef def of Function{ funClauses = cls, funDelayed = delayed } -> terSetDelayed delayed $ forM' cls $ termClause _ -> return empty -- | Mask arguments and result for termination checking -- according to type of function. -- Only arguments of types ending in data/record or Size are counted in. setMasks :: Type -> TerM a -> TerM a setMasks t cont = do (ds, d) <- liftTCM $ do TelV tel core <- telView t -- Check argument types ds <- forM (telToList tel) $ \ t -> do TelV _ t <- telView $ snd $ unDom t d <- (isNothing <$> isDataOrRecord (unEl t)) `or2M` (isJust <$> isSizeType t) when d $ reportSDoc "term.mask" 20 $ do text "argument type " <+> prettyTCM t <+> text " is not data or record type, ignoring structural descent for --without-K" return d -- Check result types d <- isNothing <.> isDataOrRecord . unEl $ core when d $ reportSLn "term.mask" 20 $ "result type is not data or record type, ignoring guardedness for --without-K" return (ds, d) terSetMaskArgs (ds ++ repeat True) $ terSetMaskResult d $ cont -- | Is the current target type among the given ones? targetElem :: [Target] -> TerM Bool targetElem ds = maybe False (`elem` ds) <$> terGetTarget {- -- | The target type of the considered recursive definition. data Target = Set -- ^ Constructing a Set (only meaningful with 'guardingTypeConstructors'). | Data QName -- ^ Constructing a coinductive or mixed type (could be data or record). deriving (Eq, Show) -- | Check wether a 'Target" corresponds to the current one. matchingTarget :: DBPConf -> Target -> TCM Bool matchingTarget conf t = maybe (return True) (match t) (currentTarget conf) where match Set Set = return True match (Data d) (Data d') = mutuallyRecursive d d' match _ _ = return False -} -- | Convert a term (from a dot pattern) to a DeBruijn pattern. -- -- The term is first normalized and stripped of all non-coinductive projections. termToDBP :: Term -> TerM DeBruijnPattern termToDBP t = ifNotM terGetUseDotPatterns (return unusedVar) $ {- else -} do termToPattern =<< do liftTCM $ stripAllProjections =<< normalise t -- | Convert a term (from a dot pattern) to a pattern for the purposes of the termination checker. -- -- @SIZESUC@ is treated as a constructor. class TermToPattern a b where termToPattern :: a -> TerM b default termToPattern :: (TermToPattern a' b', Traversable f, a ~ f a', b ~ f b') => a -> TerM b termToPattern = traverse termToPattern instance TermToPattern a b => TermToPattern [a] [b] where instance TermToPattern a b => TermToPattern (Arg a) (Arg b) where instance TermToPattern a b => TermToPattern (Named c a) (Named c b) where -- OVERLAPPING -- instance TermToPattern a b => TermToPattern a (Named c b) where -- termToPattern t = unnamed <$> termToPattern t instance TermToPattern Term DeBruijnPattern where termToPattern t = (liftTCM $ ignoreSharing <$> constructorForm t) >>= \case -- Constructors. Con c _ args -> ConP c noConPatternInfo . map (fmap unnamed) <$> termToPattern args Def s [Apply arg] -> do suc <- terGetSizeSuc if Just s == suc then ConP (ConHead s Inductive []) noConPatternInfo . map (fmap unnamed) <$> termToPattern [arg] else return $ DotP t DontCare t -> termToPattern t -- OR: __IMPOSSIBLE__ -- removed by stripAllProjections -- Leaves. Var i [] -> VarP . (`DBPatVar` i) . prettyShow <$> nameOfBV i Lit l -> return $ LitP l t -> return $ DotP t -- | Masks all non-data/record type patterns if --without-K. -- See issue #1023. maskNonDataArgs :: [DeBruijnPattern] -> TerM [Masked DeBruijnPattern] maskNonDataArgs ps = zipWith mask ps <$> terGetMaskArgs where mask p@ProjP{} _ = Masked False p mask p d = Masked d p -- | Extract recursive calls from one clause. termClause :: Clause -> TerM Calls termClause clause = do -- If with-function inlining is disallowed (e.g. --without-K), -- we check the original clause. let fallback = termClause' clause ifNotM (terGetInlineWithFunctions) fallback $ {- else -} do -- Otherwise, we will do inlining, hence, can skip with-generated functions. name <- terGetCurrent ifM (isJust <$> isWithFunction name) (return mempty) $ {- else -} do -- With inlining, the termination check for all subordinated -- with-functions is included in the parent function. (liftTCM $ inlineWithClauses name clause) >>= \case Nothing -> fallback Just cls -> terSetHaveInlinedWith $ mapM' termClause' cls termClause' :: Clause -> TerM Calls termClause' clause = do Clause{ clauseTel = tel, namedClausePats = ps, clauseBody = body } <- etaExpandClause clause liftTCM $ reportSDoc "term.check.clause" 25 $ vcat [ text "termClause" , nest 2 $ text "tel =" <+> prettyTCM tel , nest 2 $ text "ps =" <+> do addContext tel $ prettyTCMPatternList ps ] forM' body $ \ v -> addContext tel $ do -- TODO: combine the following two traversals, avoid full normalisation. -- Parse dot patterns as patterns as far as possible. ps <- postTraversePatternM parseDotP ps -- Blank out coconstructors. ps <- preTraversePatternM stripCoCon ps -- Mask non-data arguments. mdbpats <- maskNonDataArgs $ map namedArg ps terSetPatterns mdbpats $ do terSetSizeDepth tel $ do reportBody v extract v where parseDotP = \case DotP t -> termToDBP t p -> return p stripCoCon p = case p of ConP (ConHead c _ _) _ _ -> do ifM ((Just c ==) <$> terGetSizeSuc) (return p) $ {- else -} do whatInduction c >>= \case Inductive -> return p CoInductive -> return unusedVar _ -> return p reportBody :: Term -> TerM () reportBody v = verboseS "term.check.clause" 6 $ do f <- terGetCurrent delayed <- terGetDelayed pats <- terGetPatterns liftTCM $ reportSDoc "term.check.clause" 6 $ do sep [ text ("termination checking " ++ (if delayed == Delayed then "delayed " else "") ++ "clause of") <+> prettyTCM f , nest 2 $ text "lhs:" <+> sep (map prettyTCM pats) , nest 2 $ text "rhs:" <+> prettyTCM v ] -- | Extract recursive calls from expressions. class ExtractCalls a where extract :: a -> TerM Calls instance ExtractCalls a => ExtractCalls (Abs a) where extract (NoAbs _ a) = extract a extract (Abs x a) = addContext x $ terRaise $ extract a instance ExtractCalls a => ExtractCalls (Arg a) where extract = extract . unArg instance ExtractCalls a => ExtractCalls (Dom a) where extract = extract . unDom instance ExtractCalls a => ExtractCalls (Elim' a) where extract Proj{} = return empty extract (Apply a) = extract $ unArg a instance ExtractCalls a => ExtractCalls [a] where extract = mapM' extract instance (ExtractCalls a, ExtractCalls b) => ExtractCalls (a,b) where extract (a, b) = CallGraph.union <$> extract a <*> extract b -- | Sorts can contain arbitrary terms of type @Level@, -- so look for recursive calls also in sorts. -- Ideally, 'Sort' would not be its own datatype but just -- a subgrammar of 'Term', then we would not need this boilerplate. instance ExtractCalls Sort where extract s = do liftTCM $ do reportSDoc "term.sort" 20 $ text "extracting calls from sort" <+> prettyTCM s reportSDoc "term.sort" 50 $ text ("s = " ++ show s) case s of Prop -> return empty Inf -> return empty SizeUniv -> return empty Type t -> terUnguarded $ extract t -- no guarded levels DLub s1 s2 -> extract (s1, s2) -- | Extract recursive calls from a type. instance ExtractCalls Type where extract (El s t) = extract (s, t) -- | Extract recursive calls from a constructor application. constructor :: QName -- ^ Constructor name. -> Induction -- ^ Should the constructor be treated as inductive or coinductive? -> [(Arg Term, Bool)] -- ^ All the arguments, -- and for every argument a boolean which is 'True' iff the -- argument should be viewed as preserving guardedness. -> TerM Calls constructor c ind args = do cutoff <- terGetCutOff let ?cutoff = cutoff forM' args $ \ (arg, preserves) -> do let g' = case (preserves, ind) of (True, Inductive) -> id (True, CoInductive) -> (Order.lt .*.) (False, _) -> const Order.unknown terModifyGuarded g' $ extract arg -- | Handle guardedness preserving type constructor. guardPresTyCon :: QName -> Elims -> (QName -> Elims -> TerM Calls) -> TerM Calls guardPresTyCon g es cont = do ifNotM (terGetGuardingTypeConstructors) (cont g es) $ {- else -} do def <- liftTCM $ getConstInfo g let occs = defArgOccurrences def preserves = (StrictPos <=) -- Data or record type constructor. con = constructor g Inductive $ -- guardedness preserving zip (argsFromElims es) (map preserves occs ++ repeat False) case theDef def of Datatype{} -> con Record{} -> con _ -> cont g es -- | Extract calls from with function application. withFunction :: QName -> Elims -> TerM Calls withFunction g es = do v <- liftTCM $ -- billTo [Benchmark.Termination, Benchmark.With] $ -- 0ms expandWithFunctionCall g es liftTCM $ reportSDoc "term.with.call" 30 $ text "termination checking expanded with-function call:" <+> prettyTCM v extract v -- | Handles function applications @g es@. function :: QName -> Elims -> TerM Calls function g es0 = ifM (terGetInlineWithFunctions `and2M` do isJust <$> isWithFunction g) (withFunction g es0) $ {-else, no with function-} do f <- terGetCurrent names <- terGetMutual guarded <- terGetGuarded -- let gArgs = Def g es0 liftTCM $ reportSDoc "term.function" 30 $ text "termination checking function call " <+> prettyTCM (Def g es0) -- First, look for calls in the arguments of the call gArgs. -- We have to reduce constructors in case they're reexported. -- Andreas, Issue 1530: constructors have to be reduced deep inside terms, -- thus, we need to use traverseTermM. Sharing is handled by traverseTermM, -- so no ignoreSharing needed here. let (reduceCon :: Term -> TCM Term) = traverseTermM $ \ t -> case t of Con c ci vs -> (`apply` vs) <$> reduce (Con c ci []) -- make sure we don't reduce the arguments _ -> return t -- Reduce constructors only when this call is actually a recursive one. -- es <- liftTCM $ billTo [Benchmark.Termination, Benchmark.Reduce] $ forM es $ -- etaContract <=< traverse reduceCon <=< instantiateFull -- If the function is a projection but not for a coinductive record, -- then preserve guardedness for its principal argument. isProj <- isProjectionButNotCoinductive g let unguards = repeat Order.unknown let guards = applyWhen isProj (guarded :) unguards -- Collect calls in the arguments of this call. let args = map unArg $ argsFromElims es0 calls <- forM' (zip guards args) $ \ (guard, a) -> do terSetGuarded guard $ extract a -- Then, consider call gArgs itself. liftTCM $ reportSDoc "term.found.call" 20 $ sep [ text "found call from" <+> prettyTCM f , nest 2 $ text "to" <+> prettyTCM g ] -- insert this call into the call list case List.elemIndex g names of -- call leads outside the mutual block and can be ignored Nothing -> return calls -- call is to one of the mutally recursive functions Just gInd -> do delayed <- terGetDelayed pats <- terGetPatterns -- Andreas, 2017-02-14, issue #2458: -- If we have inlined with-functions, we could be illtyped, -- hence, do not reduce anything. -- Andreas, 2017-06-20 issue #2613: -- We still need to reduce constructors, even when with-inlining happened. es <- -- ifM terGetHaveInlinedWith (return es0) {-else-} $ liftTCM $ forM es0 $ etaContract <=< traverse reduceCon <=< instantiateFull -- 2017-05-16, issue #2403: Argument normalization is too expensive, -- even if we only expand non-recursive functions. -- Argument normalization TURNED OFF. -- liftTCM $ billTo [Benchmark.Termination, Benchmark.Reduce] $ do -- -- Andreas, 2017-01-13, issue #2403, normalize arguments for the structural ordering. -- -- Andreas, 2017-03-25, issue #2495, restrict this to non-recursive functions -- -- otherwise, the termination checking may run forever. -- reportSLn "term.reduce" 90 $ "normalizing call arguments" -- modifyAllowedReductions (List.\\ [UnconfirmedReductions,RecursiveReductions]) $ -- forM es0 $ \ e -> do -- reportSDoc "term.reduce" 95 $ text "normalizing " <+> prettyTCM e -- etaContract =<< normalise e -- Compute the call matrix. -- Andreas, 2014-03-26 only 6% of termination time for library test -- spent on call matrix generation (nrows, ncols, matrix) <- billTo [Benchmark.Termination, Benchmark.Compare] $ compareArgs es -- only a delayed definition can be guarded let ifDelayed o | Order.decreasing o && delayed == NotDelayed = Order.le | otherwise = o liftTCM $ reportSLn "term.guardedness" 20 $ "composing with guardedness " ++ prettyShow guarded ++ " counting as " ++ prettyShow (ifDelayed guarded) cutoff <- terGetCutOff let ?cutoff = cutoff let matrix' = composeGuardedness (ifDelayed guarded) matrix -- Andreas, 2013-04-26 FORBIDDINGLY expensive! -- This PrettyTCM QName cost 50% of the termination time for std-lib!! -- gPretty <-liftTCM $ billTo [Benchmark.Termination, Benchmark.Level] $ -- render <$> prettyTCM g -- Andreas, 2013-05-19 as pointed out by Andrea Vezzosi, -- printing the call eagerly is forbiddingly expensive. -- So we build a closure such that we can print the call -- whenever we really need to. -- This saves 30s (12%) on the std-lib! -- Andreas, 2015-01-21 Issue 1410: Go to the module where g is defined -- otherwise its free variables with be prepended to the call -- in the error message. doc <- liftTCM $ withCurrentModule (qnameModule g) $ buildClosure $ Def g $ filter ((/= Inserted) . getOrigin) es0 -- Andreas, 2017-01-05, issue #2376 -- Remove arguments inserted by etaExpandClause. let src = fromMaybe __IMPOSSIBLE__ $ List.elemIndex f names tgt = gInd cm = makeCM ncols nrows matrix' info = CallPath [CallInfo { callInfoTarget = g , callInfoRange = getRange g , callInfoCall = doc }] liftTCM $ reportSDoc "term.kept.call" 5 $ vcat [ text "kept call from" <+> text (prettyShow f) <+> hsep (map prettyTCM pats) , nest 2 $ text "to" <+> text (prettyShow g) <+> hsep (map (parens . prettyTCM) args) , nest 2 $ text "call matrix (with guardedness): " , nest 2 $ pretty cm ] return $ CallGraph.insert src tgt cm info calls -- | Extract recursive calls from a term. instance ExtractCalls Term where extract t = do liftTCM $ reportSDoc "term.check.term" 50 $ do text "looking for calls in" <+> prettyTCM t -- Instantiate top-level MetaVar. t <- liftTCM $ instantiate t case ignoreSharing t of -- Constructed value. Con ConHead{conName = c} _ args -> do -- A constructor preserves the guardedness of all its arguments. let argsg = zip args $ repeat True -- If we encounter a coinductive record constructor -- in a type mutual with the current target -- then we count it as guarding. ind <- ifM ((Just c ==) <$> terGetSharp) (return CoInductive) $ do caseMaybeM (liftTCM $ isRecordConstructor c) (return Inductive) $ \ (q, def) -> do reportSLn "term.check.term" 50 $ "constructor " ++ prettyShow c ++ " has record type " ++ prettyShow q (\ b -> if b then CoInductive else Inductive) <$> andM [ return $ recInduction def == Just CoInductive , targetElem . fromMaybe __IMPOSSIBLE__ $ recMutual def ] constructor c ind argsg -- Function, data, or record type. Def g es -> guardPresTyCon g es function -- Abstraction. Preserves guardedness. Lam h b -> extract b -- Neutral term. Destroys guardedness. Var i es -> terUnguarded $ extract es -- Dependent function space. Pi a (Abs x b) -> CallGraph.union <$> (terUnguarded $ extract a) <*> do a <- maskSizeLt a -- OR: just do not add a to the context! terPiGuarded $ addContext (x, a) $ terRaise $ extract b -- Non-dependent function space. Pi a (NoAbs _ b) -> CallGraph.union <$> terUnguarded (extract a) <*> terPiGuarded (extract b) -- Literal. Lit l -> return empty -- Sort. Sort s -> extract s -- Unsolved metas are not considered termination problems, there -- will be a warning for them anyway. MetaV x args -> return empty -- Erased and not-yet-erased proof. DontCare t -> extract t -- Level. Level l -> -- billTo [Benchmark.Termination, Benchmark.Level] $ do -- Andreas, 2014-03-26 Benchmark discontinued, < 0.3% spent on levels. extract l Shared{} -> __IMPOSSIBLE__ -- | Extract recursive calls from level expressions. deriving instance ExtractCalls Level instance ExtractCalls PlusLevel where extract (ClosedLevel n) = return $ mempty extract (Plus n l) = extract l instance ExtractCalls LevelAtom where extract (MetaLevel x es) = extract es extract (BlockedLevel x t) = extract t extract (NeutralLevel _ t) = extract t extract (UnreducedLevel t) = extract t -- | Rewrite type @tel -> Size< u@ to @tel -> Size@. maskSizeLt :: MonadTCM tcm => Dom Type -> tcm (Dom Type) maskSizeLt dom@(Dom info a) = liftTCM $ do (msize, msizelt) <- getBuiltinSize case (msize, msizelt) of (_ , Nothing) -> return dom (Nothing, _) -> __IMPOSSIBLE__ (Just size, Just sizelt) -> do TelV tel c <- telView a case ignoreSharingType a of El s (Def d [v]) | d == sizelt -> return $ Dom info $ abstract tel $ El s $ Def size [] _ -> return dom {- | @compareArgs es@ Compare the list of de Bruijn patterns (=parameters) @pats@ with a list of arguments @es@ and create a call maxtrix with |es| rows and |pats| columns. The guardedness is the number of projection patterns in @pats@ minus the number of projections in @es@. -} compareArgs :: [Elim] -> TerM (Int, Int, [[Order]]) compareArgs es = do liftTCM $ reportSDoc "term.compareArgs" 90 $ vcat [ text $ "comparing " ++ show (length es) ++ " args" ] pats <- terGetPatterns -- apats <- annotatePatsWithUseSizeLt pats -- reportSDoc "term.compare" 20 $ -- text "annotated patterns = " <+> sep (map prettyTCM apats) -- matrix <- forM es $ \ e -> forM apats $ \ (b, p) -> terSetUseSizeLt b $ compareElim e p matrix <- withUsableVars pats $ forM es $ \ e -> forM pats $ \ p -> compareElim e p -- Count the number of coinductive projection(pattern)s in caller and callee. -- Only recursive coinductive projections are eligible (Issue 1209). projsCaller <- length <$> do filterM (isCoinductiveProjection True) $ mapMaybe (fmap (head . unAmbQ . snd) . isProjP . getMasked) pats projsCallee <- length <$> do filterM (isCoinductiveProjection True) $ mapMaybe (fmap snd . isProjElim) es cutoff <- terGetCutOff let ?cutoff = cutoff let guardedness = decr True $ projsCaller - projsCallee liftTCM $ reportSDoc "term.guardedness" 30 $ sep [ text "compareArgs:" , nest 2 $ text $ "projsCaller = " ++ prettyShow projsCaller , nest 2 $ text $ "projsCallee = " ++ prettyShow projsCallee , nest 2 $ text $ "guardedness of call: " ++ prettyShow guardedness ] return $ addGuardedness guardedness (size es) (size pats) matrix -- | Traverse patterns from left to right. -- When we come to a projection pattern, -- switch usage of SIZELT constraints: -- on, if coinductive, -- off, if inductive. -- -- UNUSED annotatePatsWithUseSizeLt :: [DeBruijnPattern] -> TerM [(Bool,DeBruijnPattern)] annotatePatsWithUseSizeLt = loop where loop [] = return [] loop (p@(ProjP _ q) : pats) = ((False,p) :) <$> do projUseSizeLt q $ loop pats loop (p : pats) = (\ b ps -> (b,p) : ps) <$> terGetUseSizeLt <*> loop pats -- | @compareElim e dbpat@ compareElim :: Elim -> Masked DeBruijnPattern -> TerM Order compareElim e p = do liftTCM $ do reportSDoc "term.compare" 30 $ sep [ text "compareElim" , nest 2 $ text "e = " <> prettyTCM e , nest 2 $ text "p = " <> prettyTCM p ] reportSDoc "term.compare" 50 $ sep [ nest 2 $ text $ "e = " ++ show e , nest 2 $ text $ "p = " ++ show p ] case (e, getMasked p) of (Proj _ d, ProjP _ d') -> do d <- getOriginalProjection d d' <- getOriginalProjection d' o <- compareProj d d' reportSDoc "term.compare" 30 $ sep [ text $ "comparing callee projection " ++ prettyShow d , text $ "against caller projection " ++ prettyShow d' , text $ "yields order " ++ prettyShow o ] return o (Proj{}, _) -> return Order.unknown (Apply{}, ProjP{}) -> return Order.unknown (Apply arg, _) -> compareTerm (unArg arg) p -- | In dependent records, the types of later fields may depend on the -- values of earlier fields. Thus when defining an inhabitant of a -- dependent record type such as Σ by copattern matching, -- a recursive call eliminated by an earlier projection (proj₁) might -- occur in the definition at a later projection (proj₂). -- Thus, earlier projections are considered "smaller" when -- comparing copattern spines. This is an ok approximation -- of the actual dependency order. -- See issues 906, 942. compareProj :: MonadTCM tcm => QName -> QName -> tcm Order compareProj d d' | d == d' = return Order.le | otherwise = liftTCM $ do -- different projections mr <- getRecordOfField d mr' <- getRecordOfField d' case (mr, mr') of (Just r, Just r') | r == r' -> do -- of same record def <- theDef <$> getConstInfo r case def of Record{ recFields = fs } -> do fs <- return $ map unArg fs case (List.find (d==) fs, List.find (d'==) fs) of (Just i, Just i') -- earlier field is smaller | i < i' -> return Order.lt | i == i' -> do __IMPOSSIBLE__ | otherwise -> return Order.unknown _ -> __IMPOSSIBLE__ _ -> __IMPOSSIBLE__ _ -> return Order.unknown -- | 'makeCM' turns the result of 'compareArgs' into a proper call matrix makeCM :: Int -> Int -> [[Order]] -> CallMatrix makeCM ncols nrows matrix = CallMatrix $ Matrix.fromLists (Matrix.Size nrows ncols) matrix {- To turn off guardedness, restore this code. -- | 'addGuardedness' does nothing. addGuardedness :: Integral n => Order -> n -> n -> [[Order]] -> (n, n, [[Order]]) addGuardedness g nrows ncols m = (nrows, ncols, m) -} -- | 'addGuardedness' adds guardedness flag in the upper left corner (0,0). addGuardedness :: Order -> Int -> Int -> [[Order]] -> (Int, Int, [[Order]]) addGuardedness o nrows ncols m = (nrows + 1, ncols + 1, (o : replicate ncols Order.unknown) : map (Order.unknown :) m) -- | Compose something with the upper-left corner of a call matrix composeGuardedness :: (?cutoff :: CutOff) => Order -> [[Order]] -> [[Order]] composeGuardedness o ((corner : row) : rows) = ((o .*. corner) : row) : rows composeGuardedness _ _ = __IMPOSSIBLE__ -- | Stripping off a record constructor is not counted as decrease, in -- contrast to a data constructor. -- A record constructor increases/decreases by 0, a data constructor by 1. offsetFromConstructor :: MonadTCM tcm => QName -> tcm Int offsetFromConstructor c = maybe 1 (const 0) <$> do liftTCM $ isRecordConstructor c -- | Compute the proper subpatterns of a 'DeBruijnPattern'. subPatterns :: DeBruijnPattern -> [DeBruijnPattern] subPatterns = foldPattern $ \case ConP _ _ ps -> map namedArg ps VarP _ -> mempty LitP _ -> mempty DotP _ -> mempty AbsurdP _ -> mempty ProjP _ _ -> mempty compareTerm :: Term -> Masked DeBruijnPattern -> TerM Order compareTerm t p = do -- reportSDoc "term.compare" 25 $ -- text " comparing term " <+> prettyTCM t <+> -- text " to pattern " <+> prettyTCM p t <- liftTCM $ stripAllProjections t o <- compareTerm' t p liftTCM $ reportSDoc "term.compare" 25 $ text " comparing term " <+> prettyTCM t <+> text " to pattern " <+> prettyTCM p <+> text (" results in " ++ prettyShow o) return o -- | Remove all non-coinductive projections from an algebraic term -- (not going under binders). -- Also, remove 'DontCare's. class StripAllProjections a where stripAllProjections :: a -> TCM a instance StripAllProjections a => StripAllProjections (Arg a) where stripAllProjections = traverse stripAllProjections instance StripAllProjections Elims where stripAllProjections es = case es of [] -> return [] (Apply a : es) -> do (:) <$> (Apply <$> stripAllProjections a) <*> stripAllProjections es (Proj o p : es) -> do isP <- isProjectionButNotCoinductive p applyUnless isP (Proj o p :) <$> stripAllProjections es instance StripAllProjections Args where stripAllProjections = mapM stripAllProjections instance StripAllProjections Term where stripAllProjections t = do case ignoreSharing t of Var i es -> Var i <$> stripAllProjections es Con c ci ts -> Con c ci <$> stripAllProjections ts Def d es -> Def d <$> stripAllProjections es DontCare t -> stripAllProjections t _ -> return t -- | @compareTerm' t dbpat@ -- -- Precondition: top meta variable resolved compareTerm' :: Term -> Masked DeBruijnPattern -> TerM Order compareTerm' v mp@(Masked m p) = do suc <- terGetSizeSuc cutoff <- terGetCutOff let ?cutoff = cutoff v <- return $ ignoreSharing v case (v, p) of -- Andreas, 2013-11-20 do not drop projections, -- in any case not coinductive ones!: (Var i es, _) | Just{} <- allApplyElims es -> compareVar i mp (DontCare t, _) -> compareTerm' t mp -- Andreas, 2014-09-22, issue 1281: -- For metas, termination checking should be optimistic. -- If there is any instance of the meta making termination -- checking succeed, then we should not fail. -- Thus, we assume the meta will be instantiated with the -- deepest variable in @p@. -- For sized types, the depth is maximally -- the number of SIZELT hypotheses one can have in a context. (MetaV{}, p) -> Order.decr True . max (if m then 0 else patternDepth p) . pred <$> terAsks _terSizeDepth -- Successor on both sides cancel each other. -- We ignore the mask for sizes. (Def s [Apply t], ConP s' _ [p]) | s == conName s' && Just s == suc -> compareTerm' (unArg t) (notMasked $ namedArg p) -- Register also size increase. (Def s [Apply t], p) | Just s == suc -> -- Andreas, 2012-10-19 do not cut off here increase 1 <$> compareTerm' (unArg t) mp -- In all cases that do not concern sizes, -- we cannot continue if pattern is masked. _ | m -> return Order.unknown (Lit l, LitP l') | l == l' -> return Order.le | otherwise -> return Order.unknown (Lit l, _) -> do v <- liftTCM $ constructorForm v case ignoreSharing v of Lit{} -> return Order.unknown v -> compareTerm' v mp -- Andreas, 2011-04-19 give subterm priority over matrix order (Con{}, ConP c _ ps) | any (isSubTerm v . namedArg) ps -> decr True <$> offsetFromConstructor (conName c) (Con c _ ts, ConP c' _ ps) | conName c == conName c'-> compareConArgs ts ps (Con _ _ [], _) -> return Order.le -- new case for counting constructors / projections -- register also increase (Con c _ ts, _) -> do increase <$> offsetFromConstructor (conName c) <*> (infimum <$> mapM (\ t -> compareTerm' (unArg t) mp) ts) (t, p) -> return $ subTerm t p -- | @subTerm@ computes a size difference (Order) subTerm :: (?cutoff :: CutOff) => Term -> DeBruijnPattern -> Order subTerm t p = if equal t p then Order.le else properSubTerm t p where equal (Shared p) dbp = equal (derefPtr p) dbp equal (Con c _ ts) (ConP c' _ ps) = and $ (conName c == conName c') : (length ts == length ps) : zipWith (\ t p -> equal (unArg t) (namedArg p)) ts ps equal (Var i []) (VarP x) = i == dbPatVarIndex x equal (Lit l) (LitP l') = l == l' -- Terms. -- Checking for identity here is very fragile. -- However, we cannot do much more, as we are not allowed to normalize t. -- (It might diverge, and we are just in the process of termination checking.) equal t (DotP t') = t == t' equal _ _ = False properSubTerm t (ConP _ _ ps) = setUsability True $ decrease 1 $ supremum $ map (subTerm t . namedArg) ps properSubTerm _ _ = Order.unknown isSubTerm :: (?cutoff :: CutOff) => Term -> DeBruijnPattern -> Bool isSubTerm t p = nonIncreasing $ subTerm t p compareConArgs :: Args -> [NamedArg DeBruijnPattern] -> TerM Order compareConArgs ts ps = do cutoff <- terGetCutOff let ?cutoff = cutoff -- we may assume |ps| >= |ts|, otherwise c ps would be of functional type -- which is impossible case (length ts, length ps) of (0,0) -> return Order.le -- c <= c (0,1) -> return Order.unknown -- c not<= c x (1,0) -> __IMPOSSIBLE__ (1,1) -> compareTerm' (unArg (head ts)) (notMasked $ namedArg $ head ps) (_,_) -> foldl (Order..*.) Order.le <$> zipWithM compareTerm' (map unArg ts) (map (notMasked . namedArg) ps) -- corresponds to taking the size, not the height -- allows examples like (x, y) < (Succ x, y) {- version which does an "order matrix" -- Andreas, 2013-02-18 disabled because it is unclear -- how to scale idempotency test to matrix-shaped orders (need thinking/researcH) -- Trigges issue 787. (_,_) -> do -- build "call matrix" m <- mapM (\t -> mapM (compareTerm' suc (unArg t)) ps) ts let m2 = makeCM (length ps) (length ts) m return $ Order.orderMat (Order.mat m2) -} {- version which takes height -- if null ts then Order.Le -- else Order.infimum (zipWith compareTerm' (map unArg ts) ps) -} compareVar :: Nat -> Masked DeBruijnPattern -> TerM Order compareVar i (Masked m p) = do suc <- terGetSizeSuc cutoff <- terGetCutOff let ?cutoff = cutoff let no = return Order.unknown case p of ProjP{} -> no LitP{} -> no DotP{} -> no AbsurdP{} -> no VarP x -> compareVarVar i (Masked m x) ConP s _ [p] | Just (conName s) == suc -> setUsability True . decrease 1 <$> compareVar i (notMasked $ namedArg p) ConP c _ ps -> if m then no else setUsability True <$> do decrease <$> offsetFromConstructor (conName c) <*> (Order.supremum <$> mapM (compareVar i . notMasked . namedArg) ps) -- | Compare two variables. -- -- The first variable comes from a term, the second from a pattern. compareVarVar :: Nat -> Masked DBPatVar -> TerM Order compareVarVar i (Masked m x@(DBPatVar _ j)) | i == j = if not m then return Order.le else liftTCM $ -- If j is a size, we ignore the mask. ifM (isJust <$> do isSizeType =<< reduce =<< typeOfBV j) {- then -} (return Order.le) {- else -} (return Order.unknown) | otherwise = do -- record usability of variable u <- (i `VarSet.member`) <$> terGetUsableVars -- Andreas, 2017-07-26, issue #2331. -- The usability logic is refuted by bounded size quantification in terms. -- Thus, it is switched off (the infrastructure remains in place for now). if not u then return Order.unknown else do -- Only if usable: res <- isBounded i case res of BoundedNo -> return Order.unknown BoundedLt v -> setUsability u . decrease 1 <$> compareTerm' v (Masked m $ VarP x) Agda-2.5.3/src/full/Agda/Termination/SparseMatrix.hs0000644000000000000000000003627413154613124020413 0ustar0000000000000000{-# LANGUAGE CPP #-} {- | Sparse matrices. We assume the matrices to be very sparse, so we just implement them as sorted association lists. Most operations are linear in the number of non-zero elements. An exception is transposition, which needs to sort the association list again; it has the complexity of sorting: @n log n@ where @n@ is the number of non-zero elements. Another exception is matrix multiplication, of course. -} module Agda.Termination.SparseMatrix ( -- * Basic data types Matrix(Matrix) , unM -- , matrixInvariant -- Moved to the internal test-suite , Size(..) , MIx (..) -- * Generating and creating matrices , fromLists , fromIndexList , toLists -- , Agda.Termination.Matrix.zipWith -- , matrix -- Moved to the internal test-suite -- * Combining and querying matrices , size , square , isEmpty , isSingleton , zipMatrices , add , intersectWith , interAssocWith , mul , transpose , Diagonal(..) , toSparseRows , supSize , zipAssocWith -- * Modifying matrices , addRow , addColumn ) where import Data.Array import Data.Function import qualified Data.List as List import Data.Maybe import Data.Monoid import Data.Foldable (Foldable) import qualified Data.Foldable as Fold import Data.Traversable (Traversable) import qualified Text.PrettyPrint.Boxes as Boxes import Agda.Termination.Semiring (HasZero(..), Semiring) import qualified Agda.Termination.Semiring as Semiring import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.PartialOrd import Agda.Utils.Pretty hiding (isEmpty) import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible ------------------------------------------------------------------------ -- * Basic data types ------------------------------------------------------------------------ -- | Size of a matrix. data Size i = Size { rows :: i -- ^ Number of rows, @>= 0@. , cols :: i -- ^ Number of columns, @>= 0@. } deriving (Eq, Ord, Show) -- | Type of matrix indices (row, column). data MIx i = MIx { row :: i -- ^ Row index, @1 <= row <= rows@. , col :: i -- ^ Column index @1 <= col <= cols@. } deriving (Eq, Ord, Show, Ix) -- | Convert a 'Size' to a set of bounds suitable for use with -- the matrices in this module. toBounds :: Num i => Size i -> (MIx i, MIx i) toBounds sz = (MIx { row = 1, col = 1 }, MIx { row = rows sz, col = cols sz }) -- | Type of matrices, parameterised on the type of values. -- -- Sparse matrices are implemented as an ordered association list, -- mapping coordinates to values. data Matrix i b = Matrix { size :: Size i -- ^ Dimensions of the matrix. , unM :: [(MIx i, b)] -- ^ Association of indices to values. } deriving (Eq, Ord, Functor, Foldable, Traversable) ------------------------------------------------------------------------ -- * Operations and query on matrix size. ------------------------------------------------------------------------ -- | 'True' iff the matrix is square. square :: Ix i => Matrix i b -> Bool square m = rows (size m) == cols (size m) -- | Returns 'True' iff the matrix is empty. isEmpty :: (Num i, Ix i) => Matrix i b -> Bool isEmpty m = rows sz <= 0 || cols sz <= 0 where sz = size m -- | Compute the matrix size of the union of two matrices. supSize :: Ord i => Matrix i a -> Matrix i b -> Size i supSize (Matrix (Size r1 c1) _) (Matrix (Size r2 c2) _) = Size (max r1 r2) (max c1 c2) -- | Compute the matrix size of the intersection of two matrices. infSize :: Ord i => Matrix i a -> Matrix i b -> Size i infSize (Matrix (Size r1 c1) _) (Matrix (Size r2 c2) _) = Size (min r1 r2) (min c1 c2) ------------------------------------------------------------------------ -- * Creating matrices and converting to lists. ------------------------------------------------------------------------ -- | Constructs a matrix from a list of @(index, value)@-pairs. -- @O(n)@ where @n@ is size of the list. -- -- Precondition: indices are unique. fromIndexList :: (Ord i, HasZero b) => Size i -> [(MIx i, b)] -> Matrix i b fromIndexList sz = Matrix sz . List.sortBy (compare `on` fst) . filter ((zeroElement /=) . snd) -- | @'fromLists' sz rs@ constructs a matrix from a list of lists of -- values (a list of rows). -- @O(size)@ where @size = rows × cols@. -- -- Precondition: -- @'length' rs '==' 'rows' sz@ and -- @'all' (('cols' sz '==') . 'length') rs@. fromLists :: (Ord i, Num i, Enum i, HasZero b) => Size i -> [[b]] -> Matrix i b fromLists sz bs = fromIndexList sz $ zip ([ MIx i j | i <- [1..rows sz] , j <- [1..cols sz]]) (concat bs) -- | Converts a sparse matrix to a sparse list of rows. -- @O(n)@ where @n@ is the number of non-zero entries of the matrix. -- -- Only non-empty rows are generated. -- toSparseRows :: (Eq i) => Matrix i b -> [(i,[(i,b)])] toSparseRows (Matrix _ []) = [] toSparseRows (Matrix _ ((MIx i j, b) : m)) = aux i [(j,b)] m where aux i' [] [] = [] aux i' row [] = [(i', reverse row)] aux i' row ((MIx i j, b) : m) | i' == i = aux i' ((j,b):row) m | otherwise = (i', reverse row) : aux i [(j,b)] m -- | Turn a sparse vector into a vector by inserting a fixed element -- at the missing positions. -- @O(size)@ where @size@ is the dimension of the vector. blowUpSparseVec :: (Integral i) => b -> i -> [(i,b)] -> [b] blowUpSparseVec zero n l = aux 1 l where aux i [] = List.genericReplicate (n + 1 - i) zero aux i l@((j,b):l') | i > j || i > n = __IMPOSSIBLE__ | i == j = b : aux (i + 1) l' | otherwise = zero : aux (i + 1) l -- Older implementation without replicate. blowUpSparseVec' :: (Ord i, Num i, Enum i) => b -> i -> [(i,b)] -> [b] blowUpSparseVec' zero n l = aux 1 l where aux i [] | i > n = [] | otherwise = zero : aux (i+1) [] aux i ((j,b):l) | i <= n && j == i = b : aux (succ i) l aux i ((j,b):l) | i <= n && j >= i = zero : aux (succ i) ((j,b):l) aux i l = __IMPOSSIBLE__ -- error $ "blowUpSparseVec (n = " ++ show n ++ ") aux i=" ++ show i ++ " j=" ++ show (fst (head l)) ++ " length l = " ++ show (length l) -- | Converts a matrix to a list of row lists. -- @O(size)@ where @size = rows × cols@. toLists :: (Integral i, HasZero b) => Matrix i b -> [[b]] toLists m@(Matrix size@(Size nrows ncols) _) = blowUpSparseVec emptyRow nrows $ map (mapSnd (blowUpSparseVec zeroElement ncols)) $ toSparseRows m where emptyRow = List.genericReplicate ncols zeroElement ------------------------------------------------------------------------ -- * Combining and querying matrices ------------------------------------------------------------------------ -- | Returns 'Just b' iff it is a 1x1 matrix with just one entry 'b'. -- @O(1)@. isSingleton :: (Eq i, Num i, HasZero b) => Matrix i b -> Maybe b isSingleton (Matrix (Size 1 1) [(_,b)]) = Just b isSingleton (Matrix (Size 1 1) [] ) = Just zeroElement isSingleton (Matrix (Size 1 1) _ ) = __IMPOSSIBLE__ isSingleton _ = Nothing -- | @'diagonal' m@ extracts the diagonal of @m@. -- -- For non-square matrices, the length of the diagonal is -- the minimum of the dimensions of the matrix. class Diagonal m e | m -> e where diagonal :: m -> [e] -- | Diagonal of sparse matrix. -- -- @O(n)@ where @n@ is the number of non-zero elements in the matrix. instance (Integral i, HasZero b) => Diagonal (Matrix i b) b where diagonal (Matrix (Size r c) m) = blowUpSparseVec zeroElement (min r c) $ mapMaybe (\ ((MIx i j), b) -> if i==j then Just (i,b) else Nothing) m -- | Transposable things. class Transpose a where transpose :: a -> a -- | Size of transposed matrix. instance Transpose (Size i) where transpose (Size n m) = Size m n -- | Transposing coordinates. instance Transpose (MIx i) where transpose (MIx i j) = MIx j i -- | Matrix transposition. -- -- @O(n log n)@ where @n@ is the number of non-zero elements in the matrix. instance Ord i => Transpose (Matrix i b) where transpose (Matrix size m) = Matrix (transpose size) $ List.sortBy (compare `on` fst) $ map (mapFst transpose) m -- | General pointwise combination function for association lists. -- @O(n1 + n2)@ where @ni@ is the number of non-zero element in matrix @i@. -- -- In @zipAssocWith fs gs f g h l l'@, -- -- @fs@ is possibly more efficient version of -- @'mapMaybe' (\ (i, a) -> (i,) <$> f a)@, and same for @gs@ and @g@. zipAssocWith :: (Ord i) => ([(i,a)] -> [(i,c)]) -- ^ Only left map remaining. -> ([(i,b)] -> [(i,c)]) -- ^ Only right map remaining. -> (a -> Maybe c) -- ^ Element only present in left map. -> (b -> Maybe c) -- ^ Element only present in right map. -> (a -> b -> Maybe c) -- ^ Element present in both maps. -> [(i,a)] -> [(i,b)] -> [(i,c)] zipAssocWith fs gs f g h = merge where merge m1 [] = mapMaybe (\ (i, a) -> (i,) <$> f a) m1 merge [] m2 = mapMaybe (\ (i, b) -> (i,) <$> g b) m2 merge m1@((i,a):m1') m2@((j,b):m2') = case compare i j of LT -> mcons ((i,) <$> f a) $ merge m1' m2 GT -> mcons ((j,) <$> g b) $ merge m1 m2' EQ -> mcons ((i,) <$> h a b) $ merge m1' m2' -- | Instance of 'zipAssocWith' which keeps longer assoc lists. -- @O(n1 + n2)@. unionAssocWith :: (Ord i) => (a -> Maybe c) -- ^ Element only present in left map. -> (b -> Maybe c) -- ^ Element only present in right map. -> (a -> b -> Maybe c) -- ^ Element present in both maps. -> [(i,a)] -> [(i,b)] -> [(i,c)] unionAssocWith f g h = zipAssocWith (map_ f) (map_ g) f g h where map_ f = mapMaybe (\ (i, a) -> (i,) <$> f a) -- | General pointwise combination function for sparse matrices. -- @O(n1 + n2)@. zipMatrices :: forall a b c i . (Ord i) => (a -> c) -- ^ Element only present in left matrix. -> (b -> c) -- ^ Element only present in right matrix. -> (a -> b -> c) -- ^ Element present in both matrices. -> (c -> Bool) -- ^ Result counts as zero? -> Matrix i a -> Matrix i b -> Matrix i c zipMatrices f g h zero m1 m2 = Matrix (supSize m1 m2) $ unionAssocWith (drop0 . f) (drop0 . g) (\ a -> drop0 . h a) (unM m1) (unM m2) where drop0 = filterMaybe (not . zero) -- | @'add' (+) m1 m2@ adds @m1@ and @m2@, using @(+)@ to add values. -- @O(n1 + n2)@. -- -- Returns a matrix of size @'supSize' m1 m2@. add :: (Ord i, HasZero a) => (a -> a -> a) -> Matrix i a -> Matrix i a -> Matrix i a add plus = zipMatrices id id plus (== zeroElement) -- | @'intersectWith' f m1 m2@ build the pointwise conjunction @m1@ and @m2@. -- Uses @f@ to combine non-zero values. -- @O(n1 + n2)@. -- -- Returns a matrix of size @infSize m1 m2@. intersectWith :: (Ord i) => (a -> a -> a) -> Matrix i a -> Matrix i a -> Matrix i a intersectWith f m1 m2 = Matrix (infSize m1 m2) $ interAssocWith f (unM m1) (unM m2) -- | Association list intersection. -- @O(n1 + n2)@. -- -- @interAssocWith f l l' = { (i, f a b) | (i,a) ∈ l and (i,b) ∈ l' }@ -- -- Used to combine sparse matrices, it might introduce zero elements -- if @f@ can return zero for non-zero arguments. interAssocWith :: (Ord i) => (a -> a -> a) -> [(i,a)] -> [(i,a)] -> [(i,a)] interAssocWith f [] m = [] interAssocWith f l [] = [] interAssocWith f l@((i,a):l') m@((j,b):m') | i < j = interAssocWith f l' m | i > j = interAssocWith f l m' | otherwise = (i, f a b) : interAssocWith f l' m' -- | @'mul' semiring m1 m2@ multiplies matrices @m1@ and @m2@. -- Uses the operations of the semiring @semiring@ to perform the -- multiplication. -- -- @O(n1 + n2 log n2 + Σ(i <= r1) Σ(j <= c2) d(i,j))@ where -- @r1@ is the number of non-empty rows in @m1@ and -- @c2@ is the number of non-empty columns in @m2@ and -- @d(i,j)@ is the bigger one of the following two quantifies: -- the length of sparse row @i@ in @m1@ and -- the length of sparse column @j@ in @m2@. -- -- Given dimensions @m1 : r1 × c1@ and @m2 : r2 × c2@, -- a matrix of size @r1 × c2@ is returned. -- It is not necessary that @c1 == r2@, the matrices are implicitly -- patched with zeros to match up for multiplication. -- For sparse matrices, this patching is a no-op. mul :: (Ix i, Eq a) => Semiring a -> Matrix i a -> Matrix i a -> Matrix i a mul semiring m1 m2 = Matrix (Size { rows = rows (size m1), cols = cols (size m2) }) $ [ (MIx i j, b) | (i,v) <- toSparseRows m1 , (j,w) <- toSparseRows $ transpose m2 , let b = inner v w , b /= zero ] where zero = Semiring.zero semiring plus = Semiring.add semiring times = Semiring.mul semiring inner v w = List.foldl' plus zero $ map snd $ interAssocWith times v w -- | Pointwise comparison. -- Only matrices with the same dimension are comparable. instance (Ord i, PartialOrd a) => PartialOrd (Matrix i a) where comparable m n | size m /= size n = POAny | otherwise = Fold.fold $ zipMatrices onlym onlyn both trivial m n where -- If an element is only in @m@, then its 'Unknown' in @n@ -- so it gotten better at best, in any case, not worse. onlym o = POGT -- If an element is only in @n@, then its 'Unknown' in @m@ -- so we have strictly less information. onlyn o = POLT both = comparable -- The zero element of the result sparse matrix is the -- neutral element of the monoid. trivial = (==mempty) ------------------------------------------------------------------------ -- Modifying matrices -- | @'addColumn' x m@ adds a new column to @m@, after the columns -- already existing in the matrix. All elements in the new column get -- set to @x@. addColumn :: (Num i, HasZero b) => b -> Matrix i b -> Matrix i b addColumn x m | x == zeroElement = m { size = (size m) { cols = cols (size m) + 1 }} | otherwise = __IMPOSSIBLE__ -- | @'addRow' x m@ adds a new row to @m@, after the rows already -- existing in the matrix. All elements in the new row get set to @x@. addRow :: (Num i, HasZero b) => b -> Matrix i b -> Matrix i b addRow x m | x == zeroElement = m { size = (size m) { rows = rows (size m) + 1 }} | otherwise = __IMPOSSIBLE__ ------------------------------------------------------------------------ -- * Printing ------------------------------------------------------------------------ instance (Integral i, HasZero b, Show i, Show b) => Show (Matrix i b) where showsPrec _ m = showString "Agda.Termination.SparseMatrix.fromLists " . shows (size m) . showString " " . shows (toLists m) instance (Integral i, HasZero b, Pretty b) => Pretty (Matrix i b) where -- pretty = vcat . map (hsep . map pretty) . toLists pretty = vcat . map text . lines . Boxes.render . Boxes.hsep 1 Boxes.right . map ( Boxes.vcat Boxes.right . map ( Boxes.alignHoriz Boxes.right 4 . Boxes.text . render . pretty ) ) . toLists . transpose -- ADAPTED FROM: -- http://www.tedreed.info/programming/2012/06/02/how-to-use-textprettyprintboxes/ -- print_table :: [[String]] -> IO () -- print_table rows = printBox $ hsep 2 left (map (vcat left . map text) (transpose rows)) Agda-2.5.3/src/full/Agda/Termination/CallMatrix.hs0000644000000000000000000001545713154613124020031 0ustar0000000000000000-- {-# LANGUAGE CPP #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE ImplicitParams #-} module Agda.Termination.CallMatrix where -- module Agda.Termination.CallMatrix -- ( CallMatrix'(..), CallMatrix -- , callMatrix -- , CallComb(..) -- , tests -- ) where import Data.Semigroup import Data.Foldable (Foldable) import Data.Traversable (Traversable) import Agda.Termination.CutOff import Agda.Termination.Order as Order import Agda.Termination.SparseMatrix as Matrix import Agda.Termination.Semiring (HasZero(..)) import Agda.Utils.Favorites (Favorites) import qualified Agda.Utils.Favorites as Fav import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.PartialOrd import Agda.Utils.Pretty hiding ((<>)) import Agda.Utils.Singleton ------------------------------------------------------------------------ -- * Call matrices ------------------------------------------------------------------------ -- | Call matrix indices = function argument indices. -- -- Machine integer 'Int' is sufficient, since we cannot index more arguments -- than we have addresses on our machine. type ArgumentIndex = Int -- | Call matrices. -- -- A call matrix for a call @f --> g@ has dimensions @ar(g) × ar(f)@. -- -- Each column corresponds to one formal argument of caller @f@. -- Each row corresponds to one argument in the call to @g@. -- -- In the presence of dot patterns, a call argument can be related -- to /several/ different formal arguments of @f@. -- -- See e.g. @test/succeed/DotPatternTermination.agda@: -- -- @ -- data D : Nat -> Set where -- cz : D zero -- c1 : forall n -> D n -> D (suc n) -- c2 : forall n -> D n -> D n -- -- f : forall n -> D n -> Nat -- f .zero cz = zero -- f .(suc n) (c1 n d) = f n (c2 n d) -- f n (c2 .n d) = f n d -- @ -- -- Call matrices (without guardedness) are -- -- @ -- -1 -1 n < suc n and n < c1 n d -- ? = c2 n d <= c1 n d -- -- = -1 n <= n and n < c2 n d -- ? -1 d < c2 n d -- @ -- -- Here is a part of the original documentation for call matrices -- (kept for historical reasons): -- -- This datatype encodes information about a single recursive -- function application. The columns of the call matrix stand for -- 'source' function arguments (patterns). The rows of the matrix stand for -- 'target' function arguments. Element @(i, j)@ in the matrix should -- be computed as follows: -- -- * 'Order.lt' (less than) if the @j@-th argument to the 'target' -- function is structurally strictly smaller than the @i@-th -- pattern. -- -- * 'Order.le' (less than or equal) if the @j@-th argument to the -- 'target' function is structurally smaller than the @i@-th -- pattern. -- -- * 'Order.unknown' otherwise. newtype CallMatrix' a = CallMatrix { mat :: Matrix ArgumentIndex a } deriving (Eq, Ord, Show, Functor, Foldable, Traversable, PartialOrd) type CallMatrix = CallMatrix' Order deriving instance NotWorse CallMatrix instance HasZero a => Diagonal (CallMatrix' a) a where diagonal = diagonal . mat -- | Call matrix multiplication and call combination. class CallComb a where (>*<) :: (?cutoff :: CutOff) => a -> a -> a -- | Call matrix multiplication. -- -- @f --(m1)--> g --(m2)--> h@ is combined to @f --(m2 `mul` m1)--> h@ -- -- Note the reversed order of multiplication: -- The matrix @c1@ of the second call @g-->h@ in the sequence -- @f-->g-->h@ is multiplied with the matrix @c2@ of the first call. -- -- Preconditions: -- @m1@ has dimensions @ar(g) × ar(f)@. -- @m2@ has dimensions @ar(h) × ar(g)@. -- -- Postcondition: -- @m1 >*< m2@ has dimensions @ar(h) × ar(f)@. instance CallComb CallMatrix where CallMatrix m1 >*< CallMatrix m2 = CallMatrix $ mul orderSemiring m2 m1 {- UNUSED, BUT DON'T REMOVE! -- | Call matrix addition = minimum = pick worst information. addCallMatrices :: (?cutoff :: CutOff) => CallMatrix -> CallMatrix -> CallMatrix addCallMatrices cm1 cm2 = CallMatrix $ add (Semiring.add orderSemiring) (mat cm1) (mat cm2) -} ------------------------------------------------------------------------ -- * Call matrix augmented with path information. ------------------------------------------------------------------------ -- | Call matrix augmented with path information. data CallMatrixAug cinfo = CallMatrixAug { augCallMatrix :: CallMatrix -- ^ The matrix of the (composed call). , augCallInfo :: cinfo -- ^ Meta info, like call path. } deriving (Eq, Show) instance Diagonal (CallMatrixAug cinfo) Order where diagonal = diagonal . augCallMatrix instance PartialOrd (CallMatrixAug cinfo) where comparable m m' = comparable (augCallMatrix m) (augCallMatrix m') instance NotWorse (CallMatrixAug cinfo) where c1 `notWorse` c2 = augCallMatrix c1 `notWorse` augCallMatrix c2 -- | Augmented call matrix multiplication. instance Monoid cinfo => CallComb (CallMatrixAug cinfo) where CallMatrixAug m1 p1 >*< CallMatrixAug m2 p2 = CallMatrixAug (m1 >*< m2) (mappend p1 p2) -- | Non-augmented call matrix. noAug :: Monoid cinfo => CallMatrix -> CallMatrixAug cinfo noAug m = CallMatrixAug m mempty ------------------------------------------------------------------------ -- * Sets of incomparable call matrices augmented with path information. ------------------------------------------------------------------------ -- | Sets of incomparable call matrices augmented with path information. -- Use overloaded 'null', 'empty', 'singleton', 'mappend'. newtype CMSet cinfo = CMSet { cmSet :: Favorites (CallMatrixAug cinfo) } deriving ( Show, Semigroup, Monoid, Null, Singleton (CallMatrixAug cinfo) ) -- | Call matrix set product is the Cartesian product. instance Monoid cinfo => CallComb (CMSet cinfo) where CMSet as >*< CMSet bs = CMSet $ Fav.fromList $ [ a >*< b | a <- Fav.toList as, b <- Fav.toList bs ] -- | Insert into a call matrix set. insert :: CallMatrixAug cinfo -> CMSet cinfo -> CMSet cinfo insert a (CMSet as) = CMSet $ Fav.insert a as -- | Union two call matrix sets. union :: CMSet cinfo -> CMSet cinfo -> CMSet cinfo union = mappend -- union (CMSet as) (CMSet bs) = CMSet $ Fav.union as bs -- | Convert into a list of augmented call matrices. toList :: CMSet cinfo -> [CallMatrixAug cinfo] toList (CMSet as) = Fav.toList as ------------------------------------------------------------------------ -- * Printing ------------------------------------------------------------------------ instance Pretty CallMatrix where pretty (CallMatrix m) = pretty m instance Pretty cinfo => Pretty (CallMatrixAug cinfo) where pretty (CallMatrixAug m cinfo) = pretty cinfo $$ pretty m instance Pretty cinfo => Pretty (CMSet cinfo) where pretty = vcat . punctuate newLine . map pretty . toList where newLine = text "\n" Agda-2.5.3/src/full/Agda/Termination/RecCheck.hs0000644000000000000000000000372113154613124017427 0ustar0000000000000000{-# LANGUAGE CPP #-} {- Checking for recursion: - We detect truly (co)recursive definitions by computing the dependency graph and checking for cycles. - This is inexpensive and let us skip the termination check when there's no (co)recursion Original contribution by Andrea Vezzosi (sanzhiyan). This implementation by Andreas. -} module Agda.Termination.RecCheck ( recursive , anyDefs ) where import Control.Applicative import Data.Graph import Data.List (nub) import qualified Data.Map as Map import Agda.Syntax.Internal import Agda.Syntax.Internal.Defs import Agda.TypeChecking.Monad #include "undefined.h" import Agda.Utils.Impossible recursive :: [QName] -> TCM Bool recursive names = do graph <- zip names <$> mapM (\ d -> nub <$> recDef names d) names reportSLn "rec.graph" 20 $ show graph return $ cyclic graph -- | A graph is cyclic if it has any strongly connected component. cyclic :: [(QName, [QName])] -> Bool cyclic g = or [ True | CyclicSCC _ <- stronglyConnComp g' ] where g' = map (\ (n, ns) -> ((), n, ns)) g -- | @recDef names name@ returns all definitions from @names@ -- that are used in the body of @name@. recDef :: [QName] -> QName -> TCM [QName] recDef names name = do -- Retrieve definition def <- getConstInfo name case theDef def of Function{ funClauses = cls } -> anyDefs names cls _ -> return [] -- | @anysDef names a@ returns all definitions from @names@ -- that are used in @a@. anyDefs :: GetDefs a => [QName] -> a -> TCM [QName] anyDefs names a = do -- Prepare function to lookup metas outside of TCM st <- getMetaStore let lookup x = case mvInstantiation <$> Map.lookup x st of Just (InstV _ v) -> Just v -- TODO: ignoring the lambdas might be bad? _ -> Nothing -- we collect only those used definitions that are in @names@ emb d = if d `elem` names then [d] else [] -- get all the Defs that are in names return $ getDefs' lookup emb a Agda-2.5.3/src/full/Agda/Termination/Semiring.hs0000644000000000000000000000246713154613124017543 0ustar0000000000000000-- | Semirings. module Agda.Termination.Semiring ( HasZero(..) , Semiring(..) , integerSemiring , intSemiring , boolSemiring ) where import Data.Monoid -- | @HasZero@ is needed for sparse matrices, to tell which is the element -- that does not have to be stored. -- It is a cut-down version of @SemiRing@ which is definable -- without the implicit @?cutoff@. class Eq a => HasZero a where zeroElement :: a -- | Semirings. data Semiring a = Semiring { add :: a -> a -> a -- ^ Addition. , mul :: a -> a -> a -- ^ Multiplication. , zero :: a -- ^ Zero. -- The one is never used in matrix multiplication -- , one :: a -- ^ One. } ------------------------------------------------------------------------ -- Specific semirings -- | The standard semiring on 'Integer's. instance HasZero Integer where zeroElement = 0 integerSemiring :: Semiring Integer integerSemiring = Semiring { add = (+), mul = (*), zero = 0 } -- , one = 1 } -- | The standard semiring on 'Int's. instance HasZero Int where zeroElement = 0 intSemiring :: Semiring Int intSemiring = Semiring { add = (+), mul = (*), zero = 0 } -- , one = 1 } -- | The standard semiring on 'Bool's. boolSemiring :: Semiring Bool boolSemiring = Semiring { add = (||), mul = (&&), zero = False } --, one = True } Agda-2.5.3/src/full/Agda/Termination/Inlining.hs0000644000000000000000000002414313154613124017530 0ustar0000000000000000{-# LANGUAGE CPP #-} -- Author: Ulf Norell -- Created: 2013-11-09 {-| This module defines an inlining transformation on clauses that's run before termination checking. The purpose is to improve termination checking of with clauses (issue 59). The transformation inlines generated with-functions expanding the clauses of the parent function in such a way that termination checking the expanded clauses guarantees termination of the original function, while allowing more terminating functions to be accepted. It does in no way pretend to preserve the semantics of the original function. Roughly, the source program > f ps with as > {f ps₁i qsi = bi} is represented internally as > f ps = f-aux xs as where xs = vars(ps) > {f-aux ps₂i qsi = bi} where ps₁i = ps[ps₂i/xs] The inlining transformation turns this into > {f ps = aj} for aj ∈ as > {f ps₁i qsi = bi} The first set of clauses, called 'withExprClauses', ensure that we don't forget any recursive calls in @as@. The second set of clauses, henceforth called 'inlinedClauses', are the surface-level clauses the user sees (and probably reasons about). The reason this works is that there is a single call site for each with-function. Note that the lhss of the inlined clauses are not type-correct, neither with the type of @f@ (since there are additional patterns @qsi@) nor with the type of @f-aux@ (since there are the surface-level patterns @ps₁i@ instead of the actual patterns @ps₂i@). -} module Agda.Termination.Inlining ( inlineWithClauses , isWithFunction , expandWithFunctionCall ) where import Control.Applicative import Control.Monad.State import qualified Data.List as List import Data.Maybe import Data.Traversable (traverse) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.TypeChecking.Monad import Agda.TypeChecking.Pretty import Agda.TypeChecking.Substitute import Agda.TypeChecking.Reduce import Agda.TypeChecking.DisplayForm import Agda.TypeChecking.Telescope import Agda.Utils.List (downFrom) import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Permutation import Agda.Utils.Size import Agda.Utils.Impossible #include "undefined.h" -- | Returns 'Nothing' if no inlining happened, otherwise, the new clauses. inlineWithClauses :: QName -> Clause -> TCM (Maybe [Clause]) inlineWithClauses f cl = inTopContext $ do -- Clauses are relative to the empty context, so we operate @inTopContext@. let noInline = return Nothing -- The de Bruijn indices of @body@ are relative to the @clauseTel cl@. body <- traverse instantiate $ clauseBody cl case body of Just (Def wf els) -> caseMaybeM (isWithFunction wf) noInline $ \ f' -> if f /= f' then noInline else do -- The clause body is a with-function call @wf args@. -- @f@ is the function the with-function belongs to. let args = fromMaybe __IMPOSSIBLE__ . allApplyElims $ els reportSDoc "term.with.inline" 70 $ sep [ text "Found with (raw):", nest 2 $ text $ show cl ] reportSDoc "term.with.inline" 20 $ sep [ text "Found with:", nest 2 $ prettyTCM $ QNamed f cl ] t <- defType <$> getConstInfo wf cs1 <- withExprClauses cl t args reportSDoc "term.with.inline" 70 $ vcat $ text "withExprClauses (raw)" : map (nest 2 . text . show) cs1 reportSDoc "term.with.inline" 20 $ vcat $ text "withExprClauses" : map (nest 2 . prettyTCM . QNamed f) cs1 cs2 <- inlinedClauses f cl t wf reportSDoc "term.with.inline" 70 $ vcat $ text "inlinedClauses (raw)" : map (nest 2 . text . show) cs2 reportSDoc "term.with.inline" 20 $ vcat $ text "inlinedClauses" : map (nest 2 . prettyTCM . QNamed f) cs2 return $ Just $ cs1 ++ cs2 _ -> noInline -- | Returns the original clause if no inlining happened, otherwise, -- the new clauses. inlineWithClauses' :: QName -> Clause -> TCM [Clause] inlineWithClauses' f cl = fromMaybe [cl] <$> inlineWithClauses f cl -- | @withExprClauses cl t as@ generates a clause containing a fake -- call to with-expression @a@ for each @a@ in @as@ that is not -- a variable (and thus cannot contain a recursive call). -- -- Andreas, 2013-11-11: I guess "not a variable" could be generalized -- to "not containing a call to a mutually defined function". -- -- Note that the @as@ stem from the *unraised* clause body of @cl@ -- and thus can be simply 'fmap'ped back there (under all the 'Bind' -- abstractions). -- -- Precondition: we are 'inTopContext'. withExprClauses :: Clause -> Type -> Args -> TCM [Clause] withExprClauses cl t args = {- addContext (clauseTel cl) $ -} loop t args where -- Note: for the following code, it does not matter which context we are in. -- Restore the @addContext (clauseTel cl)@ if that should become necessary -- (like when debug printing @args@ etc). loop t [] = return [] loop t (a:as) = case unArg a of Var i [] -> rest -- TODO: smarter criterion when to skip withExprClause v -> (cl { clauseBody = Just v , clauseType = Just $ defaultArg dom } :) <$> rest where rest = loop (piApply t [a]) as dom = case unEl t of -- The type is the generated with-function type so we know it Pi a _ -> unDom a -- doesn't contain anything funny _ -> __IMPOSSIBLE__ -- | @inlinedClauses f cl t wf@ inlines the clauses of with-function @wf@ -- of type @t@ into the clause @cl@. The original function name is @f@. -- -- Precondition: we are 'inTopContext'. inlinedClauses :: QName -> Clause -> Type -> QName -> TCM [Clause] inlinedClauses f cl t wf = do -- @wf@ might define a with-function itself, so we first construct -- the with-inlined clauses @wcs@ of @wf@ recursively. wcs <- concat <$> (mapM (inlineWithClauses' wf) =<< defClauses <$> getConstInfo wf) reportSDoc "term.with.inline" 30 $ vcat $ text "With-clauses to inline" : map (nest 2 . prettyTCM . QNamed wf) wcs mapM (inline f cl t wf) wcs -- | The actual work horse. -- @inline f pcl t wf wcl@ inlines with-clause @wcl@ of with-function @wf@ -- (of type @t@) into parent clause @pcl@ (original function being @f@). inline :: QName -> Clause -> Type -> QName -> Clause -> TCM Clause inline f pcl t wf wcl = inTopContext $ addContext (clauseTel wcl) $ do -- The tricky part here is to get the variables to line up properly. The -- order of the arguments to the with-function is not the same as the order -- of the arguments to the parent function. Fortunately we have already -- figured out how to turn an application of the with-function into an -- application of the parent function in the display form. reportSDoc "term.with.inline" 70 $ text "inlining (raw) =" <+> text (show wcl) Just disp <- displayForm wf $ clauseElims wcl reportSDoc "term.with.inline" 70 $ text "display form (raw) =" <+> text (show disp) reportSDoc "term.with.inline" 40 $ text "display form =" <+> prettyTCM disp (pats, perm) <- dispToPats disp -- Jesper, 2016-07-28: Since the with-clause and the inlined clause both -- have the same clause telescope and the clause body is now relative to the -- clause telescope, there is no more need to change the clause body. return wcl { namedClausePats = numberPatVars __IMPOSSIBLE__ perm pats } where numVars = size (clauseTel wcl) dispToPats :: DisplayTerm -> TCM ([NamedArg Pattern], Permutation) dispToPats (DWithApp (DDef _ es) ws zs) = do let es' = es ++ map (Apply . defaultArg) ws ++ map (fmap DTerm) zs (ps, (j, ren)) <- (`runStateT` (0, [])) $ mapM (traverse dtermToPat) es' let perm = Perm j (map snd $ List.sort ren) return (map ePatToPat ps, perm) dispToPats t = __IMPOSSIBLE__ bindVar i = do (j, is) <- get let i' = numVars - i - 1 case lookup i' is of Nothing -> True <$ put (j + 1, (i', j) : is) Just{} -> False <$ put (j + 1, is) skip = modify $ \(j, is) -> (j + 1, is) ePatToPat :: Elim' Pattern -> NamedArg Pattern ePatToPat (Apply p) = fmap unnamed p ePatToPat (Proj o d) = defaultNamedArg $ ProjP o d dtermToPat :: DisplayTerm -> StateT (Int, [(Int, Int)]) TCM Pattern dtermToPat v = case v of DWithApp{} -> __IMPOSSIBLE__ -- I believe DCon c ci vs -> ConP c (toConPatternInfo ci) . map (fmap unnamed) <$> mapM (traverse dtermToPat) vs DDef d es -> do ifM (return (null es) `and2M` do isJust <$> lift (isProjection d)) {-then-} (return $ ProjP ProjPrefix d) {-else-} (DotP (dtermToTerm v) <$ skip) DDot v -> DotP v <$ skip DTerm (Var i []) -> ifM (bindVar i) (varP . nameToPatVarName <$> lift (nameOfBV i)) (pure $ DotP (Var i [])) DTerm (Con c ci vs) -> ConP c (toConPatternInfo ci) . map (fmap unnamed) <$> mapM (traverse (dtermToPat . DTerm)) vs DTerm v -> DotP v <$ skip isWithFunction :: MonadTCM tcm => QName -> tcm (Maybe QName) isWithFunction x = liftTCM $ do def <- getConstInfo x return $ case theDef def of Function{ funWith = w } -> w _ -> Nothing expandWithFunctionCall :: QName -> Elims -> TCM Term expandWithFunctionCall f es = do as <- displayFormArities f case as of [a] | length es >= a -> do Just disp <- displayForm f es return $ dtermToTerm disp -- We might get an underapplied with function application (issue1598), in -- which case we have to eta expand. The resulting term is only used for -- termination checking, so we don't have to worry about getting hiding -- information right. -- Andreas, 2016-07-20 let's pray that there no copatterns needed... [a] -> do let pad = a - length es es' = raise pad es ++ map (Apply . defaultArg . var) (downFrom pad) Just disp <- displayForm f es' let info = setOrigin Inserted defaultArgInfo return $ foldr (\_ -> Lam info . Abs "") (dtermToTerm disp) (replicate pad ()) _ -> __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/Termination/Termination.hs0000644000000000000000000000621513154613124020252 0ustar0000000000000000{-# LANGUAGE ImplicitParams #-} -- | Termination checker, based on -- \"A Predicative Analysis of Structural Recursion\" by -- Andreas Abel and Thorsten Altenkirch (JFP'01), -- and -- \"The Size-Change Principle for Program Termination\" by -- Chin Soon Lee, Neil Jones, and Amir Ben-Amram (POPL'01). module Agda.Termination.Termination ( terminates , terminatesFilter , endos , idempotent ) where import Agda.Termination.CutOff import Agda.Termination.CallGraph import Agda.Termination.CallMatrix hiding (toList) import qualified Agda.Termination.CallMatrix as CMSet import Agda.Termination.Order import Agda.Termination.SparseMatrix import Agda.Utils.Either import Agda.Utils.List import Agda.Utils.Maybe import Data.Monoid -- | TODO: This comment seems to be partly out of date. -- -- @'terminates' cs@ checks if the functions represented by @cs@ -- terminate. The call graph @cs@ should have one entry ('Call') per -- recursive function application. -- -- @'Right' perms@ is returned if the functions are size-change terminating. -- -- If termination can not be established, then @'Left' problems@ is -- returned instead. Here @problems@ contains an -- indication of why termination cannot be established. See 'lexOrder' -- for further details. -- -- Note that this function assumes that all data types are strictly -- positive. -- -- The termination criterion is taken from Jones et al. -- In the completed call graph, each idempotent call-matrix -- from a function to itself must have a decreasing argument. -- Idempotency is wrt. matrix multiplication. -- -- This criterion is strictly more liberal than searching for a -- lexicographic order (and easier to implement, but harder to justify). terminates :: (Monoid cinfo, ?cutoff :: CutOff) => CallGraph cinfo -> Either cinfo () terminates cs = checkIdems $ endos $ toList $ complete cs terminatesFilter :: (Monoid cinfo, ?cutoff :: CutOff) => (Node -> Bool) -> CallGraph cinfo -> Either cinfo () terminatesFilter f cs = checkIdems $ endos $ filter f' $ toList $ complete cs where f' c = f (source c) && f (target c) endos :: [Call cinfo] -> [CallMatrixAug cinfo] endos cs = [ m | c <- cs, source c == target c , m <- CMSet.toList $ callMatrixSet c ] checkIdems :: (?cutoff :: CutOff) => [CallMatrixAug cinfo] -> Either cinfo () checkIdems calls = caseMaybe (headMaybe offending) (Right ()) $ Left . augCallInfo where -- Every idempotent call must have decrease, otherwise it offends us. offending = filter (not . hasDecrease) $ filter idempotent calls checkIdem :: (?cutoff :: CutOff) => CallMatrixAug cinfo -> Bool checkIdem c = if idempotent c then hasDecrease c else True -- | A call @c@ is idempotent if it is an endo (@'source' == 'target'@) -- of order 1. -- (Endo-calls of higher orders are e.g. argument permutations). -- We can test idempotency by self-composition. -- Self-composition @c >*< c@ should not make any parameter-argument relation -- worse. idempotent :: (?cutoff :: CutOff) => CallMatrixAug cinfo -> Bool idempotent (CallMatrixAug m _) = (m >*< m) `notWorse` m hasDecrease :: CallMatrixAug cinfo -> Bool hasDecrease = any isDecr . diagonal Agda-2.5.3/src/full/Agda/Termination/CallGraph.hs0000644000000000000000000001742013154613124017616 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE ImplicitParams #-} -- | Call graphs and related concepts, more or less as defined in -- \"A Predicative Analysis of Structural Recursion\" by -- Andreas Abel and Thorsten Altenkirch. -- Originally copied from Agda1 sources. module Agda.Termination.CallGraph ( -- * Calls Node , Call, mkCall, mkCall', source, target, callMatrixSet , (>*<) -- * Call graphs , CallGraph(..) , targetNodes , fromList , toList , union , insert , complete, completionStep -- , prettyBehaviour ) where import Prelude hiding (null) import qualified Data.List as List import Data.Semigroup import Data.Set (Set) import Agda.Termination.CallMatrix (CallMatrix, CallMatrixAug(..), CMSet(..), CallComb(..)) import qualified Agda.Termination.CallMatrix as CMSet import Agda.Termination.CutOff import Agda.Termination.SparseMatrix as Matrix import Agda.Utils.Favorites (Favorites) import qualified Agda.Utils.Favorites as Fav import Agda.Utils.Graph.AdjacencyMap.Unidirectional (Edge(..),Graph(..)) import qualified Agda.Utils.Graph.AdjacencyMap.Unidirectional as Graph import Agda.Utils.Function import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.PartialOrd import Agda.Utils.Pretty hiding ((<>)) import Agda.Utils.Singleton import Agda.Utils.Tuple ------------------------------------------------------------------------ -- Calls -- | Call graph nodes. -- -- Machine integer 'Int' is sufficient, since we cannot index more than -- we have addresses on our machine. type Node = Int -- | Calls are edges in the call graph. -- It can be labelled with several call matrices if there -- are several pathes from one function to another. type Call cinfo = Edge Node Node (CMSet cinfo) callMatrixSet :: Call cinfo -> CMSet cinfo callMatrixSet = label -- | Make a call with a single matrix. mkCall :: Node -> Node -> CallMatrix -> cinfo -> Call cinfo mkCall s t m cinfo = Edge s t $ singleton $ CallMatrixAug m cinfo -- | Make a call with empty @cinfo@. mkCall' :: Monoid cinfo => Node -> Node -> CallMatrix -> Call cinfo mkCall' s t m = mkCall s t m mempty ------------------------------------------------------------------------ -- Call graphs -- | A call graph is a set of calls. Every call also has some -- associated meta information, which should be 'Monoid'al so that the -- meta information for different calls can be combined when the calls -- are combined. newtype CallGraph cinfo = CallGraph { theCallGraph :: Graph Node Node (CMSet cinfo) } deriving (Show) -- | Returns all the nodes with incoming edges. Somewhat expensive. @O(e)@. targetNodes :: CallGraph cinfo -> Set Node targetNodes = Graph.targetNodes . theCallGraph -- | Converts a call graph to a list of calls with associated meta -- information. toList :: CallGraph cinfo -> [Call cinfo] toList = Graph.edges . theCallGraph -- | Converts a list of calls with associated meta information to a -- call graph. fromList :: [Call cinfo] -> CallGraph cinfo fromList = CallGraph . Graph.fromListWith CMSet.union -- | 'null' checks whether the call graph is completely disconnected. instance Null (CallGraph cinfo) where empty = CallGraph Graph.empty null = List.all (null . label) . toList -- | Takes the union of two call graphs. union :: CallGraph cinfo -> CallGraph cinfo -> CallGraph cinfo union (CallGraph cs1) (CallGraph cs2) = CallGraph $ Graph.unionWith CMSet.union cs1 cs2 -- | 'CallGraph' is a monoid under 'union'. instance Semigroup (CallGraph cinfo) where (<>) = union instance Monoid (CallGraph cinfo) where mempty = empty mappend = (<>) -- | Inserts a call into a call graph. insert :: Node -> Node -> CallMatrix -> cinfo -> CallGraph cinfo -> CallGraph cinfo insert s t cm cinfo = CallGraph . Graph.insertEdgeWith CMSet.union e . theCallGraph where e = mkCall s t cm cinfo -- * Combination of a new thing with an old thing -- returning a really new things and updated old things. type CombineNewOldT a = a -> a -> (a, a) class CombineNewOld a where combineNewOld :: CombineNewOldT a instance PartialOrd a => CombineNewOld (Favorites a) where combineNewOld new old = (new', Fav.unionCompared (new', old')) where (new', old') = Fav.compareFavorites new old deriving instance CombineNewOld (CMSet cinfo) instance (Monoid a, CombineNewOld a, Ord s, Ord t) => CombineNewOld (Graph s t a) where combineNewOld new old = Graph.unzip $ Graph.unionWith comb new' old' where new' = (,mempty) <$> new old' = (mempty,) <$> old comb (new1,old1) (new2,old2) -- TODO: ensure old1 is null = mapFst (new2 `mappend`) $ combineNewOld new1 old2 -- -- | old1 == mempty = mapFst (new2 `mappend`) $ combineNewOld new1 old2 -- -- | otherwise = __IMPOSSIBLE__ -- Filter unlabelled edges from the resulting new graph. -- filt = Graph.filterEdges (not . null) -- | Call graph combination. -- -- Application of '>*<' to all pairs @(c1,c2)@ -- for which @'source' c1 = 'target' c2@.) -- GHC supports implicit-parameter constraints in instance declarations -- only from 7.4. To maintain compatibility with 7.2, we skip this instance: -- KEEP: -- instance (Monoid cinfo, ?cutoff :: CutOff) => CombineNewOld (CallGraph cinfo) where -- combineNewOld (CallGraph new) (CallGraph old) = CallGraph -*- CallGraph $ combineNewOld comb old -- -- combined calls: -- where comb = Graph.composeWith (>*<) CMSet.union new old -- Non-overloaded version: combineNewOldCallGraph :: (Monoid cinfo, ?cutoff :: CutOff) => CombineNewOldT (CallGraph cinfo) combineNewOldCallGraph (CallGraph new) (CallGraph old) = CallGraph -*- CallGraph $ combineNewOld comb old -- combined calls: where comb = Graph.composeWith (>*<) CMSet.union new old -- | Call graph comparison. -- A graph @cs'@ is ``worse'' than @cs@ if it has a new edge (call) -- or a call got worse, which means that one of its elements -- that was better or equal to 'Le' moved a step towards 'Un'. -- -- A call graph is complete if combining it with itself does not make it -- any worse. This is sound because of monotonicity: By combining a graph -- with itself, it can only get worse, but if it does not get worse after -- one such step, it gets never any worse. -- | @'complete' cs@ completes the call graph @cs@. A call graph is -- complete if it contains all indirect calls; if @f -> g@ and @g -> -- h@ are present in the graph, then @f -> h@ should also be present. complete :: (?cutoff :: CutOff) => Monoid cinfo => CallGraph cinfo -> CallGraph cinfo complete cs = repeatWhile (mapFst (not . null) . completionStep cs) cs completionStep :: (?cutoff :: CutOff) => Monoid cinfo => CallGraph cinfo -> CallGraph cinfo -> (CallGraph cinfo, CallGraph cinfo) completionStep gOrig gThis = combineNewOldCallGraph gOrig gThis ------------------------------------------------------------------------ -- * Printing ------------------------------------------------------------------------ -- | Displays the recursion behaviour corresponding to a call graph. instance Pretty cinfo => Pretty (CallGraph cinfo) where pretty = vcat . map prettyCall . toList where prettyCall e = if null (callMatrixSet e) then empty else align 20 $ [ ("Source:", text $ show $ source e) , ("Target:", text $ show $ target e) , ("Matrix:", pretty $ callMatrixSet e) ] -- -- | Displays the recursion behaviour corresponding to a call graph. -- prettyBehaviour :: Show cinfo => CallGraph cinfo -> Doc -- prettyBehaviour = vcat . map prettyCall . filter toSelf . toList -- where -- toSelf c = source c == target c -- prettyCall e = vcat $ map text -- [ "Function: " ++ show (source e) -- -- , "Behaviour: " ++ show (diagonal $ mat $ cm c) -- TODO -- -- , "Meta info: " ++ show cinfo -- ] Agda-2.5.3/src/full/Agda/Termination/Order.hs0000644000000000000000000002662113154613124017037 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE ImplicitParams #-} -- | An Abstract domain of relative sizes, i.e., differences -- between size of formal function parameter and function argument -- in recursive call; used in the termination checker. module Agda.Termination.Order ( -- * Structural orderings Order(..), decr , increase, decrease, setUsability , (.*.) , supremum, infimum , orderSemiring , le, lt, unknown, orderMat, collapseO , nonIncreasing, decreasing, isDecr , NotWorse(..) , isOrder ) where import qualified Data.Foldable as Fold import qualified Data.List as List import Agda.Termination.CutOff import Agda.Termination.SparseMatrix as Matrix import Agda.Termination.Semiring (HasZero(..), Semiring) import qualified Agda.Termination.Semiring as Semiring import Agda.Utils.PartialOrd import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible ------------------------------------------------------------------------ -- Structural orderings -- | In the paper referred to above, there is an order R with -- @'Unknown' '<=' 'Le' '<=' 'Lt'@. -- -- This is generalized to @'Unknown' '<=' 'Decr k'@ where -- @Decr 1@ replaces @Lt@ and @Decr 0@ replaces @Le@. -- A negative decrease means an increase. The generalization -- allows the termination checker to record an increase by 1 which -- can be compensated by a following decrease by 2 which results in -- an overall decrease. -- -- However, the termination checker of the paper itself terminates because -- there are only finitely many different call-matrices. To maintain -- termination of the terminator we set a @cutoff@ point which determines -- how high the termination checker can count. This value should be -- set by a global or file-wise option. -- -- See 'Call' for more information. -- -- TODO: document orders which are call-matrices themselves. data Order = Decr !Bool {-# UNPACK #-} !Int -- ^ Decrease of callee argument wrt. caller parameter. -- -- The @Bool@ indicates whether the decrease (if any) is usable. -- In any chain, there needs to be one usable decrease. -- Unusable decreases come from SIZELT constraints which are -- not in inductive pattern match or a coinductive copattern match. -- See issue #2331. -- -- UPDATE: Andreas, 2017-07-26: -- Feature #2331 is unsound due to size quantification in terms. -- While the infrastructure for usable/unusable decrease remains in -- place, no unusable decreases are generated by TermCheck. | Unknown -- ^ No relation, infinite increase, or increase beyond termination depth. | Mat {-# UNPACK #-} !(Matrix Int Order) -- ^ Matrix-shaped order, currently UNUSED. deriving (Eq, Ord, Show) -- instance Show Order where -- show (Decr u k) = if u then show (- k) else "(" ++ show (-k) ++ ")" -- show Unknown = "." -- show (Mat m) = "Mat " ++ show m instance HasZero Order where zeroElement = Unknown -- | Information order: 'Unknown' is least information. -- The more we decrease, the more information we have. -- -- When having comparable call-matrices, we keep the lesser one. -- Call graph completion works toward losing the good calls, -- tending towards Unknown (the least information). instance PartialOrd Order where comparable o o' = case (o, o') of (Unknown, Unknown) -> POEQ (Unknown, _ ) -> POLT (_ , Unknown) -> POGT (Decr u k, Decr u' l) -> comparableBool u u' `orPO` comparableOrd k l -- Matrix-shaped orders are no longer supported (Mat{} , _ ) -> __IMPOSSIBLE__ (_ , Mat{} ) -> __IMPOSSIBLE__ where comparableBool = curry $ \case (False, True) -> POLT (True, False) -> POGT _ -> POEQ -- | A partial order, aimed at deciding whether a call graph gets -- worse during the completion. -- class NotWorse a where notWorse :: a -> a -> Bool -- | It does not get worse then ``increase''. -- If we are still decreasing, it can get worse: less decreasing. instance NotWorse Order where o `notWorse` Unknown = True -- we are unboundedly increasing Unknown `notWorse` Decr _ k = k < 0 -- we are increasing Decr u l `notWorse` Decr u' k = k < 0 -- we are increasing or || l >= k && (u || not u') -- we are decreasing, but not less, and not less usable -- Matrix-shaped orders are no longer supported Mat m `notWorse` o = __IMPOSSIBLE__ o `notWorse` Mat m = __IMPOSSIBLE__ {- Mat m `notWorse` Mat n = m `notWorse` n -- matrices are compared pointwise o `notWorse` Mat n = o `notWorse` collapse n -- or collapsed (sound?) Mat m `notWorse` o = collapse m `notWorse` o -} -- | We assume the matrices have the same dimension. instance (Ord i, HasZero o, NotWorse o) => NotWorse (Matrix i o) where m `notWorse` n | size m /= size n = __IMPOSSIBLE__ | otherwise = Fold.and $ zipMatrices onlym onlyn both trivial m n where -- If an element is only in @m@, then its 'Unknown' in @n@ -- so it gotten better at best, in any case, not worse. onlym o = True -- @== o `notWorse` Unknown@ onlyn o = zeroElement `notWorse` o both = notWorse trivial = id -- @True@ counts as zero as it is neutral for @and@ -- | Raw increase which does not cut off. increase :: Int -> Order -> Order increase i o = case o of Unknown -> Unknown Decr u k -> Decr u $ k - i -- TODO: should we set u to False if k - i < 0 ? Mat m -> Mat $ fmap (increase i) m -- | Raw decrease which does not cut off. decrease :: Int -> Order -> Order decrease i o = increase (-i) o setUsability :: Bool -> Order -> Order setUsability u o = case o of Decr _ k -> Decr u k Unknown -> o Mat{} -> o -- | Smart constructor for @Decr k :: Order@ which cuts off too big values. -- -- Possible values for @k@: @- ?cutoff '<=' k '<=' ?cutoff + 1@. decr :: (?cutoff :: CutOff) => Bool -> Int -> Order decr u k = case ?cutoff of CutOff c | k < -c -> Unknown | k > c -> Decr u $ c + 1 _ -> Decr u k -- | Smart constructor for matrix shaped orders, avoiding empty and singleton matrices. orderMat :: Matrix Int Order -> Order orderMat m | Matrix.isEmpty m = le -- 0x0 Matrix = neutral element | Just o <- isSingleton m = o -- 1x1 Matrix | otherwise = Mat m -- nxn Matrix withinCutOff :: (?cutoff :: CutOff) => Int -> Bool withinCutOff k = case ?cutoff of DontCutOff -> True CutOff c -> k >= -c && k <= c + 1 isOrder :: (?cutoff :: CutOff) => Order -> Bool isOrder (Decr _ k) = withinCutOff k isOrder Unknown = True isOrder (Mat m) = False -- TODO: extend to matrices -- | @le@, @lt@, @decreasing@, @unknown@: for backwards compatibility, and for external use. le :: Order le = Decr False 0 -- | Usable decrease. lt :: Order lt = Decr True 1 unknown :: Order unknown = Unknown nonIncreasing :: Order -> Bool nonIncreasing (Decr _ k) = k >= 0 nonIncreasing _ = False -- | Decreasing and usable? decreasing :: Order -> Bool decreasing (Decr u k) = u && k > 0 decreasing _ = False -- | Matrix-shaped order is decreasing if any diagonal element is decreasing. isDecr :: Order -> Bool isDecr (Mat m) = any isDecr $ diagonal m isDecr o = decreasing o instance Pretty Order where pretty (Decr u 0) = text "=" pretty (Decr u k) = mparens (not u) $ text $ show (0 - k) pretty Unknown = text "?" pretty (Mat m) = text "Mat" <+> pretty m -- | Multiplication of 'Order's. -- (Corresponds to sequential composition.) -- I think this funny pattern matching is because overlapping patterns -- are producing a warning and thus an error (strict compilation settings) (.*.) :: (?cutoff :: CutOff) => Order -> Order -> Order Unknown .*. _ = Unknown (Mat m) .*. Unknown = Unknown (Decr _ k) .*. Unknown = Unknown (Decr u k) .*. (Decr u' l) = decr (u || u') (k + l) -- if one is usable, so is the composition (Decr _ 0) .*. (Mat m) = Mat m (Decr u k) .*. (Mat m) = (Decr u k) .*. (collapse m) (Mat m1) .*. (Mat m2) | okM m1 m2 = Mat $ mul orderSemiring m1 m2 | otherwise = (collapse m1) .*. (collapse m2) (Mat m) .*. (Decr _ 0) = Mat m (Mat m) .*. (Decr u k) = (collapse m) .*. (Decr u k) -- | collapse @m@ -- -- We assume that @m@ codes a permutation: each row has at most one column -- that is not @Unknown@. -- -- To collapse a matrix into a single value, we take the best value of -- each column and multiply them. That means if one column is all @Unknown@, -- i.e., no argument relates to that parameter, then the collapsed value -- is also @Unknown@. -- -- This makes order multiplication associative. collapse :: (?cutoff :: CutOff) => Matrix Int Order -> Order collapse m = case toLists $ Matrix.transpose m of [] -> __IMPOSSIBLE__ -- This can never happen if order matrices are generated by the smart constructor m' -> foldl1 (.*.) $ map (foldl1 maxO) m' collapseO :: (?cutoff :: CutOff) => Order -> Order collapseO (Mat m) = collapse m collapseO o = o -- | Can two matrices be multplied together? okM :: Matrix Int Order -> Matrix Int Order -> Bool okM m1 m2 = (rows $ size m2) == (cols $ size m1) -- | The supremum of a (possibly empty) list of 'Order's. -- More information (i.e., more decrease) is bigger. -- 'Unknown' is no information, thus, smallest. supremum :: (?cutoff :: CutOff) => [Order] -> Order supremum = foldr maxO Unknown -- | @('Order', 'maxO', '.*.')@ forms a semiring, -- with 'Unknown' as zero and 'Le' as one. maxO :: (?cutoff :: CutOff) => Order -> Order -> Order maxO o1 o2 = case (o1,o2) of -- NOTE: strictly speaking the maximum does not exists -- which is better, an unusable decrease by 2 or a usable decrease by 1? -- We give the usable information priority if it is a decrease. (Decr False _, Decr True l) | l > 0 -> o2 (Decr True k, Decr False _) | k > 0 -> o1 (Decr u k, Decr u' l) -> if l > k then o2 else o1 (Unknown, _) -> o2 (_, Unknown) -> o1 (Mat m1, Mat m2) -> Mat (Matrix.add maxO m1 m2) (Mat m, _) -> maxO (collapse m) o2 (_, Mat m) -> maxO o1 (collapse m) -- | The infimum of a (non empty) list of 'Order's. -- Gets the worst information. -- 'Unknown' is the least element, thus, dominant. infimum :: (?cutoff :: CutOff) => [Order] -> Order infimum (o:l) = List.foldl' minO o l infimum [] = __IMPOSSIBLE__ -- | Pick the worst information. minO :: (?cutoff :: CutOff) => Order -> Order -> Order minO o1 o2 = case (o1,o2) of (Unknown, _) -> Unknown (_, Unknown) -> Unknown -- different usability: -- We pick the unusable one if it is not a decrease or -- decreases not more than the usable one. (Decr False k, Decr True l) -> if k <= 0 || k <= l then o1 else o2 (Decr True k, Decr False l) -> if l <= 0 || l <= k then o2 else o1 -- same usability: (Decr u k, Decr _ l) -> Decr u (min k l) (Mat m1, Mat m2) | size m1 == size m2 -> Mat $ Matrix.intersectWith minO m1 m2 | otherwise -> minO (collapse m1) (collapse m2) (Mat m1, _) -> minO (collapse m1) o2 (_, Mat m2) -> minO o1 (collapse m2) -- | We use a record for semiring instead of a type class -- since implicit arguments cannot occur in instance constraints, -- like @instance (?cutoff :: Int) => SemiRing Order@. orderSemiring :: (?cutoff :: CutOff) => Semiring Order orderSemiring = Semiring.Semiring { Semiring.add = maxO , Semiring.mul = (.*.) , Semiring.zero = Unknown -- , Semiring.one = Le } Agda-2.5.3/src/full/Agda/Termination/Monad.hs0000644000000000000000000005366213154613124017027 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} -- | The monad for the termination checker. -- -- The termination monad @TerM@ is an extension of -- the type checking monad 'TCM' by an environment -- with information needed by the termination checker. module Agda.Termination.Monad where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad.Reader import Control.Monad.State import Data.Foldable (Foldable) import Data.Traversable (Traversable) import Data.Semigroup (Semigroup(..), Monoid(..)) import Agda.Interaction.Options import Agda.Syntax.Abstract (IsProjP(..), AllNames) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.Syntax.Literal import Agda.Syntax.Position (noRange) import Agda.Termination.CutOff import Agda.Termination.Order (Order,le,unknown) import Agda.Termination.RecCheck (anyDefs) import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Benchmark import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Pretty hiding ((<>)) import Agda.TypeChecking.Records import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Monoid import Agda.Utils.Null import Agda.Utils.Pretty (Pretty, prettyShow) import qualified Agda.Utils.Pretty as P import Agda.Utils.VarSet (VarSet) import qualified Agda.Utils.VarSet as VarSet #include "undefined.h" import Agda.Utils.Impossible -- | The mutual block we are checking. -- -- The functions are numbered according to their order of appearance -- in this list. type MutualNames = [QName] -- | The target of the function we are checking. type Target = QName -- | The current guardedness level. type Guarded = Order -- | The termination environment. data TerEnv = TerEnv -- First part: options, configuration. { terUseDotPatterns :: Bool -- ^ Are we mining dot patterns to find evindence of structal descent? , terGuardingTypeConstructors :: Bool -- ^ Do we assume that record and data type constructors -- preserve guardedness? , terInlineWithFunctions :: Bool -- ^ Do we inline with functions to enhance termination checking of with? , terSizeSuc :: Maybe QName -- ^ The name of size successor, if any. , terSharp :: Maybe QName -- ^ The name of the delay constructor (sharp), if any. , terCutOff :: CutOff -- ^ Depth at which to cut off the structural order. -- Second part: accumulated info during descent into decls./term. , terCurrent :: QName -- ^ The name of the function we are currently checking. , terMutual :: MutualNames -- ^ The names of the functions in the mutual block we are checking. -- This includes the internally generated functions -- (with, extendedlambda, coinduction). , terUserNames :: [QName] -- ^ The list of name actually appearing in the file (abstract syntax). -- Excludes the internally generated functions. , terHaveInlinedWith :: Bool -- ^ Does the actual clause result from with-inlining? -- (If yes, it may be ill-typed.) , terTarget :: Maybe Target -- ^ Target type of the function we are currently termination checking. -- Only the constructors of 'Target' are considered guarding. , terDelayed :: Delayed -- ^ Are we checking a delayed definition? , terMaskArgs :: [Bool] -- ^ Only consider the 'notMasked' 'False' arguments for establishing termination. -- See issue #1023. , terMaskResult :: Bool -- ^ Only consider guardedness if 'False' (not masked). , _terSizeDepth :: Int -- lazy by intention! -- ^ How many @SIZELT@ relations do we have in the context -- (= clause telescope). Used to approximate termination -- for metas in call args. , terPatterns :: MaskedDeBruijnPatterns -- ^ The patterns of the clause we are checking. , terPatternsRaise :: !Int -- ^ Number of additional binders we have gone under -- (and consequently need to raise the patterns to compare to terms). -- Updated during call graph extraction, hence strict. , terGuarded :: !Guarded -- ^ The current guardedness status. Changes as we go deeper into the term. -- Updated during call graph extraction, hence strict. , terUseSizeLt :: Bool -- ^ When extracting usable size variables during construction of the call -- matrix, can we take the variable for use with SIZELT constraints from the context? -- Yes, if we are under an inductive constructor. -- No, if we are under a record constructor. -- (See issue #1015). , terUsableVars :: VarSet -- ^ Pattern variables that can be compared to argument variables using SIZELT. } -- | An empty termination environment. -- -- Values are set to a safe default meaning that with these -- initial values the termination checker will not miss -- termination errors it would have seen with better settings -- of these values. -- -- Values that do not have a safe default are set to -- @IMPOSSIBLE@. -- Note: Do not write @__IMPOSSIBLE__@ in the haddock comment above -- since it will be expanded by the CPP, leading to a haddock parse error. defaultTerEnv :: TerEnv defaultTerEnv = TerEnv { terUseDotPatterns = False -- must be False initially! , terGuardingTypeConstructors = False , terInlineWithFunctions = True , terSizeSuc = Nothing , terSharp = Nothing , terCutOff = defaultCutOff , terUserNames = __IMPOSSIBLE__ -- needs to be set! , terMutual = __IMPOSSIBLE__ -- needs to be set! , terCurrent = __IMPOSSIBLE__ -- needs to be set! , terHaveInlinedWith = False , terTarget = Nothing , terDelayed = NotDelayed , terMaskArgs = repeat False -- use all arguments (mask none) , terMaskResult = False -- use result (do not mask) , _terSizeDepth = __IMPOSSIBLE__ -- needs to be set! , terPatterns = __IMPOSSIBLE__ -- needs to be set! , terPatternsRaise = 0 , terGuarded = le -- not initially guarded , terUseSizeLt = False -- initially, not under data constructor , terUsableVars = VarSet.empty } -- | Termination monad service class. class (Functor m, Monad m) => MonadTer m where terAsk :: m TerEnv terLocal :: (TerEnv -> TerEnv) -> m a -> m a terAsks :: (TerEnv -> a) -> m a terAsks f = f <$> terAsk -- | Termination monad. newtype TerM a = TerM { terM :: ReaderT TerEnv TCM a } deriving (Functor, Applicative, Monad, MonadBench Phase, HasOptions, MonadDebug) instance MonadTer TerM where terAsk = TerM $ ask terLocal f = TerM . local f . terM -- | Generic run method for termination monad. runTer :: TerEnv -> TerM a -> TCM a runTer tenv (TerM m) = runReaderT m tenv -- | Run TerM computation in default environment (created from options). runTerDefault :: TerM a -> TCM a runTerDefault cont = do -- Assemble then initial configuration of the termination environment. cutoff <- optTerminationDepth <$> pragmaOptions -- Get the name of size suc (if sized types are enabled) suc <- sizeSucName -- The name of sharp (if available). sharp <- fmap nameOfSharp <$> coinductionKit guardingTypeConstructors <- optGuardingTypeConstructors <$> pragmaOptions -- Andreas, 2014-08-28 -- We do not inline with functions if --without-K. inlineWithFunctions <- not . optWithoutK <$> pragmaOptions let tenv = defaultTerEnv { terGuardingTypeConstructors = guardingTypeConstructors , terInlineWithFunctions = inlineWithFunctions , terSizeSuc = suc , terSharp = sharp , terCutOff = cutoff } runTer tenv cont -- * Termination monad is a 'MonadTCM'. instance MonadReader TCEnv TerM where ask = TerM $ lift $ ask local f m = TerM $ ReaderT $ local f . runReaderT (terM m) instance MonadState TCState TerM where get = TerM $ lift $ get put = TerM . lift . put instance MonadIO TerM where liftIO = TerM . liftIO instance MonadTCM TerM where liftTCM = TerM . lift instance MonadError TCErr TerM where throwError = liftTCM . throwError catchError m handler = TerM $ ReaderT $ \ tenv -> do runTer tenv m `catchError` (\ err -> runTer tenv $ handler err) instance HasConstInfo TerM where getConstInfo = liftTCM . getConstInfo getRewriteRulesFor = liftTCM . getRewriteRulesFor instance Semigroup m => Semigroup (TerM m) where (<>) = liftA2 (<>) instance (Semigroup m, Monoid m) => Monoid (TerM m) where mempty = pure mempty mappend = (<>) mconcat = mconcat <.> sequence -- * Modifiers and accessors for the termination environment in the monad. terGetGuardingTypeConstructors :: TerM Bool terGetGuardingTypeConstructors = terAsks terGuardingTypeConstructors terGetInlineWithFunctions :: TerM Bool terGetInlineWithFunctions = terAsks terInlineWithFunctions terGetUseDotPatterns :: TerM Bool terGetUseDotPatterns = terAsks terUseDotPatterns terSetUseDotPatterns :: Bool -> TerM a -> TerM a terSetUseDotPatterns b = terLocal $ \ e -> e { terUseDotPatterns = b } terGetSizeSuc :: TerM (Maybe QName) terGetSizeSuc = terAsks terSizeSuc terGetCurrent :: TerM QName terGetCurrent = terAsks terCurrent terSetCurrent :: QName -> TerM a -> TerM a terSetCurrent q = terLocal $ \ e -> e { terCurrent = q } terGetSharp :: TerM (Maybe QName) terGetSharp = terAsks terSharp terGetCutOff :: TerM CutOff terGetCutOff = terAsks terCutOff terGetMutual :: TerM MutualNames terGetMutual = terAsks terMutual terGetUserNames :: TerM [QName] terGetUserNames = terAsks terUserNames terGetTarget :: TerM (Maybe Target) terGetTarget = terAsks terTarget terSetTarget :: Maybe Target -> TerM a -> TerM a terSetTarget t = terLocal $ \ e -> e { terTarget = t } terGetHaveInlinedWith :: TerM Bool terGetHaveInlinedWith = terAsks terHaveInlinedWith terSetHaveInlinedWith :: TerM a -> TerM a terSetHaveInlinedWith = terLocal $ \ e -> e { terHaveInlinedWith = True } terGetDelayed :: TerM Delayed terGetDelayed = terAsks terDelayed terSetDelayed :: Delayed -> TerM a -> TerM a terSetDelayed b = terLocal $ \ e -> e { terDelayed = b } terGetMaskArgs :: TerM [Bool] terGetMaskArgs = terAsks terMaskArgs terSetMaskArgs :: [Bool] -> TerM a -> TerM a terSetMaskArgs b = terLocal $ \ e -> e { terMaskArgs = b } terGetMaskResult :: TerM Bool terGetMaskResult = terAsks terMaskResult terSetMaskResult :: Bool -> TerM a -> TerM a terSetMaskResult b = terLocal $ \ e -> e { terMaskResult = b } terGetPatterns :: TerM (MaskedDeBruijnPatterns) terGetPatterns = do n <- terAsks terPatternsRaise mps <- terAsks terPatterns return $ if n == 0 then mps else map (fmap (raise n)) mps terSetPatterns :: MaskedDeBruijnPatterns -> TerM a -> TerM a terSetPatterns ps = terLocal $ \ e -> e { terPatterns = ps } terRaise :: TerM a -> TerM a terRaise = terLocal $ \ e -> e { terPatternsRaise = terPatternsRaise e + 1 } terGetGuarded :: TerM Guarded terGetGuarded = terAsks terGuarded terModifyGuarded :: (Order -> Order) -> TerM a -> TerM a terModifyGuarded f = terLocal $ \ e -> e { terGuarded = f $ terGuarded e } terSetGuarded :: Order -> TerM a -> TerM a terSetGuarded = terModifyGuarded . const terUnguarded :: TerM a -> TerM a terUnguarded = terSetGuarded unknown -- | Should the codomain part of a function type preserve guardedness? terPiGuarded :: TerM a -> TerM a terPiGuarded m = ifM terGetGuardingTypeConstructors m $ terUnguarded m -- | Lens for '_terSizeDepth'. terSizeDepth :: Lens' Int TerEnv terSizeDepth f e = f (_terSizeDepth e) <&> \ i -> e { _terSizeDepth = i } -- | Lens for 'terUsableVars'. terGetUsableVars :: TerM VarSet terGetUsableVars = terAsks terUsableVars terModifyUsableVars :: (VarSet -> VarSet) -> TerM a -> TerM a terModifyUsableVars f = terLocal $ \ e -> e { terUsableVars = f $ terUsableVars e } terSetUsableVars :: VarSet -> TerM a -> TerM a terSetUsableVars = terModifyUsableVars . const -- | Lens for 'terUseSizeLt'. terGetUseSizeLt :: TerM Bool terGetUseSizeLt = terAsks terUseSizeLt terModifyUseSizeLt :: (Bool -> Bool) -> TerM a -> TerM a terModifyUseSizeLt f = terLocal $ \ e -> e { terUseSizeLt = f $ terUseSizeLt e } terSetUseSizeLt :: Bool -> TerM a -> TerM a terSetUseSizeLt = terModifyUseSizeLt . const -- | Compute usable vars from patterns and run subcomputation. withUsableVars :: UsableSizeVars a => a -> TerM b -> TerM b withUsableVars pats m = do vars <- usableSizeVars pats reportSLn "term.size" 70 $ "usableSizeVars = " ++ show vars reportSDoc "term.size" 20 $ if null vars then text "no usuable size vars" else text "the size variables amoung these variables are usable: " <+> sep (map (prettyTCM . var) $ VarSet.toList vars) terSetUsableVars vars $ m -- | Set 'terUseSizeLt' when going under constructor @c@. conUseSizeLt :: QName -> TerM a -> TerM a conUseSizeLt c m = do caseMaybeM (liftTCM $ isRecordConstructor c) (terSetUseSizeLt True m) (const $ terSetUseSizeLt False m) -- | Set 'terUseSizeLt' for arguments following projection @q@. -- We disregard j TerM a -> TerM a projUseSizeLt q m = do co <- isCoinductiveProjection False q reportSLn "term.size" 20 $ applyUnless co ("not " ++) $ "using SIZELT vars after projection " ++ prettyShow q terSetUseSizeLt co m -- | For termination checking purposes flat should not be considered a -- projection. That is, it flat doesn't preserve either structural order -- or guardedness like other projections do. -- Andreas, 2012-06-09: the same applies to projections of recursive records. isProjectionButNotCoinductive :: MonadTCM tcm => QName -> tcm Bool isProjectionButNotCoinductive qn = liftTCM $ do b <- isProjectionButNotCoinductive' qn reportSDoc "term.proj" 60 $ do text "identifier" <+> prettyTCM qn <+> do text $ if b then "is an inductive projection" else "is either not a projection or coinductive" return b where isProjectionButNotCoinductive' qn = do flat <- fmap nameOfFlat <$> coinductionKit if Just qn == flat then return False else do mp <- isProjection qn case mp of Just Projection{ projProper = Just{}, projFromType = t } -> isInductiveRecord (unArg t) _ -> return False -- | Check whether a projection belongs to a coinductive record -- and is actually recursive. -- E.g. -- @ -- isCoinductiveProjection (Stream.head) = return False -- -- isCoinductiveProjection (Stream.tail) = return True -- @ isCoinductiveProjection :: MonadTCM tcm => Bool -> QName -> tcm Bool isCoinductiveProjection mustBeRecursive q = liftTCM $ do reportSLn "term.guardedness" 40 $ "checking isCoinductiveProjection " ++ prettyShow q flat <- fmap nameOfFlat <$> coinductionKit -- yes for ♭ if Just q == flat then return True else do pdef <- getConstInfo q case isProjection_ (theDef pdef) of Just Projection{ projProper = Just{}, projFromType = Arg _ r, projIndex = n } -> caseMaybeM (isRecord r) __IMPOSSIBLE__ $ \ rdef -> do -- no for inductive or non-recursive record if recInduction rdef /= Just CoInductive then return False else do reportSLn "term.guardedness" 40 $ prettyShow q ++ " is coinductive; record type is " ++ prettyShow r if not mustBeRecursive then return True else do reportSLn "term.guardedness" 40 $ prettyShow q ++ " must be recursive" if not (recRecursive rdef) then return False else do reportSLn "term.guardedness" 40 $ prettyShow q ++ " has been declared recursive, doing actual check now..." -- TODO: the following test for recursiveness of a projection should be cached. -- E.g., it could be stored in the @Projection@ component. -- Now check if type of field mentions mutually recursive symbol. -- Get the type of the field by dropping record parameters and record argument. let TelV tel core = telView' (defType pdef) (pars, tel') = splitAt n $ telToList tel mut = fromMaybe __IMPOSSIBLE__ $ recMutual rdef -- Check if any recursive symbols appear in the record type. -- Q (2014-07-01): Should we normalize the type? -- A (2017-01-13): Yes, since we also normalize during positivity check? -- See issue #1899. reportSDoc "term.guardedness" 40 $ inTopContext $ sep [ text "looking for recursive occurrences of" , sep (map prettyTCM mut) , text "in" , addContext pars $ prettyTCM (telFromList tel') , text "and" , addContext tel $ prettyTCM core ] when (null mut) __IMPOSSIBLE__ names <- anyDefs mut =<< normalise (map (snd . unDom) tel', core) reportSDoc "term.guardedness" 40 $ text "found" <+> if null names then text "none" else sep (map prettyTCM names) return $ not $ null names _ -> do reportSLn "term.guardedness" 40 $ prettyShow q ++ " is not a proper projection" return False -- * De Bruijn pattern stuff -- | How long is the path to the deepest atomic pattern? patternDepth :: forall a. Pattern' a -> Int patternDepth = getMaxNat . foldrPattern depth where depth :: Pattern' a -> MaxNat -> MaxNat depth ConP{} = succ -- add 1 to the maximum of the depth of the subpatterns depth _ = id -- atomic pattern (leaf) has depth 0 -- | A dummy pattern used to mask a pattern that cannot be used -- for structural descent. unusedVar :: DeBruijnPattern unusedVar = LitP (LitString noRange "term.unused.pat.var") -- | Extract variables from 'DeBruijnPattern's that could witness a decrease -- via a SIZELT constraint. -- -- These variables must be under an inductive constructor (with no record -- constructor in the way), or after a coinductive projection (with no -- inductive one in the way). class UsableSizeVars a where usableSizeVars :: a -> TerM VarSet instance UsableSizeVars DeBruijnPattern where usableSizeVars = foldrPattern $ \case VarP x -> const $ ifM terGetUseSizeLt (return $ VarSet.singleton $ dbPatVarIndex x) $ {-else-} return mempty ConP c _ _ -> conUseSizeLt $ conName c LitP{} -> none DotP{} -> none AbsurdP{} -> none ProjP{} -> none where none _ = return mempty instance UsableSizeVars [DeBruijnPattern] where usableSizeVars ps = case ps of [] -> return mempty (ProjP _ q : ps) -> projUseSizeLt q $ usableSizeVars ps (p : ps) -> mappend <$> usableSizeVars p <*> usableSizeVars ps instance UsableSizeVars (Masked DeBruijnPattern) where usableSizeVars (Masked m p) = (`foldrPattern` p) $ \case VarP x -> const $ ifM terGetUseSizeLt (return $ VarSet.singleton $ dbPatVarIndex x) $ {-else-} return mempty ConP c _ _ -> if m then none else conUseSizeLt $ conName c LitP{} -> none DotP{} -> none AbsurdP{} -> none ProjP{} -> none where none _ = return mempty instance UsableSizeVars MaskedDeBruijnPatterns where usableSizeVars ps = case ps of [] -> return mempty (Masked _ (ProjP _ q) : ps) -> projUseSizeLt q $ usableSizeVars ps (p : ps) -> mappend <$> usableSizeVars p <*> usableSizeVars ps -- * Masked patterns (which are not eligible for structural descent, only for size descent) -- See issue #1023. type MaskedDeBruijnPatterns = [Masked DeBruijnPattern] data Masked a = Masked { getMask :: Bool -- ^ True if thing not eligible for structural descent. , getMasked :: a -- ^ Thing. } deriving (Eq, Ord, Show, Functor, Foldable, Traversable) masked :: a -> Masked a masked = Masked True notMasked :: a -> Masked a notMasked = Masked False instance Decoration Masked where traverseF f (Masked m a) = Masked m <$> f a -- | Print masked things in double parentheses. instance PrettyTCM a => PrettyTCM (Masked a) where prettyTCM (Masked m a) = applyWhen m (parens . parens) $ prettyTCM a -- * Call pathes -- | The call information is stored as free monoid -- over 'CallInfo'. As long as we never look at it, -- only accumulate it, it does not matter whether we use -- 'Set', (nub) list, or 'Tree'. -- Internally, due to lazyness, it is anyway a binary tree of -- 'mappend' nodes and singleton leafs. -- Since we define no order on 'CallInfo' (expensive), -- we cannot use a 'Set' or nub list. -- Performance-wise, I could not see a difference between Set and list. newtype CallPath = CallPath { callInfos :: [CallInfo] } deriving (Show, Semigroup, Monoid, AllNames) -- | Only show intermediate nodes. (Drop last 'CallInfo'). instance Pretty CallPath where pretty (CallPath cis0) = if null cis then empty else P.hsep (map (\ ci -> arrow P.<+> P.pretty ci) cis) P.<+> arrow where cis = init cis0 arrow = P.text "-->" -- * Size depth estimation -- | A very crude way of estimating the @SIZELT@ chains -- @i > j > k@ in context. Returns 3 in this case. -- Overapproximates. -- TODO: more precise analysis, constructing a tree -- of relations between size variables. terSetSizeDepth :: Telescope -> TerM a -> TerM a terSetSizeDepth tel cont = do n <- liftTCM $ sum <$> do forM (telToList tel) $ \ dom -> do a <- reduce $ snd $ unDom dom ifM (isJust <$> isSizeType a) (return 1) {- else -} $ case ignoreSharing $ unEl a of MetaV{} -> return 1 _ -> return 0 terLocal (set terSizeDepth n) cont Agda-2.5.3/src/full/Agda/Termination/CutOff.hs0000644000000000000000000000146513154613124017151 0ustar0000000000000000{-# LANGUAGE CPP #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE DeriveDataTypeable #-} #endif -- | Defines 'CutOff' type which is used in "Agda.Interaction.Options". -- This module's purpose is to eliminate the dependency of -- "Agda.TypeChecking.Monad.Base" on the termination checker and -- everything it imports. module Agda.Termination.CutOff where #if __GLASGOW_HASKELL__ <= 708 import Data.Typeable ( Typeable ) #endif -- | Cut off structural order comparison at some depth in termination checker? data CutOff = CutOff Int -- ^ @c >= 0@ means: record decrease up to including @c+1@. | DontCutOff deriving ( Eq , Ord #if __GLASGOW_HASKELL__ <= 708 , Typeable #endif ) instance Show CutOff where show (CutOff k) = show k show DontCutOff = "∞" -- That's it! Agda-2.5.3/src/full/Agda/Utils/0000755000000000000000000000000013154613124014230 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Utils/BiMap.hs0000644000000000000000000000426113154613124015557 0ustar0000000000000000{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} -- | Finite bijections (implemented as a pair of tree maps). module Agda.Utils.BiMap where import Prelude hiding (lookup, unzip) import Control.Applicative ((<*>)) import Data.Function import Data.Functor import qualified Data.List as List import Data.Map (Map) import qualified Data.Map as Map import Data.Tuple import Data.Typeable ( Typeable ) -- | Finite bijective map from @a@ to @b@. There, and back again. data BiMap a b = BiMap { biMapThere :: Map a b , biMapBack :: Map b a } deriving (Typeable) -- | Lookup. O(log n). lookup :: Ord a => a -> BiMap a b -> Maybe b lookup a = Map.lookup a . biMapThere -- | Inverse lookup. O(log n). invLookup :: Ord b => b -> BiMap a b -> Maybe a invLookup b = Map.lookup b . biMapBack -- | Empty bimap. O(1). empty :: BiMap a b empty = BiMap Map.empty Map.empty -- | Singleton bimap. O(1). singleton :: a -> b -> BiMap a b singleton a b = BiMap (Map.singleton a b) (Map.singleton b a) -- | Insert. Overwrites existing value if present. O(Map.insert). insert :: (Ord a, Ord b) => a -> b -> BiMap a b -> BiMap a b insert a b (BiMap t u) = BiMap (Map.insert a b t) (Map.insert b a u) -- | Left-biased Union. O(Map.union). union :: (Ord a, Ord b) => BiMap a b -> BiMap a b -> BiMap a b union (BiMap t1 b1) (BiMap t2 b2) = BiMap (Map.union t1 t2) (Map.union b1 b2) -- | Construct from a list of pairs. -- -- Does not check for actual bijectivity of constructed finite map. O(n log n) fromList :: (Ord a, Ord b) => [(a,b)] -> BiMap a b fromList = List.foldl' (flip (uncurry insert)) empty -- | Turn into list, sorted ascendingly by first value. O(Map.toList) toList :: BiMap a b -> [(a,b)] toList = Map.toAscList . biMapThere ------------------------------------------------------------------------ -- Instances ------------------------------------------------------------------------ instance (Ord a, Ord b) => Eq (BiMap a b) where (==) = (==) `on` biMapThere instance (Ord a, Ord b) => Ord (BiMap a b) where compare = compare `on` biMapThere instance (Show a, Show b) => Show (BiMap a b) where show bimap = "Agda.Utils.BiMap.fromList " ++ show (toList bimap) Agda-2.5.3/src/full/Agda/Utils/Maybe.hs0000644000000000000000000000663513154613124015633 0ustar0000000000000000-- | Extend 'Data.Maybe' by common operations for the 'Maybe' type. -- -- Note: since this module is usually imported unqualified, -- we do not use short names, but all names contain 'Maybe', -- 'Just', or 'Nothing. module Agda.Utils.Maybe ( module Agda.Utils.Maybe , module Data.Maybe ) where import Control.Monad import Control.Monad.Trans.Maybe import Data.Maybe import Data.Functor -- * Conversion. -- | Retain object when tag is 'True'. boolToMaybe :: Bool -> a -> Maybe a boolToMaybe b x = if b then Just x else Nothing -- * Collection operations. -- | @unionWith@ for collections of size <= 1. unionMaybeWith :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a unionMaybeWith f Nothing mb = mb unionMaybeWith f ma Nothing = ma unionMaybeWith f (Just a) (Just b) = Just $ f a b -- | Unzipping a list of length <= 1. unzipMaybe :: Maybe (a,b) -> (Maybe a, Maybe b) unzipMaybe Nothing = (Nothing, Nothing) unzipMaybe (Just (a,b)) = (Just a, Just b) -- | Filtering a singleton list. -- -- @filterMaybe p a = 'listToMaybe' ('filter' p [a])@ filterMaybe :: (a -> Bool) -> a -> Maybe a filterMaybe p a | p a = Just a | otherwise = Nothing -- * Conditionals and loops. -- | Version of 'mapMaybe' with different argument ordering. forMaybe :: [a] -> (a -> Maybe b) -> [b] forMaybe = flip mapMaybe -- | Version of 'maybe' with different argument ordering. -- Often, we want to case on a 'Maybe', do something interesting -- in the 'Just' case, but only a default action in the 'Nothing' -- case. Then, the argument ordering of @caseMaybe@ is preferable. -- -- @caseMaybe m d f = flip (maybe d) m f@ caseMaybe :: Maybe a -> b -> (a -> b) -> b caseMaybe m d f = maybe d f m -- | 'caseMaybe' with flipped branches. ifJust :: Maybe a -> (a -> b) -> b -> b ifJust m f d = maybe d f m -- * Monads and Maybe. -- | Monadic version of 'maybe'. maybeM :: Monad m => m b -> (a -> m b) -> m (Maybe a) -> m b maybeM n j mm = maybe n j =<< mm -- | Monadic version of 'fromMaybe'. fromMaybeM :: Monad m => m a -> m (Maybe a) -> m a fromMaybeM m mm = maybeM m return mm -- | Monadic version of 'caseMaybe'. -- That is, 'maybeM' with a different argument ordering. caseMaybeM :: Monad m => m (Maybe a) -> m b -> (a -> m b) -> m b caseMaybeM mm d f = maybeM d f mm -- | 'caseMaybeM' with flipped branches. ifJustM :: Monad m => m (Maybe a) -> (a -> m b) -> m b -> m b ifJustM mm = flip (caseMaybeM mm) -- | A more telling name for 'Traversable.forM_' for the 'Maybe' collection type. -- Or: 'caseMaybe' without the 'Nothing' case. whenJust :: Monad m => Maybe a -> (a -> m ()) -> m () whenJust m k = caseMaybe m (return ()) k -- | 'caseMaybe' without the 'Just' case. whenNothing :: Monad m => Maybe a -> m () -> m () whenNothing m d = caseMaybe m d (\_ -> return ()) -- | 'caseMaybeM' without the 'Nothing' case. whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m () whenJustM c m = c >>= (`whenJust` m) -- | 'caseMaybeM' without the 'Just' case. whenNothingM :: Monad m => m (Maybe a) -> m () -> m () whenNothingM mm d = mm >>= (`whenNothing` d) -- | Lazy version of @allJust <.> sequence@. -- (@allJust = mapM@ for the @Maybe@ monad.) -- Only executes monadic effect while @isJust@. allJustM :: Monad m => [m (Maybe a)] -> m (Maybe [a]) allJustM = runMaybeT . mapM MaybeT -- allJustM [] = return $ Just [] -- allJustM (mm : mms) = caseMaybeM mm (return Nothing) $ \ a -> -- fmap (a:) <$> allJust mms Agda-2.5.3/src/full/Agda/Utils/Trie.hs0000644000000000000000000001325513154613124015475 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE BangPatterns #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE DeriveDataTypeable #-} #endif -- | Strict tries (based on "Data.Map.Strict" and "Agda.Utils.Maybe.Strict"). module Agda.Utils.Trie ( Trie(..) , empty, singleton, everyPrefix, insert, insertWith, union, unionWith , adjust, delete , toList, toAscList, toListOrderedBy , lookup, member, lookupPath, lookupTrie , mapSubTries, filter ) where import Prelude hiding (null, lookup, filter) import qualified Prelude import Control.Monad import Data.Function import Data.Functor import Data.Foldable (Foldable) import qualified Data.Maybe as Lazy import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map import Data.Monoid import qualified Data.List as List #if __GLASGOW_HASKELL__ <= 708 import Data.Typeable ( Typeable ) #endif import qualified Agda.Utils.Maybe.Strict as Strict import Agda.Utils.Null -- | Finite map from @[k]@ to @v@. -- -- With the strict 'Maybe' type, 'Trie' is also strict in 'v'. data Trie k v = Trie !(Strict.Maybe v) !(Map k (Trie k v)) deriving ( Show , Eq , Functor , Foldable #if __GLASGOW_HASKELL__ <= 708 , Typeable #endif ) -- | Empty trie. instance Null (Trie k v) where empty = Trie Strict.Nothing Map.empty null (Trie v t) = null v && null t -- | Helper function used to implement 'singleton' and 'everyPrefix'. singletonOrEveryPrefix :: Bool -> [k] -> v -> Trie k v singletonOrEveryPrefix _ [] !v = Trie (Strict.Just v) Map.empty singletonOrEveryPrefix everyPrefix (x : xs) !v = Trie (if everyPrefix then Strict.Just v else Strict.Nothing) (Map.singleton x (singletonOrEveryPrefix everyPrefix xs v)) -- | Singleton trie. singleton :: [k] -> v -> Trie k v singleton = singletonOrEveryPrefix False -- | @everyPrefix k v@ is a trie where every prefix of @k@ (including -- @k@ itself) is mapped to @v@. everyPrefix :: [k] -> v -> Trie k v everyPrefix = singletonOrEveryPrefix True -- | Left biased union. -- -- @union = unionWith (\ new old -> new)@. union :: (Ord k) => Trie k v -> Trie k v -> Trie k v union = unionWith const -- | Pointwise union with merge function for values. unionWith :: (Ord k) => (v -> v -> v) -> Trie k v -> Trie k v -> Trie k v unionWith f (Trie v ss) (Trie w ts) = Trie (Strict.unionMaybeWith f v w) (Map.unionWith (unionWith f) ss ts) -- | Insert. Overwrites existing value if present. -- -- @insert = insertWith (\ new old -> new)@ insert :: (Ord k) => [k] -> v -> Trie k v -> Trie k v insert k v t = union (singleton k v) t -- | Insert with function merging new value with old value. insertWith :: (Ord k) => (v -> v -> v) -> [k] -> v -> Trie k v -> Trie k v insertWith f k v t = unionWith f (singleton k v) t -- | Delete value at key, but leave subtree intact. delete :: Ord k => [k] -> Trie k v -> Trie k v delete path = adjust path (const Strict.Nothing) -- | Adjust value at key, leave subtree intact. adjust :: Ord k => [k] -> (Strict.Maybe v -> Strict.Maybe v) -> Trie k v -> Trie k v adjust path f t@(Trie v ts) = case path of -- case: found the value we want to adjust: adjust it! [] -> Trie (f v) ts -- case: found the subtrie matching the first key: adjust recursively k : ks | Just s <- Map.lookup k ts -> Trie v $ Map.insert k (adjust ks f s) ts -- case: subtrie not found: leave trie untouched _ -> t -- | Convert to ascending list. toList :: Ord k => Trie k v -> [([k],v)] toList = toAscList -- | Convert to ascending list. toAscList :: Ord k => Trie k v -> [([k],v)] toAscList (Trie mv ts) = Strict.maybeToList (([],) <$> mv) ++ [ (k:ks, v) | (k, t) <- Map.toAscList ts , (ks, v) <- toAscList t ] -- | Convert to list where nodes at the same level are ordered according to the -- given ordering. toListOrderedBy :: Ord k => (v -> v -> Ordering) -> Trie k v -> [([k], v)] toListOrderedBy cmp (Trie mv ts) = Strict.maybeToList (([],) <$> mv) ++ [ (k : ks, v) | (k, t) <- List.sortBy (cmp' `on` val . snd) $ Map.toAscList ts, (ks, v) <- toListOrderedBy cmp t ] where cmp' Strict.Nothing Strict.Just{} = LT cmp' Strict.Just{} Strict.Nothing = GT cmp' Strict.Nothing Strict.Nothing = EQ cmp' (Strict.Just x) (Strict.Just y) = cmp x y val (Trie mv _) = mv -- | Create new values based on the entire subtrie. Almost, but not quite -- comonad extend. mapSubTries :: Ord k => (Trie k u -> Maybe v) -> Trie k u -> Trie k v mapSubTries f t@(Trie mv ts) = Trie (Strict.toStrict (f t)) (fmap (mapSubTries f) ts) -- | Returns the value associated with the given key, if any. lookup :: Ord k => [k] -> Trie k v -> Maybe v lookup [] (Trie v _) = Strict.toLazy v lookup (k : ks) (Trie _ ts) = case Map.lookup k ts of Nothing -> Nothing Just t -> lookup ks t -- | Is the given key present in the trie? member :: Ord k => [k] -> Trie k v -> Bool member ks t = Lazy.isJust (lookup ks t) -- | Collect all values along a given path. lookupPath :: Ord k => [k] -> Trie k v -> [v] lookupPath xs (Trie v cs) = case xs of [] -> Strict.maybeToList v x : xs -> Strict.maybeToList v ++ maybe [] (lookupPath xs) (Map.lookup x cs) -- | Get the subtrie rooted at the given key. lookupTrie :: Ord k => [k] -> Trie k v -> Trie k v lookupTrie [] t = t lookupTrie (k : ks) (Trie _ cs) = maybe empty (lookupTrie ks) (Map.lookup k cs) -- | Filter a trie. filter :: Ord k => (v -> Bool) -> Trie k v -> Trie k v filter p (Trie mv ts) = Trie mv' (Map.filter (not . null) $ filter p <$> ts) where mv' = case mv of Strict.Just v | p v -> mv _ -> Strict.Nothing Agda-2.5.3/src/full/Agda/Utils/Cluster.hs0000644000000000000000000000251313154613124016206 0ustar0000000000000000 -- | Create clusters of non-overlapping things. module Agda.Utils.Cluster ( C , cluster , cluster' ) where import Control.Monad -- An imperative union-find library: import Data.Equivalence.Monad import Data.Char import Data.Functor import qualified Data.IntMap as IntMap -- | Characteristic identifiers. type C = Int -- | Given a function @f :: a -> (C,[C])@ which returns a non-empty list of -- characteristics @C@ of @a@, partition a list of @a@s into groups -- such that each element in a group shares at least one characteristic -- with at least one other element of the group. cluster :: (a -> (C,[C])) -> [a] -> [[a]] cluster f as = cluster' $ map (\ a -> (a, f a)) as -- | Partition a list of @a@s paired with a non-empty list of -- characteristics @C@ into groups -- such that each element in a group shares at least one characteristic -- with at least one other element of the group. cluster' :: [(a,(C,[C]))] -> [[a]] cluster' acs = runEquivM id const $ do -- Construct the equivalence classes of characteristics. forM_ acs $ \ (_,(c,cs)) -> equateAll $ c:cs -- Pair each element with its class. cas <- forM acs $ \ (a,(c,_)) -> (`IntMap.singleton` [a]) <$> classDesc c -- Create a map from class to elements. let m = IntMap.unionsWith (++) cas -- Return the values of the map return $ IntMap.elems m Agda-2.5.3/src/full/Agda/Utils/Singleton.hs0000644000000000000000000000376013154613124016534 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving #-} -- {-# LANGUAGE TypeFamilies #-} -- | Constructing singleton collections. module Agda.Utils.Singleton where import Data.Hashable (Hashable) import Data.HashMap.Strict (HashMap) import qualified Data.HashMap.Strict as HashMap import Data.HashSet (HashSet) import qualified Data.HashSet as HashSet import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import Data.IntSet (IntSet) import qualified Data.IntSet as IntSet import Data.Map (Map) import qualified Data.Map as Map import Data.Sequence (Seq) import qualified Data.Sequence as Seq import Data.Set (Set) import qualified Data.Set as Set class Singleton el coll | coll -> el where singleton :: el -> coll instance Singleton a (Maybe a) where singleton = Just instance Singleton a [a] where singleton = (:[]) instance Singleton a (Seq a) where singleton = Seq.singleton instance Singleton a (Set a) where singleton = Set.singleton instance Singleton Int IntSet where singleton = IntSet.singleton instance Singleton (k ,a) (Map k a) where singleton = uncurry Map.singleton instance Singleton (Int,a) (IntMap a) where singleton = uncurry IntMap.singleton instance Hashable a => Singleton a (HashSet a) where singleton = HashSet.singleton instance Hashable k => Singleton (k,a) (HashMap k a) where singleton = uncurry HashMap.singleton -- Testing newtype-deriving: -- newtype Wrap c = Wrap c -- deriving (Singleton k) -- Succeeds -- Type family version: -- class Singleton c where -- type Elem c -- singleton :: Elem c -> c -- instance Singleton [a] where -- type Elem [a] = a -- singleton = (:[]) -- instance Singleton (Maybe a) where -- type Elem (Maybe a) = a -- singleton = Just -- instance Singleton (Set a) where -- type Elem (Set a) = a -- singleton = Set.singleton -- instance Singleton (Map k a) where -- type Elem (Map k a) = (k,a) -- singleton = uncurry Map.singleton -- newtype Wrap a = Wrap a -- deriving (Singleton) -- Fails Agda-2.5.3/src/full/Agda/Utils/String.hs0000644000000000000000000000444213154613124016036 0ustar0000000000000000module Agda.Utils.String where import Data.Char import qualified Data.List as List import Numeric import Agda.Utils.List -- | 'quote' adds double quotes around the string, replaces newline -- characters with @\n@, and escapes double quotes and backslashes -- within the string. This is different from the behaviour of 'show': -- -- @ -- \> 'putStrLn' $ 'show' \"\\x2200\" -- \"\\8704\" -- \> 'putStrLn' $ 'quote' \"\\x2200\" -- \"∀\" -- @ -- -- (The code examples above have been tested using version 4.2.0.0 of -- the base library.) quote :: String -> String quote s = "\"" ++ concatMap escape s ++ "\"" where escape c | c == '\n' = "\\n" | c `elem` escapeChars = ['\\', c] | otherwise = [c] escapeChars = "\"\\" -- | Adds hyphens around the given string -- -- >>> putStrLn $ delimiter "Title" -- ———— Title ————————————————————————————————————————————————— delimiter :: String -> String delimiter s = concat [ replicate 4 '\x2014' , " ", s, " " , replicate (54 - length s) '\x2014' ] -- | Shows a non-negative integer using the characters ₀-₉ instead of -- 0-9. showIndex :: (Show i, Integral i) => i -> String showIndex n = showIntAtBase 10 (\i -> toEnum (i + fromEnum '\x2080')) n "" -- | Adds a final newline if there is not already one. addFinalNewLine :: String -> String addFinalNewLine "" = "\n" addFinalNewLine s | last s == '\n' = s | otherwise = s ++ "\n" -- | Indents every line the given number of steps. indent :: Integral i => i -> String -> String indent i = unlines . map (List.genericReplicate i ' ' ++) . lines newtype Str = Str { unStr :: String } deriving Eq instance Show Str where show = unStr -- | Show a number using comma to separate powers of 1,000. showThousandSep :: Show a => a -> String showThousandSep = reverse . List.intercalate "," . chop 3 . reverse . show -- | Remove leading whitespace. ltrim :: String -> String ltrim = dropWhile isSpace -- | Remove trailing whitespace. rtrim :: String -> String rtrim = reverse . ltrim . reverse -- | Remove leading and trailing whitesapce. trim :: String -> String trim = rtrim . ltrim Agda-2.5.3/src/full/Agda/Utils/AssocList.hs0000644000000000000000000000431013154613124016466 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Additional functions for association lists. module Agda.Utils.AssocList where import Prelude hiding (lookup) import Control.Applicative import qualified Data.List as List import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- | A finite map, represented as a set of pairs. -- -- Invariant: at most one value per key. type AssocList k v = [(k,v)] -- | O(n). -- Reexport 'List.lookup'. lookup :: Eq k => k -> AssocList k v -> Maybe v lookup = List.lookup -- | O(n). -- Get the domain (list of keys) of the finite map. keys :: AssocList k v -> [k] keys = map fst -- | O(1). -- Add a new binding. -- Assumes the binding is not yet in the list. insert :: k -> v -> AssocList k v -> AssocList k v insert k v = ((k,v) :) -- | O(n). -- Update the value at a key. -- The key must be in the domain of the finite map. -- Otherwise, an internal error is raised. update :: Eq k => k -> v -> AssocList k v -> AssocList k v update k v = updateAt k $ const v -- | O(n). -- Update the value at a key with a certain function. -- The key must be in the domain of the finite map. -- Otherwise, an internal error is raised. updateAt :: Eq k => k -> (v -> v) -> AssocList k v -> AssocList k v updateAt k f = loop where loop [] = __IMPOSSIBLE__ loop (p@(k',v) : ps) | k == k' = (k, f v) : ps | otherwise = p : loop ps -- | O(n). -- Map over an association list, preserving the order. mapWithKey :: (k -> v -> v) -> AssocList k v -> AssocList k v mapWithKey f = map $ \ (k,v) -> (k, f k v) -- | O(n). -- If called with a effect-producing function, violation of the invariant -- could matter here (duplicating effects). mapWithKeyM :: Applicative m => (k -> v -> m v) -> AssocList k v -> m (AssocList k v) mapWithKeyM f = mapM $ \ (k,v) -> (k,) <$> f k v where -- mapM is applicative! mapM g [] = pure [] mapM g (x : xs) = (:) <$> g x <*> mapM g xs -- | O(n). -- Named in analogy to 'Data.Map.mapKeysMonotonic'. -- To preserve the invariant, it is sufficient that the key -- transformation is injective (rather than monotonic). mapKeysMonotonic :: (k -> k') -> AssocList k v -> AssocList k' v mapKeysMonotonic f = map $ mapFst f Agda-2.5.3/src/full/Agda/Utils/IO.hs0000644000000000000000000000075213154613124015077 0ustar0000000000000000-- | Auxiliary functions for the IO monad. module Agda.Utils.IO where import Control.Exception import Control.Monad.Writer -- | Catch 'IOException's. -- class CatchIO m where catchIO :: m a -> (IOException -> m a) -> m a -- | Alias of 'catch' for the IO monad. -- instance CatchIO IO where catchIO = catch -- | Upon exception, the written output is lost. -- instance CatchIO m => CatchIO (WriterT w m) where catchIO m h = WriterT $ runWriterT m `catchIO` \ e -> runWriterT (h e) Agda-2.5.3/src/full/Agda/Utils/Impossible.hs0000644000000000000000000000300513154613124016670 0ustar0000000000000000------------------------------------------------------------------------ -- | An interface for reporting \"impossible\" errors ------------------------------------------------------------------------ {-# LANGUAGE DeriveDataTypeable #-} module Agda.Utils.Impossible where import Control.Exception as E import Data.Typeable ( Typeable ) -- | \"Impossible\" errors, annotated with a file name and a line -- number corresponding to the source code location of the error. data Impossible = Impossible String Integer -- ^ We reached a program point which should be unreachable. | Unreachable String Integer -- ^ @Impossible@ with a different error message. -- Used when we reach a program point which can in principle -- be reached, but not for a certain run. deriving Typeable instance Show Impossible where show (Impossible file line) = unlines [ "An internal error has occurred. Please report this as a bug." , "Location of the error: " ++ file ++ ":" ++ show line ] show (Unreachable file line) = unlines [ "We reached a program point we did not want to reach." , "Location of the error: " ++ file ++ ":" ++ show line ] instance Exception Impossible -- | Abort by throwing an \"impossible\" error. You should not use -- this function directly. Instead use the macro in @undefined.h@. throwImpossible :: Impossible -> a throwImpossible = throw -- | Catch an \"impossible\" error, if possible. catchImpossible :: IO a -> (Impossible -> IO a) -> IO a catchImpossible = E.catch Agda-2.5.3/src/full/Agda/Utils/Null.hs0000644000000000000000000000571013154613124015501 0ustar0000000000000000{-# OPTIONS_GHC -fno-warn-orphans #-} {-# LANGUAGE CPP #-} -- | Overloaded @null@ and @empty@ for collections and sequences. module Agda.Utils.Null where import Prelude hiding (null) import Control.Monad import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as ByteString import Data.Function import Data.HashMap.Strict (HashMap) import qualified Data.HashMap.Strict as HashMap import Data.HashSet (HashSet) import qualified Data.HashSet as HashSet import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import Data.IntSet (IntSet) import qualified Data.IntSet as IntSet import qualified Data.List as List import Data.Map (Map) import qualified Data.Map as Map import Data.Monoid import Data.Sequence (Seq) import qualified Data.Sequence as Seq import Data.Set (Set) import qualified Data.Set as Set import Text.PrettyPrint (Doc, render) import Agda.Utils.Bag (Bag) import qualified Agda.Utils.Bag as Bag -- Andreas, 2015-06-24 orphan instance has to go here -- to be able to define instance Null Doc #if !MIN_VERSION_pretty(1,1,2) instance Eq Doc where (==) = (==) `on` render #endif class Null a where empty :: a null :: a -> Bool -- ^ Satisfying @null empty == True@. default null :: Eq a => a -> Bool null = (== empty) instance Null () where empty = () null _ = True instance (Null a, Null b) => Null (a,b) where empty = (empty, empty) null (a,b) = null a && null b instance Null ByteString where empty = ByteString.empty null = ByteString.null instance Null [a] where empty = [] null = List.null instance Null (Bag a) where empty = Bag.empty null = Bag.null instance Null (IntMap a) where empty = IntMap.empty null = IntMap.null instance Null IntSet where empty = IntSet.empty null = IntSet.null instance Null (Map k a) where empty = Map.empty null = Map.null instance Null (HashMap k a) where empty = HashMap.empty null = HashMap.null instance Null (HashSet a) where empty = HashSet.empty null = HashSet.null instance Null (Seq a) where empty = Seq.empty null = Seq.null instance Null (Set a) where empty = Set.empty null = Set.null -- | A 'Maybe' is 'null' when it corresponds to the empty list. instance Null (Maybe a) where empty = Nothing null Nothing = True null (Just a) = False instance Null Doc where empty = mempty null = (== mempty) -- * Testing for null. ifNull :: (Null a) => a -> b -> (a -> b) -> b ifNull a b k = if null a then b else k a ifNullM :: (Monad m, Null a) => m a -> m b -> (a -> m b) -> m b ifNullM ma mb k = ma >>= \ a -> ifNull a mb k whenNull :: (Monad m, Null a) => a -> m () -> m () whenNull = when . null unlessNull :: (Monad m, Null a) => a -> (a -> m ()) -> m () unlessNull a k = unless (null a) $ k a whenNullM :: (Monad m, Null a) => m a -> m () -> m () whenNullM ma k = ma >>= (`whenNull` k) unlessNullM :: (Monad m, Null a) => m a -> (a -> m ()) -> m () unlessNullM ma k = ma >>= (`unlessNull` k) Agda-2.5.3/src/full/Agda/Utils/IndexedList.hs0000644000000000000000000000450113154613124017000 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE TypeOperators #-} module Agda.Utils.IndexedList where import Agda.Utils.Lens -- | Existential wrapper for indexed types. data Some :: (k -> *) -> * where Some :: f i -> Some f -- | Unpacking a wrapped value. withSome :: Some b -> (forall i. b i -> a) -> a withSome (Some x) f = f x -- | Lists indexed by a type-level list. A value of type @All p [x₁..xₙ]@ is a -- sequence of values of types @p x₁@, .., @p xₙ@. data All :: (x -> *) -> [x] -> * where Nil :: All p '[] Cons :: p x -> All p xs -> All p (x ': xs) -- | Constructing an indexed list from a plain list. makeAll :: (a -> Some b) -> [a] -> Some (All b) makeAll f [] = Some Nil makeAll f (x : xs) = case (f x, makeAll f xs) of (Some y, Some ys) -> Some (Cons y ys) -- | Turning an indexed list back into a plain list. forgetAll :: (forall x. b x -> a) -> All b xs -> [a] forgetAll f Nil = [] forgetAll f (Cons x xs) = f x : forgetAll f xs -- | An index into a type-level list. data Index :: [x] -> x -> * where Zero :: Index (x ': xs) x Suc :: Index xs x -> Index (y ': xs) x -- | Indices are just natural numbers. forgetIndex :: Index xs x -> Int forgetIndex Zero = 0 forgetIndex (Suc i) = 1 + forgetIndex i -- | Mapping over an indexed list. mapWithIndex :: (forall x. Index xs x -> p x -> q x) -> All p xs -> All q xs mapWithIndex f Nil = Nil mapWithIndex f (Cons p ps) = Cons (f Zero p) $ mapWithIndex (f . Suc) ps -- | If you have an index you can get a lens for the given element. lIndex :: Index xs x -> Lens' (p x) (All p xs) lIndex Zero f (Cons x xs) = f x <&> \ x -> Cons x xs lIndex (Suc i) f (Cons x xs) = lIndex i f xs <&> \ xs -> Cons x xs #if __GLASGOW_HASKELL__ < 800 lIndex _ _ Nil = error "-fwarn-incomplete-pattern deficiency" #endif -- | Looking up an element in an indexed list. lookupIndex :: All p xs -> Index xs x -> p x lookupIndex = flip ix where -- -Wincomplete-patterns fails for the other argument order! ix :: Index xs x -> All p xs -> p x ix Zero (Cons x xs) = x ix (Suc i) (Cons x xs) = ix i xs #if __GLASGOW_HASKELL__ < 800 ix _ Nil = error "-fwarn-incomplete-pattern deficiency" #endif -- | All indices into an indexed list. allIndices :: All p xs -> All (Index xs) xs allIndices = mapWithIndex const Agda-2.5.3/src/full/Agda/Utils/Pretty.hs0000644000000000000000000000624213154613124016057 0ustar0000000000000000{-# LANGUAGE CPP #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE DeriveDataTypeable #-} #endif {-| Pretty printing functions. -} module Agda.Utils.Pretty ( module Agda.Utils.Pretty , module Text.PrettyPrint ) where import Data.Int ( Int32 ) import Data.Data (Data(..)) #if __GLASGOW_HASKELL__ <= 708 import Data.Typeable ( Typeable ) #endif import Text.PrettyPrint hiding (TextDetails(Str), empty) #include "undefined.h" import Agda.Utils.Impossible -- * Pretty class -- | While 'Show' is for rendering data in Haskell syntax, -- 'Pretty' is for displaying data to the world, i.e., the -- user and the environment. -- -- Atomic data has no inner document structure, so just -- implement 'pretty' as @pretty a = text $ ... a ...@. class Pretty a where pretty :: a -> Doc prettyPrec :: Int -> a -> Doc prettyList :: [a] -> Doc pretty = prettyPrec 0 prettyPrec = const pretty prettyList = brackets . prettyList_ -- | Use instead of 'show' when printing to world. prettyShow :: Pretty a => a -> String prettyShow = render . pretty -- * Pretty instances instance Pretty Bool where pretty = text . show instance Pretty Int where pretty = text . show instance Pretty Int32 where pretty = text . show instance Pretty Integer where pretty = text . show instance Pretty Char where pretty c = text [c] prettyList = text instance Pretty Doc where pretty = id instance Pretty a => Pretty (Maybe a) where prettyPrec p Nothing = text "Nothing" prettyPrec p (Just x) = mparens (p > 0) $ text "Just" <+> prettyPrec 10 x instance Pretty a => Pretty [a] where pretty = prettyList -- * 'Doc' utilities pwords :: String -> [Doc] pwords = map text . words fwords :: String -> Doc fwords = fsep . pwords -- | Comma separated list, without the brackets. prettyList_ :: Pretty a => [a] -> Doc prettyList_ = fsep . punctuate comma . map pretty -- ASR (2016-12-13): In pretty >= 1.1.2.0 the below function 'mparens' -- is called 'maybeParens'. I didn't use that name due to the issue -- https://github.com/haskell/pretty/issues/40. -- | Apply 'parens' to 'Doc' if boolean is true. mparens :: Bool -> Doc -> Doc mparens True = parens mparens False = id -- | @align max rows@ lays out the elements of @rows@ in two columns, -- with the second components aligned. The alignment column of the -- second components is at most @max@ characters to the right of the -- left-most column. -- -- Precondition: @max > 0@. align :: Int -> [(String, Doc)] -> Doc align max rows = vcat $ map (\(s, d) -> text s $$ nest (maxLen + 1) d) $ rows where maxLen = maximum $ 0 : filter (< max) (map (length . fst) rows) -- | Handles strings with newlines properly (preserving indentation) multiLineText :: String -> Doc multiLineText = vcat . map text . lines #if __GLASGOW_HASKELL__ <= 708 deriving instance Typeable Doc #endif -- cheating because you shouldn't be digging this far anyway instance Data Doc where gunfold _ _ _ = __IMPOSSIBLE__ toConstr = __IMPOSSIBLE__ dataTypeOf = __IMPOSSIBLE__ infixl 6 -- | @a b = hang a 2 b@ () :: Doc -> Doc -> Doc a b = hang a 2 b -- | @pshow = text . pretty@ pshow :: Show a => a -> Doc pshow = text . show Agda-2.5.3/src/full/Agda/Utils/Empty.hs0000644000000000000000000000244413154613124015666 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE DeriveDataTypeable #-} #endif -- | An empty type with some useful instances. module Agda.Utils.Empty where import Control.Exception (evaluate) import Data.Functor ((<$)) import Data.Data (Data) #if __GLASGOW_HASKELL__ <= 708 import Data.Typeable ( Typeable ) #endif import Agda.Utils.Impossible #include "undefined.h" data Empty #if __GLASGOW_HASKELL__ <= 708 deriving Typeable #endif deriving instance Data Empty instance Eq Empty where _ == _ = True instance Ord Empty where compare _ _ = EQ instance Show Empty where showsPrec p _ = showParen (p > 9) $ showString "error \"Agda.Utils.Empty.Empty\"" absurd :: Empty -> a absurd e = seq e __IMPOSSIBLE__ -- | @toImpossible e@ extracts the @Impossible@ value raised via -- @__IMPOSSIBLE__@ to create the element @e@ of type @Empty@. -- It proceeds by evaluating @e@ to weak head normal form and -- catching the exception. -- We are forced to wrap things in a @Maybe@ because of -- @catchImpossible@'s type. toImpossible :: Empty -> IO Impossible toImpossible e = do s <- catchImpossible (Nothing <$ evaluate e) (return . Just) case s of Just i -> return i Nothing -> absurd e -- this should never happen Agda-2.5.3/src/full/Agda/Utils/TypeLevel.hs0000644000000000000000000001120213154613124016471 0ustar0000000000000000{-# LANGUAGE GADTs #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE ScopedTypeVariables #-} -- We need undecidable instances for the definition of @Foldr@, -- and @Domains@ and @CoDomain@ using @If@ for instance. {-# LANGUAGE UndecidableInstances #-} module Agda.Utils.TypeLevel where import Data.Proxy import GHC.Exts (Constraint) ------------------------------------------------------------------ -- CONSTRAINTS ------------------------------------------------------------------ -- | @All p as@ ensures that the constraint @p@ is satisfied by -- all the 'types' in @as@. -- (Types is between scare-quotes here because the code is -- actually kind polymorphic) type family All (p :: k -> Constraint) (as :: [k]) :: Constraint where All p '[] = () All p (a ': as) = (p a, All p as) ------------------------------------------------------------------ -- FUNCTIONS -- Type-level and Kind polymorphic versions of usual value-level -- functions. ------------------------------------------------------------------ -- | On Booleans type family If (b :: Bool) (l :: k) (r :: k) :: k where If 'True l r = l If 'False l r = r -- | On Lists type family Foldr (c :: k -> l -> l) (n :: l) (as :: [k]) :: l where Foldr c n '[] = n Foldr c n (a ': as) = c a (Foldr c n as) -- | Version of @Foldr@ taking a defunctionalised argument so -- that we can use partially applied functions. type family Foldr' (c :: Function k (Function l l -> *) -> *) (n :: l) (as :: [k]) :: l where Foldr' c n '[] = n Foldr' c n (a ': as) = Apply (Apply c a) (Foldr' c n as) type family Map (f :: Function k l -> *) (as :: [k]) :: [l] where Map f as = Foldr' (ConsMap0 f) '[] as data ConsMap0 :: (Function k l -> *) -> Function k (Function [l] [l] -> *) -> * data ConsMap1 :: (Function k l -> *) -> k -> Function [l] [l] -> * type instance Apply (ConsMap0 f) a = ConsMap1 f a type instance Apply (ConsMap1 f a) tl = Apply f a ': tl type family Constant (b :: l) (as :: [k]) :: [l] where Constant b as = Map (Constant1 b) as ------------------------------------------------------------------ -- TYPE FORMERS ------------------------------------------------------------------ -- | @Arrows [a1,..,an] r@ corresponds to @a1 -> .. -> an -> r@ -- | @Products [a1,..,an]@ corresponds to @(a1, (..,( an, ())..))@ type Arrows (as :: [*]) (r :: *) = Foldr (->) r as type Products (as :: [*]) = Foldr (,) () as -- | @IsBase t@ is @'True@ whenever @t@ is *not* a function space. type family IsBase (t :: *) :: Bool where IsBase (a -> t) = 'False IsBase a = 'True -- | Using @IsBase@ we can define notions of @Domains@ and @CoDomains@ -- which *reduce* under positive information @IsBase t ~ 'True@ even -- though the shape of @t@ is not formally exposed type family Domains (t :: *) :: [*] where Domains t = If (IsBase t) '[] (Domains' t) type family Domains' (t :: *) :: [*] where Domains' (a -> t) = a ': Domains t type family CoDomain (t :: *) :: * where CoDomain t = If (IsBase t) t (CoDomain' t) type family CoDomain' (t :: *) :: * where CoDomain' (a -> t) = CoDomain t ------------------------------------------------------------------ -- TYPECLASS MAGIC ------------------------------------------------------------------ -- | @Currying as b@ witnesses the isomorphism between @Arrows as b@ -- and @Products as -> b@. It is defined as a type class rather -- than by recursion on a singleton for @as@ so all of that these -- conversions are inlined at compile time for concrete arguments. class Currying as b where uncurrys :: Proxy as -> Proxy b -> Arrows as b -> Products as -> b currys :: Proxy as -> Proxy b -> (Products as -> b) -> Arrows as b instance Currying '[] b where uncurrys _ _ f = \ () -> f currys _ _ f = f () instance Currying as b => Currying (a ': as) b where uncurrys _ p f = uncurry $ uncurrys (Proxy :: Proxy as) p . f currys _ p f = currys (Proxy :: Proxy as) p . curry f ------------------------------------------------------------------ -- DEFUNCTIONALISATION -- Cf. Eisenberg and Stolarek's paper: -- Promoting Functions to Type Families in Haskell ------------------------------------------------------------------ data Function :: * -> * -> * data Constant0 :: Function a (Function b a -> *) -> * data Constant1 :: * -> Function b a -> * type family Apply (t :: Function k l -> *) (u :: k) :: l type instance Apply Constant0 a = Constant1 a type instance Apply (Constant1 a) b = a Agda-2.5.3/src/full/Agda/Utils/Favorites.hs0000644000000000000000000001121213154613124016523 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving #-} -- | Maintaining a list of favorites of some partially ordered type. -- Only the best elements are kept. -- -- To avoid name clashes, import this module qualified, as in -- @ -- import Agda.Utils.Favorites (Favorites) -- import qualified Agda.Utils.Favorites as Fav -- @ module Agda.Utils.Favorites where import Prelude hiding ( null ) import Data.Foldable (Foldable) import Data.Functor import Data.Semigroup import qualified Data.List as List import qualified Data.Set as Set import Agda.Utils.Null import Agda.Utils.PartialOrd import Agda.Utils.Singleton import Agda.Utils.Tuple -- | A list of incomparable favorites. newtype Favorites a = Favorites { toList :: [a] } deriving (Foldable, Show, Null, Singleton a) -- | Equality checking is a bit expensive, since we need to sort! -- Maybe use a 'Set' of favorites in the first place? instance Ord a => Eq (Favorites a) where as == bs = Set.fromList (toList as) == Set.fromList (toList bs) -- | Result of comparing a candidate with the current favorites. data CompareResult a = Dominates { dominated :: [a], notDominated :: [a] } -- ^ Great, you are dominating a possibly (empty list of favorites) -- but there is also a rest that is not dominated. -- If @null dominated@, then @notDominated@ is necessarily the -- complete list of favorites. | IsDominated { dominator :: a } -- ^ Sorry, but you are dominated by that favorite. -- | Gosh, got some pretty @a@ here, compare with my current favorites! -- Discard it if there is already one that is better or equal. -- (Skewed conservatively: faithful to the old favorites.) -- If there is no match for it, add it, and -- dispose of all that are worse than @a@. -- -- We require a partial ordering. Less is better! (Maybe paradoxically.) compareWithFavorites :: PartialOrd a => a -> Favorites a -> CompareResult a compareWithFavorites a favs = loop $ toList favs where loop [] = Dominates [] [] loop as@(b : bs) = case comparable a b of POLT -> dominates b $ loop bs -- @a@ is a new favorite, bye-bye, @b@ POLE -> dominates b $ loop bs -- ditto POEQ -> IsDominated b -- @b@ is as least as good as @a@, bye-bye, @a@ POGE -> IsDominated b -- ditto POGT -> IsDominated b -- ditto POAny -> doesnotd b $ loop bs -- don't know, compare with my other favorites -- add an outperformed favorite dominates b (Dominates bs as) = Dominates (b : bs) as dominates b r@IsDominated{} = r -- add an uncomparable favorite doesnotd b (Dominates as bs) = Dominates as (b : bs) doesnotd b r@IsDominated{} = r -- | Compare a new set of favorites to an old one and discard -- the new favorites that are dominated by the old ones -- and vice verse. -- (Skewed conservatively: faithful to the old favorites.) -- -- @compareFavorites new old = (new', old')@ compareFavorites :: PartialOrd a => Favorites a -> Favorites a -> (Favorites a, Favorites a) compareFavorites new old = mapFst Favorites $ loop (toList new) old where loop [] old = ([], old) loop (a : new) old = case compareWithFavorites a old of -- Better: Discard all @old@ ones that @a@ dominates and keep @a@ Dominates _ old -> mapFst (a:) $ loop new (Favorites old) -- Not better: Discard @a@ IsDominated{} -> loop new old unionCompared :: (Favorites a, Favorites a) -> Favorites a unionCompared (Favorites new, Favorites old) = Favorites $ new ++ old -- | After comparing, do the actual insertion. insertCompared :: a -> Favorites a -> CompareResult a -> Favorites a insertCompared a _ (Dominates _ as) = Favorites (a : as) insertCompared _ l IsDominated{} = l -- | Compare, then insert accordingly. -- @insert a l = insertCompared a l (compareWithFavorites a l)@ insert :: PartialOrd a => a -> Favorites a -> Favorites a insert a l = insertCompared a l (compareWithFavorites a l) -- | Insert all the favorites from the first list into the second. union :: PartialOrd a => Favorites a -> Favorites a -> Favorites a union (Favorites as) bs = List.foldr insert bs as -- | Construct favorites from elements of a partial order. -- The result depends on the order of the list if it -- contains equal elements, since earlier seen elements -- are favored over later seen equals. -- The first element of the list is seen first. fromList :: PartialOrd a => [a] -> Favorites a fromList = List.foldl' (flip insert) empty -- | 'Favorites' forms a 'Monoid' under 'empty' and 'union. instance PartialOrd a => Semigroup (Favorites a) where (<>) = union instance PartialOrd a => Monoid (Favorites a) where mempty = empty mappend = (<>) Agda-2.5.3/src/full/Agda/Utils/Lens.hs0000644000000000000000000000510313154613124015464 0ustar0000000000000000{-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} -- | A cut-down implementation of lenses, with names taken from -- Edward Kmett's lens package. module Agda.Utils.Lens ( module Agda.Utils.Lens , (<&>) -- reexported from Agda.Utils.Functor ) where import Control.Applicative import Control.Monad.State import Control.Monad.Reader import Control.Monad.Writer import Data.Map (Map) import qualified Data.Map as Map import Data.Functor.Identity import Agda.Utils.Functor ((<&>)) -- * Type-preserving lenses. -- | Van Laarhoven style homogeneous lenses. -- Mnemoic: "Lens inner outer". type Lens' i o = forall f. Functor f => (i -> f i) -> o -> f o -- * Some simple lenses. lFst :: Lens' a (a, b) lFst f (x, y) = (, y) <$> f x lSnd :: Lens' b (a, b) lSnd f (x, y) = (x,) <$> f y -- * Elementary lens operations. infixl 8 ^. -- | Get inner part @i@ of structure @o@ as designated by @Lens' i o@. (^.) :: o -> Lens' i o -> i o ^. l = getConst $ l Const o -- | Set inner part @i@ of structure @o@ as designated by @Lens' i o@. set :: Lens' i o -> i -> o -> o set l = over l . const -- | Modify inner part @i@ of structure @o@ using a function @i -> i@. over :: Lens' i o -> (i -> i) -> o -> o over l f o = runIdentity $ l (Identity . f) o -- * State accessors and modifiers. -- | Read a part of the state. use :: MonadState o m => Lens' i o -> m i use l = do !x <- gets (^. l) return x infix 4 .= -- | Write a part of the state. (.=) :: MonadState o m => Lens' i o -> i -> m () l .= i = modify $ set l i infix 4 %= -- | Modify a part of the state. (%=) :: MonadState o m => Lens' i o -> (i -> i) -> m () l %= f = modify $ over l f infix 4 %== -- | Modify a part of the state monadically. (%==) #if __GLASGOW_HASKELL__ <= 708 :: (Functor m, MonadState o m) #else :: MonadState o m #endif => Lens' i o -> (i -> m i) -> m () l %== f = put =<< l f =<< get infix 4 %%= -- | Modify a part of the state monadically, and return some result. (%%=) #if __GLASGOW_HASKELL__ <= 708 :: (Functor m, MonadState o m) #else :: MonadState o m #endif => Lens' i o -> (i -> m (i, r)) -> m r l %%= f = do o <- get (o', r) <- runWriterT $ l (WriterT . f) o put o' return r -- * Read-only state accessors and modifiers. -- | Ask for part of read-only state. view :: MonadReader o m => Lens' i o -> m i view l = asks (^. l) -- | Modify a part of the state in a subcomputation. locally :: MonadReader o m => Lens' i o -> (i -> i) -> m a -> m a locally l = local . over l key :: Ord k => k -> Lens' (Maybe v) (Map k v) key k f m = f (Map.lookup k m) <&> \ v -> Map.alter (const v) k m Agda-2.5.3/src/full/Agda/Utils/ListT.hs0000644000000000000000000001153213154613124015625 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE UndecidableInstances #-} -- Due to limitations of funct.dep. -- | @ListT@ done right, -- see https://www.haskell.org/haskellwiki/ListT_done_right_alternative -- -- There is also the @list-t@ package on hackage (Nikita Volkov) -- but it again depends on other packages we do not use yet, -- so we rather implement the few bits we need afresh. module Agda.Utils.ListT where import Control.Applicative import Control.Monad import Control.Monad.Reader import Control.Monad.State import Data.Semigroup import Agda.Utils.Maybe -- | Lazy monadic computation of a list of results. newtype ListT m a = ListT { runListT :: m (Maybe (a, ListT m a)) } deriving (Functor) -- * List operations -- | The empty lazy list. nilListT :: Monad m => ListT m a nilListT = ListT $ return Nothing -- | Consing a value to a lazy list. consListT :: Monad m => a -> ListT m a -> ListT m a consListT a l = ListT $ return $ Just (a, l) -- | Singleton lazy list. sgListT :: Monad m => a -> ListT m a sgListT a = consListT a nilListT -- | Case distinction over lazy list. caseListT :: Monad m => ListT m a -> m b -> (a -> ListT m a -> m b) -> m b caseListT l nil cons = caseMaybeM (runListT l) nil $ uncurry cons -- | Folding a lazy list, effects left-to-right. foldListT :: Monad m => (a -> m b -> m b) -> m b -> ListT m a -> m b foldListT cons nil = loop where loop l = caseListT l nil $ \ a l' -> cons a $ loop l' -- | The join operation of the @ListT m@ monad. concatListT :: Monad m => ListT m (ListT m a) -> ListT m a concatListT = ListT . foldListT append (return Nothing) where append l = runListT . mappend l . ListT -- * Monadic list operations. -- | We can ``run'' a computation of a 'ListT' as it is monadic itself. runMListT :: Monad m => m (ListT m a) -> ListT m a runMListT ml = ListT $ runListT =<< ml -- | Monadic cons. consMListT :: Monad m => m a -> ListT m a -> ListT m a consMListT ma l = ListT $ (Just . (,l)) `liftM` ma -- consMListT ma l = runMListT $ liftM (`consListT` l) ma -- simplification: -- consMListT ma l = ListT $ runListT =<< liftM (`consListT` l) ma -- consMListT ma l = ListT $ runListT =<< (`consListT` l) <$> ma -- consMListT ma l = ListT $ runListT =<< do a <- ma; return $ a `consListT` l -- consMListT ma l = ListT $ do a <- ma; runListT =<< do return $ a `consListT` l -- consMListT ma l = ListT $ do a <- ma; runListT $ a `consListT` l -- consMListT ma l = ListT $ do a <- ma; runListT $ ListT $ return $ Just (a, l) -- consMListT ma l = ListT $ do a <- ma; return $ Just (a, l) -- consMListT ma l = ListT $ Just . (,l) <$> ma -- | Monadic singleton. sgMListT :: Monad m => m a -> ListT m a sgMListT ma = consMListT ma nilListT -- | Extending a monadic function to 'ListT'. mapMListT :: ( Monad m #if __GLASGOW_HASKELL__ <= 708 , Functor m #endif ) => (a -> m b) -> ListT m a -> ListT m b mapMListT f (ListT ml) = ListT $ do caseMaybeM ml (return Nothing) $ \ (a, as) -> do b <- f a return $ Just (b , mapMListT f as) -- | Alternative implementation using 'foldListT'. mapMListT_alt :: ( Monad m #if __GLASGOW_HASKELL__ <= 708 , Functor m #endif ) => (a -> m b) -> ListT m a -> ListT m b mapMListT_alt f = runMListT . foldListT cons (return nilListT) where cons a ml = consMListT (f a) <$> ml -- | Change from one monad to another liftListT :: (Monad m, Monad m') => (forall a. m a -> m' a) -> ListT m a -> ListT m' a liftListT lift xs = runMListT $ caseMaybeM (lift $ runListT xs) (return nilListT) $ \(x,xs) -> return $ consListT x $ liftListT lift xs -- Instances instance Monad m => Semigroup (ListT m a) where l1 <> l2 = ListT $ foldListT cons (runListT l2) l1 where cons a = runListT . consListT a . ListT instance Monad m => Monoid (ListT m a) where mempty = nilListT mappend = (<>) instance (Functor m, Applicative m, Monad m) => Alternative (ListT m) where empty = mempty (<|>) = mappend instance (Functor m, Applicative m, Monad m) => MonadPlus (ListT m) where mzero = mempty mplus = mappend instance (Functor m, Applicative m, Monad m) => Applicative (ListT m) where pure = sgListT (<*>) = ap -- Another Applicative, but not the canonical one. -- l1 <*> l2 = ListT $ loop <$> runListT l1 <*> runListT l2 -- where -- loop (Just (f, l1')) (Just (a, l2')) = Just (f a, l1' <*> l2') -- loop _ _ = Nothing instance (Functor m, Applicative m, Monad m) => Monad (ListT m) where return = pure l >>= k = concatListT $ k <$> l instance MonadTrans ListT where lift = sgMListT instance (Applicative m, MonadIO m) => MonadIO (ListT m) where liftIO = lift . liftIO instance (Applicative m, MonadReader r m) => MonadReader r (ListT m) where ask = lift ask local f = ListT . local f . runListT instance (Applicative m, MonadState s m) => MonadState s (ListT m) where get = lift get put = lift . put Agda-2.5.3/src/full/Agda/Utils/Either.hs0000644000000000000000000000602513154613124016007 0ustar0000000000000000------------------------------------------------------------------------ -- | Utilities for the 'Either' type. ------------------------------------------------------------------------ module Agda.Utils.Either where -- | Loop while we have an exception. whileLeft :: Monad m => (a -> Either b c) -> (a -> b -> m a) -> (a -> c -> m d) -> a -> m d whileLeft test left right = loop where loop a = case test a of Left b -> loop =<< left a b Right c -> right a c -- | Monadic version of 'either' with a different argument ordering. caseEitherM :: Monad m => m (Either a b) -> (a -> m c) -> (b -> m c) -> m c caseEitherM mm f g = either f g =<< mm -- | 'Either' is a bifunctor. mapEither :: (a -> c) -> (b -> d) -> Either a b -> Either c d mapEither f g = either (Left . f) (Right . g) -- | 'Either _ b' is a functor. mapLeft :: (a -> c) -> Either a b -> Either c b mapLeft f = mapEither f id -- | 'Either a' is a functor. mapRight :: (b -> d) -> Either a b -> Either a d mapRight = mapEither id -- | 'Either' is bitraversable. traverseEither :: Functor f => (a -> f c) -> (b -> f d) -> Either a b -> f (Either c d) traverseEither f g = either (fmap Left . f) (fmap Right . g) -- | Returns 'True' iff the argument is @'Right' x@ for some @x@. -- -- Note: from @base >= 4.7.0.0@ already present in @Data.Either@. isRight :: Either a b -> Bool isRight (Right _) = True isRight (Left _) = False -- | Returns 'True' iff the argument is @'Left' x@ for some @x@. -- -- Note: from @base >= 4.7.0.0@ already present in @Data.Either@. isLeft :: Either a b -> Bool isLeft (Right _) = False isLeft (Left _) = True -- | Analogue of 'Data.Maybe.fromMaybe'. fromLeft :: (b -> a) -> Either a b -> a fromLeft = either id -- | Analogue of 'Data.Maybe.fromMaybe'. fromRight :: (a -> b) -> Either a b -> b fromRight f = either f id -- | Analogue of 'Agda.Utils.Maybe.fromMaybeM'. fromLeftM :: Monad m => (b -> m a) -> Either a b -> m a fromLeftM = either return -- | Analogue of 'Agda.Utils.Maybe.fromMaybeM'. fromRightM :: Monad m => (a -> m b) -> Either a b -> m b fromRightM f = either f return -- | Safe projection from 'Left'. -- -- > maybeLeft (Left a) = Just a -- > maybeLeft Right{} = Nothing -- maybeLeft :: Either a b -> Maybe a maybeLeft = either Just (const Nothing) -- | Safe projection from 'Right'. -- -- > maybeRight (Right b) = Just b -- > maybeRight Left{} = Nothing -- maybeRight :: Either a b -> Maybe b maybeRight = either (const Nothing) Just -- | Returns @'Just' input_with_tags_stripped@ if all elements are -- to the 'Left', and otherwise 'Nothing'. allLeft :: [Either a b] -> Maybe [a] allLeft = mapM maybeLeft -- | Returns @'Just' input_with_tags_stripped@ if all elements are -- to the right, and otherwise 'Nothing'. -- -- @ -- allRight xs == -- if all isRight xs then -- Just (map (\(Right x) -> x) xs) -- else -- Nothing -- @ allRight :: [Either a b] -> Maybe [b] allRight = mapM maybeRight -- | Convert 'Maybe' to @'Either' ()@. maybeToEither :: Maybe a -> Either () a maybeToEither = maybe (Left ()) Right Agda-2.5.3/src/full/Agda/Utils/Geniplate.hs0000644000000000000000000000317413154613124016501 0ustar0000000000000000{-# LANGUAGE TemplateHaskell #-} -- | Utilities related to Geniplate. module Agda.Utils.Geniplate ( instanceUniverseBiT' , instanceTransformBiMT' , dontDescendInto ) where import Data.Generics.Geniplate import Data.Map (Map) import qualified Language.Haskell.TH as TH import qualified Agda.Syntax.Abstract.Name as A import qualified Agda.Syntax.Concrete.Name as C import qualified Agda.Syntax.Position as P import qualified Agda.Syntax.Scope.Base as S import qualified Agda.Utils.FileName as F import qualified Agda.Utils.Maybe.Strict as MS -- | Types which Geniplate should not descend into. dontDescendInto :: [TH.TypeQ] dontDescendInto = [ [t| String |] , [t| A.QName |] , [t| A.Name |] , [t| C.Name |] , [t| S.ScopeInfo |] , [t| Map A.QName A.QName |] , [t| Map A.ModuleName A.ModuleName |] , [t| [(A.QName, A.QName)] |] , [t| [(A.ModuleName, A.ModuleName)] |] , [t| A.AmbiguousQName |] , [t| P.Range' (MS.Maybe F.AbsolutePath) |] ] -- | A localised instance of 'instanceUniverseBiT'. The generated -- 'universeBi' functions neither descend into the types in -- 'dontDescendInto', nor into the types in the list argument. instanceUniverseBiT' :: [TH.TypeQ] -> TH.TypeQ -> TH.Q [TH.Dec] instanceUniverseBiT' ts p = instanceUniverseBiT (ts ++ dontDescendInto) p -- | A localised instance of 'instanceTransformBiMT'. The generated -- 'transformBiM' functions neither descend into the types in -- 'dontDescendInto', nor into the types in the list argument. instanceTransformBiMT' :: [TH.TypeQ] -> TH.TypeQ -> TH.TypeQ -> TH.Q [TH.Dec] instanceTransformBiMT' ts p = instanceTransformBiMT (ts ++ dontDescendInto) p Agda-2.5.3/src/full/Agda/Utils/Warshall.hs0000644000000000000000000003432413154613124016347 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving #-} {- | Construct a graph from constraints @ x + n <= y becomes x ---(-n)---> y x <= n + y becomes x ---(+n)---> y @ the default edge (= no edge) is labelled with infinity. Building the graph involves keeping track of the node names. We do this in a finite map, assigning consecutive numbers to nodes. -} module Agda.Utils.Warshall where import Control.Applicative import Control.Monad.State import Data.Maybe import Data.Array import qualified Data.List as List import Data.Map (Map) import qualified Data.Map as Map import Agda.Syntax.Common (Nat) import Agda.Utils.SemiRing type Matrix a = Array (Int,Int) a -- assuming a square matrix warshall :: SemiRing a => Matrix a -> Matrix a warshall a0 = loop r a0 where b@((r,c),(r',c')) = bounds a0 -- assuming r == c and r' == c' loop k a | k <= r' = loop (k+1) (array b [ ((i,j), (a!(i,j)) `oplus` ((a!(i,k)) `otimes` (a!(k,j)))) | i <- [r..r'], j <- [c..c'] ]) | otherwise = a type AdjList node edge = Map node [(node, edge)] -- | Warshall's algorithm on a graph represented as an adjacency list. warshallG :: (SemiRing edge, Ord node) => AdjList node edge -> AdjList node edge warshallG g = fromMatrix $ warshall m where nodes = zip (List.nub $ Map.keys g ++ map fst (concat $ Map.elems g)) [0..] len = length nodes b = ((0,0), (len - 1,len - 1)) edge i j = do es <- Map.lookup i g foldr oplus Nothing [ Just v | (j', v) <- es, j == j' ] m = array b [ ((n, m), edge i j) | (i, n) <- nodes, (j, m) <- nodes ] fromMatrix matrix = Map.fromList $ do (i, n) <- nodes let es = [ (fst (nodes !! m), e) | m <- [0..len - 1] , Just e <- [matrix ! (n, m)] ] return (i, es) -- | Edge weight in the graph, forming a semi ring. data Weight = Finite Int | Infinite deriving (Eq) inc :: Weight -> Int -> Weight inc Infinite n = Infinite inc (Finite k) n = Finite (k + n) instance Show Weight where show (Finite i) = show i show Infinite = "." instance Ord Weight where a <= Infinite = True Infinite <= b = False Finite a <= Finite b = a <= b instance SemiRing Weight where ozero = Infinite oone = Finite 0 oplus = min otimes Infinite _ = Infinite otimes _ Infinite = Infinite otimes (Finite a) (Finite b) = Finite (a + b) -- constraints --------------------------------------------------- -- | Nodes of the graph are either -- - flexible variables (with identifiers drawn from @Int@), -- - rigid variables (also identified by @Int@s), or -- - constants (like 0, infinity, or anything between). data Node = Rigid Rigid | Flex FlexId deriving (Eq, Ord) data Rigid = RConst Weight | RVar RigidId deriving (Eq, Ord, Show) type NodeId = Int type RigidId = Int type FlexId = Int type Scope = RigidId -> Bool -- ^ Which rigid variables a flex may be instatiated to. instance Show Node where show (Flex i) = "?" ++ show i show (Rigid (RVar i)) = "v" ++ show i show (Rigid (RConst Infinite)) = "#" show (Rigid (RConst (Finite n))) = show n infinite :: Rigid -> Bool infinite (RConst Infinite) = True infinite _ = False -- | @isBelow r w r'@ -- checks, if @r@ and @r'@ are connected by @w@ (meaning @w@ not infinite), -- whether @r + w <= r'@. -- Precondition: not the same rigid variable. isBelow :: Rigid -> Weight -> Rigid -> Bool isBelow _ Infinite _ = True isBelow _ n (RConst Infinite) = True isBelow (RConst (Finite i)) (Finite n) (RConst (Finite j)) = i + n <= j isBelow _ _ _ = False -- rigid variables are not related -- | A constraint is an edge in the graph. data Constraint = NewFlex FlexId Scope | Arc Node Int Node -- ^ For @Arc v1 k v2@ at least one of @v1@ or @v2@ is a @MetaV@ (Flex), -- the other a @MetaV@ or a @Var@ (Rigid). -- If @k <= 0@ this means @suc^(-k) v1 <= v2@ -- otherwise @v1 <= suc^k v3@. instance Show Constraint where show (NewFlex i s) = "SizeMeta(?" ++ show i ++ ")" show (Arc v1 k v2) | k == 0 = show v1 ++ "<=" ++ show v2 | k < 0 = show v1 ++ "+" ++ show (-k) ++ "<=" ++ show v2 | otherwise = show v1 ++ "<=" ++ show v2 ++ "+" ++ show k type Constraints = [Constraint] emptyConstraints :: Constraints emptyConstraints = [] -- graph (matrix) ------------------------------------------------ data Graph = Graph { flexScope :: Map FlexId Scope -- ^ Scope for each flexible var. , nodeMap :: Map Node NodeId -- ^ Node labels to node numbers. , intMap :: Map NodeId Node -- ^ Node numbers to node labels. , nextNode :: NodeId -- ^ Number of nodes @n@. , graph :: NodeId -> NodeId -> Weight -- ^ The edges (restrict to @[0..n[@). } -- | The empty graph: no nodes, edges are all undefined (infinity weight). initGraph :: Graph initGraph = Graph Map.empty Map.empty Map.empty 0 (\ x y -> Infinite) -- | The Graph Monad, for constructing a graph iteratively. type GM = State Graph -- | Add a size meta node. addFlex :: FlexId -> Scope -> GM () addFlex x scope = do modify $ \ st -> st { flexScope = Map.insert x scope (flexScope st) } _ <- addNode (Flex x) return () -- | Lookup identifier of a node. -- If not present, it is added first. addNode :: Node -> GM Int addNode n = do st <- get case Map.lookup n (nodeMap st) of Just i -> return i Nothing -> do let i = nextNode st put $ st { nodeMap = Map.insert n i (nodeMap st) , intMap = Map.insert i n (intMap st) , nextNode = i + 1 } return i -- | @addEdge n1 k n2@ -- improves the weight of egde @n1->n2@ to be at most @k@. -- Also adds nodes if not yet present. addEdge :: Node -> Int -> Node -> GM () addEdge n1 k n2 = do i1 <- addNode n1 i2 <- addNode n2 st <- get let graph' x y = if (x,y) == (i1,i2) then Finite k `oplus` (graph st) x y else graph st x y put $ st { graph = graph' } addConstraint :: Constraint -> GM () addConstraint (NewFlex x scope) = addFlex x scope addConstraint (Arc n1 k n2) = addEdge n1 k n2 buildGraph :: Constraints -> Graph buildGraph cs = execState (mapM_ addConstraint cs) initGraph mkMatrix :: Int -> (Int -> Int -> Weight) -> Matrix Weight mkMatrix n g = array ((0,0),(n-1,n-1)) [ ((i,j), g i j) | i <- [0..n-1], j <- [0..n-1]] -- displaying matrices with row and column labels -------------------- -- | A matrix with row descriptions in @b@ and column descriptions in @c@. data LegendMatrix a b c = LegendMatrix { matrix :: Matrix a , rowdescr :: Int -> b , coldescr :: Int -> c } instance (Show a, Show b, Show c) => Show (LegendMatrix a b c) where show (LegendMatrix m rd cd) = -- first show column description let ((r,c),(r',c')) = bounds m in foldr (\ j s -> "\t" ++ show (cd j) ++ s) "" [c .. c'] ++ -- then output rows foldr (\ i s -> "\n" ++ show (rd i) ++ foldr (\ j t -> "\t" ++ show (m!(i,j)) ++ t) (s) [c .. c']) "" [r .. r'] -- solving the constraints ------------------------------------------- -- | A solution assigns to each flexible variable a size expression -- which is either a constant or a @v + n@ for a rigid variable @v@. type Solution = Map Int SizeExpr emptySolution :: Solution emptySolution = Map.empty extendSolution :: Solution -> Int -> SizeExpr -> Solution extendSolution subst k v = Map.insert k v subst data SizeExpr = SizeVar RigidId Int -- ^ e.g. x + 5 | SizeConst Weight -- ^ a number or infinity instance Show SizeExpr where show (SizeVar n 0) = show (Rigid (RVar n)) show (SizeVar n k) = show (Rigid (RVar n)) ++ "+" ++ show k show (SizeConst w) = show w -- | @sizeRigid r n@ returns the size expression corresponding to @r + n@ sizeRigid :: Rigid -> Int -> SizeExpr sizeRigid (RConst k) n = SizeConst (inc k n) sizeRigid (RVar i) n = SizeVar i n {- apply :: SizeExpr -> Solution -> SizeExpr apply e@(SizeExpr (Rigid _) _) phi = e apply e@(SizeExpr (Flex x) i) phi = case Map.lookup x phi of Nothing -> e Just (SizeExpr v j) -> SizeExpr v (i + j) after :: Solution -> Solution -> Solution after psi phi = Map.map (\ e -> e `apply` phi) psi -} {- compute solution a solution CANNOT exist if v < v for a rigid variable v -- Andreas, 2012-09-19 OUTDATED are: -- v <= v' for rigid variables v,v' -- x < v for a flexible variable x and a rigid variable v thus, for each flexible x, only one of the following cases is possible r+n <= x+m <= infty for a unique rigid r (meaning r --(m-n)--> x) x <= r+n for a unique rigid r (meaning x --(n)--> r) we are looking for the least values for flexible variables that solve the constraints. Algorithm while flexible variables and rigid rows left find a rigid variable row i for all flexible columns j if i --n--> j with n<=0 (meaning i+n <= j) then j = i + n while flexible variables j left search the row j for entry i if j --n--> i with n >= 0 (meaning j <= i + n) then j = i + n -} solve :: Constraints -> Maybe Solution solve cs = -- trace (show cs) $ -- trace (show lm0) $ -- trace (show lm) $ -- trace (show d) $ let solution = if solvable then loop1 flexs rigids emptySolution else Nothing in -- trace (show solution) $ solution where -- compute the graph and its transitive closure m gr = buildGraph cs n = nextNode gr -- number of nodes m0 = mkMatrix n (graph gr) m = warshall m0 -- tracing only: build output version of transitive graph legend i = fromJust $ Map.lookup i (intMap gr) -- trace only lm0 = LegendMatrix m0 legend legend -- trace only lm = LegendMatrix m legend legend -- trace only -- compute the sets of flexible and rigid node numbers ns = Map.keys (nodeMap gr) -- a set of flexible variables flexs = List.foldl' (\ l -> \case (Flex i ) -> i : l (Rigid _) -> l) [] ns -- a set of rigid variables rigids = List.foldl' (\ l -> \case (Flex _ ) -> l (Rigid i) -> i : l) [] ns -- rigid matrix indices rInds = List.foldl' (\ l r -> let Just i = Map.lookup (Rigid r) (nodeMap gr) in i : l) [] rigids -- check whether there is a solution -- d = [ m!(i,i) | i <- [0 .. (n-1)] ] -- diagonal -- a rigid variable might not be less than it self, so no -.. on the -- rigid part of the diagonal solvable = all (\ x -> x >= Finite 0) [ m!(i,i) | i <- rInds ] && True {- Andreas, 2012-09-19 We now can have constraints between rigid variables, like i < j. Thus we skip the following two test. However, a solution must be checked for consistency with the constraints on rigid vars. -- a rigid variable might not be bounded below by infinity or -- bounded above by a constant -- it might not be related to another rigid variable all (\ (r, r') -> r == r' || let Just row = (Map.lookup (Rigid r) (nodeMap gr)) Just col = (Map.lookup (Rigid r') (nodeMap gr)) edge = m!(row,col) in isBelow r edge r' ) [ (r,r') | r <- rigids, r' <- rigids ] && -- a flexible variable might not be strictly below a rigid variable all (\ (x, v) -> let Just row = (Map.lookup (Flex x) (nodeMap gr)) Just col = (Map.lookup (Rigid (RVar v)) (nodeMap gr)) edge = m!(row,col) in edge >= Finite 0) [ (x,v) | x <- flexs, (RVar v) <- rigids ] -} inScope :: FlexId -> Rigid -> Bool inScope x (RConst _) = True inScope x (RVar v) = scope v where Just scope = Map.lookup x (flexScope gr) {- loop1 while flexible variables and rigid rows left find a rigid variable row i for all flexible columns j if i --n--> j with n<=0 (meaning i + n <= j) then j = i + n -} loop1 :: [FlexId] -> [Rigid] -> Solution -> Maybe Solution loop1 [] rgds subst = Just subst loop1 flxs [] subst = loop2 flxs subst loop1 flxs (r:rgds) subst = let row = fromJust $ Map.lookup (Rigid r) (nodeMap gr) (flxs',subst') = List.foldl' (\ (flx,sub) f -> let col = fromJust $ Map.lookup (Flex f) (nodeMap gr) in case (inScope f r, m!(row,col)) of -- Finite z | z <= 0 -> (True, Finite z) -> let trunc z | z >= 0 = 0 | otherwise = -z in (flx, extendSolution sub f (sizeRigid r (trunc z))) _ -> (f : flx, sub) ) ([], subst) flxs in loop1 flxs' rgds subst' {- loop2 while flexible variables j left search the row j for entry i if j --n--> i with n >= 0 (meaning j <= i + n) then j = i -} loop2 :: [FlexId] -> Solution -> Maybe Solution loop2 [] subst = Just subst loop2 (f:flxs) subst = loop3 0 subst where row = fromJust $ Map.lookup (Flex f) (nodeMap gr) loop3 col subst | col >= n = -- default to infinity loop2 flxs (extendSolution subst f (SizeConst Infinite)) loop3 col subst = case Map.lookup col (intMap gr) of Just (Rigid r) | not (infinite r) -> case (inScope f r, m!(row,col)) of (True, Finite z) | z >= 0 -> loop2 flxs (extendSolution subst f (sizeRigid r z)) (_, Infinite) -> loop3 (col+1) subst _ -> -- trace ("unusable rigid: " ++ show r ++ " for flex " ++ show f) Nothing -- NOT: loop3 (col+1) subst _ -> loop3 (col+1) subst Agda-2.5.3/src/full/Agda/Utils/Three.hs0000644000000000000000000000102213154613124015626 0ustar0000000000000000-- | Tools for a 3-element type. module Agda.Utils.Three where -- | Enum type with 3 elements. -- data Three = One | Two | Three deriving (Eq, Ord, Show, Bounded, Enum) -- | Partition a list into 3 groups. -- -- Preserves the relative order or elements. -- partition3 :: (a -> Three) -> [a] -> ([a], [a], [a]) partition3 f = loop where loop [] = ([], [], []) loop (x:xs) = case f x of One -> (x:as, bs, cs) Two -> (as, x:bs, cs) Three -> (as, bs, x:cs) where (as, bs, cs) = loop xs Agda-2.5.3/src/full/Agda/Utils/Update.hs0000644000000000000000000001030213154613124016002 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving #-} module Agda.Utils.Update ( Change , MonadChange(..) , runChange , Updater , sharing , runUpdater , dirty , ifDirty , Updater1(..) , Updater2(..) ) where import Control.Applicative import Control.Monad.Identity import Control.Monad.Trans import Control.Monad.Writer.Strict import Data.Traversable (Traversable(..), traverse) import Agda.Utils.Tuple -- * Change monad. -- | The class of change monads. class Monad m => MonadChange m where tellDirty :: m () -- ^ Mark computation as having changed something. listenDirty :: m a -> m (a, Bool) -- | The @ChangeT@ monad transformer. newtype ChangeT m a = ChangeT { fromChangeT :: WriterT Any m a } deriving (Functor, Applicative, Monad, MonadTrans) instance Monad m => MonadChange (ChangeT m) where tellDirty = ChangeT $ tell $ Any True listenDirty m = ChangeT $ do (a, Any dirty) <- listen (fromChangeT m) return (a, dirty) -- | A mock change monad. instance MonadChange Identity where tellDirty = Identity () listenDirty (Identity a) = Identity (a, True) -- * Pure endo function and updater type EndoFun a = a -> a type Updater a = a -> Change a -- BEGIN REAL STUFF -- | The @Change@ monad. newtype Change a = Change { fromChange :: Writer Any a } deriving (Functor, Applicative, Monad) instance MonadChange Change where tellDirty = Change $ tell $ Any True listenDirty m = Change $ do (a, Any dirty) <- listen (fromChange m) return (a, dirty) -- | Run a 'Change' computation, returning result plus change flag. runChange :: Change a -> (a, Bool) runChange = mapSnd getAny . runWriter . fromChange -- | Blindly run an updater. runUpdater :: Updater a -> a -> (a, Bool) runUpdater f a = runChange $ f a -- | Mark a computation as dirty. dirty :: Updater a dirty a = do tellDirty return a {-# SPECIALIZE ifDirty :: Change a -> (a -> Change b) -> (a -> Change b) -> Change b #-} {-# SPECIALIZE ifDirty :: Identity a -> (a -> Identity b) -> (a -> Identity b) -> Identity b #-} ifDirty :: MonadChange m => m a -> (a -> m b) -> (a -> m b) -> m b ifDirty m f g = do (a, dirty) <- listenDirty m if dirty then f a else g a -- * Proper updater (Q-combinators) -- | Replace result of updating with original input if nothing has changed. sharing :: Updater a -> Updater a sharing f a = do (a', changed) <- listenDirty $ f a return $ if changed then a' else a -- | Eval an updater (using 'sharing'). evalUpdater :: Updater a -> EndoFun a evalUpdater f a = fst $ runWriter $ fromChange $ sharing f a -- END REAL STUFF -- * Updater transformer classes -- ** Unary (functors) -- | Like 'Functor', but preserving sharing. class Traversable f => Updater1 f where updater1 :: Updater a -> Updater (f a) updates1 :: Updater a -> Updater (f a) -- ^ @= sharing . updater1@ update1 :: Updater a -> EndoFun (f a) updater1 = traverse updates1 f = sharing $ updater1 f update1 f = evalUpdater $ updater1 f instance Updater1 Maybe where instance Updater1 [] where updater1 f [] = return [] updater1 f (x : xs) = (:) <$> f x <*> updates1 f xs -- ** Binary (bifunctors) -- | Like 'Bifunctor', but preserving sharing. class Updater2 f where updater2 :: Updater a -> Updater b -> Updater (f a b) updates2 :: Updater a -> Updater b -> Updater (f a b) update2 :: Updater a -> Updater b -> EndoFun (f a b) updates2 f1 f2 = sharing $ updater2 f1 f2 update2 f1 f2 = evalUpdater $ updater2 f1 f2 instance Updater2 (,) where updater2 f1 f2 (a,b) = (,) <$> sharing f1 a <*> sharing f2 b instance Updater2 Either where updater2 f1 f2 (Left a) = Left <$> f1 a updater2 f1 f2 (Right b) = Right <$> f2 b {-- BEGIN MOCK -- * Mock updater type Change = Identity -- | Replace result of updating with original input if nothing has changed. {-# INLINE sharing #-} sharing :: Updater a -> Updater a sharing f a = f a -- | Run an updater. {-# INLINE evalUpdater #-} evalUpdater :: Updater a -> EndoFun a evalUpdater f a = runIdentity (f a) -- | Mark a computation as dirty. {-# INLINE dirty #-} dirty :: Updater a dirty = Identity {-# INLINE ifDirty #-} ifDirty :: Identity a -> (a -> Identity b) -> (a -> Identity b) -> Identity b ifDirty m f g = m >>= f -- END MOCK -} Agda-2.5.3/src/full/Agda/Utils/Tuple.hs0000644000000000000000000000332613154613124015661 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving #-} module Agda.Utils.Tuple where import Control.Applicative import Data.Foldable import Data.Traversable infix 2 -*- infix 3 /\ -- backslashes at EOL interact badly with CPP... -- | Bifunctoriality for pairs. (-*-) :: (a -> c) -> (b -> d) -> (a,b) -> (c,d) (f -*- g) ~(x,y) = (f x, g y) -- | @mapFst f = f -*- id@ mapFst :: (a -> c) -> (a,b) -> (c,b) mapFst f ~(x,y) = (f x, y) -- | @mapSnd g = id -*- g@ mapSnd :: (b -> d) -> (a,b) -> (a,d) mapSnd g ~(x,y) = (x, g y) -- | Lifted pairing. (/\) :: (a -> b) -> (a -> c) -> a -> (b,c) (f /\ g) x = (f x, g x) -- | Swap. (Only in Data.Tuple from base-4.3) swap :: (a,b) -> (b,a) swap ~(a,b) = (b,a) -- * Triple (stolen from Data.Tuple.HT) {-# INLINE fst3 #-} fst3 :: (a,b,c) -> a fst3 ~(x,_,_) = x {-# INLINE snd3 #-} snd3 :: (a,b,c) -> b snd3 ~(_,x,_) = x {-# INLINE thd3 #-} thd3 :: (a,b,c) -> c thd3 ~(_,_,x) = x {-# INLINE uncurry3 #-} uncurry3 :: (a -> b -> c -> d) -> (a,b,c) -> d uncurry3 f ~(x,y,z) = f x y z {-# INLINE uncurry4 #-} uncurry4 :: (a -> b -> c -> d -> e) -> (a,b,c,d) -> e uncurry4 f ~(w,x,y,z) = f w x y z -- | Monadic version of '-*-'. mapPairM :: (Applicative m) => (a -> m c) -> (b -> m d) -> (a,b) -> m (c,d) mapPairM f g ~(a,b) = (,) <$> f a <*> g b -- | Monadic 'mapFst'. mapFstM :: (Applicative m) => (a -> m c) -> (a,b) -> m (c,b) mapFstM f ~(a,b) = (,b) <$> f a -- | Monadic 'mapSnd'. mapSndM :: (Applicative m) => (b -> m d) -> (a,b) -> m (a,d) mapSndM f ~(a,b) = (a,) <$> f b newtype List2 a = List2 { list2 :: (a,a) } deriving (Eq, Functor, Foldable, Traversable) instance Applicative List2 where pure a = List2 (a,a) (List2 (f,f')) <*> (List2 (a,a')) = List2 (f a, f' a') Agda-2.5.3/src/full/Agda/Utils/Hash.hs0000644000000000000000000000154313154613124015452 0ustar0000000000000000 {-| Instead of checking time-stamps we compute a hash of the module source and store it in the interface file. This module contains the functions to do that. -} module Agda.Utils.Hash where import Data.ByteString as B import Data.Word import qualified Data.Hash as H import qualified Data.List as L import Data.Digest.Murmur64 import Agda.Utils.FileName type Hash = Word64 hashByteString :: ByteString -> Hash hashByteString = H.asWord64 . B.foldl' (\h b -> H.combine h (H.hashWord8 b)) (H.hashWord8 0) hashFile :: AbsolutePath -> IO Hash hashFile file = do s <- B.readFile (filePath file) return $ hashByteString s combineHashes :: [Hash] -> Hash combineHashes hs = H.asWord64 $ L.foldl' H.combine (H.hashWord8 0) $ L.map H.hash hs -- | Hashing a module name for unique identifiers. hashString :: String -> Word64 hashString = asWord64 . hash64 Agda-2.5.3/src/full/Agda/Utils/Bag.hs0000644000000000000000000001176513154613124015267 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | A simple overlay over Data.Map to manage unordered sets with duplicates. module Agda.Utils.Bag where import Prelude hiding (null, map) import Control.Applicative hiding (empty) import Text.Show.Functions () import Data.Foldable (Foldable(foldMap)) import Data.Functor.Identity import qualified Data.List as List import Data.Map (Map) import qualified Data.Map as Map import Data.Semigroup import qualified Data.Set as Set import Data.Traversable import Agda.Utils.Functor #include "undefined.h" import Agda.Utils.Impossible -- | A set with duplicates. -- Faithfully stores elements which are equal with regard to (==). newtype Bag a = Bag { bag :: Map a [a] -- ^ The list contains all occurrences of @a@ (not just the duplicates!). -- Hence, the invariant: the list is never empty. } deriving (Eq, Ord) -- The list contains all occurrences of @a@ (not just the duplicates!). -- Hence the invariant: the list is never empty. -- -- This is slightly wasteful, but much easier to implement -- in terms of @Map@ as the alternative, which is to store -- only the duplicates in the list. -- See, e.g., implementation of 'union' which would be impossible -- to do in the other representation. We would need a -- 'Map.unionWithKey' that passes us *both* keys. -- But Map works under the assumption that Eq for keys is identity, -- it does not honor information in keys that goes beyond Ord. ------------------------------------------------------------------------ -- * Query ------------------------------------------------------------------------ -- | Is the bag empty? null :: Bag a -> Bool null = Map.null . bag -- | Number of elements in the bag. Duplicates count. O(n). size :: Bag a -> Int size = getSum . foldMap (Sum . length) . bag -- | @(bag ! a)@ finds all elements equal to @a@. O(log n). -- Total function, returns @[]@ if none are. (!) :: Ord a => Bag a -> a -> [a] Bag b ! a = Map.findWithDefault [] a b -- | O(log n). member :: Ord a => a -> Bag a -> Bool member a = not . notMember a -- | O(log n). notMember :: Ord a => a -> Bag a -> Bool notMember a b = List.null (b ! a) -- | Return the multiplicity of the given element. O(log n + count _ _). count :: Ord a => a -> Bag a -> Int count a b = length (b ! a) ------------------------------------------------------------------------ -- * Construction ------------------------------------------------------------------------ -- | O(1) empty :: Bag a empty = Bag $ Map.empty -- | O(1) singleton :: a -> Bag a singleton a = Bag $ Map.singleton a [a] union :: Ord a => Bag a -> Bag a -> Bag a union (Bag b) (Bag c) = Bag $ Map.unionWith (++) b c unions :: Ord a => [Bag a] -> Bag a unions = Bag . Map.unionsWith (++) . List.map bag -- | @insert a b = union b (singleton a)@ insert :: Ord a => a -> Bag a -> Bag a insert a = Bag . Map.insertWith (++) a [a] . bag -- | @fromList = unions . map singleton@ fromList :: Ord a => [a] -> Bag a fromList = Bag . Map.fromListWith (++) . List.map (\ a -> (a,[a])) ------------------------------------------------------------------------ -- * Destruction ------------------------------------------------------------------------ -- | Returns the elements of the bag, grouped by equality (==). groups :: Bag a -> [[a]] groups = Map.elems . bag -- | Returns the bag, with duplicates. toList :: Bag a -> [a] toList = concat . groups -- | Returns the bag without duplicates. keys :: Bag a -> [a] keys = Map.keys . bag -- Works because of the invariant! -- keys = catMaybes . map headMaybe . Map.elems . bag -- -- Map.keys does not work, as zero copies @(a,[])@ -- -- should count as not present in the bag. -- | Returns the bag, with duplicates. elems :: Bag a -> [a] elems = toList toAscList :: Bag a -> [a] toAscList = toList ------------------------------------------------------------------------ -- * Traversal ------------------------------------------------------------------------ map :: Ord b => (a -> b) -> Bag a -> Bag b map f = Bag . Map.fromListWith (++) . List.map ff . Map.elems . bag where ff (a : as) = (b, b : List.map f as) where b = f a ff [] = __IMPOSSIBLE__ traverse' :: forall a b m . (Applicative m, Ord b) => (a -> m b) -> Bag a -> m (Bag b) traverse' f = (Bag . Map.fromListWith (++)) <.> traverse trav . Map.elems . bag where trav :: [a] -> m (b, [b]) trav (a : as) = (\ b bs -> (b, b:bs)) <$> f a <*> traverse f as trav [] = __IMPOSSIBLE__ ------------------------------------------------------------------------ -- Instances ------------------------------------------------------------------------ instance Show a => Show (Bag a) where showsPrec _ (Bag b) = ("Agda.Utils.Bag.Bag (" ++) . showsPrec 0 b . (')':) instance Ord a => Semigroup (Bag a) where (<>) = union instance Ord a => Monoid (Bag a) where mempty = empty mappend = (<>) mconcat = unions instance Foldable Bag where foldMap f = foldMap f . toList -- not a Functor (only works for 'Ord'ered types) -- not Traversable (only works for 'Ord'ered types) Agda-2.5.3/src/full/Agda/Utils/Memo.hs0000644000000000000000000000345413154613124015467 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Utils.Memo where import Control.Applicative import Control.Monad.State import System.IO.Unsafe import Data.IORef import qualified Data.Map as Map import qualified Agda.Utils.HashMap as HMap import Data.Hashable import Agda.Utils.Lens -- Simple memoisation in a state monad -- | Simple, non-reentrant memoisation. memo #if __GLASGOW_HASKELL__ <= 708 :: (Functor m, MonadState s m) #else :: MonadState s m #endif => Lens' (Maybe a) s -> m a -> m a memo tbl compute = do mv <- use tbl case mv of Just x -> return x Nothing -> do x <- compute x <$ (tbl .= Just x) -- | Recursive memoisation, second argument is the value you get -- on recursive calls. memoRec #if __GLASGOW_HASKELL__ <= 708 :: (Functor m, MonadState s m) #else :: MonadState s m #endif => Lens' (Maybe a) s -> a -> m a -> m a memoRec tbl ih compute = do mv <- use tbl case mv of Just x -> return x Nothing -> do tbl .= Just ih x <- compute x <$ (tbl .= Just x) {-# NOINLINE memoUnsafe #-} memoUnsafe :: Ord a => (a -> b) -> (a -> b) memoUnsafe f = unsafePerformIO $ do tbl <- newIORef Map.empty return (unsafePerformIO . f' tbl) where f' tbl x = do m <- readIORef tbl case Map.lookup x m of Just y -> return y Nothing -> do let y = f x writeIORef tbl (Map.insert x y m) return y {-# NOINLINE memoUnsafeH #-} memoUnsafeH :: (Eq a, Hashable a) => (a -> b) -> (a -> b) memoUnsafeH f = unsafePerformIO $ do tbl <- newIORef HMap.empty return (unsafePerformIO . f' tbl) where f' tbl x = do m <- readIORef tbl case HMap.lookup x m of Just y -> return y Nothing -> do let y = f x writeIORef tbl (HMap.insert x y m) return y Agda-2.5.3/src/full/Agda/Utils/Time.hs0000644000000000000000000000263013154613124015463 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} -- To avoid warning on derived Integral instance for CPUTime. {-# OPTIONS_GHC -fno-warn-identities #-} -- | Time-related utilities. module Agda.Utils.Time ( ClockTime , getClockTime , getCPUTime , measureTime , CPUTime(..) , fromMilliseconds ) where import Control.Monad.Trans import qualified System.CPUTime as CPU import Data.Functor import qualified Data.Time import Agda.Utils.Pretty import Agda.Utils.String -- | Timestamps. type ClockTime = Data.Time.UTCTime -- | The current time. getClockTime :: IO ClockTime getClockTime = Data.Time.getCurrentTime -- | CPU time in pico (10^-12) seconds. newtype CPUTime = CPUTime Integer deriving (Eq, Show, Ord, Num, Real, Enum, Integral) fromMilliseconds :: Integer -> CPUTime fromMilliseconds n = CPUTime (n * 1000000000) -- | Print CPU time in milli (10^-3) seconds. instance Pretty CPUTime where pretty (CPUTime ps) = text $ showThousandSep (div ps 1000000000) ++ "ms" {-# SPECIALIZE getCPUTime :: IO CPUTime #-} getCPUTime :: MonadIO m => m CPUTime getCPUTime = liftIO $ CPUTime <$> CPU.getCPUTime -- | Measure the time of a computation. -- Of course, does not work with exceptions. measureTime :: MonadIO m => m a -> m (a, CPUTime) measureTime m = do start <- liftIO $ getCPUTime x <- m stop <- liftIO $ getCPUTime return (x, stop - start) Agda-2.5.3/src/full/Agda/Utils/Size.hs0000644000000000000000000000325413154613124015502 0ustar0000000000000000-- | Collection size. -- -- For 'TermSize' see "Agda.Syntax.Internal". module Agda.Utils.Size ( Sized(..) , SizedThing(..) , sizeThing ) where import Prelude hiding (null) import Data.HashMap.Strict (HashMap) import qualified Data.HashMap.Strict as HashMap import Data.HashSet (HashSet) import qualified Data.HashSet as HashSet import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import Data.IntSet (IntSet) import qualified Data.IntSet as IntSet import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import Data.Sequence (Seq) import qualified Data.Sequence as Seq import qualified Data.List as List import Agda.Utils.Null -- | The size of a collection (i.e., its length). class Sized a where size :: a -> Int instance Sized [a] where size = List.genericLength instance Sized (IntMap a) where size = IntMap.size instance Sized IntSet where size = IntSet.size instance Sized (Map k a) where size = Map.size instance Sized (Set a) where size = Set.size instance Sized (HashMap k a) where size = HashMap.size instance Sized (HashSet a) where size = HashSet.size instance Sized (Seq a) where size = Seq.length -- | Thing decorated with its size. -- The thing should fit into main memory, thus, the size is an @Int@. data SizedThing a = SizedThing { theSize :: !Int , sizedThing :: a } -- | Cache the size of an object. sizeThing :: Sized a => a -> SizedThing a sizeThing a = SizedThing (size a) a -- | Return the cached size. instance Sized (SizedThing a) where size = theSize instance Null a => Null (SizedThing a) where empty = SizedThing 0 empty null = null . sizedThing Agda-2.5.3/src/full/Agda/Utils/FileName.hs0000644000000000000000000000652213154613124016251 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-| Operations on file names. -} module Agda.Utils.FileName ( AbsolutePath(AbsolutePath) , filePath , mkAbsolute , absolute , (===) , doesFileExistCaseSensitive , rootPath ) where import Control.Applicative import System.Directory import System.FilePath #ifdef mingw32_HOST_OS import Control.Exception (bracket) import System.Win32 (findFirstFile, findClose, getFindDataFileName) #endif import Data.Text (Text) import qualified Data.Text as Text import Data.Function import Data.Hashable (Hashable) import Data.Data (Data) import Data.Typeable (Typeable) import Agda.Utils.Monad import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible -- | Paths which are known to be absolute. -- -- Note that the 'Eq' and 'Ord' instances do not check if different -- paths point to the same files or directories. newtype AbsolutePath = AbsolutePath { byteStringPath :: Text } deriving (Eq, Ord, Typeable, Data, Hashable) -- | Extract the 'AbsolutePath' to be used as 'FilePath'. filePath :: AbsolutePath -> FilePath filePath = Text.unpack . byteStringPath -- TODO: 'Show' should output Haskell-parseable representations. -- The following instance is deprecated, and Pretty should be used -- instead. Later, simply derive Show for this type. instance Show AbsolutePath where show = filePath instance Pretty AbsolutePath where pretty = text . filePath -- | Constructs 'AbsolutePath's. -- -- Precondition: The path must be absolute and valid. mkAbsolute :: FilePath -> AbsolutePath mkAbsolute f | isAbsolute f = AbsolutePath $ Text.pack $ dropTrailingPathSeparator $ normalise f | otherwise = __IMPOSSIBLE__ rootPath :: FilePath #ifdef mingw32_HOST_OS rootPath = joinDrive "C:" [pathSeparator] #else rootPath = [pathSeparator] #endif -- | maps @/bla/bla/bla/foo.bar.xxx@ to @foo.bar@. rootName :: AbsolutePath -> String rootName = dropExtension . snd . splitFileName . filePath -- | Makes the path absolute. -- -- This function may raise an @\_\_IMPOSSIBLE\_\_@ error if -- 'canonicalizePath' does not return an absolute path. absolute :: FilePath -> IO AbsolutePath absolute f = mkAbsolute <$> do -- canonicalizePath sometimes truncates paths pointing to -- non-existing files/directories. ex <- doesFileExist f .||. doesDirectoryExist f if ex then canonicalizePath f else do cwd <- getCurrentDirectory return (cwd f) where m1 .||. m2 = do b1 <- m1 if b1 then return True else m2 -- | Tries to establish if the two file paths point to the same file -- (or directory). infix 4 === (===) :: AbsolutePath -> AbsolutePath -> Bool (===) = equalFilePath `on` filePath -- | Case-sensitive 'doesFileExist' for Windows. -- -- This is case-sensitive only on the file name part, not on the directory part. -- (Ideally, path components coming from module name components should be -- checked case-sensitively and the other path components should be checked -- case insensitively.) doesFileExistCaseSensitive :: FilePath -> IO Bool #ifdef mingw32_HOST_OS doesFileExistCaseSensitive f = do doesFileExist f `and2M` do bracket (findFirstFile f) (findClose . fst) $ fmap (takeFileName f ==) . getFindDataFileName . snd #else doesFileExistCaseSensitive f = doesFileExist f #endif Agda-2.5.3/src/full/Agda/Utils/Function.hs0000644000000000000000000001014113154613124016346 0ustar0000000000000000 module Agda.Utils.Function where -- | Repeat a state transition @f :: a -> (b, a)@ with output @b@ -- while condition @cond@ on the output is true. -- Return all intermediate results and the final result -- where @cond@ is @False@. -- -- Postconditions (when it terminates): -- @fst (last (iterWhile cond f a)) == False@. -- @all fst (init (interWhile cond f a))@. iterWhile :: (b -> Bool) -> (a -> (b, a)) -> a -> [(b,a)] iterWhile cond f = loop where loop a = r : if cond b then loop a' else [] where r@(b, a') = f a -- | Repeat something while a condition on some state is true. -- Return the last state (including the changes of the last -- transition, even if the condition became false then). repeatWhile :: (a -> (Bool, a)) -> a -> a repeatWhile f = loop where loop a = if again then loop a' else a' where (again, a') = f a -- | Monadic version of 'repeatWhile'. repeatWhileM :: (Monad m) => (a -> m (Bool, a)) -> a -> m a repeatWhileM f = loop where loop a = do (again, a') <- f a if again then loop a' else return a' -- | A version of the trampoline function. -- -- The usual function iterates @f :: a -> Maybe a@ as long -- as @Just{}@ is returned, and returns the last value of @a@ -- upon @Nothing@. -- -- @usualTrampoline f = trampolineWhile $ \ a -> maybe (False,a) (True,) (f a)@. -- -- @trampolineWhile@ is very similar to @repeatWhile@, only that -- it discards the state on which the condition went @False@, -- and returns the last state on which the condition was @True@. trampolineWhile :: (a -> (Bool, a)) -> a -> a trampolineWhile f = repeatWhile $ \ a -> let (again, a') = f a in (again,) $ if again then a' else a -- | Monadic version of 'trampolineWhile'. trampolineWhileM :: (Monad m) => (a -> m (Bool, a)) -> a -> m a trampolineWhileM f = repeatWhileM $ \ a -> do (again, a') <- f a return $ (again,) $ if again then a' else a -- | More general trampoline, which allows some final computation -- from iteration state @a@ into result type @b@. trampoline :: (a -> Either b a) -> a -> b trampoline f = loop where loop a = either id loop $ f a -- | Monadic version of 'trampoline'. trampolineM :: Monad m => (a -> m (Either b a)) -> a -> m b trampolineM f = loop where loop a = either return loop =<< f a -- | Iteration to fixed-point. -- -- @iterateUntil r f a0@ iterates endofunction @f@, starting with @a0@, -- until @r@ relates its result to its input, i.e., @f a `r` a@. -- -- This is the generic pattern behind saturation algorithms. -- -- If @f@ is monotone with regard to @r@, -- meaning @a `r` b@ implies @f a `r` f b@, -- and @f@-chains starting with @a0@ are finite -- then iteration is guaranteed to terminate. -- -- A typical instance will work on sets, and @r@ could be set inclusion, -- and @a0@ the empty set, and @f@ the step function of a saturation algorithm. iterateUntil :: (a -> a -> Bool) -> (a -> a) -> a -> a iterateUntil r f = loop where loop a = if r a' a then a' else loop a' where a' = f a -- | Monadic version of 'iterateUntil'. iterateUntilM :: Monad m => (a -> a -> Bool) -> (a -> m a) -> a -> m a iterateUntilM r f = loop where loop a = do a' <- f a if r a' a then return a' else loop a' -- | @'iterate'' n f x@ applies @f@ to @x@ @n@ times and returns the -- result. -- -- The applications are calculated strictly. iterate' :: Integral i => i -> (a -> a) -> a -> a iterate' 0 f x = x iterate' n f x | n > 0 = iterate' (n - 1) f $! f x | otherwise = error "iterate': Negative input." -- * Iteration over Booleans. -- | @applyWhen b f a@ applies @f@ to @a@ when @b@. applyWhen :: Bool -> (a -> a) -> a -> a applyWhen b f = if b then f else id -- | @applyUnless b f a@ applies @f@ to @a@ unless @b@. applyUnless :: Bool -> (a -> a) -> a -> a applyUnless b f = if b then id else f -- | Monadic version of @applyWhen@ applyWhenM :: (Monad m) => m Bool -> (m a -> m a) -> m a -> m a applyWhenM mb f x = mb >>= \ b -> applyWhen b f x -- | Monadic version of @applyUnless@ applyUnlessM :: (Monad m) => m Bool -> (m a -> m a) -> m a -> m a applyUnlessM mb f x = mb >>= \ b -> applyUnless b f x Agda-2.5.3/src/full/Agda/Utils/SemiRing.hs0000644000000000000000000000133613154613124016304 0ustar0000000000000000module Agda.Utils.SemiRing where -- | Semirings (). class SemiRing a where ozero :: a oone :: a oplus :: a -> a -> a otimes :: a -> a -> a instance SemiRing a => SemiRing (Maybe a) where ozero = Nothing oone = Just oone oplus Nothing y = y oplus x Nothing = x oplus (Just x) (Just y) = Just (oplus x y) otimes Nothing _ = Nothing otimes _ Nothing = Nothing otimes (Just x) (Just y) = Just (otimes x y) -- | Star semirings -- (). class SemiRing a => StarSemiRing a where ostar :: a -> a instance StarSemiRing a => StarSemiRing (Maybe a) where ostar Nothing = oone ostar (Just x) = Just (ostar x) Agda-2.5.3/src/full/Agda/Utils/Functor.hs0000644000000000000000000000474613154613124016217 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Utilities for functors. module Agda.Utils.Functor ( module Agda.Utils.Functor , (<$>) -- from Data.Functor ) where import Control.Applicative ( Const(Const), getConst ) import Data.Functor import Data.Functor.Identity import Data.Functor.Compose import Data.Functor.Classes infixr 4 $> ($>) :: Functor f => f a -> b -> f b ($>) = flip (<$) infixr 9 <.> -- | Composition: pure function after functorial (monadic) function. (<.>) :: Functor m => (b -> c) -> (a -> m b) -> a -> m c (f <.> g) a = f <$> g a -- | The true pure @for@ loop. -- 'Data.Traversable.for' is a misnomer, it should be @forA@. for :: Functor m => m a -> (a -> b) -> m b for = flip fmap infix 4 <&> -- | Infix version of 'for'. (<&>) :: Functor m => m a -> (a -> b) -> m b (<&>) = for -- | A decoration is a functor that is traversable into any functor. -- -- The 'Functor' superclass is given because of the limitations -- of the Haskell class system. -- @traverseF@ actually implies functoriality. -- -- Minimal complete definition: @traverseF@ or @distributeF@. class Functor t => Decoration t where -- | @traverseF@ is the defining property. traverseF :: Functor m => (a -> m b) -> t a -> m (t b) traverseF f = distributeF . fmap f -- | Decorations commute into any functor. distributeF :: (Functor m) => t (m a) -> m (t a) distributeF = traverseF id -- | Any decoration is traversable with @traverse = traverseF@. -- Just like any 'Traversable' is a functor, so is -- any decoration, given by just @traverseF@, a functor. dmap :: Decoration t => (a -> b) -> t a -> t b dmap f = runIdentity . traverseF (Identity . f) -- | Any decoration is a lens. @set@ is a special case of @dmap@. dget :: Decoration t => t a -> a dget = getConst . traverseF Const -- | The identity functor is a decoration. instance Decoration Identity where traverseF f (Identity x) = Identity <$> f x -- | Decorations compose. (Thus, they form a category.) instance (Decoration d, Decoration t) => Decoration (Compose d t) where -- traverseF . traverseF :: Functor m => (a -> m b) -> d (t a) -> m (d (t a)) traverseF f (Compose x) = Compose <$> traverseF (traverseF f) x -- Not a decoration are: -- -- * The constant functor. -- * Maybe. Can only be traversed into pointed functors. -- * Other disjoint sum types, like lists etc. -- (Can only be traversed into Applicative.) -- | A typical decoration is pairing with some stuff. instance Decoration ((,) a) where traverseF f (a, x) = (a,) <$> f x Agda-2.5.3/src/full/Agda/Utils/VarSet.hs0000644000000000000000000000226513154613124015775 0ustar0000000000000000 -- | Var field implementation of sets of (small) natural numbers. module Agda.Utils.VarSet ( VarSet , union, unions, member, empty, delete, singleton , fromList, toList, toDescList , isSubsetOf, IntSet.null , intersection, difference , Agda.Utils.VarSet.subtract ) where import Data.IntSet as IntSet type VarSet = IntSet subtract :: Int -> VarSet -> VarSet subtract n = IntSet.map (Prelude.subtract n) {- import Data.Bits type VarSet = Integer type Var = Integer union :: VarSet -> VarSet -> VarSet union = (.|.) member :: Var -> VarSet -> Bool member b s = testVar s (fromIntegral b) empty :: VarSet empty = 0 delete :: Var -> VarSet -> VarSet delete b s = clearVar s (fromIntegral b) singleton :: Var -> VarSet singleton = bit subtract :: Int -> VarSet -> VarSet subtract n s = shiftR s n fromList :: [Var] -> VarSet fromList = foldr (union . singleton . fromIntegral) empty isSubsetOf :: VarSet -> VarSet -> Bool isSubsetOf s1 s2 = 0 == (s1 .&. complement s2) toList :: VarSet -> [Var] toList s = loop 0 s where loop i 0 = [] loop i n | testVar n 0 = fromIntegral i : (loop $! i + 1) (shiftR n 1) | otherwise = (loop $! i + 1) (shiftR n 1) -} Agda-2.5.3/src/full/Agda/Utils/Monoid.hs0000644000000000000000000000071513154613124016014 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving #-} -- | More monoids. module Agda.Utils.Monoid where import Data.Semigroup (Semigroup(..)) import Data.Monoid (Monoid(..)) -- | Maximum of on-negative (small) natural numbers. newtype MaxNat = MaxNat { getMaxNat :: Int } deriving (Num, Eq, Ord, Show, Enum) instance Semigroup MaxNat where (<>) = max instance Monoid MaxNat where mempty = 0 mappend = (<>) mconcat [] = 0 mconcat ms = maximum ms Agda-2.5.3/src/full/Agda/Utils/List.hs0000644000000000000000000002715413154613124015510 0ustar0000000000000000-- | Utility functions for lists. module Agda.Utils.List where import Control.Arrow (first) import Data.Functor ((<$>)) import Data.Function import qualified Data.List as List import Data.Maybe import qualified Data.Map as Map import qualified Data.Set as Set import Text.Show.Functions () import qualified Agda.Utils.Bag as Bag import Agda.Utils.Tuple -- | Case distinction for lists, with list first. -- Cf. 'Agda.Utils.Null.ifNull'. caseList :: [a] -> b -> (a -> [a] -> b) -> b caseList [] n c = n caseList (x:xs) n c = c x xs -- | Case distinction for lists, with list last. listCase :: b -> (a -> [a] -> b) -> [a] -> b listCase n c [] = n listCase n c (x:xs) = c x xs -- | Head function (safe). headMaybe :: [a] -> Maybe a headMaybe = listToMaybe -- | Head function (safe). Returns a value on empty lists. -- -- > headWithDefault 42 [] = 42 -- > headWithDefault 42 [1,2,3] = 1 headWithDefault :: a -> [a] -> a headWithDefault def = fromMaybe def . headMaybe -- | Last element (safe). lastMaybe :: [a] -> Maybe a lastMaybe [] = Nothing lastMaybe xs = Just $ last xs -- | Last two elements (safe). last2 :: [a] -> Maybe (a, a) last2 (x : y : xs) = Just $ loop x y xs where loop x y [] = (x, y) loop x y (z:xs) = loop y z xs last2 _ = Nothing -- | Opposite of cons @(:)@, safe. uncons :: [a] -> Maybe (a, [a]) uncons [] = Nothing uncons (x:xs) = Just (x,xs) -- | Maybe cons. @mcons ma as = maybeToList ma ++ as@ mcons :: Maybe a -> [a] -> [a] mcons ma as = maybe as (:as) ma -- | 'init' and 'last' in one go, safe. initLast :: [a] -> Maybe ([a],a) initLast [] = Nothing initLast (a:as) = Just $ loop a as where loop a [] = ([], a) loop a (b : bs) = mapFst (a:) $ loop b bs -- | Lookup function (partially safe). (!!!) :: [a] -> Int -> Maybe a [] !!! _ = Nothing (x : _) !!! 0 = Just x (_ : xs) !!! n = xs !!! (n - 1) -- | downFrom n = [n-1,..1,0] downFrom :: Integral a => a -> [a] downFrom n | n <= 0 = [] | otherwise = let n' = n-1 in n' : downFrom n' -- | Update the first element of a list, if it exists. updateHead :: (a -> a) -> [a] -> [a] updateHead f [] = [] updateHead f (a : as) = f a : as -- | Update the last element of a list, if it exists. updateLast :: (a -> a) -> [a] -> [a] updateLast f [] = [] updateLast f [a] = [f a] updateLast f (a : as@(_ : _)) = a : updateLast f as -- | Update nth element of a list, if it exists. -- Precondition: the index is >= 0. updateAt :: Int -> (a -> a) -> [a] -> [a] updateAt _ f [] = [] updateAt 0 f (a : as) = f a : as updateAt n f (a : as) = a : updateAt (n-1) f as -- | A generalized version of @partition@. -- (Cf. @mapMaybe@ vs. @filter@). mapEither :: (a -> Either b c) -> [a] -> ([b],[c]) {-# INLINE mapEither #-} mapEither f xs = foldr (deal f) ([],[]) xs deal :: (a -> Either b c) -> a -> ([b],[c]) -> ([b],[c]) deal f a ~(bs,cs) = case f a of Left b -> (b:bs, cs) Right c -> (bs, c:cs) -- | A generalized version of @takeWhile@. -- (Cf. @mapMaybe@ vs. @filter@). takeWhileJust :: (a -> Maybe b) -> [a] -> [b] takeWhileJust p = loop where loop (a : as) | Just b <- p a = b : loop as loop _ = [] -- | A generalized version of @span@. spanJust :: (a -> Maybe b) -> [a] -> ([b], [a]) spanJust p = loop where loop (a : as) | Just b <- p a = mapFst (b :) $ loop as loop as = ([], as) -- | Partition a list into 'Nothing's and 'Just's. -- @'mapMaybe' f = snd . partitionMaybe f@. partitionMaybe :: (a -> Maybe b) -> [a] -> ([a], [b]) partitionMaybe f = loop where loop [] = ([], []) loop (a : as) = case f a of Nothing -> mapFst (a :) $ loop as Just b -> mapSnd (b :) $ loop as -- | Like 'filter', but additionally return the last partition -- of the list where the predicate is @False@ everywhere. filterAndRest :: (a -> Bool) -> [a] -> ([a],[a]) filterAndRest p = mapMaybeAndRest $ \ a -> if p a then Just a else Nothing -- | Like 'mapMaybe', but additionally return the last partition -- of the list where the function always returns @Nothing@. mapMaybeAndRest :: (a -> Maybe b) -> [a] -> ([b],[a]) mapMaybeAndRest f = loop [] where loop acc = \case [] -> ([], reverse acc) x:xs | Just y <- f x -> first (y:) $ loop [] xs | otherwise -> loop (x:acc) xs -- | Drops from both lists simultaneously until one list is empty. dropCommon :: [a] -> [b] -> ([a],[b]) dropCommon (x : xs) (y : ys) = dropCommon xs ys dropCommon xs ys = (xs, ys) -- | Sublist relation. isSublistOf :: Eq a => [a] -> [a] -> Bool isSublistOf [] ys = True isSublistOf (x : xs) ys = case dropWhile (x /=) ys of [] -> False (_:ys) -> isSublistOf xs ys type Prefix a = [a] type Suffix a = [a] -- | Check if a list has a given prefix. If so, return the list -- minus the prefix. stripPrefixBy :: (a -> a -> Bool) -> Prefix a -> [a] -> Maybe (Suffix a) stripPrefixBy eq = loop where loop [] rest = Just rest loop (_:_) [] = Nothing loop (p:pat) (r:rest) | eq p r = loop pat rest | otherwise = Nothing -- | Result of 'preOrSuffix'. data PreOrSuffix a = IsPrefix a [a] -- ^ First list is prefix of second. | IsSuffix a [a] -- ^ First list is suffix of second. | IsBothfix -- ^ The lists are equal. | IsNofix -- ^ The lists are incomparable. -- | Compare lists with respect to prefix partial order. preOrSuffix :: Eq a => [a] -> [a] -> PreOrSuffix a preOrSuffix [] [] = IsBothfix preOrSuffix [] (b:bs) = IsPrefix b bs preOrSuffix (a:as) [] = IsSuffix a as preOrSuffix (a:as) (b:bs) | a == b = preOrSuffix as bs | otherwise = IsNofix -- | Split a list into sublists. Generalisation of the prelude function -- @words@. -- -- > words xs == wordsBy isSpace xs wordsBy :: (a -> Bool) -> [a] -> [[a]] wordsBy p xs = yesP xs where yesP xs = noP (dropWhile p xs) noP [] = [] noP xs = ys : yesP zs where (ys,zs) = break p xs -- | Chop up a list in chunks of a given length. chop :: Int -> [a] -> [[a]] chop _ [] = [] chop n xs = ys : chop n zs where (ys,zs) = splitAt n xs -- | Chop a list at the positions when the predicate holds. Contrary to -- 'wordsBy', consecutive separator elements will result in an empty segment -- in the result. -- > intercalate [x] (chopWhen (== x) xs) == xs chopWhen :: (a -> Bool) -> [a] -> [[a]] chopWhen p [] = [] chopWhen p xs = case break p xs of (w, []) -> [w] (w, [_]) -> [w, []] (w, _ : ys) -> w : chopWhen p ys -- | All ways of removing one element from a list. holes :: [a] -> [(a, [a])] holes [] = [] holes (x:xs) = (x, xs) : map (id -*- (x:)) (holes xs) -- | Check whether a list is sorted. -- -- Assumes that the 'Ord' instance implements a partial order. sorted :: Ord a => [a] -> Bool sorted [] = True sorted xs = and $ zipWith (<=) xs (tail xs) -- | Check whether all elements in a list are distinct from each -- other. Assumes that the 'Eq' instance stands for an equivalence -- relation. distinct :: Eq a => [a] -> Bool distinct [] = True distinct (x:xs) = x `notElem` xs && distinct xs -- | An optimised version of 'distinct'. -- -- Precondition: The list's length must fit in an 'Int'. fastDistinct :: Ord a => [a] -> Bool fastDistinct xs = Set.size (Set.fromList xs) == length xs -- | Checks if all the elements in the list are equal. Assumes that -- the 'Eq' instance stands for an equivalence relation. allEqual :: Eq a => [a] -> Bool allEqual [] = True allEqual (x : xs) = all (== x) xs -- | Returns an (arbitrary) representative for each list element -- that occurs more than once. duplicates :: Ord a => [a] -> [a] duplicates = mapMaybe dup . Bag.groups . Bag.fromList where dup (a : _ : _) = Just a dup _ = Nothing -- | A variant of 'List.groupBy' which applies the predicate to consecutive -- pairs. groupBy' :: (a -> a -> Bool) -> [a] -> [[a]] groupBy' _ [] = [] groupBy' p xxs@(x : xs) = grp x $ zipWith (\x y -> (p x y, y)) xxs xs where grp x ys = (x : map snd xs) : tail where (xs, rest) = span fst ys tail = case rest of [] -> [] ((_, z) : zs) -> grp z zs -- | @'groupOn' f = 'groupBy' (('==') \`on\` f) '.' 'List.sortBy' ('compare' \`on\` f)@. groupOn :: Ord b => (a -> b) -> [a] -> [[a]] groupOn f = List.groupBy ((==) `on` f) . List.sortBy (compare `on` f) -- | @splitExactlyAt n xs = Just (ys, zs)@ iff @xs = ys ++ zs@ -- and @genericLength ys = n@. splitExactlyAt :: Integral n => n -> [a] -> Maybe ([a], [a]) splitExactlyAt 0 xs = return ([], xs) splitExactlyAt n [] = Nothing splitExactlyAt n (x : xs) = mapFst (x :) <$> splitExactlyAt (n-1) xs -- | A generalised variant of 'elemIndex'. genericElemIndex :: (Eq a, Integral i) => a -> [a] -> Maybe i genericElemIndex x xs = listToMaybe $ map fst $ filter snd $ zip [0..] $ map (== x) xs -- | Requires both lists to have the same length. -- -- Otherwise, @Nothing@ is returned. zipWith' :: (a -> b -> c) -> [a] -> [b] -> Maybe [c] zipWith' f = loop where loop [] [] = Just [] loop (x : xs) (y : ys) = (f x y :) <$> loop xs ys loop [] (_ : _) = Nothing loop (_ : _) [] = Nothing -- -- UNUSED; a better type would be -- -- zipWithTails :: (a -> b -> c) -> [a] -> [b] -> ([c], Either [a] [b]) -- -- | Like zipWith, but returns the leftover elements of the input lists. -- zipWithTails :: (a -> b -> c) -> [a] -> [b] -> ([c], [a] , [b]) -- zipWithTails f xs [] = ([], xs, []) -- zipWithTails f [] ys = ([], [] , ys) -- zipWithTails f (x : xs) (y : ys) = (f x y : zs , as , bs) -- where (zs , as , bs) = zipWithTails f xs ys -- | Efficient variant of 'nubBy' for finite lists. -- -- Specification: -- -- > nubOn f xs == 'nubBy' ((==) `'on'` f) xs. nubOn :: Ord b => (a -> b) -> [a] -> [a] nubOn tag = map snd . List.sortBy (compare `on` fst) . map (snd . head) . List.groupBy ((==) `on` fst) . List.sortBy (compare `on` fst) . map (\p@(_, x) -> (tag x, p)) . zip [1..] -- | Efficient variant of 'nubBy' for finite lists. -- -- Specification: For each list @xs@ there is a list @ys@ which is a -- permutation of @xs@ such that -- -- > uniqOn f xs == 'nubBy' ((==) `'on'` f) ys. -- -- Furthermore -- -- > List.sortBy (compare `on` f) (uniqOn f xs) == uniqOn f xs. uniqOn :: Ord b => (a -> b) -> [a] -> [a] uniqOn key = Map.elems . Map.fromList . map (\ a -> (key a, a)) -- | Compute the common suffix of two lists. commonSuffix :: Eq a => [a] -> [a] -> [a] commonSuffix xs ys = reverse $ (commonPrefix `on` reverse) xs ys -- | Compute the common prefix of two lists. commonPrefix :: Eq a => [a] -> [a] -> [a] commonPrefix [] _ = [] commonPrefix _ [] = [] commonPrefix (x:xs) (y:ys) | x == y = x : commonPrefix xs ys | otherwise = [] editDistanceSpec :: Eq a => [a] -> [a] -> Int editDistanceSpec [] ys = length ys editDistanceSpec xs [] = length xs editDistanceSpec (x : xs) (y : ys) | x == y = editDistanceSpec xs ys | otherwise = 1 + minimum [ editDistanceSpec (x : xs) ys , editDistanceSpec xs (y : ys) , editDistanceSpec xs ys ] editDistance :: Eq a => [a] -> [a] -> Int editDistance xs ys = editD 0 0 where xss = List.tails xs yss = List.tails ys tbl = Map.fromList [ ((i, j), editD' i j) | i <- [0..length xss - 1], j <- [0..length yss - 1] ] editD i j = tbl Map.! (i, j) editD' i j = case (xss !! i, yss !! j) of ([], ys) -> length ys (xs, []) -> length xs (x : xs, y : ys) | x == y -> editD (i + 1) (j + 1) | otherwise -> 1 + minimum [ editD (i + 1) j, editD i (j + 1), editD (i + 1) (j + 1) ] Agda-2.5.3/src/full/Agda/Utils/Monad.hs0000644000000000000000000001633413154613124015631 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Utils.Monad ( module Agda.Utils.Monad , when, unless, MonadPlus(..) , (<$>), (<*>) , (<$) ) where import Prelude hiding (concat) import Control.Monad hiding (mapM, forM) import Control.Monad.State import Control.Monad.Writer import Control.Applicative import Data.Traversable as Trav hiding (for, sequence) import Data.Foldable as Fold import Data.Maybe import Agda.Utils.Either import Agda.Utils.Except ( Error(strMsg) , MonadError(catchError, throwError) ) import Agda.Utils.List #include "undefined.h" import Agda.Utils.Impossible -- | Binary bind. (==<<) :: Monad m => (a -> b -> m c) -> (m a, m b) -> m c k ==<< (ma, mb) = ma >>= \ a -> k a =<< mb -- Conditionals and monads ------------------------------------------------ whenM :: Monad m => m Bool -> m () -> m () whenM c m = c >>= (`when` m) unlessM :: Monad m => m Bool -> m () -> m () unlessM c m = c >>= (`unless` m) -- | Monadic guard. guardM :: (Monad m, MonadPlus m) => m Bool -> m () guardM c = guard =<< c -- | Monadic if-then-else. ifM :: Monad m => m Bool -> m a -> m a -> m a ifM c m m' = do b <- c if b then m else m' -- | @ifNotM mc = ifM (not <$> mc)@ ifNotM :: Monad m => m Bool -> m a -> m a -> m a ifNotM c = flip $ ifM c -- | Lazy monadic conjunction. and2M :: Monad m => m Bool -> m Bool -> m Bool and2M ma mb = ifM ma mb (return False) andM :: (Foldable f, Monad m) => f (m Bool) -> m Bool andM = Fold.foldl and2M (return True) allM :: (Functor f, Foldable f, Monad m) => f a -> (a -> m Bool) -> m Bool allM xs f = andM $ fmap f xs -- | Lazy monadic disjunction. or2M :: Monad m => m Bool -> m Bool -> m Bool or2M ma mb = ifM ma (return True) mb orM :: (Foldable f, Monad m) => f (m Bool) -> m Bool orM = Fold.foldl or2M (return False) anyM :: (Functor f, Foldable f, Monad m) => f a -> (a -> m Bool) -> m Bool anyM xs f = orM $ fmap f xs -- | Lazy monadic disjunction with @Either@ truth values. -- Returns the last error message if all fail. altM1 :: Monad m => (a -> m (Either err b)) -> [a] -> m (Either err b) altM1 f [] = __IMPOSSIBLE__ altM1 f [a] = f a altM1 f (a : as) = either (const $ altM1 f as) (return . Right) =<< f a -- | Lazy monadic disjunction with accumulation of errors in a monoid. -- Errors are discarded if we succeed. orEitherM :: (Monoid e, Monad m, Functor m) => [m (Either e b)] -> m (Either e b) orEitherM [] = return $ Left mempty orEitherM (m : ms) = caseEitherM m (\e -> mapLeft (e `mappend`) <$> orEitherM ms) (return . Right) -- Loops gathering results in a Monoid ------------------------------------ -- | Generalized version of @mapM_ :: Monad m => (a -> m ()) -> [a] -> m ()@ -- Executes effects and collects results in left-to-right order. -- Works best with left-associative monoids. -- -- Note that there is an alternative -- -- @mapM' f t = foldr mappend mempty <$> mapM f t@ -- -- that collects results in right-to-left order -- (effects still left-to-right). -- It might be preferable for right associative monoids. mapM' :: (Foldable t, Monad m, Monoid b) => (a -> m b) -> t a -> m b mapM' f = Fold.foldl (\ mb a -> liftM2 mappend mb (f a)) (return mempty) -- | Generalized version of @forM_ :: Monad m => [a] -> (a -> m ()) -> m ()@ forM' :: (Foldable t, Monad m, Monoid b) => t a -> (a -> m b) -> m b forM' = flip mapM' -- Continuation monad ----------------------------------------------------- -- Andreas, 2017-04-11, issue #2543 -- The terribly useful thread function is now UNUSED. [Sadistic laughter :)] -- -- type Cont r a = (a -> r) -> r -- -- -- | 'Control.Monad.mapM' for the continuation monad. Terribly useful. -- thread :: (a -> Cont r b) -> [a] -> Cont r [b] -- thread f [] ret = ret [] -- thread f (x:xs) ret = -- f x $ \y -> thread f xs $ \ys -> ret (y:ys) -- Lists and monads ------------------------------------------------------- -- | A monadic version of @'mapMaybe' :: (a -> Maybe b) -> [a] -> [b]@. mapMaybeM #if __GLASGOW_HASKELL__ <= 708 :: (Functor m, Monad m) #else :: Monad m #endif => (a -> m (Maybe b)) -> [a] -> m [b] mapMaybeM f xs = catMaybes <$> Trav.mapM f xs -- | The @for@ version of 'mapMaybeM'. forMaybeM #if __GLASGOW_HASKELL__ <= 708 :: (Functor m, Monad m) #else :: Monad m #endif => [a] -> (a -> m (Maybe b)) -> m [b] forMaybeM = flip mapMaybeM -- | A monadic version of @'dropWhile' :: (a -> Bool) -> [a] -> [a]@. dropWhileM :: Monad m => (a -> m Bool) -> [a] -> m [a] dropWhileM p [] = return [] dropWhileM p (x : xs) = ifM (p x) (dropWhileM p xs) (return (x : xs)) -- | A ``monadic'' version of @'partition' :: (a -> Bool) -> [a] -> ([a],[a]) partitionM :: (Functor m, Applicative m) => (a -> m Bool) -> [a] -> m ([a],[a]) partitionM f [] = pure ([], []) partitionM f (x:xs) = (\ b (l, r) -> if b then (x:l, r) else (l, x:r)) <$> f x <*> partitionM f xs -- Error monad ------------------------------------------------------------ -- | Finally for the 'Error' class. Errors in the finally part take -- precedence over prior errors. finally :: MonadError e m => m a -> m () -> m a first `finally` after = do r <- catchError (liftM Right first) (return . Left) after case r of Left e -> throwError e Right r -> return r -- | Try a computation, return 'Nothing' if an 'Error' occurs. tryMaybe :: (MonadError e m, Functor m) => m a -> m (Maybe a) tryMaybe m = (Just <$> m) `catchError` \ _ -> return Nothing -- State monad ------------------------------------------------------------ -- | Bracket without failure. Typically used to preserve state. bracket_ :: Monad m => m a -- ^ Acquires resource. Run first. -> (a -> m ()) -- ^ Releases resource. Run last. -> m b -- ^ Computes result. Run in-between. -> m b bracket_ acquire release compute = do resource <- acquire result <- compute release resource return result -- | Restore state after computation. localState :: MonadState s m => m a -> m a localState = bracket_ get put -- Read ------------------------------------------------------------------- readM :: (Error e, MonadError e m, Read a) => String -> m a readM s = case reads s of [(x,"")] -> return x _ -> throwError $ strMsg $ "readM: parse error string " ++ s -- RETIRED STUFF ---------------------------------------------------------- {- RETIRED, ASR, 09 September 2014. Not used. -- | Bracket for the 'Error' class. -- bracket :: (Error e, MonadError e m) -- => m a -- ^ Acquires resource. Run first. -- -> (a -> m c) -- ^ Releases resource. Run last. -- -> (a -> m b) -- ^ Computes result. Run in-between. -- -> m b -- bracket acquire release compute = do -- resource <- acquire -- compute resource `finally` release resource -} {- RETIRED, Andreas, 2012-04-30. Not used. concatMapM :: Applicative m => (a -> m [b]) -> [a] -> m [b] concatMapM f xs = concat <$> traverse f xs -- | Depending on the monad you have to look at the result for -- the force to be effective. For the 'IO' monad you do. forceM :: Monad m => [a] -> m () forceM xs = do () <- length xs `seq` return () return () commuteM :: (Traversable f, Applicative m) => f (m a) -> m (f a) commuteM = traverse id -} Agda-2.5.3/src/full/Agda/Utils/HashMap.hs0000644000000000000000000000066513154613124016114 0ustar0000000000000000module Agda.Utils.HashMap ( module HashMap ) where import Data.HashMap.Strict as HashMap -- ASR (20 January 2016) Issue 1779: I removed the @mapMaybe@ and -- @alter@ functions because them currently aren't used and -- them were added in unordered-containers 0.2.6.0. -- mapMaybe :: (a -> Maybe b) -> HashMap k a -> HashMap k b -- alter :: (Eq k, Hashable k) => -- (Maybe a -> Maybe a) -> k -> HashMap k a -> HashMap k a Agda-2.5.3/src/full/Agda/Utils/Map.hs0000644000000000000000000000343513154613124015306 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Utils.Map where import Prelude hiding (map, lookup, mapM) import Control.Applicative import Data.Map as Map import Data.Traversable import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- * Monadic map operations --------------------------------------------------------------------------- data EitherOrBoth a b = L a | B a b | R b -- | Not very efficient (goes via a list), but it'll do. unionWithM #if __GLASGOW_HASKELL__ <= 708 :: (Ord k, Functor m, Monad m) #else :: (Ord k, Monad m) #endif => (a -> a -> m a) -> Map k a -> Map k a -> m (Map k a) unionWithM f m1 m2 = fromList <$> mapM combine (toList m) where m = unionWith both (map L m1) (map R m2) both (L a) (R b) = B a b both _ _ = __IMPOSSIBLE__ combine (k, B a b) = (,) k <$> f a b combine (k, L a) = return (k, a) combine (k, R b) = return (k, b) insertWithKeyM :: (Ord k, Monad m) => (k -> a -> a -> m a) -> k -> a -> Map k a -> m (Map k a) insertWithKeyM clash k x m = case lookup k m of Just y -> do z <- clash k x y return $ insert k z m Nothing -> return $ insert k x m -- * Non-monadic map operations --------------------------------------------------------------------------- -- | Big conjunction over a map. allWithKey :: (k -> a -> Bool) -> Map k a -> Bool allWithKey f = Map.foldrWithKey (\ k a b -> f k a && b) True -- | Filter a map based on the keys. filterKeys :: (k -> Bool) -> Map k a -> Map k a filterKeys p = filterWithKey (const . p) -- | Unzip a map. unzip :: Map k (a, b) -> (Map k a, Map k b) unzip m = (Map.map fst m, Map.map snd m) unzip3 :: Map k (a, b, c) -> (Map k a, Map k b, Map k c) unzip3 m = (Map.map fst3 m, Map.map snd3 m, Map.map thd3 m) Agda-2.5.3/src/full/Agda/Utils/Except.hs0000644000000000000000000000174413154613124016022 0ustar0000000000000000 ------------------------------------------------------------------------------ -- | Wrapper for Control.Monad.Except from the mtl library (>= 2.2.1) ------------------------------------------------------------------------------ module Agda.Utils.Except ( Error(noMsg, strMsg) , ExceptT , mapExceptT , mkExceptT , MonadError(catchError, throwError) , runExceptT ) where import Control.Monad.Except ------------------------------------------------------------------------ -- | We cannot define data constructors synonymous, so we define the -- @mkExceptT@ function to be used instead of the data constructor -- @ExceptT@. mkExceptT :: m (Either e a) -> ExceptT e m a mkExceptT = ExceptT -- | Error class for backward compatibility (from -- Control.Monad.Trans.Error in transformers 0.3.0.0). class Error a where noMsg :: a strMsg :: String -> a noMsg = strMsg "" strMsg _ = noMsg -- | A string can be thrown as an error. instance Error String where strMsg = id Agda-2.5.3/src/full/Agda/Utils/IORef.hs0000644000000000000000000000054513154613124015534 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Utilities for Data.IORef. module Agda.Utils.IORef ( module Data.IORef , module Agda.Utils.IORef ) where import Data.IORef -- | Read 'IORef', modify it strictly, and return old value. readModifyIORef' :: IORef a -> (a -> a) -> IO a readModifyIORef' ref f = do x <- readIORef ref writeIORef ref $! f x return x Agda-2.5.3/src/full/Agda/Utils/Benchmark.hs0000644000000000000000000001557613154613124016474 0ustar0000000000000000{-# LANGUAGE NoMonomorphismRestriction #-} {-# LANGUAGE UndecidableInstances #-} -- | Tools for benchmarking and accumulating results. -- Nothing Agda-specific in here. module Agda.Utils.Benchmark where import Prelude hiding (null) import qualified Control.Exception as E (evaluate) import Control.Monad.Reader import Control.Monad.State import Data.Foldable (foldMap) import Data.Functor import Data.Function import qualified Data.List as List import Data.Monoid import Data.Maybe import qualified Text.PrettyPrint.Boxes as Boxes import Agda.Utils.Null import Agda.Utils.Monad hiding (finally) import qualified Agda.Utils.Maybe.Strict as Strict import Agda.Utils.Pretty import Agda.Utils.Time import Agda.Utils.Trie (Trie) import qualified Agda.Utils.Trie as Trie -- * Benchmark trie -- | Account we can bill computation time to. type Account a = [a] -- | Record when we started billing the current account. type CurrentAccount a = Strict.Maybe (Account a, CPUTime) type Timings a = Trie a CPUTime data BenchmarkOn a = BenchmarkOff | BenchmarkOn | BenchmarkSome (Account a -> Bool) isBenchmarkOn :: Account a -> BenchmarkOn a -> Bool isBenchmarkOn _ BenchmarkOff = False isBenchmarkOn _ BenchmarkOn = True isBenchmarkOn a (BenchmarkSome p) = p a -- | Benchmark structure is a trie, mapping accounts (phases and subphases) -- to CPU time spent on their performance. data Benchmark a = Benchmark { benchmarkOn :: !(BenchmarkOn a) -- ^ Are we benchmarking at all? , currentAccount :: !(CurrentAccount a) -- ^ What are we billing to currently? , timings :: !(Timings a) -- ^ The accounts and their accumulated timing bill. } -- | Initial benchmark structure (empty). instance Null (Benchmark a) where empty = Benchmark { benchmarkOn = BenchmarkOff , currentAccount = Strict.Nothing , timings = empty } null = null . timings -- | Semantic editor combinator. mapBenchmarkOn :: (BenchmarkOn a -> BenchmarkOn a) -> Benchmark a -> Benchmark a mapBenchmarkOn f b = b { benchmarkOn = f $ benchmarkOn b } -- | Semantic editor combinator. mapCurrentAccount :: (CurrentAccount a -> CurrentAccount a) -> Benchmark a -> Benchmark a mapCurrentAccount f b = b { currentAccount = f (currentAccount b) } -- | Semantic editor combinator. mapTimings :: (Timings a -> Timings a) -> Benchmark a -> Benchmark a mapTimings f b = b { timings = f (timings b) } -- | Add to specified CPU time account. addCPUTime :: Ord a => Account a -> CPUTime -> Benchmark a -> Benchmark a addCPUTime acc t = mapTimings (Trie.insertWith (+) acc t) -- | Print benchmark as three-column table with totals. instance (Ord a, Pretty a) => Pretty (Benchmark a) where pretty b = text $ Boxes.render table where trie = timings b (accounts, times0) = unzip $ Trie.toListOrderedBy (flip compare `on` snd) $ Trie.filter ((> fromMilliseconds 10) . snd) $ Trie.mapSubTries (Just . aggr) trie times = map fst times0 aggr t = (fromMaybe 0 $ Trie.lookup [] t, getSum $ foldMap Sum t) aggrTimes = do (a, (t, aggrT)) <- zip accounts times0 return $ if t == aggrT || null a then Boxes.text "" else Boxes.text $ "(" ++ prettyShow aggrT ++ ")" -- Generate a table. table = Boxes.hsep 1 Boxes.left [col1, col2, col3] -- First column: Accounts. col1 = Boxes.vcat Boxes.left $ map Boxes.text $ "Total" : map showAccount accounts -- Second column: Times. col2 = Boxes.vcat Boxes.right $ map (Boxes.text . prettyShow) $ sum times : times -- Thid column: Aggregate times. col3 = Boxes.vcat Boxes.right $ Boxes.text "" : aggrTimes showAccount [] = "Miscellaneous" showAccount ks = List.intercalate "." $ map prettyShow ks -- * Benchmarking monad. -- | Monad with access to benchmarking data. class (Ord a, Functor m, MonadIO m) => MonadBench a m | m -> a where getBenchmark :: m (Benchmark a) getsBenchmark :: (Benchmark a -> c) -> m c getsBenchmark f = f <$> getBenchmark putBenchmark :: Benchmark a -> m () putBenchmark b = modifyBenchmark $ const b modifyBenchmark :: (Benchmark a -> Benchmark a) -> m () modifyBenchmark f = do b <- getBenchmark putBenchmark $! f b -- | We need to be able to terminate benchmarking in case of an exception. finally :: m b -> m c -> m b -- needs UndecidableInstances because of weakness of FunctionalDependencies instance MonadBench a m => MonadBench a (ReaderT r m) where getBenchmark = lift $ getBenchmark putBenchmark = lift . putBenchmark modifyBenchmark = lift . modifyBenchmark finally m f = ReaderT $ \ r -> finally (m `runReaderT` r) (f `runReaderT` r) instance MonadBench a m => MonadBench a (StateT r m) where getBenchmark = lift $ getBenchmark putBenchmark = lift . putBenchmark modifyBenchmark = lift . modifyBenchmark finally m f = StateT $ \s -> finally (m `runStateT` s) (f `runStateT` s) -- | Turn benchmarking on/off. setBenchmarking :: MonadBench a m => BenchmarkOn a -> m () setBenchmarking b = modifyBenchmark $ mapBenchmarkOn $ const b -- | Bill current account with time up to now. -- Switch to new account. -- Return old account (if any). switchBenchmarking :: MonadBench a m => Strict.Maybe (Account a) -- ^ Maybe new account. -> m (Strict.Maybe (Account a)) -- ^ Maybe old account. switchBenchmarking newAccount = do now <- liftIO $ getCPUTime -- Stop and bill current benchmarking. oldAccount <- getsBenchmark currentAccount Strict.whenJust oldAccount $ \ (acc, start) -> modifyBenchmark $ addCPUTime acc $ now - start -- Switch to new account. modifyBenchmark $ mapCurrentAccount $ const $ (, now) <$> newAccount return $ fst <$> oldAccount -- | Resets the account and the timing information. reset :: MonadBench a m => m () reset = modifyBenchmark $ mapCurrentAccount (const Strict.Nothing) . mapTimings (const Trie.empty) -- | Bill a computation to a specific account. -- Works even if the computation is aborted by an exception. billTo :: MonadBench a m => Account a -> m c -> m c billTo account m = ifNotM (isBenchmarkOn account <$> getsBenchmark benchmarkOn) m $ do -- Switch to new account. old <- switchBenchmarking $ Strict.Just account -- Compute and switch back to old account. (liftIO . E.evaluate =<< m) `finally` switchBenchmarking old -- | Bill a CPS function to an account. Can't handle exceptions. billToCPS :: MonadBench a m => Account a -> ((b -> m c) -> m c) -> (b -> m c) -> m c billToCPS account f k = ifNotM (isBenchmarkOn account <$> getsBenchmark benchmarkOn) (f k) $ do -- Switch to new account. old <- switchBenchmarking $ Strict.Just account f $ \ x -> x `seq` do _ <- switchBenchmarking old k x -- | Bill a pure computation to a specific account. billPureTo :: MonadBench a m => Account a -> c -> m c billPureTo account = billTo account . return Agda-2.5.3/src/full/Agda/Utils/Pointer.hs0000644000000000000000000000441213154613124016205 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE BangPatterns #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE StandaloneDeriving #-} module Agda.Utils.Pointer ( Ptr, newPtr, derefPtr, setPtr , updatePtr, updatePtrM ) where import Control.Applicative import Control.DeepSeq import Control.Concurrent.MVar import Data.Foldable import Data.Function import Data.Hashable import Data.IORef import Data.Traversable import System.IO.Unsafe import Data.Data (Data (..)) import Data.Typeable (Typeable) #include "undefined.h" import Agda.Utils.Impossible data Ptr a = Ptr { ptrTag :: !Integer , ptrRef :: !(IORef a) } deriving (Typeable, Data) -- cheating because you shouldn't be digging this far anyway instance Typeable a => Data (IORef a) where gunfold _ _ _ = __IMPOSSIBLE__ toConstr = __IMPOSSIBLE__ dataTypeOf = __IMPOSSIBLE__ {-# NOINLINE freshVar #-} freshVar :: MVar Integer freshVar = unsafePerformIO $ newMVar 0 fresh :: IO Integer fresh = do x <- takeMVar freshVar putMVar freshVar $! x + 1 return x {-# NOINLINE newPtr #-} newPtr :: a -> Ptr a newPtr x = unsafePerformIO $ do i <- fresh Ptr i <$> newIORef x derefPtr :: Ptr a -> a derefPtr p = unsafePerformIO $ readIORef $ ptrRef p {-# NOINLINE updatePtr #-} updatePtr :: (a -> a) -> Ptr a -> Ptr a updatePtr f p = unsafePerformIO $ p <$ modifyIORef (ptrRef p) f setPtr :: a -> Ptr a -> Ptr a setPtr !x = updatePtr (const x) -- | If @f a@ contains many copies of @a@ they will all be the same pointer in -- the result. If the function is well-behaved (i.e. preserves the implicit -- equivalence, this shouldn't matter). updatePtrM :: Functor f => (a -> f a) -> Ptr a -> f (Ptr a) updatePtrM f p = flip setPtr p <$> f (derefPtr p) instance Show a => Show (Ptr a) where show p = "#" ++ show (ptrTag p) ++ "{" ++ show (derefPtr p) ++ "}" instance Functor Ptr where fmap f = newPtr . f . derefPtr instance Foldable Ptr where foldMap f = f . derefPtr instance Traversable Ptr where traverse f p = newPtr <$> f (derefPtr p) instance Eq (Ptr a) where (==) = (==) `on` ptrTag instance Ord (Ptr a) where compare = compare `on` ptrTag instance Hashable (Ptr a) where hashWithSalt salt = (hashWithSalt salt) . ptrTag instance NFData (Ptr a) where rnf x = seq x () Agda-2.5.3/src/full/Agda/Utils/Suffix.hs0000644000000000000000000000552113154613124016033 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Utils.Suffix where import Data.Char import Agda.Utils.Function #include "undefined.h" import Agda.Utils.Impossible ------------------------------------------------------------------------ -- Subscript digits -- | Is the character one of the subscripts @'₀'@-@'₉'@? isSubscriptDigit :: Char -> Bool isSubscriptDigit c = '₀' <= c && c <= '₉' -- | Converts @'0'@-@'9'@ to @'₀'@-@'₉'@. -- -- Precondition: The digit needs to be in range. toSubscriptDigit :: Char -> Char toSubscriptDigit d | isDigit d = toEnum (fromEnum '₀' + (fromEnum d - fromEnum '0')) | otherwise = __IMPOSSIBLE__ -- | Converts @'₀'@-@'₉'@ to @'0'@-@'9'@. -- -- Precondition: The digit needs to be in range. fromSubscriptDigit :: Char -> Char fromSubscriptDigit d | isSubscriptDigit d = toEnum (fromEnum '0' + (fromEnum d - fromEnum '₀')) | otherwise = __IMPOSSIBLE__ ------------------------------------------------------------------------ -- Suffices -- | Classification of identifier variants. data Suffix = NoSuffix | Prime Int -- ^ Identifier ends in @Int@ many primes. | Index Int -- ^ Identifier ends in number @Int@ (ordinary digits). | Subscript Int -- ^ Identifier ends in number @Int@ (subscript digits). -- | Increase the suffix by one. If no suffix yet, put a subscript @1@. nextSuffix :: Suffix -> Suffix nextSuffix NoSuffix = Subscript 1 nextSuffix (Prime i) = Prime $ i + 1 nextSuffix (Index i) = Index $ i + 1 nextSuffix (Subscript i) = Subscript $ i + 1 -- | Parse suffix. suffixView :: String -> (String, Suffix) suffixView s | (ps@(_:_), s') <- span (=='\'') rs = (reverse s', Prime $ length ps) | (ns@(_:_), s') <- span isDigit rs = (reverse s', Index $ read $ reverse ns) | (ns@(_:_), s') <- span isSubscriptDigit rs = (reverse s', Subscript $ read $ map fromSubscriptDigit $ reverse ns) | otherwise = (s, NoSuffix) where rs = reverse s -- | Print suffix. addSuffix :: String -> Suffix -> String addSuffix s NoSuffix = s addSuffix s (Prime n) = s ++ replicate n '\'' addSuffix s (Index i) = s ++ show i addSuffix s (Subscript i) = s ++ map toSubscriptDigit (show i) -- | Add first available @Suffix@ to a name. nameVariant :: (String -> Bool) -- ^ Is the given name already taken? -> String -- ^ Name of which we want an available variant. -> String -- ^ Name extended by suffix that is not taken already. nameVariant taken x | taken x = addSuffix x $ trampoline step $ Subscript 1 | otherwise = x where -- if the current suffix is taken, repeat with next suffix, else done step s = if taken (addSuffix x s) then Right (nextSuffix s) else Left s Agda-2.5.3/src/full/Agda/Utils/PartialOrd.hs0000644000000000000000000001770513154613124016637 0ustar0000000000000000 module Agda.Utils.PartialOrd where import Data.Functor import Data.Maybe import Data.Semigroup import Data.Set (Set) import qualified Data.Set as Set -- import Agda.Utils.List -- | The result of comparing two things (of the same type). data PartialOrdering = POLT -- ^ Less than. | POLE -- ^ Less or equal than. | POEQ -- ^ Equal | POGE -- ^ Greater or equal. | POGT -- ^ Greater than. | POAny -- ^ No information (incomparable). deriving (Eq, Show, Enum, Bounded) -- | Comparing the information content of two elements of -- 'PartialOrdering'. More precise information is smaller. -- -- Includes equality: @x `leqPO` x == True@. leqPO :: PartialOrdering -> PartialOrdering -> Bool leqPO _ POAny = True leqPO POLT POLT = True leqPO POLT POLE = True leqPO POLE POLE = True leqPO POEQ POLE = True leqPO POEQ POEQ = True leqPO POEQ POGE = True leqPO POGE POGE = True leqPO POGT POGT = True leqPO POGT POGE = True leqPO _ _ = False -- | Opposites. -- -- @related a po b@ iff @related b (oppPO po) a@. oppPO :: PartialOrdering -> PartialOrdering oppPO POLT = POGT oppPO POLE = POGE oppPO POEQ = POEQ oppPO POGE = POLE oppPO POGT = POLT oppPO POAny = POAny -- | Combining two pieces of information (picking the least information). -- Used for the dominance ordering on tuples. -- -- @orPO@ is associative, commutative, and idempotent. -- @orPO@ has dominant element @POAny@, but no neutral element. orPO :: PartialOrdering -> PartialOrdering -> PartialOrdering orPO POAny _ = POAny -- Shortcut if no information on first. orPO POLT POLT = POLT -- idempotent orPO POLT POLE = POLE orPO POLT POEQ = POLE orPO POLE POLT = POLE orPO POLE POLE = POLE -- idempotent orPO POLE POEQ = POLE orPO POEQ POLT = POLE orPO POEQ POLE = POLE orPO POEQ POEQ = POEQ -- idempotent orPO POEQ POGE = POGE orPO POEQ POGT = POGE orPO POGE POEQ = POGE orPO POGE POGE = POGE -- idempotent orPO POGE POGT = POGE orPO POGT POEQ = POGE orPO POGT POGE = POGE orPO POGT POGT = POGT -- idempotent orPO _ _ = POAny -- | Chains (transitivity) @x R y S z@. -- -- @seqPO@ is associative, commutative, and idempotent. -- @seqPO@ has dominant element @POAny@ and neutral element (unit) @POEQ@. seqPO :: PartialOrdering -> PartialOrdering -> PartialOrdering seqPO POAny _ = POAny -- Shortcut if no information on first. seqPO POEQ p = p -- No need to look at second if first is neutral. seqPO POLT POLT = POLT -- idempotent seqPO POLT POLE = POLT seqPO POLT POEQ = POLT -- unit seqPO POLE POLT = POLT seqPO POLE POLE = POLE -- idempotent seqPO POLE POEQ = POLE -- unit seqPO POGE POEQ = POGE -- unit seqPO POGE POGE = POGE -- idempotent seqPO POGE POGT = POGT seqPO POGT POEQ = POGT -- unit seqPO POGT POGE = POGT seqPO POGT POGT = POGT -- idempotent seqPO _ _ = POAny -- | Partial ordering forms a monoid under sequencing. instance Semigroup PartialOrdering where (<>) = seqPO instance Monoid PartialOrdering where mempty = POEQ mappend = (<>) -- | Embed 'Ordering'. fromOrdering :: Ordering -> PartialOrdering fromOrdering LT = POLT fromOrdering EQ = POEQ fromOrdering GT = POGT -- | Represent a non-empty disjunction of 'Ordering's as 'PartialOrdering'. fromOrderings :: [Ordering] -> PartialOrdering fromOrderings = foldr1 orPO . map fromOrdering -- | A 'PartialOrdering' information is a disjunction of 'Ordering' informations. toOrderings :: PartialOrdering -> [Ordering] toOrderings POLT = [LT] toOrderings POLE = [LT, EQ] toOrderings POEQ = [EQ] toOrderings POGE = [EQ, GT] toOrderings POGT = [GT] toOrderings POAny = [LT, EQ, GT] -- * Comparison with partial result type Comparable a = a -> a -> PartialOrdering -- | Decidable partial orderings. class PartialOrd a where comparable :: Comparable a -- | Any 'Ord' is a 'PartialOrd'. comparableOrd :: Ord a => Comparable a comparableOrd x y = fromOrdering $ compare x y -- | Are two elements related in a specific way? -- -- @related a o b@ holds iff @comparable a b@ is contained in @o@. related :: PartialOrd a => a -> PartialOrdering -> a -> Bool related a o b = comparable a b `leqPO` o -- * Totally ordered types. instance PartialOrd Int where comparable = comparableOrd instance PartialOrd Integer where comparable = comparableOrd -- * Generic partially ordered types. instance PartialOrd () where comparable _ _ = POEQ -- | 'Nothing' and @'Just' _@ are unrelated. -- -- Partial ordering for @Maybe a@ is the same as for @Either () a@. instance PartialOrd a => PartialOrd (Maybe a) where comparable mx my = case (mx, my) of (Nothing, Nothing) -> POEQ (Nothing, Just{} ) -> POAny (Just{} , Nothing) -> POAny (Just x , Just y ) -> comparable x y -- | Partial ordering for disjoint sums: @Left _@ and @Right _@ are unrelated. instance (PartialOrd a, PartialOrd b) => PartialOrd (Either a b) where comparable mx my = case (mx, my) of (Left x, Left y) -> comparable x y (Left _, Right _) -> POAny (Right _, Left _) -> POAny (Right x, Right y) -> comparable x y -- | Pointwise partial ordering for tuples. -- -- @related (x1,x2) o (y1,y2)@ iff @related x1 o x2@ and @related y1 o y2@. instance (PartialOrd a, PartialOrd b) => PartialOrd (a, b) where comparable (x1, x2) (y1, y2) = comparable x1 y1 `orPO` comparable x2 y2 -- | Pointwise comparison wrapper. newtype Pointwise a = Pointwise { pointwise :: a } deriving (Eq, Show, Functor) -- | The pointwise ordering for lists of the same length. -- -- There are other partial orderings for lists, -- e.g., prefix, sublist, subset, lexicographic, simultaneous order. instance PartialOrd a => PartialOrd (Pointwise [a]) where comparable (Pointwise xs) (Pointwise ys) = loop Nothing xs ys -- We need an accumulator since @orPO@ does not have a neutral element. where loop mo [] [] = fromMaybe POEQ mo loop _ [] ys = POAny loop _ xs [] = POAny loop mo (x:xs) (y:ys) = let o = comparable x y in case maybe o (orPO o) mo of POAny -> POAny o -> loop (Just o) xs ys -- | Inclusion comparison wrapper. newtype Inclusion a = Inclusion { inclusion :: a } deriving (Eq, Ord, Show, Functor) -- | Sublist for ordered lists. instance (Ord a) => PartialOrd (Inclusion [a]) where comparable (Inclusion xs) (Inclusion ys) = merge POEQ xs ys where -- We use an accumulator in order to short-cut computation -- once we know the lists are incomparable. merge' POAny xs ys = POAny merge' o xs ys = merge o xs ys merge o [] [] = o merge o [] ys = mappend o POLT merge o xs [] = mappend o POGT merge o xs@(x:xs') ys@(y:ys') = case compare x y of -- xs has an element that ys does not have => POGT LT -> merge' (mappend o POGT) xs' ys -- equal elements can be cancelled EQ -> merge o xs' ys' -- ys has an element that xs does not have => POLT GT -> merge' (mappend o POLT) xs ys' -- | Sets are partially ordered by inclusion. instance Ord a => PartialOrd (Inclusion (Set a)) where comparable s t = comparable (Set.toAscList <$> s) (Set.toAscList <$> t) -- * PartialOrdering is itself partially ordered! -- | Less is ``less general'' (i.e., more precise). instance PartialOrd PartialOrdering where -- working our way down: POAny is top comparable POAny POAny = POEQ comparable POAny _ = POGT comparable _ POAny = POLT -- next are the fuzzy notions POLE and POGE comparable POLE POLE = POEQ comparable POLE POLT = POGT comparable POLE POEQ = POGT comparable POGE POGE = POEQ comparable POGE POGT = POGT comparable POGE POEQ = POGT -- lowest are the precise notions POLT POEQ POGT comparable POLT POLT = POEQ comparable POLT POLE = POLT comparable POEQ POEQ = POEQ comparable POEQ POLE = POLT comparable POEQ POGE = POLT comparable POGT POGT = POEQ comparable POGT POGE = POLT -- anything horizontal is not comparable comparable _ _ = POAny Agda-2.5.3/src/full/Agda/Utils/Environment.hs0000644000000000000000000000256213154613124017075 0ustar0000000000000000 -- | Expand environment variables in strings module Agda.Utils.Environment ( expandEnvironmentVariables ) where import Data.Char import Data.Maybe import System.Environment import System.Directory ( getHomeDirectory ) expandEnvironmentVariables :: String -> IO String expandEnvironmentVariables s = do env <- getEnvironment home <- getHomeDirectory return $ expandVars home env s expandVars :: String -> [(String, String)] -> String -> String expandVars home env s = concatMap repl $ tokens s where repl Home = home ++ "/" repl (Var x) = fromMaybe "" $ lookup x env repl (Str s) = s data Token = Home | Var String | Str String deriving (Eq, Show) tokens :: String -> [Token] tokens s = case s of '~' : '/' : s -> Home : tokens' s '\\' : '~' : s -> cons '~' $ tokens' s _ -> tokens' s where tokens' :: String -> [Token] tokens' s = case s of '$' : '$' : s -> cons '$' $ tokens' s '$' : s@(c : _) | c == '_' || isAlpha c -> Var x : tokens' s' where (x, s') = span (\ c -> c == '_' || isAlphaNum c) s '$' : '{' : s -> case break (== '}') s of (x, '}' : s) -> Var x : tokens' s _ -> [Str $ "${" ++ s] -- abort on unterminated '{' c : s -> cons c $ tokens' s "" -> [] cons c (Str s : ts) = Str (c : s) : ts cons c ts = Str [c] : ts Agda-2.5.3/src/full/Agda/Utils/Permutation.hs0000644000000000000000000002112713154613124017076 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} module Agda.Utils.Permutation where import Prelude hiding (drop, null) import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import qualified Data.List as List import Data.Maybe import Data.Array import Data.Foldable (Foldable) import Data.Traversable (Traversable) import Data.Data (Data) import Data.Typeable (Typeable) import Agda.Syntax.Position (KillRange(..)) import Agda.Utils.Functor import Agda.Utils.List ((!!!)) import Agda.Utils.Null import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | Partial permutations. Examples: -- -- @permute [1,2,0] [x0,x1,x2] = [x1,x2,x0]@ (proper permutation). -- -- @permute [1,0] [x0,x1,x2] = [x1,x0]@ (partial permuation). -- -- @permute [1,0,1,2] [x0,x1,x2] = [x1,x0,x1,x2]@ (not a permutation because not invertible). -- -- Agda typing would be: -- @Perm : {m : Nat}(n : Nat) -> Vec (Fin n) m -> Permutation@ -- @m@ is the 'size' of the permutation. data Permutation = Perm { permRange :: Int, permPicks :: [Int] } deriving (Eq, Typeable, Data) instance Show Permutation where show (Perm n xs) = showx [0..n - 1] ++ " -> " ++ showx xs where showx = showList "," (\ i -> "x" ++ show i) showList :: String -> (a -> String) -> [a] -> String showList sep f [] = "" showList sep f [e] = f e showList sep f (e:es) = f e ++ sep ++ showList sep f es instance Sized Permutation where size (Perm _ xs) = size xs instance Null Permutation where empty = Perm 0 [] null (Perm _ picks) = null picks instance KillRange Permutation where killRange = id -- | @permute [1,2,0] [x0,x1,x2] = [x1,x2,x0]@ -- More precisely, @permute indices list = sublist@, generates @sublist@ -- from @list@ by picking the elements of list as indicated by @indices@. -- @permute [1,3,0] [x0,x1,x2,x3] = [x1,x3,x0]@ -- -- Agda typing: -- @permute (Perm {m} n is) : Vec A m -> Vec A n@ permute :: Permutation -> [a] -> [a] permute p xs = map (fromMaybe __IMPOSSIBLE__) (safePermute p xs) safePermute :: Permutation -> [a] -> [Maybe a] safePermute (Perm _ is) xs = map (xs !!!!) is where xs !!!! n | n < 0 = Nothing | otherwise = xs !!! n -- | Invert a Permutation on a partial finite int map. -- @inversePermute perm f = f'@ -- such that @permute perm f' = f@ -- -- Example, with map represented as @[Maybe a]@: -- @ -- f = [Nothing, Just a, Just b ] -- perm = Perm 4 [3,0,2] -- f' = [ Just a , Nothing , Just b , Nothing ] -- @ -- Zipping @perm@ with @f@ gives @[(0,a),(2,b)]@, after compression -- with @catMaybes@. This is an @IntMap@ which can easily -- written out into a substitution again. class InversePermute a b where inversePermute :: Permutation -> a -> b instance InversePermute [Maybe a] [(Int,a)] where inversePermute (Perm n is) = catMaybes . zipWith (\ i ma -> (i,) <$> ma) is instance InversePermute [Maybe a] (IntMap a) where inversePermute p = IntMap.fromList . inversePermute p instance InversePermute [Maybe a] [Maybe a] where inversePermute p@(Perm n _) = tabulate . inversePermute p where tabulate m = for [0..n-1] $ \ i -> IntMap.lookup i m instance InversePermute (Int -> a) [Maybe a] where inversePermute (Perm n xs) f = for [0..n-1] $ \ x -> f <$> List.findIndex (x ==) xs -- | Identity permutation. idP :: Int -> Permutation idP n = Perm n [0..n - 1] -- | Restrict a permutation to work on @n@ elements, discarding picks @>=n@. takeP :: Int -> Permutation -> Permutation takeP n (Perm m xs) = Perm n $ filter (< n) xs -- | Pick the elements that are not picked by the permutation. droppedP :: Permutation -> Permutation droppedP (Perm n xs) = Perm n $ [0..n-1] List.\\ xs -- | @liftP k@ takes a @Perm {m} n@ to a @Perm {m+k} (n+k)@. -- Analogous to 'Agda.TypeChecking.Substitution.liftS', -- but Permutations operate on de Bruijn LEVELS, not indices. liftP :: Int -> Permutation -> Permutation liftP n (Perm m xs) = Perm (n + m) $ xs ++ [m..m+n-1] -- liftP n (Perm m xs) = Perm (n + m) $ [0..n-1] ++ map (n+) xs -- WRONG, works for indices, but not for levels -- | @permute (compose p1 p2) == permute p1 . permute p2@ composeP :: Permutation -> Permutation -> Permutation composeP p1 (Perm n xs) = Perm n $ permute p1 xs {- proof: permute (compose (Perm xs) (Perm ys)) zs == permute (Perm (permute (Perm xs) ys)) zs == map (zs !!) (permute (Perm xs) ys) == map (zs !!) (map (ys !!) xs) == map (zs !! . ys !!) xs == map (\x -> zs !! (ys !! x)) xs == map (\x -> map (zs !!) ys !! x) xs {- map f xs !! n == f (xs !! n) -} == map (map (zs !!) ys !!) xs == permute (Perm xs) (permute (Perm ys) zs) -} -- | @invertP err p@ is the inverse of @p@ where defined, -- otherwise defaults to @err@. -- @composeP p (invertP err p) == p@ invertP :: Int -> Permutation -> Permutation invertP err p@(Perm n xs) = Perm (size xs) $ elems tmpArray where tmpArray = accumArray (flip const) err (0, n-1) $ zip xs [0..] -- | Turn a possible non-surjective permutation into a surjective permutation. compactP :: Permutation -> Permutation compactP (Perm n xs) = Perm m $ map adjust xs where m = List.genericLength xs missing = [0..n - 1] List.\\ xs holesBelow k = List.genericLength $ filter (< k) missing adjust k = k - holesBelow k -- | @permute (reverseP p) xs == -- reverse $ permute p $ reverse xs@ -- -- Example: -- @ -- permute (reverseP (Perm 4 [1,3,0])) [x0,x1,x2,x3] -- == permute (Perm 4 $ map (3-) [0,3,1]) [x0,x1,x2,x3] -- == permute (Perm 4 [3,0,2]) [x0,x1,x2,x3] -- == [x3,x0,x2] -- == reverse [x2,x0,x3] -- == reverse $ permute (Perm 4 [1,3,0]) [x3,x2,x1,x0] -- == reverse $ permute (Perm 4 [1,3,0]) $ reverse [x0,x1,x2,x3] -- @ -- -- With @reverseP@, you can convert a permutation on de Bruijn indices -- to one on de Bruijn levels, and vice versa. reverseP :: Permutation -> Permutation reverseP (Perm n xs) = Perm n $ map ((n - 1) -) $ reverse xs -- = flipP $ Perm n $ reverse xs -- | @permPicks (flipP p) = permute p (downFrom (permRange p))@ -- or -- @permute (flipP (Perm n xs)) [0..n-1] = permute (Perm n xs) (downFrom n)@ -- -- Can be use to turn a permutation from (de Bruijn) levels to levels -- to one from levels to indices. -- -- See 'Agda.Syntax.Internal.Patterns.numberPatVars'. flipP :: Permutation -> Permutation flipP (Perm n xs) = Perm n $ map (n - 1 -) xs -- | @expandP i n π@ in the domain of @π@ replace the /i/th element by /n/ elements. expandP :: Int -> Int -> Permutation -> Permutation expandP i n (Perm m xs) = Perm (m + n - 1) $ concatMap expand xs where expand j | j == i = [i..i + n - 1] | j < i = [j] | otherwise = [j + n - 1] -- | Stable topologic sort. The first argument decides whether its first -- argument is an immediate parent to its second argument. topoSort :: (a -> a -> Bool) -> [a] -> Maybe Permutation topoSort parent xs = fmap (Perm (size xs)) $ topo g where nodes = zip [0..] xs g = [ (n, parents x) | (n, x) <- nodes ] parents x = [ n | (n, y) <- nodes, parent y x ] topo :: Eq node => [(node, [node])] -> Maybe [node] topo [] = return [] topo g = case xs of [] -> fail "cycle detected" x : _ -> do ys <- topo $ remove x g return $ x : ys where xs = [ x | (x, []) <- g ] remove x g = [ (y, filter (/= x) ys) | (y, ys) <- g, x /= y ] ------------------------------------------------------------------------ -- * Drop (apply) and undrop (abstract) ------------------------------------------------------------------------ -- | Delayed dropping which allows undropping. data Drop a = Drop { dropN :: Int -- ^ Non-negative number of things to drop. , dropFrom :: a -- ^ Where to drop from. } deriving (Eq, Ord, Show, Typeable, Data, Functor, Foldable, Traversable) instance KillRange a => KillRange (Drop a) where killRange = fmap killRange -- | Things that support delayed dropping. class DoDrop a where doDrop :: Drop a -> a -- ^ Perform the dropping. dropMore :: Int -> Drop a -> Drop a -- ^ Drop more. dropMore n (Drop m xs) = Drop (m + n) xs unDrop :: Int -> Drop a -> Drop a -- ^ Pick up dropped stuff. unDrop n (Drop m xs) | n <= m = Drop (m - n) xs | otherwise = __IMPOSSIBLE__ instance DoDrop [a] where doDrop (Drop m xs) = List.drop m xs instance DoDrop Permutation where doDrop (Drop k (Perm n xs)) = Perm (n + m) $ [0..m-1] ++ map (+ m) (List.drop k xs) where m = -k unDrop m = dropMore (-m) -- allow picking up more than dropped Agda-2.5.3/src/full/Agda/Utils/Char.hs0000644000000000000000000000340113154613124015437 0ustar0000000000000000module Agda.Utils.Char where import Data.Char -- | Convert a character in @'0'..'9'@ into the corresponding digit @0..9@. decDigit :: Char -> Int decDigit c = ord c - ord '0' -- | Convert a character in @'0'..'9','A'..'F','a'..'f'@ -- into the corresponding digit @0..15@. hexDigit :: Char -> Int hexDigit c | isDigit c = decDigit c | otherwise = ord (toLower c) - ord 'a' + 10 -- | Convert a character in @'0'..'7'@ into the corresponding digit @0..7@. octDigit :: Char -> Int octDigit = decDigit ------------------------------------------------------------------------ -- * Unicode exploration ------------------------------------------------------------------------ -- | Unicode characters are divided into letters, numbers, marks, -- punctuation, symbols, separators (including spaces) and others -- (including control characters). -- -- These are the tests that 'Data.Char' offers: data UnicodeTest = IsControl | IsSpace | IsLower | IsUpper | IsAlpha | IsAlphaNum | IsPrint | IsDigit | IsOctDigit | IsHexDigit | IsLetter | IsMark | IsNumber | IsPunctuation | IsSymbol | IsSeparator deriving (Eq, Ord, Show) -- | Test names paired with their implementation. unicodeTests :: [(UnicodeTest, Char -> Bool)] unicodeTests = [ (IsControl, isControl), (IsSpace, isSpace) , (IsLower, isLower), (IsUpper, isUpper), (IsAlpha, isAlpha) , (IsAlphaNum, isAlphaNum) , (IsPrint, isPrint) , (IsDigit, isDigit), (IsOctDigit, isOctDigit), (IsHexDigit, isHexDigit) , (IsLetter, isLetter), (IsMark, isMark) , (IsNumber, isNumber), (IsPunctuation, isPunctuation), (IsSymbol, isSymbol) , (IsSeparator, isSeparator) ] -- | Find out which tests a character satisfies. testChar :: Char -> [UnicodeTest] testChar c = map fst $ filter (($ c) . snd) unicodeTests Agda-2.5.3/src/full/Agda/Utils/Graph/0000755000000000000000000000000013154613124015271 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Utils/Graph/AdjacencyMap/0000755000000000000000000000000013154613124017610 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Utils/Graph/AdjacencyMap/Unidirectional.hs0000644000000000000000000005624413154613124023130 0ustar0000000000000000{-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} -- | Directed graphs (can of course simulate undirected graphs). -- -- Represented as adjacency maps in direction from source to target. -- -- Each source node maps to a adjacency map of outgoing edges, -- which is a map from target nodes to edges. -- -- This allows to get outgoing edges in O(log n) time where -- @n@ is the number of nodes in the graph. -- -- However, the set of incoming edges can only be obtained in -- @O(n log n)@ or @O(e)@ where @e@ is the total number of edges. module Agda.Utils.Graph.AdjacencyMap.Unidirectional ( Graph(..) , Edge(..) , transposeEdge , edges , edgesFrom , edgesTo , diagonal , lookup , neighbours, neighboursMap , sourceNodes, targetNodes , Nodes(..) , computeNodes, nodes , fromNodes , fromList, fromListWith , toList , discrete , clean , empty , singleton , insert, insertWith , insertEdge, insertEdgeWith , union , unionWith , unions, unionsWith , removeNode , removeEdge , filterEdges , unzip , mapWithEdge , sccs' , sccs , DAG(..) , dagInvariant , oppositeDAG , reachable , sccDAG' , sccDAG , acyclic , reachableFrom , walkSatisfying , composeWith , complete , gaussJordanFloydWarshallMcNaughtonYamadaReference , gaussJordanFloydWarshallMcNaughtonYamada ) where import Prelude hiding (lookup, unzip, null) import Control.Applicative hiding (empty) import Control.Monad import qualified Data.Array.IArray as Array import qualified Data.Edison.Seq.BankersQueue as BQ import qualified Data.Edison.Seq.SimpleQueue as SQ import Data.Function import qualified Data.Graph as Graph import Data.IntMap.Strict (IntMap) import qualified Data.IntMap.Strict as IntMap import qualified Data.IntSet as IntSet import qualified Data.List as List import qualified Data.Map.Strict as Map import Data.Map.Strict (Map) import qualified Data.Maybe as Maybe import Data.Maybe (maybeToList, fromMaybe, catMaybes) import qualified Data.Set as Set import Data.Set (Set) import qualified Data.Tree as Tree import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.List (headMaybe) import Agda.Utils.Null (Null(null)) import qualified Agda.Utils.Null as Null import Agda.Utils.Pretty import Agda.Utils.SemiRing import Agda.Utils.Singleton (Singleton) import qualified Agda.Utils.Singleton as Singleton import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- | @Graph s t e@ is a directed graph with -- source nodes in @s@ -- target nodes in @t@ -- and edges in @e@. -- -- Admits at most one edge between any two nodes. -- Several edges can be modeled by using a collection type for @e@. -- -- Represented as "adjacency list", or rather, adjacency map. -- This allows to get all outgoing edges for a node -- in @O(log n)@ time where @n@ is the number of nodes of the graph. -- -- Incoming edges can only be computed in @O(n + e)@ time where -- @e@ is the number of edges. newtype Graph s t e = Graph { graph :: Map s (Map t e) -- ^ Forward edges. } deriving (Eq, Functor, Show) instance (Pretty s, Pretty t, Pretty e) => Pretty (Graph s t e) where pretty = vcat . map pretty . edges data Edge s t e = Edge { source :: s -- ^ Outgoing node. , target :: t -- ^ Incoming node. , label :: e -- ^ Edge label (weight). } deriving (Eq, Ord, Functor, Show) instance (Pretty s, Pretty t, Pretty e) => Pretty (Edge s t e) where pretty (Edge s t e) = pretty s <+> text "--(" <> pretty t <> text ")-->" <+> pretty t -- | Reverse an edge. transposeEdge :: Edge s t e -> Edge t s e transposeEdge (Edge s t e) = Edge t s e -- * Edge queries -- | Turn a graph into a list of edges. @O(n + e)@ edges :: Graph s t e -> [Edge s t e] edges (Graph g) = [ Edge s t e | (s, tes) <- Map.assocs g , (t, e) <- Map.assocs tes ] -- | All edges originating in the given nodes. -- (I.e., all outgoing edges for the given nodes.) -- -- Roughly linear in the length of the result list @O(result)@. edgesFrom :: Ord s => Graph s t e -> [s] -> [Edge s t e] edgesFrom (Graph g) ss = [ Edge s t e | s <- ss , m <- maybeToList $ Map.lookup s g , (t, e) <- Map.assocs m ] -- | All edges ending in the given nodes. -- (I.e., all incoming edges for the given nodes.) -- -- Expensive: @O(n * |ts| * log n)@. edgesTo :: Ord t => Graph s t e -> [t] -> [Edge s t e] edgesTo (Graph g) ts = [ Edge s t e | (s, m) <- Map.assocs g , t <- ts , e <- maybeToList $ Map.lookup t m ] -- | Get all self-loops. diagonal :: (Ord n) => Graph n n e -> [Edge n n e] diagonal (Graph g) = [ Edge s s e | (s, m) <- Map.assocs g , e <- maybeToList $ Map.lookup s m ] -- | Lookup label of an edge. lookup :: (Ord s, Ord t) => s -> t -> Graph s t e -> Maybe e lookup s t (Graph g) = Map.lookup t =<< Map.lookup s g -- | Get a list of outgoing edges with target. neighbours :: Ord s => s -> Graph s t e -> [(t, e)] neighbours s (Graph g) = maybe [] Map.assocs $ Map.lookup s g -- | Get a list of outgoing edges with target. neighboursMap :: Ord s => s -> Graph s t e -> Map t e neighboursMap s (Graph g) = fromMaybe Map.empty $ Map.lookup s g -- * Node queries -- | Returns all the nodes with outgoing edges. @O(n)@. sourceNodes :: Graph s t e -> Set s sourceNodes = Map.keysSet . graph -- | Returns all the nodes with incoming edges. Expensive! @O(e)@. targetNodes :: Ord t => Graph s t e -> Set t targetNodes = Set.fromList . map target . edges -- | For homogeneous graphs, @(s = t)@ we can compute a set -- of all nodes. -- -- Structure @Nodes@ is for computing all nodes but also -- remembering which were incoming and which outgoing. -- This is mostly for efficiency reasons, to avoid recomputation -- when all three sets are needed. data Nodes n = Nodes { srcNodes :: Set n , tgtNodes :: Set n , allNodes :: Set n } computeNodes :: (Ord n) => Graph n n e -> Nodes n computeNodes g = Nodes srcs tgts (srcs `Set.union` tgts) where srcs = sourceNodes g tgts = targetNodes g -- | The set of all nodes (outgoing and incoming). nodes :: (Ord n) => Graph n n e -> Set n nodes = allNodes . computeNodes -- * Graph construction. -- | Constructs a completely disconnected graph containing the given -- nodes. @O(n)@. fromNodes :: Ord n => [n] -> Graph n n e fromNodes ns = Graph $ Map.fromList $ map (, Map.empty) ns -- | Constructs a graph from a list of edges. O(e log n) -- -- Later edges overwrite earlier edges. fromList :: (Ord s, Ord t) => [Edge s t e] -> Graph s t e fromList = fromListWith $ \ new old -> new -- | Constructs a graph from a list of edges. O(e log n) -- -- Later edges are combined with earlier edges using the supplied function. fromListWith :: (Ord s, Ord t) => (e -> e -> e) -> [Edge s t e] -> Graph s t e fromListWith f = List.foldl' (flip (insertEdgeWith f)) empty -- | Convert a graph into a list of edges. O(e) toList :: Graph s t e -> [Edge s t e] toList (Graph g) = [ Edge s t a | (s,m) <- Map.assocs g, (t,a) <- Map.assocs m ] -- | Check whether the graph is discrete (no edges). -- This could be seen as an empty graph. -- Worst-case (is discrete): @O(e)@. discrete :: Null e => Graph s t e -> Bool discrete = all' (all' null) . graph where all' p = List.all p . Map.elems -- | Removes 'Null' edges (and empty 'Map's). clean :: Null e => Graph s t e -> Graph s t e clean = Graph . filt . fmap filt . graph where filt :: Null a => Map k a -> Map k a filt = Map.filter (not . null) -- | Empty graph (no nodes, no edges). empty :: Graph s t e empty = Graph Map.empty -- | A graph with two nodes and a single connecting edge. singleton :: s -> t -> e -> Graph s t e singleton s t e = Graph $ Map.singleton s (Map.singleton t e) -- | Insert an edge into the graph. insert :: (Ord s, Ord t) => s -> t -> e -> Graph s t e -> Graph s t e insert = insertWith $ \ new old -> new insertEdge :: (Ord s, Ord t) => Edge s t e -> Graph s t e -> Graph s t e insertEdge (Edge s t e) = insert s t e -- | Insert an edge, possibly combining @old@ edge weight with @new@ weight by -- given function @f@ into @f new old@. insertWith :: (Ord s, Ord t) => (e -> e -> e) -> s -> t -> e -> Graph s t e -> Graph s t e insertWith f s t e (Graph g) = Graph (Map.alter (Just . ins) s g) where ins Nothing = Map.singleton t e ins (Just m) = Map.insertWith f t e m insertEdgeWith :: (Ord s, Ord t) => (e -> e -> e) -> Edge s t e -> Graph s t e -> Graph s t e insertEdgeWith f (Edge s t e) = insertWith f s t e -- | Left-biased union. union :: (Ord s, Ord t) => Graph s t e -> Graph s t e -> Graph s t e union = unionWith $ \ left right -> left unionWith :: (Ord s, Ord t) => (e -> e -> e) -> Graph s t e -> Graph s t e -> Graph s t e unionWith f (Graph g) (Graph g') = Graph $ Map.unionWith (Map.unionWith f) g g' unions ::(Ord s, Ord t) => [Graph s t e] -> Graph s t e unions = unionsWith $ \ left right -> left unionsWith :: (Ord s, Ord t) => (e -> e -> e) -> [Graph s t e] -> Graph s t e unionsWith f = List.foldl' (unionWith f) empty -- * Graph reversal -- | The opposite graph (with all edges reversed). transpose :: (Ord s, Ord t) => Graph s t e -> Graph t s e transpose = fromList . map transposeEdge . edges -- * Graph deconstruction. -- | Auxiliary function to turn empty map into @Nothing@. discardEmpty :: Map k v -> Maybe (Map k v) discardEmpty m = if Map.null m then Nothing else Just m -- | Removes the given source node, and all corresponding edges, from the graph. -- -- O(log n). removeSourceNode :: Ord s => s -> Graph s t e -> Graph s t e removeSourceNode s (Graph g) = Graph $ Map.delete s g -- | Removes the given target node, and all corresponding edges, from the graph. -- -- Expensive! @O(n log n)@. removeTargetNode :: Ord t => t -> Graph s t e -> Graph s t e removeTargetNode t (Graph g) = Graph $ Map.mapMaybe rem g where rem = discardEmpty . Map.delete t -- | Removes the given node, be it source or target, -- and all corresponding edges, from the graph. -- -- Expensive! @O(n log n)@. removeNode :: Ord n => n -> Graph n n e -> Graph n n e removeNode n = removeTargetNode n . removeSourceNode n -- | @removeEdge s t g@ removes the edge going from @s@ to @t@, if any. -- -- @O((log n)^2)@. removeEdge :: (Ord s, Ord t) => s -> t -> Graph s t e -> Graph s t e removeEdge s t (Graph g) = Graph $ Map.adjust (Map.delete t) s g -- | Keep only the edges that satisfy the predicate. @O(e).@ filterEdges :: (e -> Bool) -> Graph s t e -> Graph s t e filterEdges f (Graph g) = Graph $ Map.mapMaybe (discardEmpty . Map.filter f) g -- | Unzipping a graph (naive implementation using fmap). unzip :: Graph s t (e, e') -> (Graph s t e, Graph s t e') unzip g = (fst <$> g, snd <$> g) -- | Maps over a graph under availability of positional information, -- like 'Map.mapWithKey'. mapWithEdge :: (Edge s t e -> e') -> Graph s t e -> Graph s t e' mapWithEdge f (Graph g) = Graph $ flip Map.mapWithKey g $ \ s m -> flip Map.mapWithKey m $ \ t e -> f (Edge s t e) -- * Strongly connected components. -- | The graph's strongly connected components, in reverse topological -- order. sccs' :: Ord n => Graph n n e -> [Graph.SCC n] sccs' g = Graph.stronglyConnComp [ (n, n, map target (edgesFrom g [n])) | n <- Set.toList (nodes g) ] -- | The graph's strongly connected components, in reverse topological -- order. sccs :: Ord n => Graph n n e -> [[n]] sccs = map Graph.flattenSCC . sccs' -- | SCC DAGs. -- -- The maps map SCC indices to and from SCCs/nodes. data DAG n = DAG { dagGraph :: Graph.Graph , dagComponentMap :: IntMap (Graph.SCC n) , dagNodeMap :: Map n Int } -- | 'DAG' invariant. dagInvariant :: Ord n => DAG n -> Bool dagInvariant g = Set.fromList (concatMap Graph.flattenSCC (IntMap.elems (dagComponentMap g))) == Map.keysSet (dagNodeMap g) && IntSet.fromList (Map.elems (dagNodeMap g)) == IntMap.keysSet (dagComponentMap g) && and [ n `elem` Graph.flattenSCC (dagComponentMap g IntMap.! (dagNodeMap g Map.! n)) | n <- Map.keys (dagNodeMap g) ] && and [ dagNodeMap g Map.! n == i | i <- Graph.vertices (dagGraph g) , n <- Graph.flattenSCC (dagComponentMap g IntMap.! i) ] && IntSet.fromList (Graph.vertices (dagGraph g)) == IntMap.keysSet (dagComponentMap g) && all isAcyclic (Graph.scc (dagGraph g)) where isAcyclic (Tree.Node r []) = not (r `elem` (dagGraph g Array.! r)) isAcyclic _ = False -- | The opposite DAG. oppositeDAG :: DAG n -> DAG n oppositeDAG g = g { dagGraph = Graph.transposeG (dagGraph g) } -- | The nodes reachable from the given SCC. reachable :: Ord n => DAG n -> Graph.SCC n -> [n] reachable g scc = case scc of Graph.AcyclicSCC n -> List.delete n (reachable' n) Graph.CyclicSCC (n : _) -> reachable' n Graph.CyclicSCC [] -> __IMPOSSIBLE__ where lookup' g k = case IntMap.lookup k g of Nothing -> __IMPOSSIBLE__ Just x -> x lookup'' g k = case Map.lookup k g of Nothing -> __IMPOSSIBLE__ Just x -> x reachable' n = concatMap (Graph.flattenSCC . lookup' (dagComponentMap g)) $ Graph.reachable (dagGraph g) (lookup'' (dagNodeMap g) n) -- | Constructs a DAG containing the graph's strongly connected -- components. sccDAG' :: forall n e. Ord n => Graph n n e -> [Graph.SCC n] -- ^ The graph's strongly connected components. -> DAG n sccDAG' g sccs = DAG theDAG componentMap secondNodeMap where components :: [(Int, Graph.SCC n)] components = zip [1..] sccs firstNodeMap :: Map n Int firstNodeMap = Map.fromList [ (n, i) | (i, c) <- components , n <- Graph.flattenSCC c ] targets :: Int -> [n] -> [Int] targets i ns = IntSet.toList $ IntSet.fromList [ j | e <- edgesFrom g ns , let j = case Map.lookup (target e) firstNodeMap of Nothing -> __IMPOSSIBLE__ Just j -> j , j /= i ] (theDAG, _, toVertex) = Graph.graphFromEdges [ (i, i, targets i (Graph.flattenSCC c)) | (i, c) <- components ] convertInt :: Int -> Graph.Vertex convertInt i = case toVertex i of Nothing -> __IMPOSSIBLE__ Just i -> i componentMap :: IntMap (Graph.SCC n) componentMap = IntMap.fromList (map (mapFst convertInt) components) secondNodeMap :: Map n Int secondNodeMap = fmap convertInt firstNodeMap -- | Constructs a DAG containing the graph's strongly connected -- components. sccDAG :: Ord n => Graph n n e -> DAG n sccDAG g = sccDAG' g (sccs' g) -- | Returns @True@ iff the graph is acyclic. acyclic :: Ord n => Graph n n e -> Bool acyclic = all isAcyclic . sccs' where isAcyclic Graph.AcyclicSCC{} = True isAcyclic Graph.CyclicSCC{} = False -- | @reachableFrom g n@ is a map containing all nodes reachable from -- @n@ in @g@. For each node a simple path to the node is given, along -- with its length (the number of edges). The paths are as short as -- possible (in terms of the number of edges). -- -- Precondition: @n@ must be a node in @g@. The number of nodes in the -- graph must not be larger than @'maxBound' :: 'Int'@. -- -- Amortised time complexity (assuming that comparisons take constant -- time): /O(e log n)/, if the lists are not inspected. Inspection of -- a prefix of a list is linear in the length of the prefix. reachableFrom :: Ord n => Graph n n e -> n -> Map n (Int, [Edge n n e]) reachableFrom g n = bfs (SQ.singleton (n, BQ.empty)) Map.empty where bfs !q !map = case SQ.lview q of Nothing -> map Just ((u, p), q) -> if u `Map.member` map then bfs q map else bfs (foldr SQ.rcons q [ (v, BQ.rcons (Edge u v e) p) | (v, e) <- neighbours u g ]) (let n = BQ.size p in n `seq` Map.insert u (n, BQ.toList p) map) -- | @walkSatisfying every some g from to@ determines if there is a -- walk from @from@ to @to@ in @g@, in which every edge satisfies the -- predicate @every@, and some edge satisfies the predicate @some@. If -- there are several such walks, then a shortest one (in terms of the -- number of edges) is returned. -- -- Precondition: @from@ and @to@ must be nodes in @g@. The number of -- nodes in the graph must not be larger than @'maxBound' :: 'Int'@. -- -- Amortised time complexity (assuming that comparisons and the -- predicates take constant time to compute): /O(e log n)/. walkSatisfying :: Ord n => (e -> Bool) -> (e -> Bool) -> Graph n n e -> n -> n -> Maybe [Edge n n e] walkSatisfying every some g from to = case [ (l1 + l2, p1 ++ [e] ++ map transposeEdge (reverse p2)) | e <- everyEdges , some (label e) , (l1, p1) <- maybeToList (Map.lookup (source e) fromReaches) , (l2, p2) <- maybeToList (Map.lookup (target e) reachesTo) ] of [] -> Nothing ess -> Just $ snd $ List.minimumBy (compare `on` fst) ess where everyEdges = [ e | e <- toList g, every (label e) ] fromReaches = reachableFrom (fromList everyEdges) from reachesTo = reachableFrom (fromList (map transposeEdge everyEdges)) to -- * Graph composition -- | @composeWith times plus g g'@ finds all edges -- @s --c_i--> t_i --d_i--> u@ and constructs the -- result graph from @edge(s,u) = sum_i (c_i times d_i)@. -- -- Complexity: for each edge @s --> t@ in @g@ we lookup up -- all edges starting in with @t@ in @g'@. -- composeWith :: (Ord t, Ord u) => (c -> d -> e) -> (e -> e -> e) -> Graph s t c -> Graph t u d -> Graph s u e composeWith times plus (Graph g) (Graph g') = Graph $ Map.mapMaybe (discardEmpty . comp) g where comp m = Map.fromListWith plus [ (u, c `times` d) | (t, c) <- Map.assocs m , m' <- maybeToList (Map.lookup t g') , (u, d) <- Map.assocs m' ] -- | Transitive closure ported from "Agda.Termination.CallGraph". -- -- Relatively efficient, see Issue 1560. complete :: (Eq e, Null e, SemiRing e, Ord n) => Graph n n e -> Graph n n e complete g = repeatWhile (mapFst (not . discrete) . combineNewOld' g) g where combineNewOld' new old = unzip $ unionWith comb new' old' where -- The following procedure allows us to check if anything new happened: -- Pair the composed graphs with an empty graph. -- The empty graph will remain empty. We only need it due to the typing -- of Map.unionWith. new' = (,Null.empty) <$> composeWith otimes oplus new old -- Pair an empty graph with the old graph. old' = (Null.empty,) <$> old -- Combine the pairs. -- Update 'old' with 'new'. This will be the new 'old'. No new 'new' if no change. comb (new, _) (_, old) = (if x == old then Null.empty else x, x) where x = old `oplus` new -- | Version of 'complete' that produces a list of intermediate results -- paired to the left with a difference that lead to the new intermediat result. -- -- The last element in the list is the transitive closure, paired with the empty graph. -- -- @complete g = snd $ last $ completeIter g@ completeIter :: (Eq e, Null e, SemiRing e, Ord n) => Graph n n e -> [(Graph n n e, Graph n n e)] completeIter g = iterWhile (not . discrete) (combineNewOld' g) g where combineNewOld' new old = unzip $ unionWith comb new' old' where -- The following procedure allows us to check if anything new happened: -- Pair the composed graphs with an empty graph. -- The empty graph will remain empty. We only need it due to the typing -- of Map.unionWith. new' = (,Null.empty) <$> composeWith otimes oplus new old -- Pair an empty graph with the old graph. old' = (Null.empty,) <$> old -- Combine the pairs. -- Update 'old' with 'new'. This will be the new 'old'. No new 'new' if no change. comb (new, _) (_, old) = (if x == old then Null.empty else x, x) where x = old `oplus` new -- | Computes the transitive closure of the graph. -- -- Uses the Gauss-Jordan-Floyd-Warshall-McNaughton-Yamada algorithm -- (as described by Russell O'Connor in \"A Very General Method of -- Computing Shortest Paths\" -- ), implemented using -- matrices. -- -- The resulting graph does not contain any zero edges. -- -- This algorithm should be seen as a reference implementation. In -- practice 'gaussJordanFloydWarshallMcNaughtonYamada' is likely to be -- more efficient. gaussJordanFloydWarshallMcNaughtonYamadaReference :: forall n e. (Ord n, Eq e, StarSemiRing e) => Graph n n e -> Graph n n e gaussJordanFloydWarshallMcNaughtonYamadaReference g = toGraph (foldr step initialMatrix nodeIndices) where indicesAndNodes = zip [1..] $ Set.toList $ nodes g nodeMap = Map.fromList $ map swap indicesAndNodes indexMap = Map.fromList indicesAndNodes noNodes = Map.size nodeMap nodeIndices = [1 .. noNodes] matrixBounds = ((1, 1), (noNodes, noNodes)) initialMatrix :: Array.Array (Int, Int) e initialMatrix = Array.accumArray oplus ozero matrixBounds [ ((nodeMap Map.! source e, nodeMap Map.! target e), label e) | e <- edges g ] rightStrictPair i !e = (i , e) step k !m = Array.array matrixBounds [ rightStrictPair (i, j) (oplus (m Array.! (i, j)) (otimes (m Array.! (i, k)) (otimes (ostar (m Array.! (k, k))) (m Array.! (k, j))))) | i <- nodeIndices, j <- nodeIndices ] toGraph m = fromList [ Edge (indexMap Map.! i) (indexMap Map.! j) e | ((i, j), e) <- Array.assocs m , e /= ozero ] -- | Computes the transitive closure of the graph. -- -- Uses the Gauss-Jordan-Floyd-Warshall-McNaughton-Yamada algorithm -- (as described by Russell O'Connor in \"A Very General Method of -- Computing Shortest Paths\" -- ), implemented using -- 'Graph', and with some shortcuts: -- -- * Zero edge differences are not added to the graph, thus avoiding -- some zero edges. -- -- * Strongly connected components are used to avoid computing some -- zero edges. -- -- The graph's strongly connected components (in reverse topological -- order) are returned along with the transitive closure. gaussJordanFloydWarshallMcNaughtonYamada :: forall n e. (Ord n, Eq e, StarSemiRing e) => Graph n n e -> (Graph n n e, [Graph.SCC n]) gaussJordanFloydWarshallMcNaughtonYamada g = (loop components g, components) where components = sccs' g forwardDAG = sccDAG' g components reverseDAG = oppositeDAG forwardDAG loop :: [Graph.SCC n] -> Graph n n e -> Graph n n e loop [] !g = g loop (scc : sccs) g = loop sccs (foldr step g (Graph.flattenSCC scc)) where -- All nodes that are reachable from the SCC. canBeReached = reachable forwardDAG scc -- All nodes that can reach the SCC. canReach = reachable reverseDAG scc step :: n -> Graph n n e -> Graph n n e step k !g = foldr (insertEdgeWith oplus) g [ Edge i j e | i <- canReach , j <- canBeReached , let e = otimes (lookup' i k) (starTimes (lookup' k j)) , e /= ozero ] where starTimes = otimes (ostar (lookup' k k)) lookup' s t = case lookup s t g of Nothing -> ozero Just e -> e Agda-2.5.3/src/full/Agda/Utils/IO/0000755000000000000000000000000013154613124014537 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Utils/IO/UTF8.hs0000644000000000000000000000453413154613124015627 0ustar0000000000000000-- | Text IO using the UTF8 character encoding. module Agda.Utils.IO.UTF8 ( readTextFile , Agda.Utils.IO.UTF8.hPutStr , Agda.Utils.IO.UTF8.writeFile , writeTextToFile ) where import Data.Text.Lazy (Text) import qualified Data.Text.Lazy.IO as T import qualified System.IO as IO import Control.Applicative -- | Converts many character sequences which may be interpreted as -- line or paragraph separators into '\n'. convertLineEndings :: String -> String -- ASCII: convertLineEndings ('\x000D' : '\x000A' : s) = '\n' : convertLineEndings s -- CR LF convertLineEndings ('\x000A' : s) = '\n' : convertLineEndings s -- LF (Line feed) convertLineEndings ('\x000D' : s) = '\n' : convertLineEndings s -- CR (Carriage return) convertLineEndings ('\x000C' : s) = '\n' : convertLineEndings s -- FF (Form feed) -- Unicode: convertLineEndings ('\x0085' : s) = '\n' : convertLineEndings s -- NEXT LINE convertLineEndings ('\x2028' : s) = '\n' : convertLineEndings s -- LINE SEPARATOR convertLineEndings ('\x2029' : s) = '\n' : convertLineEndings s -- PARAGRAPH SEPARATOR -- Not a line ending: convertLineEndings (c : s) = c : convertLineEndings s convertLineEndings "" = "" -- | Reads a UTF8-encoded text file and converts all Unicode line -- endings into '\n'. readTextFile :: FilePath -> IO String readTextFile file = convertLineEndings <$> do h <- IO.openFile file IO.ReadMode IO.hSetNewlineMode h IO.noNewlineTranslation IO.hSetEncoding h IO.utf8 IO.hGetContents h -- | Writes UTF8-encoded text to the handle, which should be opened -- for writing and in text mode. The native convention for line -- endings is used. -- -- The handle's text encoding is not necessarily preserved, it is -- changed to UTF8. hPutStr :: IO.Handle -> String -> IO () hPutStr h s = do IO.hSetEncoding h IO.utf8 IO.hPutStr h s -- | Writes a UTF8-encoded text file. The native convention for line -- endings is used. writeFile :: FilePath -> String -> IO () writeFile file s = IO.withFile file IO.WriteMode $ \h -> do hPutStr h s -- | Writes a UTF8-encoded text file. The native convention for line -- endings is used. writeTextToFile :: FilePath -> Text -> IO () writeTextToFile file s = IO.withFile file IO.WriteMode $ \h -> do IO.hSetEncoding h IO.utf8 T.hPutStr h s Agda-2.5.3/src/full/Agda/Utils/IO/Directory.hs0000644000000000000000000000403213154613124017036 0ustar0000000000000000module Agda.Utils.IO.Directory ( copyDirContent ) where import Control.Monad import Control.Monad.Writer import System.Directory import System.FilePath import Data.ByteString as BS import Paths_Agda import Agda.Utils.Functor -- | @copyDirContent src dest@ recursively copies directory @src@ onto @dest@. -- -- First, a to-do list of copy actions is created. -- Then, the to-do list is carried out. -- -- This avoids copying files we have just created again, which can happen -- if @src@ and @dest@ are not disjoint. -- (See issue #2705.) -- copyDirContent :: FilePath -> FilePath -> IO () copyDirContent src dest = mapM_ performAction =<< do (`appEndo` []) <$> execWriterT (copyDirContentDryRun src dest) -- | Action to be carried out for copying a directory recursively. -- data CopyDirAction = MkDir FilePath -- ^ Create directory if missing. | CopyFile FilePath FilePath -- ^ Copy file if changed. -- | Perform scheduled 'CopyDirAction'. -- performAction :: CopyDirAction -> IO () performAction = \case MkDir d -> createDirectoryIfMissing True d CopyFile src dest -> copyIfChanged src dest -- | @copyDirContentDryRun src dest@ creates a to-do list -- for recursively copying directory @src@ onto @dest@. -- copyDirContentDryRun :: FilePath -> FilePath -> WriterT (Endo [CopyDirAction]) IO () copyDirContentDryRun src dest = do tell $ Endo (MkDir dest :) chlds <- lift $ getDirectoryContents src forM_ chlds $ \ x -> do isDir <- lift $ doesDirectoryExist (src x) case isDir of _ | x == "." || x == ".." -> return () True -> copyDirContentDryRun (src x) (dest x) False -> tell $ Endo (CopyFile (src x) (dest x) :) -- | @copyIfChanged src dst@ makes sure that @dst@ exists -- and has the same content as @dst@. -- copyIfChanged :: FilePath -> FilePath -> IO () copyIfChanged src dst = do exist <- doesFileExist dst if not exist then copyFile src dst else do new <- BS.readFile src old <- BS.readFile dst unless (old == new) $ copyFile src dst Agda-2.5.3/src/full/Agda/Utils/IO/Binary.hs0000644000000000000000000000056213154613124016322 0ustar0000000000000000-- | Binary IO. module Agda.Utils.IO.Binary ( readBinaryFile' ) where import System.IO import Data.ByteString.Lazy as BS -- | Returns a close function for the file together with the contents. readBinaryFile' :: FilePath -> IO (ByteString, IO ()) readBinaryFile' file = do h <- openBinaryFile file ReadMode s <- BS.hGetContents h return (s, hClose h) Agda-2.5.3/src/full/Agda/Utils/Haskell/0000755000000000000000000000000013154613124015613 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Utils/Haskell/Syntax.hs0000644000000000000000000000455713154613124017450 0ustar0000000000000000-- | ASTs for subset of GHC Haskell syntax. module Agda.Utils.Haskell.Syntax where -- * Modules data Module = Module ModuleName [ModulePragma] [ImportDecl] [Decl] data ModulePragma = LanguagePragma [Name] | OtherPragma String -- ^ Unstructured pragma (Andreas, 2017-08-23, issue #2712). data ImportDecl = ImportDecl { importModule :: ModuleName , importQualified :: Bool , importSpecs :: Maybe (Bool, [ImportSpec]) } data ImportSpec = IVar Name -- * Declarations data Decl = TypeDecl Name [TyVarBind] Type | DataDecl DataOrNew Name [TyVarBind] [ConDecl] [Deriving] | TypeSig [Name] Type | FunBind [Match] | PatSyn Pat Pat | FakeDecl String deriving (Eq) data DataOrNew = DataType | NewType deriving (Eq) data ConDecl = ConDecl Name [Type] deriving (Eq) type Deriving = (QName, [Type]) data Binds = BDecls [Decl] deriving (Eq) data Rhs = UnGuardedRhs Exp | GuardedRhss [GuardedRhs] deriving (Eq) data GuardedRhs = GuardedRhs [Stmt] Exp deriving (Eq) data Match = Match Name [Pat] Rhs (Maybe Binds) deriving (Eq) -- * Expressions data Type = TyForall [TyVarBind] Type | TyFun Type Type | TyCon QName | TyVar Name | TyApp Type Type | FakeType String deriving (Eq) data Pat = PVar Name | PLit Literal | PAsPat Name Pat | PWildCard | PBangPat Pat | PApp QName [Pat] | PatTypeSig Pat Type | PIrrPat Pat deriving (Eq) data Stmt = Qualifier Exp | Generator Pat Exp deriving (Eq) data Exp = Var QName | Con QName | Lit Literal | InfixApp Exp QOp Exp | App Exp Exp | Lambda [Pat] Exp | Let Binds Exp | If Exp Exp Exp | Case Exp [Alt] | ExpTypeSig Exp Type | NegApp Exp | FakeExp String deriving (Eq) data Alt = Alt Pat Rhs (Maybe Binds) deriving (Eq) data Literal = Int Integer | Frac Rational | Char Char | String String deriving (Eq) -- * Names data ModuleName = ModuleName String deriving (Eq, Ord) data QName = Qual ModuleName Name | UnQual Name deriving (Eq) data Name = Ident String | Symbol String deriving (Eq) data QOp = QVarOp QName deriving (Eq) data TyVarBind = UnkindedVar Name deriving (Eq) unit_con :: Exp unit_con = Con (UnQual (Ident "()")) Agda-2.5.3/src/full/Agda/Utils/Parser/0000755000000000000000000000000013154613124015464 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Utils/Parser/MemoisedCPS.hs0000644000000000000000000002456313154613124020142 0ustar0000000000000000------------------------------------------------------------------------ -- | Parser combinators with support for left recursion, following -- Johnson\'s \"Memoization in Top-Down Parsing\". -- -- This implementation is based on an implementation due to Atkey -- (attached to an edlambda-members mailing list message from -- 2011-02-15 titled \'Slides for \"Introduction to Parser -- Combinators\"\'). -- -- Note that non-memoised left recursion is not guaranteed to work. -- -- The code contains an important deviation from Johnson\'s paper: the -- check for subsumed results is not included. This means that one can -- get the same result multiple times when parsing using ambiguous -- grammars. As an example, parsing the empty string using @S ∷= ε | -- ε@ succeeds twice. This change also means that parsing fails to -- terminate for some cyclic grammars that would otherwise be handled -- successfully, such as @S ∷= S | ε@. However, the library is not -- intended to handle infinitely ambiguous grammars. (It is unclear to -- the author of this module whether the change leads to more -- non-termination for grammars that are not cyclic.) {-# LANGUAGE CPP #-} module Agda.Utils.Parser.MemoisedCPS ( ParserClass(..) , sat, token, tok, doc , DocP, bindP, choiceP, seqP, starP, atomP , Parser , ParserWithGrammar ) where import Control.Applicative import Control.Monad (ap, liftM2) import Control.Monad.State.Strict (State, evalState, runState, get, put, modify') import Data.Array import Data.Hashable import qualified Data.HashMap.Strict as Map import Data.HashMap.Strict (HashMap) import qualified Data.HashSet as Set import Data.HashSet (HashSet) import qualified Data.IntMap.Strict as IntMap import Data.IntMap.Strict (IntMap) import qualified Data.List as List import Data.Maybe import Text.PrettyPrint.HughesPJ hiding (empty) import qualified Text.PrettyPrint.HughesPJ as PP import Agda.Utils.Pretty ( mparens ) #include "undefined.h" import Agda.Utils.Impossible -- | Positions. type Pos = Int -- | State monad used by the parser. type M k r tok b = State (IntMap (HashMap k (Value k r tok b))) -- | Continuations. type Cont k r tok b a = Pos -> a -> M k r tok b [b] -- | Memoised values. data Value k r tok b = Value { results :: !(IntMap [r]) , continuations :: [Cont k r tok b r] } -- | The parser type. -- -- The parameters of the type @Parser k r tok a@ have the following -- meanings: -- -- [@k@] Type used for memoisation keys. -- -- [@r@] The type of memoised values. (Yes, all memoised values have -- to have the same type.) -- -- [@tok@] The token type. -- -- [@a@] The result type. newtype Parser k r tok a = P { unP :: forall b. Array Pos tok -> Pos -> Cont k r tok b a -> M k r tok b [b] } instance Monad (Parser k r tok) where return = pure P p >>= f = P $ \input i k -> p input i $ \j x -> unP (f x) input j k instance Functor (Parser k r tok) where fmap f (P p) = P $ \input i k -> p input i $ \i -> k i . f instance Applicative (Parser k r tok) where pure x = P $ \_ i k -> k i x P p1 <*> P p2 = P $ \input i k -> p1 input i $ \i f -> p2 input i $ \i x -> k i (f x) instance Alternative (Parser k r tok) where empty = P $ \_ _ _ -> return [] P p1 <|> P p2 = P $ \input i k -> liftM2 (++) (p1 input i k) (p2 input i k) class (Functor p, Applicative p, Alternative p, Monad p) => ParserClass p k r tok | p -> k, p -> r, p -> tok where -- | Runs the parser. parse :: p a -> [tok] -> [a] -- | Tries to print the parser, or returns 'PP.empty', depending on -- the implementation. This function might not terminate. grammar :: Show k => p a -> Doc -- | Parses a token satisfying the given predicate. The computed -- value is returned. sat' :: (tok -> Maybe a) -> p a -- | Uses the given function to modify the printed representation -- (if any) of the given parser. annotate :: (DocP -> DocP) -> p a -> p a -- | Memoises the given parser. -- -- Every memoised parser must be annotated with a /unique/ key. -- (Parametrised parsers must use distinct keys for distinct -- inputs.) memoise :: (Eq k, Hashable k, Show k) => k -> p r -> p r -- | Memoises the given parser, but only if printing, not if -- parsing. -- -- Every memoised parser must be annotated with a /unique/ key. -- (Parametrised parsers must use distinct keys for distinct -- inputs.) memoiseIfPrinting :: (Eq k, Hashable k, Show k) => k -> p r -> p r -- | Uses the given document as the printed representation of the -- given parser. The document's precedence is taken to be 'atomP'. doc :: ParserClass p k r tok => Doc -> p a -> p a doc d = annotate (\_ -> (d, atomP)) -- | Parses a token satisfying the given predicate. sat :: ParserClass p k r tok => (tok -> Bool) -> p tok sat p = sat' (\t -> if p t then Just t else Nothing) -- | Parses a single token. token :: ParserClass p k r tok => p tok token = doc (text "·") (sat' Just) -- | Parses a given token. tok :: (ParserClass p k r tok, Eq tok, Show tok) => tok -> p tok tok t = doc (text (show t)) (sat (t ==)) instance ParserClass (Parser k r tok) k r tok where parse p toks = flip evalState IntMap.empty $ unP p (listArray (0, n - 1) toks) 0 $ \j x -> if j == n then return [x] else return [] where n = List.genericLength toks grammar _ = PP.empty sat' p = P $ \input i k -> if inRange (bounds input) i then case p (input ! i) of Nothing -> return [] Just x -> (k $! (i + 1)) $! x else return [] annotate _ p = p memoiseIfPrinting _ p = p memoise key p = P $ \input i k -> do let alter j zero f m = IntMap.alter (Just . f . fromMaybe zero) j m lookupTable = fmap (\m -> Map.lookup key =<< IntMap.lookup i m) get insertTable v = modify' $ alter i Map.empty (Map.insert key v) v <- lookupTable case v of Nothing -> do insertTable (Value IntMap.empty [k]) unP p input i $ \j r -> do Just (Value rs ks) <- lookupTable insertTable (Value (alter j [] (r :) rs) ks) concat <$> mapM (\k -> k j r) ks -- See note [Reverse ks?]. Just (Value rs ks) -> do insertTable (Value rs (k : ks)) concat . concat <$> mapM (\(i, rs) -> mapM (k i) rs) (IntMap.toList rs) -- [Reverse ks?] -- -- If ks were reversed, then the code would be productive for some -- infinitely ambiguous grammars, including S ∷= S | ε. However, in -- some cases the results would not be fair (some valid results would -- never be returned). -- | An extended parser type, with some support for printing parsers. data ParserWithGrammar k r tok a = PG (Bool -> Either (Parser k r tok a) (Docs k)) -- ^ Invariant: If the boolean is 'True', then the result must be -- @'Left' something@, and if the boolean is 'False', then the -- result must be @'Right' something@. -- | Documents paired with precedence levels. type DocP = (Doc, Int) -- | Precedence of @>>=@. bindP :: Int bindP = 10 -- | Precedence of @<|>@. choiceP :: Int choiceP = 20 -- | Precedence of @<*>@. seqP :: Int seqP = 30 -- | Precedence of @⋆@ and @+@. starP :: Int starP = 40 -- | Precedence of atoms. atomP :: Int atomP = 50 -- | The extended parser type computes one top-level document, plus -- one document per encountered memoisation key. -- -- 'Nothing' is used to mark that a given memoisation key has been -- seen, but that no corresponding document has yet been stored. type Docs k = State (HashMap k (Maybe DocP)) DocP -- | A smart constructor. pg :: Parser k r tok a -> Docs k -> ParserWithGrammar k r tok a pg p d = PG $ \b -> if b then Left p else Right d -- | Extracts the parser. parser :: ParserWithGrammar k r tok a -> Parser k r tok a parser (PG p) = either id __IMPOSSIBLE__ (p True) -- | Extracts the documents. docs :: ParserWithGrammar k r tok a -> Docs k docs (PG p) = either __IMPOSSIBLE__ id (p False) instance Monad (ParserWithGrammar k r tok) where return = pure p >>= f = pg (parser p >>= parser . f) ((\(d, p) -> (mparens (p < bindP) d <+> text ">>= ?", bindP)) <$> docs p) instance Functor (ParserWithGrammar k r tok) where fmap f p = pg (fmap f (parser p)) (docs p) instance Applicative (ParserWithGrammar k r tok) where pure x = pg (pure x) (return (text "ε", atomP)) p1 <*> p2 = pg (parser p1 <*> parser p2) (liftM2 (\(d1, p1) (d2, p2) -> (sep [ mparens (p1 < seqP) d1 , mparens (p2 < seqP) d2 ], seqP)) (docs p1) (docs p2)) -- | A helper function. starDocs :: String -> ParserWithGrammar k r tok a -> Docs k starDocs s p = (\(d, p) -> (mparens (p < starP) d <+> text s, starP)) <$> docs p instance Alternative (ParserWithGrammar k r tok) where empty = pg empty (return (text "∅", atomP)) p1 <|> p2 = pg (parser p1 <|> parser p2) (liftM2 (\(d1, p1) (d2, p2) -> (sep [ mparens (p1 < choiceP) d1 , text "|" , mparens (p2 < choiceP) d2 ], choiceP)) (docs p1) (docs p2)) many p = pg (many (parser p)) (starDocs "⋆" p) some p = pg (some (parser p)) (starDocs "+" p) -- | Pretty-prints a memoisation key. prettyKey :: Show k => k -> DocP prettyKey key = (text ("<" ++ show key ++ ">"), atomP) -- | A helper function. memoiseDocs :: (Eq k, Hashable k, Show k) => k -> ParserWithGrammar k r tok r -> Docs k memoiseDocs key p = do r <- Map.lookup key <$> get case r of Just _ -> return () Nothing -> do modify' (Map.insert key Nothing) d <- docs p modify' (Map.insert key (Just d)) return (prettyKey key) instance ParserClass (ParserWithGrammar k r tok) k r tok where parse p = parse (parser p) sat' p = pg (sat' p) (return (text "", atomP)) annotate f p = pg (parser p) (f <$> docs p) memoise key p = pg (memoise key (parser p)) (memoiseDocs key p) memoiseIfPrinting key p = pg (parser p) (memoiseDocs key p) grammar p = d $+$ nest 2 (foldr1 ($+$) $ text "where" : map (\(k, d) -> fst (prettyKey k) <+> text "∷=" <+> maybe __IMPOSSIBLE__ fst d) (Map.toList ds)) where ((d, _), ds) = runState (docs p) Map.empty Agda-2.5.3/src/full/Agda/Utils/Parser/ReadP.hs0000644000000000000000000004015013154613124017013 0ustar0000000000000000{-# LANGUAGE MagicHash #-} ----------------------------------------------------------------------------- -- | -- This is a modified version of the ReadP module from the standard libraries. -- The changes are: -- 1) ReadP is parameterised over the token type (hard-wired to 'Char' in the library). -- 2) Added the functions 'parse' and 'parse'' which run parsers. -- 3) Removed Read instances. -- Module : "Text.ParserCombinators.ReadP" -- Copyright : (c) The University of Glasgow 2002 -- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : provisional -- Portability : non-portable (local universal quantification) -- -- This is a library of parser combinators, originally written by Koen Claessen. -- It parses all alternatives in parallel, so it never keeps hold of -- the beginning of the input string, a common source of space leaks with -- other parsers. The '(+++)' choice combinator is genuinely commutative; -- it makes no difference which branch is \"shorter\". ----------------------------------------------------------------------------- module Agda.Utils.Parser.ReadP ( -- * The 'ReadP' type ReadP, -- :: * -> *; instance Functor, Monad, MonadPlus -- * Primitive operations get, -- :: ReadP Char look, -- :: ReadP String (+++), -- :: ReadP a -> ReadP a -> ReadP a (<++), -- :: ReadP a -> ReadP a -> ReadP a gather, -- :: ReadP a -> ReadP (String, a) -- * Parse parse, -- :: ReadP tok a -> [tok] -> [a] parse', -- :: ReadP tok a -> [tok] -> Either a [tok] -- * Other operations pfail, -- :: ReadP a satisfy, -- :: (Char -> Bool) -> ReadP Char char, -- :: Char -> ReadP Char string, -- :: String -> ReadP String munch, -- :: (Char -> Bool) -> ReadP String munch1, -- :: (Char -> Bool) -> ReadP String skipSpaces, -- :: ReadP () choice, -- :: [ReadP a] -> ReadP a count, -- :: Int -> ReadP a -> ReadP [a] between, -- :: ReadP open -> ReadP close -> ReadP a -> ReadP a option, -- :: a -> ReadP a -> ReadP a optional, -- :: ReadP a -> ReadP () many, -- :: ReadP a -> ReadP [a] many1, -- :: ReadP a -> ReadP [a] skipMany, -- :: ReadP a -> ReadP () skipMany1, -- :: ReadP a -> ReadP () sepBy, -- :: ReadP a -> ReadP sep -> ReadP [a] sepBy1, -- :: ReadP a -> ReadP sep -> ReadP [a] endBy, -- :: ReadP a -> ReadP sep -> ReadP [a] endBy1, -- :: ReadP a -> ReadP sep -> ReadP [a] chainr, -- :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a chainl, -- :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a chainl1, -- :: ReadP a -> ReadP (a -> a -> a) -> ReadP a chainr1, -- :: ReadP a -> ReadP (a -> a -> a) -> ReadP a manyTill, -- :: ReadP a -> ReadP end -> ReadP [a] -- * Properties -- $properties ) where import Control.Applicative (Applicative(..),Alternative(empty,(<|>))) import Control.Monad import GHC.Exts import Data.Char infixr 5 +++, <++ -- --------------------------------------------------------------------------- -- The P type -- is representation type -- should be kept abstract data P t a = Get (t -> P t a) | Look ([t] -> P t a) | Fail | Result a (P t a) | Final [(a,[t])] -- invariant: list is non-empty! deriving (Functor) -- Monad, MonadPlus instance Applicative (P t) where pure x = Result x Fail (<*>) = ap instance Monad (P t) where return = pure (Get f) >>= k = Get (\c -> f c >>= k) (Look f) >>= k = Look (\s -> f s >>= k) Fail >>= k = Fail (Result x p) >>= k = k x `mplus` (p >>= k) (Final r) >>= k = final [ys' | (x,s) <- r, ys' <- run (k x) s] fail _ = Fail instance Alternative (P t) where empty = mzero (<|>) = mplus instance MonadPlus (P t) where mzero = Fail -- most common case: two gets are combined Get f1 `mplus` Get f2 = Get (\c -> f1 c `mplus` f2 c) -- results are delivered as soon as possible Result x p `mplus` q = Result x (p `mplus` q) p `mplus` Result x q = Result x (p `mplus` q) -- fail disappears Fail `mplus` p = p p `mplus` Fail = p -- two finals are combined -- final + look becomes one look and one final (=optimization) -- final + sthg else becomes one look and one final Final r `mplus` Final t = Final (r ++ t) Final r `mplus` Look f = Look (\s -> Final (r ++ run (f s) s)) Final r `mplus` p = Look (\s -> Final (r ++ run p s)) Look f `mplus` Final r = Look (\s -> Final (run (f s) s ++ r)) p `mplus` Final r = Look (\s -> Final (run p s ++ r)) -- two looks are combined (=optimization) -- look + sthg else floats upwards Look f `mplus` Look g = Look (\s -> f s `mplus` g s) Look f `mplus` p = Look (\s -> f s `mplus` p) p `mplus` Look f = Look (\s -> p `mplus` f s) -- --------------------------------------------------------------------------- -- The ReadP type newtype ReadP t a = R (forall b . (a -> P t b) -> P t b) -- Functor, Monad, MonadPlus instance Functor (ReadP t) where fmap h (R f) = R (\k -> f (k . h)) instance Applicative (ReadP t) where pure x = R (\k -> k x) (<*>) = ap instance Monad (ReadP t) where return = pure fail _ = R (\_ -> Fail) R m >>= f = R (\k -> m (\a -> let R m' = f a in m' k)) instance Alternative (ReadP t) where empty = mzero (<|>) = mplus instance MonadPlus (ReadP t) where mzero = pfail mplus = (+++) -- --------------------------------------------------------------------------- -- Operations over P final :: [(a,[t])] -> P t a -- Maintains invariant for Final constructor final [] = Fail final r = Final r run :: P t a -> [t] -> [(a,[t])] run (Get f) (c:s) = run (f c) s run (Look f) s = run (f s) s run (Result x p) s = (x,s) : run p s run (Final r) _ = r run _ _ = [] -- --------------------------------------------------------------------------- -- Operations over ReadP -- | Run a parser on a list of tokens. Returns the list of complete matches. parse :: ReadP t a -> [t] -> [a] parse p ts = case complete p of R f -> map fst $ run (f return) ts get :: ReadP t t -- ^ Consumes and returns the next character. -- Fails if there is no input left. get = R Get look :: ReadP t [t] -- ^ Look-ahead: returns the part of the input that is left, without -- consuming it. look = R Look pfail :: ReadP t a -- ^ Always fails. pfail = R (\_ -> Fail) (+++) :: ReadP t a -> ReadP t a -> ReadP t a -- ^ Symmetric choice. R f1 +++ R f2 = R (\k -> f1 k `mplus` f2 k) (<++) :: ReadP t a -> ReadP t a -> ReadP t a -- ^ Local, exclusive, left-biased choice: If left parser -- locally produces any result at all, then right parser is -- not used. R f <++ q = do s <- look probe (f return) s 0# where probe (Get f) (c:s) n = probe (f c) s (n+#1#) probe (Look f) s n = probe (f s) s n probe p@(Result _ _) _ n = discard n >> R (p >>=) probe (Final r) _ _ = R (Final r >>=) probe _ _ _ = q discard 0# = return () discard n = get >> discard (n-#1#) gather :: ReadP t a -> ReadP t ([t], a) -- ^ Transforms a parser into one that does the same, but -- in addition returns the exact characters read. -- IMPORTANT NOTE: 'gather' gives a runtime error if its first argument -- is built using any occurrences of readS_to_P. gather (R m) = R (\k -> gath id (m (\a -> return (\s -> k (s,a))))) where gath l (Get f) = Get (\c -> gath (l.(c:)) (f c)) gath l Fail = Fail gath l (Look f) = Look (\s -> gath l (f s)) gath l (Result k p) = k (l []) `mplus` gath l p gath l (Final r) = error "do not use readS_to_P in gather!" -- --------------------------------------------------------------------------- -- Derived operations satisfy :: (t -> Bool) -> ReadP t t -- ^ Consumes and returns the next character, if it satisfies the -- specified predicate. satisfy p = do c <- get; if p c then return c else pfail char :: Eq t => t-> ReadP t t -- ^ Parses and returns the specified character. char c = satisfy (c ==) string :: Eq t => [t] -> ReadP t [t] -- ^ Parses and returns the specified string. string this = do s <- look; scan this s where scan [] _ = do return this scan (x:xs) (y:ys) | x == y = do _ <- get; scan xs ys scan _ _ = do pfail eof :: ReadP tok () eof = do ts <- look unless (null ts) pfail complete :: ReadP tok a -> ReadP tok a complete p = do x <- p eof return x munch :: (t -> Bool) -> ReadP t [t] -- ^ Parses the first zero or more characters satisfying the predicate. munch p = do s <- look scan s where scan (c:cs) | p c = do _ <- get; s <- scan cs; return (c:s) scan _ = do return [] munch1 :: (t -> Bool) -> ReadP t [t] -- ^ Parses the first one or more characters satisfying the predicate. munch1 p = do c <- get if p c then do s <- munch p; return (c:s) else pfail choice :: [ReadP t a] -> ReadP t a -- ^ Combines all parsers in the specified list. choice [] = pfail choice [p] = p choice (p:ps) = p +++ choice ps skipSpaces :: ReadP Char () -- ^ Skips all whitespace. skipSpaces = do s <- look skip s where skip (c:s) | isSpace c = do _ <- get; skip s skip _ = do return () count :: Int -> ReadP t a -> ReadP t [a] -- ^ @count n p@ parses @n@ occurrences of @p@ in sequence. A list of -- results is returned. count n p = sequence (replicate n p) between :: ReadP t open -> ReadP t close -> ReadP t a -> ReadP t a -- ^ @between open close p@ parses @open@, followed by @p@ and finally -- @close@. Only the value of @p@ is returned. between open close p = do _ <- open x <- p _ <- close return x option :: a -> ReadP t a -> ReadP t a -- ^ @option x p@ will either parse @p@ or return @x@ without consuming -- any input. option x p = p +++ return x optional :: ReadP t a -> ReadP t () -- ^ @optional p@ optionally parses @p@ and always returns @()@. optional p = (p >> return ()) +++ return () many :: ReadP t a -> ReadP t [a] -- ^ Parses zero or more occurrences of the given parser. many p = return [] +++ many1 p many1 :: ReadP t a -> ReadP t [a] -- ^ Parses one or more occurrences of the given parser. many1 p = liftM2 (:) p (many p) skipMany :: ReadP t a -> ReadP t () -- ^ Like 'many', but discards the result. skipMany p = many p >> return () skipMany1 :: ReadP t a -> ReadP t () -- ^ Like 'many1', but discards the result. skipMany1 p = p >> skipMany p sepBy :: ReadP t a -> ReadP t sep -> ReadP t [a] -- ^ @sepBy p sep@ parses zero or more occurrences of @p@, separated by @sep@. -- Returns a list of values returned by @p@. sepBy p sep = sepBy1 p sep +++ return [] sepBy1 :: ReadP t a -> ReadP t sep -> ReadP t [a] -- ^ @sepBy1 p sep@ parses one or more occurrences of @p@, separated by @sep@. -- Returns a list of values returned by @p@. sepBy1 p sep = liftM2 (:) p (many (sep >> p)) endBy :: ReadP t a -> ReadP t sep -> ReadP t [a] -- ^ @endBy p sep@ parses zero or more occurrences of @p@, separated and ended -- by @sep@. endBy p sep = many (do x <- p ; _ <- sep ; return x) endBy1 :: ReadP t a -> ReadP t sep -> ReadP t [a] -- ^ @endBy p sep@ parses one or more occurrences of @p@, separated and ended -- by @sep@. endBy1 p sep = many1 (do x <- p ; _ <- sep ; return x) chainr :: ReadP t a -> ReadP t (a -> a -> a) -> a -> ReadP t a -- ^ @chainr p op x@ parses zero or more occurrences of @p@, separated by @op@. -- Returns a value produced by a /right/ associative application of all -- functions returned by @op@. If there are no occurrences of @p@, @x@ is -- returned. chainr p op x = chainr1 p op +++ return x chainl :: ReadP t a -> ReadP t (a -> a -> a) -> a -> ReadP t a -- ^ @chainl p op x@ parses zero or more occurrences of @p@, separated by @op@. -- Returns a value produced by a /left/ associative application of all -- functions returned by @op@. If there are no occurrences of @p@, @x@ is -- returned. chainl p op x = chainl1 p op +++ return x chainr1 :: ReadP t a -> ReadP t (a -> a -> a) -> ReadP t a -- ^ Like 'chainr', but parses one or more occurrences of @p@. chainr1 p op = scan where scan = p >>= rest rest x = do f <- op y <- scan return (f x y) +++ return x chainl1 :: ReadP t a -> ReadP t (a -> a -> a) -> ReadP t a -- ^ Like 'chainl', but parses one or more occurrences of @p@. chainl1 p op = p >>= rest where rest x = do f <- op y <- p rest (f x y) +++ return x manyTill :: ReadP t a -> ReadP t end -> ReadP t [a] -- ^ @manyTill p end@ parses zero or more occurrences of @p@, until @end@ -- succeeds. Returns a list of values returned by @p@. manyTill p end = scan where scan = (end >> return []) <++ (liftM2 (:) p scan) -- --------------------------------------------------------------------------- -- Converting between ReadP and Read readP_to_S :: ReadP Char a -> ReadS a -- ^ Converts a parser into a Haskell ReadS-style function. -- This is the main way in which you can \"run\" a 'ReadP' parser: -- the expanded type is -- @ readP_to_S :: ReadP a -> String -> [(a,String)] @ readP_to_S (R f) = run (f return) readS_to_P :: ReadS a -> ReadP Char a -- ^ Converts a Haskell ReadS-style function into a parser. -- Warning: This introduces local backtracking in the resulting -- parser, and therefore a possible inefficiency. readS_to_P r = R (\k -> Look (\s -> final [bs'' | (a,s') <- r s, bs'' <- run (k a) s'])) -- --------------------------------------------------------------------------- -- A variant on parse which returns either a result, or the unparseable text. run' :: P t a -> [t] -> Either a [t] run' (Get f) (c:s) = run' (f c) s run' (Look f) s = run' (f s) s run' (Result x _) _ = Left x run' (Final ((x,_):_)) _ = Left x run' _ s = Right s parse' :: ReadP t a -> [t] -> Either a [t] parse' p ts = case complete p of R f -> run' (f return) ts -- --------------------------------------------------------------------------- -- QuickCheck properties that hold for the combinators {- $properties The following are QuickCheck specifications of what the combinators do. These can be seen as formal specifications of the behavior of the combinators. We use bags to give semantics to the combinators. > type Bag a = [a] Equality on bags does not care about the order of elements. > (=~) :: Ord a => Bag a -> Bag a -> Bool > xs =~ ys = sort xs == sort ys A special equality operator to avoid unresolved overloading when testing the properties. > (=~.) :: Bag (Int,String) -> Bag (Int,String) -> Bool > (=~.) = (=~) Here follow the properties: > prop_Get_Nil = > readP_to_S get [] =~ [] > > prop_Get_Cons c s = > readP_to_S get (c:s) =~ [(c,s)] > > prop_Look s = > readP_to_S look s =~ [(s,s)] > > prop_Fail s = > readP_to_S pfail s =~. [] > > prop_Return x s = > readP_to_S (return x) s =~. [(x,s)] > > prop_Bind p k s = > readP_to_S (p >>= k) s =~. > [ ys'' > | (x,s') <- readP_to_S p s > , ys'' <- readP_to_S (k (x::Int)) s' > ] > > prop_Plus p q s = > readP_to_S (p +++ q) s =~. > (readP_to_S p s ++ readP_to_S q s) > > prop_LeftPlus p q s = > readP_to_S (p <++ q) s =~. > (readP_to_S p s +<+ readP_to_S q s) > where > [] +<+ ys = ys > xs +<+ _ = xs > > prop_Gather s = > forAll readPWithoutReadS $ \p -> > readP_to_S (gather p) s =~ > [ ((pre,x::Int),s') > | (x,s') <- readP_to_S p s > , let pre = take (length s - length s') s > ] > > prop_String_Yes this s = > readP_to_S (string this) (this ++ s) =~ > [(this,s)] > > prop_String_Maybe this s = > readP_to_S (string this) s =~ > [(this, drop (length this) s) | this `isPrefixOf` s] > > prop_Munch p s = > readP_to_S (munch p) s =~ > [(takeWhile p s, dropWhile p s)] > > prop_Munch1 p s = > readP_to_S (munch1 p) s =~ > [(res,s') | let (res,s') = (takeWhile p s, dropWhile p s), not (null res)] > > prop_Choice ps s = > readP_to_S (choice ps) s =~. > readP_to_S (foldr (+++) pfail ps) s > > prop_ReadS r s = > readP_to_S (readS_to_P r) s =~. r s -} Agda-2.5.3/src/full/Agda/Utils/Maybe/0000755000000000000000000000000013154613124015265 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Utils/Maybe/Strict.hs0000644000000000000000000001257013154613124017076 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} #if __GLASGOW_HASKELL__ >= 708 {-# LANGUAGE DeriveGeneric #-} #endif {-# OPTIONS_GHC -fno-warn-orphans #-} -- | A strict version of the 'Maybe' type. -- -- Import qualified, as in -- @ -- import qualified Agda.Utils.Maybe.Strict as Strict -- @ -- -- Copyright : (c) 2006-2007 Roman Leshchinskiy -- (c) 2013 Simon Meier -- License : BSD-style (see the file LICENSE) -- -- Copyright : (c) 2014 Andreas Abel module Agda.Utils.Maybe.Strict ( module Data.Strict.Maybe , module Agda.Utils.Maybe.Strict ) where -- The following code is copied from -- http://hackage.haskell.org/package/strict-base-types-0.3.0/docs/src/Data-Maybe-Strict.html import Prelude hiding (Maybe (..), maybe, null) import qualified Prelude as Lazy import Control.Applicative (pure, (<$>)) import Control.DeepSeq (NFData (..)) import Data.Binary (Binary (..)) import Data.Data (Data (..)) import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend) import Data.Foldable (Foldable (..)) import Data.Traversable (Traversable (..)) import Data.Typeable (Typeable) import Data.Strict.Maybe (Maybe (Nothing, Just), fromJust, fromMaybe, isJust, isNothing, maybe) #if __GLASGOW_HASKELL__ >= 708 import GHC.Generics (Generic (..)) #endif import Agda.Utils.Null toStrict :: Lazy.Maybe a -> Maybe a toStrict Lazy.Nothing = Nothing toStrict (Lazy.Just x) = Just x toLazy :: Maybe a -> Lazy.Maybe a toLazy Nothing = Lazy.Nothing toLazy (Just x) = Lazy.Just x deriving instance Data a => Data (Maybe a) deriving instance Typeable Maybe #if __GLASGOW_HASKELL__ >= 708 deriving instance Generic (Maybe a) #endif instance Null (Maybe a) where empty = Nothing null = isNothing -- The monoid instance was fixed in strict-base-types 0.5.0. See -- Issue 1805. instance Semigroup a => Semigroup (Maybe a) where Nothing <> m = m m <> Nothing = m Just x1 <> Just x2 = Just (x1 <> x2) instance Semigroup a => Monoid (Maybe a) where mempty = Nothing mappend = (<>) instance Foldable Maybe where foldMap _ Nothing = mempty foldMap f (Just x) = f x instance Traversable Maybe where traverse _ Nothing = pure Nothing traverse f (Just x) = Just <$> f x instance NFData a => NFData (Maybe a) where rnf = rnf . toLazy instance Binary a => Binary (Maybe a) where put = put . toLazy get = toStrict <$> get -- | Analogous to 'Lazy.listToMaybe' in "Data.Maybe". listToMaybe :: [a] -> Maybe a listToMaybe [] = Nothing listToMaybe (a:_) = Just a -- | Analogous to 'Lazy.maybeToList' in "Data.Maybe". maybeToList :: Maybe a -> [a] maybeToList Nothing = [] maybeToList (Just x) = [x] -- | Analogous to 'Lazy.catMaybes' in "Data.Maybe". catMaybes :: [Maybe a] -> [a] catMaybes ls = [x | Just x <- ls] -- | Analogous to 'Lazy.mapMaybe' in "Data.Maybe". mapMaybe :: (a -> Maybe b) -> [a] -> [b] mapMaybe _ [] = [] mapMaybe f (x:xs) = case f x of Nothing -> rs Just r -> r:rs where rs = mapMaybe f xs -- The remaining code is a copy of Agda.Utils.Maybe -- * Collection operations. -- | @unionWith@ for collections of size <= 1. unionMaybeWith :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a unionMaybeWith f Nothing mb = mb unionMaybeWith f ma Nothing = ma unionMaybeWith f (Just a) (Just b) = Just $ f a b -- | Unzipping a list of length <= 1. unzipMaybe :: Maybe (a,b) -> (Maybe a, Maybe b) unzipMaybe Nothing = (Nothing, Nothing) unzipMaybe (Just (a,b)) = (Just a, Just b) -- | Filtering a singleton list. -- -- @filterMaybe p a = 'listToMaybe' ('filter' p [a])@ filterMaybe :: (a -> Bool) -> a -> Maybe a filterMaybe p a | p a = Just a | otherwise = Nothing -- * Conditionals and loops. -- | Version of 'mapMaybe' with different argument ordering. forMaybe :: [a] -> (a -> Maybe b) -> [b] forMaybe = flip mapMaybe -- | Version of 'maybe' with different argument ordering. -- Often, we want to case on a 'Maybe', do something interesting -- in the 'Just' case, but only a default action in the 'Nothing' -- case. Then, the argument ordering of @caseMaybe@ is preferable. -- -- @caseMaybe m err f = flip (maybe err) m f@ caseMaybe :: Maybe a -> b -> (a -> b) -> b caseMaybe m err f = maybe err f m -- * Monads and Maybe. -- | Monadic version of 'maybe'. maybeM :: Monad m => m b -> (a -> m b) -> m (Maybe a) -> m b maybeM n j mm = maybe n j =<< mm -- | Monadic version of 'fromMaybe'. fromMaybeM :: Monad m => m a -> m (Maybe a) -> m a fromMaybeM m mm = maybeM m return mm -- | Monadic version of 'caseMaybe'. -- That is, 'maybeM' with a different argument ordering. caseMaybeM :: Monad m => m (Maybe a) -> m b -> (a -> m b) -> m b caseMaybeM mm err f = maybeM err f mm -- | 'caseMaybeM' with flipped branches. ifJustM :: Monad m => m (Maybe a) -> (a -> m b) -> m b -> m b ifJustM mm = flip (caseMaybeM mm) -- | A more telling name for 'Traversable.forM' for the 'Maybe' collection type. -- Or: 'caseMaybe' without the 'Nothing' case. whenJust :: Monad m => Maybe a -> (a -> m ()) -> m () whenJust m k = caseMaybe m (return ()) k -- | 'caseMaybeM' without the 'Nothing' case. whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m () whenJustM c m = c >>= (`whenJust` m) Agda-2.5.3/src/full/Agda/Utils/Lens/0000755000000000000000000000000013154613124015131 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Utils/Lens/Examples.hs0000644000000000000000000000071013154613124017241 0ustar0000000000000000-- | Examples how to use "Agda.Utils.Lens". module Agda.Utils.Lens.Examples where import Agda.Utils.Functor import Agda.Utils.Lens data Record a b = Record { field1 :: a , field2 :: b } -- | (View source:) This is how you implement a lens for a record field. lensField1 :: Lens' a (Record a b) lensField1 f r = f (field1 r) <&> \ a -> r { field1 = a } lensField2 :: Lens' b (Record a b) lensField2 f r = f (field2 r) <&> \ b -> r { field2 = b } Agda-2.5.3/src/full/Agda/TypeChecking/0000755000000000000000000000000013154613124015505 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/With.hs0000644000000000000000000010025113154613124016753 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.With where import Control.Arrow ((&&&), (***), first, second) import Control.Applicative hiding (empty) import Control.Monad import Control.Monad.Writer (WriterT, runWriterT, tell) import Data.Either import qualified Data.List as List import Data.Maybe import Data.Monoid import Data.Traversable (traverse) import Agda.Syntax.Common import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Pattern import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Views import Agda.Syntax.Info import Agda.Syntax.Position import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Reduce import Agda.TypeChecking.Datatypes import Agda.TypeChecking.EtaContract import Agda.TypeChecking.Free import Agda.TypeChecking.Patterns.Abstract import Agda.TypeChecking.Pretty import Agda.TypeChecking.Records import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.ReconstructParameters import Agda.TypeChecking.Rules.Term import Agda.TypeChecking.Abstract import Agda.TypeChecking.Rules.LHS.Implicit import Agda.TypeChecking.Rules.LHS (isFlexiblePattern) import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null (empty) import Agda.Utils.Permutation import Agda.Utils.Pretty (prettyShow) import qualified Agda.Utils.Pretty as P import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | Split pattern variables according to with-expressions. -- Input: -- -- [@Δ@] context of types and with-arguments. -- -- [@Δ ⊢ t@] type of rhs. -- -- [@Δ ⊢ as@] types of with arguments. -- -- [@Δ ⊢ vs@] with arguments. -- -- -- Output: -- -- [@Δ₁@] part of context needed for with arguments and their types. -- -- [@Δ₂@] part of context not needed for with arguments and their types. -- -- [@π@] permutation from Δ to Δ₁Δ₂ as returned by 'splitTelescope'. -- -- [@Δ₁Δ₂ ⊢ t'@] type of rhs under @π@ -- -- [@Δ₁ ⊢ as'@] types of with-arguments depending only on @Δ₁@. -- -- [@Δ₁ ⊢ vs'@] with-arguments under @π@. splitTelForWith -- Input: :: Telescope -- ^ __@Δ@__ context of types and with-arguments. -> Type -- ^ __@Δ ⊢ t@__ type of rhs. -> [EqualityView] -- ^ __@Δ ⊢ as@__ types of with arguments. -> [Term] -- ^ __@Δ ⊢ vs@__ with arguments. -- Output: -> ( Telescope -- @Δ₁@ part of context needed for with arguments and their types. , Telescope -- @Δ₂@ part of context not needed for with arguments and their types. , Permutation -- @π@ permutation from Δ to Δ₁Δ₂ as returned by 'splitTelescope'. , Type -- @Δ₁Δ₂ ⊢ t'@ type of rhs under @π@ , [EqualityView] -- @Δ₁ ⊢ as'@ types of with- and rewrite-arguments depending only on @Δ₁@. , [Term] -- @Δ₁ ⊢ vs'@ with- and rewrite-arguments under @π@. ) -- ^ (__@Δ₁@__,__@Δ₂@__,__@π@__,__@t'@__,__@as'@__,__@vs'@__) where -- -- [@Δ₁@] part of context needed for with arguments and their types. -- -- [@Δ₂@] part of context not needed for with arguments and their types. -- -- [@π@] permutation from Δ to Δ₁Δ₂ as returned by 'splitTelescope'. -- -- [@Δ₁Δ₂ ⊢ t'@] type of rhs under @π@ -- -- [@Δ₁ ⊢ as'@] types with with-arguments depending only on @Δ₁@. -- -- [@Δ₁ ⊢ vs'@] with-arguments under @π@. splitTelForWith delta t as vs = let -- Andreas, 2016-01-27, unfixing issue 1692 -- Due to public protests, we do not rewrite in the types of rewrite -- expressions. -- Otherwise, we cannot rewrite twice after another with the same equation -- as it turns into a reflexive equation in the first rewrite. -- Thus we include the fvs of the rewrite terms in Δ₁. rewriteTerms = map snd $ filter (isEqualityType . fst) $ zip as vs -- Split the telescope into the part needed to type the with arguments -- and all the other stuff. fv = allFreeVars (as, vs) SplitTel delta1 delta2 perm = splitTelescope fv delta -- Δ₁Δ₂ ⊢ π : Δ pi = renaming __IMPOSSIBLE__ (reverseP perm) -- Δ₁ ⊢ ρ : Δ₁Δ₂ (We know that as does not depend on Δ₂.) rho = strengthenS __IMPOSSIBLE__ $ size delta2 -- Δ₁ ⊢ ρ ∘ π : Δ rhopi = composeS rho pi -- We need Δ₁Δ₂ ⊢ t' t' = applySubst pi t -- and Δ₁ ⊢ as' as' = applySubst rhopi as -- and Δ₁ ⊢ vs' : as' vs' = applySubst rhopi vs in (delta1, delta2, perm, t', as', vs') -- | Abstract with-expressions @vs@ to generate type for with-helper function. -- -- Each @EqualityType@, coming from a @rewrite@, will turn into 2 abstractions. withFunctionType :: Telescope -- ^ @Δ₁@ context for types of with types. -> [Term] -- ^ @Δ₁,Δ₂ ⊢ vs : raise Δ₂ as@ with and rewrite-expressions. -> [EqualityView] -- ^ @Δ₁ ⊢ as@ types of with and rewrite-expressions. -> Telescope -- ^ @Δ₁ ⊢ Δ₂@ context extension to type with-expressions. -> Type -- ^ @Δ₁,Δ₂ ⊢ b@ type of rhs. -> TCM (Type, Nat) -- ^ @Δ₁ → wtel → Δ₂′ → b′@ such that -- @[vs/wtel]wtel = as@ and -- @[vs/wtel]Δ₂′ = Δ₂@ and -- @[vs/wtel]b′ = b@. -- Plus the final number of with-arguments. withFunctionType delta1 vs as delta2 b = addContext delta1 $ do reportSLn "tc.with.abstract" 20 $ "preparing for with-abstraction" -- Normalize and η-contract the type @b@ of the rhs and the types @delta2@ -- of the pattern variables not mentioned in @vs : as@. let dbg n s x = reportSDoc "tc.with.abstract" n $ nest 2 $ text (s ++ " =") <+> prettyTCM x let d2b = telePi_ delta2 b dbg 30 "Δ₂ → B" d2b d2b <- normalise d2b dbg 30 "normal Δ₂ → B" d2b d2b <- etaContract d2b dbg 30 "eta-contracted Δ₂ → B" d2b vs <- etaContract =<< normalise vs as <- etaContract =<< normalise as -- do we need this? let piAbstractVs [] b = return b piAbstractVs (va : vas) b = piAbstract va =<< piAbstractVs vas b -- wd2db = wtel → [vs : as] (Δ₂ → B) wd2b <- piAbstractVs (zip vs as) d2b dbg 30 "wΓ → Δ₂ → B" wd2b return (telePi_ delta1 wd2b, countWithArgs as) countWithArgs :: [EqualityView] -> Nat countWithArgs = sum . map countArgs where countArgs OtherType{} = 1 countArgs EqualityType{} = 2 -- | From a list of @with@ and @rewrite@ expressions and their types, -- compute the list of final @with@ expressions (after expanding the @rewrite@s). withArguments :: [Term] -> [EqualityView] -> [Term] withArguments vs as = concat $ for (zip vs as) $ \case (v, OtherType a) -> [v] (prf, eqt@(EqualityType s _eq _pars _t v _v')) -> [unArg v, prf] -- | Compute the clauses for the with-function given the original patterns. buildWithFunction :: [Name] -- ^ Names of the module parameters of the parent function. -> QName -- ^ Name of the parent function. -> QName -- ^ Name of the with-function. -> Type -- ^ Types of the parent function. -> Telescope -- ^ Context of parent patterns. -> [NamedArg DeBruijnPattern] -- ^ Parent patterns. -> Nat -- ^ Number of module parameters in parent patterns -> Substitution -- ^ Substitution from parent lhs to with function lhs -> Permutation -- ^ Final permutation. -> Nat -- ^ Number of needed vars. -> Nat -- ^ Number of with expressions. -> [A.SpineClause] -- ^ With-clauses. -> TCM [A.SpineClause] -- ^ With-clauses flattened wrt. parent patterns. buildWithFunction cxtNames f aux t delta qs npars withSub perm n1 n cs = mapM buildWithClause cs where -- Nested with-functions will iterate this function once for each parent clause. buildWithClause (A.Clause (A.SpineLHS i _ ps wps) inheritedDots inhStrippedDots rhs wh catchall) = do let (wps0, wps1) = splitAt n wps ps0 = map defaultNamedArg wps0 reportSDoc "tc.with" 50 $ text "inheritedDots:" <+> vcat [ prettyTCM x <+> text "=" <+> prettyTCM v <+> text ":" <+> prettyTCM a | A.NamedDot x v a <- inheritedDots ] rhs <- buildRHS rhs (namedDots, strippedDots, ps') <- stripWithClausePatterns cxtNames f aux t delta qs npars perm ps reportSDoc "tc.with" 50 $ hang (text "strippedDots:") 2 $ vcat [ prettyTCM e <+> text "==" <+> prettyTCM v <+> (text ":" <+> prettyTCM t) | A.StrippedDot e v t <- strippedDots ] let (ps1, ps2) = splitAt n1 ps' let result = A.Clause (A.SpineLHS i aux (ps1 ++ ps0 ++ ps2) wps1) (inheritedDots ++ namedDots) (inhStrippedDots ++ strippedDots) rhs wh catchall reportSDoc "tc.with" 20 $ vcat [ text "buildWithClause returns" <+> prettyA result ] return result buildRHS rhs@A.RHS{} = return rhs buildRHS rhs@A.AbsurdRHS = return rhs buildRHS (A.WithRHS q es cs) = A.WithRHS q es <$> mapM ((A.spineToLhs . permuteNamedDots) <.> buildWithClause . A.lhsToSpine) cs buildRHS (A.RewriteRHS qes rhs wh) = flip (A.RewriteRHS qes) wh <$> buildRHS rhs -- The named dot patterns computed by buildWithClause lives in the context -- of the top with-clause (of the current call to buildWithFunction). When -- we recurse we expect inherited named dot patterns to live in the context -- of the innermost parent clause. Note that this makes them live in the -- context of the with-function arguments before any pattern matching. We -- need to update again once the with-clause patterns have been checked. -- This happens in Rules.Def.checkClause before calling checkRHS. The same -- goes for stripped dots. permuteNamedDots :: A.SpineClause -> A.SpineClause permuteNamedDots (A.Clause lhs dots sdots rhs wh catchall) = A.Clause lhs (applySubst withSub dots) (applySubst withSub sdots) rhs wh catchall {-| @stripWithClausePatterns cxtNames parent f t Δ qs np π ps = ps'@ [@Δ@] context bound by lhs of original function. [@f@] name of @with@-function. [@t@] type of the original function. [@qs@] internal patterns for original function. [@np@] number of module parameters in @qs@ [@π@] permutation taking @vars(qs)@ to @support(Δ)@. [@ps@] patterns in with clause (eliminating type @t@). [@ps'@] patterns for with function (presumably of type @Δ@). Example: @ record Stream (A : Set) : Set where coinductive constructor delay field force : A × Stream A record SEq (s t : Stream A) : Set where coinductive field ~force : let a , as = force s b , bs = force t in a ≡ b × SEq as bs test : (s : Nat × Stream Nat) (t : Stream Nat) → SEq (delay s) t → SEq t (delay s) ~force (test (a , as) t p) with force t ~force (test (suc n , as) t p) | b , bs = {!!} @ With function: @ f : (t : Stream Nat) (w : Nat × Stream Nat) (a : Nat) (as : Stream Nat) (p : SEq (delay (a , as)) t) → (fst w ≡ a) × SEq (snd w) as Δ = t a as p -- reorder to bring with-relevant (= needed) vars first π = a as t p → Δ qs = (a , as) t p ~force ps = (suc n , as) t p ~force ps' = (suc n) as t p @ Resulting with-function clause is: @ f t (b , bs) (suc n) as t p @ Note: stripWithClausePatterns factors @ps@ through @qs@, thus @ ps = qs[ps'] @ where @[..]@ is to be understood as substitution. The projection patterns have vanished from @ps'@ (as they are already in @qs@). -} stripWithClausePatterns :: [Name] -- ^ Names of the module parameters of the parent function -> QName -- ^ Name of the parent function. -> QName -- ^ Name of with-function. -> Type -- ^ __@t@__ top-level type of the original function. -> Telescope -- ^ __@Δ@__ context of patterns of parent function. -> [NamedArg DeBruijnPattern] -- ^ __@qs@__ internal patterns for original function. -> Nat -- ^ __@npars@__ number of module parameters is @qs@. -> Permutation -- ^ __@π@__ permutation taking @vars(qs)@ to @support(Δ)@. -> [NamedArg A.Pattern] -- ^ __@ps@__ patterns in with clause (eliminating type @t@). -> TCM ([A.NamedDotPattern], [A.StrippedDotPattern], [NamedArg A.Pattern]) -- ^ __@ps'@__ patterns for with function (presumably of type @Δ@). stripWithClausePatterns cxtNames parent f t delta qs npars perm ps = do -- Andreas, 2014-03-05 expand away pattern synoyms (issue 1074) ps <- expandPatternSynonyms ps -- Ulf, 2016-11-16 Issue 2303: We need the module parameter -- instantiations from qs, so we make sure -- that t is the top-level type of the parent function and add patterns for -- the module parameters to ps before stripping. let paramPat i _ = A.VarP (cxtNames !! i) ps' = zipWith (fmap . fmap . paramPat) [0..] (take npars qs) ++ ps psi <- insertImplicitPatternsT ExpandLast ps' t reportSDoc "tc.with.strip" 10 $ vcat [ text "stripping patterns" , nest 2 $ text "t = " <+> prettyTCM t , nest 2 $ text "ps = " <+> fsep (punctuate comma $ map prettyA ps) , nest 2 $ text "ps' = " <+> fsep (punctuate comma $ map prettyA ps') , nest 2 $ text "psi = " <+> fsep (punctuate comma $ map prettyA psi) , nest 2 $ text "qs = " <+> fsep (punctuate comma $ map (prettyTCM . namedArg) qs) , nest 2 $ text "perm= " <+> text (show perm) ] -- Andreas, 2015-11-09 Issue 1710: self starts with parent-function, not with-function! (ps', out) <- runWriterT $ strip (Def parent []) t psi qs let (strippedDots, namedDots) = partitionEithers out reportSDoc "tc.with.strip" 50 $ nest 2 $ text "namedDots:" <+> vcat [ prettyTCM x <+> text "=" <+> prettyTCM v <+> text ":" <+> prettyTCM a | A.NamedDot x v a <- namedDots ] let psp = permute perm ps' reportSDoc "tc.with.strip" 10 $ vcat [ nest 2 $ text "ps' = " <+> fsep (punctuate comma $ map prettyA ps') , nest 2 $ text "psp = " <+> fsep (punctuate comma $ map prettyA $ psp) ] -- Andreas, 2014-03-05 Issue 142: -- In some cases, permute throws away some dot patterns of ps' -- which are then never checked. if True then return (namedDots, strippedDots, psp) else do -- Andreas, 2014-03-05 Disabled the fix for issue 142, the following is dead code: forM_ (permute (droppedP perm) ps') $ \ p -> setCurrentRange p $ do reportSDoc "tc.with.strip" 10 $ text "warning: dropped pattern " <+> prettyA p reportSDoc "tc.with.strip" 60 $ text $ show p case namedArg p of A.DotP info o e -> case unScope e of A.Underscore{} -> return () e | o == UserWritten -> typeError $ GenericError $ "This inaccessible pattern is never checked, so only _ allowed here" _ -> return () _ -> return () return (namedDots, strippedDots, psp) where strip :: Term -- ^ Self. -> Type -- ^ The type to be eliminated. -> [NamedArg A.Pattern] -- ^ With-clause patterns. -> [NamedArg DeBruijnPattern] -- ^ Parent-clause patterns with de Bruijn indices relative to Δ. -> WriterT [Either A.StrippedDotPattern A.NamedDotPattern] TCM [NamedArg A.Pattern] -- ^ With-clause patterns decomposed by parent-clause patterns. -- Also outputs named dot patterns from the parent clause that -- we need to add let-bindings for. -- Case: out of with-clause patterns. strip self t [] qs@(_ : _) = do reportSDoc "tc.with.strip" 15 $ vcat [ text "strip (out of A.Patterns)" , nest 2 $ text "qs =" <+> fsep (punctuate comma $ map (prettyTCM . namedArg) qs) , nest 2 $ text "self=" <+> prettyTCM self , nest 2 $ text "t =" <+> prettyTCM t ] -- Andreas, 2015-06-11, issue 1551: -- As the type t develops, we need to insert more implicit patterns, -- due to copatterns / flexible arity. ps <- liftTCM $ insertImplicitPatternsT ExpandLast [] t if null ps then typeError $ GenericError $ "Too few arguments given in with-clause" else strip self t ps qs -- Case: out of parent-clause patterns. -- This is only ok if all remaining with-clause patterns -- are implicit patterns (we inserted too many). strip _ _ ps [] = do let implicit (A.WildP{}) = True implicit (A.ConP ci _ _) = patOrigin ci == ConOSystem implicit _ = False unless (all (implicit . namedArg) ps) $ typeError $ GenericError $ "Too many arguments given in with-clause" return [] -- Case: both parent-clause pattern and with-clause pattern present. -- Make sure they match, and decompose into subpatterns. strip self t (p0 : ps) qs@(q : _) | A.AsP _ x p <- namedArg p0 = do (a, _) <- mustBePi t let v = patternToTerm (namedArg q) tell [Right $ A.NamedDot x v (unDom a)] strip self t (fmap (p <$) p0 : ps) qs strip self t ps0@(p0 : ps) qs0@(q : qs) = do p <- liftTCM $ expandLitPattern p0 reportSDoc "tc.with.strip" 15 $ vcat [ text "strip" , nest 2 $ text "ps0 =" <+> fsep (punctuate comma $ map prettyA ps0) , nest 2 $ text "exp =" <+> prettyA p , nest 2 $ text "qs0 =" <+> fsep (punctuate comma $ map (prettyTCM . namedArg) qs0) , nest 2 $ text "self=" <+> prettyTCM self , nest 2 $ text "t =" <+> prettyTCM t ] let failDotPat :: Monoid w => WriterT w TCM a failDotPat = do d <- liftTCM $ prettyA p typeError $ GenericError $ "Inaccessible (dotted) patterns from the parent clause must " ++ "also be inaccessible in the with clause, when checking the " ++ "pattern " ++ show d ++ "," case namedArg q of ProjP o d -> case A.maybePostfixProjP p of Just (o', AmbQ ds) -> do -- Andreas, 2016-12-28, issue #2360: -- We disambiguate the projection in the with clause -- to the projection in the parent clause. d <- liftTCM $ getOriginalProjection d found <- anyM ds ((d ==) <.> (liftTCM . getOriginalProjection)) -- We assume here that neither @o@ nor @o'@ can be @ProjSystem@. if o /= o' then liftTCM $ mismatchOrigin o o' else do if not found then mismatch else do (self1, t1, ps) <- liftTCM $ do t <- reduce t (_, self1, t1) <- fromMaybe __IMPOSSIBLE__ <$> projectTyped self t o d -- Andreas, 2016-01-21, issue #1791 -- The type of a field might start with hidden quantifiers. -- So we may have to insert more implicit patterns here. ps <- insertImplicitPatternsT ExpandLast ps t1 return (self1, t1, ps) strip self1 t1 ps qs Nothing -> mismatch VarP x -> (p :) <$> recurse (var (dbPatVarIndex x)) AbsurdP p -> __IMPOSSIBLE__ DotP v -> case namedArg p of A.DotP r o e -> do (a, _) <- mustBePi t tell [Left $ A.StrippedDot e v (unDom a)] ok p A.WildP _ -> ok p -- Ulf, 2016-05-30: dot patterns are no longer mandatory so a parent -- dot pattern can appear as a variable in the child clause. Indeed -- this happens if you use a variable in the parent and '...' in the -- child. In this case we need to remember the the binding, so we can -- insert a let for it. A.VarP x -> do (a, _) <- mustBePi t tell [Right $ A.NamedDot x v (unDom a)] ok p -- Andreas, 2013-03-21 in case the implicit A.pattern has already been eta-expanded -- we just fold it back. This fixes issues 665 and 824. A.ConP ci _ _ | patOrigin ci == ConOSystem -> okFlex p -- Andreas, 2015-07-07 issue 1606: Same for flexible record patterns. -- Agda might have replaced a record of dot patterns (A.ConP) by a dot pattern (I.DotP). p'@A.ConP{} -> ifM (liftTCM $ isFlexiblePattern p') (okFlex p) {-else-} failDotPat p@(A.PatternSynP pi' c' [ps']) -> do reportSDoc "impossible" 10 $ text "stripWithClausePatterns: encountered pattern synonym " <+> prettyA p __IMPOSSIBLE__ _ -> failDotPat where okFlex = ok . makeImplicitP ok p = (p :) <$> recurse v q'@(ConP c ci qs') -> do reportSDoc "tc.with.strip" 60 $ text "parent pattern is constructor " <+> prettyTCM c (a, b) <- mustBePi t -- The type of the current pattern is a datatype. Def d es <- liftTCM $ ignoreSharing <$> normalise (unEl $ unDom a) let us = fromMaybe __IMPOSSIBLE__ $ allApplyElims es -- Get the original constructor and field names. c <- either __IMPOSSIBLE__ (`withRangeOf` c) <$> do liftTCM $ getConForm $ conName c case namedArg p of -- Andreas, 2015-07-07 Issue 1606. -- Agda sometimes changes a record of dot patterns into a dot pattern, -- so the user should be allowed to do likewise. A.DotP{} -> ifNotM (liftTCM $ isFlexiblePattern q') mismatch $ {-else-} do maybe __IMPOSSIBLE__ (\ p -> strip self t (p : ps) qs0) =<< do liftTCM $ expandImplicitPattern' (unDom a) $ makeImplicitP p -- Andreas, 2016-12-29, issue #2363. -- Allow _ to stand for the corresponding parent pattern. A.WildP{} -> do let ps' = map (updateNamedArg $ const $ A.WildP empty) qs' stripConP d us b c ConOCon qs' ps' A.ConP _ (A.AmbQ cs') ps' -> do -- Check whether the with-clause constructor can be (possibly trivially) -- disambiguated to be equal to the parent-clause constructor. -- Andreas, 2017-08-13, herein, ignore abstract constructors. cs' <- liftTCM $ do snd . partitionEithers <$> mapM getConForm cs' unless (elem c cs') mismatch -- Strip the subpatterns ps' and then continue. stripConP d us b c ConOCon qs' ps' A.RecP _ fs -> caseMaybeM (liftTCM $ isRecord d) mismatch $ \ def -> do ps' <- liftTCM $ insertMissingFields d (const $ A.WildP empty) fs (recordFieldNames def) stripConP d us b c ConORec qs' ps' p@(A.PatternSynP pi' c' ps') -> do reportSDoc "impossible" 10 $ text "stripWithClausePatterns: encountered pattern synonym " <+> prettyA p __IMPOSSIBLE__ p -> do reportSDoc "tc.with.strip" 60 $ text $ "with clause pattern is " ++ show p mismatch LitP lit -> case namedArg p of A.LitP lit' | lit == lit' -> recurse $ Lit lit A.WildP{} -> recurse $ Lit lit p@(A.PatternSynP pi' c' [ps']) -> do reportSDoc "impossible" 10 $ text "stripWithClausePatterns: encountered pattern synonym " <+> prettyA p __IMPOSSIBLE__ _ -> mismatch where recurse v = do t' <- piApply1 t v strip (self `apply1` v) t' ps qs mismatch = addContext delta $ typeError $ WithClausePatternMismatch (namedArg p0) q mismatchOrigin o o' = addContext delta . typeError . GenericDocError =<< fsep [ text "With clause pattern" , prettyA p0 , text "is not an instance of its parent pattern" , P.fsep <$> prettyTCMPatterns [q] , text $ "since the parent pattern is " ++ prettyProjOrigin o ++ " and the with clause pattern is " ++ prettyProjOrigin o' ] prettyProjOrigin ProjPrefix = "a prefix projection" prettyProjOrigin ProjPostfix = "a postfix projection" prettyProjOrigin ProjSystem = __IMPOSSIBLE__ -- | Make an ImplicitP, keeping arg. info. makeImplicitP :: NamedArg A.Pattern -> NamedArg A.Pattern makeImplicitP = updateNamedArg $ const $ A.WildP patNoRange -- case I.ConP / A.ConP stripConP :: QName -- ^ Data type name of this constructor pattern. -> [Arg Term] -- ^ Data type arguments of this constructor pattern. -> Abs Type -- ^ Type the remaining patterns eliminate. -> ConHead -- ^ Constructor of this pattern. -> ConInfo -- ^ Constructor info of this pattern (constructor/record). -> [NamedArg DeBruijnPattern] -- ^ Argument patterns (parent clause). -> [NamedArg A.Pattern] -- ^ Argument patterns (with clause). -> WriterT [Either A.StrippedDotPattern A.NamedDotPattern] TCM [NamedArg A.Pattern] -- ^ Stripped patterns. stripConP d us b c ci qs' ps' = do -- Get the type and number of parameters of the constructor. Defn {defType = ct, theDef = Constructor{conPars = np}} <- getConInfo c -- Compute the argument telescope for the constructor let ct' = ct `piApply` take np us TelV tel' _ <- liftTCM $ telView ct' reportSDoc "tc.with.strip" 20 $ vcat [ text "ct = " <+> prettyTCM ct , text "ct' = " <+> prettyTCM ct' , text "np = " <+> text (show np) , text "us = " <+> prettyList (map prettyTCM us) , text "us' = " <+> prettyList (map prettyTCM $ take np us) ] -- Compute the new type let v = Con c ci [ Arg info (var i) | (i, Arg info _) <- zip (downFrom $ size qs') qs' ] t' = tel' `abstract` absApp (raise (size tel') b) v self' = tel' `abstract` apply1 (raise (size tel') self) v -- Issue 1546 reportSDoc "tc.with.strip" 15 $ sep [ text "inserting implicit" , nest 2 $ prettyList $ map prettyA (ps' ++ ps) , nest 2 $ text ":" <+> prettyTCM t' ] -- Insert implicit patterns (just for the constructor arguments) psi' <- liftTCM $ insertImplicitPatterns ExpandLast ps' tel' unless (size psi' == size tel') $ typeError $ WrongNumberOfConstructorArguments (conName c) (size tel') (size psi') -- Andreas, Ulf, 2016-06-01, Ulf's variant at issue #679 -- Since instantiating the type with a constructor pattern -- can reveal more hidden arguments, we need to insert them here. psi <- liftTCM $ insertImplicitPatternsT ExpandLast (psi' ++ ps) t' -- Keep going strip self' t' psi (qs' ++ qs) -- | Construct the display form for a with function. It will display -- applications of the with function as applications to the original function. -- For instance, -- -- @ -- aux a b c -- @ -- -- as -- -- @ -- f (suc a) (suc b) | c -- @ withDisplayForm :: QName -- ^ The name of parent function. -> QName -- ^ The name of the @with@-function. -> Telescope -- ^ __@Δ₁@__ The arguments of the @with@ function before the @with@ expressions. -> Telescope -- ^ __@Δ₂@__ The arguments of the @with@ function after the @with@ expressions. -> Nat -- ^ __@n@__ The number of @with@ expressions. -> [NamedArg DeBruijnPattern] -- ^ __@qs@__ The parent patterns. -> Permutation -- ^ __@perm@__ Permutation to split into needed and unneeded vars. -> Permutation -- ^ __@lhsPerm@__ Permutation reordering the variables in parent patterns. -> TCM DisplayForm withDisplayForm f aux delta1 delta2 n qs perm@(Perm m _) lhsPerm = do -- Compute the arity of the display form. let arity0 = n + size delta1 + size delta2 -- The currently free variables have to be added to the front. topArgs <- raise arity0 <$> getContextArgs let top = length topArgs arity = arity0 + top -- Build the rhs of the display form. wild <- freshNoName_ <&> \ x -> Def (qualify_ x) [] let -- Convert the parent patterns to terms. tqs0 = patsToElims qs -- Build a substitution to replace the parent pattern vars -- by the pattern vars of the with-function. (ys0, ys1) = splitAt (size delta1) $ permute perm $ downFrom m ys = reverse (map Just ys0 ++ replicate n Nothing ++ map Just ys1) ++ map (Just . (m +)) [0..top-1] rho = sub top ys wild tqs = applySubst rho tqs0 -- Build the arguments to the with function. es = map (Apply . fmap DTerm) topArgs ++ tqs withArgs = map var $ take n $ downFrom $ size delta2 + n dt = DWithApp (DDef f es) (map DTerm withArgs) [] -- Build the lhs of the display form and finish. -- @var 0@ is the pattern variable (hole). let display = Display arity (replicate arity $ Apply $ defaultArg $ var 0) dt -- Debug printing. let addFullCtx = addContext delta1 . flip (foldr addContext) (for [1..n] $ \ i -> "w" ++ show i) . addContext delta2 reportSDoc "tc.with.display" 20 $ vcat [ text "withDisplayForm" , nest 2 $ vcat [ text "f =" <+> text (prettyShow f) , text "aux =" <+> text (prettyShow aux) , text "delta1 =" <+> prettyTCM delta1 , text "delta2 =" <+> do addContext delta1 $ prettyTCM delta2 , text "n =" <+> text (show n) , text "perm =" <+> text (show perm) , text "top =" <+> do addFullCtx $ prettyTCM topArgs , text "qs =" <+> prettyList (map pretty qs) , text "qsToTm =" <+> prettyTCM tqs0 -- ctx would be permuted form of delta1 ++ delta2 , text "ys =" <+> text (show ys) , text "rho =" <+> text (prettyShow rho) , text "qs[rho]=" <+> do addFullCtx $ prettyTCM tqs , text "dt =" <+> do addFullCtx $ prettyTCM dt ] ] reportSDoc "tc.with.display" 70 $ nest 2 $ vcat [ text "raw =" <+> text (show display) ] return display where -- Ulf, 2014-02-19: We need to rename the module parameters as well! (issue1035) -- sub top ys wild = map term [0 .. m - 1] ++# raiseS (length qs) -- Andreas, 2015-10-28: Yes, but properly! (Issue 1407) sub top ys wild = parallelS $ map term [0 .. m + top - 1] where term i = maybe wild var $ List.findIndex (Just i ==) ys -- -- OLD -- sub top rho wild = parallelS $ map term [0 .. m - 1] ++ topTerms -- where -- -- Ulf, 2014-02-19: We need to rename the module parameters as well! (issue1035) -- newVars = length qs -- topTerms = [ var (i + newVars) | i <- [0..top - 1] ] -- -- thinking required.. but ignored -- -- dropping the reverse seems to work better -- -- Andreas, 2010-09-09: I DISAGREE. -- -- Ulf, 2011-09-02: Thinking done. Neither was correct. -- -- We had the wrong permutation and we used it incorrectly. Should work now. -- term i = maybe wild var $ findIndex (Just i ==) rho -- Andreas, 2014-12-05 refactored using numberPatVars -- Andreas, 2013-02-28 modeled after Coverage/Match/buildMPatterns patsToElims :: [NamedArg DeBruijnPattern] -> [I.Elim' DisplayTerm] patsToElims = map $ toElim . fmap namedThing where toElim :: Arg DeBruijnPattern -> I.Elim' DisplayTerm toElim (Arg ai p) = case p of ProjP o d -> I.Proj o d p -> I.Apply $ Arg ai $ toTerm p toTerms :: [NamedArg DeBruijnPattern] -> [Arg DisplayTerm] toTerms = map $ fmap $ toTerm . namedThing toTerm :: DeBruijnPattern -> DisplayTerm toTerm p = case p of ProjP _ d -> DDef d [] -- WRONG. TODO: convert spine to non-spine ... DDef d . defaultArg VarP x -> DTerm $ var $ dbPatVarIndex x DotP t -> DDot $ t AbsurdP p -> toTerm p ConP c cpi ps -> DCon c (fromConPatternInfo cpi) $ toTerms ps LitP l -> DTerm $ Lit l Agda-2.5.3/src/full/Agda/TypeChecking/CheckInternal.hs0000644000000000000000000004047513154613124020565 0ustar0000000000000000{-# LANGUAGE CPP #-} -- Initially authored by Andreas, 2013-10-22. -- | A bidirectional type checker for internal syntax. -- -- Performs checking on unreduced terms. -- With the exception that projection-like function applications -- have to be reduced since they break bidirectionality. module Agda.TypeChecking.CheckInternal ( checkType , checkInternal , checkInternal' , Action(..), defaultAction, eraseUnusedAction , infer , inferSort ) where import Control.Arrow ((&&&), (***), first, second) import Control.Applicative import Control.Monad import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Conversion import Agda.TypeChecking.Datatypes -- (getConType, getFullyAppliedConType) import Agda.TypeChecking.Level import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Pretty import Agda.TypeChecking.ProjectionLike (elimView) import Agda.TypeChecking.Records (getDefType) import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.Utils.Functor (($>)) import Agda.Utils.Monad import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- * Bidirectional rechecker -- -- | Entry point for e.g. checking WithFunctionType. -- checkType :: Type -> TCM () -- checkType t = -- dontAssignMetas $ ignoreSorts $ -- checkInternal (unEl t) (sort Inf) -- | Entry point for e.g. checking WithFunctionType. checkType :: Type -> TCM () checkType t = void $ checkType' t -- | Check a type and infer its sort. -- -- Necessary because of PTS rule @(SizeUniv, Set i, Set i)@ -- but @SizeUniv@ is not included in any @Set i@. -- -- This algorithm follows -- Abel, Coquand, Dybjer, MPC 08, -- Verifying a Semantic βη-Conversion Test for Martin-Löf Type Theory -- checkType' :: Type -> TCM Sort checkType' t = do reportSDoc "tc.check.internal" 20 $ sep [ text "checking internal type " , prettyTCM t ] v <- elimView True $ unEl t -- bring projection-like funs in post-fix form case ignoreSharing v of Pi a b -> do s1 <- checkType' $ unDom a s2 <- (b $>) <$> do addContext (absName b, a) $ do checkType' $ absBody b return $ dLub s1 s2 Sort s -> do _ <- checkSort defaultAction s return $ sSuc s Var i es -> do a <- typeOfBV i checkTypeSpine a (Var i []) es Def f es -> do -- not a projection-like fun a <- defType <$> getConstInfo f checkTypeSpine a (Def f []) es MetaV x es -> do -- we assume meta instantiations to be well-typed a <- metaType x checkTypeSpine a (MetaV x []) es v@Lam{} -> typeError $ InvalidType v v@Con{} -> typeError $ InvalidType v v@Lit{} -> typeError $ InvalidType v v@Level{} -> typeError $ InvalidType v DontCare v -> checkType' $ t $> v Shared{} -> __IMPOSSIBLE__ checkTypeSpine :: Type -> Term -> Elims -> TCM Sort checkTypeSpine a self es = shouldBeSort =<< do snd <$> inferSpine a self es -- | 'checkInternal' traverses the whole 'Term', and we can use this -- traversal to modify the term. data Action = Action { preAction :: Type -> Term -> TCM Term -- ^ Called on each subterm before the checker runs. , postAction :: Type -> Term -> TCM Term -- ^ Called on each subterm after the type checking. , relevanceAction :: Relevance -> Relevance -> Relevance -- ^ Called for each @ArgInfo@. -- The first 'Relevance' is from the type, -- the second from the term. } -- | The default action is to not change the 'Term' at all. defaultAction :: Action defaultAction = Action { preAction = \ _ -> return , postAction = \ _ -> return , relevanceAction = \ _ -> id } eraseUnusedAction :: Action eraseUnusedAction = defaultAction { postAction = eraseUnused } where eraseUnused :: Type -> Term -> TCM Term eraseUnused t v = case ignoreSharing v of Def f es -> do pols <- getPolarity f return $ Def f $ eraseIfNonvariant pols es _ -> return v eraseIfNonvariant :: [Polarity] -> Elims -> Elims eraseIfNonvariant [] es = es eraseIfNonvariant pols [] = [] eraseIfNonvariant (Nonvariant : pols) (e : es) = (fmap DontCare e) : eraseIfNonvariant pols es eraseIfNonvariant (_ : pols) (e : es) = e : eraseIfNonvariant pols es -- | Entry point for term checking. checkInternal :: Term -> Type -> TCM () checkInternal v t = void $ checkInternal' defaultAction v t checkInternal' :: Action -> Term -> Type -> TCM Term checkInternal' action v t = do reportSDoc "tc.check.internal" 20 $ sep [ text "checking internal " , nest 2 $ sep [ prettyTCM v <+> text ":" , nest 2 $ prettyTCM t ] ] -- Bring projection-like funs in post-fix form, -- even lone ones (True). v <- elimView True =<< preAction action t v postAction action t =<< case ignoreSharing v of Var i es -> do a <- typeOfBV i checkSpine action a (Var i []) es t Def f es -> do -- f is not projection(-like)! a <- defType <$> getConstInfo f checkSpine action a (Def f []) es t MetaV x es -> do -- we assume meta instantiations to be well-typed a <- metaType x checkSpine action a (MetaV x []) es t Con c ci vs -> do -- We need to fully apply the constructor to make getConType work! fullyApplyCon c vs t $ \ _d _dt _pars a vs' tel t -> do Con c ci vs2 <- checkArgs action a (Con c ci []) vs' t -- Strip away the extra arguments return $ applySubst (strengthenS __IMPOSSIBLE__ (size tel)) $ Con c ci $ take (length vs) vs2 Lit l -> Lit l <$ ((`subtype` t) =<< litType l) Lam ai vb -> do (a, b) <- shouldBePi t ai <- checkArgInfo action ai $ domInfo a addContext (suggest vb b, a) $ do Lam ai . Abs (absName vb) <$> checkInternal' action (absBody vb) (absBody b) Pi a b -> do s <- shouldBeSort t when (s == SizeUniv) $ typeError $ FunctionTypeInSizeUniv v let st = sort s sa = getSort a sb = getSort (unAbs b) mkDom v = El sa v <$ a mkRng v = fmap (v <$) b -- Preserve NoAbs goInside = case b of Abs{} -> addContext (absName b, a) NoAbs{} -> id a <- mkDom <$> checkInternal' action (unEl $ unDom a) (sort sa) -- TODO: checkPTS sa sb s goInside $ Pi a . mkRng <$> checkInternal' action (unEl $ unAbs b) (sort sb) Sort s -> do s <- checkSort action s -- this ensures @s /= Inf@ Sort s <$ ((sSuc s `leqSort`) =<< shouldBeSort t) Level l -> do l <- checkLevel action l Level l <$ ((`subtype` t) =<< levelType) DontCare v -> DontCare <$> checkInternal' action v t Shared{} -> __IMPOSSIBLE__ -- | Make sure a constructor is fully applied -- and infer the type of the constructor. -- Raises a type error if the constructor does not belong to the given type. fullyApplyCon :: ConHead -- ^ Constructor. -> Args -- ^ Constructor arguments. -> Type -- ^ Type of the constructor application. -> (QName -> Type -> Args -> Type -> Args -> Telescope -> Type -> TCM a) -- ^ Name of the data/record type, -- type of the data/record type, -- reconstructed parameters, -- type of the constructor (applied to parameters), -- full application arguments, -- types of missing arguments (already added to context), -- type of the full application. -> TCM a fullyApplyCon c vs t0 ret = do TelV tel t <- telView t0 -- The type of the constructor application may still be a function -- type. In this case, we introduce the domains @tel@ into the context -- and apply the constructor to these fresh variables. addContext tel $ do getFullyAppliedConType c t >>= \case Nothing -> typeError $ DoesNotConstructAnElementOf (conName c) t Just ((d, dt, pars), a) -> ret d dt pars a (raise (size tel) vs ++ teleArgs tel) tel t checkSpine :: Action -> Type -- ^ Type of the head @self@. -> Term -- ^ The head @self@. -> Elims -- ^ The eliminations @es@. -> Type -- ^ Expected type of the application @self es@. -> TCM Term -- ^ The application after modification by the @Action@. checkSpine action a self es t = do reportSDoc "tc.check.internal" 20 $ sep [ text "checking spine " , nest 2 $ sep [ parens (sep [ prettyTCM self <+> text ":" , nest 2 $ prettyTCM a ]) , nest 4 $ prettyTCM es <+> text ":" , nest 2 $ prettyTCM t ] ] ((v, v'), t') <- inferSpine' action a self self es t' <- reduce t' v' <$ coerceSize subtype v t' t checkArgs :: Action -> Type -- ^ Type of the head. -> Term -- ^ The head. -> Args -- ^ The arguments. -> Type -- ^ Expected type of the application. -> TCM Term -- ^ The application after modification by the @Action@. checkArgs action a self vs t = checkSpine action a self (map Apply vs) t -- | @checkArgInfo actual expected@. -- -- The @expected@ 'ArgInfo' comes from the type. -- The @actual@ 'ArgInfo' comes from the term and can be updated -- by an action. checkArgInfo :: Action -> ArgInfo -> ArgInfo -> TCM ArgInfo checkArgInfo action ai ai' = do checkHiding (getHiding ai) (getHiding ai') r <- checkRelevance action (getRelevance ai) (getRelevance ai') return $ setRelevance r ai checkHiding :: Hiding -> Hiding -> TCM () checkHiding h h' = unless (sameHiding h h') $ typeError $ HidingMismatch h h' -- | @checkRelevance action term type@. -- -- The @term@ 'Relevance' can be updated by the @action@. -- Note that the relevances might not match precisedly, -- because of the non-semantic 'Forced' relevance. checkRelevance :: Action -> Relevance -> Relevance -> TCM Relevance checkRelevance action r0 r0' = do unless (r == r') $ typeError $ RelevanceMismatch r r' return $ relevanceAction action r0' r0 -- Argument order for actions: @type@ @term@ where r = canon r0 r' = canon r0' canon Forced{} = Relevant canon r = r -- | Infer type of a neutral term. infer :: Term -> TCM Type infer v = do case ignoreSharing v of Var i es -> do a <- typeOfBV i snd <$> inferSpine a (Var i []) es Def f (Apply a : es) -> inferDef' f a es -- possibly proj.like Def f es -> inferDef f es -- not a projection-like fun MetaV x es -> do -- we assume meta instantiations to be well-typed a <- metaType x snd <$> inferSpine a (MetaV x []) es Shared{} -> __IMPOSSIBLE__ _ -> __IMPOSSIBLE__ -- | Infer ordinary function application. inferDef :: QName -> Elims -> TCM Type inferDef f es = do a <- defType <$> getConstInfo f snd <$> inferSpine a (Def f []) es -- | Infer possibly projection-like function application inferDef' :: QName -> Arg Term -> Elims -> TCM Type inferDef' f a es = do isProj <- isProjection f case isProj of Just Projection{ projIndex = n } | n > 0 -> do let self = unArg a b <- infer self snd <$> inferSpine b self (Proj ProjSystem f : es) _ -> inferDef f (Apply a : es) -- | @inferSpine t self es@ checks that spine @es@ eliminates -- value @self@ of type @t@ and returns the remaining type -- (target of elimination) and the final self (has that type). inferSpine :: Type -> Term -> Elims -> TCM (Term, Type) inferSpine a v es = first fst <$> inferSpine' defaultAction a v v es -- | Returns both the real term (first) and the transformed term (second). The -- transformed term is not necessarily a valid term, so it must not be used -- in types. inferSpine' :: Action -> Type -> Term -> Term -> Elims -> TCM ((Term, Term), Type) inferSpine' action t self self' [] = return ((self, self'), t) inferSpine' action t self self' (e : es) = do reportSDoc "tc.infer.internal" 30 $ sep [ text "inferSpine': " , text "type t = " <+> prettyTCM t , text "self = " <+> prettyTCM self , text "self' = " <+> prettyTCM self' , text "eliminated by e = " <+> prettyTCM e ] case e of Apply (Arg ai v) -> do (a, b) <- shouldBePi t ai <- checkArgInfo action ai $ domInfo a v' <- checkInternal' action v $ unDom a inferSpine' action (b `absApp` v) (self `applyE` [e]) (self' `applyE` [Apply (Arg ai v')]) es -- case: projection or projection-like Proj o f -> do (a, b) <- shouldBePi =<< shouldBeProjectible t f u <- applyDef o f (argFromDom a $> self) u' <- applyDef o f (argFromDom a $> self') inferSpine' action (b `absApp` self) u u' es -- | Type should either be a record type of a type eligible for -- the principal argument of projection-like functions. shouldBeProjectible :: Type -> QName -> TCM Type -- shouldBeProjectible t f = maybe failure return =<< projectionType t f shouldBeProjectible t f = maybe failure return =<< getDefType f =<< reduce t where failure = typeError $ ShouldBeRecordType t -- TODO: more accurate error that makes sense also for proj.-like funs. shouldBePi :: Type -> TCM (Dom Type, Abs Type) shouldBePi t = ifPiType t (\ a b -> return (a, b)) $ const $ typeError $ ShouldBePi t -- | Result is in reduced form. shouldBeSort :: Type -> TCM Sort shouldBeSort t = ifIsSort t return (typeError $ ShouldBeASort t) ifIsSort :: Type -> (Sort -> TCM a) -> TCM a -> TCM a ifIsSort t yes no = do t <- reduce t case ignoreSharing $ unEl t of Sort s -> yes s _ -> no -- | Check if sort is well-formed. checkSort :: Action -> Sort -> TCM Sort checkSort action s = case s of Type l -> Type <$> checkLevel action l Prop -> __IMPOSSIBLE__ -- the dummy Prop should not be part of a term we check Inf -> typeError $ SetOmegaNotValidType -- we cannot have Setω on the lhs of the colon SizeUniv -> typeError $ InvalidTypeSort s DLub a b -> do a <- checkSort action a addContext (absName b, defaultDom (sort a) :: Dom Type) $ do DLub a . Abs (absName b) <$> checkSort action (absBody b) -- | Check if level is well-formed. checkLevel :: Action -> Level -> TCM Level checkLevel action (Max ls) = Max <$> mapM checkPlusLevel ls where checkPlusLevel l@ClosedLevel{} = return l checkPlusLevel (Plus k l) = Plus k <$> checkLevelAtom l checkLevelAtom l = do lvl <- levelType UnreducedLevel <$> case l of MetaLevel x es -> checkInternal' action (MetaV x es) lvl BlockedLevel _ v -> checkInternal' action v lvl NeutralLevel _ v -> checkInternal' action v lvl UnreducedLevel v -> checkInternal' action v lvl -- | Type of a term or sort meta. metaType :: MetaId -> TCM Type metaType x = jMetaType . mvJudgement <$> lookupMeta x -- | Universe subsumption and type equality (subtyping for sizes, resp.). subtype :: Type -> Type -> TCM () subtype t1 t2 = do ifIsSort t1 (\ s1 -> (s1 `leqSort`) =<< shouldBeSort t2) $ do -- Andreas, 2017-03-09, issue #2493 -- Only check subtyping, do not solve any metas! -- TODO: NEED? disableDestructiveUpdate dontAssignMetas $ leqType t1 t2 -- | Compute the sort of a type. inferSort :: Term -> TCM Sort inferSort t = case ignoreSharing t of Var i es -> do a <- typeOfBV i (_, s) <- eliminate (Var i []) a es shouldBeSort s Def f es -> do -- f is not projection(-like)! a <- defType <$> getConstInfo f (_, s) <- eliminate (Def f []) a es shouldBeSort s MetaV x es -> do a <- metaType x (_, s) <- eliminate (MetaV x []) a es shouldBeSort s Pi a b -> return $ dLub (getSort a) (getSort <$> b) Sort s -> return $ sSuc s Con{} -> __IMPOSSIBLE__ Lit{} -> __IMPOSSIBLE__ Lam{} -> __IMPOSSIBLE__ Level{} -> __IMPOSSIBLE__ DontCare{} -> __IMPOSSIBLE__ Shared{} -> __IMPOSSIBLE__ -- | @eliminate t self es@ eliminates value @self@ of type @t@ by spine @es@ -- and returns the remaining value and its type. eliminate :: Term -> Type -> Elims -> TCM (Term, Type) eliminate self t [] = return (self, t) eliminate self t (e : es) = case e of Apply (Arg _ v) -> do (_, b) <- shouldBePi t eliminate (self `apply1` v) (b `absApp` v) es -- case: projection or projection-like Proj o f -> do (Dom ai _, b) <- shouldBePi =<< shouldBeProjectible t f u <- applyDef o f $ Arg ai self eliminate u (b `absApp` self) es Agda-2.5.3/src/full/Agda/TypeChecking/Errors.hs-boot0000644000000000000000000000060313154613124020255 0ustar0000000000000000module Agda.TypeChecking.Errors where import Agda.TypeChecking.Monad.Base import Agda.Syntax.Position import Agda.Utils.Pretty -- Misplaced SPECIALISE pragma: -- {-# SPECIALIZE prettyError :: TCErr -> TCM String #-} prettyError :: MonadTCM tcm => TCErr -> tcm String prettyWarning :: MonadTCM tcm => Warning -> tcm Doc sayWhen :: Range -> Maybe (Closure Call) -> TCM Doc -> TCM Doc Agda-2.5.3/src/full/Agda/TypeChecking/CheckInternal.hs-boot0000644000000000000000000000051213154613124021512 0ustar0000000000000000module Agda.TypeChecking.CheckInternal where import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base (TCM) data Action defaultAction :: Action eraseUnusedAction :: Action checkType :: Type -> TCM () checkInternal :: Term -> Type -> TCM () checkInternal' :: Action -> Term -> Type -> TCM Term infer :: Term -> TCM Type Agda-2.5.3/src/full/Agda/TypeChecking/Primitive.hs0000644000000000000000000007321313154613124020017 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE UndecidableInstances #-} -- ASR (2017-04-10). TODO: Is this option required by the final -- version of GHC 8.2.1 (it was required by the RC 1)? #if __GLASGOW_HASKELL__ >= 802 {-# OPTIONS -Wno-simplifiable-class-constraints #-} #endif {-| Primitive functions, such as addition on builtin integers. -} module Agda.TypeChecking.Primitive where import Control.Monad import Control.Applicative import Control.Monad.Reader (asks) import Data.Char import Data.Map (Map) import qualified Data.Map as Map import Data.Maybe import Data.Traversable (traverse) import Data.Monoid (mempty) import Numeric.IEEE ( IEEE(identicalIEEE) ) import Agda.Interaction.Options import qualified Agda.Interaction.Options.Lenses as Lens import Agda.Syntax.Position import Agda.Syntax.Common hiding (Nat) import Agda.Syntax.Internal import Agda.Syntax.Internal.Generic (TermLike(..)) import Agda.Syntax.Literal import Agda.Syntax.Concrete.Pretty () import Agda.Syntax.Fixity import Agda.TypeChecking.Monad hiding (getConstInfo, typeOfConst) import qualified Agda.TypeChecking.Monad as TCM import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Reduce import Agda.TypeChecking.Reduce.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Errors import Agda.TypeChecking.Functions import Agda.TypeChecking.Level import Agda.TypeChecking.Quote (QuotingKit, quoteTermWithKit, quoteTypeWithKit, quoteClauseWithKit, quotingKit) import Agda.TypeChecking.Pretty () -- instances only import Agda.TypeChecking.Warnings import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Pretty (pretty, prettyShow) import Agda.Utils.Size import Agda.Utils.String ( Str(Str), unStr ) #include "undefined.h" import Agda.Utils.Impossible import Debug.Trace --------------------------------------------------------------------------- -- * Primitive functions --------------------------------------------------------------------------- data PrimitiveImpl = PrimImpl Type PrimFun -- Haskell type to Agda type newtype Nat = Nat { unNat :: Integer } deriving (Eq, Ord, Num, Enum, Real) -- In GHC > 7.8 deriving Integral causes an unnecessary toInteger -- warning. instance Integral Nat where toInteger = unNat quotRem (Nat a) (Nat b) = (Nat q, Nat r) where (q, r) = quotRem a b instance TermLike Nat where traverseTermM _ = pure foldTerm _ = mempty instance Show Nat where show = show . toInteger newtype Lvl = Lvl { unLvl :: Integer } deriving (Eq, Ord) instance Show Lvl where show = show . unLvl class PrimType a where primType :: a -> TCM Type instance (PrimType a, PrimType b) => PrimTerm (a -> b) where primTerm _ = unEl <$> (primType (undefined :: a) --> primType (undefined :: b)) instance PrimTerm a => PrimType a where primType _ = el $ primTerm (undefined :: a) class PrimTerm a where primTerm :: a -> TCM Term instance PrimTerm Integer where primTerm _ = primInteger instance PrimTerm Bool where primTerm _ = primBool instance PrimTerm Char where primTerm _ = primChar instance PrimTerm Double where primTerm _ = primFloat instance PrimTerm Str where primTerm _ = primString instance PrimTerm Nat where primTerm _ = primNat instance PrimTerm Lvl where primTerm _ = primLevel instance PrimTerm QName where primTerm _ = primQName instance PrimTerm MetaId where primTerm _ = primAgdaMeta instance PrimTerm Type where primTerm _ = primAgdaTerm instance PrimTerm Fixity' where primTerm _ = primFixity instance PrimTerm a => PrimTerm [a] where primTerm _ = list (primTerm (undefined :: a)) instance PrimTerm a => PrimTerm (IO a) where primTerm _ = io (primTerm (undefined :: a)) -- From Agda term to Haskell value class ToTerm a where toTerm :: TCM (a -> Term) toTermR :: TCM (a -> ReduceM Term) toTermR = (pure .) <$> toTerm instance ToTerm Nat where toTerm = return $ Lit . LitNat noRange . toInteger instance ToTerm Lvl where toTerm = return $ Level . Max . (:[]) . ClosedLevel . unLvl instance ToTerm Double where toTerm = return $ Lit . LitFloat noRange instance ToTerm Char where toTerm = return $ Lit . LitChar noRange instance ToTerm Str where toTerm = return $ Lit . LitString noRange . unStr instance ToTerm QName where toTerm = return $ Lit . LitQName noRange instance ToTerm MetaId where toTerm = do file <- fromMaybe __IMPOSSIBLE__ <$> asks TCM.envCurrentPath return $ Lit . LitMeta noRange file instance ToTerm Integer where toTerm = do pos <- primIntegerPos negsuc <- primIntegerNegSuc fromNat <- toTerm :: TCM (Nat -> Term) let intToTerm = fromNat . fromIntegral :: Integer -> Term let fromInt n | n >= 0 = apply pos [defaultArg $ intToTerm n] | otherwise = apply negsuc [defaultArg $ intToTerm (-n - 1)] return fromInt instance ToTerm Bool where toTerm = do true <- primTrue false <- primFalse return $ \b -> if b then true else false instance ToTerm Term where toTerm = do kit <- quotingKit; runReduceF (quoteTermWithKit kit) toTermR = do kit <- quotingKit; return (quoteTermWithKit kit) instance ToTerm Type where toTerm = do kit <- quotingKit; runReduceF (quoteTypeWithKit kit) toTermR = do kit <- quotingKit; return (quoteTypeWithKit kit) instance ToTerm ArgInfo where toTerm = do info <- primArgArgInfo vis <- primVisible hid <- primHidden ins <- primInstance rel <- primRelevant irr <- primIrrelevant return $ \ i -> info `applys` [ case getHiding i of NotHidden -> vis Hidden -> hid Instance{} -> ins , case getRelevance i of Relevant -> rel Irrelevant -> irr NonStrict -> rel Forced{} -> irr ] instance ToTerm Fixity' where toTerm = (. theFixity) <$> toTerm instance ToTerm Fixity where toTerm = do lToTm <- toTerm aToTm <- toTerm fixity <- primFixityFixity return $ \ Fixity{fixityAssoc = a, fixityLevel = l} -> fixity `apply` [defaultArg (aToTm a), defaultArg (lToTm l)] instance ToTerm Associativity where toTerm = do lassoc <- primAssocLeft rassoc <- primAssocRight nassoc <- primAssocNon return $ \ a -> case a of NonAssoc -> nassoc LeftAssoc -> lassoc RightAssoc -> rassoc instance ToTerm PrecedenceLevel where toTerm = do (iToTm :: Integer -> Term) <- toTerm related <- primPrecRelated unrelated <- primPrecUnrelated return $ \ p -> case p of Unrelated -> unrelated Related n -> related `apply` [defaultArg $ iToTm n] -- | @buildList A ts@ builds a list of type @List A@. Assumes that the terms -- @ts@ all have type @A@. buildList :: TCM ([Term] -> Term) buildList = do nil' <- primNil cons' <- primCons let nil = nil' cons x xs = cons' `applys` [x, xs] return $ foldr cons nil instance ToTerm a => ToTerm [a] where toTerm = do mkList <- buildList fromA <- toTerm return $ mkList . map fromA -- From Haskell value to Agda term type FromTermFunction a = Arg Term -> ReduceM (Reduced (MaybeReduced (Arg Term)) a) class FromTerm a where fromTerm :: TCM (FromTermFunction a) instance FromTerm Integer where fromTerm = do Con pos _ [] <- ignoreSharing <$> primIntegerPos Con negsuc _ [] <- ignoreSharing <$> primIntegerNegSuc toNat <- fromTerm :: TCM (FromTermFunction Nat) return $ \ v -> do b <- reduceB' v let v' = ignoreBlocking b arg = (<$ v') case ignoreSharing $ unArg (ignoreBlocking b) of Con c ci [u] | c == pos -> redBind (toNat u) (\ u' -> notReduced $ arg $ Con c ci [ignoreReduced u']) $ \ n -> redReturn $ fromIntegral n | c == negsuc -> redBind (toNat u) (\ u' -> notReduced $ arg $ Con c ci [ignoreReduced u']) $ \ n -> redReturn $ fromIntegral $ -n - 1 _ -> return $ NoReduction (reduced b) instance FromTerm Nat where fromTerm = fromLiteral $ \l -> case l of LitNat _ n -> Just $ fromInteger n _ -> Nothing instance FromTerm Lvl where fromTerm = fromReducedTerm $ \l -> case l of Level (Max [ClosedLevel n]) -> Just $ Lvl n _ -> Nothing instance FromTerm Double where fromTerm = fromLiteral $ \l -> case l of LitFloat _ x -> Just x _ -> Nothing instance FromTerm Char where fromTerm = fromLiteral $ \l -> case l of LitChar _ c -> Just c _ -> Nothing instance FromTerm Str where fromTerm = fromLiteral $ \l -> case l of LitString _ s -> Just $ Str s _ -> Nothing instance FromTerm QName where fromTerm = fromLiteral $ \l -> case l of LitQName _ x -> Just x _ -> Nothing instance FromTerm MetaId where fromTerm = fromLiteral $ \l -> case l of LitMeta _ _ x -> Just x _ -> Nothing instance FromTerm Bool where fromTerm = do true <- primTrue false <- primFalse fromReducedTerm $ \t -> case t of _ | t =?= true -> Just True | t =?= false -> Just False | otherwise -> Nothing where a =?= b = ignoreSharing a === ignoreSharing b Def x [] === Def y [] = x == y Con x _ [] === Con y _ [] = x == y Var n [] === Var m [] = n == m _ === _ = False instance (ToTerm a, FromTerm a) => FromTerm [a] where fromTerm = do nil' <- primNil cons' <- primCons nil <- isCon nil' cons <- isCon cons' toA <- fromTerm fromA <- toTerm return $ mkList nil cons toA fromA where isCon (Lam _ b) = isCon $ absBody b isCon (Con c _ _)= return c isCon (Shared p) = isCon (derefPtr p) isCon v = __IMPOSSIBLE__ mkList nil cons toA fromA t = do b <- reduceB' t let t = ignoreBlocking b let arg = (<$ t) case ignoreSharing $ unArg t of Con c ci [] | c == nil -> return $ YesReduction NoSimplification [] Con c ci [x,xs] | c == cons -> redBind (toA x) (\x' -> notReduced $ arg $ Con c ci [ignoreReduced x',xs]) $ \y -> redBind (mkList nil cons toA fromA xs) (fmap $ \xs' -> arg $ Con c ci [defaultArg $ fromA y, xs']) $ \ys -> redReturn (y : ys) _ -> return $ NoReduction (reduced b) -- | Conceptually: @redBind m f k = either (return . Left . f) k =<< m@ redBind :: ReduceM (Reduced a a') -> (a -> b) -> (a' -> ReduceM (Reduced b b')) -> ReduceM (Reduced b b') redBind ma f k = do r <- ma case r of NoReduction x -> return $ NoReduction $ f x YesReduction _ y -> k y redReturn :: a -> ReduceM (Reduced a' a) redReturn = return . YesReduction YesSimplification fromReducedTerm :: (Term -> Maybe a) -> TCM (FromTermFunction a) fromReducedTerm f = return $ \t -> do b <- reduceB' t case f $ ignoreSharing $ unArg (ignoreBlocking b) of Just x -> return $ YesReduction NoSimplification x Nothing -> return $ NoReduction (reduced b) fromLiteral :: (Literal -> Maybe a) -> TCM (FromTermFunction a) fromLiteral f = fromReducedTerm $ \t -> case t of Lit lit -> f lit _ -> Nothing -- | @trustMe : {a : Level} {A : Set a} {x y : A} -> x ≡ y@ primTrustMe :: TCM PrimitiveImpl primTrustMe = do -- primTrustMe is not --safe whenM (Lens.getSafeMode <$> commandLineOptions) $ warning SafeFlagPrimTrustMe -- Get the name and type of BUILTIN EQUALITY eq <- primEqualityName eqTy <- defType <$> getConstInfo eq -- E.g. @eqTy = eqTel → Set a@ where @eqTel = {a : Level} {A : Set a} (x y : A)@. TelV eqTel eqCore <- telView eqTy let eqSort = case ignoreSharing $ unEl eqCore of Sort s -> s _ -> __IMPOSSIBLE__ -- Construct the type of primTrustMe. -- E.g., type of @trustMe : {a : Level} {A : Set a} {x y : A} → eq {a} {A} x y@. let t = telePi_ (fmap hide eqTel) $ El eqSort $ Def eq $ map Apply $ teleArgs eqTel -- BUILTIN REFL maybe a constructor with one (the principal) argument or only parameters. -- Get the ArgInfo of the principal argument of refl. con@(Con rf ci []) <- ignoreSharing <$> primRefl minfo <- fmap (setOrigin Inserted) <$> getReflArgInfo rf let (refl :: Arg Term -> Term) = case minfo of Just ai -> Con rf ci . (:[]) . setArgInfo ai Nothing -> const con -- The implementation of primTrustMe: return $ PrimImpl t $ PrimFun __IMPOSSIBLE__ (size eqTel) $ \ ts -> do let (u, v) = fromMaybe __IMPOSSIBLE__ $ last2 ts -- Andreas, 2013-07-22. -- Note that we cannot call the conversion checker here, -- because 'reduce' might be called in a context where -- some bound variables do not have a type (just 'Prop), -- and the conversion checker for eliminations does not -- like this. -- We can only do untyped equality, e.g., by normalisation. (u', v') <- normalise' (u, v) if u' == v' then redReturn $ refl u else return $ NoReduction $ map notReduced ts -- | Get the 'ArgInfo' of the principal argument of BUILTIN REFL. -- -- Returns @Nothing@ for e.g. -- @ -- data Eq {a} {A : Set a} (x : A) : A → Set a where -- refl : Eq x x -- @ -- -- Returns @Just ...@ for e.g. -- @ -- data Eq {a} {A : Set a} : (x y : A) → Set a where -- refl : ∀ x → Eq x x -- @ getReflArgInfo :: ConHead -> TCM (Maybe ArgInfo) getReflArgInfo rf = do def <- getConInfo rf TelV reflTel _ <- telView $ defType def return $ fmap getArgInfo $ headMaybe $ drop (conPars $ theDef def) $ telToList reflTel -- | Used for both @primForce@ and @primForceLemma@. genPrimForce :: TCM Type -> (Term -> Arg Term -> Term) -> TCM PrimitiveImpl genPrimForce b ret = do let varEl s a = El (varSort s) <$> a varT s a = varEl s (varM a) varS s = pure $ sort $ varSort s t <- hPi "a" (el primLevel) $ hPi "b" (el primLevel) $ hPi "A" (varS 1) $ hPi "B" (varT 2 0 --> varS 1) b return $ PrimImpl t $ PrimFun __IMPOSSIBLE__ 6 $ \ ts -> case ts of [a, b, s, t, u, f] -> do u <- reduceB' u let isWHNF Blocked{} = return False isWHNF (NotBlocked _ u) = case ignoreSharing $ unArg u of Lit{} -> return True Con{} -> return True Lam{} -> return True Pi{} -> return True Sort{} -> return True -- sorts and levels are considered whnf Level{} -> return True DontCare{} -> return True Def q _ -> do def <- theDef <$> getConstInfo q return $ case def of Datatype{} -> True Record{} -> True _ -> False MetaV{} -> return False Var{} -> return False Shared{} -> __IMPOSSIBLE__ ifM (isWHNF u) (redReturn $ ret (unArg f) (ignoreBlocking u)) (return $ NoReduction $ map notReduced [a, b, s, t] ++ [reduced u, notReduced f]) _ -> __IMPOSSIBLE__ primForce :: TCM PrimitiveImpl primForce = do let varEl s a = El (varSort s) <$> a varT s a = varEl s (varM a) varS s = pure $ sort $ varSort s genPrimForce (nPi "x" (varT 3 1) $ (nPi "y" (varT 4 2) $ varEl 4 $ varM 2 <@> varM 0) --> varEl 3 (varM 1 <@> varM 0)) $ \ f u -> apply f [u] primForceLemma :: TCM PrimitiveImpl primForceLemma = do let varEl s a = El (varSort s) <$> a varT s a = varEl s (varM a) varS s = pure $ sort $ varSort s refl <- primRefl force <- primFunName <$> getPrimitive "primForce" genPrimForce (nPi "x" (varT 3 1) $ nPi "f" (nPi "y" (varT 4 2) $ varEl 4 $ varM 2 <@> varM 0) $ varEl 4 $ primEquality <#> varM 4 <#> (varM 2 <@> varM 1) <@> (pure (Def force []) <#> varM 5 <#> varM 4 <#> varM 3 <#> varM 2 <@> varM 1 <@> varM 0) <@> (varM 0 <@> varM 1) ) $ \ _ _ -> refl mkPrimLevelZero :: TCM PrimitiveImpl mkPrimLevelZero = do t <- primType (undefined :: Lvl) return $ PrimImpl t $ PrimFun __IMPOSSIBLE__ 0 $ \_ -> redReturn $ Level $ Max [] mkPrimLevelSuc :: TCM PrimitiveImpl mkPrimLevelSuc = do t <- primType (id :: Lvl -> Lvl) return $ PrimImpl t $ PrimFun __IMPOSSIBLE__ 1 $ \ ~[a] -> do l <- levelView' $ unArg a redReturn $ Level $ levelSuc l mkPrimLevelMax :: TCM PrimitiveImpl mkPrimLevelMax = do t <- primType (max :: Op Lvl) return $ PrimImpl t $ PrimFun __IMPOSSIBLE__ 2 $ \ ~[a, b] -> do Max as <- levelView' $ unArg a Max bs <- levelView' $ unArg b redReturn $ Level $ levelMax $ as ++ bs mkPrimFun1TCM :: (FromTerm a, ToTerm b, TermLike b) => TCM Type -> (a -> ReduceM b) -> TCM PrimitiveImpl mkPrimFun1TCM mt f = do toA <- fromTerm fromB <- toTermR t <- mt return $ PrimImpl t $ PrimFun __IMPOSSIBLE__ 1 $ \ts -> case ts of [v] -> redBind (toA v) (\v' -> [v']) $ \x -> do b <- f x case allMetas b of (m:_) -> return $ NoReduction [reduced (Blocked m v)] [] -> redReturn =<< fromB b _ -> __IMPOSSIBLE__ -- Tying the knot mkPrimFun1 :: (PrimType a, FromTerm a, PrimType b, ToTerm b) => (a -> b) -> TCM PrimitiveImpl mkPrimFun1 f = do toA <- fromTerm fromB <- toTerm t <- primType f return $ PrimImpl t $ PrimFun __IMPOSSIBLE__ 1 $ \ts -> case ts of [v] -> redBind (toA v) (\v' -> [v']) $ \x -> redReturn $ fromB $ f x _ -> __IMPOSSIBLE__ mkPrimFun2 :: ( PrimType a, FromTerm a, ToTerm a , PrimType b, FromTerm b , PrimType c, ToTerm c ) => (a -> b -> c) -> TCM PrimitiveImpl mkPrimFun2 f = do toA <- fromTerm fromA <- toTerm toB <- fromTerm fromC <- toTerm t <- primType f return $ PrimImpl t $ PrimFun __IMPOSSIBLE__ 2 $ \ts -> case ts of [v,w] -> redBind (toA v) (\v' -> [v', notReduced w]) $ \x -> redBind (toB w) (\w' -> [ reduced $ notBlocked $ Arg (argInfo v) (fromA x) , w']) $ \y -> redReturn $ fromC $ f x y _ -> __IMPOSSIBLE__ mkPrimFun4 :: ( PrimType a, FromTerm a, ToTerm a , PrimType b, FromTerm b, ToTerm b , PrimType c, FromTerm c, ToTerm c , PrimType d, FromTerm d , PrimType e, ToTerm e ) => (a -> b -> c -> d -> e) -> TCM PrimitiveImpl mkPrimFun4 f = do (toA, fromA) <- (,) <$> fromTerm <*> toTerm (toB, fromB) <- (,) <$> fromTerm <*> toTerm (toC, fromC) <- (,) <$> fromTerm <*> toTerm toD <- fromTerm fromE <- toTerm t <- primType f return $ PrimImpl t $ PrimFun __IMPOSSIBLE__ 4 $ \ts -> let argFrom fromX a x = reduced $ notBlocked $ Arg (argInfo a) (fromX x) in case ts of [a,b,c,d] -> redBind (toA a) (\a' -> a' : map notReduced [b,c,d]) $ \x -> redBind (toB b) (\b' -> [argFrom fromA a x, b', notReduced c, notReduced d]) $ \y -> redBind (toC c) (\c' -> [ argFrom fromA a x , argFrom fromB b y , c', notReduced d]) $ \z -> redBind (toD d) (\d' -> [ argFrom fromA a x , argFrom fromB b y , argFrom fromC c z , d']) $ \w -> redReturn $ fromE $ f x y z w _ -> __IMPOSSIBLE__ -- Type combinators infixr 4 --> infixr 4 .--> infixr 4 ..--> (-->), (.-->), (..-->) :: TCM Type -> TCM Type -> TCM Type a --> b = garr id a b a .--> b = garr (const $ Irrelevant) a b a ..--> b = garr (const $ NonStrict) a b garr :: (Relevance -> Relevance) -> TCM Type -> TCM Type -> TCM Type garr f a b = do a' <- a b' <- b return $ El (getSort a' `sLub` getSort b') $ Pi (Dom (mapRelevance f defaultArgInfo) a') (NoAbs "_" b') gpi :: ArgInfo -> String -> TCM Type -> TCM Type -> TCM Type gpi info name a b = do a <- a b <- addContext' (name, Dom info a) b let y = stringToArgName name return $ El (getSort a `dLub` Abs y (getSort b)) (Pi (Dom info a) (Abs y b)) hPi, nPi :: String -> TCM Type -> TCM Type -> TCM Type hPi = gpi $ setHiding Hidden defaultArgInfo nPi = gpi defaultArgInfo varM :: Int -> TCM Term varM = return . var infixl 9 <@>, <#> gApply :: Hiding -> TCM Term -> TCM Term -> TCM Term gApply h a b = do x <- a y <- b return $ x `apply` [Arg (setHiding h defaultArgInfo) y] (<@>),(<#>) :: TCM Term -> TCM Term -> TCM Term (<@>) = gApply NotHidden (<#>) = gApply Hidden list :: TCM Term -> TCM Term list t = primList <@> t io :: TCM Term -> TCM Term io t = primIO <@> t el :: TCM Term -> TCM Type el t = El (mkType 0) <$> t tset :: TCM Type tset = return $ sort (mkType 0) tSetOmega :: TCM Type tSetOmega = return $ sort Inf sSizeUniv :: Sort sSizeUniv = mkType 0 -- Andreas, 2016-04-14 switching off SizeUniv, unfixing issue #1428 -- sSizeUniv = SizeUniv tSizeUniv :: TCM Type tSizeUniv = tset -- Andreas, 2016-04-14 switching off SizeUniv, unfixing issue #1428 -- tSizeUniv = return $ El sSizeUniv $ Sort sSizeUniv -- Andreas, 2015-03-16 Since equality checking for types -- includes equality checking for sorts, we cannot put -- SizeUniv in Setω. (SizeUniv : Setω) == (_0 : suc _0) -- will first instantiate _0 := Setω, which is wrong. -- tSizeUniv = return $ El Inf $ Sort SizeUniv -- | Abbreviation: @argN = 'Arg' 'defaultArgInfo'@. argN :: e -> Arg e argN = Arg defaultArgInfo domN :: e -> Dom e domN = Dom defaultArgInfo -- | Abbreviation: @argH = 'hide' 'Arg' 'defaultArgInfo'@. argH :: e -> Arg e argH = Arg $ setHiding Hidden defaultArgInfo domH :: e -> Dom e domH = Dom $ setHiding Hidden defaultArgInfo --------------------------------------------------------------------------- -- * The actual primitive functions --------------------------------------------------------------------------- type Op a = a -> a -> a type Fun a = a -> a type Rel a = a -> a -> Bool type Pred a = a -> Bool primitiveFunctions :: Map String (TCM PrimitiveImpl) primitiveFunctions = Map.fromList -- Ulf, 2015-10-28: Builtin integers now map to a datatype, and since you -- can define these functions (reasonably) efficiently using the primitive -- functions on natural numbers there's no need for them anymore. Keeping the -- show function around for convenience, and as a test case for a primitive -- function taking an integer. -- -- Integer functions -- [ "primIntegerPlus" |-> mkPrimFun2 ((+) :: Op Integer) -- , "primIntegerMinus" |-> mkPrimFun2 ((-) :: Op Integer) -- , "primIntegerTimes" |-> mkPrimFun2 ((*) :: Op Integer) -- , "primIntegerDiv" |-> mkPrimFun2 (div :: Op Integer) -- partial -- , "primIntegerMod" |-> mkPrimFun2 (mod :: Op Integer) -- partial -- , "primIntegerEquality" |-> mkPrimFun2 ((==) :: Rel Integer) -- , "primIntegerLess" |-> mkPrimFun2 ((<) :: Rel Integer) -- , "primIntegerAbs" |-> mkPrimFun1 (Nat . abs :: Integer -> Nat) -- , "primNatToInteger" |-> mkPrimFun1 (toInteger :: Nat -> Integer) [ "primShowInteger" |-> mkPrimFun1 (Str . show :: Integer -> Str) -- Natural number functions , "primNatPlus" |-> mkPrimFun2 ((+) :: Op Nat) , "primNatMinus" |-> mkPrimFun2 ((\x y -> max 0 (x - y)) :: Op Nat) , "primNatTimes" |-> mkPrimFun2 ((*) :: Op Nat) , "primNatDivSucAux" |-> mkPrimFun4 ((\k m n j -> k + div (max 0 $ n + m - j) (m + 1)) :: Nat -> Nat -> Nat -> Nat -> Nat) , "primNatModSucAux" |-> let aux :: Nat -> Nat -> Nat -> Nat -> Nat aux k m n j | n > j = mod (n - j - 1) (m + 1) | otherwise = k + n in mkPrimFun4 aux , "primNatEquality" |-> mkPrimFun2 ((==) :: Rel Nat) , "primNatLess" |-> mkPrimFun2 ((<) :: Rel Nat) -- Level functions , "primLevelZero" |-> mkPrimLevelZero , "primLevelSuc" |-> mkPrimLevelSuc , "primLevelMax" |-> mkPrimLevelMax -- Floating point functions , "primNatToFloat" |-> mkPrimFun1 (fromIntegral :: Nat -> Double) , "primFloatPlus" |-> mkPrimFun2 ((+) :: Op Double) , "primFloatMinus" |-> mkPrimFun2 ((-) :: Op Double) , "primFloatTimes" |-> mkPrimFun2 ((*) :: Op Double) , "primFloatNegate" |-> mkPrimFun1 (negate :: Fun Double) , "primFloatDiv" |-> mkPrimFun2 ((/) :: Op Double) -- ASR (2016-09-29). We use bitwise equality for comparing Double -- because Haskell's Eq, which equates 0.0 and -0.0, allows to prove -- a contradiction (see Issue #2169). , "primFloatEquality" |-> mkPrimFun2 (floatEq :: Rel Double) , "primFloatNumericalEquality" |-> mkPrimFun2 ((==) :: Rel Double) , "primFloatNumericalLess" |-> mkPrimFun2 (floatLt :: Rel Double) , "primFloatSqrt" |-> mkPrimFun1 (sqrt :: Double -> Double) , "primRound" |-> mkPrimFun1 (round :: Double -> Integer) , "primFloor" |-> mkPrimFun1 (floor :: Double -> Integer) , "primCeiling" |-> mkPrimFun1 (ceiling :: Double -> Integer) , "primExp" |-> mkPrimFun1 (exp :: Fun Double) , "primLog" |-> mkPrimFun1 (log :: Fun Double) , "primSin" |-> mkPrimFun1 (sin :: Fun Double) , "primCos" |-> mkPrimFun1 (cos :: Fun Double) , "primTan" |-> mkPrimFun1 (tan :: Fun Double) , "primASin" |-> mkPrimFun1 (asin :: Fun Double) , "primACos" |-> mkPrimFun1 (acos :: Fun Double) , "primATan" |-> mkPrimFun1 (atan :: Fun Double) , "primATan2" |-> mkPrimFun2 (atan2 :: Double -> Double -> Double) , "primShowFloat" |-> mkPrimFun1 (Str . show :: Double -> Str) -- Character functions , "primCharEquality" |-> mkPrimFun2 ((==) :: Rel Char) , "primIsLower" |-> mkPrimFun1 isLower , "primIsDigit" |-> mkPrimFun1 isDigit , "primIsAlpha" |-> mkPrimFun1 isAlpha , "primIsSpace" |-> mkPrimFun1 isSpace , "primIsAscii" |-> mkPrimFun1 isAscii , "primIsLatin1" |-> mkPrimFun1 isLatin1 , "primIsPrint" |-> mkPrimFun1 isPrint , "primIsHexDigit" |-> mkPrimFun1 isHexDigit , "primToUpper" |-> mkPrimFun1 toUpper , "primToLower" |-> mkPrimFun1 toLower , "primCharToNat" |-> mkPrimFun1 (fromIntegral . fromEnum :: Char -> Nat) , "primNatToChar" |-> mkPrimFun1 (toEnum . fromIntegral . (`mod` 0x110000) :: Nat -> Char) , "primShowChar" |-> mkPrimFun1 (Str . show . pretty . LitChar noRange) -- String functions , "primStringToList" |-> mkPrimFun1 unStr , "primStringFromList" |-> mkPrimFun1 Str , "primStringAppend" |-> mkPrimFun2 (\s1 s2 -> Str $ unStr s1 ++ unStr s2) , "primStringEquality" |-> mkPrimFun2 ((==) :: Rel Str) , "primShowString" |-> mkPrimFun1 (Str . show . pretty . LitString noRange . unStr) -- Other stuff , "primTrustMe" |-> primTrustMe -- This needs to be force : A → ((x : A) → B x) → B x rather than seq because of call-by-name. , "primForce" |-> primForce , "primForceLemma" |-> primForceLemma , "primQNameEquality" |-> mkPrimFun2 ((==) :: Rel QName) , "primQNameLess" |-> mkPrimFun2 ((<) :: Rel QName) , "primShowQName" |-> mkPrimFun1 (Str . show :: QName -> Str) , "primQNameFixity" |-> mkPrimFun1 (nameFixity . qnameName) , "primMetaEquality" |-> mkPrimFun2 ((==) :: Rel MetaId) , "primMetaLess" |-> mkPrimFun2 ((<) :: Rel MetaId) , "primShowMeta" |-> mkPrimFun1 (Str . show . pretty :: MetaId -> Str) ] where (|->) = (,) floatEq :: Double -> Double -> Bool floatEq x y = identicalIEEE x y || (isNaN x && isNaN y) floatLt :: Double -> Double -> Bool floatLt x y = case compareFloat x y of LT -> True _ -> False where -- Also implemented in the GHC/UHC backends compareFloat :: Double -> Double -> Ordering compareFloat x y | identicalIEEE x y = EQ | isNegInf x = LT | isNegInf y = GT | isNaN x && isNaN y = EQ | isNaN x = LT | isNaN y = GT | otherwise = compare x y isNegInf z = z < 0 && isInfinite z lookupPrimitiveFunction :: String -> TCM PrimitiveImpl lookupPrimitiveFunction x = fromMaybe (typeError $ NoSuchPrimitiveFunction x) (Map.lookup x primitiveFunctions) lookupPrimitiveFunctionQ :: QName -> TCM (String, PrimitiveImpl) lookupPrimitiveFunctionQ q = do let s = case qnameName q of Name _ x _ _ -> prettyShow x PrimImpl t pf <- lookupPrimitiveFunction s return (s, PrimImpl t $ pf { primFunName = q }) getBuiltinName :: String -> TCM (Maybe QName) getBuiltinName b = do caseMaybeM (getBuiltin' b) (return Nothing) (Just <.> getName) where getName v = do v <- reduce v case unSpine $ ignoreSharing v of Def x _ -> return x Con x _ _ -> return $ conName x Lam _ b -> getName $ unAbs b _ -> __IMPOSSIBLE__ isBuiltin :: QName -> String -> TCM Bool isBuiltin q b = (Just q ==) <$> getBuiltinName b Agda-2.5.3/src/full/Agda/TypeChecking/Functions.hs0000644000000000000000000000730213154613124020013 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Functions ( etaExpandClause , getDef ) where import Control.Arrow ( first ) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Context import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Options import Agda.TypeChecking.Level import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.Utils.Impossible import Agda.Utils.Functor ( ($>) ) import Agda.Utils.Pretty ( prettyShow ) import Agda.Utils.Monad import Agda.Utils.Size #include "undefined.h" -- | Expand a clause to the maximal arity, by inserting variable -- patterns and applying the body to variables. -- Fixes issue #2376. -- Replaces 'introHiddenLambdas'. -- See, e.g., test/Succeed/SizedTypesExtendedLambda.agda. -- This is used instead of special treatment of lambdas -- (which was unsound: Issue #121) etaExpandClause :: MonadTCM tcm => Clause -> tcm Clause etaExpandClause clause = liftTCM $ do case clause of Clause _ _ ctel ps _ Nothing _ _ -> return clause Clause _ _ ctel ps Nothing (Just t) _ _ -> return clause Clause rl rf ctel ps (Just body) (Just t) catchall unreachable -> do -- Get the telescope to expand the clause with. TelV tel0 t' <- telView $ unArg t -- If the rhs has lambdas, harvest the names of the bound variables. let xs = peekLambdas body let ltel = useNames xs $ telToList tel0 let tel = telFromList ltel let n = size tel unless (n == size tel0) __IMPOSSIBLE__ -- useNames should not drop anything -- Join with lhs telescope, extend patterns and apply body. -- NB: no need to raise ctel! let ctel' = telFromList $ telToList ctel ++ ltel ps' = raise n ps ++ teleNamedArgs tel body' = raise n body `apply` teleArgs tel reportSDoc "term.clause.expand" 30 $ inTopContext $ vcat [ text "etaExpandClause" , text " body = " <+> (addContext ctel' $ prettyTCM body) , text " xs = " <+> text (prettyShow xs) , text " new tel = " <+> prettyTCM ctel' ] return $ Clause rl rf ctel' ps' (Just body') (Just (t $> t')) catchall unreachable where -- Get all initial lambdas of the body. peekLambdas :: Term -> [Arg ArgName] peekLambdas v = case ignoreSharing v of Lam info b -> Arg info (absName b) : peekLambdas (unAbs b) _ -> [] -- Use the names of the first argument, and set the Origin all other -- parts of the telescope to Inserted. -- The first list of arguments is a subset of the telescope. -- Thus, if compared pointwise, if the hiding does not match, -- it means we skipped an element of the telescope. useNames :: [Arg ArgName] -> ListTel -> ListTel useNames [] tel = map (setOrigin Inserted) tel useNames (_:_) [] = [] -- Andreas, 2017-03-24: not IMPOSSIBLE when positivity checking comes before termination checking, see examples/tactics/ac/AC.agda useNames (x:xs) (dom:tel) | sameHiding x dom = -- set the ArgName of the dom fmap (first $ const $ unArg x) dom : useNames xs tel | otherwise = setOrigin Inserted dom : useNames (x:xs) tel -- | Get the name of defined symbol of the head normal form of a term. -- Returns 'Nothing' if no such head exists. getDef :: Term -> TCM (Maybe QName) getDef t = (ignoreSharing <$> reduce t) >>= \case Def d _ -> return $ Just d Lam _ v -> underAbstraction_ v getDef Level v -> getDef =<< reallyUnLevelView v DontCare v -> getDef v _ -> return Nothing Agda-2.5.3/src/full/Agda/TypeChecking/InstanceArguments.hs0000644000000000000000000006466013154613124021507 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.InstanceArguments where import Control.Applicative hiding (empty) import Control.Monad.Reader import Control.Monad.State import qualified Data.Map as Map import qualified Data.Set as Set import qualified Data.List as List import Agda.Syntax.Common import Agda.Syntax.Internal as I import Agda.Syntax.Scope.Base (isNameInScope) import Agda.TypeChecking.Errors () import Agda.TypeChecking.Implicit (implicitArgs) import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce import Agda.TypeChecking.Records import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Free import {-# SOURCE #-} Agda.TypeChecking.Constraints import {-# SOURCE #-} Agda.TypeChecking.MetaVars import {-# SOURCE #-} Agda.TypeChecking.Conversion import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.Lens import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Functor import Agda.Utils.Pretty (prettyShow) import Agda.Utils.Null (empty) #include "undefined.h" import Agda.Utils.Impossible -- | Compute a list of instance candidates. -- 'Nothing' if type is a meta, error if type is not eligible -- for instance search. initialIFSCandidates :: Type -> TCM (Maybe [Candidate]) initialIFSCandidates t = do otn <- getOutputTypeName t case otn of NoOutputTypeName -> typeError $ GenericError $ "Instance search can only be used to find elements in a named type" OutputTypeNameNotYetKnown -> return Nothing OutputTypeVar -> Just <$> getContextVars OutputTypeName n -> Just <$> do (++) <$> getContextVars <*> getScopeDefs n where -- get a list of variables with their type, relative to current context getContextVars :: TCM [Candidate] getContextVars = do ctx <- getContext reportSDoc "tc.instance.cands" 40 $ hang (text "Getting candidates from context") 2 (inTopContext $ prettyTCM ctx) -- Context variables with their types lifted to live in the full context let varsAndRaisedTypes = [ (var i, raise (i + 1) t) | (i, t) <- zip [0..] ctx ] vars = [ Candidate x t ExplicitStayExplicit (isOverlappable info) | (x, Dom info (_, t)) <- varsAndRaisedTypes , isInstance info , not (unusableRelevance $ argInfoRelevance info) ] -- {{}}-fields of variables are also candidates let cxtAndTypes = [ (x, t) | (x, Dom _ (_, t)) <- varsAndRaisedTypes ] fields <- concat <$> mapM instanceFields (reverse cxtAndTypes) reportSDoc "tc.instance.fields" 30 $ if null fields then text "no instance field candidates" else text "instance field candidates" $$ do nest 2 $ vcat [ sep [ (if overlap then text "overlap" else empty) <+> prettyTCM v <+> text ":" , nest 2 $ prettyTCM t ] | Candidate v t _ overlap <- fields ] -- get let bindings env <- asks envLetBindings env <- mapM (getOpen . snd) $ Map.toList env let lets = [ Candidate v t ExplicitStayExplicit False | (v, Dom info t) <- env , isInstance info , not (unusableRelevance $ argInfoRelevance info) ] return $ vars ++ fields ++ lets etaExpand etaOnce t = isEtaRecordType t >>= \case Nothing | etaOnce -> do isRecordType t >>= \case Nothing -> return Nothing Just (r, vs, _) -> do m <- currentModule -- Are we inside the record module? If so it's safe and desirable -- to eta-expand once (issue #2320). if qnameToList r `List.isPrefixOf` mnameToList m then return (Just (r, vs)) else return Nothing r -> return r instanceFields = instanceFields' True instanceFields' etaOnce (v, t) = caseMaybeM (etaExpand etaOnce =<< reduce t) (return []) $ \ (r, pars) -> do (tel, args) <- forceEtaExpandRecord r pars v let types = map unDom $ applySubst (parallelS $ reverse $ map unArg args) (flattenTel tel) fmap concat $ forM (zip args types) $ \ (arg, t) -> ([ Candidate (unArg arg) t ExplicitStayExplicit (isOverlappable arg) | isInstance arg ] ++) <$> instanceFields' False (unArg arg, t) getScopeDefs :: QName -> TCM [Candidate] getScopeDefs n = do instanceDefs <- getInstanceDefs rel <- asks envRelevance let qs = maybe [] Set.toList $ Map.lookup n instanceDefs catMaybes <$> mapM (candidate rel) qs candidate :: Relevance -> QName -> TCM (Maybe Candidate) candidate rel q = ifNotM (isNameInScope q <$> getScope) (return Nothing) $ do -- Andreas, 2012-07-07: -- we try to get the info for q -- while opening a module, q may be in scope but not in the signature -- in this case, we just ignore q (issue 674) flip catchError handle $ do def <- getConstInfo q if not (defRelevance def `moreRelevant` rel) then return Nothing else do -- Andreas, 2017-01-14: instantiateDef is a bit of an overkill -- if we anyway get the freeVarsToApply -- WAS: t <- defType <$> instantiateDef def args <- freeVarsToApply q let t = defType def `piApply` args let v = case theDef def of -- drop parameters if it's a projection function... Function{ funProjection = Just p } -> projDropParsApply p ProjSystem args -- Andreas, 2014-08-19: constructors cannot be declared as -- instances (at least as of now). -- I do not understand why the Constructor case is not impossible. -- Ulf, 2014-08-20: constructors are always instances. Constructor{ conSrcCon = c } -> Con c ConOSystem [] _ -> Def q $ map Apply args return $ Just $ Candidate v t ExplicitToInstance False where -- unbound constant throws an internal error handle (TypeError _ (Closure {clValue = InternalError _})) = return Nothing handle err = throwError err -- | @findInScope m (v,a)s@ tries to instantiate on of the types @a@s -- of the candidate terms @v@s to the type @t@ of the metavariable @m@. -- If successful, meta @m@ is solved with the instantiation of @v@. -- If unsuccessful, the constraint is regenerated, with possibly reduced -- candidate set. -- The list of candidates is equal to @Nothing@ when the type of the meta -- wasn't known when the constraint was generated. In that case, try to find -- its type again. findInScope :: MetaId -> Maybe [Candidate] -> TCM () findInScope m Nothing = do -- Andreas, 2015-02-07: New metas should be created with range of the -- current instance meta, thus, we set the range. mv <- lookupMeta m setCurrentRange mv $ do reportSLn "tc.instance" 20 $ "The type of the FindInScope constraint isn't known, trying to find it again." t <- instantiate =<< getMetaTypeInContext m reportSLn "tc.instance" 70 $ "findInScope 1: t: " ++ prettyShow t -- Issue #2577: If the target is a function type the arguments are -- potential candidates, so we add them to the context to make -- initialIFSCandidates pick them up. TelV tel t <- telView t cands <- addContext' tel $ initialIFSCandidates t case cands of Nothing -> do reportSLn "tc.instance" 20 "Can't figure out target of instance goal. Postponing constraint." addConstraint $ FindInScope m Nothing Nothing Just {} -> findInScope m cands findInScope m (Just cands) = whenJustM (findInScope' m cands) $ (\ (cands, b) -> addConstraint $ FindInScope m b $ Just cands) -- | Result says whether we need to add constraint, and if so, the set of -- remaining candidates and an eventual blocking metavariable. findInScope' :: MetaId -> [Candidate] -> TCM (Maybe ([Candidate], Maybe MetaId)) findInScope' m cands = ifM (isFrozen m) (do reportSLn "tc.instance" 20 "Refusing to solve frozen instance meta." return (Just (cands, Nothing))) $ do -- Andreas, 2013-12-28 issue 1003: -- If instance meta is already solved, simply discard the constraint. -- Ulf, 2016-12-06 issue 2325: But only if *fully* instantiated. ifM (isFullyInstantiatedMeta m) (Nothing <$ reportSLn "tc.instance" 20 "Instance meta already solved.") $ do -- Andreas, 2015-02-07: New metas should be created with range of the -- current instance meta, thus, we set the range. mv <- lookupMeta m setCurrentRange mv $ do reportSLn "tc.instance" 15 $ "findInScope 2: constraint: " ++ prettyShow m ++ "; candidates left: " ++ show (length cands) reportSDoc "tc.instance" 60 $ nest 2 $ vcat [ sep [ (if overlap then text "overlap" else empty) <+> prettyTCM v <+> text ":" , nest 2 $ prettyTCM t ] | Candidate v t _ overlap <- cands ] reportSDoc "tc.instance" 70 $ text "raw" $$ do nest 2 $ vcat [ sep [ (if overlap then text "overlap" else empty) <+> pretty v <+> text ":" , nest 2 $ pretty t ] | Candidate v t _ overlap <- cands ] t <- normalise =<< getMetaTypeInContext m reportSLn "tc.instance" 70 $ "findInScope 2: t: " ++ prettyShow t insidePi t $ \ t -> do reportSDoc "tc.instance" 15 $ text "findInScope 3: t =" <+> prettyTCM t reportSLn "tc.instance" 70 $ "findInScope 3: t: " ++ prettyShow t -- If one of the arguments of the typeclass is a meta which is not rigidly -- constrained, then don’t do anything because it may loop. let abortNonRigid m = do reportSLn "tc.instance" 15 $ "aborting due to non-rigidly constrained meta " ++ show m return $ Just (cands, Just m) ifJustM (areThereNonRigidMetaArguments (unEl t)) abortNonRigid $ {-else-} do mcands <- checkCandidates m t cands debugConstraints case mcands of Just [] -> do reportSDoc "tc.instance" 15 $ text "findInScope 5: not a single candidate found..." typeError $ IFSNoCandidateInScope t Just [Candidate term t' _ _] -> do reportSDoc "tc.instance" 15 $ vcat [ text "findInScope 5: solved by instance search using the only candidate" , nest 2 $ prettyTCM term , text "of type " <+> prettyTCM t' , text "for type" <+> prettyTCM t ] -- If we actually solved the constraints we should wake up any held -- instance constraints, to make sure we don't forget about them. wakeConstraints (return . const True) solveAwakeConstraints' False return Nothing -- We’re done _ -> do let cs = fromMaybe cands mcands -- keep the current candidates if Nothing reportSDoc "tc.instance" 15 $ text ("findInScope 5: refined candidates: ") <+> prettyTCM (List.map candidateTerm cs) return (Just (cs, Nothing)) -- | Precondition: type is spine reduced and ends in a Def or a Var. insidePi :: Type -> (Type -> TCM a) -> TCM a insidePi t ret = case ignoreSharing $ unEl t of Pi a b -> addContext' (absName b, a) $ insidePi (absBody b) ret Def{} -> ret t Var{} -> ret t Sort{} -> __IMPOSSIBLE__ Con{} -> __IMPOSSIBLE__ Lam{} -> __IMPOSSIBLE__ Lit{} -> __IMPOSSIBLE__ Level{} -> __IMPOSSIBLE__ MetaV{} -> __IMPOSSIBLE__ Shared{} -> __IMPOSSIBLE__ DontCare{} -> __IMPOSSIBLE__ -- | A meta _M is rigidly constrained if there is a constraint _M us == D vs, -- for inert D. Such metas can safely be instantiated by recursive instance -- search, since the constraint limits the solution space. rigidlyConstrainedMetas :: TCM [MetaId] rigidlyConstrainedMetas = do cs <- (++) <$> use stSleepingConstraints <*> use stAwakeConstraints catMaybes <$> mapM rigidMetas cs where isRigid v = do bv <- reduceB v case ignoreSharing <$> bv of Blocked{} -> return False NotBlocked _ v -> case v of MetaV{} -> return False Def f _ -> return True Con{} -> return True Lit{} -> return True Var{} -> return True Sort{} -> return True Pi{} -> return True Level{} -> return False DontCare{} -> return False Lam{} -> __IMPOSSIBLE__ Shared{} -> __IMPOSSIBLE__ rigidMetas c = case clValue $ theConstraint c of ValueCmp _ _ u v -> case (ignoreSharing u, ignoreSharing v) of (MetaV m us, _) | isJust (allApplyElims us) -> ifM (isRigid v) (return $ Just m) (return Nothing) (_, MetaV m vs) | isJust (allApplyElims vs) -> ifM (isRigid u) (return $ Just m) (return Nothing) _ -> return Nothing ElimCmp{} -> return Nothing TypeCmp{} -> return Nothing TelCmp{} -> return Nothing SortCmp{} -> return Nothing LevelCmp{} -> return Nothing UnBlock{} -> return Nothing Guarded{} -> return Nothing -- don't look inside Guarded, since the inner constraint might not fire IsEmpty{} -> return Nothing CheckSizeLtSat{} -> return Nothing FindInScope{} -> return Nothing isRigid :: MetaId -> TCM Bool isRigid i = do rigid <- rigidlyConstrainedMetas return (elem i rigid) -- | Returns True if one of the arguments of @t@ is a meta which isn’t rigidly -- constrained. Note that level metas are never considered rigidly constrained -- (#1865). areThereNonRigidMetaArguments :: Term -> TCM (Maybe MetaId) areThereNonRigidMetaArguments t = case ignoreSharing t of Def n args -> do TelV tel _ <- telView . defType =<< getConstInfo n let varOccs EmptyTel = [] varOccs (ExtendTel a btel) | getRelevance a == Irrelevant = WeaklyRigid : varOccs tel -- #2171: ignore irrelevant arguments | otherwise = occurrence 0 tel : varOccs tel where tel = unAbs btel rigid StronglyRigid = True rigid Unguarded = True rigid WeaklyRigid = True rigid _ = False reportSDoc "tc.instance.rigid" 70 $ text "class args:" <+> prettyTCM tel $$ nest 2 (text $ "used: " ++ show (varOccs tel)) areThereNonRigidMetaArgs [ arg | (o, arg) <- zip (varOccs tel) args, not $ rigid o ] Var n args -> return Nothing -- TODO check what’s the right thing to do, doing the same -- thing as above makes some examples fail Sort{} -> __IMPOSSIBLE__ Con{} -> __IMPOSSIBLE__ Lam{} -> __IMPOSSIBLE__ Lit{} -> __IMPOSSIBLE__ Level{} -> __IMPOSSIBLE__ MetaV{} -> __IMPOSSIBLE__ Pi{} -> __IMPOSSIBLE__ Shared{} -> __IMPOSSIBLE__ DontCare{} -> __IMPOSSIBLE__ where areThereNonRigidMetaArgs :: Elims -> TCM (Maybe MetaId) areThereNonRigidMetaArgs [] = return Nothing areThereNonRigidMetaArgs (Proj{} : xs) = areThereNonRigidMetaArgs xs areThereNonRigidMetaArgs (Apply x : xs) = do ifJustM (isNonRigidMeta $ unArg x) (return . Just) (areThereNonRigidMetaArgs xs) isNonRigidMeta :: Term -> TCM (Maybe MetaId) isNonRigidMeta v = case ignoreSharing v of Def _ es -> areThereNonRigidMetaArgs es Var _ es -> areThereNonRigidMetaArgs es Con _ _ vs-> areThereNonRigidMetaArgs (map Apply vs) MetaV i _ -> ifM (isRigid i) (return Nothing) $ do -- Ignore unconstrained level and size metas (#1865) mlvl <- getBuiltinDefName builtinLevel (msz, mszlt) <- getBuiltinSize let ok = catMaybes [ mlvl, msz ] -- , mszlt ] -- ?! Andreas, 2016-12-22 -- @Ulf: are SIZELT metas excluded on purpose? -- How to you know the level/size meta is unconstrained? o <- getOutputTypeName . jMetaType . mvJudgement =<< lookupMeta i case o of OutputTypeName l | elem l ok -> return Nothing _ -> return $ Just i Lam _ t -> isNonRigidMeta (unAbs t) _ -> return Nothing -- | Apply the computation to every argument in turn by reseting the state every -- time. Return the list of the arguments giving the result True. -- -- If the resulting list contains exactly one element, then the state is the -- same as the one obtained after running the corresponding computation. In -- all the other cases, the state is reseted. filterResetingState :: MetaId -> [Candidate] -> (Candidate -> TCM YesNoMaybe) -> TCM [Candidate] filterResetingState m cands f = disableDestructiveUpdate $ do ctxArgs <- getContextArgs let ctxElims = map Apply ctxArgs tryC c = do ok <- f c v <- instantiateFull (MetaV m ctxElims) a <- instantiateFull =<< (`piApplyM` ctxArgs) =<< getMetaType m return (ok, v, a) result <- mapM (\c -> do bs <- localTCStateSaving (tryC c); return (c, bs)) cands -- Check that there aren't any hard failures case [ err | (_, ((HellNo err, _, _), _)) <- result ] of err : _ -> throwError err [] -> return () let result' = [ (c, v, a, s) | (c, ((r, v, a), s)) <- result, not (isNo r) ] noMaybes = null [ Maybe | (_, ((Maybe, _, _), _)) <- result ] -- It's not safe to compare maybes for equality because they might -- not have instantiated at all. result <- if noMaybes then dropSameCandidates m result' else return result' case result of [(c, _, _, s)] -> [c] <$ put s _ -> return [ c | (c, _, _, _) <- result ] -- Drop all candidates which are judgmentally equal to the first one. -- This is sufficient to reduce the list to a singleton should all be equal. dropSameCandidates :: MetaId -> [(Candidate, Term, Type, a)] -> TCM [(Candidate, Term, Type, a)] dropSameCandidates m cands0 = verboseBracket "tc.instance" 30 "dropSameCandidates" $ do metas <- Set.fromList . Map.keys <$> getMetaStore let freshMetas x = not $ Set.null $ Set.difference (Set.fromList $ allMetas x) metas -- Take overlappable candidates into account let cands = case List.partition (\ (c, _, _, _) -> candidateOverlappable c) cands0 of (cand : _, []) -> [cand] -- only overlappable candidates: pick the first one _ -> cands0 -- otherwise require equality reportSDoc "tc.instance" 50 $ vcat [ text "valid candidates:" , nest 2 $ vcat [ if freshMetas (v, a) then text "(redacted)" else sep [ prettyTCM v <+> text ":", nest 2 $ prettyTCM a ] | (_, v, a, _) <- cands ] ] rel <- getMetaRelevance <$> lookupMeta m case cands of [] -> return cands cvd : _ | isIrrelevant rel -> do reportSLn "tc.instance" 30 "Meta is irrelevant so any candidate will do." return [cvd] cvd@(_, v, a, _) : vas -> do if freshMetas (v, a) then return (cvd : vas) else (cvd :) <$> dropWhileM equal vas where equal (_, v', a', _) | freshMetas (v', a') = return False -- If there are fresh metas we can't compare | otherwise = verboseBracket "tc.instance" 30 "comparingCandidates" $ do reportSDoc "tc.instance" 30 $ sep [ prettyTCM v <+> text "==", nest 2 $ prettyTCM v' ] localTCState $ dontAssignMetas $ ifNoConstraints_ (equalType a a' >> equalTerm a v v') {- then -} (return True) {- else -} (\ _ -> return False) `catchError` (\ _ -> return False) data YesNoMaybe = Yes | No | Maybe | HellNo TCErr deriving (Show) isNo :: YesNoMaybe -> Bool isNo No = True isNo _ = False -- | Given a meta @m@ of type @t@ and a list of candidates @cands@, -- @checkCandidates m t cands@ returns a refined list of valid candidates. checkCandidates :: MetaId -> Type -> [Candidate] -> TCM (Maybe [Candidate]) checkCandidates m t cands = disableDestructiveUpdate $ verboseBracket "tc.instance.candidates" 20 ("checkCandidates " ++ prettyShow m) $ ifM (anyMetaTypes cands) (return Nothing) $ holdConstraints (\ _ -> isIFSConstraint . clValue . theConstraint) $ Just <$> do reportSDoc "tc.instance.candidates" 20 $ nest 2 $ text "target:" <+> prettyTCM t reportSDoc "tc.instance.candidates" 20 $ nest 2 $ vcat [ text "candidates" , vcat [ text "-" <+> (if overlap then text "overlap" else empty) <+> prettyTCM v <+> text ":" <+> prettyTCM t | Candidate v t _ overlap <- cands ] ] cands' <- filterResetingState m cands (checkCandidateForMeta m t) reportSDoc "tc.instance.candidates" 20 $ nest 2 $ vcat [ text "valid candidates" , vcat [ text "-" <+> (if overlap then text "overlap" else empty) <+> prettyTCM v <+> text ":" <+> prettyTCM t | Candidate v t _ overlap <- cands' ] ] return cands' where anyMetaTypes :: [Candidate] -> TCM Bool anyMetaTypes [] = return False anyMetaTypes (Candidate _ a _ _ : cands) = do a <- instantiate a case ignoreSharing $ unEl a of MetaV{} -> return True _ -> anyMetaTypes cands checkDepth :: Term -> Type -> TCM YesNoMaybe -> TCM YesNoMaybe checkDepth c a k = locally eInstanceDepth succ $ do d <- view eInstanceDepth maxDepth <- maxInstanceSearchDepth when (d > maxDepth) $ typeError $ InstanceSearchDepthExhausted c a maxDepth k checkCandidateForMeta :: MetaId -> Type -> Candidate -> TCM YesNoMaybe checkCandidateForMeta m t (Candidate term t' eti _) = checkDepth term t' $ do -- Andreas, 2015-02-07: New metas should be created with range of the -- current instance meta, thus, we set the range. mv <- lookupMeta m setCurrentRange mv $ do debugConstraints verboseBracket "tc.instance" 20 ("checkCandidateForMeta " ++ prettyShow m) $ liftTCM $ runCandidateCheck $ do reportSLn "tc.instance" 70 $ " t: " ++ prettyShow t ++ "\n t':" ++ prettyShow t' ++ "\n term: " ++ prettyShow term ++ "." reportSDoc "tc.instance" 20 $ vcat [ text "checkCandidateForMeta" , text "t =" <+> prettyTCM t , text "t' =" <+> prettyTCM t' , text "term =" <+> prettyTCM term ] -- Apply hidden and instance arguments (recursive inst. search!). (args, t'') <- implicitArgs (-1) (\h -> notVisible h || eti == ExplicitToInstance) t' reportSDoc "tc.instance" 20 $ text "instance search: checking" <+> prettyTCM t'' <+> text "<=" <+> prettyTCM t reportSDoc "tc.instance" 70 $ vcat [ text "instance search: checking (raw)" , nest 4 $ pretty t'' , nest 2 $ text "<=" , nest 4 $ pretty t ] v <- (`applyDroppingParameters` args) =<< reduce term reportSDoc "tc.instance" 15 $ vcat [ text "instance search: attempting" , nest 2 $ prettyTCM m <+> text ":=" <+> prettyTCM v ] reportSDoc "tc.instance" 70 $ nest 2 $ text "candidate v = " <+> pretty v -- if constraints remain, we abort, but keep the candidate -- Jesper, 05-12-2014: When we abort, we should add a constraint to -- instantiate the meta at a later time (see issue 1377). ctxElims <- map Apply <$> getContextArgs guardConstraint (ValueCmp CmpEq t'' (MetaV m ctxElims) v) $ leqType t'' t -- make a pass over constraints, to detect cases where some are made -- unsolvable by the assignment, but don't do this for FindInScope's -- to prevent loops. We currently also ignore UnBlock constraints -- to be on the safe side. debugConstraints solveAwakeConstraints' True verboseS "tc.instance" 15 $ do sol <- instantiateFull (MetaV m ctxElims) case sol of MetaV m' _ | m == m' -> reportSDoc "tc.instance" 15 $ sep [ text "instance search: maybe solution for" <+> prettyTCM m <> text ":" , nest 2 $ prettyTCM v ] _ -> reportSDoc "tc.instance" 15 $ sep [ text "instance search: found solution for" <+> prettyTCM m <> text ":" , nest 2 $ prettyTCM sol ] where runCandidateCheck check = flip catchError handle $ ifNoConstraints_ check (return Yes) (\ _ -> Maybe <$ reportSLn "tc.instance" 50 "assignment inconclusive") hardFailure :: TCErr -> Bool hardFailure (TypeError _ err) = case clValue err of InstanceSearchDepthExhausted{} -> True _ -> False hardFailure _ = False handle :: TCErr -> TCM YesNoMaybe handle err | hardFailure err = return $ HellNo err | otherwise = do reportSDoc "tc.instance" 50 $ text "assignment failed:" <+> prettyTCM err return No isIFSConstraint :: Constraint -> Bool isIFSConstraint FindInScope{} = True isIFSConstraint UnBlock{} = True -- otherwise test/fail/Issue723 loops isIFSConstraint _ = False -- | To preserve the invariant that a constructor is not applied to its -- parameter arguments, we explicitly check whether function term -- we are applying to arguments is a unapplied constructor. -- In this case we drop the first 'conPars' arguments. -- See Issue670a. -- Andreas, 2013-11-07 Also do this for projections, see Issue670b. applyDroppingParameters :: Term -> Args -> TCM Term applyDroppingParameters t vs = do let fallback = return $ t `apply` vs case ignoreSharing t of Con c ci [] -> do def <- theDef <$> getConInfo c case def of Constructor {conPars = n} -> return $ Con c ci (drop n vs) _ -> __IMPOSSIBLE__ Def f [] -> do mp <- isProjection f case mp of Just Projection{projIndex = n} -> do case drop n vs of [] -> return t u : us -> (`apply` us) <$> applyDef ProjPrefix f u _ -> fallback _ -> fallback Agda-2.5.3/src/full/Agda/TypeChecking/Quote.hs0000644000000000000000000003033613154613124017143 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Quote where import Control.Applicative import Control.Arrow ((&&&)) import Control.Monad import Control.Monad.State (runState, get, put) import Control.Monad.Reader (asks) import Control.Monad.Writer (execWriterT, tell) import Control.Monad.Trans (lift) import Data.Char import Data.Maybe (fromMaybe) import Data.Traversable (traverse) import Agda.Syntax.Common import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Pattern ( dbPatPerm' ) import Agda.Syntax.Literal import Agda.Syntax.Position import Agda.Syntax.Translation.InternalToAbstract import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.DropArgs import Agda.TypeChecking.Free import Agda.TypeChecking.Level import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce import Agda.TypeChecking.Reduce.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.Utils.Except import Agda.Utils.Impossible import Agda.Utils.Monad ( ifM ) import Agda.Utils.Permutation ( Permutation(Perm), compactP, reverseP ) import Agda.Utils.String ( Str(Str), unStr ) import Agda.Utils.VarSet (VarSet) import qualified Agda.Utils.VarSet as Set import Agda.Utils.FileName #include "undefined.h" data QuotingKit = QuotingKit { quoteTermWithKit :: Term -> ReduceM Term , quoteTypeWithKit :: Type -> ReduceM Term , quoteClauseWithKit :: Clause -> ReduceM Term , quoteDomWithKit :: Dom Type -> ReduceM Term , quoteDefnWithKit :: Definition -> ReduceM Term , quoteListWithKit :: forall a. (a -> ReduceM Term) -> [a] -> ReduceM Term } quotingKit :: TCM QuotingKit quotingKit = do currentFile <- fromMaybe __IMPOSSIBLE__ <$> asks envCurrentPath hidden <- primHidden instanceH <- primInstance visible <- primVisible relevant <- primRelevant irrelevant <- primIrrelevant nil <- primNil cons <- primCons abs <- primAbsAbs arg <- primArgArg arginfo <- primArgArgInfo var <- primAgdaTermVar lam <- primAgdaTermLam extlam <- primAgdaTermExtLam def <- primAgdaTermDef con <- primAgdaTermCon pi <- primAgdaTermPi sort <- primAgdaTermSort meta <- primAgdaTermMeta lit <- primAgdaTermLit litNat <- primAgdaLitNat litFloat <- primAgdaLitFloat litChar <- primAgdaLitChar litString <- primAgdaLitString litQName <- primAgdaLitQName litMeta <- primAgdaLitMeta normalClause <- primAgdaClauseClause absurdClause <- primAgdaClauseAbsurd varP <- primAgdaPatVar conP <- primAgdaPatCon dotP <- primAgdaPatDot litP <- primAgdaPatLit projP <- primAgdaPatProj absurdP <- primAgdaPatAbsurd set <- primAgdaSortSet setLit <- primAgdaSortLit unsupportedSort <- primAgdaSortUnsupported sucLevel <- primLevelSuc lub <- primLevelMax lkit <- requireLevels Con z _ _ <- ignoreSharing <$> primZero Con s _ _ <- ignoreSharing <$> primSuc unsupported <- primAgdaTermUnsupported agdaDefinitionFunDef <- primAgdaDefinitionFunDef agdaDefinitionDataDef <- primAgdaDefinitionDataDef agdaDefinitionRecordDef <- primAgdaDefinitionRecordDef agdaDefinitionPostulate <- primAgdaDefinitionPostulate agdaDefinitionPrimitive <- primAgdaDefinitionPrimitive agdaDefinitionDataConstructor <- primAgdaDefinitionDataConstructor let (@@) :: Apply a => ReduceM a -> ReduceM Term -> ReduceM a t @@ u = apply <$> t <*> ((:[]) . defaultArg <$> u) (!@) :: Apply a => a -> ReduceM Term -> ReduceM a t !@ u = pure t @@ u (!@!) :: Apply a => a -> Term -> ReduceM a t !@! u = pure t @@ pure u quoteHiding :: Hiding -> ReduceM Term quoteHiding Hidden = pure hidden quoteHiding Instance{} = pure instanceH quoteHiding NotHidden = pure visible quoteRelevance :: Relevance -> ReduceM Term quoteRelevance Relevant = pure relevant quoteRelevance Irrelevant = pure irrelevant quoteRelevance NonStrict = pure relevant quoteRelevance Forced{} = pure relevant quoteArgInfo :: ArgInfo -> ReduceM Term quoteArgInfo (ArgInfo h r _) = arginfo !@ quoteHiding h @@ quoteRelevance r quoteLit :: Literal -> ReduceM Term quoteLit l@LitNat{} = litNat !@! Lit l quoteLit l@LitFloat{} = litFloat !@! Lit l quoteLit l@LitChar{} = litChar !@! Lit l quoteLit l@LitString{} = litString !@! Lit l quoteLit l@LitQName{} = litQName !@! Lit l quoteLit l@LitMeta {} = litMeta !@! Lit l -- We keep no ranges in the quoted term, so the equality on terms -- is only on the structure. quoteSortLevelTerm :: Level -> ReduceM Term quoteSortLevelTerm (Max []) = setLit !@! Lit (LitNat noRange 0) quoteSortLevelTerm (Max [ClosedLevel n]) = setLit !@! Lit (LitNat noRange n) quoteSortLevelTerm l = set !@ quoteTerm (unlevelWithKit lkit l) quoteSort :: Sort -> ReduceM Term quoteSort (Type t) = quoteSortLevelTerm t quoteSort Prop = pure unsupportedSort quoteSort Inf = pure unsupportedSort quoteSort SizeUniv = pure unsupportedSort quoteSort DLub{} = pure unsupportedSort quoteType :: Type -> ReduceM Term quoteType (El _ t) = quoteTerm t quoteQName :: QName -> ReduceM Term quoteQName x = pure $ Lit $ LitQName noRange x quotePats :: [NamedArg DeBruijnPattern] -> ReduceM Term quotePats ps = list $ map (quoteArg quotePat . fmap namedThing) ps quotePat :: DeBruijnPattern -> ReduceM Term quotePat (AbsurdP x) = pure absurdP quotePat (VarP x) = varP !@! quoteString (dbPatVarName x) quotePat (DotP _) = pure dotP quotePat (ConP c _ ps) = conP !@ quoteQName (conName c) @@ quotePats ps quotePat (LitP l) = litP !@ quoteLit l quotePat (ProjP _ x) = projP !@ quoteQName x quoteClause :: Clause -> ReduceM Term quoteClause cl@Clause{namedClausePats = ps, clauseBody = body} = case body of Nothing -> absurdClause !@ quotePats ps Just b -> let perm = fromMaybe __IMPOSSIBLE__ $ dbPatPerm' False ps -- Dot patterns don't count (#2203) v = applySubst (renamingR perm) b in normalClause !@ quotePats ps @@ quoteTerm v list :: [ReduceM Term] -> ReduceM Term list [] = pure nil list (a : as) = cons !@ a @@ list as quoteList :: (a -> ReduceM Term) -> [a] -> ReduceM Term quoteList q xs = list (map q xs) quoteDom :: (Type -> ReduceM Term) -> Dom Type -> ReduceM Term quoteDom q (Dom info t) = arg !@ quoteArgInfo info @@ q t quoteAbs :: Subst t a => (a -> ReduceM Term) -> Abs a -> ReduceM Term quoteAbs q (Abs s t) = abs !@! quoteString s @@ q t quoteAbs q (NoAbs s t) = abs !@! quoteString s @@ q (raise 1 t) quoteArg :: (a -> ReduceM Term) -> Arg a -> ReduceM Term quoteArg q (Arg info t) = arg !@ quoteArgInfo info @@ q t quoteArgs :: Args -> ReduceM Term quoteArgs ts = list (map (quoteArg quoteTerm) ts) quoteTerm :: Term -> ReduceM Term quoteTerm v = case unSpine v of Var n es -> let ts = fromMaybe __IMPOSSIBLE__ $ allApplyElims es in var !@! Lit (LitNat noRange $ fromIntegral n) @@ quoteArgs ts Lam info t -> lam !@ quoteHiding (getHiding info) @@ quoteAbs quoteTerm t Def x es -> do defn <- getConstInfo x n <- getDefFreeVars x -- #2220: remember to restore dropped parameters let conOrProjPars = defParameters defn ts = fromMaybe __IMPOSSIBLE__ $ allApplyElims es qx Function{ funExtLam = Just (ExtLamInfo h nh), funClauses = cs } = do -- An extended lambda should not have any extra parameters! unless (null conOrProjPars) __IMPOSSIBLE__ -- Andreas, 2017-01-23 quoting Ulf -- "One would hope that @n@ includes the @h + nh@ parameters of the ext.lam." -- Let's see! -- unless (n >= h + nh) __IMPOSSIBLE__ -- Actually, no, it does not! ExtLam is not touched by module application. -- TODO: fixe me! See #2404. extlam !@ list (map (quoteClause . (`apply` (take (h + nh) ts))) cs) qx Function{ funCompiled = Just Fail, funClauses = [cl] } = extlam !@ list [quoteClause $ dropArgs (length (namedClausePats cl) - 1) cl] qx _ = def !@! quoteName x qx (theDef defn) @@ list (drop n $ conOrProjPars ++ map (quoteArg quoteTerm) ts) Con x ci ts -> do cDef <- getConstInfo (conName x) n <- getDefFreeVars (conName x) let args = list $ drop n $ defParameters cDef ++ map (quoteArg quoteTerm) ts con !@! quoteConName x @@ args Pi t u -> pi !@ quoteDom quoteType t @@ quoteAbs quoteType u Level l -> quoteTerm (unlevelWithKit lkit l) Lit l -> lit !@ quoteLit l Sort s -> sort !@ quoteSort s Shared p -> quoteTerm $ derefPtr p MetaV x es -> meta !@! quoteMeta currentFile x @@ quoteArgs vs where vs = fromMaybe __IMPOSSIBLE__ $ allApplyElims es DontCare{} -> pure unsupported -- could be exposed at some point but we have to take care defParameters :: Definition -> [ReduceM Term] defParameters def = map par hiding where np = case theDef def of Constructor{ conPars = np } -> np Function{ funProjection = Just p } -> projIndex p - 1 _ -> 0 TelV tel _ = telView' (defType def) hiding = map (getHiding &&& getRelevance) $ take np $ telToList tel par (h, r) = arg !@ (arginfo !@ quoteHiding h @@ quoteRelevance r) @@ pure unsupported quoteDefn :: Definition -> ReduceM Term quoteDefn def = case theDef def of Function{funClauses = cs} -> agdaDefinitionFunDef !@ quoteList quoteClause cs Datatype{dataPars = np, dataCons = cs} -> agdaDefinitionDataDef !@! quoteNat (fromIntegral np) @@ quoteList (pure . quoteName) cs Record{recConHead = c, recFields = fs} -> agdaDefinitionRecordDef !@! quoteName (conName c) @@ quoteList (quoteArg (pure . quoteName)) fs Axiom{} -> pure agdaDefinitionPostulate AbstractDefn{}-> pure agdaDefinitionPostulate Primitive{primClauses = cs} | not $ null cs -> agdaDefinitionFunDef !@ quoteList quoteClause cs Primitive{} -> pure agdaDefinitionPrimitive Constructor{conData = d} -> agdaDefinitionDataConstructor !@! quoteName d return $ QuotingKit quoteTerm quoteType quoteClause (quoteDom quoteType) quoteDefn quoteList quoteString :: String -> Term quoteString = Lit . LitString noRange quoteName :: QName -> Term quoteName x = Lit (LitQName noRange x) quoteNat :: Integer -> Term quoteNat n | n >= 0 = Lit (LitNat noRange n) | otherwise = __IMPOSSIBLE__ quoteConName :: ConHead -> Term quoteConName = quoteName . conName quoteMeta :: AbsolutePath -> MetaId -> Term quoteMeta file = Lit . LitMeta noRange file quoteTerm :: Term -> TCM Term quoteTerm v = do kit <- quotingKit runReduceM (quoteTermWithKit kit v) quoteType :: Type -> TCM Term quoteType v = do kit <- quotingKit runReduceM (quoteTypeWithKit kit v) quoteDom :: Dom Type -> TCM Term quoteDom v = do kit <- quotingKit runReduceM (quoteDomWithKit kit v) quoteDefn :: Definition -> TCM Term quoteDefn def = do kit <- quotingKit runReduceM (quoteDefnWithKit kit def) quoteList :: [Term] -> TCM Term quoteList xs = do kit <- quotingKit runReduceM (quoteListWithKit kit pure xs) Agda-2.5.3/src/full/Agda/TypeChecking/Telescope.hs0000644000000000000000000003507513154613124017776 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Telescope where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad (unless, guard) import Data.Foldable (forM_) import Data.IntSet (IntSet) import qualified Data.IntSet as IntSet import qualified Data.List as List import Data.Maybe import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.Syntax.Position import Agda.TypeChecking.Monad import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Free import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Size import Agda.Utils.Tuple import Agda.Utils.VarSet (VarSet) import qualified Agda.Utils.VarSet as VarSet #include "undefined.h" import Agda.Utils.Impossible -- | Flatten telescope: (Γ : Tel) -> [Type Γ] flattenTel :: Telescope -> [Dom Type] flattenTel EmptyTel = [] flattenTel (ExtendTel a tel) = raise (size tel + 1) a : flattenTel (absBody tel) -- | Order a flattened telescope in the correct dependeny order: Γ -> -- Permutation (Γ -> Γ~) -- -- Since @reorderTel tel@ uses free variable analysis of type in @tel@, -- the telescope should be 'normalise'd. reorderTel :: [Dom Type] -> Maybe Permutation reorderTel tel = topoSort comesBefore tel' where tel' = zip (downFrom $ size tel) tel (i, _) `comesBefore` (_, a) = i `freeIn` unEl (unDom a) -- a tiny bit unsafe reorderTel_ :: [Dom Type] -> Permutation reorderTel_ tel = case reorderTel tel of Nothing -> __IMPOSSIBLE__ Just p -> p -- | Unflatten: turns a flattened telescope into a proper telescope. Must be -- properly ordered. unflattenTel :: [ArgName] -> [Dom Type] -> Telescope unflattenTel [] [] = EmptyTel unflattenTel (x : xs) (a : tel) = ExtendTel a' (Abs x tel') where tel' = unflattenTel xs tel a' = applySubst rho a rho = parallelS (replicate (size tel + 1) __IMPOSSIBLE_TERM__) unflattenTel [] (_ : _) = __IMPOSSIBLE__ unflattenTel (_ : _) [] = __IMPOSSIBLE__ -- | Get the suggested names from a telescope teleNames :: Telescope -> [ArgName] teleNames = map (fst . unDom) . telToList teleArgNames :: Telescope -> [Arg ArgName] teleArgNames = map (argFromDom . fmap fst) . telToList teleArgs :: (DeBruijn a) => Telescope -> [Arg a] teleArgs tel = [ Arg info (debruijnNamedVar n i) | (i, Dom info (n,_)) <- zip (downFrom $ size l) l ] where l = telToList tel teleNamedArgs :: (DeBruijn a) => Telescope -> [NamedArg a] teleNamedArgs tel = [ Arg info (Named (Just $ Ranged noRange $ argNameToString name) (debruijnNamedVar name i)) | (i, Dom info (name,_)) <- zip (downFrom $ size l) l ] where l = telToList tel -- | A variant of `teleNamedArgs` which takes the argument names (and the argument info) -- from the first telescope and the variable names from the second telescope. -- -- Precondition: the two telescopes have the same length. tele2NamedArgs :: (DeBruijn a) => Telescope -> Telescope -> [NamedArg a] tele2NamedArgs tel0 tel = [ Arg info (Named (Just $ Ranged noRange $ argNameToString argName) (debruijnNamedVar varName i)) | (i, Dom info (argName,_), Dom _ (varName,_)) <- zip3 (downFrom $ size l) l0 l ] where l = telToList tel l0 = telToList tel0 -- | Permute telescope: permutes or drops the types in the telescope according -- to the given permutation. Assumes that the permutation preserves the -- dependencies in the telescope. -- -- For example (Andreas, 2016-12-18, issue #2344): -- @ -- tel = (A : Set) (X : _18 A) (i : Fin (_m_23 A X)) -- tel (de Bruijn) = 2:Set, 1:_18 @0, 0:Fin(_m_23 @1 @0) -- flattenTel tel = 2:Set, 1:_18 @0, 0:Fin(_m_23 @1 @0) |- [ Set, _18 @2, Fin (_m_23 @2 @1) ] -- perm = 0,1,2 -> 0,1 (picks the first two) -- renaming _ perm = [var 0, var 1, error] -- THE WRONG RENAMING! -- renaming _ (flipP perm) = [error, var 1, var 0] -- The correct renaming! -- apply to flattened tel = ... |- [ Set, _18 @1, Fin (_m_23 @1 @0) ] -- permute perm it = ... |- [ Set, _18 @1 ] -- unflatten (de Bruijn) = 1:Set, 0: _18 @0 -- unflatten = (A : Set) (X : _18 A) -- @ permuteTel :: Permutation -> Telescope -> Telescope permuteTel perm tel = let names = permute perm $ teleNames tel types = permute perm $ renameP __IMPOSSIBLE__ (flipP perm) $ flattenTel tel in unflattenTel names types -- | Recursively computes dependencies of a set of variables in a given -- telescope. Any dependencies outside of the telescope are ignored. varDependencies :: Telescope -> IntSet -> IntSet varDependencies tel = allDependencies IntSet.empty where n = size tel ts = flattenTel tel directDependencies :: Int -> IntSet directDependencies i = allFreeVars $ ts !! (n-1-i) allDependencies :: IntSet -> IntSet -> IntSet allDependencies = IntSet.foldr $ \j soFar -> if j >= n || j `IntSet.member` soFar then soFar else IntSet.insert j $ allDependencies soFar $ directDependencies j -- | A telescope split in two. data SplitTel = SplitTel { firstPart :: Telescope , secondPart :: Telescope , splitPerm :: Permutation -- ^ The permutation takes us from the original telescope to -- @firstPart ++ secondPart@. } -- | Split a telescope into the part that defines the given variables and the -- part that doesn't. -- -- See 'Agda.TypeChecking.Tests.prop_splitTelescope'. splitTelescope :: VarSet -- ^ A set of de Bruijn indices. -> Telescope -- ^ Original telescope. -> SplitTel -- ^ @firstPart@ mentions the given variables, @secondPart@ not. splitTelescope fv tel = SplitTel tel1 tel2 perm where names = teleNames tel ts0 = flattenTel tel n = size tel is = varDependencies tel fv isC = IntSet.fromList [0..(n-1)] `IntSet.difference` is perm = Perm n $ map (n-1-) $ VarSet.toDescList is ++ VarSet.toDescList isC ts1 = renameP __IMPOSSIBLE__ (reverseP perm) (permute perm ts0) tel' = unflattenTel (permute perm names) ts1 m = size is (tel1, tel2) = telFromList -*- telFromList $ splitAt m $ telToList tel' -- | As splitTelescope, but fails if any additional variables or reordering -- would be needed to make the first part well-typed. splitTelescopeExact :: [Int] -- ^ A list of de Bruijn indices -> Telescope -- ^ The telescope to split -> Maybe SplitTel -- ^ @firstPart@ mentions the given variables in the given order, -- @secondPart@ contains all other variables splitTelescopeExact is tel = guard ok $> SplitTel tel1 tel2 perm where names = teleNames tel ts0 = flattenTel tel n = size tel checkDependencies :: IntSet -> [Int] -> Bool checkDependencies soFar [] = True checkDependencies soFar (j:js) = ok && checkDependencies (IntSet.insert j soFar) js where fv' = allFreeVars $ ts0 !! (n-1-j) fv = fv' `IntSet.intersection` IntSet.fromAscList [ 0 .. n-1 ] ok = fv `IntSet.isSubsetOf` soFar ok = all ( Int -- ^ Γ ⊢ var k : A -> Term -- ^ Γ ⊢ u : A -> Maybe (Telescope, -- ⊢ Γ' PatternSubstitution, -- Γ' ⊢ σ : Γ Permutation) -- Γ ⊢ flipP ρ : Γ' instantiateTelescope tel k u = guard ok $> (tel', sigma, rho) where names = teleNames tel ts0 = flattenTel tel n = size tel j = n-1-k -- is0 is the part of Γ that is needed to type u is0 = varDependencies tel $ allFreeVars u -- is1 is the rest of Γ (minus the variable we are instantiating) is1 = IntSet.delete j $ IntSet.fromAscList [ 0 .. n-1 ] `IntSet.difference` is0 -- we work on de Bruijn indices, so later parts come first is = IntSet.toAscList is1 ++ IntSet.toAscList is0 -- if u depends on var j, we cannot instantiate ok = not $ j `IntSet.member` is0 perm = Perm n $ is -- works on de Bruijn indices rho = reverseP perm -- works on de Bruijn levels u1 = renameP __IMPOSSIBLE__ perm u -- Γ' ⊢ u1 : A' us = map (\i -> fromMaybe (DotP u1) (deBruijnVar <$> List.findIndex (i ==) is)) [ 0 .. n-1 ] sigma = us ++# raiseS (n-1) ts1 = permute rho $ applyPatSubst sigma ts0 tel' = unflattenTel (permute rho names) ts1 -- | Try to eta-expand one variable in the telescope (given by its de Bruijn -- level) expandTelescopeVar :: Telescope -- Γ = Γ₁(x : D pars)Γ₂ -> Int -- k = size Γ₁ -> Telescope -- Γ₁ ⊢ Δ -> ConHead -- Γ₁ ⊢ c : Δ → D pars -> ( Telescope -- Γ' = Γ₁ΔΓ₂[x ↦ c Δ] , PatternSubstitution) -- Γ' ⊢ ρ : Γ expandTelescopeVar gamma k delta c = (tel', rho) where (ts1,a:ts2) = fromMaybe __IMPOSSIBLE__ $ splitExactlyAt k $ telToList gamma cpi = ConPatternInfo { conPRecord = Just ConOSystem , conPType = Just $ snd <$> argFromDom a } cargs = map (setOrigin Inserted) $ teleNamedArgs delta cdelta = ConP c cpi cargs -- Γ₁Δ ⊢ c Δ : D pars rho0 = consS cdelta $ raiseS (size delta) -- Γ₁Δ ⊢ ρ₀ : Γ₁(x : D pars) rho = liftS (size ts2) rho0 -- Γ₁ΔΓ₂ρ₀ ⊢ ρ : Γ₁(x : D pars)Γ₂ gamma1 = telFromList ts1 gamma2' = applyPatSubst rho0 $ telFromList ts2 tel' = gamma1 `abstract` (delta `abstract` gamma2') -- | Gather leading Πs of a type in a telescope. telView :: Type -> TCM TelView telView = telViewUpTo (-1) -- | @telViewUpTo n t@ takes off the first @n@ function types of @t@. -- Takes off all if @n < 0@. telViewUpTo :: Int -> Type -> TCM TelView telViewUpTo n t = telViewUpTo' n (const True) t -- | @telViewUpTo' n p t@ takes off $t$ -- the first @n@ (or arbitrary many if @n < 0@) function domains -- as long as they satify @p@. telViewUpTo' :: Int -> (Dom Type -> Bool) -> Type -> TCM TelView telViewUpTo' 0 p t = return $ TelV EmptyTel t telViewUpTo' n p t = do t <- reduce t case ignoreSharing $ unEl t of Pi a b | p a -> absV a (absName b) <$> telViewUpTo' (n - 1) p (absBody b) _ -> return $ TelV EmptyTel t where absV a x (TelV tel t) = TelV (ExtendTel a (Abs x tel)) t -- | Decomposing a function type. mustBePi :: MonadTCM tcm => Type -> tcm (Dom Type, Abs Type) mustBePi t = ifNotPiType t __IMPOSSIBLE__ $ \ a b -> return (a,b) -- | If the given type is a @Pi@, pass its parts to the first continuation. -- If not (or blocked), pass the reduced type to the second continuation. ifPi :: MonadTCM tcm => Term -> (Dom Type -> Abs Type -> tcm a) -> (Term -> tcm a) -> tcm a ifPi t yes no = do t <- liftTCM $ reduce t case ignoreSharing t of Pi a b -> yes a b _ -> no t -- | If the given type is a @Pi@, pass its parts to the first continuation. -- If not (or blocked), pass the reduced type to the second continuation. ifPiType :: MonadTCM tcm => Type -> (Dom Type -> Abs Type -> tcm a) -> (Type -> tcm a) -> tcm a ifPiType (El s t) yes no = ifPi t yes (no . El s) -- | If the given type is blocked or not a @Pi@, pass it reduced to the first continuation. -- If it is a @Pi@, pass its parts to the second continuation. ifNotPi :: MonadTCM tcm => Term -> (Term -> tcm a) -> (Dom Type -> Abs Type -> tcm a) -> tcm a ifNotPi = flip . ifPi -- | If the given type is blocked or not a @Pi@, pass it reduced to the first continuation. -- If it is a @Pi@, pass its parts to the second continuation. ifNotPiType :: MonadTCM tcm => Type -> (Type -> tcm a) -> (Dom Type -> Abs Type -> tcm a) -> tcm a ifNotPiType = flip . ifPiType -- | A safe variant of piApply. piApplyM :: Type -> Args -> TCM Type piApplyM t [] = return t piApplyM t (arg : args) = do (_, b) <- mustBePi t absApp b (unArg arg) `piApplyM` args piApply1 :: MonadTCM tcm => Type -> Term -> tcm Type piApply1 t v = do (_, b) <- mustBePi t return $ absApp b v -- | Compute type arity typeArity :: Type -> TCM Nat typeArity t = do TelV tel _ <- telView t return (size tel) --------------------------------------------------------------------------- -- * Instance definitions --------------------------------------------------------------------------- data OutputTypeName = OutputTypeName QName | OutputTypeVar | OutputTypeNameNotYetKnown | NoOutputTypeName -- | Strips all Pi's and return the head definition name, if possible. getOutputTypeName :: Type -> TCM OutputTypeName getOutputTypeName t = do TelV tel t' <- telView t ifBlocked (unEl t') (\ _ _ -> return OutputTypeNameNotYetKnown) $ \ v -> case ignoreSharing v of -- Possible base types: Def n _ -> return $ OutputTypeName n Sort{} -> return NoOutputTypeName Var n _ -> return OutputTypeVar -- Not base types: Con{} -> __IMPOSSIBLE__ Lam{} -> __IMPOSSIBLE__ Lit{} -> __IMPOSSIBLE__ Level{} -> __IMPOSSIBLE__ MetaV{} -> __IMPOSSIBLE__ Pi{} -> __IMPOSSIBLE__ Shared{} -> __IMPOSSIBLE__ DontCare{} -> __IMPOSSIBLE__ addTypedInstance :: QName -> Type -> TCM () addTypedInstance x t = do n <- getOutputTypeName t case n of OutputTypeName n -> addNamedInstance x n OutputTypeNameNotYetKnown -> addUnknownInstance x NoOutputTypeName -> typeError $ WrongInstanceDeclaration OutputTypeVar -> typeError $ WrongInstanceDeclaration resolveUnknownInstanceDefs :: TCM () resolveUnknownInstanceDefs = do anonInstanceDefs <- getAnonInstanceDefs clearAnonInstanceDefs forM_ anonInstanceDefs $ \ n -> addTypedInstance n =<< typeOfConst n -- | Try to solve the instance definitions whose type is not yet known, report -- an error if it doesn't work and return the instance table otherwise. getInstanceDefs :: TCM InstanceTable getInstanceDefs = do resolveUnknownInstanceDefs insts <- getAllInstanceDefs unless (null $ snd insts) $ typeError $ GenericError $ "There are instances whose type is still unsolved" return $ fst insts Agda-2.5.3/src/full/Agda/TypeChecking/DeadCode.hs0000644000000000000000000000451513154613124017476 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.DeadCode (eliminateDeadCode) where import Control.Applicative import Control.Monad import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import Data.Foldable (foldMap, Foldable) import Data.Traversable (traverse) import Agda.Syntax.Common import Agda.Syntax.Literal import qualified Agda.Syntax.Concrete as C import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Internal import Agda.Syntax.Internal.Names import Agda.Syntax.Scope.Base import Agda.Syntax.Scope.Monad import qualified Agda.Benchmarking as Bench import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TypeChecking.Monad import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Reduce import Agda.Utils.HashMap (HashMap) import qualified Agda.Utils.HashMap as HMap import Agda.Utils.Lens import Agda.Utils.Impossible #include "undefined.h" -- | Run before serialisation to remove any definitions that are not reachable -- from the public interface to the module. eliminateDeadCode :: DisplayForms -> Signature -> TCM (DisplayForms, Signature) eliminateDeadCode disp sig = Bench.billTo [Bench.DeadCode] $ do patsyn <- getPatternSyns public <- Set.map anameName . publicNames <$> getScope defs <- traverse instantiateFull $ sig ^. sigDefinitions let r = reachableFrom public patsyn defs dead = Set.fromList (HMap.keys defs) `Set.difference` r valid = Set.null . Set.intersection dead . namesIn defs' = HMap.map ( \ d -> d { defDisplay = filter valid (defDisplay d) } ) $ HMap.filterWithKey (\ x _ -> Set.member x r) defs disp' = HMap.filter (not . null) $ HMap.map (filter valid) disp reportSLn "tc.dead" 10 $ "Removed " ++ show (HMap.size defs - HMap.size defs') ++ " unused definitions." return (disp', set sigDefinitions defs' sig) reachableFrom :: Set QName -> A.PatternSynDefns -> Definitions -> Set QName reachableFrom names psyns defs = follow names (Set.toList names) where follow visited [] = visited follow visited (x : xs) = follow (Set.union visited new) (Set.toList new ++ xs) where new = Set.filter (not . (`Set.member` visited)) $ case HMap.lookup x defs of Nothing -> namesIn (PSyn <$> Map.lookup x psyns) Just d -> namesIn d Agda-2.5.3/src/full/Agda/TypeChecking/Telescope.hs-boot0000644000000000000000000000032413154613124020724 0ustar0000000000000000 module Agda.TypeChecking.Telescope where import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Substitute piApplyM :: Type -> Args -> TCM Type telView :: Type -> TCM TelView Agda-2.5.3/src/full/Agda/TypeChecking/MetaVars.hs0000644000000000000000000014517613154613124017601 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.MetaVars where import Prelude hiding (null) import Control.Monad.Reader import Data.Function import qualified Data.List as List import qualified Data.Map as Map import qualified Data.Foldable as Fold import Agda.Syntax.Abstract.Name as A import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Generic import Agda.Syntax.Position (killRange) import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Constraints import Agda.TypeChecking.Free import Agda.TypeChecking.Level import Agda.TypeChecking.Records import Agda.TypeChecking.Pretty import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.EtaContract import Agda.TypeChecking.SizedTypes (boundedSizeMetaHook, isSizeProblem) import {-# SOURCE #-} Agda.TypeChecking.CheckInternal -- import Agda.TypeChecking.CheckInternal -- import {-# SOURCE #-} Agda.TypeChecking.CheckInternal (checkInternal) import Agda.TypeChecking.MetaVars.Occurs import Agda.Utils.Except ( ExceptT , MonadError(throwError) , runExceptT ) import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Size import Agda.Utils.Tuple import Agda.Utils.Permutation import Agda.Utils.Pretty ( prettyShow, render ) import qualified Agda.Utils.VarSet as Set #include "undefined.h" import Agda.Utils.Impossible -- | Find position of a value in a list. -- Used to change metavar argument indices during assignment. -- -- @reverse@ is necessary because we are directly abstracting over the list. -- findIdx :: Eq a => [a] -> a -> Maybe Int findIdx vs v = List.findIndex (==v) (reverse vs) -- | Check whether a meta variable is a place holder for a blocked term. isBlockedTerm :: MetaId -> TCM Bool isBlockedTerm x = do reportSLn "tc.meta.blocked" 12 $ "is " ++ prettyShow x ++ " a blocked term? " i <- mvInstantiation <$> lookupMeta x let r = case i of BlockedConst{} -> True PostponedTypeCheckingProblem{} -> True InstV{} -> False Open{} -> False OpenIFS{} -> False reportSLn "tc.meta.blocked" 12 $ if r then " yes, because " ++ show i else " no" return r isEtaExpandable :: [MetaKind] -> MetaId -> TCM Bool isEtaExpandable kinds x = do i <- mvInstantiation <$> lookupMeta x return $ case i of Open{} -> True OpenIFS{} -> notElem Records kinds InstV{} -> False BlockedConst{} -> False PostponedTypeCheckingProblem{} -> False -- * Performing the assignment -- | Performing the meta variable assignment. -- -- The instantiation should not be an 'InstV' and the 'MetaId' -- should point to something 'Open' or a 'BlockedConst'. -- Further, the meta variable may not be 'Frozen'. assignTerm :: MetaId -> [Arg ArgName] -> Term -> TCM () assignTerm x tel v = do -- verify (new) invariants whenM (isFrozen x) __IMPOSSIBLE__ assignTerm' x tel v -- | Skip frozen check. Used for eta expanding frozen metas. assignTerm' :: MetaId -> [Arg ArgName] -> Term -> TCM () assignTerm' x tel v = do reportSLn "tc.meta.assign" 70 $ prettyShow x ++ " := " ++ show v ++ "\n in " ++ show tel -- verify (new) invariants whenM (not <$> asks envAssignMetas) __IMPOSSIBLE__ {- TODO make double-checking work -- currently, it does not work since types of sort-metas are inaccurate! -- Andreas, 2013-10-25 double check solution before assigning m <- lookupMeta x case mvJudgement m of HasType _ a -> dontAssignMetas $ checkInternal t a IsSort{} -> return () -- skip double check since type of meta is not accurate -} -- Andreas, 2013-10-25 double check solution before assigning -- Andreas, 2013-11-30 this seems to open a can of worms... -- dontAssignMetas $ do -- checkInternal t . jMetaType . mvJudgement =<< lookupMeta x verboseS "profile.metas" 10 $ liftTCM $ tickMax "max-open-metas" . (fromIntegral . size) =<< getOpenMetas modifyMetaStore $ ins x $ InstV tel $ killRange v etaExpandListeners x wakeupConstraints x reportSLn "tc.meta.assign" 20 $ "completed assignment of " ++ prettyShow x where ins x i = Map.adjust (\ mv -> mv { mvInstantiation = i }) x -- * Creating meta variables. -- | Create a sort meta that cannot be instantiated with 'Inf' (Setω). newSortMetaBelowInf :: TCM Sort newSortMetaBelowInf = newSortMeta' $ HasType () -- | Create a sort meta that may be instantiated with 'Inf' (Setω). newSortMeta :: TCM Sort newSortMeta = newSortMeta' $ IsSort () newSortMeta' :: (Type -> Judgement ()) -> TCM Sort newSortMeta' judge = ifM typeInType (return $ mkType 0) $ {- else -} ifM hasUniversePolymorphism (newSortMetaCtx' judge =<< getContextArgs) -- else (no universe polymorphism) $ do i <- createMetaInfo lvl <- levelType x <- newMeta i normalMetaPriority (idP 0) $ judge lvl return $ Type $ Max [Plus 0 $ MetaLevel x []] -- | Create a sort meta that may be instantiated with 'Inf' (Setω). newSortMetaCtx :: Args -> TCM Sort newSortMetaCtx = newSortMetaCtx' $ IsSort () newSortMetaCtx' :: (Type -> Judgement ()) -> Args -> TCM Sort newSortMetaCtx' judge vs = do ifM typeInType (return $ mkType 0) $ {- else -} do i <- createMetaInfo tel <- getContextTelescope lvl <- levelType let t = telePi_ tel lvl x <- newMeta i normalMetaPriority (idP 0) $ judge t reportSDoc "tc.meta.new" 50 $ text "new sort meta" <+> prettyTCM x <+> text ":" <+> prettyTCM t return $ Type $ Max [Plus 0 $ MetaLevel x $ map Apply vs] newTypeMeta :: Sort -> TCM Type newTypeMeta s = El s . snd <$> newValueMeta RunMetaOccursCheck (sort s) newTypeMeta_ :: TCM Type newTypeMeta_ = newTypeMeta =<< (workOnTypes $ newSortMeta) -- TODO: (this could be made work with new uni-poly) -- Andreas, 2011-04-27: If a type meta gets solved, than we do not have to check -- that it has a sort. The sort comes from the solution. -- newTypeMeta_ = newTypeMeta Inf -- | @newIFSMeta s t cands@ creates a new "implicit from scope" metavariable -- of type the output type of @t@ with name suggestion @s@. newIFSMeta :: MetaNameSuggestion -> Type -> TCM (MetaId, Term) newIFSMeta s t = do vs <- getContextArgs ctx <- getContextTelescope newIFSMetaCtx s (telePi_ ctx t) vs newIFSMetaCtx :: MetaNameSuggestion -> Type -> Args -> TCM (MetaId, Term) newIFSMetaCtx s t vs = do reportSDoc "tc.meta.new" 50 $ fsep [ text "new ifs meta:" , nest 2 $ prettyTCM vs <+> text "|-" ] i0 <- createMetaInfo let i = i0 { miNameSuggestion = s } TelV tel _ <- telView t let perm = idP (size tel) x <- newMeta' OpenIFS i normalMetaPriority perm (HasType () t) reportSDoc "tc.meta.new" 50 $ fsep [ nest 2 $ pretty x <+> text ":" <+> prettyTCM t ] let c = FindInScope x Nothing Nothing -- If we're not already solving instance constraints we should add this -- to the awake constraints to make sure we don't forget about it. If we -- are solving constraints it will get woken up later (see #2690) ifM isSolvingConstraints (addConstraint c) (addAwakeConstraint' c) etaExpandMetaSafe x return (x, MetaV x $ map Apply vs) -- | Create a new value meta with specific dependencies, possibly η-expanding in the process. newNamedValueMeta :: RunMetaOccursCheck -> MetaNameSuggestion -> Type -> TCM (MetaId, Term) newNamedValueMeta b s t = do (x, v) <- newValueMeta b t setMetaNameSuggestion x s return (x, v) -- | Create a new value meta with specific dependencies without η-expanding. newNamedValueMeta' :: RunMetaOccursCheck -> MetaNameSuggestion -> Type -> TCM (MetaId, Term) newNamedValueMeta' b s t = do (x, v) <- newValueMeta' b t setMetaNameSuggestion x s return (x, v) -- | Create a new metavariable, possibly η-expanding in the process. newValueMeta :: RunMetaOccursCheck -> Type -> TCM (MetaId, Term) newValueMeta b t = do vs <- getContextArgs tel <- getContextTelescope newValueMetaCtx b t tel (idP $ size tel) vs newValueMetaCtx :: RunMetaOccursCheck -> Type -> Telescope -> Permutation -> Args -> TCM (MetaId, Term) newValueMetaCtx b t tel perm ctx = mapSndM instantiateFull =<< newValueMetaCtx' b t tel perm ctx -- | Create a new value meta without η-expanding. newValueMeta' :: RunMetaOccursCheck -> Type -> TCM (MetaId, Term) newValueMeta' b t = do vs <- getContextArgs tel <- getContextTelescope newValueMetaCtx' b t tel (idP $ size tel) vs -- | Create a new value meta with specific dependencies. newValueMetaCtx' :: RunMetaOccursCheck -> Type -> Telescope -> Permutation -> Args -> TCM (MetaId, Term) newValueMetaCtx' b a tel perm vs = do i <- createMetaInfo' b let t = telePi_ tel a x <- newMeta i normalMetaPriority perm (HasType () t) reportSDoc "tc.meta.new" 50 $ fsep [ text "new meta:" , nest 2 $ prettyTCM vs <+> text "|-" , nest 2 $ pretty x <+> text ":" <+> prettyTCM t ] etaExpandMetaSafe x -- Andreas, 2012-09-24: for Metas X : Size< u add constraint X+1 <= u u <- shared $ MetaV x $ map Apply vs boundedSizeMetaHook u tel a return (x, u) newTelMeta :: Telescope -> TCM Args newTelMeta tel = newArgsMeta (abstract tel $ typeDontCare) type Condition = Dom Type -> Abs Type -> Bool trueCondition :: Condition trueCondition _ _ = True newArgsMeta :: Type -> TCM Args newArgsMeta = newArgsMeta' trueCondition newArgsMeta' :: Condition -> Type -> TCM Args newArgsMeta' condition t = do args <- getContextArgs tel <- getContextTelescope newArgsMetaCtx' condition t tel (idP $ size tel) args newArgsMetaCtx :: Type -> Telescope -> Permutation -> Args -> TCM Args newArgsMetaCtx = newArgsMetaCtx' trueCondition newArgsMetaCtx' :: Condition -> Type -> Telescope -> Permutation -> Args -> TCM Args newArgsMetaCtx' condition (El s tm) tel perm ctx = do tm <- reduce tm case ignoreSharing tm of Pi dom@(Dom info a) codom | condition dom codom -> do (_, u) <- applyRelevanceToContext (getRelevance info) $ {- -- Andreas, 2010-09-24 skip irrelevant record fields when eta-expanding a meta var -- Andreas, 2010-10-11 this is WRONG, see Issue 347 if r == Irrelevant then return DontCare else -} newValueMetaCtx RunMetaOccursCheck a tel perm ctx args <- newArgsMetaCtx' condition (codom `absApp` u) tel perm ctx return $ Arg info u : args _ -> return [] -- | Create a metavariable of record type. This is actually one metavariable -- for each field. newRecordMeta :: QName -> Args -> TCM Term newRecordMeta r pars = do args <- getContextArgs tel <- getContextTelescope newRecordMetaCtx r pars tel (idP $ size tel) args newRecordMetaCtx :: QName -> Args -> Telescope -> Permutation -> Args -> TCM Term newRecordMetaCtx r pars tel perm ctx = do ftel <- flip apply pars <$> getRecordFieldTypes r fields <- newArgsMetaCtx (telePi_ ftel $ sort Prop) tel perm ctx con <- getRecordConstructor r return $ Con con ConOSystem fields newQuestionMark :: InteractionId -> Type -> TCM (MetaId, Term) newQuestionMark = newQuestionMark' $ newValueMeta' RunMetaOccursCheck newQuestionMark' :: (Type -> TCM (MetaId, Term)) -> InteractionId -> Type -> TCM (MetaId, Term) newQuestionMark' new ii t = do -- Andreas, 2016-07-29, issue 1720-2 -- This is slightly risky, as the same interaction id -- maybe be shared between different contexts. -- Blame goes to the record processing hack, see issue #424 -- and @ConcreteToAbstract.recordConstructorType@. let existing x = (x,) . MetaV x . map Apply <$> getContextArgs flip (caseMaybeM $ lookupInteractionMeta ii) existing $ {-else-} do -- Do not run check for recursive occurrence of meta in definitions, -- because we want to give the recursive solution interactively (Issue 589) (x, m) <- new t connectInteractionPoint ii x return (x, m) -- | Construct a blocked constant if there are constraints. blockTerm :: Type -> TCM Term -> TCM Term blockTerm t blocker = do (pid, v) <- newProblem blocker blockTermOnProblem t v pid blockTermOnProblem :: Type -> Term -> ProblemId -> TCM Term blockTermOnProblem t v pid = -- Andreas, 2012-09-27 do not block on unsolved size constraints ifM (isProblemSolved pid `or2M` isSizeProblem pid) (return v) $ do i <- createMetaInfo es <- map Apply <$> getContextArgs tel <- getContextTelescope x <- newMeta' (BlockedConst $ abstract tel v) i lowMetaPriority (idP $ size tel) (HasType () $ telePi_ tel t) -- we don't instantiate blocked terms inTopContext $ addConstraint (Guarded (UnBlock x) pid) reportSDoc "tc.meta.blocked" 20 $ vcat [ text "blocked" <+> prettyTCM x <+> text ":=" <+> inTopContext (prettyTCM $ abstract tel v) , text " by" <+> (prettyTCM =<< getConstraintsForProblem pid) ] inst <- isInstantiatedMeta x case inst of True -> instantiate (MetaV x es) False -> do -- We don't return the blocked term instead create a fresh metavariable -- that we compare against the blocked term once it's unblocked. This way -- blocked terms can be instantiated before they are unblocked, thus making -- constraint solving a bit more robust against instantiation order. -- Andreas, 2015-05-22: DontRunMetaOccursCheck to avoid Issue585-17. (_, v) <- newValueMeta DontRunMetaOccursCheck t i <- liftTCM fresh -- This constraint is woken up when unblocking, so it doesn't need a problem id. cmp <- buildProblemConstraint_ (ValueCmp CmpEq t v (MetaV x es)) listenToMeta (CheckConstraint i cmp) x return v blockTypeOnProblem :: Type -> ProblemId -> TCM Type blockTypeOnProblem (El s a) pid = El s <$> blockTermOnProblem (El Inf $ Sort s) a pid -- | @unblockedTester t@ returns @False@ if @t@ is a meta or a blocked term. -- -- Auxiliary function to create a postponed type checking problem. unblockedTester :: Type -> TCM Bool unblockedTester t = ifBlockedType t (\ m t -> return False) (\ t -> return True) -- | Create a postponed type checking problem @e : t@ that waits for type @t@ -- to unblock (become instantiated or its constraints resolved). postponeTypeCheckingProblem_ :: TypeCheckingProblem -> TCM Term postponeTypeCheckingProblem_ p = do postponeTypeCheckingProblem p (unblock p) where unblock (CheckExpr _ t) = unblockedTester t unblock (CheckArgs _ _ _ t _ _) = unblockedTester t -- The type of the head of the application. unblock (CheckLambda _ _ t) = unblockedTester t unblock (UnquoteTactic _ _ _) = __IMPOSSIBLE__ -- unquote problems must be supply their own tester -- | Create a postponed type checking problem @e : t@ that waits for conditon -- @unblock@. A new meta is created in the current context that has as -- instantiation the postponed type checking problem. An 'UnBlock' constraint -- is added for this meta, which links to this meta. postponeTypeCheckingProblem :: TypeCheckingProblem -> TCM Bool -> TCM Term postponeTypeCheckingProblem p unblock = do i <- createMetaInfo' DontRunMetaOccursCheck tel <- getContextTelescope cl <- buildClosure p t <- problemType p m <- newMeta' (PostponedTypeCheckingProblem cl unblock) i normalMetaPriority (idP (size tel)) $ HasType () $ telePi_ tel t -- Create the meta that we actually return -- Andreas, 2012-03-15 -- This is an alias to the pptc meta, in order to allow pruning (issue 468) -- and instantiation. -- Since this meta's solution comes from user code, we do not need -- to run the extended occurs check (metaOccurs) to exclude -- non-terminating solutions. es <- map Apply <$> getContextArgs (_, v) <- newValueMeta DontRunMetaOccursCheck t cmp <- buildProblemConstraint_ (ValueCmp CmpEq t v (MetaV m es)) i <- liftTCM fresh listenToMeta (CheckConstraint i cmp) m addConstraint (UnBlock m) return v -- | Type of the term that is produced by solving the 'TypeCheckingProblem'. problemType :: TypeCheckingProblem -> TCM Type problemType (CheckExpr _ t ) = return t problemType (CheckArgs _ _ _ _ t _ ) = return t -- The target type of the application. problemType (CheckLambda _ _ t ) = return t problemType (UnquoteTactic tac hole t) = return t -- | Eta expand metavariables listening on the current meta. etaExpandListeners :: MetaId -> TCM () etaExpandListeners m = do ls <- getMetaListeners m clearMetaListeners m -- we don't really have to do this mapM_ wakeupListener ls -- | Wake up a meta listener and let it do its thing wakeupListener :: Listener -> TCM () -- Andreas 2010-10-15: do not expand record mvars, lazyness needed for irrelevance wakeupListener (EtaExpand x) = etaExpandMetaSafe x wakeupListener (CheckConstraint _ c) = do reportSDoc "tc.meta.blocked" 20 $ text "waking boxed constraint" <+> prettyTCM c addAwakeConstraints [c] solveAwakeConstraints -- | Do safe eta-expansions for meta (@SingletonRecords,Levels@). etaExpandMetaSafe :: MetaId -> TCM () etaExpandMetaSafe = etaExpandMeta [SingletonRecords,Levels] -- | Various kinds of metavariables. data MetaKind = Records -- ^ Meta variables of record type. | SingletonRecords -- ^ Meta variables of \"hereditarily singleton\" record type. | Levels -- ^ Meta variables of level type, if type-in-type is activated. deriving (Eq, Enum, Bounded, Show) -- | All possible metavariable kinds. allMetaKinds :: [MetaKind] allMetaKinds = [minBound .. maxBound] -- | Eta expand a metavariable, if it is of the specified kind. -- Don't do anything if the metavariable is a blocked term. etaExpandMeta :: [MetaKind] -> MetaId -> TCM () etaExpandMeta kinds m = whenM (asks envAssignMetas `and2M` isEtaExpandable kinds m) $ do verboseBracket "tc.meta.eta" 20 ("etaExpandMeta " ++ prettyShow m) $ do let waitFor x = do reportSDoc "tc.meta.eta" 20 $ do text "postponing eta-expansion of meta variable" <+> prettyTCM m <+> text "which is blocked by" <+> prettyTCM x listenToMeta (EtaExpand m) x dontExpand = do reportSDoc "tc.meta.eta" 20 $ do text "we do not expand meta variable" <+> prettyTCM m <+> text ("(requested was expansion of " ++ show kinds ++ ")") meta <- lookupMeta m let HasType _ a = mvJudgement meta TelV tel b <- telView a -- if the target type @b@ of @m@ is a meta variable @x@ itself -- (@NonBlocked (MetaV{})@), -- or it is blocked by a meta-variable @x@ (@Blocked@), we cannot -- eta expand now, we have to postpone this. Once @x@ is -- instantiated, we can continue eta-expanding m. This is realized -- by adding @m@ to the listeners of @x@. ifBlocked (unEl b) (\ x _ -> waitFor x) $ \ t -> case ignoreSharing t of lvl@(Def r es) -> ifM (isEtaRecord r) {- then -} (do let ps = fromMaybe __IMPOSSIBLE__ $ allApplyElims es let expand = do u <- withMetaInfo' meta $ newRecordMetaCtx r ps tel (idP $ size tel) $ teleArgs tel inTopContext $ do verboseS "tc.meta.eta" 15 $ do du <- prettyTCM u reportSDoc "tc.meta.eta" 15 $ sep [ text "eta expanding: " <+> pretty m <+> text " --> " , nest 2 $ prettyTCM u ] -- Andreas, 2012-03-29: No need for occurrence check etc. -- we directly assign the solution for the meta -- 2012-05-23: We also bypass the check for frozen. noConstraints $ assignTerm' m (telToArgs tel) u -- should never produce any constraints if Records `elem` kinds then expand else if (SingletonRecords `elem` kinds) then do singleton <- isSingletonRecord r ps case singleton of Left x -> waitFor x Right False -> dontExpand Right True -> expand else dontExpand ) $ {- else -} ifM (andM [ return $ Levels `elem` kinds , typeInType , (Just lvl ==) <$> getBuiltin' builtinLevel ]) (do reportSLn "tc.meta.eta" 20 $ "Expanding level meta to 0 (type-in-type)" -- Andreas, 2012-03-30: No need for occurrence check etc. -- we directly assign the solution for the meta noConstraints $ assignTerm m (telToArgs tel) (Level $ Max []) ) $ {- else -} dontExpand _ -> dontExpand -- | Eta expand blocking metavariables of record type, and reduce the -- blocked thing. etaExpandBlocked :: Reduce t => Blocked t -> TCM (Blocked t) etaExpandBlocked t@NotBlocked{} = return t etaExpandBlocked (Blocked m t) = do etaExpandMeta [Records] m t <- reduceB t case t of Blocked m' _ | m /= m' -> etaExpandBlocked t _ -> return t -- * Solve constraint @x vs = v@. -- | Assign to an open metavar which may not be frozen. -- First check that metavar args are in pattern fragment. -- Then do extended occurs check on given thing. -- -- Assignment is aborted by throwing a @PatternErr@ via a call to -- @patternViolation@. This error is caught by @catchConstraint@ -- during equality checking (@compareAtom@) and leads to -- restoration of the original constraints. assignV :: CompareDirection -> MetaId -> Args -> Term -> TCM () assignV dir x args v = assignWrapper dir x (map Apply args) v $ assign dir x args v assignWrapper :: CompareDirection -> MetaId -> Elims -> Term -> TCM () -> TCM () assignWrapper dir x es v doAssign = do ifNotM (asks envAssignMetas) patternViolation $ {- else -} do reportSDoc "tc.meta.assign" 10 $ do text "term" <+> prettyTCM (MetaV x es) <+> text (":" ++ show dir) <+> prettyTCM v liftTCM $ nowSolvingConstraints doAssign `finally` solveAwakeConstraints -- | Miller pattern unification: -- -- @assign x vs v@ solves problem @x vs = v@ for meta @x@ -- if @vs@ are distinct variables (linearity check) -- and @v@ depends only on these variables -- and does not contain @x@ itself (occurs check). -- -- This is the basic story, but we have added some features: -- -- 1. Pruning. -- 2. Benign cases of non-linearity. -- 3. @vs@ may contain record patterns. -- -- For a reference to some of these extensions, read -- Andreas Abel and Brigitte Pientka's TLCA 2011 paper. assign :: CompareDirection -> MetaId -> Args -> Term -> TCM () assign dir x args v = do mvar <- lookupMeta x -- information associated with meta x let t = jMetaType $ mvJudgement mvar -- Andreas, 2011-05-20 TODO! -- full normalization (which also happens during occurs check) -- is too expensive! (see Issue 415) -- need to do something cheaper, especially if -- we are dealing with a Miller pattern that can be solved -- immediately! -- Ulf, 2011-08-25 DONE! -- Just instantiating the top-level meta, which is cheaper. The occurs -- check will first try without unfolding any definitions (treating -- arguments to definitions as flexible), if that fails it tries again -- with full unfolding. v <- instantiate v reportSDoc "tc.meta.assign" 45 $ text "MetaVars.assign: assigning to " <+> prettyTCM v reportSLn "tc.meta.assign" 75 $ "MetaVars.assign: assigning to " ++ show v case (ignoreSharing v, mvJudgement mvar) of (Sort Inf, HasType{}) -> typeError SetOmegaNotValidType _ -> return () -- We don't instantiate frozen mvars when (mvFrozen mvar == Frozen) $ do reportSLn "tc.meta.assign" 25 $ "aborting: meta is frozen!" patternViolation -- We never get blocked terms here anymore. TODO: we actually do. why? whenM (isBlockedTerm x) patternViolation -- Andreas, 2010-10-15 I want to see whether rhs is blocked reportSLn "tc.meta.assign" 50 $ "MetaVars.assign: I want to see whether rhs is blocked" reportSDoc "tc.meta.assign" 25 $ do v0 <- reduceB v case v0 of Blocked m0 _ -> text "r.h.s. blocked on:" <+> prettyTCM m0 NotBlocked{} -> text "r.h.s. not blocked" -- Turn the assignment problem @_X args >= SizeLt u@ into -- @_X args = SizeLt (_Y args@ and constraint -- @_Y args >= u@. subtypingForSizeLt dir x mvar t args v $ \ v -> do -- Normalise and eta contract the arguments to the meta. These are -- usually small, and simplifying might let us instantiate more metas. -- MOVED TO expandProjectedVars: -- args <- etaContract =<< normalise args -- Also, try to expand away projected vars in meta args. reportSDoc "tc.meta.assign.proj" 45 $ do cxt <- getContextTelescope vcat [ text "context before projection expansion" , nest 2 $ inTopContext $ prettyTCM cxt ] expandProjectedVars args v $ \ args v -> do reportSDoc "tc.meta.assign.proj" 45 $ do cxt <- getContextTelescope vcat [ text "context after projection expansion" , nest 2 $ inTopContext $ prettyTCM cxt ] -- If we had the type here we could save the work we put -- into expanding projected variables. -- catchConstraint (ValueCmp CmpEq ? (MetaV m $ map Apply args) v) $ do -- Andreas, 2011-04-21 do the occurs check first -- e.g. _1 x (suc x) = suc (_2 x y) -- even though the lhs is not a pattern, we can prune the y from _2 (relVL, irrVL) <- do -- Andreas, 2016-11-03 #2211 attempt to do s.th. for unused if False -- irrelevant $ getMetaRelevance mvar then do reportSDoc "tc.meta.assign" 25 $ text "meta is irrelevant or unused" return (Set.toList $ allFreeVars args, empty) else do let relVL = Set.toList $ allRelevantVars args -- Andreas, 2011-10-06 only irrelevant vars that are direct -- arguments to the meta, hence, can be abstracted over, may -- appear on the rhs. (test/fail/Issue483b) -- Update 2011-03-27: Also irr. vars under record constructors. let fromIrrVar (Var i []) = return [i] fromIrrVar (Con c _ vs) = ifM (isNothing <$> isRecordConstructor (conName c)) (return []) $ concat <$> mapM (fromIrrVar . {- stripDontCare .-} unArg) vs fromIrrVar (Shared p) = fromIrrVar (derefPtr p) fromIrrVar _ = return [] irrVL <- concat <$> mapM fromIrrVar [ v | Arg info v <- args, isIrrelevant info ] -- irrelevant (getRelevance info) ] return (relVL, irrVL) reportSDoc "tc.meta.assign" 20 $ let pr (Var n []) = text (show n) pr (Def c []) = prettyTCM c pr _ = text ".." in vcat [ text "mvar args:" <+> sep (map (pr . unArg) args) , text "fvars lhs (rel):" <+> sep (map (text . show) relVL) , text "fvars lhs (irr):" <+> sep (map (text . show) irrVL) ] -- Check that the x doesn't occur in the right hand side. -- Prune mvars on rhs such that they can only depend on lhs vars. -- Herein, distinguish relevant and irrelevant vars, -- since when abstracting irrelevant lhs vars, they may only occur -- irrelevantly on rhs. v <- liftTCM $ occursCheck x (relVL, irrVL) v reportSLn "tc.meta.assign" 15 "passed occursCheck" verboseS "tc.meta.assign" 30 $ do let n = termSize v when (n > 200) $ reportSDoc "tc.meta.assign" 30 $ sep [ text "size" <+> text (show n) -- , nest 2 $ text "type" <+> prettyTCM t , nest 2 $ text "term" <+> prettyTCM v ] -- Check linearity of @ids@ -- Andreas, 2010-09-24: Herein, ignore the variables which are not -- free in v -- Ulf, 2011-09-22: we need to respect irrelevant vars as well, otherwise -- we'll build solutions where the irrelevant terms are not valid let fvs = allFreeVars v reportSDoc "tc.meta.assign" 20 $ text "fvars rhs:" <+> sep (map (text . show) $ Set.toList fvs) -- Check that the arguments are variables mids <- do res <- runExceptT $ inverseSubst args case res of -- all args are variables Right ids -> do reportSDoc "tc.meta.assign" 50 $ text "inverseSubst returns:" <+> sep (map prettyTCM ids) return $ Just ids -- we have proper values as arguments which could be cased on -- here, we cannot prune, since offending vars could be eliminated Left CantInvert -> return Nothing -- we have non-variables, but these are not eliminateable Left NeutralArg -> Just <$> attemptPruning x args fvs -- we have a projected variable which could not be eta-expanded away: -- same as neutral Left ProjectedVar{} -> Just <$> attemptPruning x args fvs case mids of Nothing -> patternViolation -- Ulf 2014-07-13: actually not needed after all: attemptInertRHSImprovement x args v Just ids -> do -- Check linearity ids <- do res <- runExceptT $ checkLinearity {- (`Set.member` fvs) -} ids case res of -- case: linear Right ids -> return ids -- case: non-linear variables that could possibly be pruned Left () -> attemptPruning x args fvs -- Solve. m <- getContextSize assignMeta' m x t (length args) ids v where attemptPruning x args fvs = do -- non-linear lhs: we cannot solve, but prune killResult <- prune x args $ Set.toList fvs reportSDoc "tc.meta.assign" 10 $ text "pruning" <+> prettyTCM x <+> do text $ if killResult `elem` [PrunedSomething,PrunedEverything] then "succeeded" else "failed" patternViolation {- UNUSED -- | When faced with @_X us == D vs@ for an inert D we can solve this by -- @_X xs := D _Ys@ with new constraints @_Yi us == vi@. This is important -- for instance arguments, where knowing the head D might enable progress. attemptInertRHSImprovement :: MetaId -> Args -> Term -> TCM () attemptInertRHSImprovement m args v = do reportSDoc "tc.meta.inert" 30 $ vcat [ text "attempting inert rhs improvement" , nest 2 $ sep [ prettyTCM (MetaV m $ map Apply args) <+> text "==" , prettyTCM v ] ] -- Check that the right-hand side has the form D vs, for some inert constant D. -- Returns the type of D and a function to build an application of D. (a, mkRHS) <- ensureInert v -- Check that all arguments to the meta are neutral and does not have head D. -- If there are non-neutral arguments there could be solutions to the meta -- that computes over these arguments. If D is an argument to the meta we get -- multiple solutions (for instance: _M Nat == Nat can be solved by both -- _M := \ x -> x and _M := \ x -> Nat). mapM_ (ensureNeutral (mkRHS []) . unArg) args tel <- theTel <$> (telView =<< getMetaType m) -- When attempting shortcut meta solutions, metas aren't necessarily fully -- eta expanded. If this is the case we skip inert improvement. when (length args < size tel) $ do reportSDoc "tc.meta.inert" 30 $ text "not fully applied" patternViolation -- Solve the meta with _M := \ xs -> D (_Y1 xs) .. (_Yn xs), for fresh metas -- _Yi. metaArgs <- inTopContext $ addContext tel $ newArgsMeta a let varArgs = map Apply $ reverse $ zipWith (\i a -> var i <$ a) [0..] (reverse args) sol = mkRHS metaArgs argTel = map ("x" <$) args reportSDoc "tc.meta.inert" 30 $ nest 2 $ vcat [ text "a =" <+> prettyTCM a , text "tel =" <+> prettyTCM tel , text "metas =" <+> prettyList (map prettyTCM metaArgs) , text "sol =" <+> prettyTCM sol ] assignTerm m argTel sol patternViolation -- throwing a pattern violation here lets the constraint -- machinery worry about restarting the comparison. where ensureInert :: Term -> TCM (Type, Args -> Term) ensureInert v = do let notInert = do reportSDoc "tc.meta.inert" 30 $ nest 2 $ text "not inert:" <+> prettyTCM v patternViolation toArgs elims = case allApplyElims elims of Nothing -> do reportSDoc "tc.meta.inert" 30 $ nest 2 $ text "can't do projections from inert" patternViolation Just args -> return args case ignoreSharing v of Var x elims -> (, Var x . map Apply) <$> typeOfBV x Con c ci args -> notInert -- (, Con c ci) <$> defType <$> getConstInfo (conName c) Def f elims -> do def <- getConstInfo f let good = return (defType def, Def f . map Apply) case theDef def of Axiom{} -> good Datatype{} -> good Record{} -> good Function{} -> notInert Primitive{} -> notInert Constructor{} -> __IMPOSSIBLE__ Pi{} -> notInert -- this is actually inert but improving doesn't buy us anything for Pi Lam{} -> notInert Sort{} -> notInert Lit{} -> notInert Level{} -> notInert MetaV{} -> notInert DontCare{} -> notInert Shared{} -> __IMPOSSIBLE__ ensureNeutral :: Term -> Term -> TCM () ensureNeutral rhs v = do b <- reduceB v let notNeutral v = do reportSDoc "tc.meta.inert" 30 $ nest 2 $ text "not neutral:" <+> prettyTCM v patternViolation checkRHS arg | arg == rhs = do reportSDoc "tc.meta.inert" 30 $ nest 2 $ text "argument shares head with RHS:" <+> prettyTCM arg patternViolation | otherwise = return () case fmap ignoreSharing b of Blocked{} -> notNeutral v NotBlocked r v -> -- Andrea(s) 2014-12-06 can r be useful? case v of Var x _ -> checkRHS (Var x []) Def f _ -> checkRHS (Def f []) Pi{} -> return () Sort{} -> return () Level{} -> return () Lit{} -> notNeutral v DontCare{} -> notNeutral v MetaV{} -> notNeutral v Con{} -> notNeutral v Lam{} -> notNeutral v Shared{} -> __IMPOSSIBLE__ -- END UNUSED -} -- | @assignMeta m x t ids u@ solves @x ids = u@ for meta @x@ of type @t@, -- where term @u@ lives in a context of length @m@. -- Precondition: @ids@ is linear. assignMeta :: Int -> MetaId -> Type -> [Int] -> Term -> TCM () assignMeta m x t ids v = do let n = length ids cand = List.sort $ zip ids $ map var $ downFrom n assignMeta' m x t n cand v -- | @assignMeta' m x t ids u@ solves @x = [ids]u@ for meta @x@ of type @t@, -- where term @u@ lives in a context of length @m@, -- and @ids@ is a partial substitution. assignMeta' :: Int -> MetaId -> Type -> Int -> SubstCand -> Term -> TCM () assignMeta' m x t n ids v = do -- we are linear, so we can solve! reportSDoc "tc.meta.assign" 25 $ text "preparing to instantiate: " <+> prettyTCM v -- Rename the variables in v to make it suitable for abstraction over ids. v' <- do -- Basically, if -- Γ = a b c d e -- ids = d b e -- then -- v' = (λ a b c d e. v) _ 1 _ 2 0 -- -- Andreas, 2013-10-25 Solve using substitutions: -- Convert assocList @ids@ (which is sorted) into substitution, -- filling in __IMPOSSIBLE__ for the missing terms, e.g. -- [(0,0),(1,2),(3,1)] --> [0, 2, __IMP__, 1, __IMP__] -- ALT 1: O(m * size ids), serves as specification -- let ivs = [fromMaybe __IMPOSSIBLE__ $ lookup i ids | i <- [0..m-1]] -- ALT 2: O(m) let assocToList i l = case l of _ | i >= m -> [] ((j,u) : l) | i == j -> Just u : assocToList (i+1) l _ -> Nothing : assocToList (i+1) l ivs = assocToList 0 ids rho = prependS __IMPOSSIBLE__ ivs $ raiseS n return $ applySubst rho v -- Metas are top-level so we do the assignment at top-level. inTopContext $ do -- Andreas, 2011-04-18 to work with irrelevant parameters -- we need to construct tel' from the type of the meta variable -- (no longer from ids which may not be the complete variable list -- any more) reportSDoc "tc.meta.assign" 15 $ text "type of meta =" <+> prettyTCM t reportSDoc "tc.meta.assign" 70 $ text "type of meta =" <+> text (show t) TelV tel' _ <- telViewUpTo n t reportSDoc "tc.meta.assign" 30 $ text "tel' =" <+> prettyTCM tel' reportSDoc "tc.meta.assign" 30 $ text "#args =" <+> text (show n) -- Andreas, 2013-09-17 (AIM XVIII): if t does not provide enough -- types for the arguments, it might be blocked by a meta; -- then we give up. (Issue 903) when (size tel' < n) patternViolation -- WAS: __IMPOSSIBLE__ -- Perform the assignment (and wake constraints). let vsol = abstract tel' v' -- -- Andreas, 2013-10-25 double check solution before assigning -- -- Andreas, 2017-07-28 -- m <- lookupMeta x -- case mvJudgement m of -- IsSort{} -> return () -- skip double check since type of meta is not accurate -- HasType _ a -> do -- reportSDoc "tc.meta.check" 30 $ vcat -- [ text "double checking solution" -- , nest 2 $ prettyTCM vsol <+> text " : " <+> prettyTCM a -- ] -- dontAssignMetas $ checkInternal vsol a -- This can crash at assignTerm'! reportSDoc "tc.meta.assign" 10 $ text "solving" <+> prettyTCM x <+> text ":=" <+> prettyTCM vsol assignTerm x (telToArgs tel') v' -- | Turn the assignment problem @_X args <= SizeLt u@ into -- @_X args = SizeLt (_Y args)@ and constraint -- @_Y args <= u@. subtypingForSizeLt :: CompareDirection -- ^ @dir@ -> MetaId -- ^ The meta variable @x@. -> MetaVariable -- ^ Its associated information @mvar <- lookupMeta x@. -> Type -- ^ Its type @t = jMetaType $ mvJudgement mvar@ -> Args -- ^ Its arguments. -> Term -- ^ Its to-be-assigned value @v@, such that @x args `dir` v@. -> (Term -> TCM ()) -- ^ Continuation taking its possibly assigned value. -> TCM () subtypingForSizeLt DirEq x mvar t args v cont = cont v subtypingForSizeLt dir x mvar t args v cont = do let fallback = cont v -- Check whether we have built-ins SIZE and SIZELT (mSize, mSizeLt) <- getBuiltinSize caseMaybe mSize fallback $ \ qSize -> do caseMaybe mSizeLt fallback $ \ qSizeLt -> do -- Check whether v is a SIZELT v <- reduce v case ignoreSharing v of Def q [Apply (Arg ai u)] | q == qSizeLt -> do -- Clone the meta into a new size meta @y@. -- To this end, we swap the target of t for Size. TelV tel _ <- telView t let size = sizeType_ qSize t' = telePi tel size y <- newMeta (mvInfo mvar) (mvPriority mvar) (mvPermutation mvar) (HasType __IMPOSSIBLE__ t') -- Note: no eta-expansion of new meta possible/necessary. -- Add the size constraint @y args `dir` u@. let yArgs = MetaV y $ map Apply args addConstraint $ dirToCmp (`ValueCmp` size) dir yArgs u -- We continue with the new assignment problem, and install -- an exception handler, since we created a meta and a constraint, -- so we cannot fall back to the original handler. let xArgs = MetaV x $ map Apply args v' = Def qSizeLt [Apply $ Arg ai yArgs] c = dirToCmp (`ValueCmp` sizeUniv) dir xArgs v' catchConstraint c $ cont v' _ -> fallback -- | Eta-expand bound variables like @z@ in @X (fst z)@. expandProjectedVars :: (Normalise a, TermLike a, Show a, PrettyTCM a, NoProjectedVar a, Subst Term a, PrettyTCM b, Subst Term b) => a -> b -> (a -> b -> TCM c) -> TCM c expandProjectedVars args v ret = loop (args, v) where loop (args, v) = do reportSDoc "tc.meta.assign.proj" 45 $ text "meta args: " <+> prettyTCM args args <- etaContract =<< normalise args reportSDoc "tc.meta.assign.proj" 45 $ text "norm args: " <+> prettyTCM args reportSDoc "tc.meta.assign.proj" 85 $ text "norm args: " <+> text (show args) let done = ret args v case noProjectedVar args of Right () -> do reportSDoc "tc.meta.assign.proj" 40 $ text "no projected var found in args: " <+> prettyTCM args done Left (ProjVarExc i _) -> etaExpandProjectedVar i (args, v) done loop -- | Eta-expand a de Bruijn index of record type in context and passed term(s). etaExpandProjectedVar :: (PrettyTCM a, Subst Term a) => Int -> a -> TCM c -> (a -> TCM c) -> TCM c etaExpandProjectedVar i v fail succeed = do reportSDoc "tc.meta.assign.proj" 40 $ text "trying to expand projected variable" <+> prettyTCM (var i) caseMaybeM (etaExpandBoundVar i) fail $ \ (delta, sigma, tau) -> do reportSDoc "tc.meta.assign.proj" 25 $ text "eta-expanding var " <+> prettyTCM (var i) <+> text " in terms " <+> prettyTCM v inTopContext $ addContext delta $ succeed $ applySubst tau v -- | Check whether one of the meta args is a projected var. class NoProjectedVar a where noProjectedVar :: a -> Either ProjVarExc () data ProjVarExc = ProjVarExc Int [(ProjOrigin, QName)] instance NoProjectedVar Term where noProjectedVar t = case ignoreSharing t of Var i es | qs@(_:_) <- takeWhileJust id $ map isProjElim es -> Left $ ProjVarExc i qs -- Andreas, 2015-09-12 Issue #1316: -- Also look in inductive record constructors Con (ConHead _ Inductive (_:_)) _ vs -> noProjectedVar vs _ -> return () instance NoProjectedVar a => NoProjectedVar (Arg a) where noProjectedVar = Fold.mapM_ noProjectedVar instance NoProjectedVar a => NoProjectedVar [a] where noProjectedVar = Fold.mapM_ noProjectedVar {- UNUSED, BUT KEEP! -- Wrong attempt at expanding bound variables. -- The following code curries meta instead. -- | @etaExpandProjectedVar mvar x t n qs@ -- -- @mvar@ is the meta var info. -- @x@ is the meta variable we are trying to solve for. -- @t@ is its type. -- @n@ is the number of the meta arg we want to curry (starting at 0). -- @qs@ is the projection path along which we curry. -- etaExpandProjectedVar :: MetaVariable -> MetaId -> Type -> Int -> [QName] -> TCM a etaExpandProjectedVar mvar x t n qs = inTopContext $ do (_, uncurry, t') <- curryAt t n let TelV tel a = telView' t' perm = idP (size tel) y <- newMeta (mvInfo mvar) (mvPriority mvar) perm (HasType __IMPOSSIBLE__ t') assignTerm' x (uncurry $ MetaV y []) patternViolation -} {- -- first, strip the leading n domains (which remain unchanged) TelV gamma core <- telViewUpTo n t case ignoreSharing $ unEl core of -- There should be at least one domain left Pi (Dom ai a) b -> do -- Eta-expand @dom@ along @qs@ into a telescope @tel@, computing a substitution. -- For now, we only eta-expand once. -- This might trigger another call to @etaExpandProjectedVar@ later. -- A more efficient version does all the eta-expansions at once here. (r, pars, def) <- fromMaybe __IMPOSSIBLE__ <$> isRecordType a unless (recEtaEquality def) __IMPOSSIBLE__ let tel = recTel def `apply` pars m = size tel v = Con (recConHead def) $ map var $ downFrom m b' = raise m b `absApp` v fs = recFields def vs = zipWith (\ f i -> Var i [Proj f]) fs $ downFrom m -- v = c (n-1) ... 1 0 (tel, u) <- etaExpandAtRecordType a $ var 0 -- TODO: compose argInfo ai with tel. -- Substitute into @b@. -- Abstract over @tel@. -- Abstract over @gamma@. -- Create new meta. -- Solve old meta, using substitution. patternViolation _ -> __IMPOSSIBLE__ -} type FVs = Set.VarSet type SubstCand = [(Int,Term)] -- ^ a possibly non-deterministic substitution -- | Turn non-det substitution into proper substitution, if possible. -- Otherwise, raise the error. checkLinearity :: SubstCand -> ExceptT () TCM SubstCand checkLinearity ids0 = do let ids = List.sortBy (compare `on` fst) ids0 -- see issue 920 let grps = groupOn fst ids concat <$> mapM makeLinear grps where -- | Non-determinism can be healed if type is singleton. [Issue 593] -- (Same as for irrelevance.) makeLinear :: SubstCand -> ExceptT () TCM SubstCand makeLinear [] = __IMPOSSIBLE__ makeLinear grp@[_] = return grp makeLinear (p@(i,t) : _) = ifM ((Right True ==) <$> do isSingletonTypeModuloRelevance =<< typeOfBV i) (return [p]) (throwError ()) -- Intermediate result in the following function type Res = [(Arg Nat, Term)] -- | Exceptions raised when substitution cannot be inverted. data InvertExcept = CantInvert -- ^ Cannot recover. | NeutralArg -- ^ A potentially neutral arg: can't invert, but can try pruning. | ProjectedVar Int [(ProjOrigin, QName)] -- ^ Try to eta-expand var to remove projs. -- | Check that arguments @args@ to a metavar are in pattern fragment. -- Assumes all arguments already in whnf and eta-reduced. -- Parameters are represented as @Var@s so @checkArgs@ really -- checks that all args are @Var@s and returns the "substitution" -- to be applied to the rhs of the equation to solve. -- (If @args@ is considered a substitution, its inverse is returned.) -- -- The returned list might not be ordered. -- Linearity, i.e., whether the substitution is deterministic, -- has to be checked separately. -- inverseSubst :: Args -> ExceptT InvertExcept TCM SubstCand inverseSubst args = map (mapFst unArg) <$> loop (zip args terms) where loop = foldM isVarOrIrrelevant [] terms = map var (downFrom (size args)) failure = do lift $ reportSDoc "tc.meta.assign" 15 $ vcat [ text "not all arguments are variables: " <+> prettyTCM args , text " aborting assignment" ] throwError CantInvert neutralArg = throwError NeutralArg isVarOrIrrelevant :: Res -> (Arg Term, Term) -> ExceptT InvertExcept TCM Res isVarOrIrrelevant vars (arg, t) = case ignoreSharing <$> arg of -- i := x Arg info (Var i []) -> return $ (Arg info i, t) `cons` vars -- π i := x try to eta-expand projection π away! Arg _ (Var i es) | Just qs <- mapM isProjElim es -> throwError $ ProjectedVar i qs -- (i, j) := x becomes [i := fst x, j := snd x] -- Andreas, 2013-09-17 but only if constructor is fully applied Arg info (Con c ci vs) -> do let fallback | isIrrelevant info = return vars | otherwise = failure isRC <- lift $ isRecordConstructor $ conName c case isRC of Just (_, Record{ recFields = fs }) | length fs == length vs -> do let aux (Arg _ v) (Arg info' f) = (Arg ai v,) $ t `applyE` [Proj ProjSystem f] where ai = ArgInfo { argInfoHiding = min (getHiding info) (getHiding info') , argInfoRelevance = max (getRelevance info) (getRelevance info') , argInfoOrigin = min (getOrigin info) (getOrigin info') } res <- loop $ zipWith aux vs fs return $ res `append` vars | otherwise -> fallback Just _ -> __IMPOSSIBLE__ Nothing -> fallback -- An irrelevant argument which is not an irrefutable pattern is dropped Arg info _ | isIrrelevant info -> return vars -- Andreas, 2013-10-29 -- An irrelevant part can also be marked by a DontCare -- (coming from an irrelevant projection), see Issue 927: Arg _ DontCare{} -> return vars -- Distinguish args that can be eliminated (Con,Lit,Lam,unsure) ==> failure -- from those that can only put somewhere as a whole ==> neutralArg Arg _ Var{} -> neutralArg Arg _ Def{} -> neutralArg -- Note that this Def{} is in normal form and might be prunable. Arg _ Lam{} -> failure Arg _ Lit{} -> failure Arg _ MetaV{} -> failure Arg _ Pi{} -> neutralArg Arg _ Sort{} -> neutralArg Arg _ Level{} -> neutralArg Arg info (Shared p) -> isVarOrIrrelevant vars (Arg info $ derefPtr p, t) -- managing an assoc list where duplicate indizes cannot be irrelevant vars append :: Res -> Res -> Res append res vars = foldr cons vars res -- adding an irrelevant entry only if not present cons :: (Arg Nat, Term) -> Res -> Res cons a@(Arg (ArgInfo _ Irrelevant _) i, t) vars | any ((i==) . unArg . fst) vars = vars | otherwise = a : vars -- adding a relevant entry: cons a@(Arg info i, t) vars = a : -- filter out duplicate irrelevants filter (not . (\ a@(Arg info j, t) -> isIrrelevant info && i == j)) vars -- UNUSED -- -- | Used in 'Agda.Interaction.BasicOps.giveExpr'. -- updateMeta :: MetaId -> Term -> TCM () -- updateMeta mI v = do -- mv <- lookupMeta mI -- withMetaInfo' mv $ do -- args <- getContextArgs -- noConstraints $ assignV DirEq mI args v -- | Turn open metas into postulates. -- -- Preconditions: -- -- 1. We are 'inTopContext'. -- -- 2. 'envCurrentModule' is set to the top-level module. -- openMetasToPostulates :: TCM () openMetasToPostulates = do m <- asks envCurrentModule -- Go through all open metas. ms <- Map.assocs <$> use stMetaStore forM_ ms $ \ (x, mv) -> do when (isOpenMeta $ mvInstantiation mv) $ do let t = jMetaType $ mvJudgement mv -- Create a name for the new postulate. let r = clValue $ miClosRange $ mvInfo mv -- s <- render <$> prettyTCM x -- Using _ is a bad idea, as it prints as prefix op let s = "unsolved#meta." ++ show (metaId x) n <- freshName r s let q = A.QName m n -- Debug. reportSDoc "meta.postulate" 20 $ vcat [ text ("Turning " ++ if isSortMeta_ mv then "sort" else "value" ++ " meta ") <+> prettyTCM x <+> text " into postulate." , nest 2 $ vcat [ text "Name: " <+> prettyTCM q , text "Type: " <+> prettyTCM t ] ] -- Add the new postulate to the signature. addConstant q $ defaultDefn defaultArgInfo q t Axiom -- Solve the meta. let inst = InstV [] $ Def q [] stMetaStore %= Map.adjust (\ mv0 -> mv0 { mvInstantiation = inst }) x return () Agda-2.5.3/src/full/Agda/TypeChecking/Records.hs-boot0000644000000000000000000000103013154613124020375 0ustar0000000000000000 module Agda.TypeChecking.Records where import Agda.Syntax.Common import Agda.Syntax.Internal import qualified Agda.Syntax.Concrete.Name as C import Agda.TypeChecking.Monad isRecord :: HasConstInfo m => QName -> m (Maybe Defn) isEtaRecord :: HasConstInfo m => QName -> m Bool getRecordFieldNames :: QName -> TCM [Arg C.Name] etaContractRecord :: HasConstInfo m => QName -> ConHead -> ConInfo -> Args -> m Term isGeneratedRecordConstructor :: QName -> TCM Bool isRecordConstructor :: HasConstInfo m => QName -> m (Maybe (QName, Defn)) Agda-2.5.3/src/full/Agda/TypeChecking/Injectivity.hs0000644000000000000000000002447613154613124020357 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Injectivity where import Prelude hiding (mapM) import Control.Applicative import Control.Monad.State hiding (mapM, forM) import Control.Monad.Reader hiding (mapM, forM) import Data.Map (Map) import qualified Data.Map as Map import qualified Data.Set as Set import Data.Maybe import Data.Traversable hiding (for) import qualified Agda.Syntax.Abstract.Name as A import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.TypeChecking.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Reduce import Agda.TypeChecking.Primitive import {-# SOURCE #-} Agda.TypeChecking.MetaVars import {-# SOURCE #-} Agda.TypeChecking.Conversion import Agda.TypeChecking.Pretty import Agda.TypeChecking.Constraints import Agda.TypeChecking.Polarity import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Permutation import Agda.Utils.Pretty ( prettyShow ) #include "undefined.h" import Agda.Utils.Impossible headSymbol :: Term -> TCM (Maybe TermHead) headSymbol v = do -- ignoreAbstractMode $ do -- Andreas, 2013-02-18 ignoreAbstractMode leads to information leakage v <- ignoreBlocking <$> reduceHead v case ignoreSharing v of Def f _ -> do let yes = return $ Just $ ConsHead f no = return $ Nothing def <- theDef <$> do ignoreAbstractMode $ getConstInfo f -- Andreas, 2013-02-18 -- if we do not ignoreAbstractMode here, abstract Functions get turned -- into Axioms, but we want to distinguish these. case def of Datatype{} -> yes Record{} -> yes Axiom{} -> do reportSLn "tc.inj.axiom" 50 $ "headSymbol: " ++ prettyShow f ++ " is an Axiom." -- Don't treat axioms in the current mutual block -- as constructors (they might have definitions we -- don't know about yet). caseMaybeM (asks envMutualBlock) yes $ \ mb -> do fs <- mutualNames <$> lookupMutualBlock mb if Set.member f fs then no else yes Function{} -> no Primitive{} -> no Constructor{} -> __IMPOSSIBLE__ AbstractDefn{}-> __IMPOSSIBLE__ Con c _ _ -> return (Just $ ConsHead $ conName c) Sort _ -> return (Just SortHead) Pi _ _ -> return (Just PiHead) Lit _ -> return Nothing -- handle literal heads as well? can't think of -- any examples where it would be useful... Lam{} -> return Nothing Var{} -> return Nothing Level{} -> return Nothing MetaV{} -> return Nothing DontCare{} -> return Nothing Shared{} -> __IMPOSSIBLE__ checkInjectivity :: QName -> [Clause] -> TCM FunctionInverse checkInjectivity f cs | pointLess cs = do reportSLn "tc.inj.check.pointless" 20 $ "Injectivity of " ++ prettyShow (A.qnameToConcrete f) ++ " would be pointless." return NotInjective where -- Is it pointless to use injectivity for this function? pointLess [] = True pointLess (_:_:_) = False pointLess [cl] = not $ any (properlyMatching . namedArg) $ namedClausePats cl -- Andreas, 2014-06-12 -- If we only have record patterns, it is also pointless. -- We need at least one proper match. checkInjectivity f cs = do reportSLn "tc.inj.check" 40 $ "Checking injectivity of " ++ prettyShow f -- Extract the head symbol of the rhs of each clause (skip absurd clauses) es <- catMaybes <$> do forM cs $ \ c -> do -- produces a list ... mapM ((,c) <.> headSymbol) $ clauseBody c -- ... of maybes let (hs, ps) = unzip es reportSLn "tc.inj.check" 40 $ " right hand sides: " ++ show hs if all isJust hs && distinct hs then do let inv = Map.fromList (map fromJust hs `zip` ps) reportSLn "tc.inj.check" 20 $ prettyShow f ++ " is injective." reportSDoc "tc.inj.check" 30 $ nest 2 $ vcat $ for (Map.toList inv) $ \ (h, c) -> text (prettyShow h) <+> text "-->" <+> fsep (punctuate comma $ map (prettyTCM . namedArg) $ namedClausePats c) return $ Inverse inv else return NotInjective -- | Argument should be in weak head normal form. functionInverse :: Term -> TCM InvView functionInverse v = case ignoreSharing v of Def f es -> do d <- theDef <$> getConstInfo f case d of Function{ funInv = inv } -> case inv of NotInjective -> return NoInv Inverse m -> return $ Inv f es m -- NB: Invertible functions are never classified as -- projection-like, so this is fine, we are not -- missing parameters. (Andreas, 2013-11-01) _ -> return NoInv _ -> return NoInv data InvView = Inv QName [Elim] (Map TermHead Clause) | NoInv data MaybeAbort = Abort | KeepGoing useInjectivity :: Comparison -> Type -> Term -> Term -> TCM () useInjectivity cmp a u v = do reportSDoc "tc.inj.use" 30 $ fsep $ pwords "useInjectivity on" ++ [ prettyTCM u, prettyTCM cmp, prettyTCM v, text ":", prettyTCM a ] uinv <- functionInverse u vinv <- functionInverse v case (uinv, vinv) of -- Andreas, Francesco, 2014-06-12: -- We know that one of u,v is neutral -- (see calls to useInjectivity in Conversion.hs). -- Otherwise, (e.g. if both were Blocked), the following case would be -- unsound, since it assumes the arguments to be pointwise equal. -- It would deliver non-unique solutions for metas. (Inv f fArgs _, Inv g gArgs _) | f == g -> do a <- defType <$> getConstInfo f reportSDoc "tc.inj.use" 20 $ vcat [ fsep (pwords "comparing application of injective function" ++ [prettyTCM f] ++ pwords "at") , nest 2 $ fsep $ punctuate comma $ map prettyTCM fArgs , nest 2 $ fsep $ punctuate comma $ map prettyTCM gArgs , nest 2 $ text "and type" <+> prettyTCM a ] pol <- getPolarity' cmp f compareElims pol a (Def f []) fArgs gArgs | otherwise -> fallBack (Inv f args inv, NoInv) -> do a <- defType <$> getConstInfo f reportSDoc "tc.inj.use" 20 $ fsep $ pwords "inverting injective function" ++ [ prettyTCM f, text ":", prettyTCM a, text "for", prettyTCM v , parens $ text "args =" <+> prettyList (map prettyTCM args) ] invert u f a inv args =<< headSymbol v (NoInv, Inv g args inv) -> do a <- defType <$> getConstInfo g reportSDoc "tc.inj.use" 20 $ fsep $ pwords "inverting injective function" ++ [ prettyTCM g, text ":", prettyTCM a, text "for", prettyTCM u , parens $ text "args =" <+> prettyList (map prettyTCM args) ] invert v g a inv args =<< headSymbol u (NoInv, NoInv) -> fallBack where fallBack = addConstraint $ ValueCmp cmp a u v invert :: Term -> QName -> Type -> Map TermHead Clause -> [Elim] -> Maybe TermHead -> TCM () invert _ _ a inv args Nothing = fallBack invert org f ftype inv args (Just h) = case Map.lookup h inv of Nothing -> typeError $ UnequalTerms cmp u v a Just cl@Clause{ clauseTel = tel } -> maybeAbort $ do let ps = clausePats cl perm = fromMaybe __IMPOSSIBLE__ $ clausePerm cl -- These are what dot patterns should be instantiated at ms <- map unArg <$> newTelMeta tel reportSDoc "tc.inj.invert" 20 $ vcat [ text "meta patterns" <+> prettyList (map prettyTCM ms) , text " perm =" <+> text (show perm) , text " tel =" <+> prettyTCM tel , text " ps =" <+> prettyList (map (text . show) ps) ] -- and this is the order the variables occur in the patterns let msAux = permute (invertP __IMPOSSIBLE__ $ compactP perm) ms let sub = parallelS (reverse ms) margs <- runReaderT (evalStateT (mapM metaElim ps) msAux) sub reportSDoc "tc.inj.invert" 20 $ vcat [ text "inversion" , nest 2 $ vcat [ text "lhs =" <+> prettyTCM margs , text "rhs =" <+> prettyTCM args , text "type =" <+> prettyTCM ftype ] ] -- Since we do not care for the value of non-variant metas here, -- we can treat 'Nonvariant' as 'Invariant'. -- That ensures these metas do not remain unsolved. pol <- purgeNonvariant <$> getPolarity' cmp f -- The clause might not give as many patterns as there -- are arguments (point-free style definitions). let args' = take (length margs) args compareElims pol ftype (Def f []) margs args' {- Andreas, 2011-05-09 allow unsolved constraints as long as progress unless (null cs) $ do reportSDoc "tc.inj.invert" 30 $ text "aborting inversion; remaining constraints" <+> prettyTCM cs patternViolation -} -- Check that we made progress, i.e. the head symbol -- of the original term should be a constructor. org <- reduce org h <- headSymbol org case h of Just h -> KeepGoing <$ compareTerm cmp a u v Nothing -> do reportSDoc "tc.inj.invert" 30 $ vcat [ text "aborting inversion;" <+> prettyTCM org , text "plainly," <+> text (show org) , text "has TermHead" <+> text (prettyShow h) , text "which does not expose a constructor" ] return Abort maybeAbort m = do (a, s) <- localTCStateSaving m case a of KeepGoing -> put s Abort -> fallBack nextMeta = do m : ms <- get put ms return m dotP :: Monad m => Term -> StateT [Term] (ReaderT Substitution m) Term dotP v = do sub <- ask return $ applySubst sub v metaElim (Arg _ (ProjP o p)) = lift $ lift $ Proj o <$> getOriginalProjection p metaElim (Arg info p) = Apply . Arg info <$> metaPat p metaArgs args = mapM (traverse $ metaPat . namedThing) args metaPat (DotP v) = dotP v metaPat (VarP _) = nextMeta metaPat (AbsurdP p) = metaPat p metaPat (ConP c mt args) = Con c (fromConPatternInfo mt) <$> metaArgs args metaPat (LitP l) = return $ Lit l metaPat ProjP{} = __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/Datatypes.hs0000644000000000000000000002304013154613124017776 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Datatypes where import Data.Maybe (fromMaybe) import qualified Data.List as List import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin (constructorForm) import Agda.TypeChecking.Telescope import Agda.TypeChecking.Substitute import Agda.TypeChecking.Pretty import Agda.Utils.Either import Agda.Utils.Functor import Agda.Utils.Pretty ( prettyShow ) import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Constructors --------------------------------------------------------------------------- -- | Get true constructor with record fields. getConHead :: QName -> TCM (Either SigError ConHead) getConHead c = mapRight (conSrcCon . theDef) <$> getConstInfo' c -- | Get true constructor with fields, expanding literals to constructors -- if possible. getConForm :: QName -> TCM (Either SigError ConHead) getConForm c = caseEitherM (getConHead c) (return . Left) $ \ ch -> do Con con _ [] <- ignoreSharing <$> constructorForm (Con ch ConOCon []) return $ Right con -- | Augment constructor with record fields (preserve constructor name). -- The true constructor might only surface via 'reduce'. getOrigConHead :: QName -> TCM (Either SigError ConHead) getOrigConHead c = mapRight (setConName c) <$> getConHead c -- | Get the name of the datatype constructed by a given constructor. -- Precondition: The argument must refer to a constructor {-# SPECIALIZE getConstructorData :: QName -> TCM QName #-} getConstructorData :: HasConstInfo m => QName -> m QName getConstructorData c = do def <- getConstInfo c case theDef def of Constructor{conData = d} -> return d _ -> __IMPOSSIBLE__ -- | @getConType c t@ computes the constructor parameters from type @t@ -- and returns them plus the instantiated type of constructor @c@. -- This works also if @t@ is a function type ending in a data/record type; -- the term from which @c@ comes need not be fully applied -- -- @Nothing@ if @t@ is not a data/record type or does not have -- a constructor @c@. getConType :: ConHead -- ^ Constructor. -> Type -- ^ Ending in data/record type. -> TCM (Maybe ((QName, Type, Args), Type)) -- ^ @Nothing@ if not ends in data or record type. -- -- @Just ((d, dt, pars), ct)@ otherwise, where -- @d@ is the data or record type name, -- @dt@ is the type of the data or record name, -- @pars@ are the reconstructed parameters, -- @ct@ is the type of the constructor instantiated to the parameters. getConType c t = do reportSDoc "tc.getConType" 30 $ sep $ [ text "getConType: constructor " , prettyTCM c , text " at type " , prettyTCM t ] TelV tel t <- telView t -- Now @t@ lives under @tel@, we need to remove the dependency on @tel@. -- This will succeed if @t@ is indeed a data/record type that is the -- type of a constructor coming from a term -- (applied to at least the parameters). -- Note: @t@ will have some unbound deBruijn indices if view outside of @tel@. reportSLn "tc.getConType" 35 $ " target type: " ++ prettyShow t applySubst (strengthenS __IMPOSSIBLE__ (size tel)) <$> do addContext tel $ getFullyAppliedConType c t -- Andreas, 2017-08-18, issue #2703: -- The original code -- getFullyAppliedConType c $ applySubst (strengthenS __IMPOSSIBLE__ (size tel)) t -- crashes because substitution into @Def@s is slightly too strict -- (see @defApp@ and @canProject@). -- Strengthening the parameters after the call to @getFullyAppliedConType@ -- does not produce intermediate terms with __IMPOSSIBLE__s and this thus -- robust wrt. strictness/laziness of substitution. -- | @getFullyAppliedConType c t@ computes the constructor parameters -- from data type @t@ and returns them -- plus the instantiated type of constructor @c@. -- -- @Nothing@ if @t@ is not a data/record type or does not have -- a constructor @c@. -- -- Precondition: @t@ is reduced. getFullyAppliedConType :: ConHead -- ^ Constructor. -> Type -- ^ Reduced type of the fully applied constructor. -> TCM (Maybe ((QName, Type, Args), Type)) -- ^ @Nothing@ if not data or record type. -- -- @Just ((d, dt, pars), ct)@ otherwise, where -- @d@ is the data or record type name, -- @dt@ is the type of the data or record name, -- @pars@ are the reconstructed parameters, -- @ct@ is the type of the constructor instantiated to the parameters. getFullyAppliedConType c t = do reportSLn "tc.getConType" 35 $ List.intercalate " " $ [ "getFullyAppliedConType", prettyShow c, prettyShow t ] c <- fromRight __IMPOSSIBLE__ <$> do getConHead $ conName c case ignoreSharing $ unEl t of -- Note that if we come e.g. from getConType, -- then the non-parameter arguments of @es@ might contain __IMPOSSIBLE__ -- coming from strengthening. (Thus, printing them is not safe.) Def d es -> do reportSLn "tc.getConType" 35 $ List.intercalate " " $ [ "getFullyAppliedConType: case Def", prettyShow d, prettyShow es ] def <- getConstInfo d let cont n = do -- At this point we can be sure that the parameters are well-scoped. let pars = fromMaybe __IMPOSSIBLE__ $ allApplyElims $ take n es Just . ((d, defType def, pars),) <$> do (`piApplyM` pars) . defType =<< getConInfo c case theDef def of Datatype { dataPars = n, dataCons = cs } | conName c `elem` cs -> cont n Record { recPars = n, recConHead = con } | c == con -> cont n _ -> return Nothing _ -> return Nothing data HasEta = NoEta | YesEta deriving (Eq) data ConstructorInfo = DataCon Nat -- ^ Arity. | RecordCon HasEta [Arg QName] -- ^ List of field names. -- | Return the number of non-parameter arguments to a data constructor, -- or the field names of a record constructor. -- -- For getting just the arity of constructor @c@, -- use @either id size <$> getConstructorArity c@. getConstructorInfo :: QName -> TCM ConstructorInfo getConstructorInfo c = do (theDef <$> getConstInfo c) >>= \case Constructor{ conData = d, conArity = n } -> do (theDef <$> getConstInfo d) >>= \case r@Record{ recFields = fs } -> return $ RecordCon (if recEtaEquality r then YesEta else NoEta) fs Datatype{} -> return $ DataCon n _ -> __IMPOSSIBLE__ _ -> __IMPOSSIBLE__ --------------------------------------------------------------------------- -- * Data types --------------------------------------------------------------------------- -- | Check if a name refers to a datatype or a record with a named constructor. isDatatype :: QName -> TCM Bool isDatatype d = do def <- getConstInfo d case theDef def of Datatype{} -> return True Record{recNamedCon = namedC} -> return namedC _ -> return False -- | Check if a name refers to a datatype or a record. isDataOrRecordType :: QName -> TCM (Maybe DataOrRecord) isDataOrRecordType d = do def <- getConstInfo d case theDef def of Datatype{} -> return $ Just IsData Record{} -> return $ Just IsRecord _ -> return $ Nothing -- | Precodition: 'Term' is 'reduce'd. isDataOrRecord :: Term -> TCM (Maybe QName) isDataOrRecord v = do case ignoreSharing v of Def d _ -> fmap (const d) <$> isDataOrRecordType d _ -> return Nothing getNumberOfParameters :: QName -> TCM (Maybe Nat) getNumberOfParameters d = do def <- getConstInfo d case theDef def of Datatype{ dataPars = n } -> return $ Just n Record{ recPars = n } -> return $ Just n Constructor{ conPars = n } -> return $ Just n _ -> return Nothing -- | Precondition: Name is a data or record type. getConstructors :: QName -> TCM [QName] getConstructors d = do def <- theDef <$> getConstInfo d case def of Datatype{dataCons = cs} -> return cs Record{recConHead = h} -> return [conName h] _ -> __IMPOSSIBLE__ {- UNUSED data DatatypeInfo = DataInfo { datatypeName :: QName , datatypeParTel :: Telescope , datatypePars :: Args , datatypeIxTel :: Telescope , datatypeIxs :: Args } -- | Get the name and parameters from a type if it's a datatype or record type -- with a named constructor. getDatatypeInfo :: Type -> TCM (Maybe DatatypeInfo) getDatatypeInfo t = do t <- reduce t case unEl t of Def d args -> do n <- getDefFreeVars d args <- return $ genericDrop n args def <- instantiateDef =<< getConstInfo d TelV tel _ <- telView (defType def) let npars = case theDef def of Datatype{dataPars = np} -> Just np Record{recPars = np, recNamedCon = True} | genericLength args == np -> Just np | otherwise -> __IMPOSSIBLE__ _ -> Nothing return $ do np <- npars let (pt, it) = genericSplitAt np $ telToList tel parTel = telFromList pt ixTel = telFromList it (ps, is) = genericSplitAt np args return $ DataInfo { datatypeName = d , datatypeParTel = parTel , datatypePars = ps , datatypeIxTel = ixTel , datatypeIxs = is } _ -> return Nothing -} Agda-2.5.3/src/full/Agda/TypeChecking/Pretty.hs0000644000000000000000000004156713154613124017345 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE UndecidableInstances #-} -- To define <>, we will probably need to add: --import Prelude hiding ((<>)) -- but using that now gives warnings and doesn't silence -Wsemigroup #if __GLASGOW_HASKELL__ >= 800 {-# OPTIONS_GHC -Wno-semigroup #-} #endif #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE OverlappingInstances #-} #endif module Agda.TypeChecking.Pretty where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import Data.Maybe import Agda.Syntax.Position import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Literal import Agda.Syntax.Translation.InternalToAbstract import Agda.Syntax.Translation.ReflectedToAbstract import Agda.Syntax.Translation.AbstractToConcrete import qualified Agda.Syntax.Translation.ReflectedToAbstract as R import qualified Agda.Syntax.Reflected as R import qualified Agda.Syntax.Abstract as A import qualified Agda.Syntax.Concrete as C import qualified Agda.Syntax.Reflected as R import qualified Agda.Syntax.Abstract.Pretty as AP import Agda.Syntax.Concrete.Pretty (bracesAndSemicolons) import qualified Agda.Syntax.Concrete.Pretty as CP import qualified Agda.Syntax.Info as A import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin (equalityUnview) import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Substitute import Agda.Utils.Graph.AdjacencyMap.Unidirectional (Graph) import qualified Agda.Utils.Graph.AdjacencyMap.Unidirectional as Graph import Agda.Utils.Maybe import Agda.Utils.Null import Agda.Utils.Permutation (Permutation) import Agda.Utils.Pretty (Pretty, prettyShow) import qualified Agda.Utils.Pretty as P #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Wrappers for pretty printing combinators --------------------------------------------------------------------------- type Doc = P.Doc comma, colon, equals :: TCM Doc comma = return P.comma colon = return P.colon equals = return P.equals pretty :: P.Pretty a => a -> TCM Doc pretty x = return $ P.pretty x prettyA :: (P.Pretty c, ToConcrete a c) => a -> TCM Doc prettyA x = AP.prettyA x prettyAs :: (P.Pretty c, ToConcrete a [c]) => a -> TCM Doc prettyAs x = AP.prettyAs x text :: String -> TCM Doc text s = return $ P.text s multiLineText :: String -> TCM Doc multiLineText s = return $ P.multiLineText s pwords :: String -> [TCM Doc] pwords s = map return $ P.pwords s fwords :: String -> TCM Doc fwords s = return $ P.fwords s sep, fsep, hsep, hcat, vcat :: [TCM Doc] -> TCM Doc sep ds = P.sep <$> sequence ds fsep ds = P.fsep <$> sequence ds hsep ds = P.hsep <$> sequence ds hcat ds = P.hcat <$> sequence ds vcat ds = P.vcat <$> sequence ds hang :: TCM Doc -> Int -> TCM Doc -> TCM Doc hang p n q = P.hang <$> p <*> pure n <*> q infixl 6 <>, <+>, infixl 5 $$, $+$ ($$), ($+$), (<>), (<+>), () :: TCM Doc -> TCM Doc -> TCM Doc d1 $$ d2 = (P.$$) <$> d1 <*> d2 d1 $+$ d2 = (P.$+$) <$> d1 <*> d2 d1 <> d2 = (P.<>) <$> d1 <*> d2 d1 <+> d2 = (P.<+>) <$> d1 <*> d2 d1 d2 = (P.) <$> d1 <*> d2 nest :: Int -> TCM Doc -> TCM Doc nest n d = P.nest n <$> d braces, dbraces, brackets, parens :: TCM Doc -> TCM Doc braces d = P.braces <$> d dbraces d = CP.dbraces <$> d brackets d = P.brackets <$> d parens d = P.parens <$> d pshow :: Show a => a -> TCM Doc pshow = pure . P.pshow -- | Comma-separated list in brackets. prettyList :: [TCM Doc] -> TCM Doc prettyList ds = P.pretty <$> sequence ds -- | 'prettyList' without the brackets. prettyList_ :: [TCM Doc] -> TCM Doc prettyList_ ds = fsep $ punctuate comma ds punctuate :: TCM Doc -> [TCM Doc] -> [TCM Doc] punctuate _ [] = [] punctuate d ds = zipWith (<>) ds (replicate n d ++ [empty]) where n = length ds - 1 --------------------------------------------------------------------------- -- * The PrettyTCM class --------------------------------------------------------------------------- class PrettyTCM a where prettyTCM :: a -> TCM Doc instance PrettyTCM Bool where prettyTCM = pretty instance PrettyTCM C.Name where prettyTCM = pretty instance PrettyTCM C.QName where prettyTCM = pretty instance PrettyTCM Comparison where prettyTCM = pretty instance PrettyTCM Literal where prettyTCM = pretty instance PrettyTCM Nat where prettyTCM = pretty instance PrettyTCM ProblemId where prettyTCM = pretty instance PrettyTCM Range where prettyTCM = pretty -- instance PrettyTCM Interval where prettyTCM = pretty -- instance PrettyTCM Position where prettyTCM = pretty instance PrettyTCM a => PrettyTCM (Closure a) where prettyTCM cl = enterClosure cl prettyTCM instance PrettyTCM a => PrettyTCM [a] where prettyTCM = prettyList . map prettyTCM instance (PrettyTCM a, PrettyTCM b) => PrettyTCM (a,b) where prettyTCM (a, b) = parens $ prettyTCM a <> comma <> prettyTCM b instance (PrettyTCM a, PrettyTCM b, PrettyTCM c) => PrettyTCM (a,b,c) where prettyTCM (a, b, c) = parens $ prettyTCM a <> comma <> prettyTCM b <> comma <> prettyTCM c instance PrettyTCM Term where prettyTCM = prettyA <=< reify instance PrettyTCM Type where prettyTCM = prettyA <=< reify instance PrettyTCM Sort where prettyTCM = prettyA <=< reify instance PrettyTCM DisplayTerm where prettyTCM = prettyA <=< reify instance PrettyTCM NamedClause where prettyTCM = prettyA <=< reify instance PrettyTCM (QNamed Clause) where prettyTCM = prettyA <=< reify instance PrettyTCM Level where prettyTCM = prettyA <=< reify . Level instance PrettyTCM Permutation where prettyTCM = text . show instance PrettyTCM Polarity where prettyTCM = text . show instance PrettyTCM R.Term where prettyTCM = prettyA <=< toAbstractWithoutImplicit instance (Pretty a, PrettyTCM a, Subst a a) => PrettyTCM (Substitution' a) where prettyTCM IdS = text "idS" prettyTCM (Wk m IdS) = text "wkS" <+> pretty m prettyTCM (EmptyS _) = text "emptyS" prettyTCM rho = prettyTCM u <+> comma <+> prettyTCM rho1 where (rho1, rho2) = splitS 1 rho u = lookupS rho2 0 instance PrettyTCM ModuleParameters where prettyTCM = prettyTCM . mpSubstitution instance PrettyTCM Clause where prettyTCM cl = do x <- qualify_ <$> freshName_ "" prettyTCM (QNamed x cl) instance PrettyTCM a => PrettyTCM (Judgement a) where prettyTCM (HasType a t) = prettyTCM a <+> text ":" <+> prettyTCM t prettyTCM (IsSort a t) = text "Sort" <+> prettyTCM a <+> text ":" <+> prettyTCM t instance PrettyTCM MetaId where prettyTCM x = do mn <- getMetaNameSuggestion x pretty $ NamedMeta mn x instance PrettyTCM a => PrettyTCM (Blocked a) where prettyTCM (Blocked x a) = text "[" <+> prettyTCM a <+> text "]" <> text (P.prettyShow x) prettyTCM (NotBlocked _ x) = prettyTCM x instance (Reify a e, ToConcrete e c, P.Pretty c) => PrettyTCM (Named_ a) where prettyTCM x = prettyA =<< reify x instance (Reify a e, ToConcrete e c, P.Pretty c) => PrettyTCM (Arg a) where prettyTCM x = prettyA =<< reify x instance (Reify a e, ToConcrete e c, P.Pretty c) => PrettyTCM (Dom a) where prettyTCM x = prettyA =<< reify x instance (PrettyTCM k, PrettyTCM v) => PrettyTCM (Map k v) where prettyTCM m = text "Map" <> braces (sep $ punctuate comma [ hang (prettyTCM k <+> text "=") 2 (prettyTCM v) | (k, v) <- Map.toList m ]) #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} PrettyTCM ArgName where #else instance PrettyTCM ArgName where #endif prettyTCM = text . P.prettyShow -- instance (Reify a e, ToConcrete e c, P.Pretty c, PrettyTCM a) => PrettyTCM (Elim' a) where instance PrettyTCM Elim where prettyTCM (Apply v) = text "$" <+> prettyTCM v prettyTCM (Proj _ f)= text "." <> prettyTCM f instance PrettyTCM a => PrettyTCM (MaybeReduced a) where prettyTCM = prettyTCM . ignoreReduced instance PrettyTCM EqualityView where prettyTCM v = prettyTCM $ equalityUnview v instance PrettyTCM A.Expr where prettyTCM = prettyA instance PrettyTCM A.TypedBinding where prettyTCM = prettyA instance PrettyTCM Relevance where prettyTCM Irrelevant = text "." prettyTCM NonStrict = text ".." prettyTCM Relevant = empty prettyTCM Forced{} = empty instance PrettyTCM ProblemConstraint where prettyTCM (PConstr pids c) | Set.null pids = prettyTCM c | otherwise = prettyList (map prettyTCM $ Set.toList pids) <+> prettyTCM c instance PrettyTCM Constraint where prettyTCM c = case c of ValueCmp cmp ty s t -> sep [ sep [ prettyTCM s , prettyTCM cmp <+> prettyTCM t ] , nest 2 $ text ":" <+> prettyTCM ty ] ElimCmp cmps t v us vs -> sep [ sep [ prettyTCM us , nest 2 $ text "~~" <+> prettyTCM vs ] , text ":" <+> prettyTCM t ] LevelCmp cmp a b -> sep [ prettyTCM a , prettyTCM cmp <+> prettyTCM b ] TypeCmp cmp a b -> sep [ prettyTCM a , prettyTCM cmp <+> prettyTCM b ] TelCmp a b cmp tela telb -> sep [ prettyTCM tela , prettyTCM cmp <+> prettyTCM telb ] SortCmp cmp s1 s2 -> sep [ prettyTCM s1 , prettyTCM cmp <+> prettyTCM s2 ] Guarded c pid -> sep [ prettyTCM c , nest 2 $ brackets $ text "blocked on problem" <+> prettyTCM pid ] UnBlock m -> do -- BlockedConst t <- mvInstantiation <$> lookupMeta m mi <- mvInstantiation <$> lookupMeta m case mi of BlockedConst t -> sep [ pretty m <+> text ":=" , nest 2 $ prettyTCM t ] PostponedTypeCheckingProblem cl _ -> enterClosure cl $ \p -> sep [ pretty m <+> text ":=" , nest 2 $ prettyTCM p ] Open{} -> __IMPOSSIBLE__ OpenIFS{} -> __IMPOSSIBLE__ InstV{} -> empty -- Andreas, 2017-01-11, issue #2637: -- The size solver instantiates some metas with infinity -- without cleaning up the UnBlock constraints. -- Thus, this case is not IMPOSSIBLE. -- -- InstV args t -> do -- reportSLn "impossible" 10 $ unlines -- [ "UnBlock meta " ++ show m ++ " surprisingly has InstV instantiation:" -- , show m ++ show args ++ " := " ++ show t -- ] -- __IMPOSSIBLE__ FindInScope m mb mcands -> do t <- getMetaType m sep [ hang (text "Resolve instance argument" <+> blk) 2 $ hang (pretty m <+> text ":") 2 $ prettyTCM t , cands ] where blk = case mb of Nothing -> empty Just b -> parens $ text "blocked on" <+> pretty b cands = case mcands of Nothing -> text "No candidates yet" Just cnds -> hang (text "Candidates") 2 $ vcat [ hang (overlap c <+> prettyTCM (candidateTerm c) <+> text ":") 2 $ prettyTCM (candidateType c) | c <- cnds ] where overlap c | candidateOverlappable c = text "overlap" | otherwise = empty IsEmpty r t -> sep [ text "Is empty:", nest 2 $ prettyTCM t ] CheckSizeLtSat t -> sep [ text "Is not empty type of sizes:", nest 2 $ prettyTCM t ] instance PrettyTCM TypeCheckingProblem where prettyTCM (CheckExpr e a) = sep [ prettyA e <+> text ":?", prettyTCM a ] prettyTCM (CheckArgs _ _ es t0 t1 _) = sep [ parens $ text "_ :" <+> prettyTCM t0 , nest 2 $ prettyList $ map prettyA es , nest 2 $ text ":?" <+> prettyTCM t1 ] prettyTCM (CheckLambda (Arg ai (xs, mt)) e t) = sep [ return CP.lambda <+> (CP.prettyRelevance ai . CP.prettyHiding ai (if isNothing mt && length xs == 1 then id else P.parens) <$> do fsep $ map prettyTCM xs ++ caseMaybe mt [] (\ a -> [text ":", prettyTCM a])) <+> return CP.arrow <+> prettyTCM e <+> text ":?" , prettyTCM t ] prettyTCM (UnquoteTactic v _ _) = do e <- reify v let noInfo = A.exprNoRange prettyTCM (A.App noInfo (A.Unquote noInfo) (defaultNamedArg e)) instance PrettyTCM a => PrettyTCM (WithHiding a) where prettyTCM (WithHiding h a) = CP.prettyHiding h id <$> prettyTCM a instance PrettyTCM Name where prettyTCM x = P.pretty <$> abstractToConcrete_ x instance PrettyTCM QName where prettyTCM x = P.pretty <$> abstractToConcrete_ x instance PrettyTCM ModuleName where prettyTCM x = P.pretty <$> abstractToConcrete_ x instance PrettyTCM ConHead where prettyTCM = prettyTCM . conName instance PrettyTCM Telescope where prettyTCM tel = P.fsep . map P.pretty <$> (do tel <- reify tel runAbsToCon $ bindToConcrete tel (return . concat) ) newtype PrettyContext = PrettyContext Context instance PrettyTCM PrettyContext where prettyTCM (PrettyContext ctx) = prettyTCM $ telFromList' nameToArgName $ map ctxEntry $ reverse ctx instance PrettyTCM Context where prettyTCM = prettyTCM . PrettyContext instance PrettyTCM CtxId where prettyTCM (CtxId x) = prettyTCM x instance PrettyTCM DBPatVar where prettyTCM = prettyTCM . var . dbPatVarIndex instance PrettyTCM a => PrettyTCM (Pattern' a) where prettyTCM (VarP x) = prettyTCM x prettyTCM (DotP t) = text ".(" <> prettyTCM t <> text ")" prettyTCM (AbsurdP _) = text absurdPatternName prettyTCM (ConP c i ps) = (if b then braces else parens) $ prTy $ prettyTCM c <+> fsep (map (prettyTCM . namedArg) ps) where b = maybe False (/= ConOCon) $ conPRecord i showRec :: TCM Doc showRec = sep [ text "record" , bracesAndSemicolons <$> zipWithM showField (conFields c) ps ] showField x p = sep [ prettyTCM (A.qnameName x) <+> text "=" , nest 2 $ prettyTCM $ namedArg p ] showCon = parens $ prTy $ prettyTCM c <+> fsep (map (prettyTCM . namedArg) ps) prTy d = d -- caseMaybe (conPType i) d $ \ t -> d <+> text ":" <+> prettyTCM t prettyTCM (LitP l) = text (P.prettyShow l) prettyTCM (ProjP _ q) = text ("." ++ P.prettyShow q) -- | Proper pretty printing of patterns: prettyTCMPatterns :: [NamedArg DeBruijnPattern] -> TCM [Doc] prettyTCMPatterns = mapM prettyA <=< reifyPatterns prettyTCMPatternList :: [NamedArg DeBruijnPattern] -> TCM Doc prettyTCMPatternList = prettyList . map prettyA <=< reifyPatterns instance PrettyTCM (Elim' DisplayTerm) where prettyTCM (Apply v) = text "$" <+> prettyTCM (unArg v) prettyTCM (Proj _ f)= text "." <> prettyTCM f instance PrettyTCM NLPat where prettyTCM (PVar x bvs) = prettyTCM (Var x (map (Apply . fmap var) bvs)) prettyTCM (PWild) = text $ "_" prettyTCM (PDef f es) = parens $ prettyTCM f <+> fsep (map prettyTCM es) prettyTCM (PLam i u) = parens $ text ("λ " ++ absName u ++ " →") <+> (addContext (absName u) $ prettyTCM $ absBody u) prettyTCM (PPi a b) = parens $ text ("(" ++ absName b ++ " :") <+> prettyTCM (unDom a) <> text ") →" <+> (addContext (absName b) $ prettyTCM $ unAbs b) prettyTCM (PBoundVar i []) = prettyTCM (var i) prettyTCM (PBoundVar i es) = parens $ prettyTCM (var i) <+> fsep (map prettyTCM es) prettyTCM (PTerm t) = text "." <> parens (prettyTCM t) instance PrettyTCM NLPType where prettyTCM (NLPType PWild a) = prettyTCM a prettyTCM (NLPType l a) = text "{" <> prettyTCM l <> text "}" <> prettyTCM a instance PrettyTCM (Elim' NLPat) where prettyTCM (Apply v) = prettyTCM (unArg v) prettyTCM (Proj _ f)= text "." <> prettyTCM f instance PrettyTCM (Type' NLPat) where prettyTCM = prettyTCM . unEl instance PrettyTCM RewriteRule where prettyTCM (RewriteRule q gamma f ps rhs b) = fsep [ prettyTCM q , prettyTCM gamma <+> text " |- " , addContext gamma $ sep [ prettyTCM (PDef f ps) , text " --> " , prettyTCM rhs , text " : " , prettyTCM b ] ] instance PrettyTCM Occurrence where prettyTCM occ = text $ "-[" ++ prettyShow occ ++ "]->" -- | Pairing something with a node (for printing only). data WithNode n a = WithNode n a instance PrettyTCM n => PrettyTCM (WithNode n Occurrence) where prettyTCM (WithNode n o) = prettyTCM o <+> prettyTCM n instance (PrettyTCM n, PrettyTCM (WithNode n e)) => PrettyTCM (Graph n n e) where prettyTCM g = vcat $ map pr $ Map.assocs $ Graph.graph g where pr (n, es) = sep [ prettyTCM n , nest 2 $ vcat $ map (prettyTCM . uncurry WithNode) $ Map.assocs es ] Agda-2.5.3/src/full/Agda/TypeChecking/Empty.hs0000644000000000000000000000567313154613124017152 0ustar0000000000000000 module Agda.TypeChecking.Empty (isEmptyType) where import Control.Applicative import Control.Monad import Control.Monad.Except import Data.Semigroup import Data.Monoid import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.Syntax.Position import Agda.TypeChecking.Monad import Agda.TypeChecking.Coverage import Agda.TypeChecking.Constraints import Agda.TypeChecking.Records import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.Utils.Either import Agda.Utils.Monad data ErrorNonEmpty = Fail -- ^ Generic failure | FailBecause TCErr -- ^ Failure with informative error | DontKnow -- ^ Emptyness check blocked instance Semigroup ErrorNonEmpty where DontKnow <> _ = DontKnow _ <> DontKnow = DontKnow FailBecause err <> _ = FailBecause err Fail <> err = err instance Monoid ErrorNonEmpty where mempty = Fail mappend = (Data.Semigroup.<>) -- | Check whether a type is empty. -- This check may be postponed as emptiness constraint. isEmptyType :: Range -> Type -> TCM () isEmptyType r t = caseEitherM (loop t) failure return where failure DontKnow = addConstraint $ IsEmpty r t failure (FailBecause err) = throwError err failure Fail = typeError $ ShouldBeEmpty t [] -- Either the type is possibly non-empty (Left err) or it is really empty -- (Right ()). loop :: Type -> TCM (Either ErrorNonEmpty ()) loop t = do mr <- tryRecordType t case mr of -- If t is blocked or a meta, we cannot decide emptiness now. Postpone. Left Nothing -> return $ Left DontKnow -- If t is not a record type, try to split Left (Just t) -> do -- from the current context xs:ts, create a pattern list -- xs _ : ts t and try to split on _ (the last variable) tel0 <- getContextTelescope let gamma = telToList tel0 ++ [domFromArg $ defaultArg (underscore, t)] tel = telFromList gamma ps = teleNamedArgs tel dontAssignMetas $ do r <- splitLast Inductive tel ps case r of Left UnificationStuck{} -> return $ Left DontKnow Left _ -> return $ Left Fail Right cov -> do let ps = map (namedArg . last . scPats) $ splitClauses cov if (null ps) then return (Right ()) else Left . FailBecause <$> do typeError_ $ ShouldBeEmpty t ps -- If t is a record type, see if any of the field types is empty Right (r, pars, def) -> do if not $ recEtaEquality def then return $ Left Fail else do checkTel $ recTel def `apply` pars checkTel :: Telescope -> TCM (Either ErrorNonEmpty ()) checkTel EmptyTel = return $ Left Fail checkTel (ExtendTel dom tel) = orEitherM [ loop (unDom dom) , underAbstraction dom tel checkTel ] Agda-2.5.3/src/full/Agda/TypeChecking/Constraints.hs-boot0000644000000000000000000000073713154613124021320 0ustar0000000000000000module Agda.TypeChecking.Constraints where import Agda.TypeChecking.Monad.Base addConstraint :: Constraint -> TCM () catchConstraint :: Constraint -> TCM () -> TCM () solveConstraint :: Constraint -> TCM () solveAwakeConstraints' :: Bool -> TCM () noConstraints :: TCM a -> TCM a ifNoConstraints_ :: TCM () -> TCM a -> (ProblemId -> TCM a) -> TCM a guardConstraint :: Constraint -> TCM () -> TCM () debugConstraints :: TCM () Agda-2.5.3/src/full/Agda/TypeChecking/Records.hs0000644000000000000000000007363013154613124017453 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.Records where -- import Control.Applicative import Control.Monad import Data.Function import qualified Data.List as List import Data.Maybe import qualified Data.Set as Set import Data.Traversable (traverse) import Agda.Syntax.Common import qualified Agda.Syntax.Concrete.Name as C import Agda.Syntax.Concrete (FieldAssignment'(..), nameFieldA) import Agda.Syntax.Abstract.Name import Agda.Syntax.Internal as I import Agda.Syntax.Position import Agda.TypeChecking.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce import Agda.TypeChecking.Reduce.Monad () import Agda.TypeChecking.Telescope import {-# SOURCE #-} Agda.TypeChecking.ProjectionLike (eligibleForProjectionLike) import Agda.Utils.Either import Agda.Utils.Functor (for, ($>)) import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import qualified Agda.Utils.HashMap as HMap import Agda.Utils.Pretty (prettyShow) import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | Order the fields of a record construction. -- Use the second argument for missing fields. orderFields :: QName -> a -> [C.Name] -> [(C.Name, a)] -> TCM [a] orderFields r def xs fs = do unlessNull (ys List.\\ List.nub ys) $ typeError . DuplicateFields . List.nub unlessNull (ys List.\\ xs) $ typeError . TooManyFields r -- shouldBeNull (xs List.\\ ys) $ TooFewFields r return $ order xs fs where ys = map fst fs -- invariant: the first list contains at least the fields of the second list order [] [] = [] order [] _ = __IMPOSSIBLE__ order (x : xs) ys = case lookup x (assocHoles ys) of Just (e, ys') -> e : order xs ys' Nothing -> def : order xs ys assocHoles xs = [ (x, (v, xs')) | ((x, v), xs') <- holes xs ] -- | A record field assignment @record{xs = es}@ might not mention all -- visible fields. @insertMissingFields@ inserts placeholders for -- the missing visible fields and returns the values in order -- of the fields in the record declaration. insertMissingFields :: QName -- ^ Name of record type (for error reporting). -> (C.Name -> a) -- ^ Function to generate a placeholder for missing visible field. -> [FieldAssignment' a] -- ^ Given fields. -> [Arg C.Name] -- ^ All record field names with 'ArgInfo'. -> TCM [NamedArg a] -- ^ Given fields enriched by placeholders for missing explicit fields. insertMissingFields r placeholder fs axs = do -- Compute the list of given fields, decorated with the ArgInfo from the record def. let arg x e = case [ a | a <- axs, unArg a == x ] of [a] -> nameIfHidden a e <$ a _ -> defaultNamedArg e -- we only end up here if the field names are bad givenFields = [ (x, Just $ arg x e) | FieldAssignment x e <- fs ] -- Compute a list of p[aceholders for the missing visible fields. let missingExplicits = [ (x, Just $ setOrigin Inserted $ nameIfHidden a . placeholder <$> a) | a <- filter visible axs , let x = unArg a , x `notElem` map (view nameFieldA) fs ] -- In es omitted explicit fields are replaced by placeholders -- (from missingExplicits). Omitted implicit or instance fields -- are still left out and inserted later by checkArguments_. catMaybes <$> do -- Default value @Nothing@ will only be used for missing hidden fields. -- These can be ignored as they will be inserted by @checkArguments_@. orderFields r Nothing (map unArg axs) $ givenFields ++ missingExplicits where -- Andreas, 2017-04-13, issue #2494 -- We need to put the field names as argument names for hidden arguments. -- Otherwise, insertImplicit does not do the right thing. nameIfHidden :: Arg C.Name -> c -> Named_ c nameIfHidden ax | visible ax = unnamed | otherwise = named (Ranged (getRange ax) $ prettyShow $ unArg ax) -- | The name of the module corresponding to a record. recordModule :: QName -> ModuleName recordModule = mnameFromList . qnameToList -- | Get the definition for a record. Throws an exception if the name -- does not refer to a record or the record is abstract. getRecordDef :: QName -> TCM Defn getRecordDef r = maybe err return =<< isRecord r where err = typeError $ ShouldBeRecordType (El Prop $ Def r []) -- | Get the record name belonging to a field name. getRecordOfField :: QName -> TCM (Maybe QName) getRecordOfField d = caseMaybeM (isProjection d) (return Nothing) $ \ Projection{ projProper = proper, projFromType = r} -> return $ unArg r <$ proper -- if proper then Just (unArg r) else Nothing -- | Get the field names of a record. getRecordFieldNames :: QName -> TCM [Arg C.Name] getRecordFieldNames r = recordFieldNames <$> getRecordDef r recordFieldNames :: Defn -> [Arg C.Name] recordFieldNames = map (fmap (nameConcrete . qnameName)) . recFields -- | Find all records with at least the given fields. findPossibleRecords :: [C.Name] -> TCM [QName] findPossibleRecords fields = do defs <- HMap.elems <$> use (stSignature . sigDefinitions) idefs <- HMap.elems <$> use (stImports . sigDefinitions) return $ cands defs ++ cands idefs where cands defs = [ defName d | d <- defs, possible d ] possible def = -- Check whether the given fields are contained -- in the fields of record @def@ (if it is a record). case theDef def of Record{ recFields = fs } -> Set.isSubsetOf given $ Set.fromList $ map (nameConcrete . qnameName . unArg) fs _ -> False given = Set.fromList fields -- | Get the field types of a record. getRecordFieldTypes :: QName -> TCM Telescope getRecordFieldTypes r = recTel <$> getRecordDef r -- | Get the field names belonging to a record type. getRecordTypeFields :: Type -> TCM [Arg QName] getRecordTypeFields t = case ignoreSharing $ unEl t of Def r _ -> do rDef <- theDef <$> getConstInfo r case rDef of Record { recFields = fields } -> return fields _ -> __IMPOSSIBLE__ _ -> __IMPOSSIBLE__ -- | Returns the given record type's constructor name (with an empty -- range). getRecordConstructor :: QName -> TCM ConHead getRecordConstructor r = killRange <$> recConHead <$> getRecordDef r -- | Check if a name refers to a record. -- If yes, return record definition. {-# SPECIALIZE isRecord :: QName -> TCM (Maybe Defn) #-} {-# SPECIALIZE isRecord :: QName -> ReduceM (Maybe Defn) #-} isRecord :: HasConstInfo m => QName -> m (Maybe Defn) isRecord r = do def <- theDef <$> getConstInfo r return $ case def of Record{} -> Just def _ -> Nothing -- | Reduce a type and check whether it is a record type. -- Succeeds only if type is not blocked by a meta var. -- If yes, return its name, parameters, and definition. isRecordType :: Type -> TCM (Maybe (QName, Args, Defn)) isRecordType t = either (const Nothing) Just <$> tryRecordType t -- | Reduce a type and check whether it is a record type. -- Succeeds only if type is not blocked by a meta var. -- If yes, return its name, parameters, and definition. -- If no, return the reduced type (unless it is blocked). tryRecordType :: Type -> TCM (Either (Maybe Type) (QName, Args, Defn)) tryRecordType t = ifBlockedType t (\ _ _ -> return $ Left Nothing) $ \ t -> do let no = return $ Left $ Just t case ignoreSharing $ unEl t of Def r es -> do let vs = fromMaybe __IMPOSSIBLE__ $ allApplyElims es caseMaybeM (isRecord r) no $ \ def -> return $ Right (r,vs,def) _ -> no -- | Get the original projection info for name. {-# SPECIALIZE origProjection :: QName -> TCM (QName, Definition, Maybe Projection) #-} origProjection :: HasConstInfo m => QName -> m (QName, Definition, Maybe Projection) origProjection f = do def <- getConstInfo f let proj = isProjection_ $ theDef def fallback = return (f, def, proj) caseMaybe proj fallback $ \ p@Projection{ projProper = proper, projOrig = f' } -> if isNothing proper || f == f' then fallback else do def <- getConstInfo f' return (f', def, isProjection_ $ theDef def) -- | @getDefType f t@ computes the type of (possibly projection-(like)) -- function @f@ whose first argument has type @t@. -- The `parameters' for @f@ are extracted from @t@. -- @Nothing@ if @f@ is projection(like) but -- @t@ is not a data/record/axiom type. -- -- Precondition: @t@ is reduced. -- -- See also: 'Agda.TypeChecking.Datatypes.getConType' getDefType :: QName -> Type -> TCM (Maybe Type) getDefType f t = do -- Andreas, Issue #1973: we need to take the original projection -- since the parameters from the reduced type t are correct for -- the original projection only. -- Due to module application, the given (non-original) projection f -- may expect less parameters, those corresponding to a unreduced -- version of t (which we cannot obtain here). (f, def, mp) <- origProjection f let a = defType def -- if @f@ is not a projection (like) function, @a@ is the correct type fallback = return $ Just a reportSDoc "tc.deftype" 20 $ vcat [ text "definition f = " <> prettyTCM f <+> text (" -- raw: " ++ prettyShow f) , text "has type a = " <> prettyTCM a , text "principal t = " <> prettyTCM t ] caseMaybe mp fallback $ \ (Projection{ projIndex = n }) -> if n <= 0 then fallback else do -- otherwise, we have to instantiate @a@ to the "parameters" of @f@ let npars | n == 0 = __IMPOSSIBLE__ | otherwise = n - 1 reportSLn "tc.deftype" 20 $ "projIndex = " ++ show n -- we get the parameters from type @t@ case ignoreSharing $ unEl t of Def d es -> do -- Andreas, 2013-10-22 -- we need to check this @Def@ is fully reduced. -- If it is stuck due to disabled reductions -- (because of failed termination check), -- we will produce garbage parameters. ifNotM (eligibleForProjectionLike d) failNotElig $ {- else -} do -- now we know it is reduced, we can safely take the parameters let pars = fromMaybe __IMPOSSIBLE__ $ allApplyElims $ take npars es reportSDoc "tc.deftype" 20 $ vcat [ text $ "head d = " ++ prettyShow d , text "parameters =" <+> sep (map prettyTCM pars) ] reportSLn "tc.deftype" 60 $ "parameters = " ++ show pars if length pars < npars then failure "does not supply enough parameters" else Just <$> a `piApplyM` pars _ -> failNotDef where failNotElig = failure "is not eligible for projection-likeness" failNotDef = failure "is not a Def." failure reason = do reportSDoc "tc.deftype" 25 $ sep [ text "Def. " <+> prettyTCM f <+> text " is projection(like)" , text "but the type " , prettyTCM t , text $ "of its argument " ++ reason ] return Nothing -- | The analogue of 'piApply'. If @v@ is a value of record type @t@ -- with field @f@, then @projectTyped v t f@ returns the type of @f v@. -- And also the record type (as first result). -- -- Works also for projection-like definitions @f@. -- In this case, the first result is not a record type. -- -- Precondition: @t@ is reduced. -- projectTyped :: Term -- ^ Head (record value). -> Type -- ^ Its type. -> ProjOrigin -> QName -- ^ Projection. -> TCM (Maybe (Dom Type, Term, Type)) projectTyped v t o f = caseMaybeM (getDefType f t) (return Nothing) $ \ tf -> do ifNotPiType tf (const $ return Nothing) {- else -} $ \ dom b -> do u <- applyDef o f (argFromDom dom $> v) return $ Just (dom, u, b `absApp` v) -- | Typing of an elimination. data ElimType = ArgT (Dom Type) -- ^ Type of the argument. | ProjT { projTRec :: Dom Type -- ^ The type of the record which is eliminated. , projTField :: Type -- ^ The type of the field. } instance PrettyTCM ElimType where prettyTCM (ArgT a) = prettyTCM a prettyTCM (ProjT a b) = text "." <> parens (prettyTCM a <+> text "->" <+> prettyTCM b) -- | Given a head and its type, compute the types of the eliminations. typeElims :: Type -> Term -> Elims -> TCM [ElimType] typeElims a _ [] = return [] typeElims a self (e : es) = do case e of Apply v -> ifNotPiType a __IMPOSSIBLE__ {- else -} $ \ a b -> do (ArgT a :) <$> typeElims (absApp b $ unArg v) (self `applyE` [e]) es Proj o f -> do a <- reduce a (dom, self, a) <- fromMaybe __IMPOSSIBLE__ <$> projectTyped self a o f (ProjT dom a :) <$> typeElims a self es -- | Check if a name refers to an eta expandable record. {-# SPECIALIZE isEtaRecord :: QName -> TCM Bool #-} {-# SPECIALIZE isEtaRecord :: QName -> ReduceM Bool #-} isEtaRecord :: HasConstInfo m => QName -> m Bool isEtaRecord r = maybe False recEtaEquality <$> isRecord r isEtaCon :: HasConstInfo m => QName -> m Bool isEtaCon c = getConstInfo' c >>= \case Left (SigUnknown err) -> __IMPOSSIBLE__ Left SigAbstract -> return False Right def -> case theDef def of Constructor {conData = r} -> isEtaRecord r _ -> return False -- | Check if a name refers to a record which is not coinductive. (Projections are then size-preserving) isInductiveRecord :: QName -> TCM Bool isInductiveRecord r = maybe False (\ d -> recInduction d /= Just CoInductive || not (recRecursive d)) <$> isRecord r -- | Check if a type is an eta expandable record and return the record identifier and the parameters. isEtaRecordType :: Type -> TCM (Maybe (QName, Args)) isEtaRecordType a = case ignoreSharing $ unEl a of Def d es -> do let vs = fromMaybe __IMPOSSIBLE__ $ allApplyElims es ifM (isEtaRecord d) (return $ Just (d, vs)) (return Nothing) _ -> return Nothing -- | Check if a name refers to a record constructor. -- If yes, return record definition. isRecordConstructor :: HasConstInfo m => QName -> m (Maybe (QName, Defn)) isRecordConstructor c = getConstInfo' c >>= \case Left (SigUnknown err) -> __IMPOSSIBLE__ Left SigAbstract -> return Nothing Right def -> case theDef $ def of Constructor{ conData = r } -> fmap (r,) <$> isRecord r _ -> return Nothing -- | Check if a constructor name is the internally generated record constructor. -- -- Works also for abstract constructors. isGeneratedRecordConstructor :: QName -> TCM Bool isGeneratedRecordConstructor c = ignoreAbstractMode $ do caseMaybeM (isRecordConstructor c) (return False) $ \ (_, def) -> case def of Record{ recNamedCon = False } -> return True _ -> return False -- | Turn off eta for unguarded recursive records. -- Projections do not preserve guardedness. unguardedRecord :: QName -> TCM () unguardedRecord q = modifySignature $ updateDefinition q $ updateTheDef $ \case r@Record{} -> r { recEtaEquality' = setEtaEquality (recEtaEquality' r) False } _ -> __IMPOSSIBLE__ -- | Turn on eta for inductive guarded recursive records. -- Projections do not preserve guardedness. recursiveRecord :: QName -> TCM () recursiveRecord q = do ok <- etaEnabled modifySignature $ updateDefinition q $ updateTheDef $ \case r@Record{ recInduction = ind, recEtaEquality' = eta } -> r { recEtaEquality' = eta' } where eta' | ok, eta == Inferred False, ind /= Just CoInductive = Inferred True | otherwise = eta _ -> __IMPOSSIBLE__ -- | Turn on eta for non-recursive record, unless user declared otherwise. nonRecursiveRecord :: QName -> TCM () nonRecursiveRecord q = whenM etaEnabled $ do -- Do nothing if eta is disabled by option. modifySignature $ updateDefinition q $ updateTheDef $ \case r@Record{ recInduction = ind, recEtaEquality' = Inferred False } | ind /= Just CoInductive -> r { recEtaEquality' = Inferred True } r@Record{} -> r _ -> __IMPOSSIBLE__ -- | Check whether record type is marked as recursive. -- -- Precondition: record type identifier exists in signature. isRecursiveRecord :: QName -> TCM Bool isRecursiveRecord q = recRecursive . theDef . fromMaybe __IMPOSSIBLE__ . lookupDefinition q <$> getSignature {- | @etaExpandBoundVar i = (Δ, σ, τ)@ Precondition: The current context is @Γ = Γ₁, x:R pars, Γ₂@ where @|Γ₂| = i@ and @R@ is a eta-expandable record type with constructor @c@ and fields @Γ'@. Postcondition: @Δ = Γ₁, Γ', Γ₂[c Γ']@ and @Γ ⊢ σ : Δ@ and @Δ ⊢ τ : Γ@. -} etaExpandBoundVar :: Int -> TCM (Maybe (Telescope, Substitution, Substitution)) etaExpandBoundVar i = fmap (\ (delta, sigma, tau, _) -> (delta, sigma, tau)) <$> do expandRecordVar i =<< getContextTelescope -- | @expandRecordVar i Γ = (Δ, σ, τ, Γ')@ -- -- Precondition: @Γ = Γ₁, x:R pars, Γ₂@ where -- @|Γ₂| = i@ and @R@ is a eta-expandable record type -- with constructor @c@ and fields @Γ'@. -- -- Postcondition: @Δ = Γ₁, Γ', Γ₂[c Γ']@ and @Γ ⊢ σ : Δ@ and @Δ ⊢ τ : Γ@. expandRecordVar :: Int -> Telescope -> TCM (Maybe (Telescope, Substitution, Substitution, Telescope)) expandRecordVar i gamma0 = do -- Get the context with last variable added last in list. let gamma = telToList gamma0 -- Convert the de Bruijn index i to a de Bruijn level l = size gamma - 1 - i -- Extract type of @i@th de Bruijn index. -- Γ = Γ₁, x:a, Γ₂ let (gamma1, dom@(Dom ai (x, a)) : gamma2) = splitAt l gamma -- This must be a eta-expandable record type. let failure = do reportSDoc "tc.meta.assign.proj" 25 $ text "failed to eta-expand variable " <+> pretty x <+> text " since its type " <+> prettyTCM a <+> text " is not a record type" return Nothing caseMaybeM (isRecordType a) failure $ \ (r, pars, def) -> do if not (recEtaEquality def) then return Nothing else Just <$> do -- Get the record fields @Γ₁ ⊢ tel@ (@tel = Γ'@). -- TODO: compose argInfo ai with tel. let tel = recTel def `apply` pars m = size tel fs = recFields def -- Construct the record pattern @Γ₁, Γ' ⊢ u := c ys@. ys = zipWith (\ f i -> f $> var i) fs $ downFrom m u = Con (recConHead def) ConOSystem ys -- @Γ₁, Γ' ⊢ τ₀ : Γ₁, x:_@ tau0 = consS u $ raiseS m -- @Γ₁, Γ', Γ₂ ⊢ τ₀ : Γ₁, x:_, Γ₂@ tau = liftS (size gamma2) tau0 -- Fields are in order first-first. zs = for fs $ fmap $ \ f -> Var 0 [Proj ProjSystem f] -- We need to reverse the field sequence to build the substitution. -- @Γ₁, x:_ ⊢ σ₀ : Γ₁, Γ'@ sigma0 = reverse (map unArg zs) ++# raiseS 1 -- @Γ₁, x:_, Γ₂ ⊢ σ₀ : Γ₁, Γ', Γ₂@ sigma = liftS (size gamma2) sigma0 -- Construct @Δ@ as telescope. -- Note @Γ₁, x:_ ⊢ Γ₂@, thus, @Γ₁, Γ' ⊢ [τ₀]Γ₂@ -- Use "f(x)" as variable name for the projection f(x). s = prettyShow x tel' = mapAbsNames (\ f -> stringToArgName $ argNameToString f ++ "(" ++ s ++ ")") tel delta = telFromList $ gamma1 ++ telToList tel' ++ telToList (applySubst tau0 $ telFromList gamma2) -- Andreas, 2017-07-29, issue #2644 -- We cannot substitute directly into a ListTel like gamma2, -- we have to convert it to a telescope first, otherwise we get garbage. return (delta, sigma, tau, tel) -- | Precondition: variable list is ordered descendingly. Can be empty. expandRecordVarsRecursively :: [Int] -> Telescope -> TCM (Telescope, Substitution, Substitution) expandRecordVarsRecursively [] gamma = return (gamma, idS, idS) expandRecordVarsRecursively (i : is) gamma = do caseMaybeM (expandRecordVar i gamma) (expandRecordVarsRecursively is gamma) $ \ (gamma1, sigma1, tau1, tel) -> do -- Γ ⊢ σ₁ : Γ₁ and Γ₁ ⊢ τ₁ : Γ let n = size tel newis = take n $ downFrom $ i + n (gamma2, sigma2, tau2) <- expandRecordVarsRecursively (newis ++ is) gamma1 -- Γ₁ ⊢ σ₂ : Γ₂ and Γ₂ ⊢ τ₂ : Γ₁ return (gamma2, applySubst sigma1 sigma2, applySubst tau2 tau1) -- | @curryAt v (Γ (y : R pars) -> B) n = -- ( \ v -> λ Γ ys → v Γ (c ys) {- curry -} -- , \ v -> λ Γ y → v Γ (p1 y) ... (pm y) {- uncurry -} -- , Γ (ys : As) → B[c ys / y] -- )@ -- -- where @n = size Γ@. curryAt :: Type -> Int -> TCM (Term -> Term, Term -> Term, Type) curryAt t n = do -- first, strip the leading n domains (which remain unchanged) TelV gamma core <- telViewUpTo n t case ignoreSharing $ unEl core of -- There should be at least one domain left Pi (Dom ai a) b -> do -- Eta-expand @dom@ along @qs@ into a telescope @tel@, computing a substitution. -- For now, we only eta-expand once. -- This might trigger another call to @etaExpandProjectedVar@ later. -- A more efficient version does all the eta-expansions at once here. (r, pars, def) <- fromMaybe __IMPOSSIBLE__ <$> isRecordType a unless (recEtaEquality def) __IMPOSSIBLE__ -- TODO: compose argInfo ai with tel. let tel = recTel def `apply` pars m = size tel fs = recFields def ys = zipWith (\ f i -> f $> var i) fs $ downFrom m u = Con (recConHead def) ConOSystem ys b' = raise m b `absApp` u t' = gamma `telePi` (tel `telePi` b') gammai = map domInfo $ telToList gamma xs = reverse $ zipWith (\ ai i -> Arg ai $ var i) gammai [m..] curry v = teleLam gamma $ teleLam tel $ raise (n+m) v `apply` (xs ++ [Arg ai u]) zs = for fs $ fmap $ \ f -> Var 0 [Proj ProjSystem f] atel = sgTel $ Dom ai (absName b, a) uncurry v = teleLam gamma $ teleLam atel $ raise (n + 1) v `apply` (xs ++ zs) return (curry, uncurry, t') _ -> __IMPOSSIBLE__ {-| @etaExpand r pars u@ computes the eta expansion of record value @u@ at record type @r pars@. The first argument @r@ should be the name of an eta-expandable record type. Given @record R : Set where field x : A; y : B; .z : C@ and @r : R@, @etaExpand R [] r = (tel, [R.x r, R.y r, R.z r])@ where @tel@ is the record telescope instantiated at the parameters @pars@. -} etaExpandRecord :: QName -> Args -> Term -> TCM (Telescope, Args) etaExpandRecord = etaExpandRecord' False -- | Eta expand a record regardless of whether it's an eta-record or not. forceEtaExpandRecord :: QName -> Args -> Term -> TCM (Telescope, Args) forceEtaExpandRecord = etaExpandRecord' True etaExpandRecord' :: Bool -> QName -> Args -> Term -> TCM (Telescope, Args) etaExpandRecord' forceEta r pars u = do def <- getRecordDef r (tel, _, _, args) <- etaExpandRecord'_ forceEta r pars def u return (tel, args) etaExpandRecord_ :: QName -> Args -> Defn -> Term -> TCM (Telescope, ConHead, ConInfo, Args) etaExpandRecord_ = etaExpandRecord'_ False etaExpandRecord'_ :: Bool -> QName -> Args -> Defn -> Term -> TCM (Telescope, ConHead, ConInfo, Args) etaExpandRecord'_ forceEta r pars def u = do let Record{ recConHead = con , recFields = xs , recTel = tel } = def eta = recEtaEquality def tel' = apply tel pars unless (eta || forceEta) __IMPOSSIBLE__ -- make sure we do not expand non-eta records (unless forced to) case ignoreSharing u of -- Already expanded. Con con_ ci args -> do when (con /= con_) $ do reportSDoc "impossible" 10 $ vcat [ text "etaExpandRecord_: the following two constructors should be identical" , nest 2 $ text $ "con = " ++ prettyShow con , nest 2 $ text $ "con_ = " ++ prettyShow con_ ] __IMPOSSIBLE__ return (tel', con, ci, args) -- Not yet expanded. _ -> do -- Andreas, < 2016-01-18: Note: recFields are always the original projections, -- thus, we can use them in Proj directly. let xs' = for xs $ fmap $ \ x -> u `applyE` [Proj ProjSystem x] reportSDoc "tc.record.eta" 20 $ vcat [ text "eta expanding" <+> prettyTCM u <+> text ":" <+> prettyTCM r , nest 2 $ vcat [ text "tel' =" <+> prettyTCM tel' , text "args =" <+> prettyTCM xs' ] ] return (tel', con, ConOSystem, xs') etaExpandAtRecordType :: Type -> Term -> TCM (Telescope, Term) etaExpandAtRecordType t u = do (r, pars, def) <- fromMaybe __IMPOSSIBLE__ <$> isRecordType t (tel, con, ci, args) <- etaExpandRecord_ r pars def u return (tel, Con con ci args) -- | The fields should be eta contracted already. -- -- We can eta contract if all fields @f = ...@ are irrelevant -- or all fields @f@ are the projection @f v@ of the same value @v@, -- but we need at least one relevant field to find the value @v@. -- -- TODO: this can be moved out of TCM (but only if ConHead -- stores also the Arg-decoration of the record fields. {-# SPECIALIZE etaContractRecord :: QName -> ConHead -> ConInfo -> Args -> TCM Term #-} {-# SPECIALIZE etaContractRecord :: QName -> ConHead -> ConInfo -> Args -> ReduceM Term #-} etaContractRecord :: HasConstInfo m => QName -> ConHead -> ConInfo -> Args -> m Term etaContractRecord r c ci args = do Just Record{ recFields = xs } <- isRecord r let check :: Arg Term -> Arg QName -> Maybe (Maybe Term) check a ax = do -- @a@ is the constructor argument, @ax@ the corr. record field name -- skip irrelevant record fields by returning DontCare case (getRelevance a, hasElims $ unArg a) of (Irrelevant, _) -> Just Nothing -- if @a@ is the record field name applied to a single argument -- then it passes the check (_, Just (_, [])) -> Nothing -- not a projection (_, Just (h, es)) | Proj _o f <- last es, unArg ax == f -> Just $ Just $ h $ init es _ -> Nothing fallBack = return (Con c ci args) case compare (length args) (length xs) of LT -> fallBack -- Not fully applied GT -> __IMPOSSIBLE__ -- Too many arguments. Impossible. EQ -> do case zipWithM check args xs of Just as -> case [ a | Just a <- as ] of (a:as) -> if all (a ==) as then return a else fallBack _ -> fallBack -- just irrelevant terms _ -> fallBack -- a Nothing -- | Is the type a hereditarily singleton record type? May return a -- blocking metavariable. -- -- Precondition: The name should refer to a record type, and the -- arguments should be the parameters to the type. isSingletonRecord :: QName -> Args -> TCM (Either MetaId Bool) isSingletonRecord r ps = mapRight isJust <$> isSingletonRecord' False r ps isSingletonRecordModuloRelevance :: QName -> Args -> TCM (Either MetaId Bool) isSingletonRecordModuloRelevance r ps = mapRight isJust <$> isSingletonRecord' True r ps -- | Return the unique (closed) inhabitant if exists. -- In case of counting irrelevance in, the returned inhabitant -- contains garbage. isSingletonRecord' :: Bool -> QName -> Args -> TCM (Either MetaId (Maybe Term)) isSingletonRecord' regardIrrelevance r ps = do reportSLn "tc.meta.eta" 30 $ "Is " ++ prettyShow r ++ " a singleton record type?" def <- getRecordDef r emap (Con (recConHead def) ConOSystem) <$> check (recTel def `apply` ps) where check :: Telescope -> TCM (Either MetaId (Maybe [Arg Term])) check tel = do reportSDoc "tc.meta.eta" 30 $ text "isSingletonRecord' checking telescope " <+> prettyTCM tel case tel of EmptyTel -> return $ Right $ Just [] ExtendTel dom tel | isIrrelevant dom && regardIrrelevance -> do underAbstraction dom tel $ \ tel -> emap (Arg (domInfo dom) garbage :) <$> check tel | otherwise -> do isSing <- isSingletonType' regardIrrelevance $ unDom dom case isSing of Left mid -> return $ Left mid Right Nothing -> return $ Right Nothing Right (Just v) -> underAbstraction dom tel $ \ tel -> emap (Arg (domInfo dom) v :) <$> check tel garbage :: Term garbage = Sort Prop -- | Check whether a type has a unique inhabitant and return it. -- Can be blocked by a metavar. isSingletonType :: Type -> TCM (Either MetaId (Maybe Term)) isSingletonType = isSingletonType' False -- | Check whether a type has a unique inhabitant (irrelevant parts ignored). -- Can be blocked by a metavar. isSingletonTypeModuloRelevance :: (MonadTCM tcm) => Type -> tcm (Either MetaId Bool) isSingletonTypeModuloRelevance t = liftTCM $ do mapRight isJust <$> isSingletonType' True t isSingletonType' :: Bool -> Type -> TCM (Either MetaId (Maybe Term)) isSingletonType' regardIrrelevance t = do TelV tel t <- telView t ifBlockedType t (\ m _ -> return $ Left m) $ \ t -> do res <- isRecordType t case res of Just (r, ps, def) | recEtaEquality def -> do emap (abstract tel) <$> isSingletonRecord' regardIrrelevance r ps _ -> return $ Right Nothing -- | Auxiliary function. emap :: (a -> b) -> Either c (Maybe a) -> Either c (Maybe b) emap = mapRight . fmap -- | Replace projection patterns by the original projections. -- class NormaliseProjP a where normaliseProjP :: HasConstInfo m => a -> m a instance NormaliseProjP Clause where normaliseProjP cl = do ps <- normaliseProjP $ namedClausePats cl return $ cl { namedClausePats = ps } instance NormaliseProjP a => NormaliseProjP [a] where normaliseProjP = traverse normaliseProjP instance NormaliseProjP a => NormaliseProjP (Arg a) where normaliseProjP = traverse normaliseProjP instance NormaliseProjP a => NormaliseProjP (Named_ a) where normaliseProjP = traverse normaliseProjP instance NormaliseProjP (Pattern' x) where normaliseProjP p@VarP{} = return p normaliseProjP p@DotP{} = return p normaliseProjP p@AbsurdP{} = return p normaliseProjP (ConP c cpi ps) = ConP c cpi <$> normaliseProjP ps normaliseProjP p@LitP{} = return p normaliseProjP (ProjP o d0) = ProjP o <$> getOriginalProjection d0 Agda-2.5.3/src/full/Agda/TypeChecking/Coverage.hs0000644000000000000000000011217213154613124017600 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} {-| Coverage checking, case splitting, and splitting for refine tactics. -} module Agda.TypeChecking.Coverage ( SplitClause(..), clauseToSplitClause, fixTarget , Covering(..), splitClauses , coverageCheck , isCovered , splitClauseWithAbsurd , splitLast , splitResult , normaliseProjP ) where import Prelude hiding (null) import Control.Monad import Control.Monad.Trans ( lift ) #if !MIN_VERSION_base(4,8,0) import Control.Applicative hiding (empty) #endif import Data.Either (lefts) import qualified Data.List as List import Data.Monoid (Any(..)) import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import qualified Data.Traversable as Trav import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Syntax.Literal import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.TypeChecking.Monad import Agda.TypeChecking.Rules.LHS.Problem (allFlexVars) import Agda.TypeChecking.Rules.LHS.Unify import Agda.TypeChecking.Coverage.Match import Agda.TypeChecking.Coverage.SplitTree import Agda.TypeChecking.Datatypes (getConForm) import Agda.TypeChecking.Pretty import Agda.TypeChecking.Substitute import Agda.TypeChecking.Reduce import Agda.TypeChecking.Records import Agda.TypeChecking.Telescope import Agda.TypeChecking.MetaVars import Agda.TypeChecking.Warnings import Agda.Interaction.Options import Agda.Utils.Either import Agda.Utils.Except ( ExceptT , MonadError(catchError, throwError) , runExceptT ) import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Pretty (prettyShow) import Agda.Utils.Size import Agda.Utils.Suffix (nameVariant) import Agda.Utils.Tuple import Agda.Utils.Lens #include "undefined.h" import Agda.Utils.Impossible data SplitClause = SClause { scTel :: Telescope -- ^ Type of variables in @scPats@. , scPats :: [NamedArg DeBruijnPattern] -- ^ The patterns leading to the currently considered branch of -- the split tree. , scSubst :: PatternSubstitution -- ^ Substitution from 'scTel' to old context. -- Only needed directly after split on variable: -- * To update 'scTarget' -- * To rename other split variables when splitting on -- multiple variables. -- @scSubst@ is not ``transitive'', i.e., does not record -- the substitution from the original context to 'scTel' -- over a series of splits. It is freshly computed -- after each split by 'computeNeighborhood'; also -- 'splitResult', which does not split on a variable, -- should reset it to the identity 'idS', lest it be -- applied to 'scTarget' again, leading to Issue 1294. , scModuleParameterSub :: ModuleParamDict -- ^ We need to keep track of the module parameter substitutions for the -- clause for the purpose of inferring missing instance clauses. , scTarget :: Maybe (Arg Type) -- ^ The type of the rhs, living in context 'scTel'. -- This invariant is broken before calls to 'fixTarget'; -- there, 'scTarget' lives in the old context. -- 'fixTarget' moves 'scTarget' to the new context by applying -- substitution 'scSubst'. } -- | A @Covering@ is the result of splitting a 'SplitClause'. data Covering = Covering { covSplitArg :: Arg Nat -- ^ De Bruijn level (counting dot patterns) of argument we split on. , covSplitClauses :: [(QName, SplitClause)] -- ^ Covering clauses, indexed by constructor these clauses share. } -- | Project the split clauses out of a covering. splitClauses :: Covering -> [SplitClause] splitClauses (Covering _ qcs) = map snd qcs -- | Create a split clause from a clause in internal syntax. Used by make-case. clauseToSplitClause :: Clause -> SplitClause clauseToSplitClause cl = SClause { scTel = clauseTel cl , scPats = namedClausePats cl , scSubst = idS -- Andreas, 2014-07-15 TODO: Is this ok? , scModuleParameterSub = Map.empty , scTarget = clauseType cl } type CoverM = ExceptT SplitError TCM -- | Top-level function for checking pattern coverage. -- -- Effects: -- -- - Marks unreachable clauses as such in the signature. -- -- - Adds missing instances clauses to the signature. -- coverageCheck :: QName -> Type -> [Clause] -> TCM SplitTree coverageCheck f t cs = do TelV gamma a <- telView t let -- n = arity -- xs = variable patterns fitting lgamma n = size gamma xs = map (setOrigin Inserted) $ teleNamedArgs gamma -- The initial module parameter substitutions need to be weakened by the -- number of arguments that aren't module parameters. fv <- getDefFreeVars f moduleParams <- raise (n - fv) <$> use stModuleParameters -- construct the initial split clause let sc = SClause gamma xs idS moduleParams $ Just $ defaultArg a reportSDoc "tc.cover.top" 10 $ do let prCl cl = addContext (clauseTel cl) $ prettyTCMPatternList $ namedClausePats cl vcat [ text $ "Coverage checking " ++ prettyShow f ++ " with patterns:" , nest 2 $ vcat $ map prCl cs ] -- used = actually used clauses for cover -- pss = uncovered cases CoverResult splitTree used pss noex <- cover f cs sc reportSDoc "tc.cover.top" 10 $ vcat [ text "cover computed!" , text $ "used clauses: " ++ show used , text $ "non-exact clauses: " ++ show noex ] reportSDoc "tc.cover.splittree" 10 $ vcat [ text "generated split tree for" <+> prettyTCM f , text $ prettyShow splitTree ] -- report a warning if there are uncovered cases, -- generate a catch-all clause with a metavariable as its body to avoid -- internal errors in the reduction machinery. unless (null pss) $ setCurrentRange cs $ warning $ CoverageIssue f pss -- Andreas, 2017-08-28, issue #2723: -- Mark clauses as reachable or unreachable in the signature. let (is0, cs1) = unzip $ for (zip [0..] cs) $ \ (i, cl) -> let unreachable = i `Set.notMember` used in (boolToMaybe unreachable i, cl { clauseUnreachable = Just unreachable }) -- is = indices of unreachable clauses let is = catMaybes is0 reportSDoc "tc.cover.top" 10 $ vcat [ text $ "unreachable clauses: " ++ if null is then "(none)" else show is ] -- Replace the first clauses by @cs1@. There might be more -- added by @inferMissingClause@. modifyFunClauses f $ \ cs0 -> cs1 ++ drop (length cs1) cs0 -- Warn if there are unreachable clauses and mark them as unreachable. unless (null is) $ do -- Warn about unreachable clauses. let unreached = filter ((Just True ==) . clauseUnreachable) cs1 setCurrentRange (map clauseFullRange unreached) $ warning $ UnreachableClauses f $ map namedClausePats unreached -- report a warning if there are clauses that are not preserved as -- definitional equalities and --exact-split is enabled unless (null noex) $ do let noexclauses = map (cs1 !!) $ Set.toList noex setCurrentRange (map clauseLHSRange noexclauses) $ warning $ CoverageNoExactSplit f $ noexclauses return splitTree -- | Top-level function for eliminating redundant clauses in the interactive -- case splitter isCovered :: QName -> [Clause] -> SplitClause -> TCM Bool isCovered f cs sc = do CoverResult { coverMissingClauses = missing } <- cover f cs sc return $ null missing data CoverResult = CoverResult { coverSplitTree :: SplitTree , coverUsedClauses :: Set Nat , coverMissingClauses :: [(Telescope, [NamedArg DeBruijnPattern])] , coverNoExactClauses :: Set Nat } -- | @cover f cs (SClause _ _ ps _) = return (splitTree, used, pss)@. -- checks that the list of clauses @cs@ covers the given split clause. -- Returns the @splitTree@, the @used@ clauses, and missing cases @pss@. -- -- Effect: adds missing instance clauses for @f@ to signature. -- cover :: QName -> [Clause] -> SplitClause -> TCM CoverResult cover f cs sc@(SClause tel ps _ _ target) = do reportSDoc "tc.cover.cover" 10 $ inTopContext $ vcat [ text "checking coverage of pattern:" , nest 2 $ text "tel =" <+> prettyTCM tel , nest 2 $ text "ps =" <+> do addContext tel $ prettyTCMPatternList ps ] cs' <- normaliseProjP cs case match cs' ps of Yes (i,(mps,ls0)) -> do exact <- allM mps isTrivialPattern let noExactClause = if exact || clauseCatchall (cs !! i) then Set.empty else Set.singleton i reportSLn "tc.cover.cover" 10 $ "pattern covered by clause " ++ show i -- Check if any earlier clauses could match with appropriate literals let lsis = mapMaybe (\(j,c) -> (,j) <$> matchLits c ps) $ zip [0..i-1] cs reportSLn "tc.cover.cover" 10 $ "literal matches: " ++ show lsis -- Andreas, 2016-10-08, issue #2243 (#708) -- If we have several literal matches with the same literals -- only take the first matching clause of these. let is = Map.elems $ Map.fromListWith min $ (ls0,i) : lsis return $ CoverResult (SplittingDone (size tel)) (Set.fromList is) [] noExactClause No -> do reportSLn "tc.cover" 20 $ "pattern is not covered" case fmap getHiding target of Just h | isInstance h -> do -- Ulf, 2016-10-31: For now we only infer instance clauses. It would -- make sense to do it also for hidden, but since the value of a -- hidden clause is expected to be forced by later clauses, it's too -- late to add it now. If it was inferrable we would have gotten a -- type error before getting to this point. inferMissingClause f sc return $ CoverResult (SplittingDone (size tel)) Set.empty [] Set.empty _ -> return $ CoverResult (SplittingDone (size tel)) Set.empty [(tel, ps)] Set.empty -- We need to split! -- If all clauses have an unsplit copattern, we try that first. Block res bs -> tryIf (getAny res) splitRes $ do let done = return $ CoverResult (SplittingDone (size tel)) Set.empty [(tel, ps)] Set.empty if null bs then done else do -- Otherwise, if there are variables to split, we try them -- in the order determined by a split strategy. reportSLn "tc.cover.strategy" 20 $ "blocking vars = " ++ prettyShow bs -- xs is a non-empty lists of blocking variables -- try splitting on one of them xs <- splitStrategy bs tel r <- altM1 (split Inductive sc) xs case r of Left err -> typeError $ SplitError err -- If we get the empty covering, we have reached an impossible case -- and are done. Right (Covering n []) -> return $ CoverResult (SplittingDone (size tel)) Set.empty [] Set.empty Right (Covering n scs) -> do results <- mapM (cover f cs) (map snd scs) let trees = map coverSplitTree results useds = map coverUsedClauses results psss = map coverMissingClauses results noex = map coverNoExactClauses results -- Jesper, 2016-03-10 We need to remember which variables were -- eta-expanded by the unifier in order to generate a correct split -- tree (see Issue 1872). reportSDoc "tc.cover.split.eta" 60 $ vcat [ text "etaRecordSplits" , nest 2 $ vcat [ text "n = " <+> text (show n) , text "scs = " <+> prettyTCM scs , text "ps = " <+> text (show ps) ] ] let trees' = zipWith (etaRecordSplits (unArg n) ps) scs trees tree = SplitAt n trees' return $ CoverResult tree (Set.unions useds) (concat psss) (Set.unions noex) where tryIf :: Monad m => Bool -> m (Maybe a) -> m a -> m a tryIf True me m = fromMaybeM m me tryIf False me m = m -- Try to split result splitRes :: TCM (Maybe CoverResult) splitRes = do reportSLn "tc.cover" 20 $ "blocked by projection pattern" -- forM is a monadic map over a Maybe here mcov <- splitResult f sc Trav.forM mcov $ \ (Covering n scs) -> do -- If result splitting was successful, continue coverage checking. (projs, results) <- unzip <$> do mapM (traverseF $ cover f cs <=< (snd <.> fixTarget)) scs -- OR: -- forM scs $ \ (proj, sc') -> (proj,) <$> do -- cover f cs =<< do -- snd <$> fixTarget sc' let trees = map coverSplitTree results useds = map coverUsedClauses results psss = map coverMissingClauses results noex = map coverNoExactClauses results tree = SplitAt n $ zip projs trees return $ CoverResult tree (Set.unions useds) (concat psss) (Set.unions noex) gatherEtaSplits :: Int -> SplitClause -> [NamedArg DeBruijnPattern] -> [NamedArg DeBruijnPattern] gatherEtaSplits n sc [] | n >= 0 = __IMPOSSIBLE__ -- we should have encountered the main -- split by now already | otherwise = [] gatherEtaSplits n sc (p:ps) = case namedArg p of VarP x | n == 0 -> case p' of -- this is the main split VarP _ -> __IMPOSSIBLE__ DotP _ -> __IMPOSSIBLE__ AbsurdP _ -> __IMPOSSIBLE__ ConP _ _ qs -> qs ++ gatherEtaSplits (-1) sc ps LitP _ -> __IMPOSSIBLE__ ProjP{} -> __IMPOSSIBLE__ | otherwise -> updateNamedArg (\ _ -> p') p : gatherEtaSplits (n-1) sc ps where p' = lookupS (scSubst sc) $ dbPatVarIndex x DotP _ -> p : gatherEtaSplits (n-1) sc ps -- count dot patterns AbsurdP _ -> p : gatherEtaSplits (n-1) sc ps ConP _ _ qs -> gatherEtaSplits n sc (qs ++ ps) LitP _ -> gatherEtaSplits n sc ps ProjP{} -> gatherEtaSplits n sc ps addEtaSplits :: Int -> [NamedArg DeBruijnPattern] -> SplitTree -> SplitTree addEtaSplits k [] t = t addEtaSplits k (p:ps) t = case namedArg p of VarP _ -> addEtaSplits (k+1) ps t DotP _ -> addEtaSplits (k+1) ps t AbsurdP _ -> addEtaSplits (k+1) ps t ConP c cpi qs -> SplitAt (p $> k) [(conName c , addEtaSplits k (qs ++ ps) t)] LitP _ -> __IMPOSSIBLE__ ProjP{} -> __IMPOSSIBLE__ etaRecordSplits :: Int -> [NamedArg DeBruijnPattern] -> (QName,SplitClause) -> SplitTree -> (QName,SplitTree) etaRecordSplits n ps (q , sc) t = (q , addEtaSplits 0 (gatherEtaSplits n sc ps) t) -- | Append a instance clause to the clauses of a function. inferMissingClause :: QName -- ^ Function name. -> SplitClause -- ^ Clause to add. Clause hiding (in 'clauseType') must be 'Instance'. -> TCM () inferMissingClause f (SClause tel ps _ mpsub (Just t)) = setCurrentRange f $ do reportSDoc "tc.cover.infer" 20 $ addContext tel $ text "Trying to infer right-hand side of type" <+> prettyTCM t cl <- addContext tel $ withModuleParameters mpsub $ do (_x, rhs) <- case getHiding t of Instance{} -> newIFSMeta "" (unArg t) Hidden -> __IMPOSSIBLE__ NotHidden -> __IMPOSSIBLE__ return $ Clause { clauseLHSRange = noRange , clauseFullRange = noRange , clauseTel = tel , namedClausePats = ps , clauseBody = Just rhs , clauseType = Just t , clauseCatchall = False , clauseUnreachable = Just False -- missing, thus, not unreachable } addClauses f [cl] -- Important: add at the end. inferMissingClause _ (SClause _ _ _ _ Nothing) = __IMPOSSIBLE__ splitStrategy :: BlockingVars -> Telescope -> TCM BlockingVars splitStrategy bs tel = return $ updateLast clearBlockingVarCons xs -- Make sure we do not insists on precomputed coverage when -- we make our last try to split. -- Otherwise, we will not get a nice error message. where xs = bs {- KEEP! -- Andreas, 2012-10-13 -- The following split strategy which prefers all-constructor columns -- fails on test/fail/CoverStrategy xs = ys ++ zs (ys, zs) = partition allConstructors bs allConstructors :: BlockingVar -> Bool allConstructors = isJust . snd -} -- | Check that a type is a non-irrelevant datatype or a record with -- named constructor. Unless the 'Induction' argument is 'CoInductive' -- the data type must be inductive. isDatatype :: (MonadTCM tcm, MonadError SplitError tcm) => Induction -> Dom Type -> tcm (QName, [Arg Term], [Arg Term], [QName]) isDatatype ind at = do let t = unDom at throw f = throwError . f =<< do liftTCM $ buildClosure t t' <- liftTCM $ reduce t case ignoreSharing $ unEl t' of Def d es -> do let ~(Just args) = allApplyElims es def <- liftTCM $ theDef <$> getConstInfo d splitOnIrrelevantDataAllowed <- liftTCM $ optExperimentalIrrelevance <$> pragmaOptions case def of Datatype{dataPars = np, dataCons = cs, dataInduction = i} | i == CoInductive && ind /= CoInductive -> throw CoinductiveDatatype -- Andreas, 2011-10-03 allow some splitting on irrelevant data (if only one constr. matches) | isIrrelevant at && not splitOnIrrelevantDataAllowed -> throw IrrelevantDatatype | otherwise -> do let (ps, is) = splitAt np args return (d, ps, is, cs) Record{recPars = np, recConHead = con, recInduction = i} | i == Just CoInductive && ind /= CoInductive -> throw CoinductiveDatatype | otherwise -> return (d, args, [], [conName con]) _ -> throw NotADatatype _ -> throw NotADatatype -- | Update the target type, add more patterns to split clause -- if target becomes a function type. -- Returns the domains of the function type (if any). fixTarget :: SplitClause -> TCM (Telescope, SplitClause) fixTarget sc@SClause{ scTel = sctel, scPats = ps, scSubst = sigma, scModuleParameterSub = mpsub, scTarget = target } = caseMaybe target (return (empty, sc)) $ \ a -> do reportSDoc "tc.cover.target" 20 $ sep [ text "split clause telescope: " <+> prettyTCM sctel , text "old patterns : " <+> do addContext sctel $ prettyTCMPatternList ps ] reportSDoc "tc.cover.target" 60 $ sep [ text "substitution : " <+> text (show sigma) ] reportSDoc "tc.cover.target" 30 $ sep [ text "target type before substitution (variables may be wrong): " <+> do addContext sctel $ prettyTCM a ] TelV tel b <- telView $ applyPatSubst sigma $ unArg a reportSDoc "tc.cover.target" 15 $ sep [ text "target type telescope (after substitution): " <+> do addContext sctel $ prettyTCM tel , text "target type core (after substitution): " <+> do addContext sctel $ addContext tel $ prettyTCM b ] let n = size tel -- Andreas, 2016-10-04 issue #2236 -- Need to set origin to "Inserted" to avoid printing of hidden patterns. xs = map (mapArgInfo hiddenInserted) $ teleNamedArgs tel -- Compute new split clause sctel' = telFromList $ telToList (raise n sctel) ++ telToList tel -- Dot patterns in @ps@ need to be raised! (Issue 1298) ps' = applySubst (raiseS n) ps ++ xs newTarget = Just $ a $> b sc' = SClause { scTel = sctel' , scPats = ps' , scSubst = wkS n $ sigma -- Should be wkS instead of liftS since -- variables are only added to new tel. , scModuleParameterSub = applySubst (raiseS n) mpsub , scTarget = newTarget } -- Separate debug printing to find cause of crash (Issue 1374) reportSDoc "tc.cover.target" 30 $ sep [ text "new split clause telescope : " <+> prettyTCM sctel' ] reportSDoc "tc.cover.target" 30 $ sep [ text "new split clause patterns : " <+> do addContext sctel' $ prettyTCMPatternList ps' ] reportSDoc "tc.cover.target" 60 $ sep [ text "new split clause substitution: " <+> text (show $ scSubst sc') ] reportSDoc "tc.cover.target" 30 $ sep [ text "new split clause target : " <+> do addContext sctel' $ prettyTCM $ fromJust newTarget ] reportSDoc "tc.cover.target" 20 $ sep [ text "new split clause" , prettyTCM sc' ] return $ if n == 0 then (empty, sc { scTarget = newTarget }) else (tel, sc') -- Andreas, 2017-01-18, issue #819, set visible arguments to UserWritten. -- Otherwise, they will be printed as _. hiddenInserted :: ArgInfo -> ArgInfo hiddenInserted ai | visible ai = setOrigin UserWritten ai | otherwise = setOrigin Inserted ai -- | @computeNeighbourhood delta1 delta2 d pars ixs hix tel ps mpsub con@ -- -- @ -- delta1 Telescope before split point -- n Name of pattern variable at split point -- delta2 Telescope after split point -- d Name of datatype to split at -- pars Data type parameters -- ixs Data type indices -- hix Index of split variable -- tel Telescope for patterns ps -- ps Patterns before doing the split -- mpsub Current module parameter substitutions -- con Constructor to fit into hole -- @ -- @dtype == d pars ixs@ computeNeighbourhood :: Telescope -- ^ Telescope before split point. -> PatVarName -- ^ Name of pattern variable at split point. -> Telescope -- ^ Telescope after split point. -> QName -- ^ Name of datatype to split at. -> Args -- ^ Data type parameters. -> Args -- ^ Data type indices. -> Nat -- ^ Index of split variable. -> Telescope -- ^ Telescope for the patterns. -> [NamedArg DeBruijnPattern] -- ^ Patterns before doing the split. -> ModuleParamDict -- ^ Current module parameter substitution. -> QName -- ^ Constructor to fit into hole. -> CoverM (Maybe SplitClause) -- ^ New split clause if successful. computeNeighbourhood delta1 n delta2 d pars ixs hix tel ps mpsub c = do -- Get the type of the datatype dtype <- liftTCM $ (`piApply` pars) . defType <$> getConstInfo d -- Get the real constructor name con <- liftTCM $ fromRight __IMPOSSIBLE__ <$> getConForm c con <- return $ con { conName = c } -- What if we restore the current name? -- Andreas, 2013-11-29 changes nothing! -- Get the type of the constructor ctype <- liftTCM $ defType <$> getConInfo con -- Lookup the type of the constructor at the given parameters (gamma0, cixs) <- do TelV gamma0 (El _ d) <- liftTCM $ telView (ctype `piApply` pars) let Def _ es = ignoreSharing d Just cixs = allApplyElims es return (gamma0, cixs) -- Andreas, 2012-02-25 preserve name suggestion for recursive arguments -- of constructor let preserve (x, t@(El _ (Def d' _))) | d == d' = (n, t) preserve (x, (El s (Shared p))) = preserve (x, El s $ derefPtr p) preserve (x, t) = (x, t) gammal = map (fmap preserve) . telToList $ gamma0 gamma = telFromList gammal delta1Gamma = delta1 `abstract` gamma debugInit con ctype d pars ixs cixs delta1 delta2 gamma tel ps hix -- All variables are flexible let flex = allFlexVars delta1Gamma -- Unify constructor target and given type (in Δ₁Γ) let conIxs = drop (size pars) cixs givenIxs = raise (size gamma) ixs r <- unifyIndices delta1Gamma flex (raise (size gamma) dtype) conIxs givenIxs case r of NoUnify {} -> debugNoUnify $> Nothing DontKnow errs -> do debugCantSplit throwError $ UnificationStuck (conName con) (delta1 `abstract` gamma) conIxs givenIxs errs Unifies (delta1',rho0,_) -> do debugSubst "rho0" rho0 -- We have Δ₁' ⊢ ρ₀ : Δ₁Γ, so split it into the part for Δ₁ and the part for Γ let (rho1,rho2) = splitS (size gamma) rho0 -- Andreas, 2015-05-01 I guess it is fine to use no @conPType@ -- as the result of splitting is never used further down the pipeline. -- After splitting, Agda reloads the file. -- Andreas, 2017-09-03, issue #2729: remember that pattern was generated by case split. let conp = ConP con (ConPatternInfo (Just ConOSplit) Nothing) $ applySubst rho2 $ map (mapArgInfo hiddenInserted) $ tele2NamedArgs gamma0 gamma -- Andreas, 2016-09-08, issue #2166: use gamma0 for correct argument names -- Compute final context and substitution let rho3 = consS conp rho1 -- Δ₁' ⊢ ρ₃ : Δ₁(x:D) delta2' = applyPatSubst rho3 delta2 -- Δ₂' = Δ₂ρ₃ delta' = delta1' `abstract` delta2' -- Δ' = Δ₁'Δ₂' rho = liftS (size delta2) rho3 -- Δ' ⊢ ρ : Δ₁(x:D)Δ₂ debugTel "delta'" delta' debugSubst "rho" rho debugPs tel ps -- Apply the substitution let ps' = applySubst rho ps debugPlugged delta' ps' let mpsub' = applySubst (fromPatternSubstitution rho) mpsub return $ Just $ SClause delta' ps' rho mpsub' Nothing -- target fixed later where debugInit con ctype d pars ixs cixs delta1 delta2 gamma tel ps hix = liftTCM $ reportSDoc "tc.cover.split.con" 20 $ vcat [ text "computeNeighbourhood" , nest 2 $ vcat [ text "context=" <+> (inTopContext . prettyTCM =<< getContextTelescope) , text "con =" <+> prettyTCM con , text "ctype =" <+> prettyTCM ctype , text "ps =" <+> do inTopContext $ addContext tel $ prettyTCMPatternList ps , text "d =" <+> prettyTCM d , text "pars =" <+> do prettyList $ map prettyTCM pars , text "ixs =" <+> do addContext delta1 $ prettyList $ map prettyTCM ixs , text "cixs =" <+> do addContext gamma $ prettyList $ map prettyTCM cixs , text "delta1 =" <+> do inTopContext $ prettyTCM delta1 , text "delta2 =" <+> do inTopContext $ addContext delta1 $ addContext gamma $ prettyTCM delta2 , text "gamma =" <+> do inTopContext $ addContext delta1 $ prettyTCM gamma , text "hix =" <+> text (show hix) ] ] debugNoUnify = liftTCM $ reportSLn "tc.cover.split.con" 20 " Constructor impossible!" debugCantSplit = liftTCM $ reportSLn "tc.cover.split.con" 20 " Bad split!" debugSubst s sub = liftTCM $ reportSDoc "tc.cover.split.con" 20 $ nest 2 $ vcat [ text (s ++ " =") <+> prettyTCM sub ] debugTel s tel = liftTCM $ reportSDoc "tc.cover.split.con" 20 $ nest 2 $ vcat [ text (s ++ " =") <+> prettyTCM tel ] debugPs tel ps = liftTCM $ reportSDoc "tc.cover.split.con" 20 $ inTopContext $ addContext tel $ nest 2 $ vcat [ text "ps =" <+> prettyTCMPatternList ps ] debugPlugged delta' ps' = liftTCM $ reportSDoc "tc.cover.split.con" 20 $ inTopContext $ addContext delta' $ nest 2 $ vcat [ text "ps' =" <+> do prettyTCMPatternList ps' ] -- | Entry point from @Interaction.MakeCase@. splitClauseWithAbsurd :: SplitClause -> Nat -> TCM (Either SplitError (Either SplitClause Covering)) splitClauseWithAbsurd c x = split' Inductive False c (BlockingVar x Nothing) -- Andreas, 2016-05-03, issue 1950: -- Do not introduce trailing pattern vars after split, -- because this does not work for with-clauses. -- | Entry point from @TypeChecking.Empty@ and @Interaction.BasicOps@. -- @splitLast CoInductive@ is used in the @refine@ tactics. splitLast :: Induction -> Telescope -> [NamedArg DeBruijnPattern] -> TCM (Either SplitError Covering) splitLast ind tel ps = split ind sc (BlockingVar 0 Nothing) where sc = SClause tel ps empty empty Nothing -- | @split ind splitClause x = return res@ -- splits @splitClause@ at pattern var @x@ (de Bruijn index). -- -- Possible results @res@ are: -- -- 1. @Left err@: -- Splitting failed. -- -- 2. @Right covering@: -- A covering set of split clauses, one for each valid constructor. -- This could be the empty set (denoting an absurd clause). split :: Induction -- ^ Coinductive constructors are allowed if this argument is -- 'CoInductive'. -> SplitClause -> BlockingVar -> TCM (Either SplitError Covering) split ind sc x = fmap blendInAbsurdClause <$> split' ind True sc x where n = lookupPatternVar sc $ blockingVarNo x blendInAbsurdClause :: Either SplitClause Covering -> Covering blendInAbsurdClause = fromRight (const $ Covering n []) -- | Convert a de Bruijn index relative to the clause telescope to a de Bruijn -- level. The result should be the argument position (counted from left, -- starting with 0) to split at (dot patterns included!). lookupPatternVar :: SplitClause -> Int -> Arg Nat lookupPatternVar SClause{ scTel = tel, scPats = pats } x = arg $> if n < 0 then __IMPOSSIBLE__ else n where n = if k < 0 then __IMPOSSIBLE__ else fromMaybe __IMPOSSIBLE__ $ permPicks perm !!! k perm = fromMaybe __IMPOSSIBLE__ $ dbPatPerm pats k = size tel - x - 1 arg = telVars (size tel) tel !! k -- | @split' ind splitClause x = return res@ -- splits @splitClause@ at pattern var @x@ (de Bruijn index). -- -- Possible results @res@ are: -- -- 1. @Left err@: -- Splitting failed. -- -- 2. @Right (Left splitClause')@: -- Absurd clause (type of @x@ has 0 valid constructors). -- -- 3. @Right (Right covering)@: -- A covering set of split clauses, one for each valid constructor. split' :: Induction -- ^ Coinductive constructors are allowed if this argument is -- 'CoInductive'. -> Bool -- ^ If 'True', introduce new trailing variable patterns via -- 'fixTarget'. -> SplitClause -> BlockingVar -> TCM (Either SplitError (Either SplitClause Covering)) split' ind fixtarget sc@(SClause tel ps _ mpsub target) (BlockingVar x mcons) = liftTCM $ runExceptT $ do debugInit tel x ps mpsub -- Split the telescope at the variable -- t = type of the variable, Δ₁ ⊢ t (n, t, delta1, delta2) <- do let (tel1, Dom info (n, t) : tel2) = splitAt (size tel - x - 1) $ telToList tel return (n, Dom info t, telFromList tel1, telFromList tel2) -- Check that t is a datatype or a record -- Andreas, 2010-09-21, isDatatype now directly throws an exception if it fails -- cons = constructors of this datatype (d, pars, ixs, cons) <- inContextOfT $ isDatatype ind t -- Compute the neighbourhoods for the constructors ns <- catMaybes <$> do forM cons $ \ con -> fmap (con,) <$> do msc <- computeNeighbourhood delta1 n delta2 d pars ixs x tel ps mpsub con if not fixtarget then return msc else do Trav.forM msc $ \ sc -> lift $ snd <$> fixTarget sc{ scTarget = target } case ns of [] -> do let ps' = (fmap . fmap . fmap . fmap) (\(DBPatVar name y) -> if (x==y) then DBPatVar absurdPatternName y else DBPatVar name y) ps return $ Left $ SClause { scTel = telFromList $ telToList delta1 ++ [fmap ((,) "()") t] ++ -- add name "()" telToList delta2 , scPats = ps , scSubst = idS -- not used anyway , scModuleParameterSub = __IMPOSSIBLE__ -- not used , scTarget = Nothing -- not used } -- Andreas, 2011-10-03 -- if more than one constructor matches, we cannot be irrelevant -- (this piece of code is unreachable if --experimental-irrelevance is off) (_ : _ : _) | unusableRelevance (getRelevance t) -> throwError . IrrelevantDatatype =<< do liftTCM $ buildClosure (unDom t) -- Andreas, 2012-10-10 fail if precomputed constructor set does not cover -- all the data type constructors _ | Just pcons' <- mcons, let pcons = map conName pcons', let cons = (map fst ns), let diff = Set.fromList cons Set.\\ Set.fromList pcons, not (Set.null diff) -> do liftTCM $ reportSDoc "tc.cover.precomputed" 10 $ vcat [ hsep $ text "pcons =" : map prettyTCM pcons , hsep $ text "cons =" : map prettyTCM cons ] throwError (GenericSplitError "precomputed set of constructors does not cover all cases") _ -> return $ Right $ Covering (lookupPatternVar sc x) ns where inContextOfT :: MonadTCM tcm => tcm a -> tcm a inContextOfT = addContext tel . escapeContext (x + 1) inContextOfDelta2 :: MonadTCM tcm => tcm a -> tcm a inContextOfDelta2 = addContext tel . escapeContext x -- Debug printing debugInit tel x ps mpsub = liftTCM $ inTopContext $ do reportSDoc "tc.cover.top" 10 $ vcat [ text "TypeChecking.Coverage.split': split" , nest 2 $ vcat [ text "tel =" <+> prettyTCM tel , text "x =" <+> prettyTCM x , text "ps =" <+> do addContext tel $ prettyTCMPatternList ps , text "mpsub =" <+> prettyTCM mpsub ] ] debugHoleAndType delta1 delta2 s ps t = liftTCM $ reportSDoc "tc.cover.top" 10 $ nest 2 $ vcat $ [ text "p =" <+> text (patVarNameToString s) , text "ps =" <+> text (show ps) , text "delta1 =" <+> prettyTCM delta1 , text "delta2 =" <+> inContextOfDelta2 (prettyTCM delta2) , text "t =" <+> inContextOfT (prettyTCM t) ] -- | @splitResult f sc = return res@ -- -- If the target type of @sc@ is a record type, a covering set of -- split clauses is returned (@sc@ extended by all valid projection patterns), -- otherwise @res == Nothing@. -- Note that the empty set of split clauses is returned if the record has no fields. splitResult :: QName -> SplitClause -> TCM (Maybe Covering) splitResult f sc@(SClause tel ps _ _ target) = do reportSDoc "tc.cover.split" 10 $ vcat [ text "splitting result:" , nest 2 $ text "f =" <+> prettyTCM f , nest 2 $ text "target =" <+> (addContext tel $ maybe empty prettyTCM target) ] -- if we want to split projections, but have no target type, we give up let done = return Nothing caseMaybe target done $ \ t -> do isR <- addContext tel $ isRecordType $ unArg t case isR of Just (_r, vs, Record{ recFields = fs }) -> do reportSDoc "tc.cover" 20 $ sep [ text $ "we are of record type _r = " ++ prettyShow _r , text "applied to parameters vs = " <+> (addContext tel $ prettyTCM vs) , text $ "and have fields fs = " ++ prettyShow fs ] let es = patternsToElims ps -- Note: module parameters are part of ps let self = defaultArg $ Def f [] `applyE` es pargs = vs ++ [self] reportSDoc "tc.cover" 20 $ sep [ text "we are self = " <+> (addContext tel $ prettyTCM $ unArg self) ] let n = defaultArg $ permRange $ fromMaybe __IMPOSSIBLE__ $ dbPatPerm ps -- Andreas & James, 2013-11-19 includes the dot patterns! -- See test/succeed/CopatternsAndDotPatterns.agda for a case with dot patterns -- and copatterns which fails for @n = size tel@ with a broken case tree. -- Andreas, 2016-07-22 read the style of projections from the user's lips projOrigin <- ifM (optPostfixProjections <$> pragmaOptions) (return ProjPostfix) (return ProjPrefix) Just . Covering n <$> do forM fs $ \ proj -> do -- compute the new target dType <- defType <$> do getConstInfo $ unArg proj -- WRONG: typeOfConst $ unArg proj let -- type of projection instantiated at self target' = Just $ proj $> dType `piApply` pargs -- Always visible (#2287) projArg = fmap (Named Nothing . ProjP projOrigin) $ setHiding NotHidden proj sc' = sc { scPats = scPats sc ++ [projArg] , scSubst = idS , scTarget = target' } return (unArg proj, sc') _ -> done -- * Boring instances -- | For debugging only. instance PrettyTCM SplitClause where prettyTCM (SClause tel pats sigma mpsub target) = sep [ text "SplitClause" , nest 2 $ vcat [ text "tel =" <+> prettyTCM tel , text "pats =" <+> sep (map (prettyTCM . namedArg) pats) , text "subst =" <+> (text . show) sigma , text "mpsub =" <+> prettyTCM mpsub , text "target =" <+> do caseMaybe target empty $ \ t -> do addContext tel $ prettyTCM t -- Triggers crash (see Issue 1374). -- , text "subst target = " <+> do -- caseMaybe target empty $ \ t -> do -- addContext tel $ prettyTCM $ applySubst sigma t ] ] Agda-2.5.3/src/full/Agda/TypeChecking/Conversion.hs-boot0000644000000000000000000000140413154613124021126 0ustar0000000000000000 module Agda.TypeChecking.Conversion where import Agda.Syntax.Internal import Agda.TypeChecking.Monad compareTerm :: Comparison -> Type -> Term -> Term -> TCM () compareAtom :: Comparison -> Type -> Term -> Term -> TCM () compareArgs :: [Polarity] -> Type -> Term -> Args -> Args -> TCM () compareElims :: [Polarity] -> Type -> Term -> [Elim] -> [Elim] -> TCM () compareType :: Comparison -> Type -> Type -> TCM () compareTel :: Type -> Type -> Comparison -> Telescope -> Telescope -> TCM () compareSort :: Comparison -> Sort -> Sort -> TCM () compareLevel :: Comparison -> Level -> Level -> TCM () equalTerm :: Type -> Term -> Term -> TCM () equalType :: Type -> Type -> TCM () equalSort :: Sort -> Sort -> TCM () leqType :: Type -> Type -> TCM () Agda-2.5.3/src/full/Agda/TypeChecking/Level.hs-boot0000644000000000000000000000020213154613124020043 0ustar0000000000000000 module Agda.TypeChecking.Level where import Agda.TypeChecking.Monad import Agda.Syntax.Internal levelView :: Term -> TCM Level Agda-2.5.3/src/full/Agda/TypeChecking/SyntacticEquality.hs0000644000000000000000000001601613154613124021524 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE UndecidableInstances #-} -- | A syntactic equality check that takes meta instantiations into account, -- but does not reduce. It replaces -- @ -- (v, v') <- instantiateFull (v, v') -- v == v' -- @ -- by a more efficient routine which only traverses and instantiates the terms -- as long as they are equal. module Agda.TypeChecking.SyntacticEquality (SynEq, checkSyntacticEquality) where import Prelude hiding (mapM) import Control.Applicative hiding ((<**>)) import Control.Arrow ((***)) import Control.Monad.State hiding (mapM) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad (ReduceM) import Agda.TypeChecking.Reduce import Agda.TypeChecking.Reduce.Monad import Agda.TypeChecking.Substitute import Agda.Utils.Monad (ifM) #include "undefined.h" import Agda.Utils.Impossible -- | Syntactic equality check for terms. -- @ -- checkSyntacticEquality v v' = do -- (v,v') <- instantiateFull (v,v') -- return ((v,v'), v==v') -- @ -- only that @v,v'@ are only fully instantiated to the depth -- where they are equal. {-# SPECIALIZE checkSyntacticEquality :: Term -> Term -> ReduceM ((Term, Term), Bool) #-} {-# SPECIALIZE checkSyntacticEquality :: Type -> Type -> ReduceM ((Type, Type), Bool) #-} checkSyntacticEquality :: (SynEq a) => a -> a -> ReduceM ((a, a), Bool) checkSyntacticEquality v v' = synEq v v' `runStateT` True -- | Monad for checking syntactic equality type SynEqM = StateT Bool ReduceM -- | Return, flagging inequalty. inequal :: a -> SynEqM a inequal a = put False >> return a -- | If inequality is flagged, return, else continue. ifEqual :: (a -> SynEqM a) -> (a -> SynEqM a) ifEqual cont a = ifM get (cont a) (return a) -- Since List2 is only Applicative, not a monad, I cannot -- define a List2T monad transformer, so we do it manually: (<$$>) :: Functor f => (a -> b) -> f (a,a) -> f (b,b) f <$$> xx = (f *** f) <$> xx pure2 :: Applicative f => a -> f (a,a) pure2 a = pure (a,a) (<**>) :: Applicative f => f (a -> b, a -> b) -> f (a,a) -> f (b,b) ff <**> xx = pure (uncurry (***)) <*> ff <*> xx {- updateSharedM2 :: Monad m => (Term -> Term -> m (Term, Term)) -> Term -> Term -> m (Term, Term) updateSharedM2 f v0@(Shared p) = do v <- f (derefPtr p) case derefPtr (setPtr v p) of Var _ [] -> return v _ -> compressPointerChain v0 `pseq` return v0 updateSharedM2 f v = f v updateSharedTerm2 :: MonadTCM tcm => (Term -> Term -> tcm (Term, Term)) -> Term -> Term -> tcm (Term, Term) updateSharedTerm f v v' = ifM (liftTCM $ asks envAllowDestructiveUpdate) (updateSharedM2 f v v') (f (ignoreSharing v) (ignoreSharing v')) -} -- | Instantiate full as long as things are equal class SynEq a where synEq :: a -> a -> SynEqM (a,a) synEq' :: a -> a -> SynEqM (a,a) synEq' a a' = ifEqual (uncurry synEq) (a, a') -- | Syntactic term equality ignores 'DontCare' stuff. instance SynEq Term where synEq v v' = do (v, v') <- lift $ instantiate' (v, v') -- currently destroys sharing -- TODO: preserve sharing! case (ignoreSharing v, ignoreSharing v') of (Var i vs, Var i' vs') | i == i' -> Var i <$$> synEq vs vs' (Con c ci vs,Con c' ci' vs') | c == c' -> Con c (bestConInfo ci ci') <$$> synEq vs vs' (Def f vs, Def f' vs') | f == f' -> Def f <$$> synEq vs vs' (MetaV x vs, MetaV x' vs') | x == x' -> MetaV x <$$> synEq vs vs' (Lit l , Lit l' ) | l == l' -> pure2 $ v (Lam h b , Lam h' b' ) | h == h' -> Lam h <$$> synEq b b' (Level l , Level l' ) -> levelTm <$$> synEq l l' (Sort s , Sort s' ) -> sortTm <$$> synEq s s' (Pi a b , Pi a' b' ) -> Pi <$$> synEq a a' <**> synEq' b b' (DontCare _, DontCare _ ) -> pure (v, v') -- Irrelevant things are syntactically equal. ALT: -- DontCare <$$> synEq v v' (Shared{} , _ ) -> __IMPOSSIBLE__ (_ , Shared{} ) -> __IMPOSSIBLE__ _ -> inequal (v, v') instance SynEq Level where synEq (Max vs) (Max vs') = levelMax <$$> synEq vs vs' instance SynEq PlusLevel where synEq l l' = do case (l, l') of (ClosedLevel v, ClosedLevel v') | v == v' -> pure2 l (Plus n v, Plus n' v') | n == n' -> Plus n <$$> synEq v v' _ -> inequal (l, l') instance SynEq LevelAtom where synEq l l' = do l <- lift (unBlock =<< instantiate' l) case (l, l') of (MetaLevel m vs , MetaLevel m' vs' ) | m == m' -> MetaLevel m <$$> synEq vs vs' (UnreducedLevel v, UnreducedLevel v' ) -> UnreducedLevel <$$> synEq v v' -- The reason for being blocked should not matter for equality. (NeutralLevel r v, NeutralLevel r' v') -> NeutralLevel r <$$> synEq v v' (BlockedLevel m v, BlockedLevel m' v') -> BlockedLevel m <$$> synEq v v' _ -> inequal (l, l') where unBlock l = case l of BlockedLevel m v -> ifM (isInstantiatedMeta m) (pure $ UnreducedLevel v) (pure l) _ -> pure l instance SynEq Sort where synEq s s' = do (s, s') <- lift $ instantiate' (s, s') case (s, s') of (Type l , Type l' ) -> levelSort <$$> synEq l l' (DLub a b, DLub a' b') -> dLub <$$> synEq a a' <**> synEq' b b' (Prop , Prop ) -> pure2 s (Inf , Inf ) -> pure2 s _ -> inequal (s, s') -- | Syntactic equality ignores sorts. instance SynEq Type where synEq (El s t) (El s' t') = (El s *** El s') <$> synEq t t' instance SynEq a => SynEq [a] where synEq as as' | length as == length as' = unzip <$> zipWithM synEq' as as' | otherwise = inequal (as, as') instance SynEq a => SynEq (Elim' a) where synEq e e' = case (e, e') of (Proj _ f, Proj _ f') | f == f' -> pure2 e (Apply a, Apply a') -> Apply <$$> synEq a a' _ -> inequal (e, e') instance (Subst t a, SynEq a) => SynEq (Abs a) where synEq a a' = case (a, a') of (NoAbs x b, NoAbs x' b') -> (NoAbs x *** NoAbs x') <$> synEq b b' (Abs x b, Abs x' b') -> (Abs x *** Abs x') <$> synEq b b' (Abs x b, NoAbs x' b') -> Abs x <$$> synEq b (raise 1 b') -- TODO: mkAbs? (NoAbs x b, Abs x' b') -> Abs x' <$$> synEq (raise 1 b) b' {- TRIGGERS test/fail/UnequalHiding -- | Ignores 'ArgInfo'. instance SynEq a => SynEq (Arg c a) where synEq (Arg ai a) (Arg ai' a') = (Arg ai *** Arg ai') <$> synEq a a' -- | Ignores 'ArgInfo'. instance SynEq a => SynEq (Dom c a) where synEq (Dom ai a) (Dom ai' a') = (Dom ai *** Dom ai') <$> synEq a a' -} instance SynEq a => SynEq (Arg a) where synEq (Arg ai a) (Arg ai' a') = Arg <$$> synEq ai ai' <**> synEq a a' instance SynEq a => SynEq (Dom a) where synEq (Dom ai a) (Dom ai' a') = Dom <$$> synEq ai ai' <**> synEq a a' instance SynEq ArgInfo where synEq ai@(ArgInfo h r o) ai'@(ArgInfo h' r' o') | h == h', r == r' = pure2 ai | otherwise = inequal (ai, ai') Agda-2.5.3/src/full/Agda/TypeChecking/Reduce.hs0000644000000000000000000014547513154613124017270 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} {-# LANGUAGE UndecidableInstances #-} module Agda.TypeChecking.Reduce where import Prelude hiding (mapM) import Control.Monad.Reader hiding (mapM) import Control.Applicative import qualified Data.List as List import Data.Maybe import Data.Map (Map) import Data.Traversable import Data.Hashable import Agda.Interaction.Options import Agda.Syntax.Position import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.Syntax.Scope.Base (Scope) import Agda.Syntax.Literal import Agda.TypeChecking.Monad hiding ( underAbstraction_, enterClosure, isInstantiatedMeta , getConstInfo , lookupMeta ) import qualified Agda.TypeChecking.Monad as TCM import Agda.TypeChecking.Monad.Builtin hiding (getPrimitive, constructorForm) import Agda.TypeChecking.Substitute import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.EtaContract import Agda.TypeChecking.Reduce.Monad import {-# SOURCE #-} Agda.TypeChecking.CompiledClause.Match import {-# SOURCE #-} Agda.TypeChecking.Patterns.Match import {-# SOURCE #-} Agda.TypeChecking.Pretty import {-# SOURCE #-} Agda.TypeChecking.Rewriting import {-# SOURCE #-} Agda.TypeChecking.Reduce.Fast import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.Monad import Agda.Utils.HashMap (HashMap) import Agda.Utils.Size import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible instantiate :: Instantiate a => a -> TCM a instantiate = runReduceM . instantiate' instantiateFull :: InstantiateFull a => a -> TCM a instantiateFull = runReduceM . instantiateFull' reduce :: Reduce a => a -> TCM a reduce = runReduceM . reduce' reduceB :: Reduce a => a -> TCM (Blocked a) reduceB = runReduceM . reduceB' normalise :: Normalise a => a -> TCM a normalise = runReduceM . normalise' simplify :: Simplify a => a -> TCM a simplify = runReduceM . simplify' -- | Meaning no metas left in the instantiation. isFullyInstantiatedMeta :: MetaId -> TCM Bool isFullyInstantiatedMeta m = do mv <- TCM.lookupMeta m case mvInstantiation mv of InstV _tel v -> null . allMetas <$> instantiateFull v _ -> return False -- | Instantiate something. -- Results in an open meta variable or a non meta. -- Doesn't do any reduction, and preserves blocking tags (when blocking meta -- is uninstantiated). class Instantiate t where instantiate' :: t -> ReduceM t instance Instantiate Term where instantiate' t@(MetaV x es) = do mi <- mvInstantiation <$> lookupMeta x case mi of InstV tel v -> instantiate' inst where -- A slight complication here is that the meta might be underapplied, -- in which case we have to build the lambda abstraction before -- applying the substitution, or overapplied in which case we need to -- fall back to applyE. (es1, es2) = splitAt (length tel) es vs1 = reverse $ map unArg $ fromMaybe __IMPOSSIBLE__ $ allApplyElims es1 rho = vs1 ++# wkS (length vs1) idS -- really should be .. ++# emptyS but using wkS makes it reduce to idS -- when applicable -- specification: inst == foldr mkLam v tel `applyE` es inst = applySubst rho (foldr mkLam v $ drop (length es1) tel) `applyE` es2 Open -> return t OpenIFS -> return t BlockedConst _ -> return t PostponedTypeCheckingProblem _ _ -> return t instantiate' (Level l) = levelTm <$> instantiate' l instantiate' (Sort s) = sortTm <$> instantiate' s instantiate' v@Shared{} = updateSharedTerm instantiate' v instantiate' t = return t instance Instantiate Level where instantiate' (Max as) = levelMax <$> instantiate' as instance Instantiate PlusLevel where instantiate' l@ClosedLevel{} = return l instantiate' (Plus n a) = Plus n <$> instantiate' a instance Instantiate LevelAtom where instantiate' l = case l of MetaLevel m vs -> do v <- instantiate' (MetaV m vs) case ignoreSharing v of MetaV m vs -> return $ MetaLevel m vs _ -> return $ UnreducedLevel v UnreducedLevel l -> UnreducedLevel <$> instantiate' l _ -> return l instance Instantiate a => Instantiate (Blocked a) where instantiate' v@NotBlocked{} = return v instantiate' v@(Blocked x u) = do mi <- mvInstantiation <$> lookupMeta x case mi of InstV{} -> notBlocked <$> instantiate' u Open -> return v OpenIFS -> return v BlockedConst{} -> return v PostponedTypeCheckingProblem{} -> return v instance Instantiate Type where instantiate' (El s t) = El <$> instantiate' s <*> instantiate' t instance Instantiate Sort where instantiate' s = case s of Type l -> levelSort <$> instantiate' l _ -> return s instance Instantiate Elim where instantiate' (Apply v) = Apply <$> instantiate' v instantiate' (Proj o f)= pure $ Proj o f instance Instantiate t => Instantiate (Abs t) where instantiate' = traverse instantiate' instance Instantiate t => Instantiate (Arg t) where instantiate' = traverse instantiate' instance Instantiate t => Instantiate (Dom t) where instantiate' = traverse instantiate' instance Instantiate t => Instantiate [t] where instantiate' = traverse instantiate' instance (Instantiate a, Instantiate b) => Instantiate (a,b) where instantiate' (x,y) = (,) <$> instantiate' x <*> instantiate' y instance (Instantiate a, Instantiate b,Instantiate c) => Instantiate (a,b,c) where instantiate' (x,y,z) = (,,) <$> instantiate' x <*> instantiate' y <*> instantiate' z instance Instantiate a => Instantiate (Closure a) where instantiate' cl = do x <- enterClosure cl instantiate' return $ cl { clValue = x } instance Instantiate Telescope where instantiate' EmptyTel = return EmptyTel instantiate' (ExtendTel a tel) = ExtendTel <$> instantiate' a <*> instantiate' tel --instantiate' tel = return tel instance Instantiate Constraint where instantiate' (ValueCmp cmp t u v) = do (t,u,v) <- instantiate' (t,u,v) return $ ValueCmp cmp t u v instantiate' (ElimCmp cmp t v as bs) = ElimCmp cmp <$> instantiate' t <*> instantiate' v <*> instantiate' as <*> instantiate' bs instantiate' (LevelCmp cmp u v) = uncurry (LevelCmp cmp) <$> instantiate' (u,v) instantiate' (TypeCmp cmp a b) = uncurry (TypeCmp cmp) <$> instantiate' (a,b) instantiate' (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp) <$> instantiate' (tela,telb) instantiate' (SortCmp cmp a b) = uncurry (SortCmp cmp) <$> instantiate' (a,b) instantiate' (Guarded c pid) = Guarded <$> instantiate' c <*> pure pid instantiate' (UnBlock m) = return $ UnBlock m instantiate' (FindInScope m b args) = FindInScope m b <$> mapM instantiate' args instantiate' (IsEmpty r t) = IsEmpty r <$> instantiate' t instantiate' (CheckSizeLtSat t) = CheckSizeLtSat <$> instantiate' t instance Instantiate e => Instantiate (Map k e) where instantiate' = traverse instantiate' instance Instantiate Candidate where instantiate' (Candidate u t eti ov) = Candidate <$> instantiate' u <*> instantiate' t <*> pure eti <*> pure ov instance Instantiate EqualityView where instantiate' (OtherType t) = OtherType <$> instantiate' t instantiate' (EqualityType s eq l t a b) = EqualityType <$> instantiate' s <*> return eq <*> mapM instantiate' l <*> instantiate' t <*> instantiate' a <*> instantiate' b --------------------------------------------------------------------------- -- * Reduction to weak head normal form. --------------------------------------------------------------------------- -- | Case on whether a term is blocked on a meta (or is a meta). -- That means it can change its shape when the meta is instantiated. ifBlocked :: MonadTCM tcm => Term -> (MetaId -> Term -> tcm a) -> (Term -> tcm a) -> tcm a ifBlocked t blocked unblocked = do t <- liftTCM $ reduceB t case ignoreSharing <$> t of Blocked m _ -> blocked m (ignoreBlocking t) NotBlocked _ (MetaV m _) -> blocked m (ignoreBlocking t) NotBlocked{} -> unblocked (ignoreBlocking t) -- | Case on whether a type is blocked on a meta (or is a meta). ifBlockedType :: MonadTCM tcm => Type -> (MetaId -> Type -> tcm a) -> (Type -> tcm a) -> tcm a ifBlockedType (El s t) blocked unblocked = ifBlocked t (\ m v -> blocked m $ El s v) (\ v -> unblocked $ El s v) class Reduce t where reduce' :: t -> ReduceM t reduceB' :: t -> ReduceM (Blocked t) reduce' t = ignoreBlocking <$> reduceB' t reduceB' t = notBlocked <$> reduce' t instance Reduce Type where reduce' (El s t) = El s <$> reduce' t reduceB' (El s t) = fmap (El s) <$> reduceB' t instance Reduce Sort where reduce' s = {-# SCC "reduce'" #-} ifNotM hasUniversePolymorphism (red s) $ {- else -} red =<< instantiateFull' s where red s = do s <- instantiate' s case s of DLub s1 s2 -> do s <- dLub <$> reduce' s1 <*> reduce' s2 case s of DLub{} -> return s _ -> reduce' s -- TODO: not so nice Prop -> return s Type s' -> levelSort <$> reduce' s' Inf -> return Inf SizeUniv -> return SizeUniv instance Reduce Elim where reduce' (Apply v) = Apply <$> reduce' v reduce' (Proj o f)= pure $ Proj o f instance Reduce Level where reduce' (Max as) = levelMax <$> mapM reduce' as reduceB' (Max as) = fmap levelMax . traverse id <$> traverse reduceB' as instance Reduce PlusLevel where reduceB' l@ClosedLevel{} = return $ notBlocked l reduceB' (Plus n l) = fmap (Plus n) <$> reduceB' l instance Reduce LevelAtom where reduceB' l = case l of MetaLevel m vs -> fromTm (MetaV m vs) NeutralLevel r v -> return $ NotBlocked r $ NeutralLevel r v BlockedLevel m v -> ifM (isInstantiatedMeta m) (fromTm v) (return $ Blocked m $ BlockedLevel m v) UnreducedLevel v -> fromTm v where fromTm v = do bv <- reduceB' v let v = ignoreBlocking bv case ignoreSharing <$> bv of NotBlocked r (MetaV m vs) -> return $ NotBlocked r $ MetaLevel m vs Blocked m _ -> return $ Blocked m $ BlockedLevel m v NotBlocked r _ -> return $ NotBlocked r $ NeutralLevel r v instance (Subst t a, Reduce a) => Reduce (Abs a) where reduce' b@(Abs x _) = Abs x <$> underAbstraction_ b reduce' reduce' (NoAbs x v) = NoAbs x <$> reduce' v -- Lists are never blocked instance Reduce t => Reduce [t] where reduce' = traverse reduce' instance Reduce t => Reduce (Arg t) where reduce' a = case getRelevance a of Irrelevant -> return a -- Don't reduce' irr. args!? _ -> traverse reduce' a reduceB' t = traverse id <$> traverse reduceB' t instance Reduce t => Reduce (Dom t) where reduce' = traverse reduce' reduceB' t = traverse id <$> traverse reduceB' t -- Tuples are never blocked instance (Reduce a, Reduce b) => Reduce (a,b) where reduce' (x,y) = (,) <$> reduce' x <*> reduce' y instance (Reduce a, Reduce b,Reduce c) => Reduce (a,b,c) where reduce' (x,y,z) = (,,) <$> reduce' x <*> reduce' y <*> reduce' z instance Reduce Term where reduceB' = {-# SCC "reduce'" #-} maybeFastReduceTerm maybeFastReduceTerm :: Term -> ReduceM (Blocked Term) maybeFastReduceTerm v = do let tryFast = case v of Def{} -> True Con{} -> True _ -> False if not tryFast then slowReduceTerm v else do s <- optSharing <$> commandLineOptions allowed <- asks envAllowedReductions let notAll = List.delete NonTerminatingReductions allowed /= allReductions if s || notAll then slowReduceTerm v else fastReduce (elem NonTerminatingReductions allowed) v slowReduceTerm :: Term -> ReduceM (Blocked Term) slowReduceTerm v = do v <- instantiate' v let done = return $ notBlocked v case v of -- Andreas, 2012-11-05 not reducing meta args does not destroy anything -- and seems to save 2% sec on the standard library -- MetaV x args -> notBlocked . MetaV x <$> reduce' args MetaV x es -> done Def f es -> unfoldDefinitionE False reduceB' (Def f []) f es Con c ci args -> do -- Constructors can reduce' when they come from an -- instantiated module. v <- unfoldDefinition False reduceB' (Con c ci []) (conName c) args traverse reduceNat v Sort s -> fmap sortTm <$> reduceB' s Level l -> ifM (elem LevelReductions <$> asks envAllowedReductions) {- then -} (fmap levelTm <$> reduceB' l) {- else -} done Pi _ _ -> done Lit _ -> done Var _ _ -> done Lam _ _ -> done DontCare _ -> done Shared{} -> updateSharedTermF reduceB' v where -- NOTE: reduceNat can traverse the entire term. reduceNat v@Shared{} = updateSharedTerm reduceNat v reduceNat v@(Con c ci []) = do mz <- getBuiltin' builtinZero case v of _ | Just v == mz -> return $ Lit $ LitNat (getRange c) 0 _ -> return v reduceNat v@(Con c ci [a]) | visible a && isRelevant a = do ms <- fmap ignoreSharing <$> getBuiltin' builtinSuc case v of _ | Just (Con c ci []) == ms -> inc <$> reduce' (unArg a) _ -> return v where inc w = case ignoreSharing w of Lit (LitNat r n) -> Lit (LitNat (fuseRange c r) $ n + 1) _ -> Con c ci [defaultArg w] reduceNat v = return v -- Andreas, 2013-03-20 recursive invokations of unfoldCorecursion -- need also to instantiate metas, see Issue 826. unfoldCorecursionE :: Elim -> ReduceM (Blocked Elim) unfoldCorecursionE (Proj o p) = notBlocked . Proj o <$> getOriginalProjection p unfoldCorecursionE (Apply (Arg info v)) = fmap (Apply . Arg info) <$> unfoldCorecursion v unfoldCorecursion :: Term -> ReduceM (Blocked Term) unfoldCorecursion v = do v <- instantiate' v case compressPointerChain v of Def f es -> unfoldDefinitionE True unfoldCorecursion (Def f []) f es v@(Shared p) -> case derefPtr p of Def{} -> updateSharedFM unfoldCorecursion v _ -> slowReduceTerm v _ -> slowReduceTerm v -- | If the first argument is 'True', then a single delayed clause may -- be unfolded. unfoldDefinition :: Bool -> (Term -> ReduceM (Blocked Term)) -> Term -> QName -> Args -> ReduceM (Blocked Term) unfoldDefinition unfoldDelayed keepGoing v f args = unfoldDefinitionE unfoldDelayed keepGoing v f (map Apply args) unfoldDefinitionE :: Bool -> (Term -> ReduceM (Blocked Term)) -> Term -> QName -> Elims -> ReduceM (Blocked Term) unfoldDefinitionE unfoldDelayed keepGoing v f es = do r <- unfoldDefinitionStep unfoldDelayed v f es case r of NoReduction v -> return v YesReduction _ v -> keepGoing v unfoldDefinition' :: Bool -> (Simplification -> Term -> ReduceM (Simplification, Blocked Term)) -> Term -> QName -> Elims -> ReduceM (Simplification, Blocked Term) unfoldDefinition' unfoldDelayed keepGoing v0 f es = do r <- unfoldDefinitionStep unfoldDelayed v0 f es case r of NoReduction v -> return (NoSimplification, v) YesReduction simp v -> keepGoing simp v unfoldDefinitionStep :: Bool -> Term -> QName -> Elims -> ReduceM (Reduced (Blocked Term) Term) unfoldDefinitionStep unfoldDelayed v0 f es = {-# SCC "reduceDef" #-} do info <- getConstInfo f rewr <- instantiateRewriteRules =<< getRewriteRulesFor f allowed <- asks envAllowedReductions let def = theDef info v = v0 `applyE` es -- Non-terminating functions -- (i.e., those that failed the termination check) -- and delayed definitions -- are not unfolded unless explicitely permitted. dontUnfold = (defNonterminating info && notElem NonTerminatingReductions allowed) || (defTerminationUnconfirmed info && notElem UnconfirmedReductions allowed) || (defDelayed info == Delayed && not unfoldDelayed) copatterns = case def of Function{funCopatternLHS = b} -> b _ -> False case def of Constructor{conSrcCon = c} -> noReduction $ notBlocked $ Con (c `withRangeOf` f) ConOSystem [] `applyE` es Primitive{primAbstr = ConcreteDef, primName = x, primClauses = cls} -> do pf <- fromMaybe __IMPOSSIBLE__ <$> getPrimitive' x if FunctionReductions `elem` allowed then reducePrimitive x v0 f es pf dontUnfold cls (defCompiled info) rewr else noReduction $ notBlocked v _ -> do if (RecursiveReductions `elem` allowed) || (isJust (isProjection_ def) && ProjectionReductions `elem` allowed) || -- includes projection-like (isInlineFun def && InlineReductions `elem` allowed) || (definitelyNonRecursive_ def && copatterns && CopatternReductions `elem` allowed) || (definitelyNonRecursive_ def && FunctionReductions `elem` allowed) then reduceNormalE v0 f (map notReduced es) dontUnfold (defClauses info) (defCompiled info) rewr else noReduction $ notBlocked v -- Andrea(s), 2014-12-05 OK? where noReduction = return . NoReduction yesReduction s = return . YesReduction s reducePrimitive x v0 f es pf dontUnfold cls mcc rewr | length es < ar = noReduction $ NotBlocked Underapplied $ v0 `applyE` es -- not fully applied | otherwise = {-# SCC "reducePrimitive" #-} do let (es1,es2) = splitAt ar es args1 = fromMaybe __IMPOSSIBLE__ $ mapM isApplyElim es1 r <- primFunImplementation pf args1 case r of NoReduction args1' -> do let es1' = map (fmap Apply) args1' if null cls then do noReduction $ applyE (Def f []) <$> do traverse id $ map mredToBlocked es1' ++ map notBlocked es2 else reduceNormalE v0 f (es1' ++ map notReduced es2) dontUnfold cls mcc rewr YesReduction simpl v -> yesReduction simpl $ v `applyE` es2 where ar = primFunArity pf mredToBlocked :: MaybeReduced a -> Blocked a mredToBlocked (MaybeRed NotReduced x) = notBlocked x mredToBlocked (MaybeRed (Reduced b) x) = x <$ b reduceNormalE :: Term -> QName -> [MaybeReduced Elim] -> Bool -> [Clause] -> Maybe CompiledClauses -> RewriteRules -> ReduceM (Reduced (Blocked Term) Term) reduceNormalE v0 f es dontUnfold def mcc rewr = {-# SCC "reduceNormal" #-} do case (def,rewr) of _ | dontUnfold -> defaultResult -- non-terminating or delayed ([],[]) -> defaultResult -- no definition for head (cls,rewr) -> do ev <- appDefE_ f v0 cls mcc rewr es debugReduce ev return ev where defaultResult = noReduction $ NotBlocked AbsurdMatch vfull vfull = v0 `applyE` map ignoreReduced es debugReduce ev = verboseS "tc.reduce" 90 $ do case ev of NoReduction v -> do reportSDoc "tc.reduce" 90 $ vcat [ text "*** tried to reduce " <+> prettyTCM f , text " es = " <+> sep (map (prettyTCM . ignoreReduced) es) -- , text "*** tried to reduce " <+> prettyTCM vfull , text " stuck on" <+> prettyTCM (ignoreBlocking v) ] YesReduction _simpl v -> do reportSDoc "tc.reduce" 90 $ text "*** reduced definition: " <+> prettyTCM f reportSDoc "tc.reduce" 95 $ text " result" <+> prettyTCM v reportSDoc "tc.reduce" 100 $ text " raw " <+> text (show v) -- | Reduce a non-primitive definition if it is a copy linking to another def. reduceDefCopy :: QName -> Elims -> TCM (Reduced () Term) reduceDefCopy f es = do info <- TCM.getConstInfo f rewr <- instantiateRewriteRules =<< TCM.getRewriteRulesFor f if (defCopy info) then reduceDef_ info rewr f es else return $ NoReduction () where reduceDef_ :: Definition -> RewriteRules -> QName -> Elims -> TCM (Reduced () Term) reduceDef_ info rewr f es = do let v0 = Def f [] cls = (defClauses info) mcc = (defCompiled info) if (defDelayed info == Delayed) || (defNonterminating info) then return $ NoReduction () else do ev <- runReduceM $ appDefE_ f v0 cls mcc rewr $ map notReduced es case ev of YesReduction simpl t -> return $ YesReduction simpl t NoReduction{} -> return $ NoReduction () -- | Reduce simple (single clause) definitions. reduceHead :: Term -> TCM (Blocked Term) reduceHead = runReduceM . reduceHead' reduceHead' :: Term -> ReduceM (Blocked Term) reduceHead' v = do -- ignoreAbstractMode $ do -- Andreas, 2013-02-18 ignoreAbstractMode leads to information leakage -- see Issue 796 -- first, possibly rewrite literal v to constructor form v <- constructorForm v traceSDoc "tc.inj.reduce" 30 (text "reduceHead" <+> prettyTCM v) $ do case ignoreSharing v of Def f es -> do abstractMode <- envAbstractMode <$> ask isAbstract <- treatAbstractly f traceSLn "tc.inj.reduce" 50 ( "reduceHead: we are in " ++ show abstractMode++ "; " ++ show f ++ " is treated " ++ if isAbstract then "abstractly" else "concretely" ) $ do let v0 = Def f [] red = unfoldDefinitionE False reduceHead' v0 f es def <- theDef <$> getConstInfo f case def of -- Andreas, 2012-11-06 unfold aliases (single clause terminating functions) -- see test/succeed/Issue747 -- We restrict this to terminating functions to not make the -- type checker loop here on non-terminating functions. -- see test/fail/TerminationInfiniteRecord Function{ funClauses = [ _ ], funDelayed = NotDelayed, funTerminates = Just True } -> do traceSLn "tc.inj.reduce" 50 ("reduceHead: head " ++ show f ++ " is Function") $ do red Datatype{ dataClause = Just _ } -> red Record{ recClause = Just _ } -> red _ -> return $ notBlocked v _ -> return $ notBlocked v -- | Apply a definition using the compiled clauses, or fall back to -- ordinary clauses if no compiled clauses exist. appDef_ :: QName -> Term -> [Clause] -> Maybe CompiledClauses -> RewriteRules -> MaybeReducedArgs -> ReduceM (Reduced (Blocked Term) Term) appDef_ f v0 cls mcc rewr args = appDefE_ f v0 cls mcc rewr $ map (fmap Apply) args appDefE_ :: QName -> Term -> [Clause] -> Maybe CompiledClauses -> RewriteRules -> MaybeReducedElims -> ReduceM (Reduced (Blocked Term) Term) appDefE_ f v0 cls mcc rewr args = local (\ e -> e { envAppDef = Just f }) $ maybe (appDefE' v0 cls rewr args) (\cc -> appDefE v0 cc rewr args) mcc -- | Apply a defined function to it's arguments, using the compiled clauses. -- The original term is the first argument applied to the third. appDef :: Term -> CompiledClauses -> RewriteRules -> MaybeReducedArgs -> ReduceM (Reduced (Blocked Term) Term) appDef v cc rewr args = appDefE v cc rewr $ map (fmap Apply) args appDefE :: Term -> CompiledClauses -> RewriteRules -> MaybeReducedElims -> ReduceM (Reduced (Blocked Term) Term) appDefE v cc rewr es = do r <- matchCompiledE cc es case r of YesReduction simpl t -> return $ YesReduction simpl t NoReduction es' -> rewrite (void es') v rewr (ignoreBlocking es') -- | Apply a defined function to it's arguments, using the original clauses. appDef' :: Term -> [Clause] -> RewriteRules -> MaybeReducedArgs -> ReduceM (Reduced (Blocked Term) Term) appDef' v cls rewr args = appDefE' v cls rewr $ map (fmap Apply) args appDefE' :: Term -> [Clause] -> RewriteRules -> MaybeReducedElims -> ReduceM (Reduced (Blocked Term) Term) appDefE' v cls rewr es = goCls cls $ map ignoreReduced es where goCls :: [Clause] -> [Elim] -> ReduceM (Reduced (Blocked Term) Term) goCls cl es = do case cl of -- Andreas, 2013-10-26 In case of an incomplete match, -- we just do not reduce. This allows adding single function -- clauses after they have been type-checked, to type-check -- the remaining clauses (see Issue 907). -- Andrea(s), 2014-12-05: We return 'MissingClauses' here, since this -- is the most conservative reason. [] -> rewrite (NotBlocked MissingClauses ()) v rewr es cl : cls -> do let pats = namedClausePats cl body = clauseBody cl npats = length pats nvars = size $ clauseTel cl -- if clause is underapplied, skip to next clause if length es < npats then goCls cls es else do let (es0, es1) = splitAt npats es (m, es0) <- matchCopatterns pats es0 es <- return $ es0 ++ es1 case m of No -> goCls cls es DontKnow b -> rewrite b v rewr es Yes simpl vs -- vs is the subst. for the variables bound in body | Just w <- body -> do -- clause has body? -- TODO: let matchPatterns also return the reduced forms -- of the original arguments! -- Andreas, 2013-05-19 isn't this done now? let sigma = buildSubstitution __IMPOSSIBLE__ nvars vs return $ YesReduction simpl $ applySubst sigma w `applyE` es1 | otherwise -> rewrite (NotBlocked AbsurdMatch ()) v rewr es instance Reduce a => Reduce (Closure a) where reduce' cl = do x <- enterClosure cl reduce' return $ cl { clValue = x } instance Reduce Telescope where reduce' EmptyTel = return EmptyTel reduce' (ExtendTel a tel) = ExtendTel <$> reduce' a <*> reduce' tel instance Reduce Constraint where reduce' (ValueCmp cmp t u v) = do (t,u,v) <- reduce' (t,u,v) return $ ValueCmp cmp t u v reduce' (ElimCmp cmp t v as bs) = ElimCmp cmp <$> reduce' t <*> reduce' v <*> reduce' as <*> reduce' bs reduce' (LevelCmp cmp u v) = uncurry (LevelCmp cmp) <$> reduce' (u,v) reduce' (TypeCmp cmp a b) = uncurry (TypeCmp cmp) <$> reduce' (a,b) reduce' (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp) <$> reduce' (tela,telb) reduce' (SortCmp cmp a b) = uncurry (SortCmp cmp) <$> reduce' (a,b) reduce' (Guarded c pid) = Guarded <$> reduce' c <*> pure pid reduce' (UnBlock m) = return $ UnBlock m reduce' (FindInScope m b cands) = FindInScope m b <$> mapM reduce' cands reduce' (IsEmpty r t) = IsEmpty r <$> reduce' t reduce' (CheckSizeLtSat t) = CheckSizeLtSat <$> reduce' t instance Reduce e => Reduce (Map k e) where reduce' = traverse reduce' instance Reduce Candidate where reduce' (Candidate u t eti ov) = Candidate <$> reduce' u <*> reduce' t <*> pure eti <*> pure ov instance Reduce EqualityView where reduce' (OtherType t) = OtherType <$> reduce' t reduce' (EqualityType s eq l t a b) = EqualityType <$> reduce' s <*> return eq <*> mapM reduce' l <*> reduce' t <*> reduce' a <*> reduce' b --------------------------------------------------------------------------- -- * Simplification --------------------------------------------------------------------------- -- | Only unfold definitions if this leads to simplification -- which means that a constructor/literal pattern is matched. class Simplify t where simplify' :: t -> ReduceM t instance Simplify Term where simplify' v = do v <- instantiate' v case v of Def f vs -> do let keepGoing simp v = return (simp, notBlocked v) (simpl, v) <- unfoldDefinition' False keepGoing (Def f []) f vs traceSDoc "tc.simplify'" 20 ( text ("simplify': unfolding definition returns " ++ show simpl) <+> prettyTCM (ignoreBlocking v)) $ do case simpl of YesSimplification -> simplifyBlocked' v -- Dangerous, but if @simpl@ then @v /= Def f vs@ NoSimplification -> Def f <$> simplify' vs MetaV x vs -> MetaV x <$> simplify' vs Con c ci vs-> Con c ci <$> simplify' vs Sort s -> sortTm <$> simplify' s Level l -> levelTm <$> simplify' l Pi a b -> Pi <$> simplify' a <*> simplify' b Lit l -> return v Var i vs -> Var i <$> simplify' vs Lam h v -> Lam h <$> simplify' v DontCare v -> dontCare <$> simplify' v Shared{} -> updateSharedTerm simplify' v simplifyBlocked' :: Simplify t => Blocked t -> ReduceM t simplifyBlocked' (Blocked _ t) = return t simplifyBlocked' (NotBlocked _ t) = simplify' t -- Andrea(s), 2014-12-05 OK? instance Simplify Type where simplify' (El s t) = El <$> simplify' s <*> simplify' t instance Simplify Elim where simplify' (Apply v) = Apply <$> simplify' v simplify' (Proj o f)= pure $ Proj o f instance Simplify Sort where simplify' s = do case s of DLub s1 s2 -> dLub <$> simplify' s1 <*> simplify' s2 Type s -> levelSort <$> simplify' s Prop -> return s Inf -> return s SizeUniv -> return s instance Simplify Level where simplify' (Max as) = levelMax <$> simplify' as instance Simplify PlusLevel where simplify' l@ClosedLevel{} = return l simplify' (Plus n l) = Plus n <$> simplify' l instance Simplify LevelAtom where simplify' l = do l <- instantiate' l case l of MetaLevel m vs -> MetaLevel m <$> simplify' vs BlockedLevel m v -> BlockedLevel m <$> simplify' v NeutralLevel r v -> NeutralLevel r <$> simplify' v -- ?? UnreducedLevel v -> UnreducedLevel <$> simplify' v -- ?? instance (Subst t a, Simplify a) => Simplify (Abs a) where simplify' a@(Abs x _) = Abs x <$> underAbstraction_ a simplify' simplify' (NoAbs x v) = NoAbs x <$> simplify' v instance Simplify t => Simplify (Arg t) where simplify' = traverse simplify' instance Simplify t => Simplify (Named name t) where simplify' = traverse simplify' instance Simplify t => Simplify (Dom t) where simplify' = traverse simplify' instance Simplify t => Simplify [t] where simplify' = traverse simplify' instance Simplify e => Simplify (Map k e) where simplify' = traverse simplify' instance Simplify a => Simplify (Maybe a) where simplify' = traverse simplify' instance (Simplify a, Simplify b) => Simplify (a,b) where simplify' (x,y) = (,) <$> simplify' x <*> simplify' y instance (Simplify a, Simplify b, Simplify c) => Simplify (a,b,c) where simplify' (x,y,z) = do (x,(y,z)) <- simplify' (x,(y,z)) return (x,y,z) instance Simplify a => Simplify (Closure a) where simplify' cl = do x <- enterClosure cl simplify' return $ cl { clValue = x } instance (Subst t a, Simplify a) => Simplify (Tele a) where simplify' EmptyTel = return EmptyTel simplify' (ExtendTel a b) = uncurry ExtendTel <$> simplify' (a, b) instance Simplify ProblemConstraint where simplify' (PConstr pid c) = PConstr pid <$> simplify' c instance Simplify Constraint where simplify' (ValueCmp cmp t u v) = do (t,u,v) <- simplify' (t,u,v) return $ ValueCmp cmp t u v simplify' (ElimCmp cmp t v as bs) = ElimCmp cmp <$> simplify' t <*> simplify' v <*> simplify' as <*> simplify' bs simplify' (LevelCmp cmp u v) = uncurry (LevelCmp cmp) <$> simplify' (u,v) simplify' (TypeCmp cmp a b) = uncurry (TypeCmp cmp) <$> simplify' (a,b) simplify' (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp) <$> simplify' (tela,telb) simplify' (SortCmp cmp a b) = uncurry (SortCmp cmp) <$> simplify' (a,b) simplify' (Guarded c pid) = Guarded <$> simplify' c <*> pure pid simplify' (UnBlock m) = return $ UnBlock m simplify' (FindInScope m b cands) = FindInScope m b <$> mapM simplify' cands simplify' (IsEmpty r t) = IsEmpty r <$> simplify' t simplify' (CheckSizeLtSat t) = CheckSizeLtSat <$> simplify' t instance Simplify Bool where simplify' = return -- UNUSED -- instance Simplify ConPatternInfo where -- simplify' (ConPatternInfo mr mt) = ConPatternInfo mr <$> simplify' mt -- UNUSED -- instance Simplify Pattern where -- simplify' p = case p of -- VarP _ -> return p -- LitP _ -> return p -- ConP c ci ps -> ConP c <$> simplify' ci <*> simplify' ps -- DotP v -> DotP <$> simplify' v -- ProjP _ -> return p instance Simplify DisplayForm where simplify' (Display n ps v) = Display n <$> simplify' ps <*> return v instance Simplify Candidate where simplify' (Candidate u t eti ov) = Candidate <$> simplify' u <*> simplify' t <*> pure eti <*> pure ov instance Simplify EqualityView where simplify' (OtherType t) = OtherType <$> simplify' t simplify' (EqualityType s eq l t a b) = EqualityType <$> simplify' s <*> return eq <*> mapM simplify' l <*> simplify' t <*> simplify' a <*> simplify' b --------------------------------------------------------------------------- -- * Normalisation --------------------------------------------------------------------------- class Normalise t where normalise' :: t -> ReduceM t instance Normalise Sort where normalise' s = do s <- reduce' s case s of DLub s1 s2 -> dLub <$> normalise' s1 <*> normalise' s2 Prop -> return s Type s -> levelSort <$> normalise' s Inf -> return Inf SizeUniv -> return SizeUniv instance Normalise Type where normalise' (El s t) = El <$> normalise' s <*> normalise' t instance Normalise Term where normalise' = ignoreBlocking <.> (reduceB' >=> traverse normaliseArgs) where normaliseArgs :: Term -> ReduceM Term normaliseArgs v = case v of Var n vs -> Var n <$> normalise' vs Con c ci vs -> Con c ci <$> normalise' vs Def f vs -> Def f <$> normalise' vs MetaV x vs -> MetaV x <$> normalise' vs Lit _ -> return v Level l -> levelTm <$> normalise' l Lam h b -> Lam h <$> normalise' b Sort s -> sortTm <$> normalise' s Pi a b -> uncurry Pi <$> normalise' (a,b) Shared{} -> updateSharedTerm normalise' v DontCare _ -> return v instance Normalise Elim where normalise' (Apply v) = Apply <$> normalise' v normalise' (Proj o f)= pure $ Proj o f instance Normalise Level where normalise' (Max as) = levelMax <$> normalise' as instance Normalise PlusLevel where normalise' l@ClosedLevel{} = return l normalise' (Plus n l) = Plus n <$> normalise' l instance Normalise LevelAtom where normalise' l = do l <- reduce' l case l of MetaLevel m vs -> MetaLevel m <$> normalise' vs BlockedLevel m v -> BlockedLevel m <$> normalise' v NeutralLevel r v -> NeutralLevel r <$> normalise' v UnreducedLevel{} -> __IMPOSSIBLE__ -- I hope instance (Subst t a, Normalise a) => Normalise (Abs a) where normalise' a@(Abs x _) = Abs x <$> underAbstraction_ a normalise' normalise' (NoAbs x v) = NoAbs x <$> normalise' v instance Normalise t => Normalise (Arg t) where normalise' a | isIrrelevant a = return a -- Andreas, 2012-04-02: Do not normalize irrelevant terms!? | otherwise = traverse normalise' a instance Normalise t => Normalise (Named name t) where normalise' = traverse normalise' instance Normalise t => Normalise (Dom t) where normalise' = traverse normalise' instance Normalise t => Normalise [t] where normalise' = traverse normalise' instance (Normalise a, Normalise b) => Normalise (a,b) where normalise' (x,y) = (,) <$> normalise' x <*> normalise' y instance (Normalise a, Normalise b, Normalise c) => Normalise (a,b,c) where normalise' (x,y,z) = do (x,(y,z)) <- normalise' (x,(y,z)) return (x,y,z) instance Normalise a => Normalise (Closure a) where normalise' cl = do x <- enterClosure cl normalise' return $ cl { clValue = x } instance (Subst t a, Normalise a) => Normalise (Tele a) where normalise' EmptyTel = return EmptyTel normalise' (ExtendTel a b) = uncurry ExtendTel <$> normalise' (a, b) instance Normalise ProblemConstraint where normalise' (PConstr pid c) = PConstr pid <$> normalise' c instance Normalise Constraint where normalise' (ValueCmp cmp t u v) = do (t,u,v) <- normalise' (t,u,v) return $ ValueCmp cmp t u v normalise' (ElimCmp cmp t v as bs) = ElimCmp cmp <$> normalise' t <*> normalise' v <*> normalise' as <*> normalise' bs normalise' (LevelCmp cmp u v) = uncurry (LevelCmp cmp) <$> normalise' (u,v) normalise' (TypeCmp cmp a b) = uncurry (TypeCmp cmp) <$> normalise' (a,b) normalise' (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp) <$> normalise' (tela,telb) normalise' (SortCmp cmp a b) = uncurry (SortCmp cmp) <$> normalise' (a,b) normalise' (Guarded c pid) = Guarded <$> normalise' c <*> pure pid normalise' (UnBlock m) = return $ UnBlock m normalise' (FindInScope m b cands) = FindInScope m b <$> mapM normalise' cands normalise' (IsEmpty r t) = IsEmpty r <$> normalise' t normalise' (CheckSizeLtSat t) = CheckSizeLtSat <$> normalise' t instance Normalise Bool where normalise' = return instance Normalise Int where normalise' = return instance Normalise Char where normalise' = return instance Normalise ConPatternInfo where normalise' (ConPatternInfo mr mt) = ConPatternInfo mr <$> normalise' mt instance Normalise DBPatVar where normalise' = return instance Normalise a => Normalise (Pattern' a) where normalise' p = case p of VarP x -> VarP <$> normalise' x LitP _ -> return p ConP c mt ps -> ConP c <$> normalise' mt <*> normalise' ps DotP v -> DotP <$> normalise' v AbsurdP x -> AbsurdP <$> normalise' x ProjP{} -> return p instance Normalise DisplayForm where normalise' (Display n ps v) = Display n <$> normalise' ps <*> return v instance Normalise e => Normalise (Map k e) where normalise' = traverse normalise' instance Normalise a => Normalise (Maybe a) where normalise' = traverse normalise' instance Normalise Candidate where normalise' (Candidate u t eti ov) = Candidate <$> normalise' u <*> normalise' t <*> pure eti <*> pure ov instance Normalise EqualityView where normalise' (OtherType t) = OtherType <$> normalise' t normalise' (EqualityType s eq l t a b) = EqualityType <$> normalise' s <*> return eq <*> mapM normalise' l <*> normalise' t <*> normalise' a <*> normalise' b --------------------------------------------------------------------------- -- * Full instantiation --------------------------------------------------------------------------- -- | @instantiateFull'@ 'instantiate's metas everywhere (and recursively) -- but does not 'reduce'. class InstantiateFull t where instantiateFull' :: t -> ReduceM t instance InstantiateFull Name where instantiateFull' = return instance InstantiateFull Sort where instantiateFull' s = do s <- instantiate' s case s of Type n -> levelSort <$> instantiateFull' n Prop -> return s DLub s1 s2 -> dLub <$> instantiateFull' s1 <*> instantiateFull' s2 Inf -> return s SizeUniv -> return s instance (InstantiateFull a) => InstantiateFull (Type' a) where instantiateFull' (El s t) = El <$> instantiateFull' s <*> instantiateFull' t instance InstantiateFull Term where instantiateFull' v = etaOnce =<< do -- Andreas, 2010-11-12 DONT ETA!! eta-reduction breaks subject reduction -- but removing etaOnce now breaks everything v <- instantiate' v case v of Var n vs -> Var n <$> instantiateFull' vs Con c ci vs -> Con c ci <$> instantiateFull' vs Def f vs -> Def f <$> instantiateFull' vs MetaV x vs -> MetaV x <$> instantiateFull' vs Lit _ -> return v Level l -> levelTm <$> instantiateFull' l Lam h b -> Lam h <$> instantiateFull' b Sort s -> sortTm <$> instantiateFull' s Pi a b -> uncurry Pi <$> instantiateFull' (a,b) Shared{} -> updateSharedTerm instantiateFull' v DontCare v -> dontCare <$> instantiateFull' v instance InstantiateFull Level where instantiateFull' (Max as) = levelMax <$> instantiateFull' as instance InstantiateFull PlusLevel where instantiateFull' l@ClosedLevel{} = return l instantiateFull' (Plus n l) = Plus n <$> instantiateFull' l instance InstantiateFull LevelAtom where instantiateFull' l = case l of MetaLevel m vs -> do v <- instantiateFull' (MetaV m vs) case ignoreSharing v of MetaV m vs -> return $ MetaLevel m vs _ -> return $ UnreducedLevel v NeutralLevel r v -> NeutralLevel r <$> instantiateFull' v BlockedLevel m v -> ifM (isInstantiatedMeta m) (UnreducedLevel <$> instantiateFull' v) (BlockedLevel m <$> instantiateFull' v) UnreducedLevel v -> UnreducedLevel <$> instantiateFull' v instance InstantiateFull Substitution where instantiateFull' sigma = case sigma of IdS -> return IdS EmptyS err -> return $ EmptyS err Wk n sigma -> Wk n <$> instantiateFull' sigma Lift n sigma -> Lift n <$> instantiateFull' sigma Strengthen bot sigma -> Strengthen bot <$> instantiateFull' sigma t :# sigma -> consS <$> instantiateFull' t <*> instantiateFull' sigma instance InstantiateFull Bool where instantiateFull' = return instance InstantiateFull Int where instantiateFull' = return instance InstantiateFull ConPatternInfo where instantiateFull' (ConPatternInfo mr mt) = ConPatternInfo mr <$> instantiateFull' mt instance InstantiateFull DBPatVar where instantiateFull' = return instance InstantiateFull a => InstantiateFull (Pattern' a) where instantiateFull' (VarP x) = VarP <$> instantiateFull' x instantiateFull' (DotP t) = DotP <$> instantiateFull' t instantiateFull' (AbsurdP p) = AbsurdP <$> instantiateFull' p instantiateFull' (ConP n mt ps) = ConP n <$> instantiateFull' mt <*> instantiateFull' ps instantiateFull' l@LitP{} = return l instantiateFull' p@ProjP{} = return p instance (Subst t a, InstantiateFull a) => InstantiateFull (Abs a) where instantiateFull' a@(Abs x _) = Abs x <$> underAbstraction_ a instantiateFull' instantiateFull' (NoAbs x a) = NoAbs x <$> instantiateFull' a instance InstantiateFull t => InstantiateFull (Arg t) where instantiateFull' = traverse instantiateFull' instance InstantiateFull t => InstantiateFull (Named name t) where instantiateFull' = traverse instantiateFull' instance InstantiateFull t => InstantiateFull (Dom t) where instantiateFull' = traverse instantiateFull' instance InstantiateFull t => InstantiateFull [t] where instantiateFull' = traverse instantiateFull' instance (InstantiateFull a, InstantiateFull b) => InstantiateFull (a,b) where instantiateFull' (x,y) = (,) <$> instantiateFull' x <*> instantiateFull' y instance (InstantiateFull a, InstantiateFull b, InstantiateFull c) => InstantiateFull (a,b,c) where instantiateFull' (x,y,z) = do (x,(y,z)) <- instantiateFull' (x,(y,z)) return (x,y,z) instance InstantiateFull a => InstantiateFull (Closure a) where instantiateFull' cl = do x <- enterClosure cl instantiateFull' return $ cl { clValue = x } instance InstantiateFull ProblemConstraint where instantiateFull' (PConstr p c) = PConstr p <$> instantiateFull' c instance InstantiateFull Constraint where instantiateFull' c = case c of ValueCmp cmp t u v -> do (t,u,v) <- instantiateFull' (t,u,v) return $ ValueCmp cmp t u v ElimCmp cmp t v as bs -> ElimCmp cmp <$> instantiateFull' t <*> instantiateFull' v <*> instantiateFull' as <*> instantiateFull' bs LevelCmp cmp u v -> uncurry (LevelCmp cmp) <$> instantiateFull' (u,v) TypeCmp cmp a b -> uncurry (TypeCmp cmp) <$> instantiateFull' (a,b) TelCmp a b cmp tela telb -> uncurry (TelCmp a b cmp) <$> instantiateFull' (tela,telb) SortCmp cmp a b -> uncurry (SortCmp cmp) <$> instantiateFull' (a,b) Guarded c pid -> Guarded <$> instantiateFull' c <*> pure pid UnBlock m -> return $ UnBlock m FindInScope m b cands -> FindInScope m b <$> mapM instantiateFull' cands IsEmpty r t -> IsEmpty r <$> instantiateFull' t CheckSizeLtSat t -> CheckSizeLtSat <$> instantiateFull' t instance (InstantiateFull a) => InstantiateFull (Elim' a) where instantiateFull' (Apply v) = Apply <$> instantiateFull' v instantiateFull' (Proj o f)= pure $ Proj o f instance InstantiateFull e => InstantiateFull (Map k e) where instantiateFull' = traverse instantiateFull' instance InstantiateFull e => InstantiateFull (HashMap k e) where instantiateFull' = traverse instantiateFull' instance InstantiateFull ModuleName where instantiateFull' = return instance InstantiateFull Scope where instantiateFull' = return instance InstantiateFull Signature where instantiateFull' (Sig a b c) = uncurry3 Sig <$> instantiateFull' (a, b, c) instance InstantiateFull Section where instantiateFull' (Section tel) = Section <$> instantiateFull' tel instance (Subst t a, InstantiateFull a) => InstantiateFull (Tele a) where instantiateFull' EmptyTel = return EmptyTel instantiateFull' (ExtendTel a b) = uncurry ExtendTel <$> instantiateFull' (a, b) instance InstantiateFull Char where instantiateFull' = return instance InstantiateFull Definition where instantiateFull' (Defn rel x t pol occ df i c inst copy ma inj d) = do (t, df, d) <- instantiateFull' (t, df, d) return $ Defn rel x t pol occ df i c inst copy ma inj d instance InstantiateFull NLPat where instantiateFull' (PVar x y) = return $ PVar x y instantiateFull' (PWild) = return PWild instantiateFull' (PDef x y) = PDef <$> instantiateFull' x <*> instantiateFull' y instantiateFull' (PLam x y) = PLam x <$> instantiateFull' y instantiateFull' (PPi x y) = PPi <$> instantiateFull' x <*> instantiateFull' y instantiateFull' (PBoundVar x y) = PBoundVar x <$> instantiateFull' y instantiateFull' (PTerm x) = PTerm <$> instantiateFull' x instance InstantiateFull NLPType where instantiateFull' (NLPType l a) = NLPType <$> instantiateFull' l <*> instantiateFull' a instance InstantiateFull RewriteRule where instantiateFull' (RewriteRule q gamma f ps rhs t) = RewriteRule q <$> instantiateFull' gamma <*> pure f <*> instantiateFull' ps <*> instantiateFull' rhs <*> instantiateFull' t instance InstantiateFull a => InstantiateFull (Open a) where instantiateFull' (OpenThing n a) = OpenThing n <$> instantiateFull' a instance InstantiateFull a => InstantiateFull (Local a) where instantiateFull' = traverseF instantiateFull' instance InstantiateFull DisplayForm where instantiateFull' (Display n ps v) = uncurry (Display n) <$> instantiateFull' (ps, v) instance InstantiateFull DisplayTerm where instantiateFull' (DTerm v) = DTerm <$> instantiateFull' v instantiateFull' (DDot v) = DDot <$> instantiateFull' v instantiateFull' (DCon c ci vs) = DCon c ci <$> instantiateFull' vs instantiateFull' (DDef c es) = DDef c <$> instantiateFull' es instantiateFull' (DWithApp v vs ws) = uncurry3 DWithApp <$> instantiateFull' (v, vs, ws) instance InstantiateFull Defn where instantiateFull' d = case d of Axiom{} -> return d AbstractDefn d -> AbstractDefn <$> instantiateFull' d Function{ funClauses = cs, funCompiled = cc, funInv = inv } -> do (cs, cc, inv) <- instantiateFull' (cs, cc, inv) return $ d { funClauses = cs, funCompiled = cc, funInv = inv } Datatype{ dataSort = s, dataClause = cl } -> do s <- instantiateFull' s cl <- instantiateFull' cl return $ d { dataSort = s, dataClause = cl } Record{ recClause = cl, recTel = tel } -> do cl <- instantiateFull' cl tel <- instantiateFull' tel return $ d { recClause = cl, recTel = tel } Constructor{} -> return d Primitive{ primClauses = cs } -> do cs <- instantiateFull' cs return $ d { primClauses = cs } instance InstantiateFull FunctionInverse where instantiateFull' NotInjective = return NotInjective instantiateFull' (Inverse inv) = Inverse <$> instantiateFull' inv instance InstantiateFull a => InstantiateFull (WithArity a) where instantiateFull' (WithArity n a) = WithArity n <$> instantiateFull' a instance InstantiateFull a => InstantiateFull (Case a) where instantiateFull' (Branches cop cs ls m) = Branches cop <$> instantiateFull' cs <*> instantiateFull' ls <*> instantiateFull' m instance InstantiateFull CompiledClauses where instantiateFull' Fail = return Fail instantiateFull' (Done m t) = Done m <$> instantiateFull' t instantiateFull' (Case n bs) = Case n <$> instantiateFull' bs instance InstantiateFull Clause where instantiateFull' (Clause rl rf tel ps b t catchall unreachable) = Clause rl rf <$> instantiateFull' tel <*> instantiateFull' ps <*> instantiateFull' b <*> instantiateFull' t <*> return catchall <*> return unreachable instance InstantiateFull Interface where instantiateFull' (Interface h ms mod scope inside sig display b foreignCode highlighting pragmas patsyns warnings) = Interface h ms mod scope inside <$> instantiateFull' sig <*> instantiateFull' display <*> instantiateFull' b <*> return foreignCode <*> return highlighting <*> return pragmas <*> return patsyns <*> return warnings instance InstantiateFull a => InstantiateFull (Builtin a) where instantiateFull' (Builtin t) = Builtin <$> instantiateFull' t instantiateFull' (Prim x) = Prim <$> instantiateFull' x instance InstantiateFull QName where instantiateFull' = return instance InstantiateFull a => InstantiateFull (Maybe a) where instantiateFull' = mapM instantiateFull' instance InstantiateFull Candidate where instantiateFull' (Candidate u t eti ov) = Candidate <$> instantiateFull' u <*> instantiateFull' t <*> pure eti <*> pure ov instance InstantiateFull EqualityView where instantiateFull' (OtherType t) = OtherType <$> instantiateFull' t instantiateFull' (EqualityType s eq l t a b) = EqualityType <$> instantiateFull' s <*> return eq <*> mapM instantiateFull' l <*> instantiateFull' t <*> instantiateFull' a <*> instantiateFull' b Agda-2.5.3/src/full/Agda/TypeChecking/Rewriting.hs0000644000000000000000000004234013154613124020016 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} {-# OPTIONS_GHC -fno-warn-orphans #-} -- | Rewriting with arbitrary rules. -- -- The user specifies a relation symbol by the pragma -- @ -- {-# BUILTIN REWRITE rel #-} -- @ -- where @rel@ should be of type @Δ → (lhs rhs : A) → Set i@. -- -- Then the user can add rewrite rules by the pragma -- @ -- {-# REWRITE q #-} -- @ -- where @q@ should be a closed term of type @Γ → rel us lhs rhs@. -- -- We then intend to add a rewrite rule -- @ -- Γ ⊢ lhs ↦ rhs : B -- @ -- to the signature where @B = A[us/Δ]@. -- -- To this end, we normalize @lhs@, which should be of the form -- @ -- f ts -- @ -- for a @'Def'@-symbol f (postulate, function, data, record, constructor). -- Further, @FV(ts) = dom(Γ)@. -- The rule @q :: Γ ⊢ f ts ↦ rhs : B@ is added to the signature -- to the definition of @f@. -- -- When reducing a term @Ψ ⊢ f vs@ is stuck, we try the rewrites for @f@, -- by trying to unify @vs@ with @ts@. -- This is for now done by substituting fresh metas Xs for the bound -- variables in @ts@ and checking equality with @vs@ -- @ -- Ψ ⊢ (f ts)[Xs/Γ] = f vs : B[Xs/Γ] -- @ -- If successful (no open metas/constraints), we replace @f vs@ by -- @rhs[Xs/Γ]@ and continue reducing. module Agda.TypeChecking.Rewriting where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad import Control.Monad.Reader (local, asks) import Data.Foldable ( Foldable, foldMap ) import Data.IntSet (IntSet) import qualified Data.IntSet as IntSet import qualified Data.List as List import Data.Monoid import Agda.Interaction.Options import Agda.Syntax.Common import Agda.Syntax.Internal as I import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Monad.Env import Agda.TypeChecking.EtaContract import Agda.TypeChecking.Free import Agda.TypeChecking.Free.Lazy import Agda.TypeChecking.MetaVars import Agda.TypeChecking.Conversion import qualified Agda.TypeChecking.Positivity.Occurrence as Pos import Agda.TypeChecking.Pretty import Agda.TypeChecking.Primitive ( getBuiltinName ) import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Rewriting.NonLinMatch import qualified Agda.TypeChecking.Reduce.Monad as Red import Agda.Utils.Functor import qualified Agda.Utils.HashMap as HMap import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Singleton import Agda.Utils.Size import Agda.Utils.Lens import qualified Agda.Utils.HashMap as HMap #include "undefined.h" import Agda.Utils.Impossible requireOptionRewriting :: TCM () requireOptionRewriting = unlessM (optRewriting <$> pragmaOptions) $ typeError NeedOptionRewriting -- | Check that the name given to the BUILTIN REWRITE is actually -- a relation symbol. -- I.e., its type should be of the form @Δ → (lhs : A) (rhs : B) → Set ℓ@. -- Note: we do not care about hiding/non-hiding of lhs and rhs. verifyBuiltinRewrite :: Term -> Type -> TCM () verifyBuiltinRewrite v t = do requireOptionRewriting let failure reason = typeError . GenericDocError =<< sep [ prettyTCM v <+> text " does not have the right type for a rewriting relation" , reason ] caseMaybeM (relView t) (failure $ text "because it should accept at least two arguments") $ \ (RelView tel delta a b core) -> do unless (visible a && visible b) $ failure $ text "because its two final arguments are not both visible." case ignoreSharing (unEl core) of Sort{} -> return () Con{} -> __IMPOSSIBLE__ Level{} -> __IMPOSSIBLE__ Lam{} -> __IMPOSSIBLE__ Pi{} -> __IMPOSSIBLE__ Shared{} -> __IMPOSSIBLE__ _ -> failure $ text "because its type does not end in a sort, but in " <+> do inTopContext $ addContext tel $ prettyTCM core -- | Deconstructing a type into @Δ → t → t' → core@. data RelView = RelView { relViewTel :: Telescope -- ^ The whole telescope @Δ, t, t'@. , relViewDelta :: ListTel -- ^ @Δ@. , relViewType :: Dom Type -- ^ @t@. , relViewType' :: Dom Type -- ^ @t'@. , relViewCore :: Type -- ^ @core@. } -- | Deconstructing a type into @Δ → t → t' → core@. -- Returns @Nothing@ if not enough argument types. relView :: Type -> TCM (Maybe RelView) relView t = do TelV tel core <- telView t let n = size tel (delta, lastTwo) = splitAt (n - 2) $ telToList tel if size lastTwo < 2 then return Nothing else do let [a, b] = fmap snd <$> lastTwo return $ Just $ RelView tel delta a b core -- | Add @q : Γ → rel us lhs rhs@ as rewrite rule -- @ -- Γ ⊢ lhs ↦ rhs : B -- @ -- to the signature where @B = A[us/Δ]@. -- Remember that @rel : Δ → A → A → Set i@, so -- @rel us : (lhs rhs : A[us/Δ]) → Set i@. addRewriteRule :: QName -> TCM () addRewriteRule q = do requireOptionRewriting let failNoBuiltin = typeError $ GenericError $ "Cannot add rewrite rule without prior BUILTIN REWRITE" rel <- fromMaybeM failNoBuiltin $ getBuiltinName builtinRewrite def <- instantiateDef =<< getConstInfo q -- Issue 1651: Check that we are not adding a rewrite rule -- for a type signature whose body has not been type-checked yet. when (isEmptyFunction $ theDef def) $ typeError . GenericDocError =<< hsep [ text "Rewrite rule from function " , prettyTCM q , text " cannot be added before the function definition" ] -- We know that the type of rel is that of a relation. relV <- relView =<< do defType <$> getConstInfo rel let RelView _tel delta a _a' _core = -- line break for CPP fromMaybe __IMPOSSIBLE__ relV reportSDoc "rewriting" 30 $ do text "rewrite relation at type " <+> do inTopContext $ prettyTCM (telFromList delta) <+> text " |- " <+> do addContext delta $ prettyTCM a -- Get rewrite rule (type of q). TelV gamma1 core <- telView $ defType def reportSDoc "rewriting" 30 $ do text "attempting to add rewrite rule of type " <+> do prettyTCM gamma1 <+> text " |- " <+> do addContext gamma1 $ prettyTCM core let failureWrongTarget = typeError . GenericDocError =<< hsep [ prettyTCM q , text " does not target rewrite relation" ] let failureMetas = typeError . GenericDocError =<< hsep [ prettyTCM q , text " is not a legal rewrite rule, since it contains unsolved meta variables" ] let failureNotDefOrCon = typeError . GenericDocError =<< hsep [ prettyTCM q , text " is not a legal rewrite rule, since the left-hand side is neither a defined symbol nor a constructor" ] let failureFreeVars xs = typeError . GenericDocError =<< hsep [ prettyTCM q , text " is not a legal rewrite rule, since the following variables are not bound by the left hand side: " , prettyList_ (map (prettyTCM . var) $ IntSet.toList xs) ] let failureIllegalRule = typeError . GenericDocError =<< hsep [ prettyTCM q , text " is not a legal rewrite rule" ] -- Check that type of q targets rel. case ignoreSharing $ unEl core of Def rel' es@(_:_:_) | rel == rel' -> do -- Because of the type of rel (Γ → sort), all es are applications. let vs = map unArg $ fromMaybe __IMPOSSIBLE__ $ allApplyElims es -- The last two arguments are lhs and rhs. n = size vs (us, [lhs, rhs]) = splitAt (n - 2) vs unless (size delta == size us) __IMPOSSIBLE__ b <- instantiateFull $ applySubst (parallelS $ reverse us) a gamma0 <- getContextTelescope gamma1 <- instantiateFull gamma1 let gamma = gamma0 `abstract` gamma1 unless (null $ allMetas (telToList gamma1)) $ do reportSDoc "rewriting" 30 $ text "metas in gamma1: " <+> text (show $ allMetas $ telToList gamma1) failureMetas -- 2017-06-18, Jesper: Unfold inlined definitions on the LHS. -- This is necessary to replace copies created by imports by their -- original definition. lhs <- modifyAllowedReductions (const [InlineReductions]) $ normalise lhs -- Find head symbol f of the lhs and its arguments. (f , hd , es) <- case ignoreSharing lhs of Def f es -> return (f , Def f , es) Con c ci vs -> do let hd = Con c ci . fromMaybe __IMPOSSIBLE__ . allApplyElims return (conName c , hd , map Apply vs) _ -> failureNotDefOrCon rew <- addContext gamma1 $ do -- Normalize lhs args: we do not want to match redexes. es <- etaContract =<< normalise es checkNoLhsReduction f (hd es) -- Normalize rhs: might be more efficient. rhs <- etaContract =<< normalise rhs unless (null $ allMetas (es, rhs, b)) $ do reportSDoc "rewriting" 30 $ text "metas in lhs: " <+> text (show $ allMetas es) reportSDoc "rewriting" 30 $ text "metas in rhs: " <+> text (show $ allMetas rhs) reportSDoc "rewriting" 30 $ text "metas in b : " <+> text (show $ allMetas b) failureMetas ps <- patternFrom Relevant 0 es reportSDoc "rewriting" 30 $ text "Pattern generated from lhs: " <+> prettyTCM (PDef f ps) -- check that FV(rhs) ⊆ nlPatVars(lhs) let freeVars = usedArgs (defArgOccurrences def) `IntSet.union` allFreeVars (ps,rhs) boundVars = nlPatVars ps reportSDoc "rewriting" 40 $ text "variables bound by the pattern: " <+> text (show boundVars) reportSDoc "rewriting" 40 $ text "variables free in the rewrite rule: " <+> text (show freeVars) unlessNull (freeVars IntSet.\\ boundVars) failureFreeVars return $ RewriteRule q gamma f ps rhs (unDom b) reportSDoc "rewriting" 10 $ text "considering rewrite rule " <+> prettyTCM rew reportSDoc "rewriting" 60 $ text "considering rewrite rule" <+> text (show rew) -- NO LONGER WORKS: -- -- Check whether lhs can be rewritten with itself. -- -- Otherwise, there are unbound variables in either gamma or rhs. -- addContext gamma $ -- unlessM (isJust <$> runReduceM (rewriteWith (Just b) lhs rew)) $ -- failureFreeVars -- Add rewrite rule gamma ⊢ lhs ↦ rhs : b for f. addRewriteRules f [rew] _ -> failureWrongTarget where checkNoLhsReduction :: QName -> Term -> TCM () checkNoLhsReduction f v = do v' <- normalise v unless (v == v') $ do reportSDoc "rewriting" 20 $ text "v = " <+> text (show v) reportSDoc "rewriting" 20 $ text "v' = " <+> text (show v') -- Andreas, 2016-06-01, issue 1997 -- A reason for a reduction of the lhs could be that -- the rewrite rule has already been added. -- In this case, we want a nicer error message. checkNotAlreadyAdded f typeError . GenericDocError =<< fsep [ prettyTCM q <+> text " is not a legal rewrite rule, since the left-hand side " , prettyTCM v <+> text " reduces to " <+> prettyTCM v' ] checkNotAlreadyAdded :: QName -> TCM () checkNotAlreadyAdded f = do rews <- getRewriteRulesFor f -- check if q is already an added rewrite rule when (any ((q ==) . rewName) rews) $ typeError . GenericDocError =<< do text "Rewrite rule " <+> prettyTCM q <+> text " has already been added" usedArgs :: [Pos.Occurrence] -> IntSet usedArgs occs = IntSet.fromList $ map snd $ usedIxs where allIxs = zip occs $ downFrom $ size occs usedIxs = filter (used . fst) allIxs used Pos.Unused = False used _ = True -- | Append rewrite rules to a definition. addRewriteRules :: QName -> RewriteRules -> TCM () addRewriteRules f rews = do reportSDoc "rewriting" 10 $ text "rewrite rule ok, adding it to the definition of " <+> prettyTCM f let matchables = getMatchables rews reportSDoc "rewriting" 30 $ text "matchable symbols: " <+> prettyTCM matchables modifySignature $ addRewriteRulesFor f rews matchables --rules <- getRewriteRulesFor f --reportSDoc "rewriting" 20 $ vcat -- [ text "rewrite rules for " <+> prettyTCM f <+> text ":" -- , vcat (map prettyTCM rules) -- ] -- | @rewriteWith t f es rew@ -- tries to rewrite @f es : t@ with @rew@, returning the reduct if successful. rewriteWith :: Maybe Type -> Term -> RewriteRule -> Elims -> ReduceM (Either (Blocked Term) Term) rewriteWith mt v rew@(RewriteRule q gamma _ ps rhs b) es = do traceSDoc "rewriting" 75 (sep [ text "attempting to rewrite term " <+> prettyTCM (v `applyE` es) , text " with rule " <+> prettyTCM rew ]) $ do result <- nonLinMatch gamma ps es case result of Left block -> return $ Left $ block $> v `applyE` es -- TODO: remember reductions Right sub -> do let v' = applySubst sub rhs traceSDoc "rewriting" 70 (sep [ text "rewrote " <+> prettyTCM (v `applyE` es) , text " to " <+> prettyTCM v' ]) $ do return $ Right v' {- OLD CODE: -- Freeze all metas, remember which one where not frozen before. -- This ensures that we do not instantiate metas while matching -- on the rewrite lhs. ms <- freezeMetas res <- tryConversion' $ do -- Create new metas for the lhs variables of the rewriting rule. xs <- newTelMeta gamma let sigma = parallelS $ map unArg xs (lhs', rhs', b') = applySubst sigma (lhs, rhs, b) -- Unify type and term with type and lhs of rewrite rule. whenJust mt $ \ t -> leqType t b' local (\ e -> e {envCompareBlocked = True}) $ equalTerm b' lhs' v -- Check that all variables have been solved for. unlessM (isInstantiatedMeta xs) $ do reportSDoc "rewriting" 20 $ text "lhs variables solved with: " <+> do sep $ map prettyTCM xs -- The following error is caught immediately by tryConversion. typeError $ GenericError $ "free variables not bound by left hand side" return rhs' -- Thaw metas that were frozen by a call to this function. unfreezeMetas' (`elem` ms) return res-} -- | @rewrite b v rules es@ tries to rewrite @v@ applied to @es@ with the -- rewrite rules @rules@. @b@ is the default blocking tag. rewrite :: Blocked_ -> Term -> RewriteRules -> Elims -> ReduceM (Reduced (Blocked Term) Term) rewrite block v rules es = do rewritingAllowed <- optRewriting <$> pragmaOptions if (rewritingAllowed && not (null rules)) then loop block rules =<< instantiateFull' es else return $ NoReduction (block $> v `applyE` es) where loop :: Blocked_ -> RewriteRules -> Elims -> ReduceM (Reduced (Blocked Term) Term) loop block [] es = return $ NoReduction $ block $> v `applyE` es loop block (rew:rews) es | let n = rewArity rew, length es >= n = do let (es1, es2) = List.genericSplitAt n es result <- rewriteWith Nothing v rew es1 case result of Left (Blocked m u) -> loop (block `mappend` Blocked m ()) rews es Left (NotBlocked _ _) -> loop block rews es Right w -> return $ YesReduction YesSimplification $ w `applyE` es2 | otherwise = loop (block `mappend` NotBlocked Underapplied ()) rews es ------------------------------------------------------------------------ -- * Auxiliary functions ------------------------------------------------------------------------ class NLPatVars a where nlPatVarsUnder :: Int -> a -> IntSet nlPatVars :: a -> IntSet nlPatVars = nlPatVarsUnder 0 instance (Foldable f, NLPatVars a) => NLPatVars (f a) where nlPatVarsUnder k = foldMap $ nlPatVarsUnder k instance NLPatVars NLPType where nlPatVarsUnder k (NLPType l a) = nlPatVarsUnder k l `IntSet.union` nlPatVarsUnder k a instance NLPatVars NLPat where nlPatVarsUnder k p = case p of PVar i _ -> singleton $ i - k PDef _ es -> nlPatVarsUnder k es PWild -> empty PLam _ p' -> nlPatVarsUnder (k+1) $ unAbs p' PPi a b -> nlPatVarsUnder k a `IntSet.union` nlPatVarsUnder (k+1) (unAbs b) PBoundVar _ es -> nlPatVarsUnder k es PTerm{} -> empty rewArity :: RewriteRule -> Int rewArity = length . rewPats -- | Get all symbols that a rewrite rule matches against class GetMatchables a where getMatchables :: a -> [QName] instance (Foldable f, GetMatchables a) => GetMatchables (f a) where getMatchables = foldMap getMatchables instance GetMatchables NLPat where getMatchables p = case p of PVar _ _ -> empty PWild -> empty PDef f _ -> singleton f PLam _ x -> empty PPi a b -> empty PBoundVar i es -> empty PTerm _ -> empty -- should be safe (I hope) instance GetMatchables RewriteRule where getMatchables = getMatchables . rewPats -- Only computes free variables that are not bound (i.e. those in a PTerm) instance Free NLPat where freeVars' p = case p of PVar _ _ -> mempty PWild -> mempty PDef _ es -> freeVars' es PLam _ u -> freeVars' u PPi a b -> freeVars' (a,b) PBoundVar _ es -> freeVars' es PTerm t -> freeVars' t instance Free NLPType where freeVars' (NLPType l a) = ifM ((IgnoreNot ==) <$> asks feIgnoreSorts) {- then -} (freeVars' (l, a)) {- else -} (freeVars' a) Agda-2.5.3/src/full/Agda/TypeChecking/Positivity.hs0000644000000000000000000007625113154613124020237 0ustar0000000000000000{-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} {-# LANGUAGE UndecidableInstances #-} -- | Check that a datatype is strictly positive. module Agda.TypeChecking.Positivity where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.DeepSeq import Control.Monad.Reader import Control.Monad.State (get) import Data.Either import qualified Data.Foldable as Fold import Data.Function import Data.Graph (SCC(..), flattenSCC) import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import qualified Data.List as List import Data.Map (Map) import qualified Data.Map as Map import Data.Monoid (mconcat) import qualified Data.Sequence as DS import Data.Set (Set) import qualified Data.Set as Set import Debug.Trace import Agda.Syntax.Common import qualified Agda.Syntax.Info as Info import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.Syntax.Position (fuseRange, Range, HasRange(..), noRange) import Agda.TypeChecking.Datatypes ( isDataOrRecordType ) import Agda.TypeChecking.Functions import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin (primInf, CoinductionKit(..), coinductionKit) import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Pretty import Agda.TypeChecking.Records import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Warnings import qualified Agda.Utils.Graph.AdjacencyMap.Unidirectional as Graph import Agda.Utils.Function (applyUnless) import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import qualified Agda.Utils.Permutation as Perm import qualified Agda.Utils.Pretty as P import Agda.Utils.Pretty (Pretty, prettyShow) import Agda.Utils.SemiRing import Agda.Utils.Singleton import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible type Graph n e = Graph.Graph n n e -- | Check that the datatypes in the mutual block containing the given -- declarations are strictly positive. -- -- Also add information about positivity and recursivity of records -- to the signature. checkStrictlyPositive :: Info.MutualInfo -> Set QName -> TCM () checkStrictlyPositive mi qset = disableDestructiveUpdate $ do -- compute the occurrence graph for qs let qs = Set.toList qset reportSDoc "tc.pos.tick" 100 $ text "positivity of" <+> prettyTCM qs g <- buildOccurrenceGraph qset let (gstar, sccs) = Graph.gaussJordanFloydWarshallMcNaughtonYamada $ fmap occ g reportSDoc "tc.pos.tick" 100 $ text "constructed graph" reportSLn "tc.pos.graph" 5 $ "Positivity graph: N=" ++ show (size $ Graph.nodes g) ++ " E=" ++ show (length $ Graph.edges g) reportSDoc "tc.pos.graph" 10 $ vcat [ text "positivity graph for" <+> (fsep $ map prettyTCM qs) , nest 2 $ prettyTCM g ] reportSLn "tc.pos.graph" 5 $ "Positivity graph (completed): E=" ++ show (length $ Graph.edges gstar) reportSDoc "tc.pos.graph" 50 $ vcat [ text "transitive closure of positivity graph for" <+> prettyTCM qs , nest 2 $ prettyTCM gstar ] -- remember argument occurrences for qs in the signature setArgOccs qset qs gstar reportSDoc "tc.pos.tick" 100 $ text "set args" -- check positivity for all strongly connected components of the graph for qs reportSDoc "tc.pos.graph.sccs" 10 $ do let (triv, others) = partitionEithers $ for sccs $ \case AcyclicSCC v -> Left v CyclicSCC vs -> Right vs sep [ text $ show (length triv) ++ " trivial sccs" , text $ show (length others) ++ " non-trivial sccs with lengths " ++ show (map length others) ] reportSLn "tc.pos.graph.sccs" 15 $ " sccs = " ++ prettyShow [ scc | CyclicSCC scc <- sccs ] forM_ sccs $ \case -- If the mutuality information has never been set, we set it to [] AcyclicSCC (DefNode q) -> whenM (isNothing <$> getMutual q) $ do reportSLn "tc.pos.mutual" 10 $ "setting " ++ prettyShow q ++ " to non-recursive" -- Andreas, 2017-04-26, issue #2555 -- We should not have @DefNode@s pointing outside our formal mutual block. unless (Set.member q qset) __IMPOSSIBLE__ setMutual q [] AcyclicSCC (ArgNode{}) -> return () CyclicSCC scc -> setMut [ q | DefNode q <- scc ] mapM_ (checkPos g gstar) qs reportSDoc "tc.pos.tick" 100 $ text "checked positivity" where checkPos :: Graph Node Edge -> Graph Node Occurrence -> QName -> TCM () checkPos g gstar q = inConcreteOrAbstractMode q $ \ _def -> do -- we check positivity only for data or record definitions whenJustM (isDatatype q) $ \ dr -> do reportSDoc "tc.pos.check" 10 $ text "Checking positivity of" <+> prettyTCM q let loop :: Maybe Occurrence loop = Graph.lookup (DefNode q) (DefNode q) gstar -- Note the property -- Agda.Utils.Graph.AdjacencyMap.Unidirectional.Tests.prop_productOfEdgesInBoundedWalk, -- which relates productOfEdgesInBoundedWalk to -- gaussJordanFloydWarshallMcNaughtonYamada. reason bound = case productOfEdgesInBoundedWalk occ g (DefNode q) (DefNode q) bound of Just (Edge _ how) -> how Nothing -> __IMPOSSIBLE__ how :: String -> Occurrence -> TCM Doc how msg bound = fsep $ [prettyTCM q] ++ pwords "is" ++ pwords (msg ++ ", because it occurs") ++ [prettyTCM (reason bound)] -- if we have a negative loop, raise error -- ASR (23 December 2015). We don't raise a strictly positive -- error if the NO_POSITIVITY_CHECK pragma was set on in the -- mutual block. See Issue 1614. when (Info.mutualPositivityCheck mi) $ whenM positivityCheckEnabled $ case loop of Just o | o <= JustPos -> warning $ NotStrictlyPositive q (reason JustPos) _ -> return () -- if we find an unguarded record, mark it as such when (dr == IsRecord) $ case loop of Just o | o <= StrictPos -> do reportSDoc "tc.pos.record" 5 $ how "not guarded" StrictPos unguardedRecord q checkInduction q -- otherwise, if the record is recursive, mark it as well Just o | o <= GuardPos -> do reportSDoc "tc.pos.record" 5 $ how "recursive" GuardPos recursiveRecord q checkInduction q -- If the record is not recursive, switch on eta -- unless it is coinductive or a no-eta-equality record. Nothing -> do reportSDoc "tc.pos.record" 10 $ text "record type " <+> prettyTCM q <+> text "is not recursive" nonRecursiveRecord q _ -> return () checkInduction :: QName -> TCM () checkInduction q = -- ASR (01 January 2016). We don't raise this error if the -- NO_POSITIVITY_CHECK pragma was set on in the record. See -- Issue 1760. when (Info.mutualPositivityCheck mi) $ whenM positivityCheckEnabled $ do -- Check whether the recursive record has been declared as -- 'Inductive' or 'Coinductive'. Otherwise, error. unlessM (isJust . recInduction . theDef <$> getConstInfo q) $ setCurrentRange (nameBindingSite $ qnameName q) $ typeError . GenericDocError =<< text "Recursive record" <+> prettyTCM q <+> text "needs to be declared as either inductive or coinductive" occ (Edge o _) = o isDatatype :: QName -> TCM (Maybe DataOrRecord) isDatatype q = do def <- theDef <$> getConstInfo q return $ case def of Datatype{dataClause = Nothing} -> Just IsData Record {recClause = Nothing} -> Just IsRecord _ -> Nothing -- Set the mutually recursive identifiers for a SCC. setMut :: [QName] -> TCM () setMut [] = return () -- nothing to do setMut qs = forM_ qs $ \ q -> do reportSLn "tc.pos.mutual" 10 $ "setting " ++ prettyShow q ++ " to (mutually) recursive" setMutual q qs -- TODO: The previous line produces data of quadratic size -- (which has to be processed upon serialization). Presumably qs is -- usually short, but in some cases (for instance for generated -- code) it may be long. Wouldn't it be better to assign a -- unique identifier to each SCC, and avoid storing lists? -- Set the polarity of the arguments to a couple of definitions setArgOccs :: Set QName -> [QName] -> Graph Node Occurrence -> TCM () setArgOccs qset qs g = do -- Compute a map from each name in q to the maximal argument index let maxs = Map.fromListWith max [ (q, i) | ArgNode q i <- Set.toList $ Graph.sourceNodes g, q `Set.member` qset ] forM_ qs $ \ q -> inConcreteOrAbstractMode q $ \ def -> do reportSDoc "tc.pos.args" 10 $ text "checking args of" <+> prettyTCM q n <- getDefArity def -- If there is no outgoing edge @ArgNode q i@, all @n@ arguments are @Unused@. -- Otherwise, we obtain the occurrences from the Graph. let findOcc i = fromMaybe Unused $ Graph.lookup (ArgNode q i) (DefNode q) g args = caseMaybe (Map.lookup q maxs) (replicate n Unused) $ \ m -> map findOcc [0 .. max m (n - 1)] reportSDoc "tc.pos.args" 10 $ sep [ text "args of" <+> prettyTCM q <+> text "=" , nest 2 $ prettyList $ map prettyTCM args ] -- The list args can take a long time to compute, but contains -- small elements, and is stored in the interface (right?), so -- it is computed deep-strictly. setArgOccurrences q $!! args getDefArity :: Definition -> TCM Int getDefArity def = case theDef def of defn@Function{} -> do let dropped = projectionArgs defn -- TODO: instantiateFull followed by arity could perhaps be -- optimised, presumably the instantiation can be performed -- lazily. subtract dropped . arity <$> instantiateFull (defType def) Datatype{ dataPars = n } -> return n Record{ recPars = n } -> return n _ -> return 0 -- Operations on occurrences ------------------------------------------- -- See also Agda.TypeChecking.Positivity.Occurrence. (>*<) :: OccursWhere -> OccursWhere -> OccursWhere Unknown >*< _ = Unknown Known _ _ >*< Unknown = Unknown Known r1 os1 >*< Known r2 os2 = Known (fuseRange r1 r2) (os1 DS.>< os2) instance PrettyTCM OccursWhere where prettyTCM o = prettyOs $ map maxOneLeftOfArrow $ uniq $ splitOnDef o where nth 0 = pwords "first" nth 1 = pwords "second" nth 2 = pwords "third" nth n = pwords $ show (n + 1) ++ "th" -- remove consecutive duplicates uniq = map head . List.group prettyOs [] = __IMPOSSIBLE__ prettyOs [o] = prettyO o <> text "." prettyOs (o:os) = prettyO o <> text ", which occurs" $$ prettyOs os prettyO Unknown = empty prettyO (Known _ ws) = Fold.foldrM (\w d -> return d $$ fsep (prettyW w)) empty ws prettyW w = case w of LeftOfArrow -> pwords "to the left of an arrow" DefArg q i -> pwords "in the" ++ nth i ++ pwords "argument to" ++ [prettyTCM q] UnderInf -> pwords "under" ++ [do -- this cannot fail if an 'UnderInf' has been generated Def inf _ <- ignoreSharing <$> primInf prettyTCM inf] VarArg -> pwords "in an argument to a bound variable" MetaArg -> pwords "in an argument to a metavariable" ConArgType c -> pwords "in the type of the constructor" ++ [prettyTCM c] IndArgType c -> pwords "in an index of the target type of the constructor" ++ [prettyTCM c] InClause i -> pwords "in the" ++ nth i ++ pwords "clause" Matched -> pwords "as matched against" InDefOf d -> pwords "in the definition of" ++ [prettyTCM d] maxOneLeftOfArrow Unknown = Unknown maxOneLeftOfArrow (Known r ws) = Known r $ noArrows DS.>< case DS.viewl startsWithArrow of DS.EmptyL -> DS.empty w DS.:< ws -> w DS.<| DS.filter (not . isArrow) ws where (noArrows, startsWithArrow) = DS.breakl isArrow ws isArrow LeftOfArrow{} = True isArrow _ = False splitOnDef Unknown = [Unknown] splitOnDef (Known r ws) = split ws DS.empty where split ws acc = case DS.viewl ws of w@InDefOf{} DS.:< ws -> let rest = split ws (DS.singleton w) in if DS.null acc then rest else Known r acc : rest w DS.:< ws -> split ws (acc DS.|> w) DS.EmptyL -> [Known r acc] instance Sized OccursWhere where size Unknown = 1 size (Known _ ws) = 1 + size ws -- Computing occurrences -------------------------------------------------- data Item = AnArg Nat | ADef QName deriving (Eq, Ord, Show) instance HasRange Item where getRange (AnArg _) = noRange getRange (ADef qn) = getRange qn type Occurrences = Map Item [OccursWhere] -- | Used to build 'Occurrences' and occurrence graphs. data OccurrencesBuilder = Concat [OccurrencesBuilder] | OccursAs Where OccurrencesBuilder | OccursHere Item | OnlyVarsUpTo Nat OccurrencesBuilder -- ^ @OnlyVarsUpTo n occs@ discards occurrences of de Bruijn index -- @>= n@. -- | Used to build 'Occurrences' and occurrence graphs. data OccurrencesBuilder' = Concat' [OccurrencesBuilder'] | OccursAs' Where OccurrencesBuilder' | OccursHere' Item OccursWhere emptyOB :: OccurrencesBuilder emptyOB = Concat [] (>+<) :: OccurrencesBuilder -> OccurrencesBuilder -> OccurrencesBuilder occs1 >+< occs2 = Concat [occs1, occs2] -- | Removes 'OnlyVarsUpTo' entries and adds 'OccursWhere' entries. -- -- WARNING: There can be lots of sharing between the generated -- 'OccursWhere' entries. Traversing all of these entries could be -- expensive. (See 'computeEdges' for an example.) preprocess :: OccurrencesBuilder -> OccurrencesBuilder' preprocess ob = case pp Nothing DS.empty ob of Nothing -> Concat' [] Just ob -> ob where pp :: Maybe Nat -- ^ Variables larger than or equal to this number, if any, -- are not retained. -> DS.Seq Where -> OccurrencesBuilder -> Maybe OccurrencesBuilder' pp !m ws (Concat obs) = case catMaybes $ map (pp m ws) obs of [] -> Nothing obs -> return (Concat' obs) pp m ws (OccursAs w ob) = OccursAs' w <$> pp m (ws DS.|> w) ob pp m ws (OnlyVarsUpTo n ob) = pp (Just $! maybe n (min n) m) ws ob pp m ws (OccursHere i) = do guard keep return (OccursHere' i (Known (getRange i) ws)) where keep = case (m, i) of (Nothing, _) -> True (_, ADef _) -> True (Just m, AnArg i) -> i < m -- | A type used locally in 'flatten'. data OccursWheres = OccursWheres :++ OccursWheres | Occurs OccursWhere -- | An interpreter for 'OccurrencesBuilder'. -- -- WARNING: There can be lots of sharing between the generated -- 'OccursWhere' entries. Traversing all of these entries could be -- expensive. (See 'computeEdges' for an example.) flatten :: OccurrencesBuilder -> Occurrences flatten = fmap (flip flatten'' []) . Map.fromListWith (:++) . flip flatten' [] . preprocess where flatten' :: OccurrencesBuilder' -> [(Item, OccursWheres)] -> [(Item, OccursWheres)] flatten' (Concat' obs) = foldr (\occs f -> flatten' occs . f) id obs flatten' (OccursAs' _ ob) = flatten' ob flatten' (OccursHere' i o) = ((i, Occurs o) :) flatten'' (os1 :++ os2) = flatten'' os1 . flatten'' os2 flatten'' (Occurs o) = (o :) -- | Context for computing occurrences. data OccEnv = OccEnv { vars :: [Maybe Item] -- ^ Items corresponding to the free variables. -- -- Potential invariant: It seems as if the list has the form -- @'genericReplicate' n 'Nothing' ++ 'map' ('Just' . 'AnArg') is@, -- for some @n@ and @is@, where @is@ is decreasing -- (non-strictly). , inf :: Maybe QName -- ^ Name for ∞ builtin. } -- | Monad for computing occurrences. type OccM = Reader OccEnv withExtendedOccEnv :: Maybe Item -> OccM a -> OccM a withExtendedOccEnv i = withExtendedOccEnv' [i] withExtendedOccEnv' :: [Maybe Item] -> OccM a -> OccM a withExtendedOccEnv' is = local $ \ e -> e { vars = is ++ vars e } -- | Running the monad getOccurrences :: (Show a, PrettyTCM a, ComputeOccurrences a) => [Maybe Item] -> a -> TCM OccurrencesBuilder getOccurrences vars a = do reportSDoc "tc.pos.occ" 70 $ text "computing occurrences in " <+> text (show a) reportSDoc "tc.pos.occ" 20 $ text "computing occurrences in " <+> prettyTCM a kit <- coinductionKit return $ runReader (occurrences a) $ OccEnv vars $ fmap nameOfInf kit class ComputeOccurrences a where occurrences :: a -> OccM OccurrencesBuilder instance ComputeOccurrences Clause where occurrences cl = do let ps = namedClausePats cl items = IntMap.elems $ patItems ps -- sorted from low to high DBI (Concat (mapMaybe matching (zip [0..] ps)) >+<) <$> withExtendedOccEnv' items (occurrences $ clauseBody cl) where matching (i, p) | properlyMatching (namedThing $ unArg p) = Just $ OccursAs Matched $ OccursHere $ AnArg i | otherwise = Nothing -- @patItems ps@ creates a map from the pattern variables of @ps@ -- to the index of the argument they are bound in. patItems ps = mconcat $ zipWith patItem [0..] ps -- @patItem i p@ assigns index @i@ to each pattern variable in @p@ patItem :: Int -> NamedArg DeBruijnPattern -> IntMap (Maybe Item) patItem i p = Fold.foldMap makeEntry ixs where ixs = map dbPatVarIndex $ lefts $ map unArg $ patternVars $ namedThing <$> p makeEntry x = singleton (x, Just $ AnArg i) instance ComputeOccurrences Term where occurrences v = case unSpine v of Var i args -> do vars <- asks vars occs <- occurrences args -- Apparently some development version of GHC chokes if the -- following line is replaced by vars ! i. let mi | i < length vars = vars !! i | otherwise = flip trace __IMPOSSIBLE__ $ "impossible: occurrence of de Bruijn index " ++ show i ++ " in vars " ++ show vars ++ " is unbound" return $ maybe emptyOB OccursHere mi >+< OccursAs VarArg occs Def d args -> do inf <- asks inf let occsAs = if Just d /= inf then OccursAs . DefArg d else \ n -> -- the principal argument of builtin INF (∞) is the second (n==1) -- the first is a level argument (n==0, counting from 0!) if n == 1 then OccursAs UnderInf else OccursAs (DefArg d n) occs <- mapM occurrences args return $ OccursHere (ADef d) >+< Concat (zipWith occsAs [0..] occs) Con _ _ args -> occurrences args MetaV _ args -> OccursAs MetaArg <$> occurrences args Pi a b -> do oa <- occurrences a ob <- occurrences b return $ OccursAs LeftOfArrow oa >+< ob Lam _ b -> occurrences b Level l -> occurrences l Lit{} -> return emptyOB Sort{} -> return emptyOB DontCare _ -> return emptyOB -- Andreas, 2011-09-09: do we need to check for negative occurrences in irrelevant positions? Shared p -> occurrences $ derefPtr p instance ComputeOccurrences Level where occurrences (Max as) = occurrences as instance ComputeOccurrences PlusLevel where occurrences ClosedLevel{} = return emptyOB occurrences (Plus _ l) = occurrences l instance ComputeOccurrences LevelAtom where occurrences l = case l of MetaLevel x es -> occurrences $ MetaV x es -- Andreas, 2016-07-25, issue 2108 -- NOT: OccursAs MetaArg <$> occurrences vs -- since we need to unSpine! -- (Otherwise, we run into __IMPOSSIBLE__ at Proj elims) BlockedLevel _ v -> occurrences v NeutralLevel _ v -> occurrences v UnreducedLevel v -> occurrences v instance ComputeOccurrences Type where occurrences (El _ v) = occurrences v instance ComputeOccurrences a => ComputeOccurrences (Tele a) where occurrences EmptyTel = return emptyOB occurrences (ExtendTel a b) = occurrences (a, b) instance ComputeOccurrences a => ComputeOccurrences (Abs a) where occurrences (Abs _ b) = withExtendedOccEnv Nothing $ occurrences b occurrences (NoAbs _ b) = occurrences b instance ComputeOccurrences a => ComputeOccurrences (Elim' a) where occurrences Proj{} = __IMPOSSIBLE__ occurrences (Apply a) = occurrences a instance ComputeOccurrences a => ComputeOccurrences (Arg a) where occurrences = occurrences . unArg instance ComputeOccurrences a => ComputeOccurrences (Dom a) where occurrences = occurrences . unDom instance ComputeOccurrences a => ComputeOccurrences [a] where occurrences vs = Concat <$> mapM occurrences vs instance ComputeOccurrences a => ComputeOccurrences (Maybe a) where occurrences (Just v) = occurrences v occurrences Nothing = return emptyOB instance (ComputeOccurrences a, ComputeOccurrences b) => ComputeOccurrences (a, b) where occurrences (x, y) = do ox <- occurrences x oy <- occurrences y return $ ox >+< oy -- | Computes the occurrences in the given definition. -- -- WARNING: There can be lots of sharing between the 'OccursWhere' -- entries. Traversing all of these entries could be expensive. (See -- 'computeEdges' for an example.) computeOccurrences :: QName -> TCM Occurrences computeOccurrences q = flatten <$> computeOccurrences' q -- | Computes the occurrences in the given definition. computeOccurrences' :: QName -> TCM OccurrencesBuilder computeOccurrences' q = inConcreteOrAbstractMode q $ \ def -> do reportSDoc "tc.pos" 25 $ do let a = defAbstract def m <- asks envAbstractMode cur <- asks envCurrentModule text "computeOccurrences" <+> prettyTCM q <+> text (show a) <+> text (show m) <+> prettyTCM cur OccursAs (InDefOf q) <$> case theDef def of Function{funClauses = cs} -> do cs <- mapM etaExpandClause =<< instantiateFull cs Concat . zipWith (OccursAs . InClause) [0..] <$> mapM (getOccurrences []) cs Datatype{dataClause = Just c} -> getOccurrences [] =<< instantiateFull c Datatype{dataPars = np0, dataCons = cs} -> do -- Andreas, 2013-02-27: first, each data index occurs as matched on. TelV tel t <- telView $ defType def -- Andreas, 2017-04-26, issue #2554: count first index as parameter if it has type Size. -- We compute sizeIndex=1 if first first index has type Size, otherwise sizeIndex==0 sizeIndex <- caseMaybe (headMaybe $ drop np0 $ telToList tel) (return 0) $ \ dom -> do caseMaybeM (isSizeType dom) (return 0) $ \ _ -> return 1 let np = np0 + sizeIndex let xs = [np .. size tel - 1] -- argument positions corresponding to indices ioccs = Concat $ map (OccursHere . AnArg) [np0 .. np - 1] ++ map (OccursAs Matched . OccursHere . AnArg) xs -- Then, we compute the occurrences in the constructor types. let conOcc c = do a <- defType <$> getConstInfo c TelV tel t <- telView' <$> normalise a -- normalization needed e.g. for test/succeed/Bush.agda let indices = case unEl t of Def _ vs -> drop np vs _ -> __IMPOSSIBLE__ let tel' = telFromList $ drop np $ telToList tel vars np = map (Just . AnArg) $ downFrom np (>+<) <$> (OccursAs (ConArgType c) <$> getOccurrences (vars np) tel') <*> (OccursAs (IndArgType c) . OnlyVarsUpTo np <$> getOccurrences (vars $ size tel) indices) (>+<) ioccs <$> (Concat <$> mapM conOcc cs) Record{recClause = Just c} -> getOccurrences [] =<< instantiateFull c Record{recPars = np, recTel = tel} -> do let tel' = telFromList $ drop np $ telToList tel vars = map (Just . AnArg) $ downFrom np getOccurrences vars =<< normalise tel' -- Andreas, 2017-01-01, issue #1899, treat like data types -- Arguments to other kinds of definitions are hard-wired. Constructor{} -> return emptyOB Axiom{} -> return emptyOB Primitive{} -> return emptyOB AbstractDefn{}-> __IMPOSSIBLE__ -- Building the occurrence graph ------------------------------------------ data Node = DefNode !QName | ArgNode !QName !Nat deriving (Eq, Ord) instance Pretty Node where pretty = \case DefNode q -> P.pretty q ArgNode q i -> P.pretty q P.<> P.text ("." ++ show i) instance PrettyTCM Node where prettyTCM = return . P.pretty instance PrettyTCM n => PrettyTCM (WithNode n Edge) where prettyTCM (WithNode n (Edge o w)) = vcat [ prettyTCM o <+> prettyTCM n , nest 2 $ return $ P.pretty w ] -- | Edge labels for the positivity graph. data Edge = Edge !Occurrence OccursWhere deriving (Eq, Ord, Show) instance Null Edge where null (Edge o _) = null o empty = Edge empty Unknown -- | These operations form a semiring if we quotient by the relation -- \"the 'Occurrence' components are equal\". instance SemiRing Edge where ozero = Edge ozero Unknown oone = Edge oone Unknown oplus _ e@(Edge Mixed _) = e -- dominant oplus e@(Edge Mixed _) _ = e oplus (Edge Unused _) e = e -- neutral oplus e (Edge Unused _) = e oplus (Edge JustNeg _) e@(Edge JustNeg _) = e oplus _ e@(Edge JustNeg w) = Edge Mixed w oplus e@(Edge JustNeg w) _ = Edge Mixed w oplus _ e@(Edge JustPos _) = e -- dominates strict pos. oplus e@(Edge JustPos _) _ = e oplus _ e@(Edge StrictPos _) = e -- dominates 'GuardPos' oplus e@(Edge StrictPos _) _ = e oplus (Edge GuardPos _) e@(Edge GuardPos _) = e otimes (Edge o1 w1) (Edge o2 w2) = Edge (otimes o1 o2) (w1 >*< w2) -- | As 'OccursWhere' does not have an 'oplus' we cannot do something meaningful -- for the @OccursWhere@ here. -- -- E.g. @ostar (Edge JustNeg w) = Edge Mixed (w `oplus` (w >*< w))@ -- would probably more sense, if we could do it. instance StarSemiRing Edge where ostar (Edge o w) = Edge (ostar o) w -- | WARNING: There can be lots of sharing between the 'OccursWhere' -- entries in the edges. Traversing all of these entries could be -- expensive. (See 'computeEdges' for an example.) buildOccurrenceGraph :: Set QName -> TCM (Graph Node Edge) buildOccurrenceGraph qs = Graph.fromListWith oplus . concat <$> mapM defGraph (Set.toList qs) where defGraph :: QName -> TCM [Graph.Edge Node Node Edge] defGraph q = inConcreteOrAbstractMode q $ \ _def -> do occs <- computeOccurrences' q reportSDoc "tc.pos.occs" 40 $ (text "Occurrences in" <+> prettyTCM q <> text ":") $+$ (nest 2 $ vcat $ map (\(i, (n, s)) -> text (show i) <> text ":" <+> text (show n) <+> text "occurrences, of total size" <+> text (show s)) $ List.sortBy (compare `on` fst . snd) $ map (\(i, os) -> (i, (length os, sum $ map size os))) $ Map.toList (flatten occs)) reportSDoc "tc.pos.occs" 50 $ (nest 2 $ vcat $ map (\(i, os) -> (text (show i) <> text ":") $+$ (nest 2 $ vcat $ map (return . P.pretty) os)) (Map.toList (flatten occs))) -- Placing this line before the reportSDoc lines above creates a -- space leak: occs is retained for too long. es <- computeEdges qs q occs reportSDoc "tc.pos.occs.edges" 60 $ text "Edges:" $+$ (nest 2 $ vcat $ map (\e -> let Edge o w = Graph.label e in prettyTCM (Graph.source e) <+> text "-[" <+> return (P.pretty o) <> text "," <+> return (P.pretty w) <+> text "]->" <+> prettyTCM (Graph.target e)) es) return es -- | Computes all non-'ozero' occurrence graph edges represented by -- the given 'OccurrencesBuilder'. -- -- WARNING: There can be lots of sharing between the 'OccursWhere' -- entries in the edges. Traversing all of these entries could be -- expensive. For instance, for the function @F@ in -- @benchmark/misc/SlowOccurrences.agda@ a large number of edges from -- the argument @X@ to the function @F@ are computed. These edges have -- polarity 'StrictPos', 'JustNeg' or 'JustPos', and contain the -- following 'OccursWhere' elements: -- -- * @'Known' ('DS.fromList' ['InDefOf' "F", 'InClause' 0])@, -- -- * @'Known' ('DS.fromList' ['InDefOf' "F", 'InClause' 0, 'LeftOfArrow'])@, -- -- * @'Known' ('DS.fromList' ['InDefOf' "F", 'InClause' 0, 'LeftOfArrow', 'LeftOfArrow'])@, -- -- * @'Known' ('DS.fromList' ['InDefOf' "F", 'InClause' 0, 'LeftOfArrow', 'LeftOfArrow', 'LeftOfArrow'])@, -- -- * and so on. computeEdges :: Set QName -- ^ The names in the current mutual block. -> QName -- ^ The current name. -> OccurrencesBuilder -> TCM [Graph.Edge Node Node Edge] computeEdges muts q ob = ($ []) <$> mkEdge __IMPOSSIBLE__ StrictPos (preprocess ob) where mkEdge to !pol ob = case ob of Concat' obs -> foldr (liftM2 (.)) (return id) [ mkEdge to pol ob | ob <- obs ] OccursAs' w ob -> do (to, pol) <- mkEdge' to pol w mkEdge to pol ob OccursHere' (AnArg i) o -> return $ applyUnless (null pol) (Graph.Edge { Graph.source = ArgNode q i , Graph.target = to , Graph.label = Edge pol o } :) OccursHere' (ADef q') o -> -- Andreas, 2017-04-26, issue #2555 -- Skip nodes pointing outside the mutual block. return $ applyUnless (null pol || Set.notMember q' muts) (Graph.Edge { Graph.source = DefNode q' , Graph.target = to , Graph.label = Edge pol o } :) mkEdge' to !pol w = case w of VarArg -> mixed MetaArg -> mixed LeftOfArrow -> negative DefArg d i -> do pol' <- isGuarding d if Set.member d muts then return (ArgNode d i, pol') else addPol =<< otimes pol' <$> getArgOccurrence d i UnderInf -> addPol GuardPos -- Andreas, 2012-06-09: ∞ is guarding ConArgType _ -> keepGoing IndArgType _ -> mixed InClause _ -> keepGoing Matched -> mixed -- consider arguments matched against as used InDefOf d -> do pol' <- isGuarding d return (DefNode d, pol') where keepGoing = return (to, pol) mixed = return (to, Mixed) negative = return (to, otimes pol JustNeg) addPol pol' = return (to, otimes pol pol') isGuarding d = do isDR <- isDataOrRecordType d return $ case isDR of Just IsData -> GuardPos -- a datatype is guarding _ -> StrictPos Agda-2.5.3/src/full/Agda/TypeChecking/Free.hs0000644000000000000000000002750313154613124016731 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving #-} -- | Computing the free variables of a term. -- -- The distinction between rigid and strongly rigid occurrences comes from: -- Jason C. Reed, PhD thesis, 2009, page 96 (see also his LFMTP 2009 paper) -- -- The main idea is that x = t(x) is unsolvable if x occurs strongly rigidly -- in t. It might have a solution if the occurrence is not strongly rigid, e.g. -- -- x = \f -> suc (f (x (\ y -> k))) has x = \f -> suc (f (suc k)) -- -- [Jason C. Reed, PhD thesis, page 106] -- -- Under coinductive constructors, occurrences are never strongly rigid. -- Also, function types and lambdas do not establish strong rigidity. -- Only inductive constructors do so. -- (See issue 1271). module Agda.TypeChecking.Free ( FreeVars(..) , Free , IsVarSet(..) , IgnoreSorts(..) , runFree , rigidVars, relevantVars, allVars , allFreeVars , allRelevantVars, allRelevantVarsIgnoring , freeIn, freeInIgnoringSorts, isBinderUsed , relevantIn, relevantInIgnoringSortAnn , Occurrence(..) , occurrence , closed , freeVars -- only for testing , freeVars' ) where import Prelude hiding (null) import Control.Monad.Reader import Data.Maybe import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend, mconcat, Any(..), All(..)) import Data.IntSet (IntSet) import qualified Data.IntSet as Set import Data.IntMap (IntMap) import qualified Data.IntMap as Map import Data.Set (Set) import Data.Proxy import qualified Agda.Benchmarking as Bench import Agda.Syntax.Common hiding (Arg, Dom, NamedArg) import Agda.Syntax.Internal import Agda.TypeChecking.Free.Lazy ( Free(..) , FreeEnv(..), initFreeEnv , VarOcc(..), IgnoreSorts(..), Variable, SingleVar , MetaSet, IsVarSet(..), runFreeM ) import qualified Agda.TypeChecking.Free.Lazy as Free import Agda.Utils.Null import Agda.Utils.Singleton type VarSet = IntSet -- | Free variables of a term, (disjointly) partitioned into strongly and -- and weakly rigid variables, flexible variables and irrelevant variables. data FreeVars = FV { stronglyRigidVars :: VarSet -- ^ Variables under only and at least one inductive constructor(s). , unguardedVars :: VarSet -- ^ Variables at top or only under inductive record constructors -- λs and Πs. -- The purpose of recording these separately is that they -- can still become strongly rigid if put under a constructor -- whereas weakly rigid ones stay weakly rigid. , weaklyRigidVars :: VarSet -- ^ Ordinary rigid variables, e.g., in arguments of variables. , flexibleVars :: IntMap MetaSet -- ^ Variables occuring in arguments of metas. -- These are only potentially free, depending how the meta variable is instantiated. -- The set contains the id's of the meta variables that this variable is an argument to. , irrelevantVars :: VarSet -- ^ Variables in irrelevant arguments and under a @DontCare@, i.e., -- in irrelevant positions. , unusedVars :: VarSet -- ^ Variables in 'UnusedArg'uments. } deriving (Eq, Show) mapSRV, mapUGV, mapWRV, mapIRV, mapUUV :: (VarSet -> VarSet) -> FreeVars -> FreeVars mapSRV f fv = fv { stronglyRigidVars = f $ stronglyRigidVars fv } mapUGV f fv = fv { unguardedVars = f $ unguardedVars fv } mapWRV f fv = fv { weaklyRigidVars = f $ weaklyRigidVars fv } mapIRV f fv = fv { irrelevantVars = f $ irrelevantVars fv } mapUUV f fv = fv { unusedVars = f $ unusedVars fv } mapFXV :: (IntMap MetaSet -> IntMap MetaSet) -> FreeVars -> FreeVars mapFXV f fv = fv { flexibleVars = f $ flexibleVars fv } -- | Rigid variables: either strongly rigid, unguarded, or weakly rigid. rigidVars :: FreeVars -> VarSet rigidVars fv = Set.unions [ stronglyRigidVars fv , unguardedVars fv , weaklyRigidVars fv ] -- | All but the irrelevant variables. relevantVars :: FreeVars -> VarSet relevantVars fv = Set.unions [rigidVars fv, Map.keysSet (flexibleVars fv)] -- | @allVars fv@ includes irrelevant variables. allVars :: FreeVars -> VarSet allVars fv = Set.unions [relevantVars fv, irrelevantVars fv, unusedVars fv] data Occurrence = NoOccurrence | Irrelevantly | StronglyRigid -- ^ Under at least one and only inductive constructors. | Unguarded -- ^ In top position, or only under inductive record constructors. | WeaklyRigid -- ^ In arguments to variables and definitions. | Flexible MetaSet -- ^ In arguments of metas. | Unused deriving (Eq,Show) -- | Compute an occurrence of a single variable in a piece of internal syntax. occurrence :: Free a => Nat -> a -> Occurrence occurrence x v = occurrenceFV x $ freeVars v -- | Extract occurrence of a single variable from computed free variables. occurrenceFV :: Nat -> FreeVars -> Occurrence occurrenceFV x fv | x `Set.member` stronglyRigidVars fv = StronglyRigid | x `Set.member` unguardedVars fv = Unguarded | x `Set.member` weaklyRigidVars fv = WeaklyRigid | Just ms <- Map.lookup x (flexibleVars fv) = Flexible ms | x `Set.member` irrelevantVars fv = Irrelevantly | x `Set.member` unusedVars fv = Unused | otherwise = NoOccurrence -- | Mark variables as flexible. Useful when traversing arguments of metas. flexible :: MetaSet -> FreeVars -> FreeVars flexible ms fv = fv { stronglyRigidVars = Set.empty , unguardedVars = Set.empty , weaklyRigidVars = Set.empty , flexibleVars = Map.unionsWith mappend [ Map.fromSet (const ms) (rigidVars fv) , fmap (mappend ms) (flexibleVars fv) ] } -- | Mark rigid variables as non-strongly. Useful when traversing arguments of variables. weakly :: FreeVars -> FreeVars weakly fv = fv { stronglyRigidVars = Set.empty , unguardedVars = Set.empty , weaklyRigidVars = rigidVars fv } -- | Mark unguarded variables as strongly rigid. Useful when traversing arguments of inductive constructors. strongly :: FreeVars -> FreeVars strongly fv = fv { stronglyRigidVars = stronglyRigidVars fv `Set.union` unguardedVars fv , unguardedVars = Set.empty } -- | What happens to the variables occurring under a constructor? underConstructor :: ConHead -> FreeVars -> FreeVars underConstructor (ConHead c i fs) = case (i,fs) of -- Coinductive (record) constructors admit infinite cycles: (CoInductive, _) -> weakly -- Inductive data constructors do not admit infinite cycles: (Inductive, []) -> strongly -- Inductive record constructors do not admit infinite cycles, -- but this cannot be proven inside Agda. -- Thus, unification should not prove it either. (Inductive, (_:_)) -> id -- | Mark all free variables as irrelevant. irrelevantly :: FreeVars -> FreeVars irrelevantly fv = empty { irrelevantVars = allVars fv } -- | Mark all free variables as unused, except for irrelevant vars. unused :: FreeVars -> FreeVars unused fv = empty { irrelevantVars = irrelevantVars fv , unusedVars = Set.unions [ rigidVars fv, Map.keysSet (flexibleVars fv), unusedVars fv ] } -- | Pointwise union. union :: FreeVars -> FreeVars -> FreeVars union (FV sv1 gv1 rv1 fv1 iv1 uv1) (FV sv2 gv2 rv2 fv2 iv2 uv2) = FV (Set.union sv1 sv2) (Set.union gv1 gv2) (Set.union rv1 rv2) (Map.unionWith mappend fv1 fv2) (Set.union iv1 iv2) (Set.union uv1 uv2) unions :: [FreeVars] -> FreeVars unions = foldr union empty instance Null FreeVars where empty = FV Set.empty Set.empty Set.empty Map.empty Set.empty Set.empty null (FV a b c d e f) = null a && null b && null c && null d && null e && null f -- | Free variable sets form a monoid under 'union'. instance Semigroup FreeVars where (<>) = union instance Monoid FreeVars where mempty = empty mappend = (<>) mconcat = unions -- | @delete x fv@ deletes variable @x@ from variable set @fv@. delete :: Nat -> FreeVars -> FreeVars delete n (FV sv gv rv fv iv uv) = FV (Set.delete n sv) (Set.delete n gv) (Set.delete n rv) (Map.delete n fv) (Set.delete n iv) (Set.delete n uv) instance Singleton Variable FreeVars where singleton i = mapUGV (Set.insert i) mempty instance IsVarSet FreeVars where withVarOcc (VarOcc o r) = goOcc o . goRel r where goOcc o = case o of Free.Flexible ms -> flexible ms Free.WeaklyRigid -> weakly Free.Unguarded -> id Free.StronglyRigid -> strongly goRel r = case r of Relevant -> id NonStrict -> id -- we don't track non-strict and Forced{} -> id -- forced in FreeVars Irrelevant -> irrelevantly -- In most cases we don't care about the VarOcc. instance IsVarSet VarSet where withVarOcc _ = id instance IsVarSet [Int] where withVarOcc _ = id instance IsVarSet Any where withVarOcc _ = id instance IsVarSet All where withVarOcc _ = id -- * Collecting free variables. bench :: a -> a bench = Bench.billToPure [ Bench.Typing , Bench.Free ] -- | Doesn't go inside solved metas, but collects the variables from a -- metavariable application @X ts@ as @flexibleVars@. {-# SPECIALIZE freeVars :: Free a => a -> FreeVars #-} freeVars :: (IsVarSet c, Singleton Variable c, Free a) => a -> c freeVars = freeVarsIgnore IgnoreNot {-# SPECIALIZE freeVarsIgnore :: Free a => IgnoreSorts -> a -> FreeVars #-} freeVarsIgnore :: (IsVarSet c, Singleton Variable c, Free a) => IgnoreSorts -> a -> c freeVarsIgnore = runFree singleton -- Specialization to typical monoids {-# SPECIALIZE runFree :: Free a => SingleVar Any -> IgnoreSorts -> a -> Any #-} {-# SPECIALIZE runFree :: Free a => SingleVar FreeVars -> IgnoreSorts -> a -> FreeVars #-} -- Specialization to Term {-# SPECIALIZE runFree :: SingleVar Any -> IgnoreSorts -> Term -> Any #-} {-# SPECIALIZE runFree :: SingleVar FreeVars -> IgnoreSorts -> Term -> FreeVars #-} -- | Compute free variables. runFree :: (IsVarSet c, Free a) => SingleVar c -> IgnoreSorts -> a -> c runFree single i t = -- bench $ -- Benchmarking is expensive (4% on std-lib) runFreeM single i (freeVars' t) -- | Check if a variable is free, possibly ignoring sorts. freeIn' :: Free a => IgnoreSorts -> Nat -> a -> Bool freeIn' ig x t = getAny $ runFree (Any . (x ==)) ig t {-# SPECIALIZE freeIn :: Nat -> Term -> Bool #-} freeIn :: Free a => Nat -> a -> Bool freeIn = freeIn' IgnoreNot freeInIgnoringSorts :: Free a => Nat -> a -> Bool freeInIgnoringSorts = freeIn' IgnoreAll freeInIgnoringSortAnn :: Free a => Nat -> a -> Bool freeInIgnoringSortAnn = freeIn' IgnoreInAnnotations newtype RelevantIn a = RelevantIn {getRelevantIn :: a} deriving (Semigroup, Monoid) instance IsVarSet a => IsVarSet (RelevantIn a) where withVarOcc o x | irrelevant (varRelevance o) = mempty | otherwise = RelevantIn $ withVarOcc o $ getRelevantIn x relevantIn' :: Free a => IgnoreSorts -> Nat -> a -> Bool relevantIn' ig x t = getAny . getRelevantIn $ runFree (RelevantIn . Any . (x ==)) ig t relevantInIgnoringSortAnn :: Free a => Nat -> a -> Bool relevantInIgnoringSortAnn = relevantIn' IgnoreInAnnotations relevantIn :: Free a => Nat -> a -> Bool relevantIn = relevantIn' IgnoreAll -- | Is the variable bound by the abstraction actually used? isBinderUsed :: Free a => Abs a -> Bool isBinderUsed NoAbs{} = False isBinderUsed (Abs _ x) = 0 `freeIn` x -- | Is the term entirely closed (no free variables)? closed :: Free a => a -> Bool closed t = getAll $ runFree (const $ All False) IgnoreNot t -- | Collect all free variables. allFreeVars :: Free a => a -> VarSet allFreeVars = runFree Set.singleton IgnoreNot -- | Collect all relevant free variables, excluding the "unused" ones, possibly ignoring sorts. allRelevantVarsIgnoring :: Free a => IgnoreSorts -> a -> VarSet allRelevantVarsIgnoring ig = getRelevantIn . runFree (RelevantIn . Set.singleton) ig -- | Collect all relevant free variables, excluding the "unused" ones. allRelevantVars :: Free a => a -> VarSet allRelevantVars = allRelevantVarsIgnoring IgnoreNot Agda-2.5.3/src/full/Agda/TypeChecking/Empty.hs-boot0000644000000000000000000000035013154613124020076 0ustar0000000000000000 module Agda.TypeChecking.Empty where import Agda.TypeChecking.Monad (TCM) import Agda.Syntax.Internal (Type) import Agda.Syntax.Position (Range) -- isReallyEmptyType :: Type -> TCM () isEmptyType :: Range -> Type -> TCM () Agda-2.5.3/src/full/Agda/TypeChecking/ReconstructParameters.hs0000644000000000000000000000633713154613124022411 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Reconstruct dropped parameters from constructors. Used by -- with-abstraction to avoid ill-typed abstractions (#745). Note that the -- term is invalid after parameter reconstruction. Parameters need to be -- dropped again before using it. module Agda.TypeChecking.ReconstructParameters where import Control.Applicative import Data.Traversable import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Generic import Agda.TypeChecking.Monad import Agda.TypeChecking.CheckInternal import Agda.TypeChecking.Substitute import Agda.TypeChecking.Reduce import Agda.TypeChecking.Telescope import Agda.TypeChecking.Pretty import Agda.Utils.Size import Agda.Utils.Impossible #include "undefined.h" reconstructParametersInType :: Type -> TCM Type reconstructParametersInType a = traverse (reconstructParameters (sort $ getSort a)) a reconstructParametersInTel :: Telescope -> TCM Telescope reconstructParametersInTel EmptyTel = return EmptyTel reconstructParametersInTel (ExtendTel a tel) = do ar <- reconstructParametersInType (unDom a) addContext (absName tel, a) $ ExtendTel (ar <$ a) <$> traverse reconstructParametersInTel tel reconstructParametersInEqView :: EqualityView -> TCM EqualityView reconstructParametersInEqView (EqualityType s eq l a u v) = EqualityType s eq l <$> traverse (reconstructParameters $ sort s) a <*> traverse (reconstructParameters $ El s $ unArg a) u <*> traverse (reconstructParameters $ El s $ unArg a) v reconstructParametersInEqView (OtherType a) = OtherType <$> reconstructParametersInType a reconstructParameters :: Type -> Term -> TCM Term reconstructParameters a v = do reportSDoc "tc.with.reconstruct" 30 $ sep [ text "reconstructing parameters in" , nest 2 $ sep [ prettyTCM v <+> text ":", nest 2 $ prettyTCM a ] ] v <- checkInternal' (defaultAction{ postAction = reconstruct }) v a reportSDoc "tc.with.reconstruct" 30 $ nest 2 $ text "-->" <+> prettyTCM v return v where reconstruct a v = do case ignoreSharing v of Con h ci vs -> do TelV tel a <- telView a let under = size tel -- under-applied when under > 0 reportSDoc "tc.with.reconstruct" 50 $ sep [ text "reconstructing" , nest 2 $ sep [ prettyTCM v <+> text ":" , nest 2 $ prettyTCM a ] ] case ignoreSharing (unEl a) of Def d es -> do Just n <- defParameters <$> getConstInfo d let Just ps = applySubst (strengthenS __IMPOSSIBLE__ under) . take n <$> allApplyElims es reportSLn "tc.with.reconstruct" 50 $ show n ++ " parameters" -- TODO: the reconstructed parameters are not reconstructed recursively! return $ Con h ci (ps ++ vs) _ -> __IMPOSSIBLE__ _ -> return v dropParameters :: TermLike a => a -> TCM a dropParameters = traverseTermM dropPars where dropPars v = case ignoreSharing v of Con c ci vs -> do Constructor{ conData = d } <- theDef <$> getConstInfo (conName c) Just n <- defParameters <$> getConstInfo d return $ Con c ci $ drop n vs _ -> return v Agda-2.5.3/src/full/Agda/TypeChecking/Irrelevance.hs0000644000000000000000000000653313154613124020307 0ustar0000000000000000-- {-# LANGUAGE CPP #-} {-| Irrelevant function types. -} module Agda.TypeChecking.Irrelevance where import Control.Arrow (first, second) import Control.Applicative import Control.Monad.Reader import qualified Data.Map as Map import Agda.Interaction.Options import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad -- | data 'Relevance' -- see "Agda.Syntax.Common". -- * Operations on 'Dom'. -- | Prepare parts of a parameter telescope for abstraction in constructors -- and projections. hideAndRelParams :: Dom a -> Dom a hideAndRelParams = hideOrKeepInstance . mapRelevance nonStrictToIrr -- | Used to modify context when going into a @rel@ argument. inverseApplyRelevance :: Relevance -> Dom a -> Dom a inverseApplyRelevance rel = mapRelevance (rel `inverseComposeRelevance`) -- | Compose two relevance flags. -- This function is used to update the relevance information -- on pattern variables @a@ after a match against something @rel@. applyRelevance :: Relevance -> Dom a -> Dom a applyRelevance rel = mapRelevance (rel `composeRelevance`) -- * Operations on 'Context'. -- | Modify the context whenever going from the l.h.s. (term side) -- of the typing judgement to the r.h.s. (type side). workOnTypes :: TCM a -> TCM a workOnTypes cont = do allowed <- optExperimentalIrrelevance <$> pragmaOptions verboseBracket "tc.irr" 20 "workOnTypes" $ workOnTypes' allowed cont -- | Internal workhorse, expects value of --experimental-irrelevance flag -- as argument. workOnTypes' :: Bool -> TCM a -> TCM a workOnTypes' experimental cont = modifyContext (modifyContextEntries $ mapRelevance f) cont where f | experimental = irrToNonStrict . nonStrictToRel | otherwise = nonStrictToRel -- | (Conditionally) wake up irrelevant variables and make them relevant. -- For instance, -- in an irrelevant function argument otherwise irrelevant variables -- may be used, so they are awoken before type checking the argument. applyRelevanceToContext :: Relevance -> TCM a -> TCM a applyRelevanceToContext rel = case rel of Relevant -> id Forced{} -> id _ -> local $ \ e -> e { envContext = modifyContextEntries (inverseApplyRelevance rel) (envContext e) , envLetBindings = (Map.map . fmap . second) (inverseApplyRelevance rel) (envLetBindings e) -- enable local irr. defs , envRelevance = composeRelevance rel (envRelevance e) -- enable global irr. defs } -- | Wake up irrelevant variables and make them relevant. This is used -- when type checking terms in a hole, in which case you want to be able to -- (for instance) infer the type of an irrelevant variable. In the course -- of type checking an irrelevant function argument 'applyRelevanceToContext' -- is used instead, which also sets the context relevance to 'Irrelevant'. -- This is not the right thing to do when type checking interactively in a -- hole since it also marks all metas created during type checking as -- irrelevant (issue #2568). wakeIrrelevantVars :: TCM a -> TCM a wakeIrrelevantVars = local $ \ e -> e { envContext = modifyContextEntries (inverseApplyRelevance Irrelevant) (envContext e) , envLetBindings = (Map.map . fmap . second) (inverseApplyRelevance Irrelevant) (envLetBindings e) } Agda-2.5.3/src/full/Agda/TypeChecking/Rewriting.hs-boot0000644000000000000000000000037013154613124020754 0ustar0000000000000000module Agda.TypeChecking.Rewriting where import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base verifyBuiltinRewrite :: Term -> Type -> TCM () rewrite :: Blocked_ -> Term -> RewriteRules -> Elims -> ReduceM (Reduced (Blocked Term) Term) Agda-2.5.3/src/full/Agda/TypeChecking/Forcing.hs0000644000000000000000000001130513154613124017430 0ustar0000000000000000{-# LANGUAGE CPP #-} {-| A constructor argument is forced if it appears as pattern variable in an index of the target. For instance @x@ is forced in @sing@ and @n@ is forced in @zero@ and @suc@: @ data Sing {a}{A : Set a} : A -> Set where sing : (x : A) -> Sing x data Fin : Nat -> Set where zero : (n : Nat) -> Fin (suc n) suc : (n : Nat) (i : Fin n) -> Fin (suc n) @ At runtime, forced constructor arguments may be erased as they can be recovered from dot patterns. For instance, @ unsing : {A : Set} (x : A) -> Sing x -> A unsing .x (sing x) = x @ can become @ unsing x sing = x @ and @ proj : (n : Nat) (i : Fin n) -> Nat proj .(suc n) (zero n) = n proj .(suc n) (suc n i) = n @ becomes @ proj (suc n) zero = n proj (suc n) (suc i) = n @ Forcing is a concept from pattern matching and thus builds on the concept of equality (I) used there (closed terms, extensional) which is different from the equality (II) used in conversion checking and the constraint solver (open terms, intensional). Up to issue 1441 (Feb 2015), the forcing analysis here relied on the wrong equality (II), considering type constructors as injective. This is unsound for program extraction, but ok if forcing is only used to decide which arguments to skip during conversion checking. From now on, forcing uses equality (I) and does not search for forced variables under type constructors. This may lose some savings during conversion checking. If this turns out to be a problem, the old forcing could be brought back, using a new modality @Skip@ to indicate that this is a relevant argument but still can be skipped during conversion checking as it is forced by equality (II). -} module Agda.TypeChecking.Forcing where import Prelude hiding (elem, maximum) import Control.Applicative import Data.Foldable import Data.Traversable import Agda.Interaction.Options import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Conversion import Agda.Utils.Function import Agda.Utils.Monad import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | Given the type of a constructor (excluding the parameters), -- decide which arguments are forced. -- Update the relevance info in the domains accordingly. -- Precondition: the type is of the form @Γ → D vs@ and the @vs@ -- are in normal form. addForcingAnnotations :: Type -> TCM Type addForcingAnnotations t = ifM (not . optForcing <$> commandLineOptions) (return t) $ do -- Andreas, 2015-03-10 Normalization prevents Issue 1454. -- t <- normalise t -- Andreas, 2015-03-28 Issue 1469: Normalization too costly. -- Instantiation also fixes Issue 1454. -- Note that normalization of s0 below does not help. t <- instantiateFull t let TelV tel (El s a) = telView' t vs = case ignoreSharing a of Def _ us -> us _ -> __IMPOSSIBLE__ n = size tel indexToLevel x = n - x - 1 -- Note: data parameters will be negative levels. let xs = filter (>=0) $ map indexToLevel $ forcedVariables vs let s0 = raise (0 - size tel) s t' <- force s0 xs t reportSLn "tc.force" 60 $ unlines [ "Forcing analysis" , " xs = " ++ show xs , " t = " ++ show t , " t' = " ++ show t' ] return t' -- | Compute the pattern variables of a term or term-like thing. class ForcedVariables a where forcedVariables :: a -> [Nat] instance (ForcedVariables a, Foldable t) => ForcedVariables (t a) where forcedVariables = foldMap forcedVariables -- | Assumes that the term is in normal form. instance ForcedVariables Term where forcedVariables t = case ignoreSharing t of Var i [] -> [i] Con _ _ vs -> forcedVariables vs _ -> [] -- | @force s xs t@ marks the domains @xs@ in function type @t@ as forced. -- Domains bigger than @s@ are marked as @'Forced' 'Big'@, others as -- @'Forced' 'Small'@. -- Counting left-to-right, starting with 0. -- Precondition: function type is exposed. force :: Sort -> [Nat] -> Type -> TCM Type force s0 xs t = loop 0 t where m = maximum (-1:xs) -- number of domains to look at loop i t | i > m = return t loop i t = case ignoreSharingType t of El s (Pi a b) -> do a' <- if not (i `elem` xs) then return a else do -- If the sort of the data type is >= the sort of the argument type -- then the index is small, else big. b <- ifM (tryConversion $ leqSort (getSort a) (raise i s0)) (return Small) (return Big) return $ mapRelevance (composeRelevance $ Forced b) a El s . Pi a' <$> traverse (loop $ i + 1) b _ -> __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/EtaContract.hs0000644000000000000000000000772613154613124020264 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Compute eta short normal forms. module Agda.TypeChecking.EtaContract where import Control.Monad.Reader import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Generic import Agda.TypeChecking.Substitute import Agda.TypeChecking.Free import Agda.TypeChecking.Monad import Agda.TypeChecking.Reduce.Monad () import {-# SOURCE #-} Agda.TypeChecking.Records import {-# SOURCE #-} Agda.TypeChecking.Datatypes import Agda.Utils.Monad #include "undefined.h" import Agda.Utils.Impossible -- TODO: move to Agda.Syntax.Internal.SomeThing data BinAppView = App Term (Arg Term) | NoApp Term binAppView :: Term -> BinAppView binAppView t = case t of Var i xs -> appE (Var i) xs Def c xs -> appE (Def c) xs -- Andreas, 2013-09-17: do not eta-contract when body is (record) constructor -- like in \ x -> s , x! (See interaction/DoNotEtaContractFunIntoRecord) -- (Cf. also issue 889 (fixed differently).) -- At least record constructors should be fully applied where possible! -- TODO: also for ordinary constructors (\ x -> suc x vs. suc)? Con c ci xs | null (conFields c) -> app (Con c ci) xs | otherwise -> noApp Lit _ -> noApp Level _ -> noApp -- could be an application, but let's not eta contract levels Lam _ _ -> noApp Pi _ _ -> noApp Sort _ -> noApp MetaV _ _ -> noApp DontCare _ -> noApp Shared p -> binAppView (derefPtr p) -- destroys sharing where noApp = NoApp t app f [] = noApp app f xs = App (f $ init xs) (last xs) appE f [] = noApp appE f xs | Apply v <- last xs = App (f $ init xs) v | otherwise = noApp -- | Contracts all eta-redexes it sees without reducing. {-# SPECIALIZE etaContract :: TermLike a => a -> TCM a #-} {-# SPECIALIZE etaContract :: TermLike a => a -> ReduceM a #-} etaContract :: (MonadReader TCEnv m, HasConstInfo m, HasOptions m, TermLike a) => a -> m a etaContract = traverseTermM etaOnce {-# SPECIALIZE etaOnce :: Term -> TCM Term #-} {-# SPECIALIZE etaOnce :: Term -> ReduceM Term #-} etaOnce :: (MonadReader TCEnv m, HasConstInfo m, HasOptions m) => Term -> m Term etaOnce v = case v of -- Andreas, 2012-11-18: this call to reportSDoc seems to cost me 2% -- performance on the std-lib -- reportSDoc "tc.eta" 70 $ text "eta-contracting" <+> prettyTCM v Shared{} -> updateSharedTerm etaOnce v Lam i (Abs _ b) -> do -- NoAbs can't be eta'd tyty <- typeInType case binAppView b of App u (Arg info v) | isVar0 tyty v -- Andreas, 2017-02-20 issue #2464 -- Contracting with any irrelevant argument breaks subject reduction. -- E.g. \ .x -> f .(subst P eq x) can in general not be contracted to f. -- -- | (isIrrelevant info || isVar0 tyty v) && sameHiding i info && not (freeIn 0 u) -> return $ strengthen __IMPOSSIBLE__ u _ -> return v where isVar0 tyty (Shared p) = isVar0 tyty (derefPtr p) isVar0 _ (Var 0 []) = True -- Andreas, 2016-01-08 If --type-in-type, all levels are equal. isVar0 True Level{} = True isVar0 tyty (Level (Max [Plus 0 l])) = case l of NeutralLevel _ v -> isVar0 tyty v UnreducedLevel v -> isVar0 tyty v BlockedLevel{} -> False MetaLevel{} -> False isVar0 _ _ = False -- Andreas, 2012-12-18: Abstract definitions could contain -- abstract records whose constructors are not in scope. -- To be able to eta-contract them, we ignore abstract. Con c ci args -> ignoreAbstractMode $ do -- reportSDoc "tc.eta" 20 $ text "eta-contracting record" <+> prettyTCM t r <- getConstructorData $ conName c -- fails in ConcreteMode if c is abstract ifM (isEtaRecord r) (do -- reportSDoc "tc.eta" 20 $ text "eta-contracting record" <+> prettyTCM t etaContractRecord r c ci args) (return v) v -> return v Agda-2.5.3/src/full/Agda/TypeChecking/Implicit.hs0000644000000000000000000001144513154613124017620 0ustar0000000000000000{-# LANGUAGE CPP #-} {-| Functions for inserting implicit arguments at the right places. -} module Agda.TypeChecking.Implicit where import Control.Applicative import Control.Monad import Agda.Syntax.Common import Agda.Syntax.Internal as I import Agda.TypeChecking.Irrelevance import {-# SOURCE #-} Agda.TypeChecking.MetaVars import Agda.TypeChecking.Monad import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Pretty import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- | @implicitArgs n expand eti t@ generates up to @n@ implicit arguments -- metas (unbounded if @n<0@), as long as @t@ is a function type -- and @expand@ holds on the hiding info of its domain. -- -- If explicit arguments are to be inserted as well, they are -- inserted as instance arguments (used for recursive instance search). implicitArgs :: Int -> (Hiding -> Bool) -> Type -> TCM (Args, Type) implicitArgs n expand t = mapFst (map (fmap namedThing)) <$> do implicitNamedArgs n (\ h x -> expand h) t -- | @implicitNamedArgs n expand eti t@ generates up to @n@ named implicit arguments -- metas (unbounded if @n<0@), as long as @t@ is a function type -- and @expand@ holds on the hiding and name info of its domain. -- -- If explicit arguments are to be inserted as well, they are -- inserted as instance arguments (used for recursive instance search). implicitNamedArgs :: Int -> (Hiding -> ArgName -> Bool) -> Type -> TCM (NamedArgs, Type) implicitNamedArgs 0 expand t0 = return ([], t0) implicitNamedArgs n expand t0 = do t0' <- reduce t0 case ignoreSharing $ unEl t0' of Pi (Dom info a) b | let x = absName b, expand (getHiding info) x -> do info' <- if hidden info then return info else do reportSDoc "tc.term.args.ifs" 15 $ text "inserting instance meta for type" <+> prettyTCM a return $ makeInstance info (_, v) <- newMetaArg info' x a let narg = Arg info (Named (Just $ unranged x) v) mapFst (narg :) <$> implicitNamedArgs (n-1) expand (absApp b v) _ -> return ([], t0') -- | Create a metavariable according to the 'Hiding' info. newMetaArg :: ArgInfo -- ^ Kind/relevance of meta. -> ArgName -- ^ Name suggestion for meta. -> Type -- ^ Type of meta. -> TCM (MetaId, Term) -- ^ The created meta as id and as term. newMetaArg info x a = do applyRelevanceToContext (getRelevance info) $ newMeta (getHiding info) (argNameToString x) a where newMeta :: Hiding -> String -> Type -> TCM (MetaId, Term) newMeta Instance{} = newIFSMeta newMeta Hidden = newNamedValueMeta RunMetaOccursCheck newMeta NotHidden = newNamedValueMeta RunMetaOccursCheck -- | Create a questionmark according to the 'Hiding' info. newInteractionMetaArg :: ArgInfo -- ^ Kind/relevance of meta. -> ArgName -- ^ Name suggestion for meta. -> Type -- ^ Type of meta. -> TCM (MetaId, Term) -- ^ The created meta as id and as term. newInteractionMetaArg info x a = do applyRelevanceToContext (getRelevance info) $ newMeta (getHiding info) (argNameToString x) a where newMeta :: Hiding -> String -> Type -> TCM (MetaId, Term) newMeta Instance{} = newIFSMeta newMeta Hidden = newNamedValueMeta' RunMetaOccursCheck newMeta NotHidden = newNamedValueMeta' RunMetaOccursCheck --------------------------------------------------------------------------- data ImplicitInsertion = ImpInsert [Hiding] -- ^ this many implicits have to be inserted | BadImplicits -- ^ hidden argument where there should have been a non-hidden arg | NoSuchName ArgName -- ^ bad named argument | NoInsertNeeded deriving (Show) impInsert :: [Hiding] -> ImplicitInsertion impInsert [] = NoInsertNeeded impInsert hs = ImpInsert hs -- | The list should be non-empty. insertImplicit :: NamedArg e -> [Arg ArgName] -> ImplicitInsertion insertImplicit _ [] = __IMPOSSIBLE__ insertImplicit a ts | visible a = impInsert $ nofHidden ts where nofHidden :: [Arg a] -> [Hiding] nofHidden = takeWhile notVisible . map getHiding insertImplicit a ts = case nameOf (unArg a) of Nothing -> maybe BadImplicits impInsert $ upto (getHiding a) $ map getHiding ts Just x -> find [] (rangedThing x) (getHiding a) ts where upto h [] = Nothing upto h (NotHidden : _) = Nothing upto h (h' : _) | sameHiding h h' = Just [] upto h (h' : hs) = (h' :) <$> upto h hs find :: [Hiding] -> ArgName -> Hiding -> [Arg ArgName] -> ImplicitInsertion find _ x _ (a@(Arg{}) : _) | visible a = NoSuchName x find hs x hidingx (a@(Arg _ y) : ts) | x == y && sameHiding hidingx a = impInsert $ reverse hs | x == y && sameHiding hidingx a = BadImplicits | otherwise = find (getHiding a : hs) x hidingx ts find i x _ [] = NoSuchName x Agda-2.5.3/src/full/Agda/TypeChecking/LevelConstraints.hs0000644000000000000000000000437313154613124021347 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.LevelConstraints ( simplifyLevelConstraint ) where import qualified Data.List as List import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Substitute import Agda.TypeChecking.Free import Agda.Utils.Impossible #include "undefined.h" -- | @simplifyLevelConstraint c cs@ turns an @c@ into an equality -- constraint if it is an inequality constraint and the reverse -- inequality is contained in @cs@. -- -- The constraints doesn't necessarily have to live in the same context, but -- they do need to be universally quanitfied over the context. This function -- takes care of renaming variables when checking for matches. simplifyLevelConstraint :: Constraint -> [Constraint] -> Constraint simplifyLevelConstraint new old = case inequalities new of [a :=< b] | any (matchLeq (b :=< a)) leqs -> LevelCmp CmpEq (Max [a]) (Max [b]) _ -> new where leqs = concatMap inequalities old data Leq = PlusLevel :=< PlusLevel deriving (Show, Eq) -- | Check if two inequality constraints are the same up to variable renaming. matchLeq :: Leq -> Leq -> Bool matchLeq (a :=< b) (c :=< d) | length xs == length ys = (a, b) == applySubst rho (c, d) | otherwise = False where free :: Free a => a -> [Int] free = List.nub . runFree (:[]) IgnoreNot -- Note: use a list to preserve order of variables xs = free (a, b) ys = free (c, d) rho = mkSub $ List.sort $ zip ys xs mkSub = go 0 where go _ [] = IdS go y ren0@((y', x) : ren) | y == y' = Var x [] :# go (y + 1) ren | otherwise = Strengthen __IMPOSSIBLE__ $ go (y + 1) ren0 -- | Turn a level constraint into a list of level inequalities, if possible. inequalities :: Constraint -> [Leq] inequalities (LevelCmp CmpLeq (Max as) (Max [b])) = map (:=< b) as -- Andreas, 2016-09-28 -- Why was this most natural case missing? -- See test/Succeed/LevelLeqGeq.agda for where it is useful! -- These are very special cases only, in no way complete: inequalities (LevelCmp CmpEq (Max [a, b]) (Max [c])) | a == c = [b :=< a] | b == c = [a :=< b] inequalities (LevelCmp CmpEq (Max [a]) (Max [b, c])) | a == b = [c :=< b] | a == c = [b :=< c] inequalities _ = [] Agda-2.5.3/src/full/Agda/TypeChecking/Datatypes.hs-boot0000644000000000000000000000041313154613124020736 0ustar0000000000000000 module Agda.TypeChecking.Datatypes where import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Signature import Agda.Syntax.Internal getConHead :: QName -> TCM (Either SigError ConHead) getConstructorData :: HasConstInfo m => QName -> m QName Agda-2.5.3/src/full/Agda/TypeChecking/SizedTypes.hs0000644000000000000000000006514413154613124020156 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.SizedTypes where import Prelude hiding (null) import Control.Monad.Writer import qualified Data.List as List import qualified Data.Map as Map import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce import {-# SOURCE #-} Agda.TypeChecking.MetaVars import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import {-# SOURCE #-} Agda.TypeChecking.Conversion import {-# SOURCE #-} Agda.TypeChecking.Constraints import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.List as List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Size import Agda.Utils.Tuple import qualified Agda.Utils.Warshall as W #include "undefined.h" import Agda.Utils.Impossible ------------------------------------------------------------------------ -- * SIZELT stuff ------------------------------------------------------------------------ -- | Check whether a type is either not a SIZELT or a SIZELT that is non-empty. checkSizeLtSat :: Term -> TCM () checkSizeLtSat t = do reportSDoc "tc.size" 10 $ do tel <- getContextTelescope sep [ text "checking that " <+> prettyTCM t <+> text " is not an empty type of sizes" , if null tel then empty else do text "in context " <+> inTopContext (prettyTCM tel) ] reportSLn "tc.size" 60 $ "- raw type = " ++ show t let postpone :: Term -> TCM () postpone t = do reportSDoc "tc.size.lt" 20 $ sep [ text "- postponing `not empty type of sizes' check for " <+> prettyTCM t ] addConstraint $ CheckSizeLtSat t let ok :: TCM () ok = reportSLn "tc.size.lt" 20 $ "- succeeded: not an empty type of sizes" ifBlocked t (const postpone) $ \ t -> do reportSLn "tc.size.lt" 20 $ "- type is not blocked" caseMaybeM (isSizeType t) ok $ \ b -> do reportSLn "tc.size.lt" 20 $ " - type is a size type" case b of BoundedNo -> ok BoundedLt b -> do reportSDoc "tc.size.lt" 20 $ text " - type is SIZELT" <+> prettyTCM b ifBlocked b (\ _ _ -> postpone t) $ \ b -> do reportSLn "tc.size.lt" 20 $ " - size bound is not blocked" catchConstraint (CheckSizeLtSat t) $ do unlessM (checkSizeNeverZero b) $ do typeError . GenericDocError =<< do text "Possibly empty type of sizes " <+> prettyTCM t -- | Precondition: Term is reduced and not blocked. -- Throws a 'patternViolation' if undecided checkSizeNeverZero :: Term -> TCM Bool checkSizeNeverZero u = do v <- sizeView u case v of SizeInf -> return True -- OK, infty is never 0. SizeSuc{} -> return True -- OK, a + 1 is never 0. OtherSize u -> case ignoreSharing u of Var i [] -> checkSizeVarNeverZero i -- neutral sizes cannot be guaranteed > 0 _ -> return False -- -- | A size variable is never zero if it is the strict upper bound of -- -- some other size variable in context. -- -- Eg. @i : Size, j : Size< i@ |- i is never zero. -- -- Throws a 'patternViolation' if undecided. -- checkSizeVarNeverZero :: Int -> TCM Bool -- checkSizeVarNeverZero i = do -- -- Looking for a variable j : Size< i, we can restrict to the last i -- -- entries, as this variable necessarily has been defined later than i. -- doms <- take i <$> getContext -- -- We raise each type to make sense in the current context. -- let ts = zipWith raise [1..] $ map (snd . unDom) doms -- reportSDoc "tc.size" 15 $ sep -- [ text "checking that size " <+> prettyTCM (var i) <+> text " is never 0" -- , text "in context " <+> do sep $ map prettyTCM ts -- ] -- foldr f (return False) ts -- where -- f t cont = do -- -- If we encounter a blocked type in the context, we cannot -- -- definitely say no. -- let yes = return True -- no = cont -- perhaps = cont >>= \ res -> if res then return res else patternViolation -- ifBlockedType t (\ _ _ -> perhaps) $ \ t -> do -- caseMaybeM (isSizeType t) no $ \ b -> do -- case b of -- BoundedNo -> no -- BoundedLt u -> ifBlocked u (\ _ _ -> perhaps) $ \ u -> do -- case ignoreSharing u of -- Var i' [] | i == i' -> yes -- _ -> no -- | Checks that a size variable is ensured to be @> 0@. -- E.g. variable @i@ cannot be zero in context -- @(i : Size) (j : Size< ↑ ↑ i) (k : Size< j) (k' : Size< k)@. -- Throws a 'patternViolation' if undecided. checkSizeVarNeverZero :: Int -> TCM Bool checkSizeVarNeverZero i = do reportSDoc "tc.size" 20 $ text "checkSizeVarNeverZero" <+> prettyTCM (var i) -- Looking for the minimal value for size variable i, -- we can restrict to the last i -- entries, as only these can contain i in an upper bound. ts <- map (snd . unDom) . take i <$> getContext -- If we encountered a blocking meta in the context, we cannot -- say ``no'' for sure. (n, Any meta) <- runWriterT $ minSizeValAux ts $ repeat 0 if n > 0 then return True else if meta then patternViolation else return False where -- Compute the least valuation for size context ts above the -- given valuation and return its last value. minSizeValAux :: [Type] -> [Int] -> WriterT Any TCM Int minSizeValAux _ [] = __IMPOSSIBLE__ minSizeValAux [] (n : _) = return n minSizeValAux (t : ts) (n : ns) = do reportSDoc "tc.size" 60 $ text ("minSizeVal (n:ns) = " ++ show (take (length ts + 2) $ n:ns) ++ " t =") <+> (text . show) t -- prettyTCM t -- Wrong context! -- n is the min. value for variable 0 which has type t. let cont = minSizeValAux ts ns perhaps = tell (Any True) >> cont -- If we encounter a blocked type in the context, we cannot -- give a definite answer. ifBlockedType t (\ _ _ -> perhaps) $ \ t -> do caseMaybeM (liftTCM $ isSizeType t) cont $ \ b -> do case b of BoundedNo -> cont BoundedLt u -> ifBlocked u (\ _ _ -> perhaps) $ \ u -> do reportSLn "tc.size" 60 $ "minSizeVal upper bound u = " ++ show u v <- liftTCM $ deepSizeView u case v of -- Variable 0 has bound @(< j + m)@ -- meaning that @minval(j) > n - m@, i.e., @minval(j) >= n+1-m@. -- Thus, we update the min value for @j@ with function @(max (n+1-m))@. DSizeVar j m -> do reportSLn "tc.size" 60 $ "minSizeVal upper bound v = " ++ show v let ns' = List.updateAt j (max $ n+1-m) ns reportSLn "tc.size" 60 $ "minSizeVal ns' = " ++ show (take (length ts + 1) ns') minSizeValAux ts ns' DSizeMeta{} -> perhaps _ -> cont -- | Check whether a variable in the context is bounded by a size expression. -- If @x : Size< a@, then @a@ is returned. isBounded :: MonadTCM tcm => Nat -> tcm BoundedSize isBounded i = liftTCM $ do t <- reduce =<< typeOfBV i case ignoreSharing $ unEl t of Def x [Apply u] -> do sizelt <- getBuiltin' builtinSizeLt return $ if (Just (Def x []) == sizelt) then BoundedLt $ unArg u else BoundedNo _ -> return BoundedNo -- | Whenever we create a bounded size meta, add a constraint -- expressing the bound. -- In @boundedSizeMetaHook v tel a@, @tel@ includes the current context. boundedSizeMetaHook :: Term -> Telescope -> Type -> TCM () boundedSizeMetaHook v tel0 a = do res <- isSizeType a case res of Just (BoundedLt u) -> do n <- getContextSize let tel | n > 0 = telFromList $ drop n $ telToList tel0 | otherwise = tel0 addContext' tel $ do v <- sizeSuc 1 $ raise (size tel) v `apply` teleArgs tel -- compareSizes CmpLeq v u size <- sizeType addConstraint $ ValueCmp CmpLeq size v u _ -> return () -- | @trySizeUniv cmp t m n x els1 y els2@ -- is called as a last resort when conversion checking @m `cmp` n : t@ -- failed for definitions @m = x els1@ and @n = y els2@, -- where the heads @x@ and @y@ are not equal. -- -- @trySizeUniv@ accounts for subtyping between SIZELT and SIZE, -- like @Size< i =< Size@. -- -- If it does not succeed it reports failure of conversion check. trySizeUniv :: Comparison -> Type -> Term -> Term -> QName -> Elims -> QName -> Elims -> TCM () trySizeUniv cmp t m n x els1 y els2 = do let failure = typeError $ UnequalTerms cmp m n t forceInfty u = compareSizes CmpEq (unArg u) =<< primSizeInf -- Get the SIZE built-ins. (size, sizelt) <- flip catchError (const failure) $ do Def size _ <- ignoreSharing <$> primSize Def sizelt _ <- ignoreSharing <$> primSizeLt return (size, sizelt) case (cmp, els1, els2) of -- Case @Size< _ <= Size@: true. (CmpLeq, [_], []) | x == sizelt && y == size -> return () -- Case @Size< u = Size@: forces @u = ∞@. (_, [Apply u], []) | x == sizelt && y == size -> forceInfty u (_, [], [Apply u]) | x == size && y == sizelt -> forceInfty u -- This covers all cases for SIZE and SIZELT. -- The remaining case is for @x@ and @y@ which are not size built-ins. _ -> failure ------------------------------------------------------------------------ -- * Size views that 'reduce'. ------------------------------------------------------------------------ -- | Compute the deep size view of a term. -- Precondition: sized types are enabled. deepSizeView :: Term -> TCM DeepSizeView deepSizeView v = do Def inf [] <- ignoreSharing <$> primSizeInf Def suc [] <- ignoreSharing <$> primSizeSuc let loop v = do v <- reduce v case ignoreSharing v of Def x [] | x == inf -> return $ DSizeInf Def x [Apply u] | x == suc -> sizeViewSuc_ suc <$> loop (unArg u) Var i [] -> return $ DSizeVar i 0 MetaV x us -> return $ DSizeMeta x us 0 _ -> return $ DOtherSize v loop v sizeMaxView :: Term -> TCM SizeMaxView sizeMaxView v = do inf <- getBuiltinDefName builtinSizeInf suc <- getBuiltinDefName builtinSizeSuc max <- getBuiltinDefName builtinSizeMax let loop v = do v <- reduce v case ignoreSharing v of Def x [] | Just x == inf -> return $ [DSizeInf] Def x [Apply u] | Just x == suc -> maxViewSuc_ (fromJust suc) <$> loop (unArg u) Def x [Apply u1, Apply u2] | Just x == max -> maxViewMax <$> loop (unArg u1) <*> loop (unArg u2) Var i [] -> return $ [DSizeVar i 0] MetaV x us -> return $ [DSizeMeta x us 0] _ -> return $ [DOtherSize v] loop v ------------------------------------------------------------------------ -- * Size comparison that might add constraints. ------------------------------------------------------------------------ -- | Compare two sizes. compareSizes :: Comparison -> Term -> Term -> TCM () compareSizes cmp u v = do reportSDoc "tc.conv.size" 10 $ vcat [ text "Comparing sizes" , nest 2 $ sep [ prettyTCM u <+> prettyTCM cmp , prettyTCM v ] ] verboseS "tc.conv.size" 60 $ do u <- reduce u v <- reduce v reportSDoc "tc.conv.size" 60 $ nest 2 $ sep [ text (show u) <+> prettyTCM cmp , text (show v) ] us <- sizeMaxView u vs <- sizeMaxView v compareMaxViews cmp us vs -- | Compare two sizes in max view. compareMaxViews :: Comparison -> SizeMaxView -> SizeMaxView -> TCM () compareMaxViews cmp us vs = case (cmp, us, vs) of (CmpLeq, _, (DSizeInf : _)) -> return () (cmp, [u], [v]) -> compareSizeViews cmp u v (CmpLeq, us, [v]) -> forM_ us $ \ u -> compareSizeViews cmp u v (CmpLeq, us, vs) -> forM_ us $ \ u -> compareBelowMax u vs (CmpEq, us, vs) -> compareMaxViews CmpLeq us vs >> compareMaxViews CmpLeq vs us -- | @compareBelowMax u vs@ checks @u <= max vs@. Precondition: @size vs >= 2@ compareBelowMax :: DeepSizeView -> SizeMaxView -> TCM () compareBelowMax u vs = do reportSDoc "tc.conv.size" 45 $ vcat [ text "compareBelowMax" ] alt (dontAssignMetas $ alts $ map (compareSizeViews CmpLeq u) vs) $ do reportSDoc "tc.conv.size" 45 $ vcat [ text "compareBelowMax: giving up" ] u <- unDeepSizeView u v <- unMaxView vs size <- sizeType addConstraint $ ValueCmp CmpLeq size u v where alt c1 c2 = c1 `catchError` const c2 alts [] = __IMPOSSIBLE__ alts [c] = c alts (c:cs) = c `alt` alts cs compareSizeViews :: Comparison -> DeepSizeView -> DeepSizeView -> TCM () compareSizeViews cmp s1' s2' = do reportSDoc "tc.conv.size" 45 $ hsep [ text "compareSizeViews" , text (show s1') , text (show cmp) , text (show s2') ] size <- sizeType let (s1, s2) = removeSucs (s1', s2') withUnView cont = do u <- unDeepSizeView s1 v <- unDeepSizeView s2 cont u v failure = withUnView $ \ u v -> typeError $ UnequalTerms cmp u v size continue cmp = withUnView $ compareAtom cmp size case (cmp, s1, s2) of (CmpLeq, _, DSizeInf) -> return () (CmpEq, DSizeInf, DSizeInf) -> return () (CmpEq, DSizeVar{}, DSizeInf) -> failure (_ , DSizeInf, DSizeVar{}) -> failure (_ , DSizeInf, _ ) -> continue CmpEq (CmpLeq, DSizeVar i n, DSizeVar j m) | i == j -> unless (n <= m) failure (CmpLeq, DSizeVar i n, DSizeVar j m) | i /= j -> do res <- isBounded i case res of BoundedNo -> failure BoundedLt u' -> do -- now we have i < u', in the worst case i+1 = u' -- and we want to check i+n <= v v <- unDeepSizeView s2 if n > 0 then do u'' <- sizeSuc (n - 1) u' compareSizes cmp u'' v else compareSizes cmp u' =<< sizeSuc 1 v (CmpLeq, s1, s2) -> withUnView $ \ u v -> do unlessM (trivial u v) $ addConstraint $ ValueCmp CmpLeq size u v (CmpEq, s1, s2) -> continue cmp -- | Checked whether a size constraint is trivial (like @X <= X+1@). trivial :: Term -> Term -> TCM Bool trivial u v = do a@(e , n ) <- oldSizeExpr u b@(e', n') <- oldSizeExpr v let triv = e == e' && n <= n' -- Andreas, 2012-02-24 filtering out more trivial constraints fixes -- test/lib-succeed/SizeInconsistentMeta4.agda reportSDoc "tc.conv.size" 60 $ nest 2 $ sep [ if triv then text "trivial constraint" else empty , text (show a) <+> text "<=" , text (show b) ] return triv `catchError` \_ -> return False ------------------------------------------------------------------------ -- * Size constraints. ------------------------------------------------------------------------ -- | Test whether a problem consists only of size constraints. isSizeProblem :: ProblemId -> TCM Bool isSizeProblem pid = andM . map (isSizeConstraint . theConstraint) =<< getConstraintsForProblem pid -- | Test is a constraint speaks about sizes. isSizeConstraint :: Closure Constraint -> TCM Bool isSizeConstraint Closure{ clValue = ValueCmp _ s _ _ } = isJust <$> isSizeType s isSizeConstraint _ = return False -- | Take out all size constraints (DANGER!). takeSizeConstraints :: TCM [Closure Constraint] takeSizeConstraints = do test <- isSizeTypeTest let sizeConstraint :: Closure Constraint -> Bool sizeConstraint cl@Closure{ clValue = ValueCmp CmpLeq s _ _ } | isJust (test $ unEl s) = True sizeConstraint _ = False cs <- filter sizeConstraint . map theConstraint <$> getAllConstraints dropConstraints $ sizeConstraint . theConstraint return cs -- | Find the size constraints. getSizeConstraints :: TCM [Closure Constraint] getSizeConstraints = do test <- isSizeTypeTest let sizeConstraint :: Closure Constraint -> Bool sizeConstraint cl@Closure{ clValue = ValueCmp CmpLeq s _ _ } | isJust (test $ unEl s) = True sizeConstraint _ = False filter sizeConstraint . map theConstraint <$> getAllConstraints -- | Return a list of size metas and their context. getSizeMetas :: Bool -> TCM [(MetaId, Type, Telescope)] getSizeMetas interactionMetas = do test <- isSizeTypeTest catMaybes <$> do getOpenMetas >>= do mapM $ \ m -> do let no = return Nothing mi <- lookupMeta m case mvJudgement mi of HasType _ a -> do TelV tel b <- telView a -- b is reduced caseMaybe (test $ unEl b) no $ \ _ -> do let yes = return $ Just (m, a, tel) if interactionMetas then yes else do ifM (isJust <$> isInteractionMeta m) no yes _ -> no {- ROLLED BACK getSizeMetas :: TCM ([(MetaId, Int)], [SizeConstraint]) getSizeMetas = do ms <- getOpenMetas test <- isSizeTypeTest let sizeCon m = do let nothing = return ([], []) mi <- lookupMeta m case mvJudgement mi of HasType _ a -> do TelV tel b <- telView =<< instantiateFull a let noConstr = return ([(m, size tel)], []) case test b of Nothing -> nothing Just BoundedNo -> noConstr Just (BoundedLt u) -> noConstr {- WORKS NOT Just (BoundedLt u) -> flip catchError (const $ noConstr) $ do -- we assume the metavariable is used in an -- extension of its creation context ctxIds <- getContextId let a = SizeMeta m $ take (size tel) $ reverse ctxIds (b, n) <- oldSizeExpr u return ([(m, size tel)], [Leq a (n-1) b]) -} _ -> nothing (mss, css) <- unzip <$> mapM sizeCon ms return (concat mss, concat css) -} ------------------------------------------------------------------------ -- * Size constraint solving. ------------------------------------------------------------------------ -- | Atomic size expressions. data OldSizeExpr = SizeMeta MetaId [Int] -- ^ A size meta applied to de Bruijn indices. | Rigid Int -- ^ A de Bruijn index. deriving (Eq) instance Show OldSizeExpr where show (SizeMeta m _) = "X" ++ show (fromIntegral m :: Int) show (Rigid i) = "c" ++ show i -- | Size constraints we can solve. data OldSizeConstraint = Leq OldSizeExpr Int OldSizeExpr -- ^ @Leq a +n b@ represents @a =< b + n@. -- @Leq a -n b@ represents @a + n =< b@. instance Show OldSizeConstraint where show (Leq a n b) | n == 0 = show a ++ " =< " ++ show b | n > 0 = show a ++ " =< " ++ show b ++ " + " ++ show n | otherwise = show a ++ " + " ++ show (-n) ++ " =< " ++ show b -- | Compute a set of size constraints that all live in the same context -- from constraints over terms of type size that may live in different -- contexts. -- -- cf. 'Agda.TypeChecking.LevelConstraints.simplifyLevelConstraint' oldComputeSizeConstraints :: [Closure Constraint] -> TCM [OldSizeConstraint] oldComputeSizeConstraints [] = return [] -- special case to avoid maximum [] oldComputeSizeConstraints cs = catMaybes <$> mapM oldComputeSizeConstraint leqs where -- get the constraints plus contexts they are defined in gammas = map (envContext . clEnv) cs ls = map clValue cs -- compute the longest context (common water level) ns = map size gammas waterLevel = maximum ns -- lift all constraints to live in the longest context -- (assuming this context is an extension of the shorter ones) -- by raising the de Bruijn indices leqs = zipWith raise (map (waterLevel -) ns) ls -- | Turn a constraint over de Bruijn indices into a size constraint. oldComputeSizeConstraint :: Constraint -> TCM (Maybe OldSizeConstraint) oldComputeSizeConstraint c = case c of ValueCmp CmpLeq _ u v -> do reportSDoc "tc.size.solve" 50 $ sep [ text "converting size constraint" , prettyTCM c ] (a, n) <- oldSizeExpr u (b, m) <- oldSizeExpr v return $ Just $ Leq a (m - n) b `catchError` \ err -> case err of PatternErr{} -> return Nothing _ -> throwError err _ -> __IMPOSSIBLE__ -- | Turn a term with de Bruijn indices into a size expression with offset. -- -- Throws a 'patternViolation' if the term isn't a proper size expression. oldSizeExpr :: Term -> TCM (OldSizeExpr, Int) oldSizeExpr u = do u <- reduce u -- Andreas, 2009-02-09. -- This is necessary to surface the solutions of metavariables. reportSDoc "tc.conv.size" 60 $ text "oldSizeExpr:" <+> prettyTCM u s <- sizeView u case s of SizeInf -> patternViolation SizeSuc u -> mapSnd (+1) <$> oldSizeExpr u OtherSize u -> case ignoreSharing u of Var i [] -> return (Rigid i, 0) MetaV m es | Just xs <- mapM isVar es, fastDistinct xs -> return (SizeMeta m xs, 0) _ -> patternViolation where isVar (Proj{}) = Nothing isVar (Apply v) = case ignoreSharing $ unArg v of Var i [] -> Just i _ -> Nothing -- | Compute list of size metavariables with their arguments -- appearing in a constraint. flexibleVariables :: OldSizeConstraint -> [(MetaId, [Int])] flexibleVariables (Leq a _ b) = flex a ++ flex b where flex (Rigid _) = [] flex (SizeMeta m xs) = [(m, xs)] -- | Convert size constraint into form where each meta is applied -- to indices @0,1,..,n-1@ where @n@ is the arity of that meta. -- -- @X[σ] <= t@ becomes @X[id] <= t[σ^-1]@ -- -- @X[σ] ≤ Y[τ]@ becomes @X[id] ≤ Y[τ[σ^-1]]@ or @X[σ[τ^1]] ≤ Y[id]@ -- whichever is defined. If none is defined, we give up. -- oldCanonicalizeSizeConstraint :: OldSizeConstraint -> Maybe OldSizeConstraint oldCanonicalizeSizeConstraint c@(Leq a n b) = case (a,b) of (Rigid{}, Rigid{}) -> return c (SizeMeta m xs, Rigid i) -> do j <- List.findIndex (==i) xs return $ Leq (SizeMeta m [0..size xs-1]) n (Rigid j) (Rigid i, SizeMeta m xs) -> do j <- List.findIndex (==i) xs return $ Leq (Rigid j) n (SizeMeta m [0..size xs-1]) (SizeMeta m xs, SizeMeta l ys) -- try to invert xs on ys | Just ys' <- mapM (\ y -> List.findIndex (==y) xs) ys -> return $ Leq (SizeMeta m [0..size xs-1]) n (SizeMeta l ys') -- try to invert ys on xs | Just xs' <- mapM (\ x -> List.findIndex (==x) ys) xs -> return $ Leq (SizeMeta m xs') n (SizeMeta l [0..size ys-1]) -- give up | otherwise -> Nothing -- | Main function. -- Uses the old solver for size constraints using "Agda.Utils.Warshall". -- This solver does not smartly use size hypotheses @j : Size< i@. -- It only checks that its computed solution is compatible oldSolveSizeConstraints :: TCM () oldSolveSizeConstraints = whenM haveSizedTypes $ do reportSLn "tc.size.solve" 70 $ "Considering to solve size constraints" cs0 <- getSizeConstraints cs <- oldComputeSizeConstraints cs0 ms <- getSizeMetas True -- get all size metas, also interaction metas when (not (null cs) || not (null ms)) $ do reportSLn "tc.size.solve" 10 $ "Solving size constraints " ++ show cs cs <- return $ mapMaybe oldCanonicalizeSizeConstraint cs reportSLn "tc.size.solve" 10 $ "Canonicalized constraints: " ++ show cs let -- Error for giving up cannotSolve = typeError . GenericDocError =<< vcat (text "Cannot solve size constraints" : map prettyTCM cs0) -- Size metas in constraints. metas0 :: [(MetaId, Int)] -- meta id + arity metas0 = List.nub $ map (mapSnd length) $ concatMap flexibleVariables cs -- Unconstrained size metas that do not occur in constraints. metas1 :: [(MetaId, Int)] metas1 = forMaybe ms $ \ (m, _, tel) -> maybe (Just (m, size tel)) (const Nothing) $ lookup m metas0 -- All size metas metas = metas0 ++ metas1 reportSLn "tc.size.solve" 15 $ "Metas: " ++ show metas0 ++ ", " ++ show metas1 verboseS "tc.size.solve" 20 $ -- debug print the type of all size metas forM_ metas $ \ (m, _) -> reportSDoc "tc.size.solve" 20 $ prettyTCM =<< mvJudgement <$> lookupMeta m -- Run the solver. unlessM (oldSolver metas cs) cannotSolve -- Double-checking the solution. -- Andreas, 2012-09-19 -- The returned solution might not be consistent with -- the hypotheses on rigid vars (j : Size< i). -- Thus, we double check that all size constraints -- have been solved correctly. flip catchError (const cannotSolve) $ noConstraints $ forM_ cs0 $ \ cl -> enterClosure cl solveConstraint -- | Old solver for size constraints using "Agda.Utils.Warshall". -- This solver does not smartly use size hypotheses @j : Size< i@. oldSolver :: [(MetaId, Int)] -- ^ Size metas and their arity. -> [OldSizeConstraint] -- ^ Size constraints (in preprocessed form). -> TCM Bool -- ^ Returns @False@ if solver fails. oldSolver metas cs = do let cannotSolve = return False mkFlex (m, ar) = W.NewFlex (fromIntegral m) $ \ i -> fromIntegral i < ar mkConstr (Leq a n b) = W.Arc (mkNode a) n (mkNode b) mkNode (Rigid i) = W.Rigid $ W.RVar i mkNode (SizeMeta m _) = W.Flex $ fromIntegral m -- run the Warshall solver case W.solve $ map mkFlex metas ++ map mkConstr cs of Nothing -> cannotSolve Just sol -> do reportSLn "tc.size.solve" 10 $ "Solved constraints: " ++ show sol suc <- primSizeSuc infty <- primSizeInf let plus v 0 = v plus v n = suc `apply1` plus v (n - 1) inst (i, e) = do let m = fromIntegral i -- meta variable identifier ar = fromMaybe __IMPOSSIBLE__ $ lookup m metas -- meta var arity term (W.SizeConst W.Infinite) = infty term (W.SizeVar j n) | j < ar = plus (var $ ar - j - 1) n term _ = __IMPOSSIBLE__ tel = replicate ar $ defaultArg "s" -- convert size expression to term v = term e reportSDoc "tc.size.solve" 20 $ sep [ pretty m <+> text ":=" , nest 2 $ prettyTCM v ] -- Andreas, 2012-09-25: do not assign interaction metas to \infty let isInf (W.SizeConst W.Infinite) = True isInf _ = False unlessM (((isInf e &&) . isJust <$> isInteractionMeta m) `or2M` isFrozen m) $ assignTerm m tel v mapM_ inst $ Map.toList sol return True Agda-2.5.3/src/full/Agda/TypeChecking/MetaVars.hs-boot0000644000000000000000000000201413154613124020521 0ustar0000000000000000module Agda.TypeChecking.MetaVars where import Agda.Syntax.Common ( Arg, Dom ) import Agda.Syntax.Internal ( MetaId, Term, Type, Args, Abs, Telescope ) import Agda.Syntax.Internal.Generic ( TermLike ) import Agda.TypeChecking.Monad.Base ( TCM, RunMetaOccursCheck(..), CompareDirection(..), Candidate ) type Condition = Dom Type -> Abs Type -> Bool newArgsMeta' :: Condition -> Type -> TCM Args newArgsMeta :: Type -> TCM Args assignTerm :: MetaId -> [Arg String] -> Term -> TCM () etaExpandMetaSafe :: MetaId -> TCM () assignV :: CompareDirection -> MetaId -> Args -> Term -> TCM () assign :: CompareDirection -> MetaId -> Args -> Term -> TCM () newIFSMeta :: String -> Type -> TCM (MetaId, Term) newValueMeta :: RunMetaOccursCheck -> Type -> TCM (MetaId, Term) newNamedValueMeta :: RunMetaOccursCheck -> String -> Type -> TCM (MetaId, Term) newNamedValueMeta':: RunMetaOccursCheck -> String -> Type -> TCM (MetaId, Term) newTelMeta :: Telescope -> TCM Args Agda-2.5.3/src/full/Agda/TypeChecking/Polarity.hs0000644000000000000000000003733113154613124017653 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Polarity where import Control.Applicative import Control.Monad.State import Data.Maybe import Data.Traversable (traverse) import Agda.Syntax.Abstract.Name import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.TypeChecking.Monad import Agda.TypeChecking.Pretty import Agda.TypeChecking.SizedTypes import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Reduce import Agda.TypeChecking.Free hiding (Occurrence(..)) import Agda.TypeChecking.Positivity.Occurrence import Agda.Interaction.Options import Agda.Utils.List import Agda.Utils.Maybe ( whenNothingM ) import Agda.Utils.Monad import Agda.Utils.Permutation import Agda.Utils.Pretty ( prettyShow ) import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible ------------------------------------------------------------------------ -- * Polarity lattice. ------------------------------------------------------------------------ -- | Infimum on the information lattice. -- 'Invariant' is bottom (dominant for inf), -- 'Nonvariant' is top (neutral for inf). (/\) :: Polarity -> Polarity -> Polarity Nonvariant /\ b = b a /\ Nonvariant = a a /\ b | a == b = a | otherwise = Invariant -- | 'Polarity' negation, swapping monotone and antitone. neg :: Polarity -> Polarity neg Covariant = Contravariant neg Contravariant = Covariant neg Invariant = Invariant neg Nonvariant = Nonvariant -- | What is the polarity of a function composition? composePol :: Polarity -> Polarity -> Polarity composePol Nonvariant _ = Nonvariant composePol _ Nonvariant = Nonvariant composePol Invariant _ = Invariant composePol Covariant x = x composePol Contravariant x = neg x polFromOcc :: Occurrence -> Polarity polFromOcc o = case o of GuardPos -> Covariant StrictPos -> Covariant JustPos -> Covariant JustNeg -> Contravariant Mixed -> Invariant Unused -> Nonvariant ------------------------------------------------------------------------ -- * Auxiliary functions ------------------------------------------------------------------------ -- | Get the next polarity from a list, 'Invariant' if empty. nextPolarity :: [Polarity] -> (Polarity, [Polarity]) nextPolarity [] = (Invariant, []) nextPolarity (p : ps) = (p, ps) -- | Replace 'Nonvariant' by 'Covariant'. -- (Arbitrary bias, but better than 'Invariant', see issue 1596). purgeNonvariant :: [Polarity] -> [Polarity] purgeNonvariant = map (\ p -> if p == Nonvariant then Covariant else p) -- | A quick transliterations of occurrences to polarities. polarityFromPositivity :: QName -> TCM () polarityFromPositivity x = inConcreteOrAbstractMode x $ \ def -> do -- Get basic polarity from positivity analysis. let npars = droppedPars def let pol0 = replicate npars Nonvariant ++ map polFromOcc (defArgOccurrences def) reportSLn "tc.polarity.set" 15 $ "Polarity of " ++ prettyShow x ++ " from positivity: " ++ prettyShow pol0 -- set the polarity in the signature (not the final polarity, though) setPolarity x $ drop npars pol0 ------------------------------------------------------------------------ -- * Computing the polarity of a symbol. ------------------------------------------------------------------------ -- | Main function of this module. computePolarity :: [QName] -> TCM () computePolarity xs = do -- Andreas, 2017-04-26, issue #2554 -- First, for mutual definitions, obtain a crude polarity from positivity. when (length xs >= 2) $ mapM_ polarityFromPositivity xs -- Then, refine it. forM_ xs $ \ x -> inConcreteOrAbstractMode x $ \ def -> do reportSLn "tc.polarity.set" 25 $ "Refining polarity of " ++ prettyShow x -- Again: get basic polarity from positivity analysis. let npars = droppedPars def let pol0 = replicate npars Nonvariant ++ map polFromOcc (defArgOccurrences def) reportSLn "tc.polarity.set" 15 $ "Polarity of " ++ prettyShow x ++ " from positivity: " ++ prettyShow pol0 {- -- get basic polarity from shape of def (arguments matched on or not?) def <- getConstInfo x let usagePol = usagePolarity $ theDef def reportSLn "tc.polarity.set" 15 $ "Polarity of " ++ prettyShow x ++ " from definition form: " ++ prettyShow usagePol let n = genericLength usagePol -- n <- getArity x reportSLn "tc.polarity.set" 20 $ " arity = " ++ show n -- refine polarity by positivity information pol0 <- zipWith (/\) usagePol <$> mapM getPol [0..n - 1] reportSLn "tc.polarity.set" 15 $ "Polarity of " ++ prettyShow x ++ " from positivity: " ++ prettyShow pol0 -} -- compute polarity of sized types pol1 <- sizePolarity x pol0 -- refine polarity again by using type information let t = defType def -- Instantiation takes place in Rules.Decl.instantiateDefinitionType -- t <- instantiateFull t -- Andreas, 2014-04-11 Issue 1099: needed for -- -- variable occurrence test in dependentPolarity. reportSDoc "tc.polarity.set" 15 $ text "Refining polarity with type " <+> prettyTCM t reportSDoc "tc.polarity.set" 60 $ text "Refining polarity with type (raw): " <+> (text .show) t pol <- dependentPolarity t (enablePhantomTypes (theDef def) pol1) pol1 reportSLn "tc.polarity.set" 10 $ "Polarity of " ++ prettyShow x ++ ": " ++ prettyShow pol -- set the polarity in the signature setPolarity x $ drop npars pol -- purgeNonvariant pol -- temporarily disable non-variance -- | Data and record parameters are used as phantom arguments all over -- the test suite (and possibly in user developments). -- @enablePhantomTypes@ turns 'Nonvariant' parameters to 'Covariant' -- to enable phantoms. enablePhantomTypes :: Defn -> [Polarity] -> [Polarity] enablePhantomTypes def pol = case def of Datatype{ dataPars = np } -> enable np Record { recPars = np } -> enable np _ -> pol where enable np = let (pars, rest) = splitAt np pol in purgeNonvariant pars ++ rest {- UNUSED -- | Extract a basic approximate polarity info from the shape of definition. -- Arguments that are matched against get 'Invariant', others 'Nonvariant'. -- For data types, parameters get 'Nonvariant', indices 'Invariant'. usagePolarity :: Defn -> [Polarity] usagePolarity def = case def of Axiom{} -> [] Function{ funClauses = [] } -> [] Function{ funClauses = cs } -> usage $ map namedClausePats cs Datatype{ dataPars = np, dataIxs = ni } -> genericReplicate np Nonvariant Record{ recPars = n } -> genericReplicate n Nonvariant Constructor{} -> [] Primitive{} -> [] where usage = foldr1 (zipWith (/\)) . map (map (usagePat . namedArg)) usagePat VarP{} = Nonvariant usagePat DotP{} = Nonvariant usagePat ConP{} = Invariant usagePat LitP{} = Invariant -} -- | Make arguments 'Invariant' if the type of a not-'Nonvariant' -- later argument depends on it. -- Also, enable phantom types by turning 'Nonvariant' into something -- else if it is a data/record parameter but not a size argument. [See issue 1596] -- -- Precondition: the "phantom" polarity list has the same length as the polarity list. dependentPolarity :: Type -> [Polarity] -> [Polarity] -> TCM [Polarity] dependentPolarity t _ [] = return [] -- all remaining are 'Invariant' dependentPolarity t [] (_ : _) = __IMPOSSIBLE__ dependentPolarity t (q:qs) pols@(p:ps) = do t <- reduce $ unEl t reportSDoc "tc.polarity.dep" 20 $ text "dependentPolarity t = " <+> prettyTCM t reportSDoc "tc.polarity.dep" 70 $ text "dependentPolarity t = " <+> (text . show) t case ignoreSharing t of Pi dom b -> do ps <- underAbstraction dom b $ \ c -> dependentPolarity c qs ps let fallback = ifM (isJust <$> isSizeType (unDom dom)) (return p) (return q) p <- case b of Abs{} | p /= Invariant -> -- Andreas, 2014-04-11 see Issue 1099 -- Free variable analysis is not in the monad, -- hence metas must have been instantiated before! ifM (relevantInIgnoringNonvariant 0 (absBody b) ps) {- then -} (return Invariant) {- else -} fallback _ -> fallback return $ p : ps _ -> return pols -- | Check whether a variable is relevant in a type expression, -- ignoring domains of non-variant arguments. relevantInIgnoringNonvariant :: Nat -> Type -> [Polarity] -> TCM Bool relevantInIgnoringNonvariant i t [] = return $ i `relevantInIgnoringSortAnn` t relevantInIgnoringNonvariant i t (p:ps) = do t <- reduce $ unEl t case ignoreSharing t of Pi a b -> if p /= Nonvariant && i `relevantInIgnoringSortAnn` a then return True else relevantInIgnoringNonvariant (i + 1) (absBody b) ps _ -> return $ i `relevantInIgnoringSortAnn` t ------------------------------------------------------------------------ -- * Sized types ------------------------------------------------------------------------ -- | Hack for polarity of size indices. -- As a side effect, this sets the positivity of the size index. -- See test/succeed/PolaritySizeSucData.agda for a case where this is needed. sizePolarity :: QName -> [Polarity] -> TCM [Polarity] sizePolarity d pol0 = do let exit = return pol0 ifM (not . optSizedTypes <$> pragmaOptions) exit $ do def <- getConstInfo d case theDef def of Datatype{ dataPars = np, dataCons = cons } -> do let TelV tel _ = telView' $ defType def (parTel, ixTel) = splitAt np $ telToList tel case ixTel of [] -> exit -- No size index Dom _ (_, a) : _ -> ifM ((/= Just BoundedNo) <$> isSizeType a) exit $ do -- we assume the size index to be 'Covariant' ... let pol = take np pol0 polCo = pol ++ [Covariant] polIn = pol ++ [Invariant] setPolarity d $ polCo -- and seek confirm it by looking at the constructor types let check c = do t <- defType <$> getConstInfo c addContext (telFromList parTel) $ do let pars = map (defaultArg . var) $ downFrom np TelV conTel target <- telView =<< (t `piApplyM` pars) case conTel of EmptyTel -> return False -- no size argument ExtendTel arg tel -> ifM ((/= Just BoundedNo) <$> isSizeType (unDom arg)) (return False) $ do -- also no size argument -- First constructor argument has type Size -- check that only positive occurences in tel let isPos = underAbstraction arg tel $ \ tel -> do pols <- zipWithM polarity [0..] $ map (snd . unDom) $ telToList tel reportSDoc "tc.polarity.size" 25 $ text $ "to pass size polarity check, the following polarities need all to be covariant: " ++ prettyShow pols return $ all (`elem` [Nonvariant, Covariant]) pols -- check that the size argument appears in the -- right spot in the target type let sizeArg = size tel isLin = addContext conTel $ checkSizeIndex d np sizeArg target ok <- isPos `and2M` isLin reportSDoc "tc.polarity.size" 15 $ text "constructor" <+> prettyTCM c <+> text (if ok then "passes" else "fails") <+> text "size polarity check" return ok ifNotM (andM $ map check cons) (return polIn) -- no, does not conform to the rules of sized types $ do -- yes, we have a sized type here -- Andreas, 2015-07-01 -- As a side effect, mark the size also covariant for subsequent -- positivity checking (which feeds back into polarity analysis). modifyArgOccurrences d $ \ occ -> take np occ ++ [JustPos] return polCo _ -> exit -- | @checkSizeIndex d np i a@ checks that constructor target type @a@ -- has form @d ps (↑ⁿ i) idxs@ where @|ps| = np@. -- -- Precondition: @a@ is reduced and of form @d ps idxs0@. checkSizeIndex :: QName -> Nat -> Nat -> Type -> TCM Bool checkSizeIndex d np i a = do reportSDoc "tc.polarity.size" 15 $ withShowAllArguments $ vcat [ text "checking that constructor target type " <+> prettyTCM a , text " is data type " <+> prettyTCM d , text " and has size index (successor(s) of) " <+> prettyTCM (var i) ] case ignoreSharing $ unEl a of Def d0 es -> do whenNothingM (sameDef d d0) __IMPOSSIBLE__ s <- deepSizeView $ unArg ix case s of DSizeVar j _ | i == j -> return $ not $ freeIn i (pars ++ ixs) _ -> return False where (pars, Apply ix : ixs) = splitAt np es _ -> __IMPOSSIBLE__ -- | @polarities i a@ computes the list of polarities of de Bruijn index @i@ -- in syntactic entity @a@. class HasPolarity a where polarities :: Nat -> a -> TCM [Polarity] -- | @polarity i a@ computes the polarity of de Bruijn index @i@ -- in syntactic entity @a@ by taking the infimum of all 'polarities'. polarity :: HasPolarity a => Nat -> a -> TCM Polarity polarity i x = do ps <- polarities i x case ps of [] -> return Nonvariant ps -> return $ foldr1 (/\) ps instance HasPolarity a => HasPolarity (Arg a) where polarities i = polarities i . unArg instance HasPolarity a => HasPolarity (Dom a) where polarities i = polarities i . unDom instance HasPolarity a => HasPolarity (Abs a) where polarities i (Abs _ b) = polarities (i + 1) b polarities i (NoAbs _ v) = polarities i v instance HasPolarity a => HasPolarity [a] where polarities i xs = concat <$> mapM (polarities i) xs instance (HasPolarity a, HasPolarity b) => HasPolarity (a, b) where polarities i (x, y) = (++) <$> polarities i x <*> polarities i y instance HasPolarity Type where polarities i (El _ v) = polarities i v instance HasPolarity a => HasPolarity (Elim' a) where polarities i Proj{} = return [] polarities i (Apply a) = polarities i a instance HasPolarity Term where polarities i v = do v <- instantiate v case v of -- Andreas, 2012-09-06: taking the polarities of the arguments -- without taking the variance of the function into account seems wrong. Var n ts | n == i -> (Covariant :) . map (const Invariant) <$> polarities i ts | otherwise -> map (const Invariant) <$> polarities i ts Lam _ t -> polarities i t Lit _ -> return [] Level l -> polarities i l Def x ts -> do pols <- getPolarity x let compose p ps = map (composePol p) ps concat . zipWith compose (pols ++ repeat Invariant) <$> mapM (polarities i) ts Con _ _ ts -> polarities i ts -- constructors can be seen as monotone in all args. Pi a b -> (++) <$> (map neg <$> polarities i a) <*> polarities i b Sort s -> return [] -- polarities i s -- return [] MetaV _ ts -> map (const Invariant) <$> polarities i ts Shared p -> polarities i $ derefPtr p DontCare t -> polarities i t -- return [] instance HasPolarity Level where polarities i (Max as) = polarities i as instance HasPolarity PlusLevel where polarities i ClosedLevel{} = return [] polarities i (Plus _ l) = polarities i l instance HasPolarity LevelAtom where polarities i l = case l of MetaLevel _ vs -> map (const Invariant) <$> polarities i vs BlockedLevel _ v -> polarities i v NeutralLevel _ v -> polarities i v UnreducedLevel v -> polarities i v Agda-2.5.3/src/full/Agda/TypeChecking/Warnings.hs0000644000000000000000000000753413154613124017642 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Warnings where import qualified Data.List as List import Agda.TypeChecking.Monad.Base import {-# SOURCE #-} Agda.TypeChecking.Errors import {-# SOURCE #-} Agda.TypeChecking.Pretty import Agda.Syntax.Position import Agda.Syntax.Parser import Agda.Interaction.Options import Agda.Utils.Lens import qualified Agda.Utils.Pretty as P import Agda.Utils.Except #if __GLASGOW_HASKELL__ <= 708 import Control.Applicative ((<$>)) #endif {-# SPECIALIZE genericWarning :: P.Doc -> TCM () #-} genericWarning :: MonadTCM tcm => P.Doc -> tcm () genericWarning = warning . GenericWarning {-# SPECIALIZE genericNonFatalError :: P.Doc -> TCM () #-} genericNonFatalError :: MonadTCM tcm => P.Doc -> tcm () genericNonFatalError = warning . GenericNonFatalError {-# SPECIALIZE warning_ :: Warning -> TCM TCWarning #-} warning_ :: MonadTCM tcm => Warning -> tcm TCWarning warning_ w = do r <- view eRange c <- view eCall -- NicifierIssues print their own error locations in their list of -- issues (but we might need to keep the overall range `r` for -- comparing ranges) let r' = case w of { NicifierIssue{} -> NoRange ; _ -> r } p <- liftTCM $ sayWhen r' c $ prettyWarning w liftTCM $ return $ TCWarning r w p {-# SPECIALIZE warning :: Warning -> TCM () #-} warning :: MonadTCM tcm => Warning -> tcm () warning w = do tcwarn <- warning_ w wmode <- optWarningMode <$> pragmaOptions case wmode of IgnoreAllWarnings -> case classifyWarning w of -- not allowed to ignore non-fatal errors ErrorWarnings -> raiseWarning tcwarn AllWarnings -> return () TurnIntoErrors -> typeError $ NonFatalErrors [tcwarn] LeaveAlone -> raiseWarning tcwarn where raiseWarning tcw = stTCWarnings %= (tcw :) -- | Classifying warnings: some are benign, others are (non-fatal) errors data WhichWarnings = ErrorWarnings -- ^ warnings that will be turned into errors | AllWarnings -- ^ all warnings, including errors and benign ones -- Note: order of constructors is important for the derived Ord instance deriving (Eq, Ord) isUnsolvedWarning :: Warning -> Bool isUnsolvedWarning w = case w of UnsolvedMetaVariables{} -> True UnsolvedInteractionMetas{} -> True UnsolvedConstraints{} -> True -- rest _ -> False classifyWarning :: Warning -> WhichWarnings classifyWarning w = case w of OldBuiltin{} -> AllWarnings EmptyRewritePragma -> AllWarnings UselessPublic -> AllWarnings UnreachableClauses{} -> AllWarnings UselessInline{} -> AllWarnings GenericWarning{} -> AllWarnings DeprecationWarning{} -> AllWarnings NicifierIssue{} -> AllWarnings TerminationIssue{} -> ErrorWarnings CoverageIssue{} -> ErrorWarnings CoverageNoExactSplit{} -> ErrorWarnings NotStrictlyPositive{} -> ErrorWarnings UnsolvedMetaVariables{} -> ErrorWarnings UnsolvedInteractionMetas{} -> ErrorWarnings UnsolvedConstraints{} -> ErrorWarnings GenericNonFatalError{} -> ErrorWarnings SafeFlagPostulate{} -> ErrorWarnings SafeFlagPragma{} -> ErrorWarnings SafeFlagNonTerminating -> ErrorWarnings SafeFlagTerminating -> ErrorWarnings SafeFlagPrimTrustMe -> ErrorWarnings SafeFlagNoPositivityCheck -> ErrorWarnings SafeFlagPolarity -> ErrorWarnings ParseWarning{} -> ErrorWarnings classifyWarnings :: [TCWarning] -> ([TCWarning], [TCWarning]) classifyWarnings = List.partition $ (< AllWarnings) . classifyWarning . tcWarning -- | running the Parse monad runPM :: PM a -> TCM a runPM m = do (res, ws) <- runPMIO m mapM_ (warning . ParseWarning) ws case res of Left e -> throwError (Exception (getRange e) (P.pretty e)) Right a -> return a Agda-2.5.3/src/full/Agda/TypeChecking/Constraints.hs0000644000000000000000000002357113154613124020360 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.Constraints where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad import Control.Monad.Reader import Control.Monad.State import Control.Monad.Trans.Maybe import qualified Data.List as List import qualified Data.Set as Set import Agda.Syntax.Internal import Agda.TypeChecking.Free import Agda.TypeChecking.Monad import Agda.TypeChecking.InstanceArguments import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.LevelConstraints import Agda.TypeChecking.SizedTypes import Agda.TypeChecking.MetaVars.Mention import Agda.TypeChecking.Warnings import {-# SOURCE #-} Agda.TypeChecking.Rules.Term import {-# SOURCE #-} Agda.TypeChecking.Conversion import {-# SOURCE #-} Agda.TypeChecking.MetaVars import {-# SOURCE #-} Agda.TypeChecking.Empty import Agda.Utils.Except ( MonadError(throwError) ) import Agda.Utils.Functor import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Pretty (prettyShow) import Agda.Utils.Lens import Agda.Utils.Size import qualified Agda.Utils.VarSet as VarSet #include "undefined.h" import Agda.Utils.Impossible -- | Catches pattern violation errors and adds a constraint. -- catchConstraint :: Constraint -> TCM () -> TCM () catchConstraint c v = liftTCM $ catchError_ v $ \err -> case err of -- Not putting s (which should really be the what's already there) makes things go -- a lot slower (+20% total time on standard library). How is that possible?? -- The problem is most likely that there are internal catchErrors which forgets the -- state. catchError should preserve the state on pattern violations. PatternErr{} -> addConstraint c _ -> throwError err addConstraint :: Constraint -> TCM () addConstraint c = do pids <- asks envActiveProblems reportSDoc "tc.constr.add" 20 $ hsep [ text "adding constraint" , text (show $ Set.toList pids) , prettyTCM c ] -- Need to reduce to reveal possibly blocking metas c <- reduce =<< instantiateFull c c' <- simpl c if (c /= c') then do reportSDoc "tc.constr.add" 20 $ text " simplified:" <+> prettyTCM c' solveConstraint_ c' else addConstraint' c' -- the added constraint can cause IFS constraints to be solved (but only -- the constraints which aren’t blocked on an uninstantiated meta) unless (isIFSConstraint c) $ wakeConstraints (isWakeableIFSConstraint . clValue . theConstraint) where isWakeableIFSConstraint :: Constraint -> TCM Bool isWakeableIFSConstraint (FindInScope _ b _) = caseMaybe b (return True) (\m -> isInstantiatedMeta m) isWakeableIFSConstraint _ = return False isIFSConstraint :: Constraint -> Bool isIFSConstraint FindInScope{} = True isIFSConstraint _ = False isLvl LevelCmp{} = True isLvl _ = False -- Try to simplify a level constraint simpl :: Constraint -> TCM Constraint simpl c = if not $ isLvl c then return c else do cs <- map theConstraint <$> getAllConstraints lvls <- instantiateFull $ List.filter (isLvl . clValue) cs when (not $ null lvls) $ do reportSDoc "tc.constr.lvl" 40 $ text "simplifying level constraint" <+> prettyTCM c $$ nest 2 (hang (text "using") 2 (prettyTCM lvls)) let c' = simplifyLevelConstraint c $ map clValue lvls reportSDoc "tc.constr.lvl" 40 $ if c' /= c then text "simplified to" <+> prettyTCM c' else text "no simplification" return c' -- | Don't allow the argument to produce any constraints. noConstraints :: TCM a -> TCM a noConstraints problem = liftTCM $ do (pid, x) <- newProblem problem cs <- getConstraintsForProblem pid w <- warning_ (UnsolvedConstraints cs) unless (null cs) $ typeError $ NonFatalErrors [ w ] return x -- | Create a fresh problem for the given action. newProblem :: TCM a -> TCM (ProblemId, a) newProblem action = do pid <- fresh -- Don't get distracted by other constraints while working on the problem x <- nowSolvingConstraints $ solvingProblem pid action -- Now we can check any woken constraints solveAwakeConstraints return (pid, x) newProblem_ :: TCM () -> TCM ProblemId newProblem_ action = fst <$> newProblem action ifNoConstraints :: TCM a -> (a -> TCM b) -> (ProblemId -> a -> TCM b) -> TCM b ifNoConstraints check ifNo ifCs = do (pid, x) <- newProblem check ifM (isProblemSolved pid) (ifNo x) (ifCs pid x) ifNoConstraints_ :: TCM () -> TCM a -> (ProblemId -> TCM a) -> TCM a ifNoConstraints_ check ifNo ifCs = ifNoConstraints check (const ifNo) (\pid _ -> ifCs pid) -- | @guardConstraint c blocker@ tries to solve @blocker@ first. -- If successful without constraints, it moves on to solve @c@, otherwise it -- adds a @Guarded c cs@ constraint -- to the @blocker@-generated constraints @cs@. guardConstraint :: Constraint -> TCM () -> TCM () guardConstraint c blocker = ifNoConstraints_ blocker (solveConstraint_ c) (addConstraint . Guarded c) whenConstraints :: TCM () -> TCM () -> TCM () whenConstraints action handler = ifNoConstraints_ action (return ()) $ \pid -> do stealConstraints pid handler -- | Wake up the constraints depending on the given meta. wakeupConstraints :: MetaId -> TCM () wakeupConstraints x = do wakeConstraints (return . mentionsMeta x) solveAwakeConstraints -- | Wake up all constraints. wakeupConstraints_ :: TCM () wakeupConstraints_ = do wakeConstraints (return . const True) solveAwakeConstraints solveAwakeConstraints :: TCM () solveAwakeConstraints = solveAwakeConstraints' False solveAwakeConstraints' :: Bool -> TCM () solveAwakeConstraints' force = do verboseS "profile.constraints" 10 $ liftTCM $ tickMax "max-open-constraints" . List.genericLength =<< getAllConstraints whenM ((force ||) . not <$> isSolvingConstraints) $ nowSolvingConstraints $ do -- solveSizeConstraints -- Andreas, 2012-09-27 attacks size constrs too early -- Ulf, 2016-12-06: Don't inherit problems here! Stored constraints -- already contain all their dependencies. locally eActiveProblems (const Set.empty) solve where solve = do reportSDoc "tc.constr.solve" 10 $ hsep [ text "Solving awake constraints." , text . show . length =<< getAwakeConstraints , text "remaining." ] whenJustM takeAwakeConstraint $ \ c -> do withConstraint solveConstraint c solve solveConstraint :: Constraint -> TCM () solveConstraint c = do verboseS "profile.constraints" 10 $ liftTCM $ tick "attempted-constraints" verboseBracket "tc.constr.solve" 20 "solving constraint" $ do pids <- asks envActiveProblems reportSDoc "tc.constr.solve" 20 $ text (show $ Set.toList pids) <+> prettyTCM c solveConstraint_ c solveConstraint_ :: Constraint -> TCM () solveConstraint_ (ValueCmp cmp a u v) = compareTerm cmp a u v solveConstraint_ (ElimCmp cmp a e u v) = compareElims cmp a e u v solveConstraint_ (TypeCmp cmp a b) = compareType cmp a b solveConstraint_ (TelCmp a b cmp tela telb) = compareTel a b cmp tela telb solveConstraint_ (SortCmp cmp s1 s2) = compareSort cmp s1 s2 solveConstraint_ (LevelCmp cmp a b) = compareLevel cmp a b solveConstraint_ c0@(Guarded c pid) = do ifM (isProblemSolved pid) {-then-} (do reportSLn "tc.constr.solve" 50 $ "Guarding problem " ++ show pid ++ " is solved, moving on..." solveConstraint_ c) {-else-} $ do reportSLn "tc.constr.solve" 50 $ "Guarding problem " ++ show pid ++ " is still unsolved." addConstraint c0 solveConstraint_ (IsEmpty r t) = isEmptyType r t solveConstraint_ (CheckSizeLtSat t) = checkSizeLtSat t solveConstraint_ (UnBlock m) = ifM (isFrozen m) (addConstraint $ UnBlock m) $ do inst <- mvInstantiation <$> lookupMeta m reportSDoc "tc.constr.unblock" 15 $ text ("unblocking a metavar yields the constraint: " ++ show inst) case inst of BlockedConst t -> do reportSDoc "tc.constr.blocked" 15 $ text ("blocked const " ++ prettyShow m ++ " :=") <+> prettyTCM t assignTerm m [] t PostponedTypeCheckingProblem cl unblock -> enterClosure cl $ \prob -> do ifNotM unblock (addConstraint $ UnBlock m) $ do tel <- getContextTelescope v <- liftTCM $ checkTypeCheckingProblem prob assignTerm m (telToArgs tel) v -- Andreas, 2009-02-09, the following were IMPOSSIBLE cases -- somehow they pop up in the context of sized types -- -- already solved metavariables: should only happen for size -- metas (not sure why it does, Andreas?) -- Andreas, 2017-07-11: -- I think this is because the size solver instantiates -- some metas with infinity but does not clean up the UnBlock constraints. -- See also issue #2637. InstV{} -> return () -- Open (whatever that means) Open -> __IMPOSSIBLE__ OpenIFS -> __IMPOSSIBLE__ solveConstraint_ (FindInScope m b cands) = findInScope m cands checkTypeCheckingProblem :: TypeCheckingProblem -> TCM Term checkTypeCheckingProblem p = case p of CheckExpr e t -> checkExpr e t CheckArgs eh r args t0 t1 k -> checkArguments' eh r args t0 t1 k CheckLambda args body target -> checkPostponedLambda args body target UnquoteTactic tac hole t -> unquoteTactic tac hole t $ return hole debugConstraints :: TCM () debugConstraints = verboseS "tc.constr" 50 $ do awake <- use stAwakeConstraints sleeping <- use stSleepingConstraints reportSDoc "" 0 $ vcat [ text "Current constraints" , nest 2 $ vcat [ text "awake " <+> vcat (map prettyTCM awake) , text "asleep" <+> vcat (map prettyTCM sleeping) ] ] Agda-2.5.3/src/full/Agda/TypeChecking/DisplayForm.hs0000644000000000000000000002160713154613124020300 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE UndecidableInstances #-} -- for Arg a => Elim' a -- | Tools for 'DisplayTerm' and 'DisplayForm'. module Agda.TypeChecking.DisplayForm where import Prelude hiding (all) import Control.Applicative import Control.Monad import Control.Monad.Trans (lift) import Control.Monad.Trans.Maybe import Data.Foldable (all) import qualified Data.Set as Set import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Names import Agda.Syntax.Scope.Base (inverseScopeLookupName) import Agda.TypeChecking.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Level import Agda.TypeChecking.Reduce (instantiate) import Agda.Utils.Except import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Pretty ( prettyShow ) #include "undefined.h" import Agda.Utils.Impossible -- | Convert a 'DisplayTerm' into a 'Term'. dtermToTerm :: DisplayTerm -> Term dtermToTerm dt = case dt of DWithApp d ds es -> dtermToTerm d `apply` map (defaultArg . dtermToTerm) ds `applyE` es DCon c ci args -> Con c ci $ map (fmap dtermToTerm) args DDef f es -> Def f $ map (fmap dtermToTerm) es DDot v -> v DTerm v -> v -- | Get the arities of all display forms for a name. displayFormArities :: QName -> TCM [Int] displayFormArities q = map (length . dfPats . dget) <$> getDisplayForms q -- | Find a matching display form for @q es@. -- In essence this tries to rewrite @q es@ with any -- display form @q ps --> dt@ and returns the instantiated -- @dt@ if successful. First match wins. displayForm :: QName -> Elims -> TCM (Maybe DisplayTerm) displayForm q es = do -- Get display forms for name q. odfs <- getDisplayForms q `catchError` \_ -> return [] -- Display debug info about the @Open@s. unless (null odfs) $ verboseS "tc.display.top" 100 $ do n <- getContextId reportSLn "tc.display.top" 100 $ "displayForm for " ++ prettyShow q ++ ": context = " ++ show n ++ ", dfs = " ++ show odfs -- Use only the display forms that can be opened in the current context. dfs <- catMaybes <$> mapM getLocal odfs scope <- getScope -- Keep the display forms that match the application @q es@. ms <- do ms <- mapM (runMaybeT . (`matchDisplayForm` es)) dfs return [ m | Just (d, m) <- ms, wellScoped scope d ] -- Not safe when printing non-terminating terms. -- (nfdfs, us) <- normalise (dfs, es) unless (null odfs) $ reportSLn "tc.display.top" 100 $ unlines [ "name : " ++ prettyShow q , "displayForms: " ++ show dfs , "arguments : " ++ show es , "matches : " ++ show ms , "result : " ++ show (headMaybe ms) ] -- Return the first display form that matches. return $ headMaybe ms -- Andreas, 2014-06-11: The following error swallowing -- is potentially harmful, making debugging harder. -- I removed it, and it does not cause problems on the test suite. -- `catchError` \_ -> return Nothing where -- Look at the original display form, not the instantiated result when -- checking if it's well-scoped. Otherwise we might pick up out of scope -- identifiers coming from the source term. wellScoped scope (Display _ _ d) | isWithDisplay d = True | otherwise = all (inScope scope) $ namesIn d inScope scope x = not $ null $ inverseScopeLookupName x scope isWithDisplay DWithApp{} = True isWithDisplay _ = False -- | Match a 'DisplayForm' @q ps = v@ against @q es@. -- Return the 'DisplayTerm' @v[us]@ if the match was successful, -- i.e., @es / ps = Just us@. matchDisplayForm :: DisplayForm -> Elims -> MaybeT TCM (DisplayForm, DisplayTerm) matchDisplayForm d@(Display _ ps v) es | length ps > length es = mzero | otherwise = do let (es0, es1) = splitAt (length ps) es us <- reverse <$> do match ps $ raise 1 es0 return (d, substWithOrigin (parallelS $ map woThing us) us v `applyE` es1) -- | Class @Match@ for matching a term @p@ in the role of a pattern -- against a term @v@. -- -- The 0th variable in @p@ plays the role -- of a place holder (pattern variable). Each occurrence of -- @var 0@ in @p@ stands for a different pattern variable. -- -- The result of matching, if successful, is a list of solutions for the -- pattern variables, in left-to-right order. -- -- The 0th variable is in scope in the input @v@, but should not -- actually occur! -- In the output solution, the @0th@ variable is no longer in scope. -- (It has been substituted by __IMPOSSIBLE__ which corresponds to -- a raise by -1). class Match a where match :: a -> a -> MaybeT TCM [WithOrigin Term] instance Match a => Match [a] where match xs ys = concat <$> zipWithM match xs ys instance Match a => Match (Arg a) where match p v = map (setOrigin (getOrigin v)) <$> match (unArg p) (unArg v) instance Match a => Match (Elim' a) where match p v = case (p, v) of (Proj _ f, Proj _ f') | f == f' -> return [] (Apply a, Apply a') -> match a a' _ -> mzero instance Match Term where match p v = lift (instantiate v) >>= \ v -> case (ignoreSharing p, ignoreSharing v) of (Var 0 [], v) -> return [ WithOrigin Inserted $ strengthen __IMPOSSIBLE__ v ] (Var i ps, Var j vs) | i == j -> match ps vs (Def c ps, Def d vs) | c == d -> match ps vs (Con c _ ps, Con d _ vs) | c == d -> match ps vs (Lit l, Lit l') | l == l' -> return [] (p, v) | p == v -> return [] (p, Level l) -> match p =<< reallyUnLevelView l (Sort ps, Sort pv) -> match ps pv (p, Sort (Type v)) -> match p =<< reallyUnLevelView v _ -> mzero instance Match Sort where match p v = case (p, v) of (Type pl, Type vl) -> match pl vl _ | p == v -> return [] _ -> mzero instance Match Level where match p v = do p <- reallyUnLevelView p v <- reallyUnLevelView v match p v -- | Substitute terms with origin into display terms, -- replacing variables along with their origins. -- -- The purpose is to replace the pattern variables in a with-display form, -- and only on the top level of the lhs. Thus, we are happy to fall back -- to ordinary substitution where it does not matter. -- This fixes issue #2590. class SubstWithOrigin a where substWithOrigin :: Substitution -> [WithOrigin Term] -> a -> a instance SubstWithOrigin a => SubstWithOrigin [a] where substWithOrigin rho ots = map (substWithOrigin rho ots) instance SubstWithOrigin (Arg a) => SubstWithOrigin (Elim' a) where substWithOrigin rho ots (Apply arg) = Apply $ substWithOrigin rho ots arg substWithOrigin rho ots e@Proj{} = e instance SubstWithOrigin (Arg Term) where substWithOrigin rho ots (Arg ai v) = case ignoreSharing v of -- pattern variable: replace origin if better Var x [] -> case ots !!! x of Just (WithOrigin o u) -> Arg (mapOrigin (replaceOrigin o) ai) u Nothing -> Arg ai $ applySubst rho v -- Issue #2717, not __IMPOSSIBLE__ -- constructor: recurse Con c ci args -> Arg ai $ Con c ci $ substWithOrigin rho ots args -- def: recurse Def q es -> Arg ai $ Def q $ substWithOrigin rho ots es -- otherwise: fall back to ordinary substitution _ -> Arg ai $ applySubst rho v where replaceOrigin _ UserWritten = UserWritten replaceOrigin o _ = o instance SubstWithOrigin Term where substWithOrigin rho ots v = case ignoreSharing v of -- constructor: recurse Con c ci args -> Con c ci $ substWithOrigin rho ots args -- def: recurse Def q es -> Def q $ substWithOrigin rho ots es -- otherwise: fall back to oridinary substitution _ -> applySubst rho v -- Do not go into dot pattern, otherwise interaction test #231 fails instance SubstWithOrigin DisplayTerm where substWithOrigin rho ots dt = case dt of DTerm v -> DTerm $ substWithOrigin rho ots v DDot v -> DDot $ applySubst rho v DDef q es -> DDef q $ substWithOrigin rho ots es DCon c ci args -> DCon c ci $ substWithOrigin rho ots args DWithApp t ts es -> DWithApp (substWithOrigin rho ots t) (substWithOrigin rho ots ts) (substWithOrigin rho ots es) -- Do not go into dot pattern, otherwise interaction test #231 fails instance SubstWithOrigin (Arg DisplayTerm) where substWithOrigin rho ots (Arg ai dt) = case dt of DTerm v -> fmap DTerm $ substWithOrigin rho ots $ Arg ai v DDot v -> Arg ai $ DDot $ applySubst rho v DDef q es -> Arg ai $ DDef q $ substWithOrigin rho ots es DCon c ci args -> Arg ai $ DCon c ci $ substWithOrigin rho ots args DWithApp t ts es -> Arg ai $ DWithApp (substWithOrigin rho ots t) (substWithOrigin rho ots ts) (substWithOrigin rho ots es) Agda-2.5.3/src/full/Agda/TypeChecking/CompiledClause.hs0000644000000000000000000001271113154613124020734 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} -- | Case trees. -- -- After coverage checking, pattern matching is translated -- to case trees, i.e., a tree of successive case splits -- on one variable at a time. module Agda.TypeChecking.CompiledClause where import Prelude hiding (null) import qualified Data.Map as Map import Data.Map (Map) import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend, Any(..)) import Data.Foldable (Foldable, foldMap) import Data.Traversable (Traversable, traverse) import Data.Data (Data) import Data.Typeable (Typeable) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Generic import Agda.Syntax.Literal import Agda.Syntax.Position import Agda.Utils.Null import Agda.Utils.Pretty hiding ((<>)) #include "undefined.h" import Agda.Utils.Impossible data WithArity c = WithArity { arity :: Int, content :: c } deriving (Typeable, Data, Functor, Foldable, Traversable, Show) -- | Branches in a case tree. data Case c = Branches { projPatterns :: Bool -- ^ We are constructing a record here (copatterns). -- 'conBranches' lists projections. , conBranches :: Map QName (WithArity c) -- ^ Map from constructor (or projection) names to their arity -- and the case subtree. (Projections have arity 0.) , litBranches :: Map Literal c -- ^ Map from literal to case subtree. , catchAllBranch :: Maybe c -- ^ (Possibly additional) catch-all clause. } deriving (Typeable, Data, Functor, Foldable, Traversable, Show) -- | Case tree with bodies. data CompiledClauses' a = Case (Arg Int) (Case (CompiledClauses' a)) -- ^ @Case n bs@ stands for a match on the @n@-th argument -- (counting from zero) with @bs@ as the case branches. -- If the @n@-th argument is a projection, we have only 'conBranches' -- with arity 0. | Done [Arg ArgName] a -- ^ @Done xs b@ stands for the body @b@ where the @xs@ contains hiding -- and name suggestions for the free variables. This is needed to build -- lambdas on the right hand side for partial applications which can -- still reduce. | Fail -- ^ Absurd case. deriving (Typeable, Data, Functor, Traversable, Foldable, Show) type CompiledClauses = CompiledClauses' Term litCase :: Literal -> c -> Case c litCase l x = Branches False Map.empty (Map.singleton l x) Nothing conCase :: QName -> WithArity c -> Case c conCase c x = Branches False (Map.singleton c x) Map.empty Nothing projCase :: QName -> c -> Case c projCase c x = Branches True (Map.singleton c $ WithArity 0 x) Map.empty Nothing catchAll :: c -> Case c catchAll x = Branches False Map.empty Map.empty (Just x) -- | Check whether a case tree has a catch-all clause. hasCatchAll :: CompiledClauses -> Bool hasCatchAll = getAny . loop where loop cc = case cc of Fail{} -> mempty Done{} -> mempty Case _ br -> maybe (foldMap loop br) (const $ Any True) $ catchAllBranch br instance Semigroup c => Semigroup (WithArity c) where WithArity n1 c1 <> WithArity n2 c2 | n1 == n2 = WithArity n1 (c1 <> c2) | otherwise = __IMPOSSIBLE__ -- arity must match! instance (Semigroup c, Monoid c) => Monoid (WithArity c) where mempty = WithArity __IMPOSSIBLE__ mempty mappend = (<>) instance Semigroup m => Semigroup (Case m) where Branches cop cs ls m <> Branches cop' cs' ls' m' = Branches (cop || cop') -- for @projCase <> mempty@ (Map.unionWith (<>) cs cs') (Map.unionWith (<>) ls ls') (m <> m') instance (Semigroup m, Monoid m) => Monoid (Case m) where mempty = empty mappend = (<>) instance Null (Case m) where empty = Branches False Map.empty Map.empty Nothing null (Branches _cop cs ls mcatch) = null cs && null ls && null mcatch -- * Pretty instances. instance Pretty a => Pretty (WithArity a) where pretty = pretty . content instance Pretty a => Pretty (Case a) where prettyPrec p (Branches _cop cs ls m) = mparens (p > 0) $ vcat $ prettyMap cs ++ prettyMap ls ++ prC m where prC Nothing = [] prC (Just x) = [text "_ ->" <+> pretty x] prettyMap :: (Pretty k, Pretty v) => Map k v -> [Doc] prettyMap m = [ sep [ pretty k <+> text "->" , nest 2 $ pretty v ] | (k, v) <- Map.toList m ] instance Pretty CompiledClauses where pretty (Done hs t) = text ("done" ++ show hs) <+> pretty t pretty Fail = text "fail" pretty (Case n bs) | projPatterns bs = sep [ text "record" , nest 2 $ pretty bs ] pretty (Case n bs) = sep [ text ("case " ++ prettyShow n ++ " of") , nest 2 $ pretty bs ] -- * KillRange instances. instance KillRange c => KillRange (WithArity c) where killRange = fmap killRange instance KillRange c => KillRange (Case c) where killRange (Branches cop con lit all) = Branches cop (killRangeMap con) (killRangeMap lit) (killRange all) instance KillRange CompiledClauses where killRange (Case i br) = killRange2 Case i br killRange (Done xs v) = killRange2 Done xs v killRange Fail = Fail -- * TermLike instances instance TermLike a => TermLike (WithArity a) where traverseTermM = traverse . traverseTermM foldTerm = foldMap . foldTerm instance TermLike a => TermLike (Case a) where traverseTermM = traverse . traverseTermM foldTerm = foldMap . foldTerm instance TermLike a => TermLike (CompiledClauses' a) where traverseTermM = traverse . traverseTermM foldTerm = foldMap . foldTerm Agda-2.5.3/src/full/Agda/TypeChecking/ProjectionLike.hs-boot0000644000000000000000000000031613154613124021723 0ustar0000000000000000module Agda.TypeChecking.ProjectionLike where import Agda.Syntax.Abstract.Name (QName) import Agda.TypeChecking.Monad.Base makeProjection :: QName -> TCM () eligibleForProjectionLike :: QName -> TCM Bool Agda-2.5.3/src/full/Agda/TypeChecking/RecordPatterns.hs0000644000000000000000000007227513154613124021015 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} -- | Code which replaces pattern matching on record constructors with -- uses of projection functions. module Agda.TypeChecking.RecordPatterns ( translateRecordPatterns , translateCompiledClauses , translateSplitTree , recordPatternToProjections ) where import Control.Applicative import Control.Arrow (first, second) import Control.Monad.Fix import Control.Monad.Reader import Control.Monad.State import qualified Data.List as List import Data.Maybe import qualified Data.Map as Map import qualified Data.Traversable as Trav import Agda.Syntax.Common import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Pattern as I import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Coverage.SplitTree import Agda.TypeChecking.EtaContract import Agda.TypeChecking.Datatypes import Agda.TypeChecking.Monad import Agda.TypeChecking.Pretty hiding (pretty) import Agda.TypeChecking.Records import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.Utils.Either import Agda.Utils.Functor import Agda.Utils.List import qualified Agda.Utils.Map as Map import Agda.Utils.Maybe import Agda.Utils.Permutation hiding (dropFrom) import Agda.Utils.Pretty (Pretty(..)) import qualified Agda.Utils.Pretty as P import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Record pattern translation for let bindings --------------------------------------------------------------------------- -- | Take a record pattern @p@ and yield a list of projections -- corresponding to the pattern variables, from left to right. -- -- E.g. for @(x , (y , z))@ we return @[ fst, fst . snd, snd . snd ]@. -- -- If it is not a record pattern, error 'ShouldBeRecordPattern' is raised. recordPatternToProjections :: DeBruijnPattern -> TCM [Term -> Term] recordPatternToProjections p = case p of VarP{} -> return [ \ x -> x ] LitP{} -> typeError $ ShouldBeRecordPattern p DotP{} -> typeError $ ShouldBeRecordPattern p AbsurdP{} -> typeError $ ShouldBeRecordPattern p ConP c ci ps -> do whenNothing (conPRecord ci) $ typeError $ ShouldBeRecordPattern p t <- reduce $ fromMaybe __IMPOSSIBLE__ $ conPType ci fields <- getRecordTypeFields (unArg t) concat <$> zipWithM comb (map proj fields) (map namedArg ps) ProjP{} -> __IMPOSSIBLE__ -- copattern cannot appear here where proj p = (`applyE` [Proj ProjSystem $ unArg p]) comb :: (Term -> Term) -> DeBruijnPattern -> TCM [Term -> Term] comb prj p = map (\ f -> f . prj) <$> recordPatternToProjections p --------------------------------------------------------------------------- -- * Record pattern translation for compiled clauses --------------------------------------------------------------------------- -- | Take a matrix of booleans (at least one row!) and summarize the columns -- using conjunction. conjColumns :: [[Bool]] -> [Bool] conjColumns = foldl1 (zipWith (&&)) -- | @insertColumn i a m@ inserts a column before the @i@th column in -- matrix @m@ and fills it with value @a@. insertColumn :: Int -> a -> [[a]] -> [[a]] insertColumn i a rows = map ins rows where ins row = let (init, last) = splitAt i row in init ++ a : last {- UNUSED -- | @cutColumn i m@ removes the @i@th column from matrix @m@. cutColumn :: Int -> [[a]] -> [[a]] cutColumn i rows = map cut rows where cut row = let (init, _:last) = splitAt i row in init ++ last -- | @cutColumns i n xss = (yss, xss')@ cuts out a submatrix @yss@ -- of width @n@ from @xss@, starting at column @i@. cutColumns :: Int -> Int -> [[a]] -> ([[a]], [[a]]) cutColumns i n rows = unzip (map (cutSublist i n) rows) -} -- | @cutSublist i n xs = (xs', ys, xs'')@ cuts out a sublist @ys@ -- of width @n@ from @xs@, starting at column @i@. cutSublist :: Int -> Int -> [a] -> ([a], [a], [a]) cutSublist i n row = let (init, rest) = splitAt i row (mid , last) = splitAt n rest in (init, mid, last) getEtaAndArity :: QName -> TCM (Bool, Nat) getEtaAndArity c = for (getConstructorInfo c) $ \case DataCon n -> (False, n) RecordCon eta fs -> (eta == YesEta, size fs) translateCompiledClauses :: CompiledClauses -> TCM CompiledClauses translateCompiledClauses cc = do reportSDoc "tc.cc.record" 20 $ vcat [ text "translate record patterns in compiled clauses" , nest 2 $ return $ pretty cc ] cc <- snd <$> loop cc reportSDoc "tc.cc.record" 20 $ vcat [ text "translated compiled clauses (no eta record patterns):" , nest 2 $ return $ pretty cc ] return cc where loop :: CompiledClauses -> TCM ([Bool], CompiledClauses) loop cc = case cc of Fail -> return (repeat True, cc) Done xs t -> return (map (const True) xs, cc) Case i cs -> loops i cs loops :: Arg Int -- ^ split variable -> Case CompiledClauses -- ^ original split tree -> TCM ([Bool], CompiledClauses) loops i cs@Branches{ projPatterns = cop , conBranches = conMap , litBranches = litMap , catchAllBranch = catchAll } = do -- recurse on and compute variable status of catch-all clause (xssa, catchAll) <- unzipMaybe <$> Trav.mapM loop catchAll let xsa = fromMaybe (repeat True) xssa -- recurse on compute variable status of literal clauses (xssl, litMap) <- Map.unzip <$> Trav.mapM loop litMap let xsl = conjColumns (xsa : insertColumn (unArg i) False (Map.elems xssl)) -- recurse on constructor clauses (ccs, xssc, conMap) <- Map.unzip3 <$> do Trav.forM (Map.mapWithKey (,) conMap) $ \ (c, WithArity ar cc) -> do (xs, cc) <- loop cc dataOrRecCon <- do isProj <- isProjection c case isProj of Nothing -> do i <- getConstructorInfo c case i of DataCon n -> return $ Left n RecordCon NoEta fs -> return $ Left (size fs) RecordCon YesEta fs -> return $ Right fs Just{} -> return $ Left 0 let (isRC, n) = either (False,) ((True,) . size) dataOrRecCon (xs0, rest) = splitAt (unArg i) xs (xs1, xs2 ) = splitAt n rest -- if all dropped variables (xs1) are virgins and we are record cons. -- then new variable x is also virgin -- and we can translate away the split x = isRC && and xs1 -- xs' = updated variables xs' = xs0 ++ x : xs2 -- get the record fields fs = either __IMPOSSIBLE__ id dataOrRecCon -- if x we can translate mcc <- if x then etaContract [replaceByProjections i (map unArg fs) cc] else return [] when (n /= ar) __IMPOSSIBLE__ return (mcc, xs', WithArity ar cc) -- compute result let xs = conjColumns (xsl : Map.elems xssc) case concat $ Map.elems ccs of -- case: no record pattern was translated [] -> return (xs, Case i $ Branches { projPatterns = cop , conBranches = conMap , litBranches = litMap , catchAllBranch = catchAll }) -- case: translated away one record pattern [cc] -> do -- Andreas, 2013-03-22 -- Due to catch-all-expansion this is actually possible: -- -- we cannot have a catch-all if we had a record pattern -- whenJust catchAll __IMPOSSIBLE__ -- We just drop the catch-all clause. This is safe because -- for record patterns we have expanded all the catch-alls. return (xs, cc) -- mergeCatchAll cc catchAll) -- case: more than one record patterns (impossible) _ -> __IMPOSSIBLE__ {- UNUSED instance Monoid CompiledClauses where mempty = __IMPOSSIBLE__ mappend (Case n c) (Case n' c') | n == n' = Case n $ mappend c c' mappend _ _ = __IMPOSSIBLE__ mergeCatchAll :: CompiledClauses -> Maybe CompiledClauses -> CompiledClauses mergeCatchAll cc ca = maybe cc (mappend cc) ca {- case (cc, ca) of (_ , Nothing) -> cc (Case n c, Just (Case n' c')) | n == n' -> Case n $ mappend c c' _ -> __IMPOSSIBLE__ -- this would mean non-determinism -} -} -- | @replaceByProjections i projs cc@ replaces variables @i..i+n-1@ -- (counted from left) by projections @projs_1 i .. projs_n i@. -- -- If @n==0@, we matched on a zero-field record, which means that -- we are actually introduce a new variable, increasing split -- positions greater or equal to @i@ by one. -- Otherwise, we have to lower -- replaceByProjections :: Arg Int -> [QName] -> CompiledClauses -> CompiledClauses replaceByProjections (Arg ai i) projs cc = let n = length projs loop :: Int -> CompiledClauses -> CompiledClauses loop i cc = case cc of Case j cs -- if j < i, we leave j untouched, but we increase i by the number -- of variables replacing j in the branches | unArg j < i -> Case j $ loops i cs -- if j >= i then we shrink j by (n-1) | otherwise -> Case (j <&> \ k -> k - (n-1)) $ fmap (loop i) cs Done xs v -> -- we have to delete (n-1) variables from xs -- and instantiate v suitably with the projections let (xs0,xs1,xs2) = cutSublist i n xs names | null xs1 = ["r"] | otherwise = map unArg xs1 x = Arg ai $ foldr1 appendArgNames names xs' = xs0 ++ x : xs2 us = map (\ p -> Var 0 [Proj ProjSystem p]) (reverse projs) -- go from level (i + n - 1) to index (subtract from |xs|-1) index = length xs - (i + n) in Done xs' $ applySubst (liftS (length xs2) $ us ++# raiseS 1) v -- The body is NOT guarded by lambdas! -- WRONG: underLambdas i (flip apply) (map defaultArg us) v Fail -> Fail loops :: Int -> Case CompiledClauses -> Case CompiledClauses loops i Branches{ projPatterns = cop , conBranches = conMap , litBranches = litMap , catchAllBranch = catchAll } = Branches{ projPatterns = cop , conBranches = fmap (\ (WithArity n c) -> WithArity n $ loop (i + n - 1) c) conMap , litBranches = fmap (loop (i - 1)) litMap , catchAllBranch = fmap (loop i) catchAll } in loop i cc -- | Check if a split is on a record constructor, and return the projections -- if yes. isRecordCase :: Case c -> TCM (Maybe ([QName], c)) isRecordCase (Branches { conBranches = conMap , litBranches = litMap , catchAllBranch = Nothing }) | Map.null litMap , [(con, WithArity _ br)] <- Map.toList conMap = do isRC <- isRecordConstructor con case isRC of Just (r, Record { recFields = fs }) -> return $ Just (map unArg fs, br) Just (r, _) -> __IMPOSSIBLE__ Nothing -> return Nothing isRecordCase _ = return Nothing --------------------------------------------------------------------------- -- * Record pattern translation for split trees --------------------------------------------------------------------------- -- | Split tree annotation. data RecordSplitNode = RecordSplitNode { splitCon :: QName -- ^ Constructor name for this branch. , splitArity :: Int -- ^ Arity of the constructor. , splitRecordPattern :: Bool -- ^ Should we translate this split away? } -- | Split tree annotated for record pattern translation. type RecordSplitTree = SplitTree' RecordSplitNode type RecordSplitTrees = SplitTrees' RecordSplitNode -- | Bottom-up procedure to annotate split tree. recordSplitTree :: SplitTree -> TCM RecordSplitTree recordSplitTree t = snd <$> loop t where loop :: SplitTree -> TCM ([Bool], RecordSplitTree) loop t = case t of SplittingDone n -> return (replicate n True, SplittingDone n) SplitAt i ts -> do (xs, ts) <- loops (unArg i) ts return (xs, SplitAt i ts) loops :: Int -> SplitTrees -> TCM ([Bool], RecordSplitTrees) loops i ts = do (xss, ts) <- unzip <$> do forM ts $ \ (c, t) -> do (xs, t) <- loop t (isRC, n) <- getEtaAndArity c let (xs0, rest) = splitAt i xs (xs1, xs2) = splitAt n rest x = isRC && and xs1 xs' = xs0 ++ x : xs2 return (xs, (RecordSplitNode c n x, t)) return (foldl1 (zipWith (&&)) xss, ts) -- | Bottom-up procedure to record-pattern-translate split tree. translateSplitTree :: SplitTree -> TCM SplitTree translateSplitTree t = snd <$> loop t where -- @loop t = return (xs, t')@ returns the translated split tree @t'@ -- plus the status @xs@ of the clause variables -- True = variable will never be split on in @t'@ (virgin variable) -- False = variable will be spilt on in @t'@ loop :: SplitTree -> TCM ([Bool], SplitTree) loop t = case t of SplittingDone n -> -- start with n virgin variables return (replicate n True, SplittingDone n) SplitAt i ts -> do (x, xs, ts) <- loops (unArg i) ts -- if we case on record constructor, drop case let t' = if x then case ts of [(c,t)] -> t _ -> __IMPOSSIBLE__ -- else retain case else SplitAt i ts return (xs, t') -- @loops i ts = return (x, xs, ts')@ cf. @loop@ -- @x@ says wether at arg @i@ we have a record pattern split -- that can be removed loops :: Int -> SplitTrees -> TCM (Bool, [Bool], SplitTrees) loops i ts = do -- note: ts not empty (rs, xss, ts) <- unzip3 <$> do forM ts $ \ (c, t) -> do (xs, t) <- loop t (isRC, n) <- getEtaAndArity c -- now drop variables from i to i+n-1 let (xs0, rest) = splitAt i xs (xs1, xs2) = splitAt n rest -- if all dropped variables are virgins and we are record cons. -- then new variable x is also virgin -- and we can translate away the split x = isRC && and xs1 -- xs' = updated variables xs' = xs0 ++ x : xs2 -- delete splits from t if record match t' = if x then dropFrom i (n - 1) t else t return (x, xs', (c, t')) -- x = did we split on a record constructor? let x = and rs -- invariant: if record constructor, then exactly one constructor if x then unless (rs == [True]) __IMPOSSIBLE__ -- else no record constructor else unless (or rs == False) __IMPOSSIBLE__ return (x, conjColumns xss, ts) -- | @dropFrom i n@ drops arguments @j@ with @j < i + n@ and @j >= i@. -- NOTE: @n@ can be negative, in which case arguments are inserted. class DropFrom a where dropFrom :: Int -> Int -> a -> a instance DropFrom (SplitTree' c) where dropFrom i n t = case t of SplittingDone m -> SplittingDone (m - n) SplitAt x@(Arg ai j) ts | j >= i + n -> SplitAt (Arg ai $ j - n) $ dropFrom i n ts | j < i -> SplitAt x $ dropFrom i n ts | otherwise -> __IMPOSSIBLE__ instance DropFrom (c, SplitTree' c) where dropFrom i n (c, t) = (c, dropFrom i n t) instance DropFrom a => DropFrom [a] where dropFrom i n ts = map (dropFrom i n) ts {- -- | Check if a split is on a record constructor, and return the projections -- if yes. isRecordSplit :: SplitTrees -> TCM (Maybe ([QName], c)) isRecordSplit (Branches { conBranches = conMap , litBranches = litMap , catchAllBranch = Nothing }) | Map.null litBranches , [(con,br)] <- Map.toList conMap = do isRC <- isRecordConstructor con case isRC of Just (r, Record { recFields = fs }) -> return $ Just (map unArg fs, br) Just (r, _) -> __IMPOSSIBLE__ Nothing -> return Nothing isRecordSplit _ = return Nothing -} --------------------------------------------------------------------------- -- * Record pattern translation for function definitions --------------------------------------------------------------------------- -- | Replaces pattern matching on record constructors with uses of -- projection functions. Does not remove record constructor patterns -- which have sub-patterns containing non-record constructor or -- literal patterns. translateRecordPatterns :: Clause -> TCM Clause translateRecordPatterns clause = do -- ps: New patterns, in left-to-right order, in the context of the -- old RHS. -- s: Partial substitution taking the old pattern variables -- (including dot patterns; listed from left to right) to terms in -- the context of the new RHS. -- cs: List of changes, with types in the context of the old -- telescope. (ps, s, cs) <- runRecPatM $ translatePatterns $ unnumberPatVars $ namedClausePats clause let -- Number of variables + dot patterns in new clause. noNewPatternVars = size cs s' = reverse s mkSub s = s ++# raiseS noNewPatternVars -- Substitution used to convert terms in the old RHS's -- context to terms in the new RHS's context. rhsSubst = mkSub s' -- Substitution used to convert terms in the old telescope's -- context to terms in the new RHS's context. perm = fromMaybe __IMPOSSIBLE__ $ clausePerm clause rhsSubst' = mkSub $ permute (reverseP perm) s' -- TODO: Is it OK to replace the definition above with the -- following one? -- -- rhsSubst' = mkSub $ permute (clausePerm clause) s -- The old telescope, flattened and in textual left-to-right -- order (i.e. the type signature for the variable which occurs -- first in the list of patterns comes first). flattenedOldTel = permute (invertP __IMPOSSIBLE__ $ compactP perm) $ zip (teleNames $ clauseTel clause) $ flattenTel $ clauseTel clause -- The new telescope, still flattened, with types in the context -- of the new RHS, in textual left-to-right order, and with -- Nothing in place of dot patterns. substTel = map . fmap . second . applySubst newTel' = substTel rhsSubst' $ translateTel cs $ flattenedOldTel -- Permutation taking the new variable and dot patterns to the -- new telescope. newPerm = adjustForDotPatterns $ reorderTel_ $ map (maybe dummyDom snd) newTel' -- It is important that dummyDom does not mention any variable -- (see the definition of reorderTel). where isDotP n = case List.genericIndex cs n of Left DotP{} -> True _ -> False adjustForDotPatterns (Perm n is) = Perm n (filter (not . isDotP) is) -- Substitution used to convert terms in the new RHS's context -- to terms in the new telescope's context. lhsSubst' = {-'-} renaming __IMPOSSIBLE__ (reverseP newPerm) -- Substitution used to convert terms in the old telescope's -- context to terms in the new telescope's context. lhsSubst = applySubst lhsSubst' rhsSubst' -- The new telescope. newTel = uncurry unflattenTel . unzip $ map (fromMaybe __IMPOSSIBLE__) $ permute newPerm $ substTel lhsSubst' $ newTel' -- New clause. c = clause { clauseTel = newTel , namedClausePats = numberPatVars __IMPOSSIBLE__ newPerm $ applySubst lhsSubst ps , clauseBody = applySubst lhsSubst $ clauseBody clause } reportSDoc "tc.lhs.recpat" 20 $ vcat [ text "Original clause:" , nest 2 $ inTopContext $ vcat [ text "delta =" <+> prettyTCM (clauseTel clause) , text "pats =" <+> text (show $ clausePats clause) ] , text "Intermediate results:" , nest 2 $ vcat [ text "ps =" <+> text (show ps) , text "s =" <+> prettyTCM s , text "cs =" <+> prettyTCM cs , text "flattenedOldTel =" <+> (text . show) flattenedOldTel , text "newTel' =" <+> (text . show) newTel' , text "newPerm =" <+> prettyTCM newPerm ] ] reportSDoc "tc.lhs.recpat" 20 $ vcat [ text "lhsSubst' =" <+> (text . show) lhsSubst' , text "lhsSubst =" <+> (text . show) lhsSubst , text "newTel =" <+> prettyTCM newTel ] reportSDoc "tc.lhs.recpat" 10 $ escapeContext (size $ clauseTel clause) $ vcat [ text "Translated clause:" , nest 2 $ vcat [ text "delta =" <+> prettyTCM (clauseTel c) , text "ps =" <+> text (show $ clausePats c) , text "body =" <+> text (show $ clauseBody c) , text "body =" <+> addContext (clauseTel c) (maybe (text "_|_") prettyTCM (clauseBody c)) ] ] return c ------------------------------------------------------------------------ -- Record pattern monad -- | A monad used to translate record patterns. -- -- The state records the number of variables produced so far, the -- reader records the total number of variables produced by the entire -- computation. Functions using this monad need to be sufficiently -- lazy in the reader component. newtype RecPatM a = RecPatM (TCMT (ReaderT Nat (StateT Nat IO)) a) deriving (Functor, Applicative, Monad, MonadIO, MonadTCM, HasOptions, MonadDebug, MonadReader TCEnv, MonadState TCState) -- | Runs a computation in the 'RecPatM' monad. runRecPatM :: RecPatM a -> TCM a runRecPatM (RecPatM m) = mapTCMT (\m -> do (x, noVars) <- mfix $ \ ~(_, noVars) -> runStateT (runReaderT m noVars) 0 return x) m -- | Returns the next pattern variable, and the corresponding term. nextVar :: RecPatM (Pattern, Term) nextVar = RecPatM $ do n <- lift get lift $ put $ succ n noVars <- lift ask return (varP "r", var $ noVars - n - 1) ------------------------------------------------------------------------ -- Types used to record changes to a clause -- | @VarPat@ stands for variable patterns, and @DotPat@ for dot -- patterns. data Kind = VarPat | DotPat deriving Eq -- | @'Left' p@ means that a variable (corresponding to the pattern -- @p@, a variable or dot pattern) should be kept unchanged. @'Right' -- (n, x, t)@ means that @n 'VarPat'@ variables, and @n 'DotPat'@ dot -- patterns, should be removed, and a new variable, with the name @x@, -- inserted instead. The type of the new variable is @t@. type Change = Either Pattern (Kind -> Nat, ArgName, Dom Type) type Changes = [Change] instance Pretty (Kind -> Nat) where pretty f = P.text "(VarPat:" P.<+> P.text (show $ f VarPat) P.<+> P.text "DotPat:" P.<+> P.text (show $ f DotPat) P.<> P.text ")" instance PrettyTCM (Kind -> Nat) where prettyTCM = return . pretty instance PrettyTCM Change where prettyTCM (Left p) = prettyTCM p prettyTCM (Right (f, x, t)) = text "Change" <+> prettyTCM f <+> text x <+> prettyTCM t -- | Record pattern trees. data RecordTree = Leaf Pattern -- ^ Corresponds to variable and dot patterns; contains the -- original pattern. | RecCon (Arg Type) [(Term -> Term, RecordTree)] -- ^ @RecCon t args@ stands for a record constructor application: -- @t@ is the type of the application, and the list contains a -- projection function and a tree for every argument. ------------------------------------------------------------------------ -- Record pattern trees -- | @projections t@ returns a projection for every non-dot leaf -- pattern in @t@. The term is the composition of the projection -- functions from the leaf to the root. -- -- Every term is tagged with its origin: a variable pattern or a dot -- pattern. projections :: RecordTree -> [(Term -> Term, Kind)] projections (Leaf (DotP{})) = [(id, DotPat)] projections (Leaf (VarP{})) = [(id, VarPat)] projections (Leaf _) = __IMPOSSIBLE__ projections (RecCon _ args) = concatMap (\ (p, t) -> map (first (. p)) $ projections t) args -- | Converts a record tree to a single pattern along with information -- about the deleted pattern variables. removeTree :: RecordTree -> RecPatM (Pattern, [Term], Changes) removeTree tree = do (pat, x) <- nextVar let ps = projections tree s = map (\(p, _) -> p x) ps count k = length $ filter ((== k) . snd) ps return $ case tree of Leaf p -> (p, s, [Left p]) RecCon t _ -> (pat, s, [Right (count, "r", domFromArg t)]) ------------------------------------------------------------------------ -- Translation of patterns -- | Removes record constructors from patterns. -- -- Returns the following things: -- -- * The new pattern. -- -- * A substitution which maps the /old/ pattern variables (in the -- order they occurred in the pattern; not including dot patterns) -- to terms (either the new name of the variable, or a projection -- applied to a new pattern variable). -- -- * A list explaining the changes to the variables bound in the -- pattern. -- -- Record patterns containing non-record constructor patterns are not -- translated (though their sub-patterns may be). -- -- Example: The pattern @rec1 (con1 a) (rec2 b c) (rec3 d)@ should -- yield the pattern @rec1 (con1 x) y z@, along with a substitution -- similar to @[x, proj2-1 y, proj2-2 y, proj3-1 z]@. -- -- This function assumes that literals are never of record type. translatePattern :: Pattern -> RecPatM (Pattern, [Term], Changes) translatePattern p@(ConP c ci ps) -- Andreas, 2015-05-28 only translate implicit record patterns | Just ConOSystem <- conPRecord ci = do r <- recordTree p case r of Left r -> r Right t -> removeTree t | otherwise = do (ps, s, cs) <- translatePatterns ps return (ConP c ci ps, s, cs) translatePattern p@VarP{} = removeTree (Leaf p) translatePattern p@DotP{} = removeTree (Leaf p) translatePattern p@AbsurdP{} = removeTree (Leaf p) translatePattern p@LitP{} = return (p, [], []) translatePattern p@ProjP{}= return (p, [], []) translatePatterns :: [NamedArg Pattern] -> RecPatM ([NamedArg Pattern], [Term], Changes) translatePatterns ps = do (ps', ss, cs) <- unzip3 <$> mapM (translatePattern . namedArg) ps return (zipWith (\p -> fmap (p <$)) ps' ps, concat ss, concat cs) -- | Traverses a pattern and returns one of two things: -- -- * If there is no non-record constructor in the pattern, then -- @'Right' ps@ is returned, where @ps@ contains one projection for -- every variable in the input pattern (in the order they are -- encountered). -- -- * Otherwise the output is a computation returning the same kind of -- result as that coming from 'translatePattern'. (Computations are -- returned rather than values to ensure that variable numbers are -- allocated in the right order.) -- -- Assumes that literals are never of record type. recordTree :: Pattern -> RecPatM (Either (RecPatM (Pattern, [Term], Changes)) RecordTree) -- Andreas, 2015-05-28 only translate implicit record patterns recordTree (ConP c ci ps) | Just ConOSystem <- conPRecord ci = do let t = fromMaybe __IMPOSSIBLE__ $ conPType ci rs <- mapM (recordTree . namedArg) ps case allRight rs of Nothing -> return $ Left $ do (ps', ss, cs) <- unzip3 <$> mapM (either id removeTree) rs return (ConP c ci (ps' `withNamedArgsFrom` ps), concat ss, concat cs) Just ts -> liftTCM $ do t <- reduce t fields <- getRecordTypeFields (unArg t) -- let proj p = \x -> Def (unArg p) [defaultArg x] let proj p = (`applyE` [Proj ProjSystem $ unArg p]) return $ Right $ RecCon t $ zip (map proj fields) ts recordTree p@(ConP _ ci _) = return $ Left $ translatePattern p recordTree p@VarP{} = return (Right (Leaf p)) recordTree p@DotP{} = return (Right (Leaf p)) recordTree p@AbsurdP{} = return (Right (Leaf p)) recordTree p@LitP{} = return $ Left $ translatePattern p recordTree p@ProjP{}= return $ Left $ translatePattern p ------------------------------------------------------------------------ -- Translation of the clause telescope and body -- | Translates the telescope. translateTel :: Changes -- ^ Explanation of how the telescope should be changed. Types -- should be in the context of the old telescope. -> [(ArgName, Dom Type)] -- ^ Old telescope, flattened, in textual left-to-right -- order. -> [Maybe (ArgName, Dom Type)] -- ^ New telescope, flattened, in textual left-to-right order. -- 'Nothing' is used to indicate the locations of dot patterns. translateTel (Left (DotP{}) : rest) tel = Nothing : translateTel rest tel translateTel (Right (n, x, t) : rest) tel = Just (x, t) : translateTel rest (drop (n VarPat) tel) translateTel (Left _ : rest) (t : tel) = Just t : translateTel rest tel translateTel [] [] = [] translateTel (Left _ : _) [] = __IMPOSSIBLE__ translateTel [] (_ : _) = __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/Monad.hs0000644000000000000000000000313413154613124017100 0ustar0000000000000000module Agda.TypeChecking.Monad ( module Agda.TypeChecking.Monad.Base , module Agda.TypeChecking.Monad.Closure , module Agda.TypeChecking.Monad.Constraints , module Agda.TypeChecking.Monad.Context , module Agda.TypeChecking.Monad.Debug , module Agda.TypeChecking.Monad.Env , module Agda.TypeChecking.Monad.Imports , module Agda.TypeChecking.Monad.Local , module Agda.TypeChecking.Monad.MetaVars , module Agda.TypeChecking.Monad.Mutual , module Agda.TypeChecking.Monad.Open , module Agda.TypeChecking.Monad.Options , module Agda.TypeChecking.Monad.Sharing , module Agda.TypeChecking.Monad.Signature , module Agda.TypeChecking.Monad.SizedTypes , module Agda.TypeChecking.Monad.State , module Agda.TypeChecking.Monad.Statistics , module Agda.TypeChecking.Monad.Trace , module Agda.TypeChecking.Monad.Caching ) where import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Closure import Agda.TypeChecking.Monad.Constraints import Agda.TypeChecking.Monad.Context import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Env import Agda.TypeChecking.Monad.Imports import Agda.TypeChecking.Monad.Local import Agda.TypeChecking.Monad.MetaVars import Agda.TypeChecking.Monad.Mutual import Agda.TypeChecking.Monad.Options import Agda.TypeChecking.Monad.Open import Agda.TypeChecking.Monad.Sharing import Agda.TypeChecking.Monad.Signature import Agda.TypeChecking.Monad.SizedTypes import Agda.TypeChecking.Monad.State import Agda.TypeChecking.Monad.Statistics import Agda.TypeChecking.Monad.Trace import Agda.TypeChecking.Monad.Caching Agda-2.5.3/src/full/Agda/TypeChecking/DropArgs.hs0000644000000000000000000000516313154613124017567 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.DropArgs where import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.CompiledClause import Agda.Utils.Functor import Agda.Utils.Permutation #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Dropping initial arguments to create a projection-like function --------------------------------------------------------------------------- -- | When making a function projection-like, we drop the first @n@ -- arguments. class DropArgs a where dropArgs :: Int -> a -> a instance DropArgs a => DropArgs (Maybe a) where dropArgs n = fmap (dropArgs n) -- | NOTE: This creates telescopes with unbound de Bruijn indices. instance DropArgs Telescope where dropArgs n tel = telFromList $ drop n $ telToList tel instance DropArgs Permutation where dropArgs n (Perm m p) = Perm (m - n) $ map (subtract n) $ drop n p -- | NOTE: does not work for recursive functions. instance DropArgs Clause where dropArgs n cl = cl{ -- Andreas, 2012-09-25: just dropping the front of telescope -- makes it ill-formed (unbound indices) -- we should let the telescope intact!? -- Ulf, 2016-06-23: Indeed. After parameter refinement it's even -- worse: the module parameters we want to drop aren't necessarily -- the first things in the telescope. namedClausePats = drop n $ namedClausePats cl -- BUG: need to drop also from recursive calls!! } instance DropArgs FunctionInverse where dropArgs n finv = fmap (dropArgs n) finv {- UNUSED, but don't remove (Andreas, 2012-10-08) -- | Use for dropping initial lambdas in compiled clause bodies. -- NOTE: does not reduce term, need lambdas to be present. instance DropArgs Term where dropArgs 0 v = v dropArgs n v = case ignoreSharing v of Lam h b -> dropArgs (n - 1) (absBody b) _ -> __IMPOSSIBLE__ -} -- | To drop the first @n@ arguments in a compiled clause, -- we reduce the split argument indices by @n@ and -- drop @n@ arguments from the bodies. -- NOTE: this only works for non-recursive functions, we -- are not dropping arguments to recursive calls in bodies. instance DropArgs CompiledClauses where dropArgs n cc = case cc of Case i br | unArg i < n -> __IMPOSSIBLE__ | otherwise -> Case (i <&> \ j -> j - n) $ fmap (dropArgs n) br Done xs t | length xs < n -> __IMPOSSIBLE__ | otherwise -> Done (drop n xs) t Fail -> Fail Agda-2.5.3/src/full/Agda/TypeChecking/Polarity.hs-boot0000644000000000000000000000030413154613124020602 0ustar0000000000000000 module Agda.TypeChecking.Polarity where import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base computePolarity :: [QName] -> TCM () composePol :: Polarity -> Polarity -> Polarity Agda-2.5.3/src/full/Agda/TypeChecking/Substitute.hs0000644000000000000000000014210113154613124020213 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE UndecidableInstances #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE OverlappingInstances #-} #endif {-# OPTIONS_GHC -fno-warn-orphans #-} -- | This module contains the definition of hereditary substitution -- and application operating on internal syntax which is in β-normal -- form (β including projection reductions). -- -- Further, it contains auxiliary functions which rely on substitution -- but not on reduction. module Agda.TypeChecking.Substitute ( module Agda.TypeChecking.Substitute , module Agda.TypeChecking.Substitute.Class , module Agda.TypeChecking.Substitute.DeBruijn , Substitution'(..), Substitution ) where import Control.Applicative import Data.Function import Data.Functor import qualified Data.List as List import Data.Map (Map) import Data.Maybe import Data.Monoid import Data.Typeable (Typeable) import Debug.Trace (trace) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import qualified Agda.Syntax.Abstract as A import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Free as Free import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Positivity.Occurrence as Occ import Agda.TypeChecking.Substitute.Class import Agda.TypeChecking.Substitute.DeBruijn import Agda.Utils.Empty import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Permutation import Agda.Utils.Size import Agda.Utils.Tuple import Agda.Utils.HashMap (HashMap) #include "undefined.h" import Agda.Utils.Impossible instance Apply Term where applyE m [] = m applyE m es = case m of Var i es' -> Var i (es' ++ es) Def f es' -> defApp f es' es -- remove projection redexes Con c ci args -> conApp c ci args es Lam _ b -> case es of Apply a : es0 -> lazyAbsApp b (unArg a) `applyE` es0 _ -> __IMPOSSIBLE__ MetaV x es' -> MetaV x (es' ++ es) Shared p -> Shared $ applyE p es Lit{} -> __IMPOSSIBLE__ Level{} -> __IMPOSSIBLE__ Pi _ _ -> __IMPOSSIBLE__ Sort _ -> __IMPOSSIBLE__ DontCare mv -> dontCare $ mv `applyE` es -- Andreas, 2011-10-02 -- need to go under DontCare, since "with" might resurrect irrelevant term -- | If $v$ is a record value, @canProject f v@ -- returns its field @f@. canProject :: QName -> Term -> Maybe (Arg Term) canProject f v = case ignoreSharing v of (Con (ConHead _ _ fs) _ vs) -> do i <- List.elemIndex f fs headMaybe (drop i vs) _ -> Nothing -- | Eliminate a constructed term. conApp :: ConHead -> ConInfo -> Args -> Elims -> Term conApp ch ci args [] = Con ch ci args conApp ch ci args (Apply a : es) = conApp ch ci (args ++ [a]) es conApp ch@(ConHead c _ fs) ci args (Proj o f : es) = let failure = flip trace __IMPOSSIBLE__ $ "conApp: constructor " ++ show c ++ " with fields " ++ show fs ++ " projected by " ++ show f i = maybe failure id $ List.elemIndex f fs v = maybe failure argToDontCare $ headMaybe $ drop i args in applyE v es -- -- Andreas, 2016-07-20 futile attempt to magically fix ProjOrigin -- fallback = v -- in if not $ null es then applyE v es else -- -- If we have no more eliminations, we can return v -- if o == ProjSystem then fallback else -- -- If the result is a projected term with ProjSystem, -- -- we can can restore it to ProjOrigin o. -- -- Otherwise, we get unpleasant printing with eta-expanded record metas. -- caseMaybe (hasElims v) fallback $ \ (hd, es0) -> -- caseMaybe (initLast es0) fallback $ \ (es1, e2) -> -- case e2 of -- -- We want to replace this ProjSystem by o. -- Proj ProjSystem q -> hd (es1 ++ [Proj o q]) -- -- Andreas, 2016-07-21 for the whole testsuite -- -- this case was never triggered! -- _ -> fallback {- i = maybe failure id $ elemIndex f $ map unArg fs v = maybe failure unArg $ headMaybe $ drop i args -- Andreas, 2013-10-20 see Issue543a: -- protect result of irrelevant projection. r = maybe __IMPOSSIBLE__ getRelevance $ headMaybe $ drop i fs u | Irrelevant <- r = DontCare v | otherwise = v in applyE v es -} -- | @defApp f us vs@ applies @Def f us@ to further arguments @vs@, -- eliminating top projection redexes. -- If @us@ is not empty, we cannot have a projection redex, since -- the record argument is the first one. defApp :: QName -> Elims -> Elims -> Term defApp f [] (Apply a : es) | Just v <- canProject f (unArg a) = argToDontCare v `applyE` es defApp f es0 es = Def f $ es0 ++ es -- protect irrelevant fields (see issue 610) argToDontCare :: Arg Term -> Term argToDontCare (Arg ai v) | Irrelevant <- getRelevance ai = dontCare v | otherwise = v -- Andreas, 2016-01-19: In connection with debugging issue #1783, -- I consider the Apply instance for Type harmful, as piApply is not -- safe if the type is not sufficiently reduced. -- (piApply is not in the monad and hence cannot unfold type synonyms). -- -- Without apply for types, one has to at least use piApply and be -- aware of doing something which has a precondition -- (type sufficiently reduced). -- -- By grepping for piApply, one can quickly get an overview over -- potentially harmful uses. -- -- In general, piApplyM is preferable over piApply since it is more robust -- and fails earlier than piApply, which may only fail at serialization time, -- when all thunks are forced. -- REMOVED: -- instance Apply Type where -- apply = piApply -- -- Maybe an @applyE@ instance would be useful here as well. -- -- A record type could be applied to a projection name -- -- to yield the field type. -- -- However, this works only in the monad where we can -- -- look up the fields of a record type. instance Apply Sort where applyE s [] = s applyE s _ = __IMPOSSIBLE__ instance Apply a => Apply (Ptr a) where applyE p xs = fmap (`applyE` xs) p -- @applyE@ does not make sense for telecopes, definitions, clauses etc. instance Subst Term a => Apply (Tele a) where apply tel [] = tel apply EmptyTel _ = __IMPOSSIBLE__ apply (ExtendTel _ tel) (t : ts) = lazyAbsApp tel (unArg t) `apply` ts instance Apply Definition where apply (Defn info x t pol occ df m c inst copy ma inj d) args = Defn info x (piApply t args) (apply pol args) (apply occ args) df m c inst copy ma inj (apply d args) instance Apply RewriteRule where apply r args = let newContext = apply (rewContext r) args sub = liftS (size newContext) $ parallelS $ reverse $ map (PTerm . unArg) args in RewriteRule { rewName = rewName r , rewContext = newContext , rewHead = rewHead r , rewPats = applySubst sub (rewPats r) , rewRHS = applyNLPatSubst sub (rewRHS r) , rewType = applyNLPatSubst sub (rewType r) } #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} Apply [Occ.Occurrence] where #else instance Apply [Occ.Occurrence] where #endif apply occ args = List.drop (length args) occ #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} Apply [Polarity] where #else instance Apply [Polarity] where #endif apply pol args = List.drop (length args) pol -- | Make sure we only drop variable patterns. #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} Apply [NamedArg (Pattern' a)] where #else instance Apply [NamedArg (Pattern' a)] where #endif apply ps args = loop (length args) ps where loop 0 ps = ps loop n [] = __IMPOSSIBLE__ loop n (p : ps) = let recurse = loop (n - 1) ps in case namedArg p of VarP{} -> recurse DotP{} -> __IMPOSSIBLE__ AbsurdP{} -> __IMPOSSIBLE__ LitP{} -> __IMPOSSIBLE__ ConP{} -> __IMPOSSIBLE__ ProjP{} -> __IMPOSSIBLE__ instance Apply Projection where apply p args = p { projIndex = projIndex p - size args , projLams = projLams p `apply` args } instance Apply ProjLams where apply (ProjLams lams) args = ProjLams $ List.drop (length args) lams instance Apply Defn where apply d [] = d apply d args = case d of Axiom{} -> d AbstractDefn d -> AbstractDefn $ apply d args Function{ funClauses = cs, funCompiled = cc, funInv = inv , funProjection = Nothing } -> d { funClauses = apply cs args , funCompiled = apply cc args , funInv = apply inv args } Function{ funClauses = cs, funCompiled = cc, funInv = inv , funProjection = Just p0} -> case p0 `apply` args of p@Projection{ projIndex = n } | n < 0 -> __IMPOSSIBLE__ -- case: applied only to parameters | n > 0 -> d { funProjection = Just p } -- case: applied also to record value (n == 0) | otherwise -> d { funClauses = apply cs args' , funCompiled = apply cc args' , funInv = apply inv args' , funProjection = if isVar0 then Just p{ projIndex = 0 } else Nothing } where larg = last args -- the record value args' = [larg] isVar0 = case ignoreSharing $ unArg larg of Var 0 [] -> True; _ -> False {- Function{ funClauses = cs, funCompiled = cc, funInv = inv , funProjection = Just p@Projection{ projIndex = n } } -- case: only applying parameters | size args < n -> d { funProjection = Just $ p `apply` args } -- case: apply also to record value | otherwise -> d { funClauses = apply cs args' , funCompiled = apply cc args' , funInv = apply inv args' , funProjection = Just $ p { projIndex = 0 } -- Nothing ? } where args' = [last args] -- the record value -} Datatype{ dataPars = np, dataSmallPars = sps, dataNonLinPars = nlps, dataClause = cl {-, dataArgOccurrences = occ-} } -> d { dataPars = np - size args , dataSmallPars = apply sps args , dataNonLinPars = apply nlps args , dataClause = apply cl args -- , dataArgOccurrences = List.drop (length args) occ } Record{ recPars = np, recClause = cl, recTel = tel {-, recArgOccurrences = occ-} } -> d { recPars = np - size args , recClause = apply cl args, recTel = apply tel args -- , recArgOccurrences = List.drop (length args) occ } Constructor{ conPars = np } -> d { conPars = np - size args } Primitive{ primClauses = cs } -> d { primClauses = apply cs args } instance Apply PrimFun where apply (PrimFun x ar def) args = PrimFun x (ar - size args) $ \vs -> def (args ++ vs) instance Apply Clause where -- This one is a little bit tricksy after the parameter refinement change. -- It is assumed that we only apply a clause to "parameters", i.e. -- arguments introduced by lambda lifting. The problem is that these aren't -- necessarily the first elements of the clause telescope. apply cls@(Clause rl rf tel ps b t catchall unreachable) args | length args > length ps = __IMPOSSIBLE__ | otherwise = Clause rl rf tel' (applySubst rhoP $ drop (length args) ps) (applySubst rho b) (applySubst rho t) catchall unreachable where -- We have -- Γ ⊢ args, for some outer context Γ -- Δ ⊢ ps, where Δ is the clause telescope (tel) rargs = map unArg $ reverse args rps = reverse $ take (length args) ps n = size tel -- This is the new telescope. Created by substituting the args into the -- appropriate places in the old telescope. We know where those are by -- looking at the deBruijn indices of the patterns. tel' = newTel n tel rps rargs -- We then have to create a substitution from the old telescope to the -- new telescope that we can apply to dot patterns and the clause body. rhoP :: PatternSubstitution rhoP = mkSub DotP n rps rargs rho = mkSub id n rps rargs substP :: Nat -> Term -> [NamedArg DeBruijnPattern] -> [NamedArg DeBruijnPattern] substP i v = subst i (DotP v) -- Building the substitution from the old telescope to the new. The -- interesting case is when we have a variable pattern: -- We need Δ′ ⊢ ρ : Δ -- where Δ′ = newTel Δ (xⁱ : ps) (v : vs) -- = newTel Δ[xⁱ:=v] ps[xⁱ:=v'] vs -- Note that we need v' = raise (|Δ| - 1) v, to make Γ ⊢ v valid in -- ΓΔ[xⁱ:=v]. -- A recursive call ρ′ = mkSub (substP i v' ps) vs gets us -- Δ′ ⊢ ρ′ : Δ[xⁱ:=v] -- so we just need Δ[xⁱ:=v] ⊢ σ : Δ and then ρ = ρ′ ∘ σ. -- That's achieved by σ = singletonS i v'. mkSub :: Subst a a => (Term -> a) -> Nat -> [NamedArg DeBruijnPattern] -> [Term] -> Substitution' a mkSub _ _ [] [] = idS mkSub tm n (p : ps) (v : vs) = case namedArg p of VarP (DBPatVar _ i) -> mkSub tm (n - 1) (substP i v' ps) vs `composeS` singletonS i (tm v') where v' = raise (n - 1) v DotP{} -> mkSub tm n ps vs AbsurdP p' -> mkSub tm n (setNamedArg p p' : ps) (v : vs) ConP c _ ps' -> mkSub tm n (ps' ++ ps) (projections c v ++ vs) LitP{} -> __IMPOSSIBLE__ ProjP{} -> __IMPOSSIBLE__ mkSub _ _ _ _ = __IMPOSSIBLE__ -- The parameter patterns 'ps' are all variables or dot patterns, or eta -- expanded record patterns (issue #2550). If they are variables they -- can appear anywhere in the clause telescope. This function -- constructs the new telescope with 'vs' substituted for 'ps'. -- Example: -- tel = (x : A) (y : B) (z : C) (w : D) -- ps = y@3 w@0 -- vs = u v -- newTel tel ps vs = (x : A) (z : C[u/y]) newTel :: Nat -> Telescope -> [NamedArg DeBruijnPattern] -> [Term] -> Telescope newTel n tel [] [] = tel newTel n tel (p : ps) (v : vs) = case namedArg p of VarP (DBPatVar _ i) -> newTel (n - 1) (subTel (size tel - 1 - i) v tel) (substP i (raise (n - 1) v) ps) vs DotP{} -> newTel n tel ps vs AbsurdP{} -> __IMPOSSIBLE__ ConP c _ ps' -> newTel n tel (ps' ++ ps) (projections c v ++ vs) LitP{} -> __IMPOSSIBLE__ ProjP{} -> __IMPOSSIBLE__ newTel _ tel _ _ = __IMPOSSIBLE__ projections c v = [ applyE v [Proj ProjSystem f] | f <- conFields c ] -- subTel i v (Δ₁ (xᵢ : A) Δ₂) = Δ₁ Δ₂[xᵢ = v] subTel i v EmptyTel = __IMPOSSIBLE__ subTel 0 v (ExtendTel _ tel) = absApp tel v subTel i v (ExtendTel a tel) = ExtendTel a $ subTel (i - 1) (raise 1 v) <$> tel instance Apply CompiledClauses where apply cc args = case cc of Fail -> Fail Done hs t | length hs >= len -> let sub = parallelS $ map var [0..length hs - len - 1] ++ map unArg args in Done (List.drop len hs) $ applySubst sub t | otherwise -> __IMPOSSIBLE__ Case n bs | unArg n >= len -> Case (n <&> \ m -> m - len) (apply bs args) | otherwise -> __IMPOSSIBLE__ where len = length args instance Apply a => Apply (WithArity a) where apply (WithArity n a) args = WithArity n $ apply a args applyE (WithArity n a) es = WithArity n $ applyE a es instance Apply a => Apply (Case a) where apply (Branches cop cs ls m) args = Branches cop (apply cs args) (apply ls args) (apply m args) applyE (Branches cop cs ls m) es = Branches cop (applyE cs es) (applyE ls es) (applyE m es) instance Apply FunctionInverse where apply NotInjective args = NotInjective apply (Inverse inv) args = Inverse $ apply inv args instance Apply DisplayTerm where apply (DTerm v) args = DTerm $ apply v args apply (DDot v) args = DDot $ apply v args apply (DCon c ci vs) args = DCon c ci $ vs ++ map (fmap DTerm) args apply (DDef c es) args = DDef c $ es ++ map (Apply . fmap DTerm) args apply (DWithApp v ws es) args = DWithApp v ws $ es ++ map Apply args applyE (DTerm v) es = DTerm $ applyE v es applyE (DDot v) es = DDot $ applyE v es applyE (DCon c ci vs) es = DCon c ci $ vs ++ map (fmap DTerm) ws where ws = fromMaybe __IMPOSSIBLE__ $ allApplyElims es applyE (DDef c es') es = DDef c $ es' ++ map (fmap DTerm) es applyE (DWithApp v ws es') es = DWithApp v ws $ es' ++ es #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPABLE #-} Apply t => Apply [t] where #else instance Apply t => Apply [t] where #endif apply ts args = map (`apply` args) ts applyE ts es = map (`applyE` es) ts instance Apply t => Apply (Blocked t) where apply b args = fmap (`apply` args) b applyE b es = fmap (`applyE` es) b instance Apply t => Apply (Maybe t) where apply x args = fmap (`apply` args) x applyE x es = fmap (`applyE` es) x instance Apply v => Apply (Map k v) where apply x args = fmap (`apply` args) x applyE x es = fmap (`applyE` es) x instance Apply v => Apply (HashMap k v) where apply x args = fmap (`apply` args) x applyE x es = fmap (`applyE` es) x instance (Apply a, Apply b) => Apply (a,b) where apply (x,y) args = (apply x args, apply y args) applyE (x,y) es = (applyE x es , applyE y es ) instance (Apply a, Apply b, Apply c) => Apply (a,b,c) where apply (x,y,z) args = (apply x args, apply y args, apply z args) applyE (x,y,z) es = (applyE x es , applyE y es , applyE z es ) instance DoDrop a => Apply (Drop a) where apply x args = dropMore (size args) x instance DoDrop a => Abstract (Drop a) where abstract tel x = unDrop (size tel) x instance Apply Permutation where -- The permutation must start with [0..m - 1] -- NB: section (- m) not possible (unary minus), hence (flip (-) m) apply (Perm n xs) args = Perm (n - m) $ map (flip (-) m) $ drop m xs where m = size args instance Abstract Permutation where abstract tel (Perm n xs) = Perm (n + m) $ [0..m - 1] ++ map (+ m) xs where m = size tel -- | @(x:A)->B(x) `piApply` [u] = B(u)@ -- -- Precondition: The type must contain the right number of pis without -- having to perform any reduction. -- -- @piApply@ is potentially unsafe, the monadic 'piApplyM' is preferable. piApply :: Type -> Args -> Type piApply t [] = t piApply (El _ (Pi _ b)) (a:args) = lazyAbsApp b (unArg a) `piApply` args piApply (El s (Shared p)) args = piApply (El s $ derefPtr p) args piApply t args = trace ("piApply t = " ++ show t ++ "\n args = " ++ show args) __IMPOSSIBLE__ --------------------------------------------------------------------------- -- * Abstraction --------------------------------------------------------------------------- instance Abstract Term where abstract = teleLam instance Abstract Type where abstract = telePi_ instance Abstract Sort where abstract EmptyTel s = s abstract _ s = __IMPOSSIBLE__ instance Abstract Telescope where EmptyTel `abstract` tel = tel ExtendTel arg xtel `abstract` tel = ExtendTel arg $ xtel <&> (`abstract` tel) instance Abstract Definition where abstract tel (Defn info x t pol occ df m c inst copy ma inj d) = Defn info x (abstract tel t) (abstract tel pol) (abstract tel occ) df m c inst copy ma inj (abstract tel d) -- | @tel ⊢ (Γ ⊢ lhs ↦ rhs : t)@ becomes @tel, Γ ⊢ lhs ↦ rhs : t)@ -- we do not need to change lhs, rhs, and t since they live in Γ. -- See 'Abstract Clause'. instance Abstract RewriteRule where abstract tel (RewriteRule q gamma f ps rhs t) = RewriteRule q (abstract tel gamma) f ps rhs t #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} Abstract [Occ.Occurrence] where #else instance Abstract [Occ.Occurrence] where #endif abstract tel [] = [] abstract tel occ = replicate (size tel) Mixed ++ occ -- TODO: check occurrence #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} Abstract [Polarity] where #else instance Abstract [Polarity] where #endif abstract tel [] = [] abstract tel pol = replicate (size tel) Invariant ++ pol -- TODO: check polarity instance Abstract Projection where abstract tel p = p { projIndex = size tel + projIndex p , projLams = abstract tel $ projLams p } instance Abstract ProjLams where abstract tel (ProjLams lams) = ProjLams $ map (\ (Dom ai (x, _)) -> Arg ai x) (telToList tel) ++ lams instance Abstract Defn where abstract tel d = case d of Axiom{} -> d AbstractDefn d -> AbstractDefn $ abstract tel d Function{ funClauses = cs, funCompiled = cc, funInv = inv , funProjection = Nothing } -> d { funClauses = abstract tel cs , funCompiled = abstract tel cc , funInv = abstract tel inv } Function{ funClauses = cs, funCompiled = cc, funInv = inv , funProjection = Just p } -> -- Andreas, 2015-05-11 if projection was applied to Var 0 -- then abstract over last element of tel (the others are params). if projIndex p > 0 then d' else d' { funClauses = abstract tel1 cs , funCompiled = abstract tel1 cc , funInv = abstract tel1 inv } where d' = d { funProjection = Just $ abstract tel p } tel1 = telFromList $ drop (size tel - 1) $ telToList tel Datatype{ dataPars = np, dataSmallPars = sps, dataNonLinPars = nlps, dataClause = cl } -> d { dataPars = np + size tel , dataSmallPars = abstract tel sps , dataNonLinPars = abstract tel nlps , dataClause = abstract tel cl } Record{ recPars = np, recClause = cl, recTel = tel' } -> d { recPars = np + size tel , recClause = abstract tel cl , recTel = abstract tel tel' } Constructor{ conPars = np } -> d { conPars = np + size tel } Primitive{ primClauses = cs } -> d { primClauses = abstract tel cs } instance Abstract PrimFun where abstract tel (PrimFun x ar def) = PrimFun x (ar + n) $ \ts -> def $ drop n ts where n = size tel instance Abstract Clause where abstract tel (Clause rl rf tel' ps b t catchall unreachable) = Clause rl rf (abstract tel tel') (namedTelVars m tel ++ ps) b t -- nothing to do for t, since it lives under the telescope catchall unreachable where m = size tel + size tel' instance Abstract CompiledClauses where abstract tel Fail = Fail abstract tel (Done xs t) = Done (map (argFromDom . fmap fst) (telToList tel) ++ xs) t abstract tel (Case n bs) = Case (n <&> \ i -> i + size tel) (abstract tel bs) instance Abstract a => Abstract (WithArity a) where abstract tel (WithArity n a) = WithArity n $ abstract tel a instance Abstract a => Abstract (Case a) where abstract tel (Branches cop cs ls m) = Branches cop (abstract tel cs) (abstract tel ls) (abstract tel m) telVars :: Int -> Telescope -> [Arg DeBruijnPattern] telVars m = map (fmap namedThing) . (namedTelVars m) namedTelVars :: Int -> Telescope -> [NamedArg DeBruijnPattern] namedTelVars m EmptyTel = [] namedTelVars m (ExtendTel (Dom info a) tel) = Arg info (namedDBVarP (m-1) $ absName tel) : namedTelVars (m-1) (unAbs tel) instance Abstract FunctionInverse where abstract tel NotInjective = NotInjective abstract tel (Inverse inv) = Inverse $ abstract tel inv #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPABLE #-} Abstract t => Abstract [t] where #else instance Abstract t => Abstract [t] where #endif abstract tel = map (abstract tel) instance Abstract t => Abstract (Maybe t) where abstract tel x = fmap (abstract tel) x instance Abstract v => Abstract (Map k v) where abstract tel m = fmap (abstract tel) m instance Abstract v => Abstract (HashMap k v) where abstract tel m = fmap (abstract tel) m abstractArgs :: Abstract a => Args -> a -> a abstractArgs args x = abstract tel x where tel = foldr (\(Arg info x) -> ExtendTel (Dom info $ sort Prop) . Abs x) EmptyTel $ zipWith (<$) names args names = cycle $ map (stringToArgName . (:[])) ['a'..'z'] --------------------------------------------------------------------------- -- * Substitution and raising/shifting/weakening --------------------------------------------------------------------------- -- | If @permute π : [a]Γ -> [a]Δ@, then @applySubst (renaming _ π) : Term Γ -> Term Δ@ renaming :: forall a. DeBruijn a => Empty -> Permutation -> Substitution' a renaming err p = prependS err gamma $ raiseS $ size p where gamma :: [Maybe a] gamma = inversePermute p (deBruijnVar :: Int -> a) -- gamma = safePermute (invertP (-1) p) $ map deBruijnVar [0..] -- | If @permute π : [a]Γ -> [a]Δ@, then @applySubst (renamingR π) : Term Δ -> Term Γ@ renamingR :: DeBruijn a => Permutation -> Substitution' a renamingR p@(Perm n _) = permute (reverseP p) (map deBruijnVar [0..]) ++# raiseS n -- | The permutation should permute the corresponding context. (right-to-left list) renameP :: Subst t a => Empty -> Permutation -> a -> a renameP err p = applySubst (renaming err p) instance Subst a a => Subst a (Substitution' a) where applySubst rho sgm = composeS rho sgm instance Subst Term Term where applySubst IdS t = t applySubst rho t = case t of Var i es -> lookupS rho i `applyE` applySubst rho es Lam h m -> Lam h $ applySubst rho m Def f es -> defApp f [] $ applySubst rho es Con c ci vs -> Con c ci $ applySubst rho vs MetaV x es -> MetaV x $ applySubst rho es Lit l -> Lit l Level l -> levelTm $ applySubst rho l Pi a b -> uncurry Pi $ applySubst rho (a,b) Sort s -> sortTm $ applySubst rho s Shared p -> Shared $ applySubst rho p DontCare mv -> dontCare $ applySubst rho mv instance Subst t a => Subst t (Ptr a) where applySubst rho = fmap (applySubst rho) instance Subst Term a => Subst Term (Type' a) where applySubst rho (El s t) = applySubst rho s `El` applySubst rho t instance Subst Term Sort where applySubst rho s = case s of Type n -> levelSort $ sub n Prop -> Prop Inf -> Inf SizeUniv -> SizeUniv DLub s1 s2 -> DLub (sub s1) (sub s2) where sub x = applySubst rho x instance Subst Term Level where applySubst rho (Max as) = Max $ applySubst rho as instance Subst Term PlusLevel where applySubst rho l@ClosedLevel{} = l applySubst rho (Plus n l) = Plus n $ applySubst rho l instance Subst Term LevelAtom where applySubst rho (MetaLevel m vs) = MetaLevel m $ applySubst rho vs applySubst rho (BlockedLevel m v) = BlockedLevel m $ applySubst rho v applySubst rho (NeutralLevel _ v) = UnreducedLevel $ applySubst rho v applySubst rho (UnreducedLevel v) = UnreducedLevel $ applySubst rho v instance Subst Term Name where applySubst rho = id #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} Subst Term String where #else instance Subst Term String where #endif applySubst rho = id instance Subst Term ConPatternInfo where applySubst rho (ConPatternInfo mr mt) = ConPatternInfo mr $ applySubst rho mt instance Subst Term Pattern where applySubst rho p = case p of ConP c mt ps -> ConP c (applySubst rho mt) $ applySubst rho ps DotP t -> DotP $ applySubst rho t VarP s -> p AbsurdP p -> AbsurdP $ applySubst rho p LitP l -> p ProjP{} -> p instance DeBruijn NLPat where deBruijnVar i = PVar i [] deBruijnView p = case p of PVar i [] -> Just i PVar{} -> Nothing PWild{} -> Nothing PDef{} -> Nothing PLam{} -> Nothing PPi{} -> Nothing PBoundVar{} -> Nothing -- or... ? PTerm{} -> Nothing -- or... ? applyNLPatSubst :: (Subst Term a) => Substitution' NLPat -> a -> a applyNLPatSubst = applySubst . fmap nlPatToTerm where nlPatToTerm :: NLPat -> Term nlPatToTerm p = case p of PVar i xs -> Var i $ map (Apply . fmap var) xs PTerm u -> u PWild -> __IMPOSSIBLE__ PDef f es -> __IMPOSSIBLE__ PLam i u -> __IMPOSSIBLE__ PPi a b -> __IMPOSSIBLE__ PBoundVar i es -> __IMPOSSIBLE__ instance Subst NLPat NLPat where applySubst rho p = case p of PVar i bvs -> lookupS rho i `applyBV` bvs PWild -> p PDef f es -> PDef f $ applySubst rho es PLam i u -> PLam i $ applySubst rho u PPi a b -> PPi (applySubst rho a) (applySubst rho b) PBoundVar i es -> PBoundVar i $ applySubst rho es PTerm u -> PTerm $ applyNLPatSubst rho u where applyBV :: NLPat -> [Arg Int] -> NLPat applyBV p ys = case p of PVar i xs -> PVar i (xs ++ ys) PTerm u -> PTerm $ u `apply` map (fmap var) ys PWild -> __IMPOSSIBLE__ PDef f es -> __IMPOSSIBLE__ PLam i u -> __IMPOSSIBLE__ PPi a b -> __IMPOSSIBLE__ PBoundVar i es -> __IMPOSSIBLE__ instance Subst NLPat NLPType where applySubst rho (NLPType s a) = NLPType (applySubst rho s) (applySubst rho a) instance Subst NLPat RewriteRule where applySubst rho (RewriteRule q gamma f ps rhs t) = RewriteRule q (applyNLPatSubst rho gamma) f (applySubst (liftS n rho) ps) (applyNLPatSubst (liftS n rho) rhs) (applyNLPatSubst (liftS n rho) t) where n = size gamma instance Subst t a => Subst t (Blocked a) where applySubst rho b = fmap (applySubst rho) b instance Subst Term DisplayForm where applySubst rho (Display n ps v) = Display n (applySubst (liftS 1 rho) ps) (applySubst (liftS n rho) v) instance Subst Term DisplayTerm where applySubst rho (DTerm v) = DTerm $ applySubst rho v applySubst rho (DDot v) = DDot $ applySubst rho v applySubst rho (DCon c ci vs) = DCon c ci $ applySubst rho vs applySubst rho (DDef c es) = DDef c $ applySubst rho es applySubst rho (DWithApp v vs es) = uncurry3 DWithApp $ applySubst rho (v, vs, es) instance Subst t a => Subst t (Tele a) where applySubst rho EmptyTel = EmptyTel applySubst rho (ExtendTel t tel) = uncurry ExtendTel $ applySubst rho (t, tel) instance Subst Term Constraint where applySubst rho c = case c of ValueCmp cmp a u v -> ValueCmp cmp (rf a) (rf u) (rf v) ElimCmp ps a v e1 e2 -> ElimCmp ps (rf a) (rf v) (rf e1) (rf e2) TypeCmp cmp a b -> TypeCmp cmp (rf a) (rf b) TelCmp a b cmp tel1 tel2 -> TelCmp (rf a) (rf b) cmp (rf tel1) (rf tel2) SortCmp cmp s1 s2 -> SortCmp cmp (rf s1) (rf s2) LevelCmp cmp l1 l2 -> LevelCmp cmp (rf l1) (rf l2) Guarded c cs -> Guarded (rf c) cs IsEmpty r a -> IsEmpty r (rf a) CheckSizeLtSat t -> CheckSizeLtSat (rf t) FindInScope m b cands -> FindInScope m b (rf cands) UnBlock{} -> c where rf x = applySubst rho x instance Subst Term ModuleParameters where applySubst rho mp = mp { mpSubstitution = applySubst rho $ mpSubstitution mp } instance Subst Term A.NamedDotPattern where applySubst rho (A.NamedDot x v a) = A.NamedDot x (applySubst rho v) (applySubst rho a) instance Subst Term A.StrippedDotPattern where applySubst rho (A.StrippedDot e v a) = A.StrippedDot e (applySubst rho v) (applySubst rho a) instance Subst t a => Subst t (Elim' a) where applySubst rho e = case e of Apply v -> Apply $ applySubst rho v Proj{} -> e instance Subst t a => Subst t (Abs a) where applySubst rho (Abs x a) = Abs x $ applySubst (liftS 1 rho) a applySubst rho (NoAbs x a) = NoAbs x $ applySubst rho a instance Subst t a => Subst t (Arg a) where applySubst rho = fmap (applySubst rho) instance Subst t a => Subst t (Named name a) where applySubst rho = fmap (applySubst rho) instance Subst t a => Subst t (Dom a) where applySubst rho = fmap (applySubst rho) instance Subst t a => Subst t (Maybe a) where applySubst rho = fmap (applySubst rho) instance Subst t a => Subst t [a] where applySubst rho = map (applySubst rho) instance (Ord k, Subst t a) => Subst t (Map k a) where applySubst rho = fmap (applySubst rho) instance Subst Term () where applySubst _ _ = () instance (Subst t a, Subst t b) => Subst t (a, b) where applySubst rho (x,y) = (applySubst rho x, applySubst rho y) instance (Subst t a, Subst t b, Subst t c) => Subst t (a, b, c) where applySubst rho (x,y,z) = (applySubst rho x, applySubst rho y, applySubst rho z) instance (Subst t a, Subst t b, Subst t c, Subst t d) => Subst t (a, b, c, d) where applySubst rho (x,y,z,u) = (applySubst rho x, applySubst rho y, applySubst rho z, applySubst rho u) instance Subst Term Candidate where applySubst rho (Candidate u t eti ov) = Candidate (applySubst rho u) (applySubst rho t) eti ov instance Subst Term EqualityView where applySubst rho (OtherType t) = OtherType (applySubst rho t) applySubst rho (EqualityType s eq l t a b) = EqualityType (applySubst rho s) eq (map (applySubst rho) l) (applySubst rho t) (applySubst rho a) (applySubst rho b) instance DeBruijn DeBruijnPattern where debruijnNamedVar n i = VarP $ DBPatVar n i deBruijnView (VarP x) = Just $ dbPatVarIndex x deBruijnView _ = Nothing fromPatternSubstitution :: PatternSubstitution -> Substitution fromPatternSubstitution = fmap patternToTerm applyPatSubst :: (Subst Term a) => PatternSubstitution -> a -> a applyPatSubst = applySubst . fromPatternSubstitution instance Subst DeBruijnPattern DeBruijnPattern where applySubst IdS p = p applySubst rho p = case p of VarP x -> useName (dbPatVarName x) $ lookupS rho $ dbPatVarIndex x DotP u -> DotP $ applyPatSubst rho u ConP c ci ps -> ConP c ci $ applySubst rho ps AbsurdP p -> AbsurdP $ applySubst rho p LitP x -> p ProjP{} -> p where useName :: PatVarName -> DeBruijnPattern -> DeBruijnPattern useName n (VarP x) | isUnderscore (dbPatVarName x) = debruijnNamedVar n (dbPatVarIndex x) useName _ x = x --------------------------------------------------------------------------- -- * Projections --------------------------------------------------------------------------- -- | @projDropParsApply proj o args = 'projDropPars' proj o `'apply'` args@ -- -- This function is an optimization, saving us from construction lambdas we -- immediately remove through application. projDropParsApply :: Projection -> ProjOrigin -> Args -> Term projDropParsApply (Projection prop d r _ lams) o args = case initLast $ getProjLams lams of -- If we have no more abstractions, we must be a record field -- (projection applied already to record value). Nothing -> if proper then Def d $ map Apply args else __IMPOSSIBLE__ Just (pars, Arg i y) -> let core = if proper then Lam i $ Abs y $ Var 0 [Proj o d] else Lam i $ Abs y $ Def d [Apply $ Var 0 [] <$ r] -- Issue2226: get ArgInfo for principal argument from projFromType -- Now drop pars many args (pars', args') = dropCommon pars args -- We only have to abstract over the parameters that exceed the arguments. -- We only have to apply to the arguments that exceed the parameters. in List.foldr (\ (Arg ai x) -> Lam ai . NoAbs x) (core `apply` args') pars' where proper = isJust prop --------------------------------------------------------------------------- -- * Telescopes --------------------------------------------------------------------------- -- ** Telescope view of a type type TelView = TelV Type data TelV a = TelV { theTel :: Tele (Dom a), theCore :: a } deriving (Typeable, Show, Functor) deriving instance (Subst t a, Eq a) => Eq (TelV a) deriving instance (Subst t a, Ord a) => Ord (TelV a) -- | Takes off all exposed function domains from the given type. -- This means that it does not reduce to expose @Pi@-types. telView' :: Type -> TelView telView' = telView'UpTo (-1) -- | @telView'UpTo n t@ takes off the first @n@ exposed function types of @t@. -- Takes off all (exposed ones) if @n < 0@. telView'UpTo :: Int -> Type -> TelView telView'UpTo 0 t = TelV EmptyTel t telView'UpTo n t = case ignoreSharing $ unEl t of Pi a b -> absV a (absName b) $ telView'UpTo (n - 1) (absBody b) _ -> TelV EmptyTel t where absV a x (TelV tel t) = TelV (ExtendTel a (Abs x tel)) t -- ** Creating telescopes from lists of types -- | Turn a typed binding @(x1 .. xn : A)@ into a telescope. bindsToTel' :: (Name -> a) -> [Name] -> Dom Type -> ListTel' a bindsToTel' f [] t = [] bindsToTel' f (x:xs) t = fmap (f x,) t : bindsToTel' f xs (raise 1 t) bindsToTel :: [Name] -> Dom Type -> ListTel bindsToTel = bindsToTel' nameToArgName -- | Turn a typed binding @(x1 .. xn : A)@ into a telescope. bindsWithHidingToTel' :: (Name -> a) -> [WithHiding Name] -> Dom Type -> ListTel' a bindsWithHidingToTel' f [] t = [] bindsWithHidingToTel' f (WithHiding h x : xs) t = fmap (f x,) (mapHiding (mappend h) t) : bindsWithHidingToTel' f xs (raise 1 t) bindsWithHidingToTel :: [WithHiding Name] -> Dom Type -> ListTel bindsWithHidingToTel = bindsWithHidingToTel' nameToArgName -- ** Abstracting in terms and types -- | @mkPi dom t = telePi (telFromList [dom]) t@ mkPi :: Dom (ArgName, Type) -> Type -> Type mkPi (Dom info (x, a)) b = el $ Pi (Dom info a) (mkAbs x b) where el = El $ dLub (getSort a) (Abs x (getSort b)) -- dLub checks x freeIn mkLam :: Arg ArgName -> Term -> Term mkLam a v = Lam (argInfo a) (Abs (unArg a) v) telePi' :: (Abs Type -> Abs Type) -> Telescope -> Type -> Type telePi' reAbs = telePi where telePi EmptyTel t = t telePi (ExtendTel u tel) t = el $ Pi u $ reAbs b where b = (`telePi` t) <$> tel s1 = getSort u s2 = getSort <$> b el = El $ dLub s1 s2 -- | Uses free variable analysis to introduce 'NoAbs' bindings. telePi :: Telescope -> Type -> Type telePi = telePi' reAbs -- | Everything will be an 'Abs'. telePi_ :: Telescope -> Type -> Type telePi_ = telePi' id {- OLD -- | Everything will be a pi. telePi_ EmptyTel t = t telePi_ (ExtendTel u tel) t = el $ Pi u b where el = El (dLub s1 s2) b = fmap (flip telePi_ t) tel s1 = getSort $ unDom u s2 = fmap getSort b -} -- | Abstract over a telescope in a term, producing lambdas. -- Dumb abstraction: Always produces 'Abs', never 'NoAbs'. -- -- The implementation is sound because 'Telescope' does not use 'NoAbs'. teleLam :: Telescope -> Term -> Term teleLam EmptyTel t = t teleLam (ExtendTel u tel) t = Lam (domInfo u) $ flip teleLam t <$> tel -- | Performs void ('noAbs') abstraction over telescope. class TeleNoAbs a where teleNoAbs :: a -> Term -> Term instance TeleNoAbs ListTel where teleNoAbs tel t = foldr (\ (Dom ai (x, _)) -> Lam ai . NoAbs x) t tel instance TeleNoAbs Telescope where teleNoAbs tel = teleNoAbs $ telToList tel -- ** Telescope typing -- | Given arguments @vs : tel@ (vector typing), extract their individual types. -- Returns @Nothing@ is @tel@ is not long enough. typeArgsWithTel :: Telescope -> [Term] -> Maybe [Dom Type] typeArgsWithTel _ [] = return [] typeArgsWithTel (ExtendTel dom tel) (v : vs) = (dom :) <$> typeArgsWithTel (absApp tel v) vs typeArgsWithTel EmptyTel{} (_:_) = Nothing --------------------------------------------------------------------------- -- * Clauses --------------------------------------------------------------------------- -- | In compiled clauses, the variables in the clause body are relative to the -- pattern variables (including dot patterns) instead of the clause telescope. compiledClauseBody :: Clause -> Maybe Term compiledClauseBody cl = applySubst (renamingR perm) $ clauseBody cl where perm = fromMaybe __IMPOSSIBLE__ $ clausePerm cl --------------------------------------------------------------------------- -- * Syntactic equality and order -- -- Needs weakening. --------------------------------------------------------------------------- deriving instance Eq Substitution deriving instance Ord Substitution deriving instance Eq Sort deriving instance Ord Sort deriving instance Eq Level deriving instance Ord Level deriving instance Eq PlusLevel deriving instance Ord LevelAtom deriving instance Eq NotBlocked deriving instance Ord NotBlocked deriving instance Eq t => Eq (Blocked t) deriving instance Ord t => Ord (Blocked t) deriving instance Eq Candidate deriving instance (Subst t a, Eq a) => Eq (Tele a) deriving instance (Subst t a, Ord a) => Ord (Tele a) deriving instance Eq Constraint deriving instance Eq Section instance Ord PlusLevel where compare ClosedLevel{} Plus{} = LT compare Plus{} ClosedLevel{} = GT compare (ClosedLevel n) (ClosedLevel m) = compare n m -- Compare on the atom first. Makes most sense for levelMax. compare (Plus n a) (Plus m b) = compare (a,n) (b,m) instance Eq LevelAtom where (==) = (==) `on` unLevelAtom -- | Syntactic 'Type' equality, ignores sort annotations. instance Eq a => Eq (Type' a) where (==) = (==) `on` unEl instance Ord a => Ord (Type' a) where compare = compare `on` unEl -- | Syntactic 'Term' equality, ignores stuff below @DontCare@ and sharing. instance Eq Term where Var x vs == Var x' vs' = x == x' && vs == vs' Lam h v == Lam h' v' = h == h' && v == v' Lit l == Lit l' = l == l' Def x vs == Def x' vs' = x == x' && vs == vs' Con x _ vs == Con x' _ vs' = x == x' && vs == vs' Pi a b == Pi a' b' = a == a' && b == b' Sort s == Sort s' = s == s' Level l == Level l' = l == l' MetaV m vs == MetaV m' vs' = m == m' && vs == vs' DontCare _ == DontCare _ = True Shared p == Shared q = p == q || derefPtr p == derefPtr q Shared p == b = derefPtr p == b a == Shared q = a == derefPtr q _ == _ = False instance Ord Term where Shared a `compare` Shared x | a == x = EQ Shared a `compare` x = compare (derefPtr a) x a `compare` Shared x = compare a (derefPtr x) Var a b `compare` Var x y = compare (a, b) (x, y) Var{} `compare` _ = LT _ `compare` Var{} = GT Def a b `compare` Def x y = compare (a, b) (x, y) Def{} `compare` _ = LT _ `compare` Def{} = GT Con a _ b `compare` Con x _ y = compare (a, b) (x, y) Con{} `compare` _ = LT _ `compare` Con{} = GT Lit a `compare` Lit x = compare a x Lit{} `compare` _ = LT _ `compare` Lit{} = GT Lam a b `compare` Lam x y = compare (a, b) (x, y) Lam{} `compare` _ = LT _ `compare` Lam{} = GT Pi a b `compare` Pi x y = compare (a, b) (x, y) Pi{} `compare` _ = LT _ `compare` Pi{} = GT Sort a `compare` Sort x = compare a x Sort{} `compare` _ = LT _ `compare` Sort{} = GT Level a `compare` Level x = compare a x Level{} `compare` _ = LT _ `compare` Level{} = GT MetaV a b `compare` MetaV x y = compare (a, b) (x, y) MetaV{} `compare` _ = LT _ `compare` MetaV{} = GT DontCare{} `compare` DontCare{} = EQ -- | Equality of binders relies on weakening -- which is a specical case of renaming -- which is a specical case of substitution. instance (Subst t a, Eq a) => Eq (Abs a) where NoAbs _ a == NoAbs _ b = a == b Abs _ a == Abs _ b = a == b a == b = absBody a == absBody b instance (Subst t a, Ord a) => Ord (Abs a) where NoAbs _ a `compare` NoAbs _ b = a `compare` b Abs _ a `compare` Abs _ b = a `compare` b a `compare` b = absBody a `compare` absBody b instance (Subst t a, Eq a) => Eq (Elim' a) where Apply a == Apply b = a == b Proj _ x == Proj _ y = x == y _ == _ = False instance (Subst t a, Ord a) => Ord (Elim' a) where Apply a `compare` Apply b = a `compare` b Proj _ x `compare` Proj _ y = x `compare` y Apply{} `compare` Proj{} = LT Proj{} `compare` Apply{} = GT --------------------------------------------------------------------------- -- * Level stuff --------------------------------------------------------------------------- -- | The ``rule'', if Agda is considered as a functional -- pure type system (pts). -- -- TODO: This needs to be properly implemented, requiring -- refactoring of Agda's handling of levels. -- Without impredicativity or 'SizeUniv', Agda's pts rule is -- just the least upper bound, which is total and commutative. -- The handling of levels relies on this simplification. pts :: Sort -> Sort -> Sort pts = sLub sLub :: Sort -> Sort -> Sort sLub s Prop = s sLub Prop s = s sLub Inf _ = Inf sLub _ Inf = Inf sLub SizeUniv s = s -- one can freely quantify over sizes in any Set sLub _ SizeUniv = SizeUniv -- but everything resulting in a size lives in the SizeUniv sLub (Type (Max as)) (Type (Max bs)) = Type $ levelMax (as ++ bs) -- sLub (DLub a b) c = DLub (sLub a c) b -- no longer commutative! sLub (DLub a NoAbs{}) c = __IMPOSSIBLE__ sLub (DLub a (Abs x b)) c = DLub a $ Abs x $ sLub b $ raise 1 c sLub a (DLub b c) = DLub (sLub a b) c -- | Dependent least upper bound, to assign a level to expressions -- like @forall i -> Set i@. -- -- @dLub s1 \i.s2 = \omega@ if @i@ appears in the rigid variables of @s2@. dLub :: Sort -> Abs Sort -> Sort dLub s1 (NoAbs _ s2) = sLub s1 s2 dLub s1 b@(Abs _ s2) = case occurrence 0 s2 of Flexible _ -> DLub s1 b -- Andreas, 2017-01-18, issue #2408: -- The sort of @.(a : A) → Set (f a)@ in context @f : .A → Level@ -- is @dLub Set λ a → Set (lsuc (f a))@, but @DLub@s are not serialized. -- Alternatives: -- 1. -- Irrelevantly -> sLub s1 (absApp b $ DontCare $ Sort Prop) -- We cheat here by simplifying the sort to @Set (lsuc (f *))@ -- where * is a dummy value. The rationale is that @f * = f a@ (irrelevance!) -- and that if we already have a neutral level @f a@ -- it should not hurt to have @f *@ even if type @A@ is empty. -- However: sorts are printed in error messages when sorts do not match. -- Also, sorts with a dummy like Prop would be ill-typed. -- 2. We keep the DLub, and serialize it. -- That's clean and principled, even though DLubs make level solving harder. Irrelevantly -> DLub s1 b NoOccurrence -> sLub s1 (noabsApp __IMPOSSIBLE__ b) -- Free.Unused -> sLub s1 (absApp b __IMPOSSIBLE__) -- triggers Issue784 Free.Unused -> DLub s1 b StronglyRigid -> Inf Unguarded -> Inf WeaklyRigid -> Inf lvlView :: Term -> Level lvlView v = case ignoreSharing v of Level l -> l Sort (Type l) -> l _ -> Max [Plus 0 $ UnreducedLevel v] levelMax :: [PlusLevel] -> Level levelMax as0 = Max $ ns ++ List.sort bs where as = Prelude.concatMap expand as0 -- ns is empty or a singleton ns = case [ n | ClosedLevel n <- as, n > 0 ] of [] -> [] ns -> [ ClosedLevel n | let n = Prelude.maximum ns, n > greatestB ] bs = subsume [ b | b@Plus{} <- as ] greatestB | null bs = 0 | otherwise = Prelude.maximum [ n | Plus n _ <- bs ] expand l@ClosedLevel{} = [l] expand (Plus n l) = map (plus n) $ expand0 $ expandAtom l expand0 [] = [ClosedLevel 0] expand0 as = as expandAtom l = case l of BlockedLevel _ v -> expandTm v NeutralLevel _ v -> expandTm v UnreducedLevel v -> expandTm v MetaLevel{} -> [Plus 0 l] where expandTm v = case ignoreSharing v of Level (Max as) -> as Sort (Type (Max as)) -> as _ -> [Plus 0 l] plus n (ClosedLevel m) = ClosedLevel (n + m) plus n (Plus m l) = Plus (n + m) l subsume (ClosedLevel{} : _) = __IMPOSSIBLE__ subsume [] = [] subsume (Plus n a : bs) | not $ null ns = subsume bs | otherwise = Plus n a : subsume [ b | b@(Plus _ a') <- bs, a /= a' ] where ns = [ m | Plus m a' <- bs, a == a', m > n ] sortTm :: Sort -> Term sortTm (Type l) = Sort $ levelSort l sortTm s = Sort s levelSort :: Level -> Sort levelSort (Max as) | List.any (levelIs Inf ) as = Inf | List.any (levelIs SizeUniv) as = SizeUniv where levelIs s ClosedLevel{} = False levelIs s (Plus _ l) = atomIs s l atomIs s (NeutralLevel _ a) = tmIs s a atomIs s (UnreducedLevel a) = tmIs s a atomIs s MetaLevel{} = False atomIs s BlockedLevel{} = False tmIs s (Sort s') = s == s' tmIs s (Shared p) = tmIs s $ derefPtr p tmIs s _ = False levelSort l = case ignoreSharing $ levelTm l of Sort s -> s _ -> Type l levelTm :: Level -> Term levelTm l = case l of Max [Plus 0 l] -> unLevelAtom l _ -> Level l unLevelAtom :: LevelAtom -> Term unLevelAtom (MetaLevel x es) = MetaV x es unLevelAtom (NeutralLevel _ v) = v unLevelAtom (UnreducedLevel v) = v unLevelAtom (BlockedLevel _ v) = v Agda-2.5.3/src/full/Agda/TypeChecking/ProjectionLike.hs0000644000000000000000000003574413154613124020777 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Dropping initial arguments (``parameters'') from a function which can be -- easily reconstructed from its principal argument. -- -- A function which has such parameters is called ``projection-like''. -- -- The motivation for this optimization comes from the use of nested records. -- -- First, let us look why proper projections need not store the parameters: -- The type of a projection @f@ is of the form -- @ -- f : Γ → R Γ → C -- @ -- where @R@ is the record type and @C@ is the type of the field @f@. -- Given a projection application -- @ -- p pars u -- @ -- we know that the type of the principal argument @u@ is -- @ -- u : R pars -- @ -- thus, the parameters @pars@ are redundant in the projection application -- if we can always infer the type of @u@. -- For projections, this is case, because the principal argument @u@ must be -- neutral; otherwise, if it was a record value, we would have a redex, -- yet Agda maintains a β-normal form. -- -- The situation for projections can be generalized to ``projection-like'' -- functions @f@. Conditions: -- -- 1. The type of @f@ is of the form @f : Γ → D Γ → ...@ for some -- type constructor @D@ which can never reduce. -- -- 2. For every reduced welltyped application @f pars u ...@, -- the type of @u@ is inferable. -- -- This then allows @pars@ to be dropped always. -- -- Condition 2 is approximated by a bunch of criteria, for details see function -- 'makeProjection'. -- -- Typical projection-like functions are compositions of projections -- which arise from nested records. -- -- Notes: -- -- 1. This analysis could be dualized to ``constructor-like'' functions -- whose parameters are reconstructable from the target type. -- But such functions would need to be fully applied. -- -- 2. A more general analysis of which arguments are reconstructible -- can be found in -- -- Jason C. Reed, Redundancy elimination for LF -- LFTMP 2004. module Agda.TypeChecking.ProjectionLike where import Control.Monad import qualified Data.Map as Map import Data.Monoid (Any(..), getAny) import Agda.Syntax.Abstract.Name import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.TypeChecking.Monad import Agda.TypeChecking.Free (runFree, IgnoreSorts(..)) import Agda.TypeChecking.Substitute import Agda.TypeChecking.Positivity import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce (reduce) import Agda.TypeChecking.DropArgs import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Permutation import Agda.Utils.Pretty ( prettyShow ) import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | View for a @Def f (Apply a : es)@ where @isProjection f@. -- Used for projection-like @f@s. data ProjectionView = ProjectionView { projViewProj :: QName , projViewSelf :: Arg Term , projViewSpine :: Elims } -- ^ A projection or projection-like function, applied to its -- principal argument | LoneProjectionLike QName ArgInfo -- ^ Just a lone projection-like function, missing its principal -- argument (from which we could infer the parameters). | NoProjection Term -- ^ Not a projection or projection-like thing. -- | Semantics of 'ProjectionView'. unProjView :: ProjectionView -> Term unProjView pv = case pv of ProjectionView f a es -> Def f (Apply a : es) LoneProjectionLike f ai -> Def f [] NoProjection v -> v -- | Top-level 'ProjectionView' (no reduction). {-# SPECIALIZE projView :: Term -> TCM ProjectionView #-} projView :: HasConstInfo m => Term -> m ProjectionView projView v = do let fallback = return $ NoProjection v case ignoreSharing v of Def f es -> caseMaybeM (isProjection f) fallback $ \ isP -> do if projIndex isP <= 0 then fallback else do case es of [] -> return $ LoneProjectionLike f $ projArgInfo isP Apply a : es -> return $ ProjectionView f a es -- Since a projection is a function, it cannot be projected itself. Proj{} : _ -> __IMPOSSIBLE__ _ -> fallback -- | Reduce away top-level projection like functions. -- (Also reduces projections, but they should not be there, -- since Internal is in lambda- and projection-beta-normal form.) -- reduceProjectionLike :: Term -> TCM Term reduceProjectionLike v = do -- Andreas, 2013-11-01 make sure we do not reduce a constructor -- because that could be folded back into a literal by reduce. pv <- projView v case pv of ProjectionView{} -> onlyReduceProjections $ reduce v -- ordinary reduce, only different for Def's _ -> return v -- | Turn prefix projection-like function application into postfix ones. -- This does just one layer, such that the top spine contains -- the projection-like functions as projections. -- Used in 'compareElims' in @TypeChecking.Conversion@ -- and in "Agda.TypeChecking.CheckInternal". -- -- If the 'Bool' is 'True', a lone projection like function will be -- turned into a lambda-abstraction, expecting the principal argument. -- If the 'Bool' is 'False', it will be returned unaltered. -- -- No precondition. -- Preserves constructorForm, since it really does only something -- on (applications of) projection-like functions. elimView :: Bool -> Term -> TCM Term elimView loneProjToLambda v = do reportSDoc "tc.conv.elim" 30 $ text "elimView of " <+> prettyTCM v reportSLn "tc.conv.elim" 50 $ "v = " ++ show v v <- reduceProjectionLike v reportSDoc "tc.conv.elim" 40 $ text "elimView (projections reduced) of " <+> prettyTCM v pv <- projView v case pv of NoProjection{} -> return v LoneProjectionLike f ai | loneProjToLambda -> return $ Lam ai $ Abs "r" $ Var 0 [Proj ProjPrefix f] | otherwise -> return v ProjectionView f a es -> (`applyE` (Proj ProjPrefix f : es)) <$> elimView loneProjToLambda (unArg a) -- | Which @Def@types are eligible for the principle argument -- of a projection-like function? eligibleForProjectionLike :: QName -> TCM Bool eligibleForProjectionLike d = eligible . theDef <$> getConstInfo d where eligible = \case Datatype{} -> True Record{} -> True Axiom{} -> True Function{} -> False Primitive{} -> False Constructor{} -> __IMPOSSIBLE__ AbstractDefn d -> eligible d -- Andreas, 2017-08-14, issue #2682: -- Abstract records still export the projections. -- Andreas, 2016-10-11 AIM XXIV -- Projection-like at abstract types violates the parameter reconstructibility property. -- See test/Fail/AbstractTypeProjectionLike. -- | Turn a definition into a projection if it looks like a projection. -- -- Conditions for projection-likeness of @f@: -- -- 1. The type of @f@ must be of the shape @Γ → D Γ → C@ for @D@ -- a name (@Def@) which is 'eligibleForProjectionLike': -- @data@ / @record@ / @postulate@. -- -- 2. The application of f should only get stuck if the principal argument -- is inferable (neutral). Thus: -- -- a. @f@ cannot have absurd clauses (which are stuck even if the principal -- argument is a constructor) -- -- b. @f@ cannot be abstract as it does not reduce outside abstract blocks -- (always stuck). -- -- c. @f@ cannot match on other arguments than the principal argument. -- -- d. @f@ cannot match deeply. -- -- e. @f@s body may not mention the paramters. -- -- For internal reasons: -- -- 3. @f@ cannot be constructor headed -- -- 4. @f@ cannot be recursive, since we have not implemented a function -- which goes through the bodies of the @f@ and the mutually recursive -- functions and drops the parameters from all applications of @f@. -- -- Examples for these reasons: see test/Succeed/NotProjectionLike.agda makeProjection :: QName -> TCM () makeProjection x = -- if True then return () else do inTopContext $ do reportSLn "tc.proj.like" 70 $ "Considering " ++ prettyShow x ++ " for projection likeness" defn <- getConstInfo x let t = defType defn reportSDoc "tc.proj.like" 20 $ sep [ text "Checking for projection likeness " , prettyTCM x <+> text " : " <+> prettyTCM t ] case theDef defn of Function{funClauses = cls} | any (isNothing . clauseBody) cls -> reportSLn "tc.proj.like" 30 $ " projection-like functions cannot have absurd clauses" -- Constructor-headed functions can't be projection-like (at the moment). The reason -- for this is that invoking constructor-headedness will circumvent the inference of -- the dropped arguments. -- Nor can abstract definitions be projection-like since they won't reduce -- outside the abstract block. def@Function{funProjection = Nothing, funClauses = cls, funCompiled = cc0, funInv = NotInjective, funMutual = Just [], -- Andreas, 2012-09-28: only consider non-mutual funs (or those whose recursion status has not yet been determined) funAbstr = ConcreteDef} -> do ps0 <- filterM validProj $ candidateArgs [] t reportSLn "tc.proj.like" 30 $ if null ps0 then " no candidates found" else " candidates: " ++ show ps0 unless (null ps0) $ do -- Andreas 2012-09-26: only consider non-recursive functions for proj.like. -- Issue 700: problems with recursive funs. in term.checker and reduction ifM recursive (reportSLn "tc.proj.like" 30 $ " recursive functions are not considered for projection-likeness") $ do {- else -} case lastMaybe (filter (checkOccurs cls . snd) ps0) of Nothing -> reportSLn "tc.proj.like" 50 $ " occurs check failed\n clauses = " ++ show cls Just (d, n) -> do -- Yes, we are projection-like! reportSDoc "tc.proj.like" 10 $ sep [ prettyTCM x <+> text " : " <+> prettyTCM t , text $ " is projection like in argument " ++ show n ++ " for type " ++ show d ] __CRASH_WHEN__ "tc.proj.like.crash" 1000 let cls' = map (dropArgs n) cls cc = dropArgs n cc0 reportSLn "tc.proj.like" 60 $ " rewrote clauses to\n " ++ show cc -- Andreas, 2013-10-20 build parameter dropping function let pIndex = n + 1 tel = take pIndex $ telToList $ theTel $ telView' t unless (length tel == pIndex) __IMPOSSIBLE__ let projection = Projection { projProper = Nothing , projOrig = x , projFromType = d , projIndex = pIndex , projLams = ProjLams $ map (\ (Dom ai (y, _)) -> Arg ai y) tel } let newDef = def { funProjection = Just projection , funClauses = cls' , funCompiled = cc , funInv = dropArgs n $ funInv def } addConstant x $ defn { defPolarity = drop n $ defPolarity defn , defArgOccurrences = drop n $ defArgOccurrences defn , defDisplay = [] , theDef = newDef } Function{funInv = Inverse{}} -> reportSLn "tc.proj.like" 30 $ " injective functions can't be projections" Function{funAbstr = AbstractDef} -> reportSLn "tc.proj.like" 30 $ " abstract functions can't be projections" Function{funProjection = Just{}} -> reportSLn "tc.proj.like" 30 $ " already projection like" _ -> reportSLn "tc.proj.like" 30 $ " not a function" where -- @validProj (d,n)@ checks whether the head @d@ of the type of the -- @n@th argument is injective in all args (i.d. being name of data/record/axiom). validProj :: (Arg QName, Int) -> TCM Bool validProj (_, 0) = return False validProj (d, _) = eligibleForProjectionLike (unArg d) -- NOTE: If the following definition turns out to be slow, then -- one could perhaps reuse information computed by the termination -- and/or positivity checkers. recursive = do occs <- computeOccurrences x let xocc = Map.lookup (ADef x) occs case xocc of Just (_ : _) -> return True -- recursive occurrence _ -> return False checkOccurs cls n = all (nonOccur n) cls nonOccur n cl = and [ take n p == [0..n - 1] , onlyMatch n ps -- projection-like functions are only allowed to match on the eliminatee -- otherwise we may end up projecting from constructor applications, in -- which case we can't reconstruct the dropped parameters , checkBody m n b ] where Perm _ p = fromMaybe __IMPOSSIBLE__ $ clausePerm cl ps = namedClausePats cl b = compiledClauseBody cl -- Renumbers variables to match order in patterns -- and includes dot patterns as variables. m = size $ concatMap patternVars ps -- This also counts dot patterns! onlyMatch n ps = all (shallowMatch . namedArg) (take 1 ps1) && noMatches (ps0 ++ drop 1 ps1) where (ps0, ps1) = splitAt n ps shallowMatch (ConP _ _ ps) = noMatches ps shallowMatch _ = True noMatches = all (noMatch . namedArg) noMatch ConP{} = False noMatch LitP{} = False noMatch ProjP{}= False noMatch VarP{} = True noMatch DotP{} = True noMatch AbsurdP{} = True -- Make sure non of the parameters occurs in the body of the function. checkBody m n b = not . getAny $ runFree badVar IgnoreNot b where badVar x = Any $ m - n <= x && x < m -- @candidateArgs [var 0,...,var(n-1)] t@ adds @(n,d)@ to the output, -- if @t@ is a function-type with domain @t 0 .. (n-1)@ -- (the domain of @t@ is the type of the arg @n@). -- -- This means that from the type of arg @n@ all previous arguments -- can be computed by a simple matching. -- (Provided the @d@ is data/record/postulate, checked in @validProj@). -- -- E.g. f : {x : _}(y : _){z : _} -> D x y z -> ... -- will return (D,3) as a candidate (amongst maybe others). -- candidateArgs :: [Term] -> Type -> [(Arg QName, Int)] candidateArgs vs t = case ignoreSharing $ unEl t of Pi a b | Def d es <- ignoreSharing $ unEl $ unDom a, Just us <- allApplyElims es, vs == map unArg us -> (d <$ argFromDom a, length vs) : candidateRec b | otherwise -> candidateRec b _ -> [] where candidateRec NoAbs{} = [] candidateRec (Abs x t) = candidateArgs (var (size vs) : vs) t Agda-2.5.3/src/full/Agda/TypeChecking/Serialise.hs0000644000000000000000000002030413154613124017760 0ustar0000000000000000 -- Andreas, Makoto, Francesco 2014-10-15 AIM XX: -- -O2 does not have any noticable effect on runtime -- but sabotages cabal repl with -Werror -- (due to a conflict with --interactive warning) -- {-# OPTIONS_GHC -O2 #-} -- | Structure-sharing serialisation of Agda interface files. -- -!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!- -- NOTE: Every time the interface format is changed the interface -- version number should be bumped _in the same patch_. -- -- See 'currentInterfaceVersion' below. -- -- -!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!- module Agda.TypeChecking.Serialise ( encode, encodeFile, encodeInterface , decode, decodeFile, decodeInterface, decodeHashes , EmbPrj ) where import Control.Applicative import Control.Arrow (second) import Control.DeepSeq import qualified Control.Exception as E import Control.Monad import Control.Monad.Reader import Control.Monad.State.Strict import Data.Array.IArray import Data.Word import qualified Data.ByteString.Lazy as L import qualified Data.HashTable.IO as H import qualified Data.Map as Map import qualified Data.Binary as B import qualified Data.Binary.Get as B import qualified Data.Binary.Put as B import qualified Data.List as List import Data.Function import qualified Codec.Compression.GZip as G import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TypeChecking.Serialise.Base import Agda.TypeChecking.Serialise.Instances () import Agda.TypeChecking.Monad import Agda.Utils.Hash import Agda.Utils.IORef import Agda.Utils.Lens import Agda.Utils.Except -- Note that the Binary instance for Int writes 64 bits, but throws -- away the 32 high bits when reading (at the time of writing, on -- 32-bit machines). Word64 does not have these problems. currentInterfaceVersion :: Word64 currentInterfaceVersion = 20170907 * 10 + 0 -- | Encodes something. To ensure relocatability file paths in -- positions are replaced with module names. encode :: EmbPrj a => a -> TCM L.ByteString encode a = do collectStats <- hasVerbosity "profile.serialize" 20 fileMod <- sourceToModule newD@(Dict nD sD bD iD dD _tD _nameD _qnameD nC sC bC iC dC tC nameC qnameC stats _ _) <- liftIO $ emptyDict collectStats root <- liftIO $ (`runReaderT` newD) $ do icodeFileMod fileMod -- Only fills absPathD from fileMod icode a nL <- benchSort $ l nD sL <- benchSort $ l sD bL <- benchSort $ l bD iL <- benchSort $ l iD dL <- benchSort $ l dD -- Record reuse statistics. verboseS "profile.sharing" 10 $ do statistics "pointers" tC verboseS "profile.serialize" 10 $ do statistics "Integer" iC statistics "String" sC statistics "ByteString" bC statistics "Double" dC statistics "Node" nC statistics "Shared Term" tC statistics "A.QName" qnameC statistics "A.Name" nameC when collectStats $ do stats <- Map.fromList . map (second toInteger) <$> do liftIO $ H.toList stats modifyStatistics $ Map.union stats -- Encode hashmaps and root, and compress. bits1 <- Bench.billTo [ Bench.Serialization, Bench.BinaryEncode ] $ return $!! B.encode (root, nL, sL, bL, iL, dL) let compressParams = G.defaultCompressParams { G.compressLevel = G.bestSpeed , G.compressStrategy = G.huffmanOnlyStrategy } cbits <- Bench.billTo [ Bench.Serialization, Bench.Compress ] $ return $!! G.compressWith compressParams bits1 let x = B.encode currentInterfaceVersion `L.append` cbits return x where l h = List.map fst . List.sortBy (compare `on` snd) <$> H.toList h benchSort = Bench.billTo [Bench.Serialization, Bench.Sort] . liftIO statistics :: String -> IORef FreshAndReuse -> TCM () statistics kind ioref = do FreshAndReuse fresh reused <- liftIO $ readIORef ioref tickN (kind ++ " (fresh)") $ fromIntegral fresh tickN (kind ++ " (reused)") $ fromIntegral reused -- encode :: EmbPrj a => a -> TCM L.ByteString -- encode a = do -- fileMod <- sourceToModule -- (x, shared, total) <- liftIO $ do -- newD@(Dict nD sD iD dD _ _ _ _ _ stats _) <- emptyDict fileMod -- root <- runReaderT (icode a) newD -- nL <- l nD; sL <- l sD; iL <- l iD; dL <- l dD -- (shared, total) <- readIORef stats -- return (B.encode currentInterfaceVersion `L.append` -- G.compress (B.encode (root, nL, sL, iL, dL)), shared, total) -- verboseS "profile.sharing" 10 $ do -- tickN "pointers (reused)" $ fromIntegral shared -- tickN "pointers" $ fromIntegral total -- return x -- where -- l h = List.map fst . List.sortBy (compare `on` snd) <$> H.toList h -- | Decodes something. The result depends on the include path. -- -- Returns 'Nothing' if the input does not start with the right magic -- number or some other decoding error is encountered. decode :: EmbPrj a => L.ByteString -> TCM (Maybe a) decode s = do mf <- use stModuleToSource incs <- getIncludeDirs -- Note that B.runGetState and G.decompress can raise errors if the -- input is malformed. The decoder is (intended to be) strict enough -- to ensure that all such errors can be caught by the handler here. shared <- sharedFun (mf, r) <- liftIO $ E.handle (\(E.ErrorCall s) -> noResult s) $ do (ver, s, _) <- return $ runGetState B.get s 0 if ver /= currentInterfaceVersion then noResult "Wrong interface version." else do ((r, nL, sL, bL, iL, dL), s, _) <- return $ runGetState B.get (G.decompress s) 0 if s /= L.empty -- G.decompress seems to throw away garbage at the end, so -- the then branch is possibly dead code. then noResult "Garbage at end." else do st <- St (ar nL) (ar sL) (ar bL) (ar iL) (ar dL) <$> liftIO H.new <*> return mf <*> return incs <*> return shared (r, st) <- runStateT (runExceptT (value r)) st return (Just (modFile st), r) case mf of Nothing -> return () Just mf -> stModuleToSource .= mf case r of Right x -> return (Just x) Left err -> do reportSLn "import.iface" 5 $ "Error when decoding interface file" -- Andreas, 2014-06-11 deactivated debug printing -- in order to get rid of dependency of Serialize on TCM.Pretty -- reportSDoc "import.iface" 5 $ -- text "Error when decoding interface file:" -- $+$ nest 2 (prettyTCM err) return Nothing where ar l = listArray (0, List.genericLength l - 1) l noResult s = return (Nothing, Left $ GenericError s) encodeInterface :: Interface -> TCM L.ByteString encodeInterface i = L.append hashes <$> encode i where hashes :: L.ByteString hashes = B.runPut $ B.put (iSourceHash i) >> B.put (iFullHash i) -- | Encodes something. To ensure relocatability file paths in -- positions are replaced with module names. encodeFile :: FilePath -> Interface -> TCM () encodeFile f i = liftIO . L.writeFile f =<< encodeInterface i -- | Decodes something. The result depends on the include path. -- -- Returns 'Nothing' if the file does not start with the right magic -- number or some other decoding error is encountered. decodeInterface :: L.ByteString -> TCM (Maybe Interface) decodeInterface s = decode $ L.drop 16 s decodeHashes :: L.ByteString -> Maybe (Hash, Hash) decodeHashes s | L.length s < 16 = Nothing | otherwise = Just $ B.runGet getH $ L.take 16 s where getH = (,) <$> B.get <*> B.get decodeFile :: FilePath -> TCM (Maybe Interface) decodeFile f = decodeInterface =<< liftIO (L.readFile f) -- | Store a 'SourceToModule' (map from 'AbsolutePath' to 'TopLevelModuleName') -- as map from 'AbsolutePath' to 'Int32', in order to directly get the identifiers -- from absolute pathes rather than going through top level module names. icodeFileMod :: SourceToModule -- ^ Maps file names to the corresponding module names. -- Must contain a mapping for every file name that is later encountered. -> S () icodeFileMod fileMod = do hmap <- asks absPathD forM_ (Map.toList fileMod) $ \ (absolutePath, topLevelModuleName) -> do i <- icod_ topLevelModuleName liftIO $ H.insert hmap absolutePath i Agda-2.5.3/src/full/Agda/TypeChecking/Conversion.hs0000644000000000000000000017122713154613124020200 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.Conversion where import Control.Applicative import Control.Monad import Control.Monad.Reader import Control.Monad.State import qualified Data.List as List import Data.Traversable hiding (mapM, sequence) import Agda.Syntax.Abstract.Views (isSet) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Translation.InternalToAbstract (reify) import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.CompiledClause (CompiledClauses'(Fail)) import Agda.TypeChecking.MetaVars import Agda.TypeChecking.MetaVars.Occurs (killArgs,PruneResult(..)) import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import qualified Agda.TypeChecking.SyntacticEquality as SynEq import Agda.TypeChecking.Telescope import Agda.TypeChecking.Constraints import {-# SOURCE #-} Agda.TypeChecking.CheckInternal (infer) import Agda.TypeChecking.Errors import Agda.TypeChecking.Free import Agda.TypeChecking.Datatypes (getConType, getFullyAppliedConType) import Agda.TypeChecking.Records import Agda.TypeChecking.Pretty import Agda.TypeChecking.Injectivity import Agda.TypeChecking.Polarity import Agda.TypeChecking.SizedTypes import Agda.TypeChecking.Level import Agda.TypeChecking.Implicit (implicitArgs) import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.ProjectionLike (elimView) import Agda.Interaction.Options import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.Functor import Agda.Utils.Monad import Agda.Utils.Maybe import Agda.Utils.Size import Agda.Utils.Tuple import Agda.Utils.Lens #include "undefined.h" import Agda.Utils.Impossible -- | Try whether a computation runs without errors or new constraints -- (may create new metas, though). -- Restores state upon failure. tryConversion :: TCM () -> TCM Bool tryConversion = isJust <.> tryConversion' -- | Try whether a computation runs without errors or new constraints -- (may create new metas, though). -- Return 'Just' the result upon success. -- Return 'Nothing' and restore state upon failure. tryConversion' :: TCM a -> TCM (Maybe a) tryConversion' m = tryMaybe $ disableDestructiveUpdate $ noConstraints m -- | Check if to lists of arguments are the same (and all variables). -- Precondition: the lists have the same length. sameVars :: Elims -> Elims -> Bool sameVars xs ys = and $ zipWith same xs ys where same (Apply (Arg _ (Var n []))) (Apply (Arg _ (Var m []))) = n == m same _ _ = False -- | @intersectVars us vs@ checks whether all relevant elements in @us@ and @vs@ -- are variables, and if yes, returns a prune list which says @True@ for -- arguments which are different and can be pruned. intersectVars :: Elims -> Elims -> Maybe [Bool] intersectVars = zipWithM areVars where -- ignore irrelevant args areVars (Apply u) v | isIrrelevant u = Just False -- do not prune areVars (Apply (Arg _ (Var n []))) (Apply (Arg _ (Var m []))) = Just $ n /= m -- prune different vars areVars _ _ = Nothing equalTerm :: Type -> Term -> Term -> TCM () equalTerm = compareTerm CmpEq equalAtom :: Type -> Term -> Term -> TCM () equalAtom = compareAtom CmpEq equalType :: Type -> Type -> TCM () equalType = compareType CmpEq {- Comparing in irrelevant context always succeeds. However, we might want to dig for solutions of irrelevant metas. To this end, we can just ignore errors during conversion checking. -} -- convError :: MonadTCM tcm => TypeError -> tcm a -- | Ignore errors in irrelevant context. convError :: TypeError -> TCM () convError err = ifM ((==) Irrelevant <$> asks envRelevance) (return ()) $ typeError err -- | Type directed equality on values. -- compareTerm :: Comparison -> Type -> Term -> Term -> TCM () -- If one term is a meta, try to instantiate right away. This avoids unnecessary unfolding. -- Andreas, 2012-02-14: This is UNSOUND for subtyping! compareTerm cmp a u v = do reportSDoc "tc.conv.term" 10 $ sep [ text "compareTerm" , nest 2 $ prettyTCM u <+> prettyTCM cmp <+> prettyTCM v , nest 2 $ text ":" <+> prettyTCM a ] -- Check pointer equality first. let checkPointerEquality def | not $ null $ List.intersect (pointerChain u) (pointerChain v) = do verboseS "profile.sharing" 10 $ tick "pointer equality" return () checkPointerEquality def = def checkPointerEquality $ do -- Check syntactic equality. This actually saves us quite a bit of work. ((u, v), equal) <- runReduceM $ SynEq.checkSyntacticEquality u v -- OLD CODE, traverses the *full* terms u v at each step, even if they -- are different somewhere. Leads to infeasibility in issue 854. -- (u, v) <- instantiateFull (u, v) -- let equal = u == v unifyPointers cmp u v $ if equal then verboseS "profile.sharing" 20 $ tick "equal terms" else do verboseS "profile.sharing" 20 $ tick "unequal terms" reportSDoc "tc.conv.term" 15 $ sep [ text "compareTerm (not syntactically equal)" , nest 2 $ prettyTCM u <+> prettyTCM cmp <+> prettyTCM v , nest 2 $ text ":" <+> prettyTCM a ] -- If we are at type Size, we cannot short-cut comparison -- against metas by assignment. -- Andreas, 2014-04-12: this looks incomplete. -- It seems to assume we are never comparing -- at function types into Size. let fallback = compareTerm' cmp a u v unlessSubtyping cont = if cmp == CmpEq then cont else do -- Andreas, 2014-04-12 do not short cut if type is blocked. ifBlockedType a (\ _ _ -> fallback) {-else-} $ \ a -> do -- do not short circuit size comparison! caseMaybeM (isSizeType a) cont (\ _ -> fallback) dir = fromCmp cmp rid = flipCmp dir -- The reverse direction. Bad name, I know. case (ignoreSharing u, ignoreSharing v) of (MetaV x us, MetaV y vs) | x /= y -> unlessSubtyping $ solve1 `orelse` solve2 `orelse` compareTerm' cmp a u v | otherwise -> fallback where (solve1, solve2) | x > y = (assign dir x us v, assign rid y vs u) | otherwise = (assign rid y vs u, assign dir x us v) (MetaV x us, _) -> unlessSubtyping $ assign dir x us v `orelse` fallback (_, MetaV y vs) -> unlessSubtyping $ assign rid y vs u `orelse` fallback _ -> fallback where assign dir x es v = do -- Andreas, 2013-10-19 can only solve if no projections reportSDoc "tc.conv.term.shortcut" 20 $ sep [ text "attempting shortcut" , nest 2 $ prettyTCM (MetaV x es) <+> text ":=" <+> prettyTCM v ] ifM (isInstantiatedMeta x) patternViolation {-else-} $ do assignE dir x es v $ compareTermDir dir a reportSDoc "tc.conv.term.shortcut" 50 $ text "shortcut successful" $$ nest 2 (text "result:" <+> (pretty =<< instantiate (MetaV x es))) -- Should be ok with catchError_ but catchError is much safer since we don't -- rethrow errors. orelse m h = catchError m (\_ -> h) unifyPointers :: Comparison -> Term -> Term -> TCM () -> TCM () unifyPointers _ _ _ action = action -- unifyPointers cmp _ _ action | cmp /= CmpEq = action -- unifyPointers _ u v action = do -- reportSLn "tc.ptr.unify" 50 $ "Maybe unifying pointers\n u = " ++ show u ++ "\n v = " ++ show v -- old <- use stDirty -- stDirty .= False -- action -- reportSLn "tc.ptr.unify" 50 $ "Finished comparison\n u = " ++ show u ++ "\n v = " ++ show v -- (u, v) <- instantiate (u, v) -- reportSLn "tc.ptr.unify" 50 $ "After instantiation\n u = " ++ show u ++ "\n v = " ++ show v -- dirty <- use stDirty -- stDirty .= old -- if dirty then verboseS "profile.sharing" 20 (tick "unifyPtr: dirty") -- else do -- verboseS "profile.sharing" 20 (tick "unifyPtr: clean") -- reportSLn "tc.ptr.unify" 80 $ "Unifying\n u = " ++ show u ++ "\n v = " ++ show v -- forceEqualTerms u v -- reportSLn "tc.ptr.unify" 80 $ "After unification\n u = " ++ show u ++ "\n v = " ++ show v -- | Try to assign meta. If meta is projected, try to eta-expand -- and run conversion check again. assignE :: CompareDirection -> MetaId -> Elims -> Term -> (Term -> Term -> TCM ()) -> TCM () assignE dir x es v comp = assignWrapper dir x es v $ do case allApplyElims es of Just vs -> assignV dir x vs v Nothing -> do reportSDoc "tc.conv.assign" 30 $ sep [ text "assigning to projected meta " , prettyTCM x <+> sep (map prettyTCM es) <+> text (":" ++ show dir) <+> prettyTCM v ] etaExpandMeta [Records] x res <- isInstantiatedMeta' x case res of Just u -> do reportSDoc "tc.conv.assign" 30 $ sep [ text "seems like eta expansion instantiated meta " , prettyTCM x <+> text (":" ++ show dir) <+> prettyTCM u ] let w = u `applyE` es comp w v Nothing -> do reportSLn "tc.conv.assign" 30 "eta expansion did not instantiate meta" patternViolation -- nothing happened, give up compareTermDir :: CompareDirection -> Type -> Term -> Term -> TCM () compareTermDir dir a = dirToCmp (`compareTerm'` a) dir compareTerm' :: Comparison -> Type -> Term -> Term -> TCM () compareTerm' cmp a m n = verboseBracket "tc.conv.term" 20 "compareTerm" $ do a' <- reduce a catchConstraint (ValueCmp cmp a' m n) $ do reportSDoc "tc.conv.term" 30 $ fsep [ text "compareTerm", prettyTCM m, prettyTCM cmp, prettyTCM n, text ":", prettyTCM a' ] proofIrr <- proofIrrelevance isSize <- isJust <$> isSizeType a' s <- reduce $ getSort a' mlvl <- tryMaybe primLevel reportSDoc "tc.conv.level" 60 $ nest 2 $ sep [ text "a' =" <+> pretty a' , text "mlvl =" <+> pretty mlvl , text $ "(Just (ignoreSharing $ unEl a') == mlvl) = " ++ show (Just (ignoreSharing $ unEl a') == mlvl) ] case s of Prop | proofIrr -> return () _ | isSize -> compareSizes cmp m n _ -> case ignoreSharing $ unEl a' of a | Just a == mlvl -> do a <- levelView m b <- levelView n equalLevel a b -- OLD: Pi dom _ -> equalFun (dom, a') m n a@Pi{} -> equalFun a m n Lam _ _ -> __IMPOSSIBLE__ Def r es -> do isrec <- isEtaRecord r if isrec then do sig <- getSignature let ps = fromMaybe __IMPOSSIBLE__ $ allApplyElims es -- Andreas, 2010-10-11: allowing neutrals to be blocked things does not seem -- to change Agda's behavior -- isNeutral Blocked{} = False isNeutral = isNeutral' . fmap ignoreSharing isMeta = isMeta' . fmap ignoreSharing isNeutral' (NotBlocked _ Con{}) = return False -- Andreas, 2013-09-18 / 2015-06-29: a Def by copatterns is -- not neutral if it is blocked (there can be missing projections -- to trigger a reduction. isNeutral' (NotBlocked r (Def q _)) = do -- Andreas, 2014-12-06 optimize this using r !! not <$> usesCopatterns q -- a def by copattern can reduce if projected isNeutral' _ = return True isMeta' (NotBlocked _ MetaV{}) = True isMeta' _ = False reportSDoc "tc.conv.term" 30 $ prettyTCM a <+> text "is eta record type" m <- reduceB m mNeutral <- isNeutral m n <- reduceB n nNeutral <- isNeutral n case (m, n) of _ | isMeta m || isMeta n -> compareAtom cmp a' (ignoreBlocking m) (ignoreBlocking n) _ | mNeutral && nNeutral -> do -- Andreas 2011-03-23: (fixing issue 396) -- if we are dealing with a singleton record, -- we can succeed immediately isSing <- isSingletonRecordModuloRelevance r ps case isSing of Right True -> return () -- do not eta-expand if comparing two neutrals _ -> compareAtom cmp a' (ignoreBlocking m) (ignoreBlocking n) _ -> do (tel, m') <- etaExpandRecord r ps $ ignoreBlocking m (_ , n') <- etaExpandRecord r ps $ ignoreBlocking n -- No subtyping on record terms c <- getRecordConstructor r -- Record constructors are covariant (see test/succeed/CovariantConstructors). compareArgs (repeat $ polFromCmp cmp) (telePi_ tel $ sort Prop) (Con c ConOSystem []) m' n' else compareAtom cmp a' m n _ -> compareAtom cmp a' m n where -- equality at function type (accounts for eta) equalFun :: Term -> Term -> Term -> TCM () equalFun (Shared p) m n = equalFun (derefPtr p) m n equalFun (Pi dom@(Dom info _) b) m n = do name <- freshName_ $ suggest (absName b) "x" addContext' (name, dom) $ compareTerm cmp (absBody b) m' n' where (m',n') = raise 1 (m,n) `apply` [Arg info $ var 0] equalFun _ _ _ = __IMPOSSIBLE__ -- | @compareTel t1 t2 cmp tel1 tel1@ checks whether pointwise -- @tel1 \`cmp\` tel2@ and complains that @t2 \`cmp\` t1@ failed if -- not. compareTel :: Type -> Type -> Comparison -> Telescope -> Telescope -> TCM () compareTel t1 t2 cmp tel1 tel2 = verboseBracket "tc.conv.tel" 20 "compareTel" $ catchConstraint (TelCmp t1 t2 cmp tel1 tel2) $ case (tel1, tel2) of (EmptyTel, EmptyTel) -> return () (EmptyTel, _) -> bad (_, EmptyTel) -> bad (ExtendTel dom1@(Dom i1 a1) tel1, ExtendTel dom2@(Dom i2 a2) tel2) -> do compareDom cmp dom1 dom2 tel1 tel2 bad bad $ compareTel t1 t2 cmp (absBody tel1) (absBody tel2) {- OLD, before 2013-05-15 let checkDom = escapeContext 1 $ compareType cmp a1 a2 c = TelCmp t1 t2 cmp (absBody tel1) (absBody tel2) addContext (name, dom1) $ if dependent then guardConstraint c checkDom else checkDom >> solveConstraint_ c -} where -- Andreas, 2011-05-10 better report message about types bad = typeError $ UnequalTypes cmp t2 t1 -- switch t2 and t1 because of contravariance! -- | Raise 'UnequalTerms' if there is no hope that by -- meta solving and subsequent eta-contraction these -- terms could become equal. -- Precondition: the terms are in reduced form -- (with no top-level pointer) and -- failed to be equal in the 'compareAtom' check. -- -- By eta-contraction, a lambda or a record constructor term -- can become anything. etaInequal :: Comparison -> Type -> Term -> Term -> TCM () etaInequal cmp t m n = do let inequal = typeError $ UnequalTerms cmp m n t dontKnow = do reportSDoc "tc.conv.inequal" 20 $ hsep [ text "etaInequal: postponing " , prettyTCM m , text " != " , prettyTCM n ] patternViolation -- if type is not blocked, then we would have tried eta already flip (ifBlockedType t) (\ _ -> inequal) $ \ _ _ -> do -- type is blocked case (m, n) of (Con{}, _) -> dontKnow (_, Con{}) -> dontKnow (Lam{}, _) -> dontKnow (_, Lam{}) -> dontKnow _ -> inequal compareAtomDir :: CompareDirection -> Type -> Term -> Term -> TCM () compareAtomDir dir a = dirToCmp (`compareAtom` a) dir -- | Compute the head type of an elimination. For projection-like functions -- this requires inferring the type of the principal argument. computeElimHeadType :: QName -> Elims -> Elims -> TCM Type computeElimHeadType f es es' = do def <- getConstInfo f -- To compute the type @a@ of a projection-like @f@, -- we have to infer the type of its first argument. if projectionArgs (theDef def) <= 0 then return $ defType def else do -- Find an first argument to @f@. let arg = case (es, es') of (Apply arg : _, _) -> arg (_, Apply arg : _) -> arg _ -> __IMPOSSIBLE__ -- Infer its type. reportSDoc "tc.conv.infer" 30 $ text "inferring type of internal arg: " <+> prettyTCM arg targ <- infer $ unArg arg reportSDoc "tc.conv.infer" 30 $ text "inferred type: " <+> prettyTCM targ -- getDefType wants the argument type reduced. -- Andreas, 2016-02-09, Issue 1825: The type of arg might be -- a meta-variable, e.g. in interactive development. -- In this case, we postpone. fromMaybeM patternViolation $ getDefType f =<< reduce targ -- | Syntax directed equality on atomic values -- compareAtom :: Comparison -> Type -> Term -> Term -> TCM () compareAtom cmp t m n = verboseBracket "tc.conv.atom" 20 "compareAtom" $ -- if a PatternErr is thrown, rebuild constraint! catchConstraint (ValueCmp cmp t m n) $ do reportSDoc "tc.conv.atom" 50 $ text "compareAtom" <+> fsep [ prettyTCM m <+> prettyTCM cmp , prettyTCM n , text ":" <+> prettyTCM t ] -- Andreas: what happens if I cut out the eta expansion here? -- Answer: Triggers issue 245, does not resolve 348 (mb',nb') <- ifM (asks envCompareBlocked) ((notBlocked -*- notBlocked) <$> reduce (m,n)) $ do mb' <- etaExpandBlocked =<< reduceB m nb' <- etaExpandBlocked =<< reduceB n return (mb', nb') -- constructorForm changes literal to constructors -- only needed if the other side is not a literal (mb'', nb'') <- case (ignoreSharing $ ignoreBlocking mb', ignoreSharing $ ignoreBlocking nb') of (Lit _, Lit _) -> return (mb', nb') _ -> (,) <$> traverse constructorForm mb' <*> traverse constructorForm nb' mb <- traverse unLevel mb'' nb <- traverse unLevel nb'' let m = ignoreBlocking mb n = ignoreBlocking nb postpone = addConstraint $ ValueCmp cmp t m n checkSyntacticEquality = do n <- normalise n -- is this what we want? m <- normalise m if m == n then return () -- Check syntactic equality for blocked terms else postpone dir = fromCmp cmp rid = flipCmp dir -- The reverse direction. Bad name, I know. assign dir x es v = assignE dir x es v $ compareAtomDir dir t unifyPointers cmp (ignoreBlocking mb') (ignoreBlocking nb') $ do -- this needs to go after eta expansion to avoid creating infinite terms reportSDoc "tc.conv.atom" 30 $ text "compareAtom" <+> fsep [ prettyTCM mb <+> prettyTCM cmp , prettyTCM nb , text ":" <+> prettyTCM t ] case (ignoreSharing <$> mb, ignoreSharing <$> nb) of -- equate two metas x and y. if y is the younger meta, -- try first y := x and then x := y (NotBlocked _ (MetaV x xArgs), NotBlocked _ (MetaV y yArgs)) | x == y -> case intersectVars xArgs yArgs of -- all relevant arguments are variables Just kills -> do -- kills is a list with 'True' for each different var killResult <- killArgs kills x case killResult of NothingToPrune -> return () PrunedEverything -> return () PrunedNothing -> postpone PrunedSomething -> postpone -- OLD CODE: if killedAll then return () else checkSyntacticEquality -- not all relevant arguments are variables Nothing -> checkSyntacticEquality -- Check syntactic equality on meta-variables -- (same as for blocked terms) | otherwise -> do [p1, p2] <- mapM getMetaPriority [x,y] -- instantiate later meta variables first let (solve1, solve2) | (p1,x) > (p2,y) = (l,r) | otherwise = (r,l) where l = assign dir x xArgs n r = assign rid y yArgs m try m h = m `catchError_` \err -> case err of PatternErr{} -> h _ -> throwError err -- First try the one with the highest priority. If that doesn't -- work, try the low priority one. try solve1 solve2 -- one side a meta, the other an unblocked term (NotBlocked _ (MetaV x es), _) -> assign dir x es n (_, NotBlocked _ (MetaV x es)) -> assign rid x es m (Blocked{}, Blocked{}) -> checkSyntacticEquality (Blocked{}, _) -> useInjectivity cmp t m n (_,Blocked{}) -> useInjectivity cmp t m n _ -> do -- -- Andreas, 2013-10-20 put projection-like function -- -- into the spine, to make compareElims work. -- -- 'False' means: leave (Def f []) unchanged even for -- -- proj-like funs. -- m <- elimView False m -- n <- elimView False n -- Andreas, 2015-07-01, actually, don't put them into the spine. -- Polarity cannot be communicated properly if projection-like -- functions are post-fix. case (ignoreSharing m, ignoreSharing n) of (Pi{}, Pi{}) -> equalFun m n (Sort s1, Sort Inf) -> return () (Sort s1, Sort s2) -> compareSort CmpEq s1 s2 (Lit l1, Lit l2) | l1 == l2 -> return () (Var i es, Var i' es') | i == i' -> do a <- typeOfBV i -- Variables are invariant in their arguments compareElims [] a (var i) es es' (Def f [], Def f' []) | f == f' -> return () (Def f es, Def f' es') | f == f' -> do a <- computeElimHeadType f es es' -- The polarity vector of projection-like functions -- does not include the parameters. pol <- getPolarity' cmp f compareElims pol a (Def f []) es es' (Def f es, Def f' es') -> unlessM (bothAbsurd f f') $ do trySizeUniv cmp t m n f es f' es' -- Due to eta-expansion, these constructors are fully applied. (Con x ci xArgs, Con y _ yArgs) | x == y -> do -- Get the type of the constructor instantiated to the datatype parameters. a' <- conType x t -- Constructors are covariant in their arguments -- (see test/succeed/CovariantConstructors). compareArgs (repeat $ polFromCmp cmp) a' (Con x ci []) xArgs yArgs _ -> etaInequal cmp t m n -- fixes issue 856 (unsound conversion error) where -- Andreas, 2013-05-15 due to new postponement strategy, type can now be blocked conType c t = ifBlockedType t (\ _ _ -> patternViolation) $ \ t -> do let impossible = do reportSDoc "impossible" 10 $ text "expected data/record type, found " <+> prettyTCM t reportSDoc "impossible" 70 $ nest 2 $ text "raw =" <+> pretty t -- __IMPOSSIBLE__ -- Andreas, 2013-10-20: in case termination checking fails -- we might get some unreduced types here. -- In issue 921, this happens during the final attempt -- to solve left-over constraints. -- Thus, instead of crashing, just give up gracefully. patternViolation maybe impossible (return . snd) =<< getFullyAppliedConType c t equalFun t1 t2 = case (ignoreSharing t1, ignoreSharing t2) of (Pi dom1 b1, Pi dom2 b2) -> do verboseBracket "tc.conv.fun" 15 "compare function types" $ do reportSDoc "tc.conv.fun" 20 $ nest 2 $ vcat [ text "t1 =" <+> prettyTCM t1 , text "t2 =" <+> prettyTCM t2 ] compareDom cmp dom2 dom1 b1 b2 errH errR $ compareType cmp (absBody b1) (absBody b2) where errH = typeError $ UnequalHiding t1 t2 errR = typeError $ UnequalRelevance cmp t1 t2 {- OLD, before 2013-05-15 let checkDom = escapeContext 1 $ compareType cmp a2 a1 conCoDom = TypeCmp cmp (absBody b1) (absBody b2) -- We only need to require a1 == a2 if t2 is a dependent function type. -- If it's non-dependent it doesn't matter what we add to the context. name <- freshName_ (suggest b1 b2) addContext (name, dom1) $ if isBinderUsed b2 -- dependent function type? then guardConstraint conCoDom checkDom else checkDom >> solveConstraint_ conCoDom -} _ -> __IMPOSSIBLE__ -- | Check whether @a1 `cmp` a2@ and continue in context extended by @a1@. compareDom :: Free c => Comparison -- ^ @cmp@ The comparison direction -> Dom Type -- ^ @a1@ The smaller domain. -> Dom Type -- ^ @a2@ The other domain. -> Abs b -- ^ @b1@ The smaller codomain. -> Abs c -- ^ @b2@ The bigger codomain. -> TCM () -- ^ Continuation if mismatch in 'Hiding'. -> TCM () -- ^ Continuation if mismatch in 'Relevance'. -> TCM () -- ^ Continuation if comparison is successful. -> TCM () compareDom cmp dom1@(Dom i1 a1) dom2@(Dom i2 a2) b1 b2 errH errR cont | not (sameHiding dom1 dom2) = errH -- Andreas 2010-09-21 compare r1 and r2, but ignore forcing annotations! | not $ compareRelevance cmp (ignoreForced $ getRelevance dom1) (ignoreForced $ getRelevance dom2) = errR | otherwise = do let r = max (getRelevance dom1) (getRelevance dom2) -- take "most irrelevant" dependent = (r /= Irrelevant) && isBinderUsed b2 pid <- newProblem_ $ compareType cmp a1 a2 dom <- if dependent then Dom i1 <$> blockTypeOnProblem a1 pid else return dom1 -- We only need to require a1 == a2 if b2 is dependent -- If it's non-dependent it doesn't matter what we add to the context. name <- freshName_ $ suggest b1 b2 addContext' (name, dom) $ cont stealConstraints pid -- Andreas, 2013-05-15 Now, comparison of codomains is not -- blocked any more by getting stuck on domains. -- Only the domain type in context will be blocked. -- But see issue #1258. compareRelevance :: Comparison -> Relevance -> Relevance -> Bool compareRelevance CmpEq = (==) compareRelevance CmpLeq = (<=) -- | When comparing argument spines (in compareElims) where the first arguments -- don't match, we keep going, substituting the anti-unification of the two -- terms in the telescope. More precisely: -- -- @@ -- (u = v : A)[pid] w = antiUnify pid A u v us = vs : Δ[w/x] -- ------------------------------------------------------------- -- u us = v vs : (x : A) Δ -- @@ -- -- The simplest case of anti-unification is to return a fresh metavariable -- (created by blockTermOnProblem), but if there's shared structure between -- the two terms we can expose that. -- -- This is really a crutch that lets us get away with things that otherwise -- would require heterogenous conversion checking. See for instance issue -- #2384. antiUnify :: ProblemId -> Type -> Term -> Term -> TCM Term antiUnify pid a u v = do ((u, v), eq) <- runReduceM (SynEq.checkSyntacticEquality u v) if eq then return u else do (u, v) <- reduce (u, v) case (ignoreSharing u, ignoreSharing v) of (Pi ua ub, Pi va vb) -> do wa0 <- antiUnifyType pid (unDom ua) (unDom va) let wa = wa0 <$ ua wb <- addContext wa $ antiUnifyType pid (unAbs ub) (unAbs vb) return $ Pi wa (wb <$ ub) (Lam i u, Lam _ v) -> case ignoreSharing $ unEl a of Pi a b -> Lam i . (<$ u) <$> addContext a (antiUnify pid (unAbs b) (unAbs u) (unAbs v)) _ -> fallback (Var i us, Var j vs) | i == j -> maybeGiveUp $ do a <- typeOfBV i antiUnifyElims pid a (var i) us vs -- Andreas, 2017-07-27: -- It seems that nothing guarantees here that the constructors are fully -- applied!? Thus, @a@ could be a function type and we need the robust -- @getConType@ here. -- (Note that @patternViolation@ swallows exceptions coming from @getConType@ -- thus, we would not see clearly if we used @getFullyAppliedConType@ instead.) (Con x ci us, Con y _ vs) | x == y -> maybeGiveUp $ do a <- maybe patternViolation (return . snd) =<< getConType x a antiUnifyElims pid a (Con x ci []) (map Apply us) (map Apply vs) (Def f us, Def g vs) | f == g, length us == length vs -> maybeGiveUp $ do a <- computeElimHeadType f us vs antiUnifyElims pid a (Def f []) us vs _ -> fallback where fallback = blockTermOnProblem a u pid maybeGiveUp m = m `catchError_` \ err -> case err of PatternErr{} -> fallback _ -> throwError err antiUnifyType :: ProblemId -> Type -> Type -> TCM Type antiUnifyType pid (El s a) (El _ b) = El s <$> antiUnify pid (sort s) a b antiUnifyElims :: ProblemId -> Type -> Term -> Elims -> Elims -> TCM Term antiUnifyElims pid a self [] [] = return self antiUnifyElims pid a self (Proj o f : es1) (Proj _ g : es2) | f == g = do res <- projectTyped self a o f case res of Just (_, self, a) -> antiUnifyElims pid a self es1 es2 Nothing -> patternViolation -- can fail for projection like antiUnifyElims pid a self (Apply u : es1) (Apply v : es2) = do case ignoreSharing $ unEl a of Pi a b -> do w <- antiUnify pid (unDom a) (unArg u) (unArg v) antiUnifyElims pid (b `lazyAbsApp` w) (apply self [w <$ u]) es1 es2 _ -> patternViolation antiUnifyElims _ _ _ _ _ = patternViolation -- trigger maybeGiveUp in antiUnify -- | @compareElims pols a v els1 els2@ performs type-directed equality on eliminator spines. -- @t@ is the type of the head @v@. compareElims :: [Polarity] -> Type -> Term -> [Elim] -> [Elim] -> TCM () compareElims pols0 a v els01 els02 = catchConstraint (ElimCmp pols0 a v els01 els02) $ do let v1 = applyE v els01 v2 = applyE v els02 failure = typeError $ UnequalTerms CmpEq v1 v2 a -- Andreas, 2013-03-15 since one of the spines is empty, @a@ -- is the correct type here. unless (null els01) $ do reportSDoc "tc.conv.elim" 25 $ text "compareElims" $$ do nest 2 $ vcat [ text "a =" <+> prettyTCM a , text "pols0 (truncated to 10) =" <+> sep (map prettyTCM $ take 10 pols0) , text "v =" <+> prettyTCM v , text "els01 =" <+> prettyTCM els01 , text "els02 =" <+> prettyTCM els02 ] case (els01, els02) of ([] , [] ) -> return () ([] , Proj{}:_ ) -> failure -- not impossible, see issue 821 (Proj{} : _, [] ) -> failure -- could be x.p =?= x for projection p ([] , Apply{} : _) -> failure -- not impossible, see issue 878 (Apply{} : _, [] ) -> failure (Apply{} : _, Proj{} : _) -> __IMPOSSIBLE__ <$ solveAwakeConstraints' True -- NB: popped up in issue 889 (Proj{} : _, Apply{} : _) -> __IMPOSSIBLE__ <$ solveAwakeConstraints' True -- but should be impossible (but again in issue 1467) (Apply arg1 : els1, Apply arg2 : els2) -> verboseBracket "tc.conv.elim" 20 "compare Apply" $ do reportSDoc "tc.conv.elim" 10 $ nest 2 $ vcat [ text "a =" <+> prettyTCM a , text "v =" <+> prettyTCM v , text "arg1 =" <+> prettyTCM arg1 , text "arg2 =" <+> prettyTCM arg2 ] reportSDoc "tc.conv.elim" 50 $ nest 2 $ vcat [ text "v =" <+> pretty v , text "arg1 =" <+> pretty arg1 , text "arg2 =" <+> pretty arg2 , text "" ] let (pol, pols) = nextPolarity pols0 ifBlockedType a (\ m t -> patternViolation) $ \ a -> do case ignoreSharing . unEl $ a of (Pi (Dom info b) codom) -> do mlvl <- tryMaybe primLevel let freeInCoDom (Abs _ c) = 0 `freeInIgnoringSorts` c freeInCoDom _ = False dependent = (Just (unEl b) /= mlvl) && freeInCoDom codom -- Level-polymorphism (x : Level) -> ... does not count as dependency here -- NB: we could drop the free variable test and still be sound. -- It is a trade-off between the administrative effort of -- creating a blocking and traversing a term for free variables. -- Apparently, it is believed that checking free vars is cheaper. -- Andreas, 2013-05-15 r = getRelevance info -- NEW, Andreas, 2013-05-15 -- compare arg1 and arg2 pid <- newProblem_ $ applyRelevanceToContext r $ case r of Forced{} -> return () r | irrelevant r -> compareIrrelevant b (unArg arg1) (unArg arg2) _ -> compareWithPol pol (flip compareTerm b) (unArg arg1) (unArg arg2) -- if comparison got stuck and function type is dependent, block arg solved <- isProblemSolved pid arg <- if dependent && not solved then do arg <- (arg1 $>) <$> antiUnify pid b (unArg arg1) (unArg arg2) reportSDoc "tc.conv.elims" 30 $ hang (text "Anti-unification:") 2 (prettyTCM arg) reportSDoc "tc.conv.elims" 70 $ nest 2 $ text "raw:" <+> pretty arg return arg else return arg1 -- continue, possibly with blocked instantiation compareElims pols (codom `lazyAbsApp` unArg arg) (apply v [arg]) els1 els2 -- any left over constraints of arg are associatd to the comparison stealConstraints pid {- Stealing solves this issue: Does not create enough blocked tc-problems, see test/fail/DontPrune. (There are remaining problems which do not show up as yellow.) Need to find a way to associate pid also to result of compareElims. -} {- OLD, before 2013-05-15 let checkArg = applyRelevanceToContext r $ case r of Forced -> return () r | irrelevant r -> compareIrrelevant b (unArg arg1) (unArg arg2) _ -> compareWithPol pol (flip compareTerm b) (unArg arg1) (unArg arg2) theRest = ElimCmp pols (piApply a [arg1]) (apply v [arg1]) els1 els2 if dependent then guardConstraint theRest checkArg else checkArg >> solveConstraint_ theRest -} a -> do reportSDoc "impossible" 10 $ text "unexpected type when comparing apply eliminations " <+> prettyTCM a reportSDoc "impossible" 50 $ text "raw type:" <+> pretty a patternViolation -- Andreas, 2013-10-22 -- in case of disabled reductions (due to failing termination check) -- we might get stuck, so do not crash, but fail gently. -- __IMPOSSIBLE__ -- case: f == f' are projections (Proj o f : els1, Proj _ f' : els2) | f /= f' -> typeError . GenericError . show =<< prettyTCM f <+> text "/=" <+> prettyTCM f' | otherwise -> ifBlockedType a (\ m t -> patternViolation) $ \ a -> do res <- projectTyped v a o f -- fails only if f is proj.like but parameters cannot be retrieved case res of Just (_, u, t) -> do -- Andreas, 2015-07-01: -- The arguments following the principal argument of a projection -- are invariant. (At least as long as we have no explicit polarity -- annotations.) compareElims [] t u els1 els2 Nothing -> do reportSDoc "tc.conv.elims" 30 $ sep [ text $ "projection " ++ show f , text "applied to value " <+> prettyTCM v , text "of unexpected type " <+> prettyTCM a ] patternViolation -- | "Compare" two terms in irrelevant position. This always succeeds. -- However, we can dig for solutions of irrelevant metas in the -- terms we compare. -- (Certainly not the systematic solution, that'd be proof search...) compareIrrelevant :: Type -> Term -> Term -> TCM () {- 2012-04-02 DontCare no longer present compareIrrelevant t (DontCare v) w = compareIrrelevant t v w compareIrrelevant t v (DontCare w) = compareIrrelevant t v w -} compareIrrelevant t v w = do reportSDoc "tc.conv.irr" 20 $ vcat [ text "compareIrrelevant" , nest 2 $ text "v =" <+> prettyTCM v , nest 2 $ text "w =" <+> prettyTCM w ] reportSDoc "tc.conv.irr" 50 $ vcat [ nest 2 $ text "v =" <+> pretty v , nest 2 $ text "w =" <+> pretty w ] try v w $ try w v $ return () where try (Shared p) w fallback = try (derefPtr p) w fallback try (MetaV x es) w fallback = do mv <- lookupMeta x let rel = getMetaRelevance mv inst = case mvInstantiation mv of InstV{} -> True _ -> False reportSDoc "tc.conv.irr" 20 $ vcat [ nest 2 $ text $ "rel = " ++ show rel , nest 2 $ text "inst =" <+> pretty inst ] if not (irrelevant rel) || inst then fallback -- Andreas, 2016-08-08, issue #2131: -- Mining for solutions for irrelevant metas is not definite. -- Thus, in case of error, leave meta unsolved. else (assignE DirEq x es w $ compareIrrelevant t) `catchError` \ _ -> fallback -- the value of irrelevant or unused meta does not matter try v w fallback = fallback compareWithPol :: Polarity -> (Comparison -> a -> a -> TCM ()) -> a -> a -> TCM () compareWithPol Invariant cmp x y = cmp CmpEq x y compareWithPol Covariant cmp x y = cmp CmpLeq x y compareWithPol Contravariant cmp x y = cmp CmpLeq y x compareWithPol Nonvariant cmp x y = return () polFromCmp :: Comparison -> Polarity polFromCmp CmpLeq = Covariant polFromCmp CmpEq = Invariant -- | Type-directed equality on argument lists -- compareArgs :: [Polarity] -> Type -> Term -> Args -> Args -> TCM () compareArgs pol a v args1 args2 = compareElims pol a v (map Apply args1) (map Apply args2) --------------------------------------------------------------------------- -- * Types --------------------------------------------------------------------------- -- | Equality on Types compareType :: Comparison -> Type -> Type -> TCM () compareType cmp ty1@(El s1 a1) ty2@(El s2 a2) = verboseBracket "tc.conv.type" 20 "compareType" $ catchConstraint (TypeCmp cmp ty1 ty2) $ do reportSDoc "tc.conv.type" 50 $ vcat [ text "compareType" <+> sep [ prettyTCM ty1 <+> prettyTCM cmp , prettyTCM ty2 ] , hsep [ text " sorts:", prettyTCM s1, text " and ", prettyTCM s2 ] ] -- Andreas, 2011-4-27 should not compare sorts, but currently this is needed -- for solving sort and level metas compareSort CmpEq s1 s2 `catchError` \err -> case err of TypeError _ e -> do reportSDoc "tc.conv.type" 30 $ vcat [ text "sort comparison failed" , nest 2 $ vcat [ text "s1 =" <+> prettyTCM s1 , text "s2 =" <+> prettyTCM s2 ] ] case clValue e of -- Issue 659: Better error message SetOmegaNotValidType -> typeError $ UnequalBecauseOfUniverseConflict cmp a1 a2 _ -> do -- This error will probably be more informative compareTerm cmp (sort s1) a1 a2 -- Throw the original error if the above doesn't -- give an error (for instance, due to pending -- constraints). -- Or just ignore it... We run into this with irrelevant levels -- which may show up in sort constraints, causing them to fail. -- In any case it's not safe to ignore the error, for instance -- a1 might be Set and a2 a meta of type Set, in which case we -- really need the sort comparison to fail, instead of silently -- instantiating the meta. -- Andreas, 2013-10-31 Maybe the error went away -- when we compared the types. So we try the sort comparison -- again, this time not catching the error. (see Issue 930) -- throwError err compareSort CmpEq s1 s2 _ -> throwError err compareTerm cmp (sort s1) a1 a2 return () leqType :: Type -> Type -> TCM () leqType = compareType CmpLeq -- | @coerce v a b@ coerces @v : a@ to type @b@, returning a @v' : b@ -- with maybe extra hidden applications or hidden abstractions. -- -- In principle, this function can host coercive subtyping, but -- currently it only tries to fix problems with hidden function types. -- -- Precondition: @a@ and @b@ are reduced. coerce :: Term -> Type -> Type -> TCM Term coerce v t1 t2 = blockTerm t2 $ do verboseS "tc.conv.coerce" 10 $ do (a1,a2) <- reify (t1,t2) let dbglvl = if isSet a1 && isSet a2 then 50 else 10 reportSDoc "tc.conv.coerce" dbglvl $ text "coerce" <+> vcat [ text "term v =" <+> prettyTCM v , text "from type t1 =" <+> prettyTCM a1 , text "to type t2 =" <+> prettyTCM a2 ] reportSDoc "tc.conv.coerce" 70 $ text "coerce" <+> vcat [ text "term v =" <+> pretty v , text "from type t1 =" <+> pretty t1 , text "to type t2 =" <+> pretty t2 ] -- v <$ do workOnTypes $ leqType t1 t2 -- take off hidden/instance domains from t1 and t2 TelV tel1 b1 <- telViewUpTo' (-1) notVisible t1 TelV tel2 b2 <- telViewUpTo' (-1) notVisible t2 let n = size tel1 - size tel2 -- the crude solution would be -- v' = λ {tel2} → v {tel1} -- however, that may introduce unneccessary many function types -- If n > 0 and b2 is not blocked, it is safe to -- insert n many hidden args if n <= 0 then fallback else do ifBlockedType b2 (\ _ _ -> fallback) $ \ _ -> do (args, t1') <- implicitArgs n notVisible t1 coerceSize leqType (v `apply` args) t1' t2 where fallback = coerceSize leqType v t1 t2 -- | Account for situations like @k : (Size< j) <= (Size< k + 1)@ -- -- Actually, the semantics is -- @(Size<= k) ∩ (Size< j) ⊆ rhs@ -- which gives a disjunctive constraint. Mmmh, looks like stuff -- TODO. -- -- For now, we do a cheap heuristics. -- -- Precondition: types are reduced. coerceSize :: (Type -> Type -> TCM ()) -> Term -> Type -> Type -> TCM Term coerceSize leqType v t1 t2 = workOnTypes $ do reportSDoc "tc.conv.coerce" 70 $ text "coerceSize" <+> vcat [ text "term v =" <+> pretty v , text "from type t1 =" <+> pretty t1 , text "to type t2 =" <+> pretty t2 ] let fallback = v <$ leqType t1 t2 done = caseMaybeM (isSizeType t1) fallback $ \ b1 -> return v -- Andreas, 2015-07-22, Issue 1615: -- If t1 is a meta and t2 a type like Size< v2, we need to make sure we do not miss -- the constraint v < v2! caseMaybeM (isSizeType t2) fallback $ \ b2 -> do -- Andreas, 2017-01-20, issue #2329: -- If v is not a size suitable for the solver, like a neutral term, -- we can only rely on the type. mv <- sizeMaxView v if any (\case{ DOtherSize{} -> True; _ -> False }) mv then fallback else do -- Andreas, 2015-02-11 do not instantiate metas here (triggers issue 1203). ifM (tryConversion $ dontAssignMetas $ leqType t1 t2) (return v) $ {- else -} do -- A (most probably weaker) alternative is to just check syn.eq. -- ifM (snd <$> checkSyntacticEquality t1 t2) (return v) $ {- else -} do reportSDoc "tc.conv.coerce" 20 $ text "coercing to a size type" case b2 of -- @t2 = Size@. We are done! BoundedNo -> done -- @t2 = Size< v2@ BoundedLt v2 -> do sv2 <- sizeView v2 case sv2 of SizeInf -> done OtherSize{} -> do -- Andreas, 2014-06-16: -- Issue 1203: For now, just treat v < v2 as suc v <= v2 -- TODO: Need proper < comparison vinc <- sizeSuc 1 v compareSizes CmpLeq vinc v2 done -- @v2 = a2 + 1@: In this case, we can try @v <= a2@ SizeSuc a2 -> do compareSizes CmpLeq v a2 done -- to pass Issue 1136 --------------------------------------------------------------------------- -- * Sorts and levels --------------------------------------------------------------------------- compareLevel :: Comparison -> Level -> Level -> TCM () compareLevel CmpLeq u v = leqLevel u v compareLevel CmpEq u v = equalLevel u v compareSort :: Comparison -> Sort -> Sort -> TCM () compareSort CmpEq = equalSort compareSort CmpLeq = leqSort -- | Check that the first sort is less or equal to the second. -- -- We can put @SizeUniv@ below @Inf@, but otherwise, it is -- unrelated to the other universes. -- leqSort :: Sort -> Sort -> TCM () leqSort s1 s2 = catchConstraint (SortCmp CmpLeq s1 s2) $ do (s1,s2) <- reduce (s1,s2) let postpone = addConstraint (SortCmp CmpLeq s1 s2) no = typeError $ NotLeqSort s1 s2 yes = return () reportSDoc "tc.conv.sort" 30 $ sep [ text "leqSort" , nest 2 $ fsep [ prettyTCM s1 <+> text "=<" , prettyTCM s2 ] ] case (s1, s2) of (_ , Inf ) -> yes (SizeUniv, _ ) -> equalSort s1 s2 (_ , SizeUniv) -> equalSort s1 s2 (Type a , Type b ) -> leqLevel a b (Prop , Prop ) -> yes (Prop , Type _ ) -> yes (Type _ , Prop ) -> no -- (SizeUniv, SizeUniv) -> yes -- (SizeUniv, _ ) -> no -- (_ , SizeUniv) -> no (Inf , _ ) -> equalSort s1 s2 (DLub{} , _ ) -> postpone (_ , DLub{} ) -> postpone leqLevel :: Level -> Level -> TCM () leqLevel a b = liftTCM $ do reportSDoc "tc.conv.nat" 30 $ text "compareLevel" <+> sep [ prettyTCM a <+> text "=<" , prettyTCM b ] -- Andreas, 2015-12-28 Issue 1757 -- We normalize both sides to make the syntactic equality check (==) stronger. -- See case for `same term` below. a <- normalise a b <- normalise b leqView a b where -- Andreas, 2016-09-28 -- If we have to postpone a constraint, then its simplified form! leqView a@(Max as) b@(Max bs) = catchConstraint (LevelCmp CmpLeq a b) $ do reportSDoc "tc.conv.nat" 30 $ text "compareLevelView" <+> sep [ pretty a <+> text "=<" , pretty b ] wrap $ case (as, bs) of -- same term _ | as == bs -> ok -- 0 ≤ any ([], _) -> ok -- as ≤ 0 (as, []) -> sequence_ [ equalLevel' (Max [a]) (Max []) | a <- as ] (as, [ClosedLevel 0]) -> sequence_ [ equalLevel' (Max [a]) (Max []) | a <- as ] -- Andreas, 2016-09-28, @[ClosedLevel 0]@ is possible if we come from case -- "reduce constants" where we run @subtr@ on both sides. -- See test/Succeed/LevelMetaLeqZero.agda. -- as ≤ [b] (as@(_:_:_), [b]) -> sequence_ [ leqView (Max [a]) (Max [b]) | a <- as ] -- reduce constants (as, bs) | minN > 0 -> leqView (Max $ map (subtr minN) as) (Max $ map (subtr minN) bs) where ns = map constant as ms = map constant bs minN = minimum (ns ++ ms) -- remove subsumed -- Andreas, 2014-04-07: This is ok if we do not go back to equalLevel (as, bs) | not $ null subsumed -> leqView (Max $ as List.\\ subsumed) (Max bs) where subsumed = [ a | a@(Plus m l) <- as, n <- findN l, m <= n ] -- @findN a@ finds the unique(?) term @Plus n a@ in @bs@, if any. -- Andreas, 2014-04-07 Why must there be a unique term? findN a = case [ n | Plus n b <- bs, b == a ] of [n] -> [n] _ -> [] -- Andreas, 2012-10-02 raise error on unsolvable constraint ([ClosedLevel n], [ClosedLevel m]) -> if n <= m then ok else notok -- closed ≤ bs ([ClosedLevel n], bs) | n <= maximum (map constant bs) -> ok -- as ≤ neutral (as, bs) | neutralB && maxA > maxB -> notok | neutralB && any (\a -> neutral a && not (isInB a)) as -> notok | neutralB && neutralA -> maybeok $ all (\a -> constant a <= findN a) as where maxA = maximum $ map constant as maxB = maximum $ map constant bs neutralA = all neutral as neutralB = all neutral bs isInB a = elem (unneutral a) $ map unneutral bs findN a = case [ n | b@(Plus n _) <- bs, unneutral b == unneutral a ] of [n] -> n _ -> __IMPOSSIBLE__ -- Andreas, 2016-09-28: This simplification loses the solution lzero. -- Thus, it is invalid. -- See test/Succeed/LevelMetaLeqNeutralLevel.agda. -- -- [a] ≤ [neutral] -- ([a@(Plus n _)], [b@(Plus m NeutralLevel{})]) -- | m == n -> equalLevel' (Max [a]) (Max [b]) -- -- Andreas, 2014-04-07: This call to equalLevel is ok even if we removed -- -- subsumed terms from the lhs. -- anything else _ -> postpone where ok = return () notok = unlessM typeInType $ typeError $ NotLeqSort (Type a) (Type b) postpone = patternViolation wrap m = catchError m $ \e -> case e of TypeError{} -> notok _ -> throwError e maybeok True = ok maybeok False = notok neutral (Plus _ NeutralLevel{}) = True neutral _ = False meta (Plus _ MetaLevel{}) = True meta _ = False unneutral (Plus _ (NeutralLevel _ v)) = v unneutral _ = __IMPOSSIBLE__ constant (ClosedLevel n) = n constant (Plus n _) = n subtr m (ClosedLevel n) = ClosedLevel (n - m) subtr m (Plus n l) = Plus (n - m) l -- choice [] = patternViolation -- choice (m:ms) = noConstraints m `catchError` \_ -> choice ms -- case e of -- PatternErr{} -> choice ms -- _ -> throwError e equalLevel :: Level -> Level -> TCM () equalLevel a b = do -- Andreas, 2013-10-31 Use normalization to make syntactic equality stronger (a, b) <- normalise (a, b) equalLevel' a b -- | Precondition: levels are 'normalise'd. equalLevel' :: Level -> Level -> TCM () equalLevel' a b = do reportSDoc "tc.conv.level" 50 $ sep [ text "equalLevel", nest 2 $ parens $ pretty a, nest 2 $ parens $ pretty b ] liftTCM $ catchConstraint (LevelCmp CmpEq a b) $ check a b where check a@(Max as) b@(Max bs) = do -- Jesper, 2014-02-02 remove terms that certainly do not contribute -- to the maximum as <- return $ [ a | a <- as, not $ a `isStrictlySubsumedBy` bs ] bs <- return $ [ b | b <- bs, not $ b `isStrictlySubsumedBy` as ] -- Andreas, 2013-10-31 remove common terms (that don't contain metas!!) -- THAT's actually UNSOUND when metas are instantiated, because -- max a b == max a c does not imply b == c -- as <- return $ Set.fromList $ closed0 as -- bs <- return $ Set.fromList $ closed0 bs -- let cs = Set.filter (not . hasMeta) $ Set.intersection as bs -- as <- return $ Set.toList $ as Set.\\ cs -- bs <- return $ Set.toList $ bs Set.\\ cs as <- return $ List.sort $ closed0 as bs <- return $ List.sort $ closed0 bs reportSDoc "tc.conv.level" 40 $ sep [ text "equalLevel" , vcat [ nest 2 $ sep [ prettyTCM a <+> text "==" , prettyTCM b ] , text "reduced" , nest 2 $ sep [ prettyTCM (Max as) <+> text "==" , prettyTCM (Max bs) ] ] ] reportSDoc "tc.conv.level" 50 $ sep [ text "equalLevel" , vcat [ nest 2 $ sep [ pretty (Max as) <+> text "==" , pretty (Max bs) ] ] ] case (as, bs) of _ | as == bs -> ok | any isBlocked (as ++ bs) -> do lvl <- levelType liftTCM $ useInjectivity CmpEq lvl (Level a) (Level b) -- closed == closed ([ClosedLevel n], [ClosedLevel m]) | n == m -> ok | otherwise -> notok -- closed == neutral ([ClosedLevel{}], _) | any isNeutral bs -> notok (_, [ClosedLevel{}]) | any isNeutral as -> notok -- 0 == any ([ClosedLevel 0], bs@(_:_:_)) -> sequence_ [ equalLevel' (Max []) (Max [b]) | b <- bs ] (as@(_:_:_), [ClosedLevel 0]) -> sequence_ [ equalLevel' (Max [a]) (Max []) | a <- as ] -- Andreas, 2014-04-07 Why should the following be ok? -- X (suc a) could be different from X (suc (suc a)) -- -- Same meta -- ([Plus n (MetaLevel x _)], [Plus m (MetaLevel y _)]) -- | n == m && x == y -> ok -- meta == any ([Plus n (MetaLevel x as)], _) | any (isThisMeta x) bs -> postpone (_, [Plus n (MetaLevel x bs)]) | any (isThisMeta x) as -> postpone ([Plus n (MetaLevel x as')], [Plus m (MetaLevel y bs')]) -- lexicographic comparison intended! | (n, y) < (m, x) -> meta n x as' bs | otherwise -> meta m y bs' as ([Plus n (MetaLevel x as')],_) -> meta n x as' bs (_,[Plus m (MetaLevel y bs')]) -> meta m y bs' as -- any other metas -- Andreas, 2013-10-31: There could be metas in neutral levels (see Issue 930). -- Should not we postpone there as well? Yes! _ | any hasMeta (as ++ bs) -> postpone -- neutral/closed == neutral/closed _ | all isNeutralOrClosed (as ++ bs) -> do reportSLn "tc.conv.level" 60 $ "equalLevel: all are neutral or closed" if length as == length bs then zipWithM_ (\a b -> [a] =!= [b]) as bs else notok -- more cases? _ -> postpone where a === b = unlessM typeInType $ do lvl <- levelType equalAtom lvl a b as =!= bs = levelTm (Max as) === levelTm (Max bs) ok = return () notok = unlessM typeInType notOk notOk = typeError $ UnequalSorts (Type a) (Type b) postpone = do reportSDoc "tc.conv.level" 30 $ hang (text "postponing:") 2 $ hang (pretty a <+> text "==") 0 (pretty b) patternViolation closed0 [] = [ClosedLevel 0] closed0 as = as -- perform assignment (Plus n (MetaLevel x as)) := bs meta n x as bs = do reportSLn "tc.meta.level" 30 $ "Assigning meta level" reportSDoc "tc.meta.level" 50 $ text "meta" <+> sep [prettyList $ map pretty as, prettyList $ map pretty bs] bs' <- mapM (subtr n) bs assignE DirEq x as (levelTm (Max bs')) (===) -- fallback: check equality as atoms -- Make sure to give a sensible error message wrap m = m `catchError` \err -> case err of TypeError{} -> notok _ -> throwError err subtr n (ClosedLevel m) | m >= n = return $ ClosedLevel (m - n) | otherwise = ifM typeInType (return $ ClosedLevel 0) $ notOk subtr n (Plus m a) | m >= n = return $ Plus (m - n) a subtr _ (Plus _ BlockedLevel{}) = postpone subtr _ (Plus _ MetaLevel{}) = postpone subtr _ (Plus _ NeutralLevel{}) = postpone subtr _ (Plus _ UnreducedLevel{}) = __IMPOSSIBLE__ isNeutral (Plus _ NeutralLevel{}) = True isNeutral _ = False isClosed ClosedLevel{} = True isClosed _ = False isNeutralOrClosed l = isClosed l || isNeutral l isBlocked (Plus _ BlockedLevel{}) = True isBlocked _ = False hasMeta ClosedLevel{} = False hasMeta (Plus _ MetaLevel{}) = True hasMeta (Plus _ (BlockedLevel _ v)) = not $ null $ allMetas v hasMeta (Plus _ (NeutralLevel _ v)) = not $ null $ allMetas v hasMeta (Plus _ (UnreducedLevel v)) = not $ null $ allMetas v isThisMeta x (Plus _ (MetaLevel y _)) = x == y isThisMeta _ _ = False constant (ClosedLevel n) = n constant (Plus n _) = n (ClosedLevel m) `isStrictlySubsumedBy` [] = m == 0 (ClosedLevel m) `isStrictlySubsumedBy` ys = m < maximum (map constant ys) (Plus m x) `isStrictlySubsumedBy` ys = not $ null $ [ n | Plus n y <- ys, x == y, m < n ] -- | Check that the first sort equal to the second. equalSort :: Sort -> Sort -> TCM () equalSort s1 s2 = do catchConstraint (SortCmp CmpEq s1 s2) $ do (s1,s2) <- reduce (s1,s2) let postpone = addConstraint (SortCmp CmpEq s1 s2) yes = return () no = unlessM typeInType $ typeError $ UnequalSorts s1 s2 -- Test whether a level is infinity. isInf ClosedLevel{} = no isInf (Plus _ l) = case l of MetaLevel x es -> assignE DirEq x es (Sort Inf) $ equalAtom topSort -- Andreas, 2015-02-14 -- This seems to be a hack, as a level meta is instantiated -- by a sort. NeutralLevel _ v -> case ignoreSharing v of Sort Inf -> yes _ -> no _ -> no -- Equate a level with SizeUniv. eqSizeUniv l0 = case l0 of Plus 0 l -> case l of MetaLevel x es -> assignE DirEq x es (Sort SizeUniv) $ equalAtom topSort NeutralLevel _ v -> case ignoreSharing v of Sort SizeUniv -> yes _ -> no _ -> no _ -> no reportSDoc "tc.conv.sort" 30 $ sep [ text "equalSort" , vcat [ nest 2 $ fsep [ prettyTCM s1 <+> text "==" , prettyTCM s2 ] , nest 2 $ fsep [ pretty s1 <+> text "==" , pretty s2 ] ] ] case (s1, s2) of (Type a , Type b ) -> equalLevel a b (SizeUniv, SizeUniv) -> yes (SizeUniv, Type (Max as@(_:_))) -> mapM_ eqSizeUniv as (Type (Max as@(_:_)), SizeUniv) -> mapM_ eqSizeUniv as (SizeUniv, _ ) -> no (_ , SizeUniv) -> no (Prop , Prop ) -> yes (Type _ , Prop ) -> no (Prop , Type _ ) -> no (Inf , Inf ) -> yes (Inf , Type (Max as@(_:_))) -> mapM_ isInf as (Type (Max as@(_:_)), Inf) -> mapM_ isInf as -- Andreas, 2014-06-27: -- @Type (Max [])@ (which is Set0) falls through to error. (Inf , _ ) -> no (_ , Inf ) -> no -- Andreas, 2014-06-27: Why are there special cases for Set0? -- Andreas, 2015-02-14: Probably because s ⊔ s' = Set0 -- entailed that both s and s' are Set0. -- This is no longer true if SizeUniv ⊔ s = s -- (DLub s1 s2, s0@(Type (Max []))) -> do -- equalSort s1 s0 -- underAbstraction_ s2 $ \s2 -> equalSort s2 s0 -- (s0@(Type (Max [])), DLub s1 s2) -> do -- equalSort s0 s1 -- underAbstraction_ s2 $ \s2 -> equalSort s0 s2 (DLub{} , _ ) -> postpone (_ , DLub{} ) -> postpone --------------------------------------------------------------------------- -- * Definitions --------------------------------------------------------------------------- bothAbsurd :: QName -> QName -> TCM Bool bothAbsurd f f' | isAbsurdLambdaName f, isAbsurdLambdaName f' = do def <- getConstInfo f def' <- getConstInfo f' case (theDef def, theDef def') of (Function{ funCompiled = Just Fail}, Function{ funCompiled = Just Fail}) -> return True _ -> return False | otherwise = return False Agda-2.5.3/src/full/Agda/TypeChecking/Abstract.hs0000644000000000000000000001714213154613124017611 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE UndecidableInstances #-} -- | Functions for abstracting terms over other terms. module Agda.TypeChecking.Abstract where import Control.Applicative import Control.Monad import Control.Monad.State import Data.Function import Data.Traversable import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin (equalityUnview) import Agda.TypeChecking.Substitute import Agda.TypeChecking.CheckInternal import Agda.TypeChecking.Conversion import Agda.TypeChecking.Constraints import Agda.TypeChecking.Pretty import Agda.Utils.Functor import Agda.Utils.List (splitExactlyAt) import Agda.Utils.Size import Agda.Utils.Except import qualified Agda.Utils.HashMap as HMap import Agda.Utils.Impossible #include "undefined.h" typeOf :: Type -> Type typeOf = sort . getSort -- | @abstractType a v b[v] = b@ where @a : v@. abstractType :: Type -> Term -> Type -> TCM Type abstractType a v (El s b) = El (absTerm v s) <$> abstractTerm a v (sort s) b -- | @piAbstractTerm v a b[v] = (w : a) -> b[w]@ piAbstractTerm :: Term -> Type -> Type -> TCM Type piAbstractTerm v a b = do fun <- mkPi (defaultDom ("w", a)) <$> abstractType a v b reportSDoc "tc.abstract" 50 $ sep [ text "piAbstract" <+> sep [ prettyTCM v <+> text ":", nest 2 $ prettyTCM a ] , nest 2 $ text "from" <+> prettyTCM b , nest 2 $ text "-->" <+> prettyTCM fun ] return fun -- | @piAbstract (v, a) b[v] = (w : a) -> b[w]@ -- -- For @rewrite@, it does something special: -- -- @piAbstract (prf, Eq a v v') b[v,prf] = (w : a) (w' : Eq a w v') -> b[w,w']@ piAbstract :: (Term, EqualityView) -> Type -> TCM Type piAbstract (v, OtherType a) b = piAbstractTerm v a b piAbstract (prf, eqt@(EqualityType _ _ _ (Arg _ a) v _)) b = do s <- inferSort a let prfTy = equalityUnview eqt vTy = El s a b <- abstractType prfTy prf b b <- addContext ("w", defaultDom prfTy) $ abstractType (raise 1 vTy) (unArg $ raise 1 v) b return . funType vTy . funType eqTy' . swap01 $ b where funType a = mkPi $ defaultDom ("w", a) -- Abstract the lhs (@a@) of the equality only. eqt1 = raise 1 eqt eqTy' = equalityUnview $ eqt1 { eqtLhs = eqtLhs eqt1 $> var 0 } -- | @isPrefixOf u v = Just es@ if @v == u `applyE` es@. class IsPrefixOf a where isPrefixOf :: a -> a -> Maybe Elims instance IsPrefixOf Elims where isPrefixOf us vs = do (vs1, vs2) <- splitExactlyAt (length us) vs guard $ us == vs1 return vs2 instance IsPrefixOf Args where isPrefixOf us vs = do (vs1, vs2) <- splitExactlyAt (length us) vs guard $ us == vs1 return $ map Apply vs2 instance IsPrefixOf Term where isPrefixOf u v = case (ignoreSharing u, ignoreSharing v) of (Var i us, Var j vs) | i == j -> us `isPrefixOf` vs (Def f us, Def g vs) | f == g -> us `isPrefixOf` vs (Con c _ us, Con d _ vs) | c == d -> us `isPrefixOf` vs (MetaV x us, MetaV y vs) | x == y -> us `isPrefixOf` vs (u, v) -> guard (u == v) >> return [] -- Type-based abstraction. Needed if u is a constructor application (#745). abstractTerm :: Type -> Term -> Type -> Term -> TCM Term abstractTerm a u@Con{} b v = do reportSDoc "tc.abstract" 50 $ sep [ text "Abstracting" , nest 2 $ sep [ prettyTCM u <+> text ":", nest 2 $ prettyTCM a ] , text "over" , nest 2 $ sep [ prettyTCM v <+> text ":", nest 2 $ prettyTCM b ] ] hole <- qualify <$> currentModule <*> freshName_ "hole" noMutualBlock $ addConstant hole $ defaultDefn defaultArgInfo hole a Axiom args <- map Apply <$> getContextArgs let n = length args let abstr b v = do m <- getContextSize let (a', u') = raise (m - n) (a, u) case isPrefixOf u' v of Nothing -> return v Just es -> do -- Check that the types match. s <- get do disableDestructiveUpdate (noConstraints $ equalType a' b) put s return $ Def hole (raise (m - n) args ++ es) `catchError` \ _ -> do reportSDoc "tc.abstract.ill-typed" 50 $ sep [ text "Skipping ill-typed abstraction" , nest 2 $ sep [ prettyTCM v <+> text ":", nest 2 $ prettyTCM b ] ] return v v <- catchError_ (checkInternal' (defaultAction { preAction = abstr }) v b) $ \ err -> do reportSDoc "impossible" 10 $ vcat [ text "Type error in term to abstract" , nest 2 $ (prettyTCM =<< getContextTelescope) <+> text "⊢" , nest 2 $ sep [ prettyTCM v <+> text ":", nest 2 $ prettyTCM b ] , nest 2 $ prettyTCM err ] reportSDoc "impossible" 60 $ vcat [ text "Type error in term to abstract (raw)" , nest 2 $ ((text . show) =<< getContextTelescope) <+> text "⊢" , nest 2 $ sep [ (text . show) v <+> text ":", nest 2 $ (text . show) b ] ] __IMPOSSIBLE__ reportSDoc "tc.abstract" 50 $ sep [ text "Resulting abstraction", nest 2 $ prettyTCM v ] modifySignature $ updateDefinitions $ HMap.delete hole return $ absTerm (Def hole args) v abstractTerm _ u _ v = return $ absTerm u v -- Non-constructors can use untyped abstraction class AbsTerm a where -- | @subst u . absTerm u == id@ absTerm :: Term -> a -> a instance AbsTerm Term where absTerm u v | Just es <- u `isPrefixOf` v = Var 0 $ absT es | otherwise = case v of -- Andreas, 2013-10-20: the original impl. works only at base types -- v | u == v -> Var 0 [] -- incomplete see succeed/WithOfFunctionType Var i vs -> Var (i + 1) $ absT vs Lam h b -> Lam h $ absT b Def c vs -> Def c $ absT vs Con c ci vs -> Con c ci $ absT vs Pi a b -> uncurry Pi $ absT (a, b) Lit l -> Lit l Level l -> Level $ absT l Sort s -> Sort $ absT s MetaV m vs -> MetaV m $ absT vs DontCare mv -> DontCare $ absT mv Shared p -> Shared $ absT p where absT x = absTerm u x instance AbsTerm a => AbsTerm (Ptr a) where absTerm u = fmap (absTerm u) instance AbsTerm Type where absTerm u (El s v) = El (absTerm u s) (absTerm u v) instance AbsTerm Sort where absTerm u s = case s of Type n -> Type $ absS n Prop -> Prop Inf -> Inf SizeUniv -> SizeUniv DLub s1 s2 -> DLub (absS s1) (absS s2) where absS x = absTerm u x instance AbsTerm Level where absTerm u (Max as) = Max $ absTerm u as instance AbsTerm PlusLevel where absTerm u l@ClosedLevel{} = l absTerm u (Plus n l) = Plus n $ absTerm u l instance AbsTerm LevelAtom where absTerm u l = case l of MetaLevel m vs -> MetaLevel m $ absTerm u vs NeutralLevel r v -> NeutralLevel r $ absTerm u v BlockedLevel _ v -> UnreducedLevel $ absTerm u v -- abstracting might remove the blockage UnreducedLevel v -> UnreducedLevel $ absTerm u v instance AbsTerm a => AbsTerm (Elim' a) where absTerm = fmap . absTerm instance AbsTerm a => AbsTerm (Arg a) where absTerm = fmap . absTerm instance AbsTerm a => AbsTerm (Dom a) where absTerm = fmap . absTerm instance AbsTerm a => AbsTerm [a] where absTerm = fmap . absTerm instance AbsTerm a => AbsTerm (Maybe a) where absTerm = fmap . absTerm instance (Subst Term a, AbsTerm a) => AbsTerm (Abs a) where absTerm u (NoAbs x v) = NoAbs x $ absTerm u v absTerm u (Abs x v) = Abs x $ swap01 $ absTerm (raise 1 u) v instance (AbsTerm a, AbsTerm b) => AbsTerm (a, b) where absTerm u (x, y) = (absTerm u x, absTerm u y) -- | This swaps @var 0@ and @var 1@. swap01 :: (Subst Term a) => a -> a swap01 = applySubst $ var 1 :# liftS 1 (raiseS 1) Agda-2.5.3/src/full/Agda/TypeChecking/Errors.hs0000644000000000000000000017177013154613124017332 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.TypeChecking.Errors ( prettyError , prettyWarning , tcErrString , prettyTCWarnings' , prettyTCWarnings , tcWarningsToError , applyFlagsToTCWarnings , dropTopLevelModule , stringTCErr , sayWhen ) where import Prelude hiding (null) import Control.Monad.Reader import Control.Monad.State #if __GLASGOW_HASKELL__ <= 708 import Data.Foldable (foldMap) #endif import Data.Function import Data.List (nub, sortBy, intersperse, isInfixOf) import Data.Maybe import Data.Char (toLower) import qualified Data.Set as Set import qualified Data.Map as Map import qualified Text.PrettyPrint.Boxes as Boxes import Agda.Interaction.Options import Agda.Syntax.Common import Agda.Syntax.Fixity import Agda.Syntax.Notation import Agda.Syntax.Position import qualified Agda.Syntax.Info as A import qualified Agda.Syntax.Concrete as C import qualified Agda.Syntax.Concrete.Definitions as D import Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Views (deepUnscope) import Agda.Syntax.Internal as I import Agda.Syntax.Translation.InternalToAbstract import Agda.Syntax.Translation.AbstractToConcrete import Agda.Syntax.Scope.Monad (isDatatypeModule) import Agda.Syntax.Scope.Base import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Closure import Agda.TypeChecking.Monad.Context import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Options import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Monad.State import Agda.TypeChecking.Positivity import Agda.TypeChecking.Pretty import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope ( ifPiType ) import Agda.TypeChecking.Reduce (instantiate) import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.FileName import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Pretty ( prettyShow ) import qualified Agda.Utils.Pretty as P import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Top level function --------------------------------------------------------------------------- {-# SPECIALIZE prettyError :: TCErr -> TCM String #-} prettyError :: MonadTCM tcm => TCErr -> tcm String prettyError err = liftTCM $ show <$> prettyError' err [] where prettyError' :: TCErr -> [TCErr] -> TCM Doc prettyError' err errs | length errs > 3 = fsep ( pwords "total panic: error when printing error from printing error from printing error." ++ pwords "I give up! Approximations of errors (original error last):" ) $$ vcat (map (text . tcErrString) errs) | otherwise = applyUnless (null errs) (text "panic: error when printing error!" $$) $ do (prettyTCM err $$ vcat (map (text . ("when printing error " ++) . tcErrString) errs)) `catchError` \ err' -> prettyError' err' (err:errs) --------------------------------------------------------------------------- -- * Warnings --------------------------------------------------------------------------- instance PrettyTCM TCWarning where prettyTCM = return . tcWarningPrintedWarning instance PrettyTCM Warning where prettyTCM = prettyWarning {-# SPECIALIZE prettyWarning :: Warning -> TCM Doc #-} prettyWarning :: MonadTCM tcm => Warning -> tcm Doc prettyWarning wng = liftTCM $ case wng of UnsolvedMetaVariables ms -> fsep ( pwords "Unsolved metas at the following locations:" ) $$ nest 2 (vcat $ map prettyTCM ms) UnsolvedInteractionMetas is -> fsep ( pwords "Unsolved interaction metas at the following locations:" ) $$ nest 2 (vcat $ map prettyTCM is) UnsolvedConstraints cs -> fsep ( pwords "Failed to solve the following constraints:" ) $$ nest 2 (P.vcat . nub <$> mapM prettyConstraint cs) where prettyConstraint :: ProblemConstraint -> TCM Doc prettyConstraint c = f (prettyTCM c) where r = getRange c f d = if null $ P.pretty r then d else d $$ nest 4 (text "[ at" <+> prettyTCM r <+> text "]") TerminationIssue tes -> do dropTopLevel <- topLevelModuleDropper fwords "Termination checking failed for the following functions:" $$ (nest 2 $ fsep $ punctuate comma $ map (pretty . dropTopLevel) $ concatMap termErrFunctions tes) $$ fwords "Problematic calls:" $$ (nest 2 $ fmap (P.vcat . nub) $ mapM prettyTCM $ sortBy (compare `on` callInfoRange) $ concatMap termErrCalls tes) UnreachableClauses f pss -> fsep $ pwords "Unreachable" ++ pwords (plural (length pss) "clause") where plural 1 thing = thing plural n thing = thing ++ "s" CoverageIssue f pss -> fsep ( pwords "Incomplete pattern matching for" ++ [prettyTCM f <> text "."] ++ pwords "Missing cases:") $$ nest 2 (vcat $ map display pss) where display (tel, ps) = prettyTCM $ NamedClause f True $ empty { clauseTel = tel, namedClausePats = ps } CoverageNoExactSplit f cs -> vcat $ [ fsep $ pwords "Exact splitting is enabled, but the following" ++ pwords (singPlural cs "clause" "clauses") ++ pwords "could not be preserved as definitional equalities in the translation to a case tree:" ] ++ map (nest 2 . prettyTCM . NamedClause f True) cs NotStrictlyPositive d ocs -> fsep $ [prettyTCM d] ++ pwords "is not strictly positive, because it occurs" ++ [prettyTCM ocs] OldBuiltin old new -> fwords $ "Builtin " ++ old ++ " does no longer exist. " ++ "It is now bound by BUILTIN " ++ new EmptyRewritePragma -> fsep . pwords $ "Empty REWRITE pragma" UselessPublic -> fwords $ "Keyword `public' is ignored here" UselessInline q -> fsep $ pwords "It is pointless for INLINE'd function" ++ [prettyTCM q] ++ pwords "to have a separate Haskell definition" GenericWarning d -> return d GenericNonFatalError d -> return d SafeFlagPostulate e -> fsep $ pwords "Cannot postulate" ++ [pretty e] ++ pwords "with safe flag" SafeFlagPragma xs -> let plural | length xs == 1 = "" | otherwise = "s" in fsep $ [fwords ("Cannot set OPTION pragma" ++ plural)] ++ map text xs ++ [fwords "with safe flag."] SafeFlagNonTerminating -> fsep $ pwords "Cannot use NON_TERMINATING pragma with safe flag." SafeFlagTerminating -> fsep $ pwords "Cannot use TERMINATING pragma with safe flag." SafeFlagPrimTrustMe -> fsep (pwords "Cannot use primTrustMe with safe flag") SafeFlagNoPositivityCheck -> fsep $ pwords "Cannot use NO_POSITIVITY_CHECK pragma with safe flag." SafeFlagPolarity -> fsep $ pwords "The POLARITY pragma must not be used in safe mode." ParseWarning pw -> pretty pw DeprecationWarning old new version -> fsep $ [text old] ++ pwords "has been deprecated. Use" ++ [text new] ++ pwords "instead. This will be an error in Agda" ++ [text version <> text "."] NicifierIssue ws -> vcat $ do for ws $ \ w -> do sayWhere (getRange w) $ pretty w prettyTCWarnings :: [TCWarning] -> TCM String prettyTCWarnings = fmap (unlines . intersperse "") . prettyTCWarnings' prettyTCWarnings' :: [TCWarning] -> TCM [String] prettyTCWarnings' = mapM (fmap show . prettyTCM) -- | Turns all warnings into errors. tcWarningsToError :: [TCWarning] -> TCM a tcWarningsToError ws = typeError $ case ws of [] -> SolvedButOpenHoles _ -> NonFatalErrors ws -- | Depending which flags are set, one may happily ignore some -- warnings. applyFlagsToTCWarnings :: IgnoreFlags -> [TCWarning] -> TCM [TCWarning] applyFlagsToTCWarnings ifs ws = do -- For some reason some SafeFlagPragma seem to be created multiple times. -- This is a way to collect all of them and remove duplicates. let pragmas w = case tcWarning w of { SafeFlagPragma ps -> ([w], ps); _ -> ([], []) } let sfp = case fmap nub (foldMap pragmas ws) of (TCWarning r w p:_, sfp) -> [TCWarning r (SafeFlagPragma sfp) p] _ -> [] unsolvedNotOK <- not . optAllowUnsolved <$> pragmaOptions negativeNotOK <- not . optDisablePositivity <$> pragmaOptions loopingNotOK <- optTerminationCheck <$> pragmaOptions catchallNotOK <- optExactSplit <$> pragmaOptions -- filter out the warnings the flags told us to ignore let cleanUp w = let ignore = ifs == IgnoreFlags keepUnsolved us = not (null us) && (ignore || unsolvedNotOK) in case w of TerminationIssue{} -> ignore || loopingNotOK CoverageIssue{} -> ignore || unsolvedNotOK NotStrictlyPositive{} -> ignore || negativeNotOK UnsolvedMetaVariables ums -> keepUnsolved ums UnsolvedInteractionMetas uis -> keepUnsolved uis UnsolvedConstraints ucs -> keepUnsolved ucs OldBuiltin{} -> True EmptyRewritePragma -> True UselessPublic -> True ParseWarning{} -> True UnreachableClauses{} -> True CoverageNoExactSplit{} -> catchallNotOK UselessInline{} -> True GenericWarning{} -> True GenericNonFatalError{} -> True SafeFlagPostulate{} -> True SafeFlagPragma{} -> False -- dealt with separately SafeFlagNonTerminating -> True SafeFlagTerminating -> True SafeFlagPrimTrustMe -> True SafeFlagNoPositivityCheck -> True SafeFlagPolarity -> True DeprecationWarning{} -> True NicifierIssue{} -> True return $ sfp ++ filter (cleanUp . tcWarning) ws --------------------------------------------------------------------------- -- * Helpers --------------------------------------------------------------------------- sayWhere :: HasRange a => a -> TCM Doc -> TCM Doc sayWhere x d = applyUnless (null r) (prettyTCM r $$) d where r = getRange x sayWhen :: Range -> Maybe (Closure Call) -> TCM Doc -> TCM Doc sayWhen r Nothing m = sayWhere r m sayWhen r (Just cl) m = sayWhere r (m $$ prettyTCM cl) panic :: String -> TCM Doc panic s = fwords $ "Panic: " ++ s nameWithBinding :: QName -> TCM Doc nameWithBinding q = sep [ prettyTCM q, text "bound at", prettyTCM r ] where r = nameBindingSite $ qnameName q tcErrString :: TCErr -> String tcErrString err = show (getRange err) ++ " " ++ case err of TypeError _ cl -> errorString $ clValue cl Exception r s -> show r ++ " " ++ show s IOException _ r e -> show r ++ " " ++ show e PatternErr{} -> "PatternErr" stringTCErr :: String -> TCErr stringTCErr = Exception noRange . P.text errorString :: TypeError -> String errorString err = case err of AmbiguousModule{} -> "AmbiguousModule" AmbiguousName{} -> "AmbiguousName" AmbiguousParseForApplication{} -> "AmbiguousParseForApplication" AmbiguousParseForLHS{} -> "AmbiguousParseForLHS" -- AmbiguousParseForPatternSynonym{} -> "AmbiguousParseForPatternSynonym" AmbiguousTopLevelModuleName {} -> "AmbiguousTopLevelModuleName" BadArgumentsToPatternSynonym{} -> "BadArgumentsToPatternSynonym" TooFewArgumentsToPatternSynonym{} -> "TooFewArgumentsToPatternSynonym" BothWithAndRHS -> "BothWithAndRHS" BuiltinInParameterisedModule{} -> "BuiltinInParameterisedModule" BuiltinMustBeConstructor{} -> "BuiltinMustBeConstructor" ClashingDefinition{} -> "ClashingDefinition" ClashingFileNamesFor{} -> "ClashingFileNamesFor" ClashingImport{} -> "ClashingImport" ClashingModule{} -> "ClashingModule" ClashingModuleImport{} -> "ClashingModuleImport" CompilationError{} -> "CompilationError" ConstructorPatternInWrongDatatype{} -> "ConstructorPatternInWrongDatatype" CyclicModuleDependency{} -> "CyclicModuleDependency" DataMustEndInSort{} -> "DataMustEndInSort" -- UNUSED: DataTooManyParameters{} -> "DataTooManyParameters" CantResolveOverloadedConstructorsTargetingSameDatatype{} -> "CantResolveOverloadedConstructorsTargetingSameDatatype" DifferentArities -> "DifferentArities" DoesNotConstructAnElementOf{} -> "DoesNotConstructAnElementOf" DuplicateBuiltinBinding{} -> "DuplicateBuiltinBinding" DuplicateConstructors{} -> "DuplicateConstructors" DuplicateFields{} -> "DuplicateFields" DuplicateImports{} -> "DuplicateImports" FieldOutsideRecord -> "FieldOutsideRecord" FileNotFound{} -> "FileNotFound" GenericError{} -> "GenericError" GenericDocError{} -> "GenericDocError" IFSNoCandidateInScope{} -> "IFSNoCandidateInScope" IlltypedPattern{} -> "IlltypedPattern" IllformedProjectionPattern{} -> "IllformedProjectionPattern" CannotEliminateWithPattern{} -> "CannotEliminateWithPattern" IllegalLetInTelescope{} -> "IllegalLetInTelescope" -- UNUSED: IncompletePatternMatching{} -> "IncompletePatternMatching" InternalError{} -> "InternalError" InvalidPattern{} -> "InvalidPattern" LocalVsImportedModuleClash{} -> "LocalVsImportedModuleClash" MetaCannotDependOn{} -> "MetaCannotDependOn" MetaOccursInItself{} -> "MetaOccursInItself" ModuleArityMismatch{} -> "ModuleArityMismatch" ModuleDefinedInOtherFile {} -> "ModuleDefinedInOtherFile" ModuleDoesntExport{} -> "ModuleDoesntExport" ModuleNameUnexpected{} -> "ModuleNameUnexpected" ModuleNameDoesntMatchFileName {} -> "ModuleNameDoesntMatchFileName" NeedOptionCopatterns{} -> "NeedOptionCopatterns" NeedOptionRewriting{} -> "NeedOptionRewriting" NoBindingForBuiltin{} -> "NoBindingForBuiltin" NoParseForApplication{} -> "NoParseForApplication" NoParseForLHS{} -> "NoParseForLHS" -- NoParseForPatternSynonym{} -> "NoParseForPatternSynonym" NoRHSRequiresAbsurdPattern{} -> "NoRHSRequiresAbsurdPattern" AbsurdPatternRequiresNoRHS{} -> "AbsurdPatternRequiresNoRHS" NoSuchBuiltinName{} -> "NoSuchBuiltinName" NoSuchModule{} -> "NoSuchModule" NoSuchPrimitiveFunction{} -> "NoSuchPrimitiveFunction" NotAModuleExpr{} -> "NotAModuleExpr" NotAProperTerm -> "NotAProperTerm" SetOmegaNotValidType{} -> "SetOmegaNotValidType" InvalidType{} -> "InvalidType" InvalidTypeSort{} -> "InvalidTypeSort" FunctionTypeInSizeUniv{} -> "FunctionTypeInSizeUniv" NotAValidLetBinding{} -> "NotAValidLetBinding" NotValidBeforeField{} -> "NotValidBeforeField" NotAnExpression{} -> "NotAnExpression" NotImplemented{} -> "NotImplemented" NotSupported{} -> "NotSupported" AbstractConstructorNotInScope{} -> "AbstractConstructorNotInScope" NotInScope{} -> "NotInScope" NotLeqSort{} -> "NotLeqSort" NothingAppliedToHiddenArg{} -> "NothingAppliedToHiddenArg" NothingAppliedToInstanceArg{} -> "NothingAppliedToInstanceArg" OverlappingProjects {} -> "OverlappingProjects" OperatorInformation {} -> "OperatorInformation" PatternShadowsConstructor {} -> "PatternShadowsConstructor" PropMustBeSingleton -> "PropMustBeSingleton" RepeatedVariablesInPattern{} -> "RepeatedVariablesInPattern" ShadowedModule{} -> "ShadowedModule" ShouldBeASort{} -> "ShouldBeASort" ShouldBeApplicationOf{} -> "ShouldBeApplicationOf" ShouldBeAppliedToTheDatatypeParameters{} -> "ShouldBeAppliedToTheDatatypeParameters" ShouldBeEmpty{} -> "ShouldBeEmpty" ShouldBePi{} -> "ShouldBePi" ShouldBeRecordType{} -> "ShouldBeRecordType" ShouldBeRecordPattern{} -> "ShouldBeRecordPattern" NotAProjectionPattern{} -> "NotAProjectionPattern" ShouldEndInApplicationOfTheDatatype{} -> "ShouldEndInApplicationOfTheDatatype" SplitError{} -> "SplitError" ImpossibleConstructor{} -> "ImpossibleConstructor" TerminationCheckFailed{} -> "TerminationCheckFailed" TooFewFields{} -> "TooFewFields" TooManyArgumentsInLHS{} -> "TooManyArgumentsInLHS" TooManyFields{} -> "TooManyFields" TooManyPolarities{} -> "TooManyPolarities" SplitOnIrrelevant{} -> "SplitOnIrrelevant" DefinitionIsIrrelevant{} -> "DefinitionIsIrrelevant" VariableIsIrrelevant{} -> "VariableIsIrrelevant" UnequalBecauseOfUniverseConflict{} -> "UnequalBecauseOfUniverseConflict" UnequalRelevance{} -> "UnequalRelevance" UnequalHiding{} -> "UnequalHiding" -- UnequalLevel{} -> "UnequalLevel" -- UNUSED UnequalSorts{} -> "UnequalSorts" UnequalTerms{} -> "UnequalTerms" UnequalTypes{} -> "UnequalTypes" -- UnequalTelescopes{} -> "UnequalTelescopes" -- UNUSED WithOnFreeVariable{} -> "WithOnFreeVariable" UnexpectedWithPatterns{} -> "UnexpectedWithPatterns" UninstantiatedDotPattern{} -> "UninstantiatedDotPattern" UninstantiatedModule{} -> "UninstantiatedModule" SolvedButOpenHoles{} -> "SolvedButOpenHoles" UnusedVariableInPatternSynonym -> "UnusedVariableInPatternSynonym" UnquoteFailed{} -> "UnquoteFailed" DeBruijnIndexOutOfScope{} -> "DeBruijnIndexOutOfScope" WithClausePatternMismatch{} -> "WithClausePatternMismatch" WrongHidingInApplication{} -> "WrongHidingInApplication" WrongHidingInLHS{} -> "WrongHidingInLHS" WrongHidingInLambda{} -> "WrongHidingInLambda" WrongInstanceDeclaration{} -> "WrongInstanceDeclaration" WrongIrrelevanceInLambda{} -> "WrongIrrelevanceInLambda" WrongNamedArgument{} -> "WrongNamedArgument" WrongNumberOfConstructorArguments{} -> "WrongNumberOfConstructorArguments" HidingMismatch{} -> "HidingMismatch" RelevanceMismatch{} -> "RelevanceMismatch" NonFatalErrors{} -> "NonFatalErrors" InstanceSearchDepthExhausted{} -> "InstanceSearchDepthExhausted" instance PrettyTCM TCErr where prettyTCM err = case err of -- Gallais, 2016-05-14 -- Given where `NonFatalErrors` are created, we know for a -- fact that ̀ws` is non-empty. TypeError _ (Closure _ _ _ _ (NonFatalErrors ws)) -> foldr1 ($$) $ fmap prettyTCM ws -- Andreas, 2014-03-23 -- This use of localState seems ok since we do not collect -- Benchmark info during printing errors. TypeError s e -> localState $ do put s sayWhen (envRange $ clEnv e) (envCall $ clEnv e) $ prettyTCM e Exception r s -> sayWhere r $ return s IOException _ r e -> sayWhere r $ fwords $ show e PatternErr{} -> sayWhere err $ panic "uncaught pattern violation" instance PrettyTCM CallInfo where prettyTCM c = do let call = prettyTCM $ callInfoCall c r = callInfoRange c if null $ P.pretty r then call else call $$ nest 2 (text "(at" <+> prettyTCM r <> text ")") -- | Drops given amount of leading components of the qualified name. dropTopLevelModule' :: Int -> QName -> QName dropTopLevelModule' k (QName (MName ns) n) = QName (MName (drop k ns)) n -- | Drops the filename component of the qualified name. dropTopLevelModule :: QName -> TCM QName dropTopLevelModule q = ($ q) <$> topLevelModuleDropper -- | Produces a function which drops the filename component of the qualified name. topLevelModuleDropper :: TCM (QName -> QName) topLevelModuleDropper = do caseMaybeM (asks envCurrentPath) (return id) $ \ f -> do m <- fromMaybe __IMPOSSIBLE__ <$> lookupModuleFromSource f return $ dropTopLevelModule' $ size m instance PrettyTCM TypeError where prettyTCM err = case err of InternalError s -> panic s NotImplemented s -> fwords $ "Not implemented: " ++ s NotSupported s -> fwords $ "Not supported: " ++ s CompilationError s -> sep [fwords "Compilation error:", text s] GenericError s -> fwords s GenericDocError d -> return d TerminationCheckFailed because -> do dropTopLevel <- topLevelModuleDropper fwords "Termination checking failed for the following functions:" $$ (nest 2 $ fsep $ punctuate comma $ map (pretty . dropTopLevel) $ concatMap termErrFunctions because) $$ fwords "Problematic calls:" $$ (nest 2 $ fmap (P.vcat . nub) $ mapM prettyTCM $ sortBy (compare `on` callInfoRange) $ concatMap termErrCalls because) PropMustBeSingleton -> fwords "Datatypes in Prop must have at most one constructor when proof irrelevance is enabled" DataMustEndInSort t -> fsep $ pwords "The type of a datatype must end in a sort." ++ [prettyTCM t] ++ pwords "isn't a sort." {- UNUSED: DataTooManyParameters -> fsep $ pwords "Too many parameters given to data type." -} ShouldEndInApplicationOfTheDatatype t -> fsep $ pwords "The target of a constructor must be the datatype applied to its parameters," ++ [prettyTCM t] ++ pwords "isn't" ShouldBeAppliedToTheDatatypeParameters s t -> fsep $ pwords "The target of the constructor should be" ++ [prettyTCM s] ++ pwords "instead of" ++ [prettyTCM t] ShouldBeApplicationOf t q -> fsep $ pwords "The pattern constructs an element of" ++ [prettyTCM q] ++ pwords "which is not the right datatype" ShouldBeRecordType t -> fsep $ pwords "Expected non-abstract record type, found " ++ [prettyTCM t] ShouldBeRecordPattern p -> fsep $ pwords "Expected record pattern" -- ", found " ++ [prettyTCM p] NotAProjectionPattern p -> fsep $ pwords "Not a valid projection for a copattern: " ++ [ prettyA p ] DifferentArities -> fwords "The number of arguments in the defining equations differ" WrongHidingInLHS -> fwords "Unexpected implicit argument" WrongHidingInLambda t -> fwords "Found an implicit lambda where an explicit lambda was expected" WrongIrrelevanceInLambda -> fwords "Found a non-strict lambda where a irrelevant lambda was expected" WrongNamedArgument a -> fsep $ pwords "Function does not accept argument " ++ [prettyTCM a] -- ++ pwords " (wrong argument name)" WrongHidingInApplication t -> fwords "Found an implicit application where an explicit application was expected" WrongInstanceDeclaration -> fwords "Terms marked as eligible for instance search should end with a name" HidingMismatch h h' -> fwords $ "Expected " ++ verbalize (Indefinite h') ++ " argument, but found " ++ verbalize (Indefinite h) ++ " argument" RelevanceMismatch r r' -> fwords $ "Expected " ++ verbalize (Indefinite r') ++ " argument, but found " ++ verbalize (Indefinite r) ++ " argument" UninstantiatedDotPattern e -> fsep $ pwords "Failed to infer the value of dotted pattern" IlltypedPattern p a -> do let ho _ _ = fsep $ pwords "Cannot pattern match on functions" ifPiType a ho $ {- else -} \ _ -> do fsep $ pwords "Type mismatch" IllformedProjectionPattern p -> fsep $ pwords "Ill-formed projection pattern " ++ [prettyA p] CannotEliminateWithPattern p a -> do let isProj = isJust (isProjP p) fsep $ pwords "Cannot eliminate type" ++ prettyTCM a : if isProj then pwords "with projection pattern" ++ [prettyA p] else pwords "with pattern" ++ prettyA p : pwords "(did you supply too many arguments?)" TooManyArgumentsInLHS a -> fsep $ pwords "Left hand side gives too many arguments to a function of type" ++ [prettyTCM a] WrongNumberOfConstructorArguments c expect given -> fsep $ pwords "The constructor" ++ [prettyTCM c] ++ pwords "expects" ++ [prettyTCM expect] ++ pwords "arguments (including hidden ones), but has been given" ++ [prettyTCM given] ++ pwords "(including hidden ones)" CantResolveOverloadedConstructorsTargetingSameDatatype d cs -> fsep $ pwords "Can't resolve overloaded constructors targeting the same datatype" ++ [(parens $ prettyTCM (qnameToConcrete d)) <> colon] ++ map pretty cs DoesNotConstructAnElementOf c t -> fsep $ pwords "The constructor" ++ [prettyTCM c] ++ pwords "does not construct an element of" ++ [prettyTCM t] ConstructorPatternInWrongDatatype c d -> fsep $ [prettyTCM c] ++ pwords "is not a constructor of the datatype" ++ [prettyTCM d] ShadowedModule x [] -> __IMPOSSIBLE__ ShadowedModule x ms@(m0 : _) -> do -- Clash! Concrete module name x already points to the abstract names ms. (r, m) <- do -- Andreas, 2017-07-28, issue #719. -- First, we try to find whether one of the abstract names @ms@ points back to @x@ scope <- getScope -- Get all pairs (y,m) such that y points to some m ∈ ms. let xms0 = ms >>= \ m -> map (,m) $ inverseScopeLookupModule m scope reportSLn "scope.clash.error" 30 $ "candidates = " ++ prettyShow xms0 -- Try to find x (which will have a different Range, if it has one (#2649)). let xms = filter ((\ y -> not (null $ getRange y) && y == C.QName x) . fst) xms0 reportSLn "scope.class.error" 30 $ "filtered candidates = " ++ prettyShow xms -- If we found a copy of x with non-empty range, great! ifJust (headMaybe xms) (\ (x', m) -> return (getRange x', m)) $ {-else-} do -- If that failed, we pick the first m from ms which has a nameBindingSite. let rms = ms >>= \ m -> map (,m) $ filter (noRange /=) $ map nameBindingSite $ reverse $ mnameToList m -- Andreas, 2017-07-25, issue #2649 -- Take the first nameBindingSite we can get hold of. reportSLn "scope.class.error" 30 $ "rangeful clashing modules = " ++ prettyShow rms -- If even this fails, we pick the first m and give no range. return $ fromMaybe (noRange, m0) $ headMaybe rms fsep $ pwords "Duplicate definition of module" ++ [prettyTCM x <> text "."] ++ pwords "Previous definition of" ++ [help m] ++ pwords "module" ++ [prettyTCM x] ++ pwords "at" ++ [prettyTCM r] where help m = caseMaybeM (isDatatypeModule m) empty $ \case IsData -> text "(datatype)" IsRecord -> text "(record)" ModuleArityMismatch m EmptyTel args -> fsep $ pwords "The module" ++ [prettyTCM m] ++ pwords "is not parameterized, but is being applied to arguments" ModuleArityMismatch m tel@(ExtendTel _ _) args -> fsep $ pwords "The arguments to " ++ [prettyTCM m] ++ pwords "do not fit the telescope" ++ [prettyTCM tel] ShouldBeEmpty t [] -> fsep $ [prettyTCM t] ++ pwords "should be empty, but that's not obvious to me" ShouldBeEmpty t ps -> fsep ( [prettyTCM t] ++ pwords "should be empty, but the following constructor patterns are valid:" ) $$ nest 2 (vcat $ map (prettyPat 0) ps) ShouldBeASort t -> fsep $ [prettyTCM t] ++ pwords "should be a sort, but it isn't" ShouldBePi t -> fsep $ [prettyTCM t] ++ pwords "should be a function type, but it isn't" NotAProperTerm -> fwords "Found a malformed term" SetOmegaNotValidType -> fwords "Setω is not a valid type" InvalidTypeSort s -> fsep $ [prettyTCM s] ++ pwords "is not a valid type" InvalidType v -> fsep $ [prettyTCM v] ++ pwords "is not a valid type" FunctionTypeInSizeUniv v -> fsep $ pwords "Functions may not return sizes, thus, function type " ++ [ prettyTCM v ] ++ pwords " is illegal" SplitOnIrrelevant p t -> fsep $ pwords "Cannot pattern match" ++ [prettyA p] ++ pwords "against" ++ [text $ verbalize $ getRelevance t] ++ pwords "argument of type" ++ [prettyTCM t] DefinitionIsIrrelevant x -> fsep $ text "Identifier" : prettyTCM x : pwords "is declared irrelevant, so it cannot be used here" VariableIsIrrelevant x -> fsep $ text "Variable" : prettyTCM x : pwords "is declared irrelevant, so it cannot be used here" UnequalBecauseOfUniverseConflict cmp s t -> fsep $ [prettyTCM s, notCmp cmp, prettyTCM t, text "because this would result in an invalid use of Setω" ] UnequalTerms cmp s t a -> do (d1, d2, d) <- prettyInEqual s t fsep $ [return d1, notCmp cmp, return d2] ++ pwords "of type" ++ [prettyTCM a] ++ [return d] -- UnequalLevel is UNUSED -- UnequalLevel cmp s t -> fsep $ -- [prettyTCM s, notCmp cmp, prettyTCM t] -- UnequalTelescopes is UNUSED -- UnequalTelescopes cmp a b -> fsep $ -- [prettyTCM a, notCmp cmp, prettyTCM b] UnequalTypes cmp a b -> prettyUnequal a (notCmp cmp) b -- fsep $ [prettyTCM a, notCmp cmp, prettyTCM b] UnequalRelevance cmp a b -> fsep $ [prettyTCM a, notCmp cmp, prettyTCM b] ++ -- Andreas 2010-09-21 to reveal Forced annotations, print also uglily -- [text $ show a, notCmp cmp, text $ show b] ++ pwords "because one is a relevant function type and the other is an irrelevant function type" UnequalHiding a b -> fsep $ [prettyTCM a, text "!=", prettyTCM b] ++ pwords "because one is an implicit function type and the other is an explicit function type" UnequalSorts s1 s2 -> fsep $ [prettyTCM s1, text "!=", prettyTCM s2] NotLeqSort s1 s2 -> fsep $ pwords "The type of the constructor does not fit in the sort of the datatype, since" ++ [prettyTCM s1] ++ pwords "is not less or equal than" ++ [prettyTCM s2] TooFewFields r xs -> fsep $ pwords "Missing fields" ++ punctuate comma (map pretty xs) ++ pwords "in an element of the record" ++ [prettyTCM r] TooManyFields r xs -> fsep $ pwords "The record type" ++ [prettyTCM r] ++ pwords "does not have the fields" ++ punctuate comma (map pretty xs) DuplicateConstructors xs -> fsep $ pwords "Duplicate constructors" ++ punctuate comma (map pretty xs) ++ pwords "in datatype" DuplicateFields xs -> fsep $ pwords "Duplicate fields" ++ punctuate comma (map pretty xs) ++ pwords "in record" WithOnFreeVariable e v -> do de <- prettyA e dv <- prettyTCM v if show de == show dv then fsep $ pwords "Cannot `with` on variable" ++ [return dv] ++ pwords " bound in a module telescope (or patterns of a parent clause)" else fsep $ pwords "Cannot `with` on expression" ++ [return de] ++ pwords "which reduces to variable" ++ [return dv] ++ pwords " bound in a module telescope (or patterns of a parent clause)" UnexpectedWithPatterns ps -> fsep $ pwords "Unexpected with patterns" ++ (punctuate (text " |") $ map prettyA ps) WithClausePatternMismatch p q -> fsep $ pwords "With clause pattern " ++ [prettyA p] ++ pwords " is not an instance of its parent pattern " ++ [P.fsep <$> prettyTCMPatterns [q]] MetaCannotDependOn m ps i -> fsep $ pwords "The metavariable" ++ [prettyTCM $ MetaV m []] ++ pwords "cannot depend on" ++ [pvar i] ++ pwords "because it" ++ deps where pvar = prettyTCM . I.var deps = case map pvar ps of [] -> pwords "does not depend on any variables" [x] -> pwords "only depends on the variable" ++ [x] xs -> pwords "only depends on the variables" ++ punctuate comma xs MetaOccursInItself m -> fsep $ pwords "Cannot construct infinite solution of metavariable" ++ [prettyTCM $ MetaV m []] BuiltinMustBeConstructor s e -> fsep $ [prettyA e] ++ pwords "must be a constructor in the binding to builtin" ++ [text s] NoSuchBuiltinName s -> fsep $ pwords "There is no built-in thing called" ++ [text s] DuplicateBuiltinBinding b x y -> fsep $ pwords "Duplicate binding for built-in thing" ++ [text b <> comma] ++ pwords "previous binding to" ++ [prettyTCM x] NoBindingForBuiltin x | elem x [builtinZero, builtinSuc] -> fsep $ pwords "No binding for builtin " ++ [text x <> comma] ++ pwords ("use {-# BUILTIN " ++ builtinNat ++ " name #-} to bind builtin natural " ++ "numbers to the type 'name'") | otherwise -> fsep $ pwords "No binding for builtin thing" ++ [text x <> comma] ++ pwords ("use {-# BUILTIN " ++ x ++ " name #-} to bind it to 'name'") NoSuchPrimitiveFunction x -> fsep $ pwords "There is no primitive function called" ++ [text x] BuiltinInParameterisedModule x -> fwords $ "The BUILTIN pragma cannot appear inside a bound context " ++ "(for instance, in a parameterised module or as a local declaration)" IllegalLetInTelescope tb -> fsep $ -- pwords "The binding" ++ [pretty tb] ++ pwords " is not allowed in a telescope here." NoRHSRequiresAbsurdPattern ps -> fwords $ "The right-hand side can only be omitted if there " ++ "is an absurd pattern, () or {}, in the left-hand side." AbsurdPatternRequiresNoRHS ps -> fwords $ "The right-hand side must be omitted if there " ++ "is an absurd pattern, () or {}, in the left-hand side." LocalVsImportedModuleClash m -> fsep $ pwords "The module" ++ [prettyTCM m] ++ pwords "can refer to either a local module or an imported module" SolvedButOpenHoles -> fsep $ pwords "Module cannot be imported since it has open interaction points" ++ pwords "(consider adding {-# OPTIONS --allow-unsolved-metas #-} to this module)" CyclicModuleDependency ms -> fsep (pwords "cyclic module dependency:") $$ nest 2 (vcat $ map pretty ms) FileNotFound x files -> fsep ( pwords "Failed to find source of module" ++ [pretty x] ++ pwords "in any of the following locations:" ) $$ nest 2 (vcat $ map (text . filePath) files) OverlappingProjects f m1 m2 -> fsep ( pwords "The file" ++ [text (filePath f)] ++ pwords "can be accessed via several project roots. Both" ++ [pretty m1] ++ pwords "and" ++ [pretty m2] ++ pwords "point to this file." ) AmbiguousTopLevelModuleName x files -> fsep ( pwords "Ambiguous module name. The module name" ++ [pretty x] ++ pwords "could refer to any of the following files:" ) $$ nest 2 (vcat $ map (text . filePath) files) ClashingFileNamesFor x files -> fsep ( pwords "Multiple possible sources for module" ++ [prettyTCM x] ++ pwords "found:" ) $$ nest 2 (vcat $ map (text . filePath) files) ModuleDefinedInOtherFile mod file file' -> fsep $ pwords "You tried to load" ++ [text (filePath file)] ++ pwords "which defines the module" ++ [pretty mod <> text "."] ++ pwords "However, according to the include path this module should" ++ pwords "be defined in" ++ [text (filePath file') <> text "."] ModuleNameUnexpected given expected -> fsep $ pwords "The name of the top level module does not match the file name. The module" ++ [ pretty given ] ++ pwords "should probably be named" ++ [ pretty expected ] ModuleNameDoesntMatchFileName given files -> fsep (pwords "The name of the top level module does not match the file name. The module" ++ [ pretty given ] ++ pwords "should be defined in one of the following files:") $$ nest 2 (vcat $ map (text . filePath) files) BothWithAndRHS -> fsep $ pwords "Unexpected right hand side" AbstractConstructorNotInScope q -> fsep $ [ text "Constructor" , prettyTCM q ] ++ pwords "is abstract, thus, not in scope here" NotInScope xs -> do inscope <- Set.toList . concreteNamesInScope <$> getScope fsep (pwords "Not in scope:") $$ nest 2 (vcat $ map (name inscope) xs) where name inscope x = fsep [ pretty x , text "at" <+> prettyTCM (getRange x) , suggestion inscope x ] suggestion inscope x = nest 2 $ par $ [ text "did you forget space around the ':'?" | elem ':' s ] ++ [ text "did you forget space around the '->'?" | isInfixOf "->" s ] ++ [ sep [ text "did you mean" , nest 2 $ vcat (punctuate (text " or") $ map (\ y -> text $ "'" ++ y ++ "'") ys) <> text "?" ] | not $ null ys ] where s = P.prettyShow x par [] = empty par [d] = parens d par ds = parens $ vcat ds strip x = map toLower $ filter (/= '_') $ P.prettyShow $ C.unqualify x maxDist n = div n 3 close a b = editDistance a b <= maxDist (length a) ys = map P.prettyShow $ filter (close (strip x) . strip) inscope NoSuchModule x -> fsep $ pwords "No such module" ++ [pretty x] AmbiguousName x ys -> vcat [ fsep $ pwords "Ambiguous name" ++ [pretty x <> text "."] ++ pwords "It could refer to any one of" , nest 2 $ vcat $ map nameWithBinding ys , fwords "(hint: Use C-c C-w (in Emacs) if you want to know why)" ] AmbiguousModule x ys -> vcat [ fsep $ pwords "Ambiguous module name" ++ [pretty x <> text "."] ++ pwords "It could refer to any one of" , nest 2 $ vcat $ map help ys , fwords "(hint: Use C-c C-w (in Emacs) if you want to know why)" ] where help :: ModuleName -> TCM Doc help m = do anno <- caseMaybeM (isDatatypeModule m) (return empty) $ \case IsData -> return $ text "(datatype module)" IsRecord -> return $ text "(record module)" sep [prettyTCM m, anno ] UninstantiatedModule x -> fsep ( pwords "Cannot access the contents of the parameterised module" ++ [pretty x <> text "."] ++ pwords "To do this the module first has to be instantiated. For instance:" ) $$ nest 2 (hsep [ text "module", pretty x <> text "'", text "=", pretty x, text "e1 .. en" ]) ClashingDefinition x y -> fsep $ pwords "Multiple definitions of" ++ [pretty x <> text "."] ++ pwords "Previous definition at" ++ [prettyTCM $ nameBindingSite $ qnameName y] ClashingModule m1 m2 -> fsep $ pwords "The modules" ++ [prettyTCM m1, text "and", prettyTCM m2] ++ pwords "clash." ClashingImport x y -> fsep $ pwords "Import clash between" ++ [pretty x, text "and", prettyTCM y] ClashingModuleImport x y -> fsep $ pwords "Module import clash between" ++ [pretty x, text "and", prettyTCM y] PatternShadowsConstructor x c -> fsep $ pwords "The pattern variable" ++ [prettyTCM x] ++ pwords "has the same name as the constructor" ++ [prettyTCM c] DuplicateImports m xs -> fsep $ pwords "Ambiguous imports from module" ++ [pretty m] ++ pwords "for" ++ punctuate comma (map pretty xs) ModuleDoesntExport m xs -> fsep $ pwords "The module" ++ [pretty m] ++ pwords "doesn't export the following:" ++ punctuate comma (map pretty xs) NotAModuleExpr e -> fsep $ pwords "The right-hand side of a module definition must have the form 'M e1 .. en'" ++ pwords "where M is a module name. The expression" ++ [pretty e, text "doesn't."] FieldOutsideRecord -> fsep $ pwords "Field appearing outside record declaration." InvalidPattern p -> fsep $ pretty p : pwords "is not a valid pattern" RepeatedVariablesInPattern xs -> fsep $ pwords "Repeated variables in pattern:" ++ map pretty xs NotAnExpression e -> fsep $ [pretty e] ++ pwords "is not a valid expression." NotAValidLetBinding nd -> fwords $ "Not a valid let-declaration" NotValidBeforeField nd -> fwords $ "This declaration is illegal in a record before the last field" NothingAppliedToHiddenArg e -> fsep $ [pretty e] ++ pwords "cannot appear by itself. It needs to be the argument to" ++ pwords "a function expecting an implicit argument." NothingAppliedToInstanceArg e -> fsep $ [pretty e] ++ pwords "cannot appear by itself. It needs to be the argument to" ++ pwords "a function expecting an instance argument." NoParseForApplication es -> fsep ( pwords "Could not parse the application" ++ [pretty $ C.RawApp noRange es]) AmbiguousParseForApplication es es' -> fsep ( pwords "Don't know how to parse" ++ [pretty_es <> (text ".")] ++ pwords "Could mean any one of:" ) $$ nest 2 (vcat $ map pretty' es') where pretty_es :: TCM Doc pretty_es = pretty $ C.RawApp noRange es pretty' :: C.Expr -> TCM Doc pretty' e = do p1 <- pretty_es p2 <- pretty e if show p1 == show p2 then unambiguous e else pretty e unambiguous :: C.Expr -> TCM Doc unambiguous e@(C.OpApp r op _ xs) | all (isOrdinary . namedArg) xs = pretty $ foldl (C.App r) (C.Ident op) $ (map . fmap . fmap) fromOrdinary xs | any (isPlaceholder . namedArg) xs = pretty e <+> text "(section)" unambiguous e = pretty e isOrdinary :: MaybePlaceholder (C.OpApp e) -> Bool isOrdinary (NoPlaceholder _ (C.Ordinary _)) = True isOrdinary _ = False fromOrdinary :: MaybePlaceholder (C.OpApp e) -> e fromOrdinary (NoPlaceholder _ (C.Ordinary e)) = e fromOrdinary _ = __IMPOSSIBLE__ isPlaceholder :: MaybePlaceholder a -> Bool isPlaceholder Placeholder{} = True isPlaceholder NoPlaceholder{} = False BadArgumentsToPatternSynonym x -> fsep $ pwords "Bad arguments to pattern synonym " ++ [prettyTCM x] TooFewArgumentsToPatternSynonym x -> fsep $ pwords "Too few arguments to pattern synonym " ++ [prettyTCM x] UnusedVariableInPatternSynonym -> fsep $ pwords "Unused variable in pattern synonym." NoParseForLHS IsLHS p -> fsep ( pwords "Could not parse the left-hand side" ++ [pretty p]) NoParseForLHS IsPatSyn p -> fsep ( pwords "Could not parse the pattern synonym" ++ [pretty p]) {- UNUSED NoParseForPatternSynonym p -> fsep $ pwords "Could not parse the pattern synonym" ++ [pretty p] -} AmbiguousParseForLHS lhsOrPatSyn p ps -> fsep ( pwords "Don't know how to parse" ++ [pretty_p <> text "."] ++ pwords "Could mean any one of:" ) $$ nest 2 (vcat $ map pretty' ps) where pretty_p :: TCM Doc pretty_p = pretty p pretty' :: C.Pattern -> TCM Doc pretty' p' = do p1 <- pretty_p p2 <- pretty p' pretty $ if show p1 == show p2 then unambiguousP p' else p' -- the entire pattern is shown, not just the ambiguous part, -- so we need to dig in order to find the OpAppP's. unambiguousP :: C.Pattern -> C.Pattern unambiguousP (C.AppP x y) = C.AppP (unambiguousP x) $ (fmap.fmap) unambiguousP y unambiguousP (C.HiddenP r x) = C.HiddenP r $ fmap unambiguousP x unambiguousP (C.InstanceP r x) = C.InstanceP r $ fmap unambiguousP x unambiguousP (C.ParenP r x) = C.ParenP r $ unambiguousP x unambiguousP (C.AsP r n x) = C.AsP r n $ unambiguousP x unambiguousP (C.OpAppP r op _ xs) = foldl C.AppP (C.IdentP op) xs unambiguousP e = e OperatorInformation sects err -> prettyTCM err $+$ fsep (pwords "Operators used in the grammar:") $$ nest 2 (if null sects then text "None" else vcat (map text $ lines $ Boxes.render $ (\(col1, col2, col3) -> Boxes.hsep 1 Boxes.top $ map (Boxes.vcat Boxes.left) [col1, col2, col3]) $ unzip3 $ map prettySect $ sortBy (compare `on` show . notaName . sectNotation) $ filter (not . closedWithoutHoles) sects)) where trimLeft = dropWhile isNormalHole trimRight = reverse . dropWhile isNormalHole . reverse closedWithoutHoles sect = sectKind sect == NonfixNotation && null [ () | NormalHole {} <- trimLeft $ trimRight $ notation (sectNotation sect) ] prettyName n = Boxes.text $ P.render (P.pretty n) ++ " (" ++ P.render (P.pretty (nameBindingSite n)) ++ ")" prettySect sect = ( Boxes.text (P.render (P.pretty section)) Boxes.// strut , Boxes.text ("(" ++ kind ++ " " ++ (if notaIsOperator nota then "operator" else "notation") ++ (if sectIsSection sect then " section" else "") ++ (case sectLevel sect of Nothing -> "" Just Unrelated -> ", unrelated" Just (Related n) -> ", level " ++ show n) ++ ")") Boxes.// strut , Boxes.text "[" Boxes.<> Boxes.vcat Boxes.left (map (\n -> prettyName n Boxes.<> Boxes.text ",") names ++ [prettyName name Boxes.<> Boxes.text "]"]) ) where nota = sectNotation sect section = qualifyFirstIdPart (foldr (\x s -> C.nameToRawName x ++ "." ++ s) "" (init (C.qnameParts (notaName nota)))) (trim (notation nota)) qualifyFirstIdPart _ [] = [] qualifyFirstIdPart q (IdPart x : ps) = IdPart (q ++ x) : ps qualifyFirstIdPart q (p : ps) = p : qualifyFirstIdPart q ps trim = case sectKind sect of InfixNotation -> trimLeft . trimRight PrefixNotation -> trimRight PostfixNotation -> trimLeft NonfixNotation -> id NoNotation -> __IMPOSSIBLE__ (names, name) = case Set.toList $ notaNames nota of [] -> __IMPOSSIBLE__ ns -> (init ns, last ns) strut = Boxes.emptyBox (length names) 0 kind = case sectKind sect of PrefixNotation -> "prefix" PostfixNotation -> "postfix" NonfixNotation -> "closed" NoNotation -> __IMPOSSIBLE__ InfixNotation -> case fixityAssoc $ notaFixity nota of NonAssoc -> "infix" LeftAssoc -> "infixl" RightAssoc -> "infixr" {- UNUSED AmbiguousParseForPatternSynonym p ps -> fsep ( pwords "Don't know how to parse" ++ [pretty p <> text "."] ++ pwords "Could mean any one of:" ) $$ nest 2 (vcat $ map pretty ps) -} {- UNUSED IncompletePatternMatching v args -> fsep $ pwords "Incomplete pattern matching for" ++ [prettyTCM v <> text "."] ++ pwords "No match for" ++ map prettyTCM args -} SplitError e -> prettyTCM e ImpossibleConstructor c neg -> fsep $ pwords "The case for the constructor " ++ [prettyTCM c] ++ pwords " is impossible" ++ [prettyTCM neg] ++ pwords "Possible solution: remove the clause, or use an absurd pattern ()." TooManyPolarities x n -> fsep $ pwords "Too many polarities given in the POLARITY pragma for" ++ [prettyTCM x] ++ pwords "(at most" ++ [text (show n)] ++ pwords "allowed)." IFSNoCandidateInScope t -> fsep $ pwords "No instance of type" ++ [prettyTCM t] ++ pwords "was found in scope." UnquoteFailed e -> case e of BadVisibility msg arg -> fsep $ pwords $ "Unable to unquote the argument. It should be `" ++ msg ++ "'." ConInsteadOfDef x def con -> fsep $ pwords ("Use " ++ con ++ " instead of " ++ def ++ " for constructor") ++ [prettyTCM x] DefInsteadOfCon x def con -> fsep $ pwords ("Use " ++ def ++ " instead of " ++ con ++ " for non-constructor") ++ [prettyTCM x] NonCanonical kind t -> fwords ("Cannot unquote non-canonical " ++ kind) $$ nest 2 (prettyTCM t) BlockedOnMeta _ m -> fsep $ pwords $ "Unquote failed because of unsolved meta variables." UnquotePanic err -> __IMPOSSIBLE__ DeBruijnIndexOutOfScope i EmptyTel [] -> fsep $ pwords $ "de Bruijn index " ++ show i ++ " is not in scope in the empty context" DeBruijnIndexOutOfScope i cxt names -> sep [ text ("de Bruijn index " ++ show i ++ " is not in scope in the context") , inTopContext $ addContext "_" $ prettyTCM cxt' ] where cxt' = cxt `abstract` raise (size cxt) (nameCxt names) nameCxt [] = EmptyTel nameCxt (x : xs) = ExtendTel (defaultDom (El I.Prop $ I.var 0)) $ NoAbs (P.prettyShow x) $ nameCxt xs NeedOptionCopatterns -> fsep $ pwords "Option --copatterns needed to enable destructor patterns" NeedOptionRewriting -> fsep $ pwords "Option --rewriting needed to add and use rewrite rules" NonFatalErrors ws -> foldr1 ($$) $ fmap prettyTCM ws InstanceSearchDepthExhausted c a d -> fsep $ pwords ("Instance search depth exhausted (max depth: " ++ show d ++ ") for candidate") ++ [hang (prettyTCM c <+> text ":") 2 (prettyTCM a)] where mpar n args | n > 0 && not (null args) = parens | otherwise = id prettyArg :: Arg (I.Pattern' a) -> TCM Doc prettyArg (Arg info x) = case getHiding info of Hidden -> braces $ prettyPat 0 x Instance{} -> dbraces $ prettyPat 0 x NotHidden -> prettyPat 1 x prettyPat :: Integer -> (I.Pattern' a) -> TCM Doc prettyPat _ (I.VarP _) = text "_" prettyPat _ (I.DotP _) = text "._" prettyPat _ (I.AbsurdP _) = text absurdPatternName prettyPat n (I.ConP c _ args) = mpar n args $ prettyTCM c <+> fsep (map (prettyArg . fmap namedThing) args) prettyPat _ (I.LitP l) = prettyTCM l prettyPat _ (I.ProjP _ p) = text "." <> prettyTCM p notCmp :: Comparison -> TCM Doc notCmp cmp = text "!" <> prettyTCM cmp -- | Print two terms that are supposedly unequal. -- If they print to the same identifier, add some explanation -- why they are different nevertheless. prettyInEqual :: Term -> Term -> TCM (Doc, Doc, Doc) prettyInEqual t1 t2 = do d1 <- prettyTCM t1 d2 <- prettyTCM t2 (d1, d2,) <$> do -- if printed differently, no extra explanation needed if P.render d1 /= P.render d2 then empty else do (v1, v2) <- instantiate (t1, t2) case (ignoreSharing v1, ignoreSharing v2) of (I.Var i1 _, I.Var i2 _) | i1 == i2 -> generic -- possible, see issue 1826 | otherwise -> varVar i1 i2 (I.Def{}, I.Con{}) -> __IMPOSSIBLE__ -- ambiguous identifiers (I.Con{}, I.Def{}) -> __IMPOSSIBLE__ (I.Var{}, I.Def{}) -> varDef (I.Def{}, I.Var{}) -> varDef (I.Var{}, I.Con{}) -> varCon (I.Con{}, I.Var{}) -> varCon _ -> empty where varDef, varCon, generic :: TCM Doc varDef = parens $ fwords "because one is a variable and one a defined identifier" varCon = parens $ fwords "because one is a variable and one a constructor" generic = parens $ fwords $ "although these terms are looking the same, " ++ "they contain different but identically rendered identifiers somewhere" varVar :: Int -> Int -> TCM Doc varVar i j = parens $ fwords $ "because one has de Bruijn index " ++ show i ++ " and the other " ++ show j class PrettyUnequal a where prettyUnequal :: a -> TCM Doc -> a -> TCM Doc instance PrettyUnequal Term where prettyUnequal t1 ncmp t2 = do (d1, d2, d) <- prettyInEqual t1 t2 fsep $ return d1 : ncmp : return d2 : return d : [] instance PrettyUnequal Type where prettyUnequal t1 ncmp t2 = prettyUnequal (unEl t1) ncmp (unEl t2) instance PrettyTCM SplitError where prettyTCM err = case err of NotADatatype t -> enterClosure t $ \ t -> fsep $ pwords "Cannot split on argument of non-datatype" ++ [prettyTCM t] IrrelevantDatatype t -> enterClosure t $ \ t -> fsep $ pwords "Cannot split on argument of irrelevant datatype" ++ [prettyTCM t] CoinductiveDatatype t -> enterClosure t $ \ t -> fsep $ pwords "Cannot pattern match on the coinductive type" ++ [prettyTCM t] {- UNUSED NoRecordConstructor t -> fsep $ pwords "Cannot pattern match on record" ++ [prettyTCM t] ++ pwords "because it has no constructor" -} UnificationStuck c tel cIxs gIxs errs | length cIxs /= length gIxs -> __IMPOSSIBLE__ | otherwise -> vcat $ [ fsep $ pwords "I'm not sure if there should be a case for the constructor" ++ [prettyTCM c <> text ","] ++ pwords "because I get stuck when trying to solve the following" ++ pwords "unification problems (inferred index ≟ expected index):" ] ++ zipWith (\c g -> nest 2 $ addContext tel $ prettyTCM c <+> text "≟" <+> prettyTCM g) cIxs gIxs ++ if null errs then [] else [ fsep $ pwords "Possible" ++ pwords (singPlural errs "reason" "reasons") ++ pwords "why unification failed:" ] ++ map (nest 2 . prettyTCM) errs GenericSplitError s -> fsep $ pwords "Split failed:" ++ pwords s instance PrettyTCM NegativeUnification where prettyTCM err = case err of UnifyConflict tel u v -> addContext tel $ vcat $ [ fsep $ pwords "because unification ended with a conflicting equation " , nest 2 $ prettyTCM u <+> text "≟" <+> prettyTCM v ] UnifyCycle tel i u -> addContext tel $ vcat $ [ fsep $ pwords "because unification ended with a cyclic equation " , nest 2 $ prettyTCM (var i) <+> text "≟" <+> prettyTCM u ] instance PrettyTCM UnificationFailure where prettyTCM err = case err of UnifyIndicesNotVars tel a u v ixs -> addContext tel $ fsep $ pwords "Cannot apply injectivity to the equation" ++ [prettyTCM u] ++ pwords "=" ++ [prettyTCM v] ++ pwords "of type" ++ [prettyTCM a] ++ pwords "because I cannot generalize over the indices" ++ [prettyList (map prettyTCM ixs) <> text "."] UnifyRecursiveEq tel a i u -> addContext tel $ fsep $ pwords "Cannot solve variable " ++ [prettyTCM (var i)] ++ pwords " of type " ++ [prettyTCM a] ++ pwords " with solution " ++ [prettyTCM u] ++ pwords " because the variable occurs in the solution," ++ pwords " or in the type of one of the variables in the solution." UnifyReflexiveEq tel a u -> addContext tel $ fsep $ pwords "Cannot eliminate reflexive equation" ++ [prettyTCM u] ++ pwords "=" ++ [prettyTCM u] ++ pwords "of type" ++ [prettyTCM a] ++ pwords "because K has been disabled." instance PrettyTCM Call where prettyTCM c = case c of CheckClause t cl -> do reportSLn "error.checkclause" 60 $ "prettyTCM CheckClause: cl = " ++ show (deepUnscope cl) clc <- abstractToConcrete_ cl reportSLn "error.checkclause" 40 $ "cl (Concrete) = " ++ show clc fsep $ pwords "when checking that the clause" ++ [prettyA cl] ++ pwords "has type" ++ [prettyTCM t] CheckPattern p tel t -> addContext tel $ fsep $ pwords "when checking that the pattern" ++ [prettyA p] ++ pwords "has type" ++ [prettyTCM t] CheckLetBinding b -> fsep $ pwords "when checking the let binding" ++ [prettyA b] InferExpr e -> fsep $ pwords "when inferring the type of" ++ [prettyA e] CheckExprCall e t -> fsep $ pwords "when checking that the expression" ++ [prettyA e] ++ pwords "has type" ++ [prettyTCM t] IsTypeCall e s -> fsep $ pwords "when checking that the expression" ++ [prettyA e] ++ pwords "is a type of sort" ++ [prettyTCM s] IsType_ e -> fsep $ pwords "when checking that the expression" ++ [prettyA e] ++ pwords "is a type" CheckProjection _ x t -> fsep $ pwords "when checking the projection" ++ [ sep [ prettyTCM x <+> text ":" , nest 2 $ prettyTCM t ] ] CheckArguments r es t0 t1 -> fsep $ pwords "when checking that" ++ map hPretty es ++ pwords (singPlural es "is a valid argument" "are valid arguments") ++ pwords "to a function of type" ++ [prettyTCM t0] CheckRecDef _ x ps cs -> fsep $ pwords "when checking the definition of" ++ [prettyTCM x] CheckDataDef _ x ps cs -> fsep $ pwords "when checking the definition of" ++ [prettyTCM x] CheckConstructor d _ _ (A.Axiom _ _ _ _ c _) -> fsep $ pwords "when checking the constructor" ++ [prettyTCM c] ++ pwords "in the declaration of" ++ [prettyTCM d] CheckConstructor{} -> __IMPOSSIBLE__ CheckFunDef _ f _ -> fsep $ pwords "when checking the definition of" ++ [prettyTCM f] CheckPragma _ p -> fsep $ pwords "when checking the pragma" ++ [prettyA $ RangeAndPragma noRange p] CheckPrimitive _ x e -> fsep $ pwords "when checking that the type of the primitive function" ++ [prettyTCM x] ++ pwords "is" ++ [prettyA e] CheckWithFunctionType e -> fsep $ pwords "when checking that the type" ++ [prettyA e] ++ pwords "of the generated with function is well-formed" CheckDotPattern e v -> fsep $ pwords "when checking that the given dot pattern" ++ [prettyA e] ++ pwords "matches the inferred value" ++ [prettyTCM v] CheckPatternShadowing c -> fsep $ pwords "when checking the clause" ++ [prettyA c] InferVar x -> fsep $ pwords "when inferring the type of" ++ [prettyTCM x] InferDef x -> fsep $ pwords "when inferring the type of" ++ [prettyTCM x] CheckIsEmpty r t -> fsep $ pwords "when checking that" ++ [prettyTCM t] ++ pwords "has no constructors" ScopeCheckExpr e -> fsep $ pwords "when scope checking" ++ [pretty e] ScopeCheckDeclaration d -> fwords ("when scope checking the declaration" ++ suffix) $$ nest 2 (vcat $ map pretty ds) where ds = D.notSoNiceDeclarations d suffix = case ds of [_] -> "" _ -> "s" ScopeCheckLHS x p -> fsep $ pwords "when scope checking the left-hand side" ++ [pretty p] ++ pwords "in the definition of" ++ [pretty x] NoHighlighting -> empty SetRange r -> fsep (pwords "when doing something at") <+> prettyTCM r CheckSectionApplication _ m1 modapp -> fsep $ pwords "when checking the module application" ++ [prettyA $ A.Apply info m1 modapp initCopyInfo defaultImportDir] where info = A.ModuleInfo noRange noRange Nothing Nothing Nothing ModuleContents -> fsep $ pwords "when retrieving the contents of a module" where hPretty :: Arg (Named_ Expr) -> TCM Doc hPretty a = pretty =<< abstractToConcreteCtx (hiddenArgumentCtx (getHiding a)) a --------------------------------------------------------------------------- -- * Natural language --------------------------------------------------------------------------- class Verbalize a where verbalize :: a -> String instance Verbalize Hiding where verbalize h = case h of Hidden -> "hidden" NotHidden -> "visible" Instance{} -> "instance" instance Verbalize Relevance where verbalize r = case r of Relevant -> "relevant" Irrelevant -> "irrelevant" NonStrict -> "shape-irrelevant" Forced{} -> "forced" -- | Indefinite article. data Indefinite a = Indefinite a instance Verbalize a => Verbalize (Indefinite a) where verbalize (Indefinite a) = case verbalize a of "" -> "" w@(c:cs) | c `elem` ['a','e','i','o'] -> "an " ++ w | otherwise -> "a " ++ w -- Aarne Ranta would whip me if he saw this. singPlural :: Sized a => a -> c -> c -> c singPlural xs singular plural = if size xs == 1 then singular else plural Agda-2.5.3/src/full/Agda/TypeChecking/Unquote.hs0000644000000000000000000006056013154613124017510 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Unquote where import Control.Arrow ((&&&), (***), first, second) import Control.Applicative import Control.Monad.State (StateT(..), evalStateT, get, gets, put, modify) import Control.Monad.Reader (ReaderT(..), ask, asks) import Control.Monad.Writer (WriterT(..), execWriterT, runWriterT, tell) import Control.Monad.Trans (lift) import Control.Monad import Data.Char import Data.Maybe (fromMaybe) import Data.Traversable (traverse) import Data.Map (Map) import qualified Data.Map as Map import Agda.Syntax.Common import Agda.Syntax.Internal as I import qualified Agda.Syntax.Reflected as R import Agda.Syntax.Literal import Agda.Syntax.Position import Agda.Syntax.Fixity import Agda.Syntax.Info import Agda.Syntax.Translation.InternalToAbstract import Agda.Syntax.Translation.ReflectedToAbstract import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.DropArgs import Agda.TypeChecking.Free import Agda.TypeChecking.Level import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce import Agda.TypeChecking.Reduce.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Quote import Agda.TypeChecking.Conversion import Agda.TypeChecking.MetaVars import Agda.TypeChecking.EtaContract import Agda.TypeChecking.Primitive import {-# SOURCE #-} Agda.TypeChecking.Rules.Term import {-# SOURCE #-} Agda.TypeChecking.Rules.Def import Agda.Utils.Except ( mkExceptT , MonadError(catchError, throwError) , ExceptT , runExceptT ) import Agda.Utils.Either import Agda.Utils.FileName import Agda.Utils.Lens import Agda.Utils.Maybe import Agda.Utils.Maybe.Strict (toLazy) import Agda.Utils.Monad import Agda.Utils.Permutation ( Permutation(Perm), compactP ) import Agda.Utils.Pretty (prettyShow) import Agda.Utils.String ( Str(Str), unStr ) import Agda.Utils.VarSet (VarSet) import qualified Agda.Utils.VarSet as Set #include "undefined.h" import Agda.Utils.Impossible agdaTermType :: TCM Type agdaTermType = El (mkType 0) <$> primAgdaTerm agdaTypeType :: TCM Type agdaTypeType = agdaTermType qNameType :: TCM Type qNameType = El (mkType 0) <$> primQName data Dirty = Dirty | Clean deriving (Eq) -- Keep track of the original context. We need to use that when adding new -- definitions. Also state snapshot from last commit and whether the state is -- dirty (definitions have been added). type UnquoteState = (Dirty, TCState) type UnquoteM = ReaderT Context (StateT UnquoteState (WriterT [QName] (ExceptT UnquoteError TCM))) type UnquoteRes a = Either UnquoteError ((a, UnquoteState), [QName]) unpackUnquoteM :: UnquoteM a -> Context -> UnquoteState -> TCM (UnquoteRes a) unpackUnquoteM m cxt s = runExceptT $ runWriterT $ runStateT (runReaderT m cxt) s packUnquoteM :: (Context -> UnquoteState -> TCM (UnquoteRes a)) -> UnquoteM a packUnquoteM f = ReaderT $ \ cxt -> StateT $ \ s -> WriterT $ mkExceptT $ f cxt s runUnquoteM :: UnquoteM a -> TCM (Either UnquoteError (a, [QName])) runUnquoteM m = do cxt <- asks envContext s <- get z <- unpackUnquoteM m cxt (Clean, s) case z of Left err -> return $ Left err Right ((x, _), decls) -> Right (x, decls) <$ mapM_ isDefined decls where isDefined x = do def <- theDef <$> getConstInfo x case def of Function{funClauses = []} -> genericError $ "Missing definition for " ++ prettyShow x _ -> return () liftU :: TCM a -> UnquoteM a liftU = lift . lift . lift . lift liftU1 :: (TCM (UnquoteRes a) -> TCM (UnquoteRes b)) -> UnquoteM a -> UnquoteM b liftU1 f m = packUnquoteM $ \ cxt s -> f (unpackUnquoteM m cxt s) liftU2 :: (TCM (UnquoteRes a) -> TCM (UnquoteRes b) -> TCM (UnquoteRes c)) -> UnquoteM a -> UnquoteM b -> UnquoteM c liftU2 f m1 m2 = packUnquoteM $ \ cxt s -> f (unpackUnquoteM m1 cxt s) (unpackUnquoteM m2 cxt s) inOriginalContext :: UnquoteM a -> UnquoteM a inOriginalContext m = packUnquoteM $ \ cxt s -> modifyContext (const cxt) $ unpackUnquoteM m cxt s isCon :: ConHead -> TCM Term -> UnquoteM Bool isCon con tm = do t <- liftU tm case ignoreSharing t of Con con' _ _ -> return (con == con') _ -> return False isDef :: QName -> TCM Term -> UnquoteM Bool isDef f tm = do t <- liftU (etaContract =<< normalise =<< tm) case ignoreSharing t of Def g _ -> return (f == g) _ -> return False reduceQuotedTerm :: Term -> UnquoteM Term reduceQuotedTerm t = do b <- liftU $ ifBlocked t (\ m _ -> pure $ Left m) (\ t -> pure $ Right t) case b of Left m -> do s <- gets snd; throwError $ BlockedOnMeta s m Right t -> return t class Unquote a where unquote :: I.Term -> UnquoteM a unquoteN :: Unquote a => Arg Term -> UnquoteM a unquoteN a | visible a && isRelevant a = unquote $ unArg a unquoteN a = throwError $ BadVisibility "visible" a choice :: Monad m => [(m Bool, m a)] -> m a -> m a choice [] dflt = dflt choice ((mb, mx) : mxs) dflt = ifM mb mx $ choice mxs dflt ensureDef :: QName -> UnquoteM QName ensureDef x = do i <- liftU $ either (const Axiom) theDef <$> getConstInfo' x -- for recursive unquoteDecl case i of Constructor{} -> do def <- liftU $ prettyTCM =<< primAgdaTermDef con <- liftU $ prettyTCM =<< primAgdaTermCon throwError $ ConInsteadOfDef x (show def) (show con) _ -> return x ensureCon :: QName -> UnquoteM QName ensureCon x = do i <- liftU $ either (const Axiom) theDef <$> getConstInfo' x -- for recursive unquoteDecl case i of Constructor{} -> return x _ -> do def <- liftU $ prettyTCM =<< primAgdaTermDef con <- liftU $ prettyTCM =<< primAgdaTermCon throwError $ DefInsteadOfCon x (show def) (show con) pickName :: R.Type -> String pickName a = case a of R.Pi{} -> "f" R.Sort{} -> "A" R.Def d _ | c:_ <- show (qnameName d), isAlpha c -> [toLower c] _ -> "_" instance Unquote ArgInfo where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [h,r] -> do choice [(c `isCon` primArgArgInfo, ArgInfo <$> unquoteN h <*> unquoteN r <*> pure Reflected)] __IMPOSSIBLE__ Con c _ _ -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "arg info" t instance Unquote a => Unquote (Arg a) where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [info,x] -> do choice [(c `isCon` primArgArg, Arg <$> unquoteN info <*> unquoteN x)] __IMPOSSIBLE__ Con c _ _ -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "arg" t -- Andreas, 2013-10-20: currently, post-fix projections are not part of the -- quoted syntax. instance Unquote R.Elim where unquote t = R.Apply <$> unquote t instance Unquote Bool where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [] -> choice [ (c `isCon` primTrue, pure True) , (c `isCon` primFalse, pure False) ] __IMPOSSIBLE__ _ -> throwError $ NonCanonical "boolean" t instance Unquote Integer where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Lit (LitNat _ n) -> return n _ -> throwError $ NonCanonical "integer" t instance Unquote Double where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Lit (LitFloat _ x) -> return x _ -> throwError $ NonCanonical "float" t instance Unquote Char where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Lit (LitChar _ x) -> return x _ -> throwError $ NonCanonical "char" t instance Unquote Str where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Lit (LitString _ x) -> return (Str x) _ -> throwError $ NonCanonical "string" t unquoteString :: Term -> UnquoteM String unquoteString x = unStr <$> unquote x unquoteNString :: Arg Term -> UnquoteM String unquoteNString x = unStr <$> unquoteN x data ErrorPart = StrPart String | TermPart R.Term | NamePart QName instance PrettyTCM ErrorPart where prettyTCM (StrPart s) = text s prettyTCM (TermPart t) = prettyTCM t prettyTCM (NamePart x) = prettyTCM x instance Unquote ErrorPart where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [x] -> choice [ (c `isCon` primAgdaErrorPartString, StrPart <$> unquoteNString x) , (c `isCon` primAgdaErrorPartTerm, TermPart <$> unquoteN x) , (c `isCon` primAgdaErrorPartName, NamePart <$> unquoteN x) ] __IMPOSSIBLE__ _ -> throwError $ NonCanonical "error part" t instance Unquote a => Unquote [a] where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [x,xs] -> do choice [(c `isCon` primCons, (:) <$> unquoteN x <*> unquoteN xs)] __IMPOSSIBLE__ Con c _ [] -> do choice [(c `isCon` primNil, return [])] __IMPOSSIBLE__ Con c _ _ -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "list" t instance Unquote Hiding where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [] -> do choice [(c `isCon` primHidden, return Hidden) ,(c `isCon` primInstance, return (Instance NoOverlap)) ,(c `isCon` primVisible, return NotHidden)] __IMPOSSIBLE__ Con c _ vs -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "visibility" t instance Unquote Relevance where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [] -> do choice [(c `isCon` primRelevant, return Relevant) ,(c `isCon` primIrrelevant, return Irrelevant)] __IMPOSSIBLE__ Con c _ vs -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "relevance" t instance Unquote QName where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Lit (LitQName _ x) -> return x _ -> throwError $ NonCanonical "name" t instance Unquote a => Unquote (R.Abs a) where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [x,y] -> do choice [(c `isCon` primAbsAbs, R.Abs <$> (hint <$> unquoteNString x) <*> unquoteN y)] __IMPOSSIBLE__ Con c _ _ -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "abstraction" t where hint x | not (null x) = x | otherwise = "_" getCurrentPath :: TCM AbsolutePath getCurrentPath = fromMaybe __IMPOSSIBLE__ <$> asks envCurrentPath instance Unquote MetaId where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Lit (LitMeta r f x) -> liftU $ do live <- (f ==) <$> getCurrentPath unless live $ do m <- fromMaybe __IMPOSSIBLE__ <$> lookupModuleFromSource f typeError . GenericDocError =<< sep [ text "Can't unquote stale metavariable" , pretty m <> text "." <> pretty x ] return x _ -> throwError $ NonCanonical "meta variable" t instance Unquote a => Unquote (Dom a) where unquote t = domFromArg <$> unquote t instance Unquote R.Sort where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [] -> do choice [(c `isCon` primAgdaSortUnsupported, return R.UnknownS)] __IMPOSSIBLE__ Con c _ [u] -> do choice [(c `isCon` primAgdaSortSet, R.SetS <$> unquoteN u) ,(c `isCon` primAgdaSortLit, R.LitS <$> unquoteN u)] __IMPOSSIBLE__ Con c _ _ -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "sort" t instance Unquote Literal where unquote t = do t <- reduceQuotedTerm t let litMeta r x = do file <- liftU getCurrentPath return $ LitMeta r file x case ignoreSharing t of Con c _ [x] -> choice [ (c `isCon` primAgdaLitNat, LitNat noRange <$> unquoteN x) , (c `isCon` primAgdaLitFloat, LitFloat noRange <$> unquoteN x) , (c `isCon` primAgdaLitChar, LitChar noRange <$> unquoteN x) , (c `isCon` primAgdaLitString, LitString noRange <$> unquoteNString x) , (c `isCon` primAgdaLitQName, LitQName noRange <$> unquoteN x) , (c `isCon` primAgdaLitMeta, litMeta noRange =<< unquoteN x) ] __IMPOSSIBLE__ Con c _ _ -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "literal" t instance Unquote R.Term where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [] -> choice [ (c `isCon` primAgdaTermUnsupported, return R.Unknown) ] __IMPOSSIBLE__ Con c _ [x] -> do choice [ (c `isCon` primAgdaTermSort, R.Sort <$> unquoteN x) , (c `isCon` primAgdaTermLit, R.Lit <$> unquoteN x) ] __IMPOSSIBLE__ Con c _ [x, y] -> choice [ (c `isCon` primAgdaTermVar, R.Var <$> (fromInteger <$> unquoteN x) <*> unquoteN y) , (c `isCon` primAgdaTermCon, R.Con <$> (ensureCon =<< unquoteN x) <*> unquoteN y) , (c `isCon` primAgdaTermDef, R.Def <$> (ensureDef =<< unquoteN x) <*> unquoteN y) , (c `isCon` primAgdaTermMeta, R.Meta <$> unquoteN x <*> unquoteN y) , (c `isCon` primAgdaTermLam, R.Lam <$> unquoteN x <*> unquoteN y) , (c `isCon` primAgdaTermPi, mkPi <$> unquoteN x <*> unquoteN y) , (c `isCon` primAgdaTermExtLam, R.ExtLam <$> unquoteN x <*> unquoteN y) ] __IMPOSSIBLE__ where mkPi :: Dom R.Type -> R.Abs R.Type -> R.Term -- TODO: implement Free for reflected syntax so this works again --mkPi a (R.Abs "_" b) = R.Pi a (R.Abs x b) -- where x | 0 `freeIn` b = pickName (unDom a) -- | otherwise = "_" mkPi a (R.Abs "_" b) = R.Pi a (R.Abs (pickName (unDom a)) b) mkPi a b = R.Pi a b Con{} -> __IMPOSSIBLE__ Lit{} -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "term" t instance Unquote R.Pattern where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [] -> do choice [ (c `isCon` primAgdaPatAbsurd, return R.AbsurdP) , (c `isCon` primAgdaPatDot, return R.DotP) ] __IMPOSSIBLE__ Con c _ [x] -> do choice [ (c `isCon` primAgdaPatVar, R.VarP <$> unquoteNString x) , (c `isCon` primAgdaPatProj, R.ProjP <$> unquoteN x) , (c `isCon` primAgdaPatLit, R.LitP <$> unquoteN x) ] __IMPOSSIBLE__ Con c _ [x, y] -> do choice [ (c `isCon` primAgdaPatCon, R.ConP <$> unquoteN x <*> unquoteN y) ] __IMPOSSIBLE__ Con c _ _ -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "pattern" t instance Unquote R.Clause where unquote t = do t <- reduceQuotedTerm t case ignoreSharing t of Con c _ [x] -> do choice [ (c `isCon` primAgdaClauseAbsurd, R.AbsurdClause <$> unquoteN x) ] __IMPOSSIBLE__ Con c _ [x, y] -> do choice [ (c `isCon` primAgdaClauseClause, R.Clause <$> unquoteN x <*> unquoteN y) ] __IMPOSSIBLE__ Con c _ _ -> __IMPOSSIBLE__ _ -> throwError $ NonCanonical "clause" t -- Unquoting TCM computations --------------------------------------------- -- | Argument should be a term of type @Term → TCM A@ for some A. Returns the -- resulting term of type @A@. The second argument is the term for the hole, -- which will typically be a metavariable. This is passed to the computation -- (quoted). unquoteTCM :: I.Term -> I.Term -> UnquoteM I.Term unquoteTCM m hole = do qhole <- liftU $ quoteTerm hole evalTCM (m `apply` [defaultArg qhole]) evalTCM :: I.Term -> UnquoteM I.Term evalTCM v = do v <- reduceQuotedTerm v liftU $ reportSDoc "tc.unquote.eval" 90 $ text "evalTCM" <+> prettyTCM v let failEval = throwError $ NonCanonical "type checking computation" v case ignoreSharing v of I.Def f [] -> choice [ (f `isDef` primAgdaTCMGetContext, tcGetContext) , (f `isDef` primAgdaTCMCommit, tcCommit) ] failEval I.Def f [u] -> choice [ (f `isDef` primAgdaTCMInferType, tcFun1 tcInferType u) , (f `isDef` primAgdaTCMNormalise, tcFun1 tcNormalise u) , (f `isDef` primAgdaTCMReduce, tcFun1 tcReduce u) , (f `isDef` primAgdaTCMGetType, tcFun1 tcGetType u) , (f `isDef` primAgdaTCMGetDefinition, tcFun1 tcGetDefinition u) , (f `isDef` primAgdaTCMIsMacro, tcFun1 tcIsMacro u) , (f `isDef` primAgdaTCMFreshName, tcFun1 tcFreshName u) ] failEval I.Def f [u, v] -> choice [ (f `isDef` primAgdaTCMUnify, tcFun2 tcUnify u v) , (f `isDef` primAgdaTCMCheckType, tcFun2 tcCheckType u v) , (f `isDef` primAgdaTCMDeclareDef, uqFun2 tcDeclareDef u v) , (f `isDef` primAgdaTCMDefineFun, uqFun2 tcDefineFun u v) ] failEval I.Def f [l, a, u] -> choice [ (f `isDef` primAgdaTCMReturn, return (unElim u)) , (f `isDef` primAgdaTCMTypeError, tcFun1 tcTypeError u) , (f `isDef` primAgdaTCMQuoteTerm, tcQuoteTerm (unElim u)) , (f `isDef` primAgdaTCMUnquoteTerm, tcFun1 (tcUnquoteTerm (mkT (unElim l) (unElim a))) u) , (f `isDef` primAgdaTCMBlockOnMeta, uqFun1 tcBlockOnMeta u) , (f `isDef` primAgdaTCMDebugPrint, tcFun3 tcDebugPrint l a u) ] failEval I.Def f [_, _, u, v] -> choice [ (f `isDef` primAgdaTCMCatchError, tcCatchError (unElim u) (unElim v)) , (f `isDef` primAgdaTCMWithNormalisation, tcWithNormalisation (unElim u) (unElim v)) , (f `isDef` primAgdaTCMExtendContext, tcExtendContext (unElim u) (unElim v)) , (f `isDef` primAgdaTCMInContext, tcInContext (unElim u) (unElim v)) ] failEval I.Def f [_, _, _, _, m, k] -> choice [ (f `isDef` primAgdaTCMBind, tcBind (unElim m) (unElim k)) ] failEval _ -> failEval where unElim = unArg . argFromElim tcBind m k = do v <- evalTCM m evalTCM (k `apply` [defaultArg v]) process :: (InstantiateFull a, Normalise a) => a -> TCM a process v = do norm <- view eUnquoteNormalise if norm then normalise v else instantiateFull v mkT l a = El s a where s = Type $ Max [Plus 0 $ UnreducedLevel l] -- Don't catch Unquote errors! tcCatchError :: Term -> Term -> UnquoteM Term tcCatchError m h = liftU2 (\ m1 m2 -> m1 `catchError` \ _ -> m2) (evalTCM m) (evalTCM h) tcWithNormalisation :: Term -> Term -> UnquoteM Term tcWithNormalisation b m = do v <- unquote b liftU1 (locally eUnquoteNormalise $ const v) (evalTCM m) uqFun1 :: Unquote a => (a -> UnquoteM b) -> Elim -> UnquoteM b uqFun1 fun a = do a <- unquote (unElim a) fun a tcFun1 :: Unquote a => (a -> TCM b) -> Elim -> UnquoteM b tcFun1 fun = uqFun1 (liftU . fun) uqFun2 :: (Unquote a, Unquote b) => (a -> b -> UnquoteM c) -> Elim -> Elim -> UnquoteM c uqFun2 fun a b = do a <- unquote (unElim a) b <- unquote (unElim b) fun a b uqFun3 :: (Unquote a, Unquote b, Unquote c) => (a -> b -> c -> UnquoteM d) -> Elim -> Elim -> Elim -> UnquoteM d uqFun3 fun a b c = do a <- unquote (unElim a) b <- unquote (unElim b) c <- unquote (unElim c) fun a b c tcFun2 :: (Unquote a, Unquote b) => (a -> b -> TCM c) -> Elim -> Elim -> UnquoteM c tcFun2 fun = uqFun2 (\ x y -> liftU (fun x y)) tcFun3 :: (Unquote a, Unquote b, Unquote c) => (a -> b -> c -> TCM d) -> Elim -> Elim -> Elim -> UnquoteM d tcFun3 fun = uqFun3 (\ x y z -> liftU (fun x y z)) tcFreshName :: Str -> TCM Term tcFreshName s = do m <- currentModule quoteName . qualify m <$> freshName_ (unStr s) tcUnify :: R.Term -> R.Term -> TCM Term tcUnify u v = do (u, a) <- inferExpr =<< toAbstract_ u v <- flip checkExpr a =<< toAbstract_ v equalTerm a u v primUnitUnit tcBlockOnMeta :: MetaId -> UnquoteM Term tcBlockOnMeta x = do s <- gets snd throwError (BlockedOnMeta s x) tcCommit :: UnquoteM Term tcCommit = do dirty <- gets fst when (dirty == Dirty) $ liftU $ typeError $ GenericError "Cannot use commitTC after declaring new definitions." s <- liftU get modify (second $ const s) liftU primUnitUnit tcTypeError :: [ErrorPart] -> TCM a tcTypeError err = typeError . GenericDocError =<< fsep (map prettyTCM err) tcDebugPrint :: Str -> Integer -> [ErrorPart] -> TCM Term tcDebugPrint (Str s) n msg = do reportSDoc s (fromIntegral n) $ fsep (map prettyTCM msg) primUnitUnit tcInferType :: R.Term -> TCM Term tcInferType v = do (_, a) <- inferExpr =<< toAbstract_ v quoteType =<< process a tcCheckType :: R.Term -> R.Type -> TCM Term tcCheckType v a = do a <- isType_ =<< toAbstract_ a e <- toAbstract_ v v <- checkExpr e a quoteTerm =<< process v tcQuoteTerm :: Term -> UnquoteM Term tcQuoteTerm v = liftU $ quoteTerm =<< process v tcUnquoteTerm :: Type -> R.Term -> TCM Term tcUnquoteTerm a v = do e <- toAbstract_ v v <- checkExpr e a return v tcNormalise :: R.Term -> TCM Term tcNormalise v = do (v, _) <- inferExpr =<< toAbstract_ v quoteTerm =<< normalise v tcReduce :: R.Term -> TCM Term tcReduce v = do (v, _) <- inferExpr =<< toAbstract_ v quoteTerm =<< reduce =<< instantiateFull v tcGetContext :: UnquoteM Term tcGetContext = liftU $ do as <- map (fmap snd) <$> getContext as <- etaContract =<< process as buildList <*> mapM quoteDom as extendCxt :: Arg R.Type -> UnquoteM a -> UnquoteM a extendCxt a m = do a <- liftU $ traverse (isType_ <=< toAbstract_) a liftU1 (addContext' (domFromArg a :: Dom Type)) m tcExtendContext :: Term -> Term -> UnquoteM Term tcExtendContext a m = do a <- unquote a extendCxt a (evalTCM m) tcInContext :: Term -> Term -> UnquoteM Term tcInContext c m = do c <- unquote c liftU1 inTopContext $ go c m where go :: [Arg R.Type] -> Term -> UnquoteM Term go [] m = evalTCM m go (a : as) m = extendCxt a $ go as m constInfo :: QName -> TCM Definition constInfo x = either err return =<< getConstInfo' x where err _ = genericError $ "Unbound name: " ++ prettyShow x tcGetType :: QName -> TCM Term tcGetType x = quoteType . defType =<< constInfo x tcIsMacro :: QName -> TCM Term tcIsMacro x = do true <- primTrue false <- primFalse let qBool True = true qBool False = false qBool . isMacro . theDef <$> constInfo x tcGetDefinition :: QName -> TCM Term tcGetDefinition x = quoteDefn =<< constInfo x setDirty :: UnquoteM () setDirty = modify (first $ const Dirty) tcDeclareDef :: Arg QName -> R.Type -> UnquoteM Term tcDeclareDef (Arg i x) a = inOriginalContext $ do setDirty let r = getRelevance i when (hidden i) $ liftU $ typeError . GenericDocError =<< text "Cannot declare hidden function" <+> prettyTCM x tell [x] liftU $ do reportSDoc "tc.unquote.decl" 10 $ sep [ text "declare" <+> prettyTCM x <+> text ":" , nest 2 $ prettyTCM a ] a <- isType_ =<< toAbstract_ a alreadyDefined <- isRight <$> getConstInfo' x when alreadyDefined $ genericError $ "Multiple declarations of " ++ prettyShow x addConstant x $ defaultDefn i x a emptyFunction when (isInstance i) $ addTypedInstance x a primUnitUnit tcDefineFun :: QName -> [R.Clause] -> UnquoteM Term tcDefineFun x cs = inOriginalContext $ (setDirty >>) $ liftU $ do whenM (isLeft <$> getConstInfo' x) $ genericError $ "Missing declaration for " ++ prettyShow x cs <- mapM (toAbstract_ . QNamed x) cs reportSDoc "tc.unquote.def" 10 $ vcat $ map prettyA cs let i = mkDefInfo (nameConcrete $ qnameName x) noFixity' PublicAccess ConcreteDef noRange checkFunDef NotDelayed i x cs primUnitUnit Agda-2.5.3/src/full/Agda/TypeChecking/Level.hs0000644000000000000000000001232113154613124017107 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Level where import Control.Applicative import Data.Maybe import qualified Data.List as List import Data.Traversable (Traversable,traverse) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Reduce import Agda.TypeChecking.Reduce.Monad () import Agda.TypeChecking.Monad.Builtin import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.Monad ( tryMaybe ) #include "undefined.h" import Agda.Utils.Impossible data LevelKit = LevelKit { lvlType :: Term , lvlSuc :: Term -> Term , lvlMax :: Term -> Term -> Term , lvlZero :: Term , typeName :: QName , sucName :: QName , maxName :: QName , zeroName :: QName } -- | Get the 'primLevel' as a 'Type'. levelType :: TCM Type levelType = El (mkType 0) <$> primLevel levelSucFunction :: TCM (Term -> Term) levelSucFunction = apply1 <$> primLevelSuc {-# SPECIALIZE builtinLevelKit :: TCM LevelKit #-} {-# SPECIALIZE builtinLevelKit :: ReduceM LevelKit #-} builtinLevelKit :: (HasBuiltins m) => m LevelKit builtinLevelKit = do level@(Def l []) <- ignoreSharing . fromMaybe __IMPOSSIBLE__ <$> getBuiltin' builtinLevel zero@(Def z []) <- ignoreSharing . fromMaybe __IMPOSSIBLE__ <$> getBuiltin' builtinLevelZero suc@(Def s []) <- ignoreSharing . fromMaybe __IMPOSSIBLE__ <$> getBuiltin' builtinLevelSuc max@(Def m []) <- ignoreSharing . fromMaybe __IMPOSSIBLE__ <$> getBuiltin' builtinLevelMax return $ LevelKit { lvlType = level , lvlSuc = \ a -> suc `apply1` a , lvlMax = \ a b -> max `applys` [a, b] , lvlZero = zero , typeName = l , sucName = s , maxName = m , zeroName = z } -- | Raises an error if no level kit is available. requireLevels :: TCM LevelKit requireLevels = builtinLevelKit {-# SPECIALIZE unLevel :: Term -> TCM Term #-} {-# SPECIALIZE unLevel :: Term -> ReduceM Term #-} unLevel :: (HasBuiltins m) => Term -> m Term unLevel (Level l) = reallyUnLevelView l unLevel (Shared p) = unLevel (derefPtr p) unLevel v = return v {-# SPECIALIZE reallyUnLevelView :: Level -> TCM Term #-} {-# SPECIALIZE reallyUnLevelView :: Level -> ReduceM Term #-} reallyUnLevelView :: (HasBuiltins m) => Level -> m Term reallyUnLevelView nv = do suc <- fromMaybe __IMPOSSIBLE__ <$> getBuiltin' builtinLevelSuc zer <- fromMaybe __IMPOSSIBLE__ <$> getBuiltin' builtinLevelZero case nv of Max [] -> return zer Max [Plus 0 a] -> return $ unLevelAtom a Max [a] -> do return $ unPlusV zer (apply1 suc) a _ -> (`unlevelWithKit` nv) <$> builtinLevelKit unlevelWithKit :: LevelKit -> Level -> Term unlevelWithKit LevelKit{ lvlZero = zer, lvlSuc = suc, lvlMax = max } (Max as) = case map (unPlusV zer suc) as of [a] -> a [] -> zer as -> foldr1 max as unPlusV :: Term -> (Term -> Term) -> PlusLevel -> Term unPlusV zer suc (ClosedLevel n) = foldr (.) id (List.genericReplicate n suc) zer unPlusV _ suc (Plus n a) = foldr (.) id (List.genericReplicate n suc) (unLevelAtom a) maybePrimCon :: TCM Term -> TCM (Maybe ConHead) maybePrimCon prim = tryMaybe $ do Con c ci [] <- prim return c maybePrimDef :: TCM Term -> TCM (Maybe QName) maybePrimDef prim = tryMaybe $ do Def f [] <- prim return f levelView :: Term -> TCM Level levelView a = do reportSLn "tc.level.view" 50 $ "{ levelView " ++ show a v <- runReduceM $ levelView' a reportSLn "tc.level.view" 50 $ " view: " ++ show v ++ "}" return v levelView' :: Term -> ReduceM Level levelView' a = do Def lzero [] <- ignoreSharing . fromMaybe __IMPOSSIBLE__ <$> getBuiltin' builtinLevelZero Def lsuc [] <- ignoreSharing . fromMaybe __IMPOSSIBLE__ <$> getBuiltin' builtinLevelSuc Def lmax [] <- ignoreSharing . fromMaybe __IMPOSSIBLE__ <$> getBuiltin' builtinLevelMax let view a = do a <- reduce' a case ignoreSharing a of Level l -> return l Def s [Apply arg] | s == lsuc -> inc <$> view (unArg arg) Def z [] | z == lzero -> return $ closed 0 Def m [Apply arg1, Apply arg2] | m == lmax -> levelLub <$> view (unArg arg1) <*> view (unArg arg2) _ -> mkAtom a v <- view a return v where mkAtom a = do b <- reduceB' a return $ case ignoreSharing <$> b of NotBlocked _ (MetaV m as) -> atom $ MetaLevel m as NotBlocked r _ -> atom $ NeutralLevel r $ ignoreBlocking b Blocked m _ -> atom $ BlockedLevel m $ ignoreBlocking b atom a = Max [Plus 0 a] closed n = Max [ClosedLevel n | n > 0] inc (Max as) = Max $ map inc' as where inc' (ClosedLevel n) = ClosedLevel $ n + 1 inc' (Plus n a) = Plus (n + 1) a levelLub :: Level -> Level -> Level levelLub (Max as) (Max bs) = levelMax $ as ++ bs subLevel :: Integer -> Level -> Maybe Level subLevel n (Max ls) = Max <$> traverse sub ls where sub :: PlusLevel -> Maybe PlusLevel sub (ClosedLevel j) | j >= n = Just $ ClosedLevel $ j - n | otherwise = Nothing sub (Plus j l) | j >= n = Just $ Plus (j - n) l | otherwise = Nothing Agda-2.5.3/src/full/Agda/TypeChecking/Pretty.hs-boot0000644000000000000000000000142013154613124020266 0ustar0000000000000000{-# LANGUAGE FlexibleInstances #-} module Agda.TypeChecking.Pretty where -- import Agda.Syntax.Common import Agda.Syntax.Internal -- import Agda.Syntax.Literal import Agda.TypeChecking.Monad.Base import Agda.Utils.Pretty (Doc) -- import qualified Agda.Utils.Pretty as P text :: String -> TCM Doc sep, fsep, hsep, vcat :: [TCM Doc] -> TCM Doc ($$), (<>), (<+>) :: TCM Doc -> TCM Doc -> TCM Doc class PrettyTCM a where prettyTCM :: a -> TCM Doc instance PrettyTCM a => PrettyTCM (Closure a) instance PrettyTCM a => PrettyTCM [a] instance PrettyTCM Name instance PrettyTCM QName instance PrettyTCM Term instance PrettyTCM Elim instance PrettyTCM Type instance PrettyTCM Sort instance PrettyTCM DisplayTerm instance PrettyTCM DBPatVar Agda-2.5.3/src/full/Agda/TypeChecking/SizedTypes/0000755000000000000000000000000013154613124017610 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/SizedTypes/Syntax.hs0000644000000000000000000002353713154613124021444 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE NoMonomorphismRestriction #-} {-# LANGUAGE UndecidableInstances #-} -- | Syntax of size expressions and constraints. module Agda.TypeChecking.SizedTypes.Syntax where import Prelude hiding (null) import Data.Maybe import Data.Foldable (Foldable) import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import Data.Traversable (Traversable) import Agda.TypeChecking.SizedTypes.Utils import Agda.Utils.Functor import Agda.Utils.Null import Agda.Utils.Pretty -- * Syntax -- | Constant finite sizes @n >= 0@. newtype Offset = O Int deriving (Eq, Ord, Num, Enum) -- This Show instance is ok because of the Enum constraint. instance Show Offset where show (O n) = show n instance Pretty Offset where pretty (O n) = pretty n instance MeetSemiLattice Offset where meet = min instance Plus Offset Offset Offset where plus (O x) (O y) = O (plus x y) -- | Fixed size variables @i@. newtype Rigid = RigidId { rigidId :: String } deriving (Eq, Ord) instance Show Rigid where show (RigidId s) = "RigidId " ++ show s instance Pretty Rigid where pretty = text . rigidId -- | Size meta variables @X@ to solve for. newtype Flex = FlexId { flexId :: String } deriving (Eq, Ord) instance Show Flex where show (FlexId s) = "FlexId " ++ show s instance Pretty Flex where pretty = text . flexId -- | Size expressions appearing in constraints. data SizeExpr' rigid flex = Const { offset :: Offset } -- ^ Constant number @n@. | Rigid { rigid :: rigid, offset :: Offset } -- ^ Variable plus offset @i + n@. | Infty -- ^ Infinity @∞@. | Flex { flex :: flex, offset :: Offset } -- ^ Meta variable @X + n@. deriving (Show, Eq, Ord, Functor, Foldable, Traversable) type SizeExpr = SizeExpr' Rigid Flex -- | Comparison operator, e.g. for size expression. data Cmp = Lt -- ^ @<@. | Le -- ^ @≤@. deriving (Show, Eq, Bounded, Enum) instance Dioid Cmp where compose = min unitCompose = Le -- | Comparison operator is ordered @'Lt' < 'Le'@. instance Ord Cmp where Lt <= x = True Le <= Lt = False Le <= Le = True instance MeetSemiLattice Cmp where meet = min instance Top Cmp where top = Le -- | Constraint: an inequation between size expressions, -- e.g. @X < ∞@ or @i + 3 ≤ j@. data Constraint' rigid flex = Constraint { leftExpr :: SizeExpr' rigid flex , cmp :: Cmp , rightExpr :: SizeExpr' rigid flex } deriving (Show, Functor, Foldable, Traversable) type Constraint = Constraint' Rigid Flex -- * Polarities to specify solutions. ------------------------------------------------------------------------ -- | What type of solution are we looking for? data Polarity = Least | Greatest deriving (Eq, Ord) -- | Assigning a polarity to a flexible variable. data PolarityAssignment flex = PolarityAssignment Polarity flex -- | Type of solution wanted for each flexible. type Polarities flex = Map flex Polarity emptyPolarities :: Polarities flex emptyPolarities = Map.empty polaritiesFromAssignments :: Ord flex => [PolarityAssignment flex] -> Polarities flex polaritiesFromAssignments = Map.fromList . map (\ (PolarityAssignment p x) -> (x,p)) -- | Default polarity is 'Least'. getPolarity :: Ord flex => Polarities flex -> flex -> Polarity getPolarity pols x = Map.findWithDefault Least x pols -- * Solutions. ------------------------------------------------------------------------ -- | Partial substitution from flexible variables to size expression. newtype Solution rigid flex = Solution { theSolution :: Map flex (SizeExpr' rigid flex) } deriving (Show, Null) instance (Pretty r, Pretty f) => Pretty (Solution r f) where pretty (Solution sol) = prettyList $ for (Map.toList sol) $ \ (x, e) -> pretty x <+> text ":=" <+> pretty e emptySolution :: Solution r f emptySolution = Solution Map.empty -- | Executing a substitution. class Substitute r f a where subst :: Solution r f -> a -> a instance Ord f => Substitute r f (SizeExpr' r f) where subst (Solution sol) e = case e of Flex x n -> maybe e (`plus` n) $ Map.lookup x sol _ -> e instance Ord f => Substitute r f (Constraint' r f) where subst sol (Constraint e cmp e') = Constraint (subst sol e) cmp (subst sol e') instance Substitute r f a => Substitute r f [a] where subst = map . subst instance Substitute r f a => Substitute r f (Map k a) where subst = fmap . subst instance Ord f => Substitute r f (Solution r f) where subst s = Solution . subst s . theSolution -- | Add offset to size expression. instance Plus (SizeExpr' r f) Offset (SizeExpr' r f) where plus e m = case e of Const n -> Const $ n + m Rigid i n -> Rigid i $ n + m Flex x n -> Flex x $ n + m Infty -> Infty -- * Constraint simplification type CTrans r f = Constraint' r f -> Either String [Constraint' r f] -- | Returns an error message if we have a contradictory constraint. simplify1 :: (Pretty f, Pretty r, Eq r) => CTrans r f -> CTrans r f simplify1 test c = do let err = Left $ "size constraint " ++ prettyShow c ++ " is inconsistent" case c of -- rhs is Infty Constraint a Le Infty -> return [] Constraint Const{} Lt Infty -> return [] Constraint Infty Lt Infty -> err Constraint (Rigid i n) Lt Infty -> test $ Constraint (Rigid i 0) Lt Infty Constraint a@Flex{} Lt Infty -> return [c { leftExpr = a { offset = 0 }}] -- rhs is Const Constraint (Const n) cmp (Const m) -> if compareOffset n cmp m then return [] else err Constraint Infty cmp Const{} -> err Constraint (Rigid i n) cmp (Const m) -> if compareOffset n cmp m then test (Constraint (Rigid i 0) Le (Const (m - n - ifLe cmp 0 1))) else err Constraint (Flex x n) cmp (Const m) -> if compareOffset n cmp m then return [Constraint (Flex x 0) Le (Const (m - n - ifLe cmp 0 1))] else err -- rhs is Rigid Constraint Infty cmp Rigid{} -> err Constraint (Const m) cmp (Rigid i n) -> if compareOffset m cmp n then return [] else test (Constraint (Const $ m - n) cmp (Rigid i 0)) Constraint (Rigid j m) cmp (Rigid i n) | i == j -> if compareOffset m cmp n then return [] else err Constraint (Rigid j m) cmp (Rigid i n) -> test c Constraint (Flex x m) cmp (Rigid i n) -> if compareOffset m cmp n then return [Constraint (Flex x 0) Le (Rigid i (n - m - ifLe cmp 0 1))] else return [Constraint (Flex x $ m - n + ifLe cmp 0 1) Le (Rigid i 0)] -- rhs is Flex Constraint Infty Le (Flex x n) -> return [Constraint Infty Le (Flex x 0)] Constraint Infty Lt (Flex x n) -> err Constraint (Const m) cmp (Flex x n) -> if compareOffset m cmp n then return [] else return [Constraint (Const $ m - n + ifLe cmp 0 1) Le (Flex x 0)] Constraint (Rigid i m) cmp (Flex x n) -> if compareOffset m cmp n then return [Constraint (Rigid i 0) cmp (Flex x $ n - m)] else return [Constraint (Rigid i $ m - n) cmp (Flex x 0)] Constraint (Flex y m) cmp (Flex x n) -> if compareOffset m cmp n then return [Constraint (Flex y 0) cmp (Flex x $ n - m)] else return [Constraint (Flex y $ m - n) cmp (Flex x 0)] -- | 'Le' acts as 'True', 'Lt' as 'False'. ifLe :: Cmp -> a -> a -> a ifLe Le a b = a ifLe Lt a b = b -- | Interpret 'Cmp' as relation on 'Offset'. compareOffset :: Offset -> Cmp -> Offset -> Bool compareOffset n Le m = n <= m compareOffset n Lt m = n < m -- * Printing instance (Pretty r, Pretty f) => Pretty (SizeExpr' r f) where pretty (Const n) = pretty n pretty (Infty) = text "∞" pretty (Rigid i 0) = pretty i pretty (Rigid i n) = pretty i <> text ("+" ++ show n) pretty (Flex x 0) = pretty x pretty (Flex x n) = pretty x <> text ("+" ++ show n) instance Pretty Polarity where pretty Least = text "-" pretty Greatest = text "+" instance Pretty flex => Pretty (PolarityAssignment flex) where pretty (PolarityAssignment pol flex) = pretty pol <> pretty flex instance Pretty Cmp where pretty Le = text "≤" pretty Lt = text "<" instance (Pretty r, Pretty f) => Pretty (Constraint' r f) where pretty (Constraint a cmp b) = pretty a <+> pretty cmp <+> pretty b -- * Wellformedness -- | Offsets @+ n@ must be non-negative class ValidOffset a where validOffset :: a -> Bool instance ValidOffset Offset where validOffset = (>= 0) instance ValidOffset (SizeExpr' r f) where validOffset e = case e of Infty -> True _ -> validOffset (offset e) -- | Make offsets non-negative by rounding up. class TruncateOffset a where truncateOffset :: a -> a instance TruncateOffset Offset where truncateOffset n | n >= 0 = n | otherwise = 0 instance TruncateOffset (SizeExpr' r f) where truncateOffset e = case e of Infty -> e Const n -> Const $ truncateOffset n Rigid i n -> Rigid i $ truncateOffset n Flex x n -> Flex x $ truncateOffset n -- * Computing variable sets -- | The rigid variables contained in a pice of syntax. class Rigids r a where rigids :: a -> Set r instance (Ord r, Rigids r a) => Rigids r [a] where rigids as = Set.unions (map rigids as) instance Rigids r (SizeExpr' r f) where rigids (Rigid x _) = Set.singleton x rigids _ = Set.empty instance Ord r => Rigids r (Constraint' r f) where rigids (Constraint l _ r) = Set.union (rigids l) (rigids r) -- | The flexibe variables contained in a pice of syntax. class Flexs flex a | a -> flex where flexs :: a -> Set flex instance (Ord flex, Flexs flex a) => Flexs flex [a] where flexs as = Set.unions (map flexs as) instance Flexs flex (SizeExpr' rigid flex) where flexs (Flex x _) = Set.singleton x flexs _ = Set.empty instance (Ord flex) => Flexs flex (Constraint' rigid flex) where flexs (Constraint l _ r) = Set.union (flexs l) (flexs r) Agda-2.5.3/src/full/Agda/TypeChecking/SizedTypes/Utils.hs0000644000000000000000000000165313154613124021251 0ustar0000000000000000 module Agda.TypeChecking.SizedTypes.Utils where import Control.Applicative import Data.IORef import qualified Debug.Trace as Debug import System.IO.Unsafe import Agda.Utils.Function {-# NOINLINE debug #-} debug :: IORef Bool debug = unsafePerformIO $ newIORef False setDebugging :: Bool -> IO () setDebugging = writeIORef debug trace :: String -> a -> a trace s = applyWhen (unsafePerformIO $ readIORef debug) $ Debug.trace s traceM :: Applicative f => String -> f () traceM s = trace s $ pure () class Eq a => Top a where top :: a isTop :: a -> Bool isTop = (==top) class Plus a b c where plus :: a -> b -> c instance Plus Int Int Int where plus = (+) class MeetSemiLattice a where meet :: a -> a -> a -- | Semiring with idempotent '+' == dioid class (MeetSemiLattice a, Top a) => Dioid a where compose :: a -> a -> a -- ^ E.g. + unitCompose :: a -- ^ neutral element of @compose@, e.g. zero Agda-2.5.3/src/full/Agda/TypeChecking/SizedTypes/Solve.hs0000644000000000000000000007546413154613124021254 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} -- | Solving size constraints under hypotheses. -- -- The size solver proceeds as follows: -- -- 1. Get size constraints, cluster into connected components. -- -- All size constraints that mention the same metas go into the same -- cluster. Each cluster can be solved by itself. -- -- Constraints that do not fit our format are ignored. -- We check whether our computed solution fulfills them as well -- in the last step. -- -- 2. Find a joint context for each cluster. -- -- Each constraint comes with its own typing context, which -- contains size hypotheses @j : Size< i@. We need to find a -- common super context in which all constraints of a cluster live, -- and raise all constraints to this context. -- -- There might not be a common super context. Then we are screwed, -- since our solver is not ready to deal with such a situation. We -- will blatantly refuse to solve this cluster and blame it on the -- user. -- -- 3. Convert the joint context into a hypothesis graph. -- -- This is straightforward. Each de Bruijn index becomes a -- rigid variable, each typing assumption @j : Size< i@ becomes an -- arc. -- -- 4. Convert the constraints into a constraint graph. -- -- Here we need to convert @MetaV@s into flexible variables. -- -- 5. Run the solver -- -- 6. Convert the solution into meta instantiations. -- -- 7. Double-check whether the constraints are solved. -- Opportunities for optimization: -- -- - NamedRigids has some cost to retrieve variable names -- just for the sake of debug printing. module Agda.TypeChecking.SizedTypes.Solve where import Prelude hiding (null) import Control.Monad hiding (forM, forM_) import Control.Monad.Trans.Maybe import Control.Monad.Reader (asks) import Data.Foldable (Foldable, foldMap, forM_) import Data.Function import qualified Data.List as List import Data.Monoid import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import qualified Data.Traversable as Trav import Data.Traversable (Traversable, forM) import Agda.Interaction.Options import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad as TCM hiding (Offset) import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Pretty import Agda.TypeChecking.Free import Agda.TypeChecking.Reduce import Agda.TypeChecking.MetaVars import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Conversion import Agda.TypeChecking.Constraints as C import qualified Agda.TypeChecking.SizedTypes as S import Agda.TypeChecking.SizedTypes.Syntax as Size import Agda.TypeChecking.SizedTypes.Utils import Agda.TypeChecking.SizedTypes.WarshallSolver as Size import Agda.Utils.Cluster import Agda.Utils.Either import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.Lens #if MIN_VERSION_base(4,8,0) import qualified Agda.Utils.List as List hiding ( uncons ) #else import qualified Agda.Utils.List as List #endif import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Pretty (Pretty, prettyShow) import qualified Agda.Utils.Pretty as P import Agda.Utils.Size import Agda.Utils.Tuple import qualified Agda.Utils.VarSet as VarSet #include "undefined.h" import Agda.Utils.Impossible type CC = Closure TCM.Constraint -- | Flag to control the behavior of size solver. data DefaultToInfty = DefaultToInfty -- ^ Instantiate all unconstrained size variables to ∞. | DontDefaultToInfty -- ^ Leave unconstrained size variables unsolved. deriving (Eq, Ord, Show) -- | Solve size constraints involving hypotheses. solveSizeConstraints :: DefaultToInfty -> TCM () solveSizeConstraints flag = do -- 1. Take out the size constraints normalised. cs0 <- mapM (mapClosure normalise) =<< S.takeSizeConstraints -- NOTE: this deletes the size constraints from the constraint set! unless (null cs0) $ reportSDoc "tc.size.solve" 40 $ vcat $ [ text $ "Solving constraints (" ++ show flag ++ ")" ] ++ map prettyTCM cs0 let -- Error for giving up cannotSolve = typeError . GenericDocError =<< vcat (text "Cannot solve size constraints" : map prettyTCM cs0) -- 2. Cluster the constraints by common size metas. -- Get all size metas. sizeMetaSet <- Set.fromList . map (\ (x, _t, _tel) -> x) <$> S.getSizeMetas True -- Pair each constraint with its list of size metas occurring in it. cms <- forM cs0 $ \ cl -> enterClosure cl $ \ c -> do -- @allMetas@ does not reduce or instantiate; -- this is why we require the size constraints to be normalised. return (cl, map metaId . Set.toList $ sizeMetaSet `Set.intersection` Set.fromList (allMetas c)) -- Now, some constraints may have no metas (clcs), the others have at least one (othercs). let classify :: (a, [b]) -> Either a (a, (b,[b])) classify (cl, []) = Left cl classify (cl, (x:xs)) = Right (cl, (x,xs)) let (clcs, othercs) = List.mapEither classify cms -- We cluster the constraints by their metas. let ccs = cluster' othercs -- 3. Solve each cluster -- Solve the closed constraints, one by one. forM_ clcs $ \ c -> () <$ solveSizeConstraints_ flag [c] -- Solve the clusters. constrainedMetas <- Set.unions <$> do forM (ccs) $ \ (cs :: [CC]) -> do when (null cs) __IMPOSSIBLE__ -- Convert each constraint in the cluster to the largest context. -- (Keep fingers crossed). enterClosure (List.maximumBy (compare `on` (length . envContext . clEnv)) cs) $ \ _ -> do -- Get all constraints that can be cast to the longest context. cs' :: [TCM.Constraint] <- catMaybes <$> do mapM (runMaybeT . castConstraintToCurrentContext) cs reportSDoc "tc.size.solve" 20 $ vcat $ [ text "converted size constraints to context: " <+> do tel <- getContextTelescope inTopContext $ prettyTCM tel ] ++ map (nest 2 . prettyTCM) cs' -- Solve the converted constraints. solveSizeConstraints_ flag =<< mapM buildClosure cs' -- 4. Possibly set remaining metas to infinity. -- Andreas, issue 1862: do not default to ∞ always, could be too early. when (flag == DefaultToInfty) $ do -- let constrainedMetas = Set.fromList $ concat $ -- for cs0 $ \ Closure{ clValue = ValueCmp _ _ u v } -> -- allMetas u ++ allMetas v -- Set the unconstrained size metas to ∞. ms <- S.getSizeMetas False -- do not get interaction metas unless (null ms) $ do inf <- primSizeInf forM_ ms $ \ (m, t, tel) -> do unless (m `Set.member` constrainedMetas) $ do unlessM (isFrozen m) $ do reportSDoc "tc.size.solve" 20 $ text "solution " <+> prettyTCM (MetaV m []) <+> text " := " <+> prettyTCM inf assignMeta 0 m t (List.downFrom $ size tel) inf -- -- Double check. -- unless (null cs0 && null ms) $ do -- flip catchError (const cannotSolve) $ -- noConstraints $ -- forM_ cs0 $ \ cl -> enterClosure cl solveConstraint -- 5. Make sure we did not lose any constraints. -- This is necessary since we have removed the size constraints. forM_ cs0 $ \ cl -> enterClosure cl solveConstraint -- | TODO: this does not actually work! -- -- We would like to use a constraint @c@ created in context @Δ@ from module @N@ -- in the current context @Γ@ and current module @M@. -- -- @Δ@ is module tel @Δ₁@ of @N@ extended by some local bindings @Δ₂@. -- @Γ@ is the current context. -- The module parameter substitution from current @M@ to @N@ be -- @Γ ⊢ σ : Δ₁@. -- -- If @M == N@, we do not need the parameter substitution. We try raising. -- -- We first strengthen @Δ ⊢ c@ to live in @Δ₁@ and obtain @c₁ = strengthen Δ₂ c@. -- We then transport @c₁@ to @Γ@ and obtain @c₂ = applySubst σ c₁@. -- -- This works for different modules, but if @M == N@ we should not strengthen -- and then weaken, because strengthening is a partial operation. -- We should rather lift the substitution @σ@ by @Δ₂@ and then -- raise by @Γ₂ - Δ₂@. -- This "raising" might be a strengthening if @Γ₂@ is shorter than @Δ₂@. -- -- (TODO: If the module substitution does not exist, because @N@ is not -- a parent of @M@, we cannot use the constraint, as it has been created -- in an unrelated context.) castConstraintToCurrentContext' :: Closure TCM.Constraint -> MaybeT TCM TCM.Constraint castConstraintToCurrentContext' cl = do let modN = envCurrentModule $ clEnv cl delta = envContext $ clEnv cl -- The module telescope of the constraint. -- The constraint could come from the module telescope of the top level module. -- In this case, it does not live in any module! -- Thus, getSection can return Nothing. delta1 <- liftTCM $ maybe empty (^. secTelescope) <$> getSection modN -- The number of locals of the constraint. let delta2 = size delta - size delta1 unless (delta2 >= 0) __IMPOSSIBLE__ -- The current module M and context Γ. modM <- currentModule gamma <- liftTCM $ getContextSize -- The current module telescope. -- Could also be empty, if we are in the front matter or telescope of the top-level module. gamma1 <-liftTCM $ maybe empty (^. secTelescope) <$> getSection modM -- The current locals. let gamma2 = gamma - size gamma1 -- Γ ⊢ σ : Δ₁ sigma <- liftTCM $ getModuleParameterSub modN -- Debug printing. reportSDoc "tc.constr.cast" 40 $ text "casting constraint" $$ do tel <- getContextTelescope inTopContext $ nest 2 $ vcat $ [ text "current module = " <+> prettyTCM modM , text "current module telescope = " <+> prettyTCM gamma1 , text "current context = " <+> prettyTCM tel , text "constraint module = " <+> prettyTCM modN , text "constraint module telescope = " <+> prettyTCM delta1 , text "constraint context = " <+> (prettyTCM =<< enterClosure cl (const $ getContextTelescope)) , text "constraint = " <+> enterClosure cl prettyTCM , text "module parameter substitution = " <+> prettyTCM sigma ] -- If gamma2 < 0, we must be in the wrong context. -- E.g. we could have switched to the empty context even though -- we are still inside a module with parameters. -- In this case, we cannot safely convert the constraint, -- since the module parameter substitution may be wrong. guard (gamma2 >= 0) -- Shortcut for modN == modM: -- Raise constraint from Δ to Γ, if possible. -- This might save us some strengthening. if modN == modM then raiseMaybe (gamma - size delta) $ clValue cl else do -- Strengthen constraint to Δ₁ ⊢ c c <- raiseMaybe (-delta2) $ clValue cl -- Ulf, 2016-11-09: I don't understand what this function does when M and N -- are not related. Certainly things can go terribly wrong (see -- test/Succeed/Issue2223b.agda) fv <- liftTCM $ getModuleFreeVars modN guard $ fv == size delta1 -- Γ ⊢ c[σ] return $ applySubst sigma c where raiseMaybe n c = do -- Fine if we have to weaken or strengthening is safe. guard $ n >= 0 || List.all (>= -n) (VarSet.toList $ allVars $ freeVars c) return $ raise n c -- | A hazardous hack, may the Gods have mercy on us. -- -- To cast to the current context, we match the context of the -- given constraint by 'CtxId', and as fallback, by variable name (douh!). -- -- This hack lets issue 2046 go through. castConstraintToCurrentContext :: Closure TCM.Constraint -> MaybeT TCM TCM.Constraint castConstraintToCurrentContext cl = do -- The target context gamma <- asks envContext -- The context where the constraint lives. let delta = envContext $ clEnv cl -- The constraint let c = clValue cl let findInGamma (Ctx cid (Dom _ (x, t))) = -- try to find same CtxId (safe) case List.findIndex ((cid ==) . ctxId) gamma of Just i -> Just i Nothing -> -- match by name (hazardous) -- This is one of the seven deadly sins (not respecting alpha). List.findIndex ((x ==) . fst . unDom . ctxEntry) gamma let cand = map findInGamma delta -- The domain of our substitution let coveredVars = VarSet.fromList $ catMaybes $ zipWith ($>) cand [0..] -- Check that all the free variables of the constraint are contained in -- coveredVars. -- We ignore the free variables occurring in sorts. guard $ getAll $ runFree (All . (`VarSet.member` coveredVars)) IgnoreAll c -- Turn cand into a substitution. -- Since we ignored the free variables in sorts, we better patch up -- the substitution with some dummy term (Sort Prop) rather than __IMPOSSIBLE__. let dummy = Sort Prop let sigma = parallelS $ map (maybe dummy var) cand -- Apply substitution to constraint and pray that the Gods are merciful on us. return $ applySubst sigma c -- Note: the resulting constraint may not well-typed. -- Even if it is, it may map variables to their wrong counterpart. -- | Return the size metas occurring in the simplified constraints. -- A constraint like @↑ _j =< ∞ : Size@ simplifies to nothing, -- so @_j@ would not be in this set. solveSizeConstraints_ :: DefaultToInfty -> [CC] -> TCM (Set MetaId) solveSizeConstraints_ flag cs0 = do -- Pair constraints with their representation as size constraints. -- Discard constraints that do not have such a representation. ccs :: [(CC,HypSizeConstraint)] <- catMaybes <$> do forM cs0 $ \ c0 -> fmap (c0,) <$> computeSizeConstraint c0 -- Simplify constraints and check for obvious inconsistencies. ccs' <- concat <$> do forM ccs $ \ (c0, HypSizeConstraint cxt hids hs sc) -> do case simplify1 (\ sc -> return [sc]) sc of Left _ -> typeError . GenericDocError =<< do text "Contradictory size constraint" <+> prettyTCM c0 Right cs -> return $ (c0,) . HypSizeConstraint cxt hids hs <$> cs -- Cluster constraints according to the meta variables they mention. -- @csNoM@ are the constraints that do not mention any meta. let (csNoM, csMs) = (`List.partitionMaybe` ccs') $ \ p@(c0, c) -> fmap (p,) $ List.uncons $ map (metaId . sizeMetaId) $ Set.toList $ flexs c -- @css@ are the clusters of constraints. css :: [[(CC,HypSizeConstraint)]] css = cluster' csMs -- Check that the closed constraints are valid. solveCluster flag csNoM -- Now, process the clusters. forM_ css $ solveCluster flag return $ Set.mapMonotonic sizeMetaId $ flexs $ map (snd . fst) csMs -- | Solve a cluster of constraints sharing some metas. -- solveCluster :: DefaultToInfty -> [(CC,HypSizeConstraint)] -> TCM () solveCluster flag [] = return () solveCluster flag ccs = do let cs = map snd ccs let err reason = typeError . GenericDocError =<< do vcat $ [ text $ "Cannot solve size constraints" ] ++ map prettyTCM cs ++ [ text $ "Reason: " ++ reason ] reportSDoc "tc.size.solve" 20 $ vcat $ [ text "Solving constraint cluster" ] ++ map prettyTCM cs -- Find the super context of all contexts. {- -- We use the @'ctxId'@s. let cis@(ci:cis') = for cs $ \ c -> (c, reverse $ map ctxId $ sizeContext c) -- let cis@(ci:cis') = for cs $ \ c -> (c, reverse $ sizeHypIds c) max a@Left{} _ = a max a@(Right ci@(c,is)) ci'@(c',is') = case preOrSuffix is is' of -- No common context: IsNofix -> Left (ci, ci') IsPrefix{} -> Right ci' _ -> a res = foldl' max (Right ci) cis' noContext ((c,is),(c',is')) = typeError . GenericDocError =<< vcat [ text "Cannot solve size constraints; the following constraints have no common typing context:" , prettyTCM c , prettyTCM c' ] flip (either noContext) res $ \ (HypSizeConstraint gamma hids hs _, _) -> do -} -- We rely on the fact that contexts are only extended... -- Just take the longest context. let HypSizeConstraint gamma hids hs _ = List.maximumBy (compare `on` (length . sizeContext)) cs -- Length of longest context. let n = size gamma -- Now convert all size constraints to the largest context. csL = for cs $ \ (HypSizeConstraint cxt _ _ c) -> raise (n - size cxt) c -- Canonicalize the constraints. -- This is unsound in the presence of hypotheses. csC :: [SizeConstraint] csC = applyWhen (null hs) (mapMaybe canonicalizeSizeConstraint) csL reportSDoc "tc.size.solve" 30 $ vcat $ [ text "Size hypotheses" ] ++ map (prettyTCM . HypSizeConstraint gamma hids hs) hs ++ [ text "Canonicalized constraints" ] ++ map (prettyTCM . HypSizeConstraint gamma hids hs) csC -- -- ALT: -- -- Now convert all size constraints to de Bruijn levels. -- -- To get from indices in a context of length m <= n -- -- to levels into the target context of length n, -- -- we apply the following substitution: -- -- Index m-1 needs to be mapped to level 0, -- -- index m-2 needs to be mapped to level 1, -- -- index 0 needs to be mapped to level m-1, -- -- so the desired substitution is @downFrom m@. -- let sub m = applySubst $ parallelS $ map var $ downFrom m -- -- We simply reverse the context to get to de Bruijn levels. -- -- Of course, all types in the context are broken, but -- -- only need it for pretty printing constraints. -- gamma <- return $ reverse gamma -- -- We convert the hypotheses to de Bruijn levels. -- hs <- return $ sub n hs -- -- We get a form for pretty-printing -- let prettyC = prettyTCM . HypSizeConstraint gamma hids hs -- -- We convert the constraints to de Bruijn level format. -- let csC :: [SizeConstraint] -- csC = for cs $ \ (HypSizeConstraint cxt _ _ c) -> sub (size cxt) c -- reportSDoc "tc.size.solve" 30 $ vcat $ -- [ text "Size hypotheses" ] ++ map prettyC hs ++ -- [ text "Canonicalized constraints" ] ++ map prettyC csC -- Convert size metas to flexible vars. let metas :: [SizeMeta] metas = concat $ map (foldMap (:[])) csC csF :: [Size.Constraint' NamedRigid Int] csF = map (fmap (metaId . sizeMetaId)) csC -- Construct the hypotheses graph. let hyps = map (fmap (metaId . sizeMetaId)) hs -- There cannot be negative cycles in hypotheses graph due to scoping. let hg = fromRight __IMPOSSIBLE__ $ hypGraph (rigids csF) hyps -- -- Construct the constraint graph. -- -- g :: Size.Graph NamedRigid Int Label -- g <- either err return $ constraintGraph csF hg -- reportSDoc "tc.size.solve" 40 $ vcat $ -- [ text "Constraint graph" -- , text (show g) -- ] -- sol :: Solution NamedRigid Int <- either err return $ solveGraph Map.empty hg g -- either err return $ verifySolution hg csF sol -- Andreas, 2016-07-13, issue 2096. -- Running the solver once might result in unsolvable left-over constraints. -- We need to iterate the solver to detect this. sol :: Solution NamedRigid Int <- either err return $ iterateSolver Map.empty hg csF emptySolution -- Convert solution to meta instantiation. forM_ (Map.assocs $ theSolution sol) $ \ (m, a) -> do unless (validOffset a) __IMPOSSIBLE__ -- Solution does not contain metas u <- unSizeExpr $ fmap __IMPOSSIBLE__ a let x = MetaId m let SizeMeta _ xs = fromMaybe __IMPOSSIBLE__ $ List.find ((m==) . metaId . sizeMetaId) metas -- Check that solution is well-scoped let ys = rigidIndex <$> Set.toList (rigids a) ok = all (`elem` xs) ys -- TODO: more efficient -- unless ok $ err "ill-scoped solution for size meta variable" u <- if ok then return u else primSizeInf t <- getMetaType x reportSDoc "tc.size.solve" 20 $ inTopContext $ modifyContext (const gamma) $ do let args = map (Apply . defaultArg . var) xs text "solution " <+> prettyTCM (MetaV x args) <+> text " := " <+> prettyTCM u reportSDoc "tc.size.solve" 60 $ vcat [ text $ " xs = " ++ show xs , text $ " u = " ++ show u ] unlessM (isFrozen x) $ assignMeta n x t xs u -- WRONG: -- let partialSubst = List.sort $ zip xs $ map var $ downFrom n -- assignMeta' n x t (length xs) partialSubst u -- WRONG: assign DirEq x (map (defaultArg . var) xs) u -- Possibly set remaining size metas to ∞ (issue 1862) -- unless we have an interaction meta in the cluster (issue 2095). ims <- Set.fromList <$> getInteractionMetas -- ms = unsolved size metas from cluster let ms = Set.fromList (map sizeMetaId metas) Set.\\ -- Some CPP or ghc does not like trailing backslash, thus, this comment! Set.mapMonotonic MetaId (Map.keysSet $ theSolution sol) -- Make sure they do not contain an interaction point let noIP = Set.null $ Set.intersection ims ms unless (null ms) $ reportSDoc "tc.size.solve" 30 $ fsep $ [ text "cluster did not solve these size metas: " ] ++ map prettyTCM (Set.toList ms) solvedAll <- do -- If no metas are left, we have solved this cluster completely. if Set.null ms then return True else do -- Otherwise, we can solve it completely if we are allowed to set to ∞. if flag == DontDefaultToInfty then return False else do -- Which is only the case when we have no interaction points in the cluster. if not noIP then return False else do -- Try to set all unconstrained size metas to ∞. inf <- primSizeInf and <$> do forM (Set.toList ms) $ \ m -> do -- If one variable is frozen, we cannot set it (and hence not all) to ∞ let no = do reportSDoc "tc.size.solve" 30 $ prettyTCM (MetaV m []) <+> text "is frozen, cannot set it to ∞" return False ifM (isFrozen m `or2M` do not <$> asks envAssignMetas) no $ {-else-} do reportSDoc "tc.size.solve" 20 $ text "solution " <+> prettyTCM (MetaV m []) <+> text " := " <+> prettyTCM inf t <- jMetaType . mvJudgement <$> lookupMeta m TelV tel core <- telView t unlessM (isJust <$> isSizeType core) __IMPOSSIBLE__ assignMeta 0 m t (List.downFrom $ size tel) inf return True -- Double check. when solvedAll $ do let cs0 = map fst ccs -- Error for giving up cannotSolve = typeError . GenericDocError =<< vcat (text "Cannot solve size constraints" : map prettyTCM cs0) flip catchError (const cannotSolve) $ noConstraints $ forM_ cs0 $ \ cl -> enterClosure cl solveConstraint -- | Collect constraints from a typing context, looking for SIZELT hypotheses. getSizeHypotheses :: Context -> TCM [(CtxId, SizeConstraint)] getSizeHypotheses gamma = inTopContext $ modifyContext (const gamma) $ do (_, msizelt) <- getBuiltinSize caseMaybe msizelt (return []) $ \ sizelt -> do -- Traverse the context from newest to oldest de Bruijn Index catMaybes <$> do forM (zip [0..] gamma) $ \ (i, ce) -> do -- Get name and type of variable i. let xt = unDom $ ctxEntry ce x = prettyShow $ fst xt t <- reduce . raise (1 + i) . unEl . snd $ xt case ignoreSharing t of Def d [Apply u] | d == sizelt -> do caseMaybeM (sizeExpr $ unArg u) (return Nothing) $ \ a -> return $ Just $ (ctxId ce, Constraint (Rigid (NamedRigid x i) 0) Lt a) _ -> return Nothing -- | Convert size constraint into form where each meta is applied -- to indices @n-1,...,1,0@ where @n@ is the arity of that meta. -- -- @X[σ] <= t@ becomes @X[id] <= t[σ^-1]@ -- -- @X[σ] ≤ Y[τ]@ becomes @X[id] ≤ Y[τ[σ^-1]]@ or @X[σ[τ^1]] ≤ Y[id]@ -- whichever is defined. If none is defined, we give up. -- -- Cf. @SizedTypes.oldCanonicalizeSizeConstraint@. -- -- Fixes (the rather artificial) issue 300. -- But it is unsound when pruned metas occur and triggers issue 1914. -- Thus we deactivate it. -- This needs to be properly implemented, possibly using the -- metaPermuatation of each meta variable. canonicalizeSizeConstraint :: SizeConstraint -> Maybe (SizeConstraint) canonicalizeSizeConstraint c@(Constraint a cmp b) = Just c {- case (a,b) of -- Case flex-flex (Flex (SizeMeta m xs) n, Flex (SizeMeta l ys) n') -- try to invert xs on ys | let len = size xs , Just ys' <- mapM (\ y -> (len-1 -) <$> findIndex (==y) xs) ys -> return $ Constraint (Flex (SizeMeta m $ downFrom len) n) cmp (Flex (SizeMeta l ys') n') -- try to invert ys on xs | let len = size ys , Just xs' <- mapM (\ x -> (len-1 -) <$> findIndex (==x) ys) xs -> return $ Constraint (Flex (SizeMeta m xs') n) cmp (Flex (SizeMeta l $ downFrom len) n') -- give up | otherwise -> Nothing -- Case flex-rigid (Flex (SizeMeta m xs) n, Rigid (NamedRigid x i) n') -> do let len = size xs j <- (len-1 -) <$> findIndex (==i) xs return $ Constraint (Flex (SizeMeta m $ downFrom len) n) cmp (Rigid (NamedRigid x j) n') -- Case rigid-flex (Rigid (NamedRigid x i) n, Flex (SizeMeta m xs) n') -> do let len = size xs j <- (len-1 -) <$> findIndex (==i) xs return $ Constraint (Rigid (NamedRigid x j) n) cmp (Flex (SizeMeta m $ downFrom len) n') -- Case flex-const (Flex (SizeMeta m xs) n, _) -> return $ Constraint (Flex (SizeMeta m $ downFrom $ size xs) n) cmp b -- Case const-flex (_, Flex (SizeMeta m xs) n') -> do return $ Constraint a cmp (Flex (SizeMeta m $ downFrom $ size xs) n') -- Case no flex _ -> return c -} -- | Identifiers for rigid variables. data NamedRigid = NamedRigid { rigidName :: String -- ^ Name for printing in debug messages. , rigidIndex :: Int -- ^ De Bruijn index. } deriving (Show) instance Eq NamedRigid where (==) = (==) `on` rigidIndex instance Ord NamedRigid where compare = compare `on` rigidIndex instance Pretty NamedRigid where pretty = P.text . rigidName instance Plus NamedRigid Int NamedRigid where plus (NamedRigid x i) j = NamedRigid x (i + j) -- | Size metas in size expressions. data SizeMeta = SizeMeta { sizeMetaId :: MetaId -- TODO to fix issue 300? -- , sizeMetaPerm :: Permutation -- ^ Permutation from the current context -- -- to the context of the meta. , sizeMetaArgs :: [Int] -- ^ De Bruijn indices. } deriving (Show) -- | An equality which ignores the meta arguments. instance Eq SizeMeta where (==) = (==) `on` sizeMetaId -- | An order which ignores the meta arguments. instance Ord SizeMeta where compare = compare `on` sizeMetaId instance Pretty SizeMeta where pretty = P.pretty . sizeMetaId instance PrettyTCM SizeMeta where prettyTCM (SizeMeta x es) = prettyTCM (MetaV x $ map (Apply . defaultArg . var) es) instance Subst Term SizeMeta where applySubst sigma (SizeMeta x es) = SizeMeta x (map raise es) where raise i = case lookupS sigma i of Var j [] -> j _ -> __IMPOSSIBLE__ -- | Size expression with de Bruijn indices. type DBSizeExpr = SizeExpr' NamedRigid SizeMeta -- deriving instance Functor (SizeExpr' Int) -- deriving instance Foldable (SizeExpr' Int) -- deriving instance Traversable (SizeExpr' Int) -- | Only for 'raise'. instance Subst Term (SizeExpr' NamedRigid SizeMeta) where applySubst sigma a = case a of Infty -> a Const{} -> a Flex x n -> Flex (applySubst sigma x) n Rigid r n -> case lookupS sigma $ rigidIndex r of Var j [] -> Rigid r{ rigidIndex = j } n _ -> __IMPOSSIBLE__ type SizeConstraint = Constraint' NamedRigid SizeMeta instance Subst Term SizeConstraint where applySubst sigma (Constraint a cmp b) = Constraint (applySubst sigma a) cmp (applySubst sigma b) -- | Assumes we are in the right context. instance PrettyTCM (SizeConstraint) where prettyTCM (Constraint a cmp b) = do u <- unSizeExpr a v <- unSizeExpr b prettyTCM u <+> pretty cmp <+> prettyTCM v -- | Size constraint with de Bruijn indices. data HypSizeConstraint = HypSizeConstraint { sizeContext :: Context , sizeHypIds :: [CtxId] , sizeHypotheses :: [SizeConstraint] -- ^ Living in @Context@. , sizeConstraint :: SizeConstraint -- ^ Living in @Context@. } instance Flexs SizeMeta HypSizeConstraint where flexs (HypSizeConstraint _ _ hs c) = flexs hs `mappend` flexs c instance PrettyTCM HypSizeConstraint where prettyTCM (HypSizeConstraint cxt _ hs c) = inTopContext $ modifyContext (const cxt) $ do let cxtNames = reverse $ map (fst . unDom . ctxEntry) cxt -- text ("[#cxt=" ++ show (size cxt) ++ "]") <+> do prettyList (map prettyTCM cxtNames) <+> do applyUnless (null hs) (((hcat $ punctuate (text ", ") $ map prettyTCM hs) <+> text "|-") <+>) (prettyTCM c) -- | Turn a constraint over de Bruijn indices into a size constraint. computeSizeConstraint :: Closure TCM.Constraint -> TCM (Maybe HypSizeConstraint) computeSizeConstraint c = do let cxt = envContext $ clEnv c inTopContext $ modifyContext (const cxt) $ do case clValue c of ValueCmp CmpLeq _ u v -> do reportSDoc "tc.size.solve" 50 $ sep $ [ text "converting size constraint" , prettyTCM c ] ma <- sizeExpr u mb <- sizeExpr v (hids, hs) <- unzip <$> getSizeHypotheses cxt let mk a b = HypSizeConstraint cxt hids hs $ Size.Constraint a Le b -- We only create a size constraint if both terms can be -- parsed to our format of size expressions. return $ mk <$> ma <*> mb _ -> __IMPOSSIBLE__ -- | Turn a term into a size expression. -- -- Returns 'Nothing' if the term isn't a proper size expression. sizeExpr :: Term -> TCM (Maybe DBSizeExpr) sizeExpr u = do u <- reduce u -- Andreas, 2009-02-09. -- This is necessary to surface the solutions of metavariables. reportSDoc "tc.conv.size" 60 $ text "sizeExpr:" <+> prettyTCM u s <- sizeView u case s of SizeInf -> return $ Just Infty SizeSuc u -> fmap (`plus` (1 :: Offset)) <$> sizeExpr u OtherSize u -> case ignoreSharing u of Var i [] -> (\ x -> Just $ Rigid (NamedRigid x i) 0) . prettyShow <$> nameOfBV i -- MetaV m es -> return $ Just $ Flex (SizeMeta m es) 0 MetaV m es | Just xs <- mapM isVar es, List.fastDistinct xs -> return $ Just $ Flex (SizeMeta m xs) 0 _ -> return Nothing where isVar (Proj{}) = Nothing isVar (Apply v) = case ignoreSharing $ unArg v of Var i [] -> Just i _ -> Nothing -- | Turn a de size expression into a term. unSizeExpr :: DBSizeExpr -> TCM Term unSizeExpr a = case a of Infty -> primSizeInf Rigid r (O n) -> do unless (n >= 0) __IMPOSSIBLE__ sizeSuc n $ var $ rigidIndex r Flex (SizeMeta x es) (O n) -> do unless (n >= 0) __IMPOSSIBLE__ sizeSuc n $ MetaV x $ map (Apply . defaultArg . var) es Const{} -> __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/SizedTypes/WarshallSolver.hs0000644000000000000000000011007113154613124023114 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NoMonomorphismRestriction #-} module Agda.TypeChecking.SizedTypes.WarshallSolver where import Prelude hiding (null, truncate) import Control.Applicative hiding (Const, empty) import Control.Monad import Data.Function (on) import qualified Data.List as List import Data.Maybe import Data.Set (Set) import qualified Data.Set as Set import Data.Map (Map) import qualified Data.Map as Map import Agda.TypeChecking.SizedTypes.Syntax import Agda.TypeChecking.SizedTypes.Utils import Agda.Utils.Graph.AdjacencyMap.Unidirectional (Edge(..), Nodes(..), nodes, computeNodes) -- (Edge'(..), allNodes, emptyGraph, insertEdge, graphToList, graphFromList, nodes, lookupEdge, outgoing, incoming, diagonal, transClos) import qualified Agda.Utils.Graph.AdjacencyMap.Unidirectional as Graph import Agda.Utils.Functor import Agda.Utils.Null import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible type Graph r f a = Graph.Graph (Node r f) (Node r f) a type Edge' r f a = Graph.Edge (Node r f) (Node r f) a type Key r f = Edge' r f () type Nodes r f = Graph.Nodes (Node r f) type LabelledEdge r f = Edge' r f Label src :: Edge s t e -> s src = Graph.source dest :: Edge s t e -> t dest = Graph.target lookupEdge :: (Ord s, Ord t) => Graph.Graph s t e -> s -> t -> Maybe e lookupEdge g s t = Graph.lookup s t g graphToList :: Graph.Graph s t e -> [Edge s t e] graphToList = Graph.toList graphFromList :: (Ord s, Ord t) => [Edge s t e] -> Graph.Graph s t e graphFromList = Graph.fromList insertEdge :: (Ord s, Ord t, MeetSemiLattice e, Top e) => Edge s t e -> Graph.Graph s t e -> Graph.Graph s t e insertEdge e g | isTop (label e) = g | otherwise = Graph.insertEdgeWith meet e g -- | Compute list of edges that start in a given node. outgoing :: (Ord r, Ord f) => Graph r f a -> Node r f -> [Edge' r f a] outgoing g s = Graph.edgesFrom g [s] -- | Compute list of edges that target a given node. -- -- Note: expensive for unidirectional graph representations. incoming :: (Ord r, Ord f) => Graph r f a -> Node r f -> [Edge' r f a] incoming g t = Graph.edgesTo g [t] -- | @Set.foldl@ does not exist in legacy versions of the @containers@ package. setFoldl :: (b -> a -> b) -> b -> Set a -> b setFoldl step start = List.foldl' step start . Set.toAscList -- setFoldl = Set.foldl' -- | Floyd-Warshall algorithm. transClos :: forall n a . (Ord n, Dioid a) => Graph.Graph n n a -> Graph.Graph n n a transClos g = setFoldl step g $ allNodes ns where ns = computeNodes g srcs = Set.toAscList $ srcNodes ns dests = Set.toAscList $ tgtNodes ns -- @step g v@ adds all intermediate edges @u --> w@ via @v@ to @g@ -- step :: (Ord n, Dioid a) => Graph.Graph n n a -> n -> Graph.Graph n n a step g v = foldl (flip insertEdge) g $ [ Edge u w $ l1 `compose` l2 | u <- srcs , w <- dests , l1 <- maybeToList $ lookupEdge g u v , l2 <- maybeToList $ lookupEdge g v w ] -- * Edge weights data Weight = Offset Offset | Infinity deriving (Eq, Show) instance Pretty Weight where pretty (Offset x) = pretty x pretty Infinity = text "∞" instance Ord Weight where x <= Infinity = True Infinity <= y = False Offset x <= Offset y = x <= y instance MeetSemiLattice Weight where meet = min instance Top Weight where top = Infinity instance Enum Weight where succ (Offset x) = Offset (succ x) succ (Infinity) = Infinity pred (Offset x) = Offset (pred x) pred (Infinity) = Infinity toEnum = Offset . toEnum fromEnum (Offset x) = fromEnum x fromEnum (Infinity) = __IMPOSSIBLE__ -- | Partial implementation of @Num@. instance Num Weight where Infinity + y = Infinity x + Infinity = Infinity Offset x + Offset y = Offset $ x + y Infinity - Offset y = Infinity Offset x - Offset y = Offset $ x - y x - Infinity = __IMPOSSIBLE__ abs (Offset x) = Offset $ abs x abs Infinity = Infinity signum (Offset x) = Offset $ signum x signum Infinity = Offset $ 1 fromInteger x = Offset (fromInteger x) x * y = __IMPOSSIBLE__ instance Plus Weight Offset Weight where plus w k = w + (Offset k) -- | Test for negativity, used to detect negative cycles. class Negative a where negative :: a -> Bool {- leads to Undecidable/OverlappingInstances: instance (Ord a, Num a) => Negative a where negative = (< 0) -} instance Negative Int where negative = (< 0) instance Negative Offset where negative (O x) = negative x instance Negative Weight where negative Infinity = False negative (Offset x) = negative x -- * Edge labels -- | Going from @Lt@ to @Le@ is @pred@, going from @Le@ to @Lt@ is @succ@. -- -- @X --(R,n)--> Y@ -- means @X (R) Y + n@. -- [ ... if @n@ positive -- and @X + (-n) (R) Y@ if @n@ negative. ] data Label = Label { lcmp :: Cmp, loffset :: Offset } | LInf -- ^ Nodes not connected. deriving (Show) -- | Convert a label to a weight, decrementing in case of 'Lt'. toWeight :: Label -> Weight toWeight (Label Le w) = Offset w toWeight (Label Lt w) = Offset $ pred w toWeight LInf = Infinity instance Negative Label where negative = negative . toWeight instance Eq Label where Label cmp w == Label cmp' w' = cmp == cmp' && w == w' LInf == LInf = True _ == _ = False instance Ord Label where Label Lt w <= Label Lt w' = w <= w' Label Le w <= Label Le w' = w <= w' Label Lt w <= Label Le w' = pred w <= w' Label Le w <= Label Lt w' = succ w <= w' _ <= LInf = True LInf{} <= Label{} = False instance Pretty Label where pretty (Label cmp w) = pretty cmp <> pretty w pretty LInf = text "∞" instance MeetSemiLattice Label where -- one label is neutral LInf `meet` l = l l `meet` LInf = l -- other cases Label Lt w `meet` Label Lt w' = Label Lt $ w `meet` w' Label Le w `meet` Label Le w' = Label Le $ w `meet` w' Label Lt w `meet` Label Le w' = Label Lt $ w `meet` succ w' Label Le w `meet` Label Lt w' = Label Lt $ succ w `meet` w' instance Top Label where top = LInf isTop Label{} = False isTop LInf = True -- * Semiring with idempotent '+' == dioid instance Dioid Weight where compose = (+) unitCompose = 0 instance Dioid Label where compose (Label Lt w) (Label Lt w') = Label Lt $ pred $ w + w' compose (Label cmp w) (Label cmp' w') = Label (compose cmp cmp') $ w + w' compose _ LInf = LInf compose LInf _ = LInf unitCompose = Label Le 0 -- * Graphs -- ** Nodes data Node rigid flex = NodeZero | NodeInfty | NodeRigid rigid | NodeFlex flex deriving (Show, Eq, Ord) instance (Pretty rigid, Pretty flex) => Pretty (Node rigid flex) where pretty NodeZero = text "0" pretty NodeInfty = text "∞" pretty (NodeRigid x) = pretty x pretty (NodeFlex x) = pretty x isFlexNode :: Node rigid flex -> Maybe flex isFlexNode (NodeFlex x) = Just x isFlexNode _ = Nothing isZeroNode :: Node rigid flex -> Bool isZeroNode NodeZero{} = True isZeroNode _ = False isInftyNode :: Node rigid flex -> Bool isInftyNode NodeInfty{} = True isInftyNode _ = False nodeToSizeExpr :: Node rigid flex -> SizeExpr' rigid flex nodeToSizeExpr n = case n of NodeZero -> Const 0 NodeInfty -> Infty NodeRigid i -> Rigid i 0 NodeFlex x -> Flex x 0 -- ** Edges -- | An edge is negative if its label is. instance Negative a => Negative (Edge' r f a) where negative = negative . label -- instance Show a => Show (Edge' a) where -- show (Edge u v l) = show u ++ " -(" ++ show l ++ ")-> " ++ show v instance (Ord r, Ord f, MeetSemiLattice a) => MeetSemiLattice (Edge' r f a) where e@(Edge u v l) `meet` e'@(Edge u' v' l') | u == u' && v == v' = Edge u v $ l `meet` l' | otherwise = __IMPOSSIBLE__ -- error $ show e ++ " `meet` " ++ show e' instance (Ord r, Ord f, Top a) => Top (Edge' r f a) where top = __IMPOSSIBLE__ isTop e = isTop (label e) instance (Ord r, Ord f, Dioid a) => Dioid (Edge' r f a) where e@(Edge u v l) `compose` e'@(Edge v' w l') | v == v' = Edge u w $ l `compose` l' | otherwise = __IMPOSSIBLE__ -- error $ show e ++ " `compose` " ++ show e' unitCompose = __IMPOSSIBLE__ -- ** Graphs -- | A graph forest. type Graphs r f a = [Graph r f a] emptyGraphs :: Graphs r f a emptyGraphs = [] -- | Split a list of graphs @gs@ into those that mention node @n@ and those that do not. -- If @n@ is zero or infinity, we regard it as "not mentioned". mentions :: (Ord r, Ord f) => Node r f -> Graphs r f a -> (Graphs r f a, Graphs r f a) mentions NodeZero gs = ([], gs) mentions NodeInfty gs = ([], gs) mentions NodeRigid{} gs = ([], gs) mentions n gs = List.partition (Set.member n . nodes) gs -- | Add an edge to a graph forest. -- Graphs that share a node with the edge are joined. addEdge :: (Ord r, Ord f, MeetSemiLattice a, Top a) => Edge' r f a -> Graphs r f a -> Graphs r f a addEdge e@(Edge src dest l) gs = -- Note: If we started from an empty forest -- and only added edges via @addEdge@, then -- @gsSrc@ and @gsDest@ contain each at most one graph. let (gsSrc , gsNotSrc) = mentions src gs (gsDest, gsNotDest) = mentions dest gsNotSrc in insertEdge e (Graph.unionsWith meet $ gsSrc ++ gsDest) : gsNotDest -- | Reflexive closure. Add edges @0 -> n -> n -> oo@ for all nodes @n@. reflClos :: (Ord r, Ord f, Dioid a) => Set (Node r f) -> Graph r f a -> Graph r f a reflClos ns g = setFoldl step g ns' where -- have at least the nodes in @ns@ ns' = nodes g `Set.union` ns -- add the trivial edges for all nodes ns' step g n = foldl (flip insertEdge) g es where es = [ Edge NodeZero n unitCompose , Edge n n unitCompose , Edge n NodeInfty unitCompose ] -- UNUSED -- -- | Reflexive-transitive closure. -- complete :: (Pretty a, Dioid a) => Graph r f a -> Graph r f a -- complete = transClos . reflClos -- | A graph is 'negative' if it contains a negative loop (diagonal edge). -- Makes sense on transitive graphs. instance (Ord r, Ord f, Negative a) => Negative (Graph r f a) where negative = any negative . Graph.diagonal instance (Ord r, Ord f, Negative a) => Negative (Graphs r f a) where negative = any negative -- | @h `implies` g@ if any edge in @g@ between rigids and constants -- is implied by a corresponding edge in @h@, which means that -- the edge in @g@ carries at most the information of the one in @h@. -- -- Application: Constraint implication: Constraints are compatible -- with hypotheses. implies :: (Ord r, Ord f, Pretty r, Pretty f, Pretty a, Top a, Ord a, Negative a) => Graph r f a -> Graph r f a -> Bool -- iterate 'test' over all edges in g implies h g = and $ map test $ graphToList g -- NB: doing the @test k l@ before the recursive @b@ gives -- opportunity to short-cut the conjunction @&&@. where -- test :: Key -> a -> Bool test k@(Edge src dest l) | isZeroNode src, not (negative l) = True | isInftyNode dest = True | isJust $ isFlexNode src = True | isJust $ isFlexNode dest = True | isTop l = True | otherwise = case lookupEdge h src dest of Nothing -> False Just l' -> if l' <= l then True else trace ("edge " ++ prettyShow (l <$ k) ++ " not implied by " ++ prettyShow (l' <$ k)) $ False -- implies h g = Map.foldlWithKey (\ b k l -> test k l && b) True g -- -- NB: doing the @test k l@ before the recursive @b@ gives -- -- opportunity to short-cut the conjunction @&&@. -- where -- -- test :: Key -> a -> Bool -- test k@(Edge src dest ()) l -- | isZeroNode src, not (negative l) = True -- | isInftyNode dest = True -- | isJust $ isFlexNode src = True -- | isJust $ isFlexNode dest = True -- | isTop l = True -- | otherwise = case lookupEdge h src dest of -- Nothing -> False -- Just l' -> if l' <= l then True else -- trace ("edge " ++ show (l <$ k) ++ " not implied by " ++ show (l' <$ k)) $ -- False nodeFromSizeExpr :: SizeExpr' rigid flex -> (Node rigid flex, Offset) nodeFromSizeExpr e = case e of Const n -> (NodeZero , n) Rigid i n -> (NodeRigid i, n) Flex x n -> (NodeFlex x , n) Infty -> (NodeInfty , 0) edgeFromConstraint :: Constraint' rigid flex -> LabelledEdge rigid flex edgeFromConstraint (Constraint lexp cmp rexp) = let (leftNode , n) = nodeFromSizeExpr lexp (rightNode, m) = nodeFromSizeExpr rexp in Edge leftNode rightNode (Label cmp $ m - n) -- | Build a graph from list of simplified constraints. graphFromConstraints :: (Ord rigid, Ord flex) => [Constraint' rigid flex] -> Graph rigid flex Label graphFromConstraints cs = let -- convert to edges edges = map edgeFromConstraint cs -- build a graph from the edges g = foldl (flip insertEdge) Graph.empty edges in g -- | Build a graph from list of simplified constraints. graphsFromConstraints :: (Ord rigid, Ord flex) => [Constraint' rigid flex] -> Graphs rigid flex Label graphsFromConstraints cs = let -- convert to edges edges = map edgeFromConstraint cs -- get all the flexibles mentioned in constraints xs = Set.toList $ flexs cs -- for each flexible X, add edges 0 <= X and X <= oo fedges = concat $ forM xs $ \ x -> [ Edge NodeZero (NodeFlex x) (Label Le 0) , Edge (NodeFlex x) NodeInfty (Label Le 0) ] -- build a graph from the edges gs = foldl (flip addEdge) emptyGraphs (fedges ++ edges) in gs -- Build hypotheses graph, complete it, check for negative loops. type Hyp = Constraint type Hyp' = Constraint' type HypGraph r f = Graph r f Label hypGraph :: (Ord rigid, Ord flex, Pretty rigid, Pretty flex) => Set rigid -> [Hyp' rigid flex] -> Either String (HypGraph rigid flex) hypGraph is hyps0 = do -- get a list of hypothesis from a list of constraints hyps <- concat <$> mapM (simplify1 $ \ c -> return [c]) hyps0 let g = transClos $ reflClos (Set.mapMonotonic NodeRigid is) $ graphFromConstraints hyps when (negative g) $ Left "size hypotheses graph has negative loop" return g hypConn :: (Ord r, Ord f) => HypGraph r f -> Node r f -> Node r f -> Label -- hypConn hg NodeZero n2 = Label Le 0 -- WRONG: not the best information -- hypConn hg n1 NodeInfty = Label Le 0 hypConn hg n1 n2 | n1 == n2 = Label Le 0 | Just l <- lookupEdge hg n1 n2 = l | otherwise = top simplifyWithHypotheses :: (Ord rigid, Ord flex, Pretty rigid, Pretty flex) => HypGraph rigid flex -> [Constraint' rigid flex] -> Either String [Constraint' rigid flex] simplifyWithHypotheses hg cons = concat <$> mapM (simplify1 test) cons where -- Test whether a constraint is compatible with the hypotheses: -- Succeeds, if constraint is implied by hypotheses, -- fails otherwise. test c = do let Edge n1 n2 l = edgeFromConstraint c l' = hypConn hg n1 n2 -- l' <- lookupEdge hg n1 n2 unless (l' <= l) $ Left $ "size constraint " ++ prettyShow c ++ " not consistent with size hypotheses" return [c] -- if (l' <= l) then Just [c] else Nothing -- Build constraint graph, complete it, check for negative loops. -- Check that hypotheses graph implies constraint graphs (rigids). type ConGraph r f = Graph r f Label constraintGraph :: (Ord r, Ord f, Pretty r, Pretty f) => [Constraint' r f] -> HypGraph r f -> Either String (ConGraph r f) constraintGraph cons0 hg = do traceM $ "original constraints cons0 = " ++ prettyShow cons0 -- Simplify constraints, ensure they are locally consistent with -- hypotheses. cons <- simplifyWithHypotheses hg cons0 traceM $ "simplified constraints cons = " ++ prettyShow cons -- Build a transitive graph from constraints. let g = transClos $ graphFromConstraints cons traceM $ "transitive graph g = " ++ prettyShow (graphToList g) -- Ensure it has no negative loops. when (negative g) $ Left $ "size constraint graph has negative loops" -- Ensure it does not constrain the hypotheses. unless (hg `implies` g) $ Left $ "size constraint graph constrains size hypotheses" return g type ConGraphs r f = Graphs r f Label constraintGraphs :: (Ord r, Ord f, Pretty r, Pretty f) => [Constraint' r f] -> HypGraph r f -> Either String ([f], ConGraphs r f) constraintGraphs cons0 hg = do traceM $ "original constraints cons0 = " ++ prettyShow cons0 -- Simplify constraints, ensure they are locally consistent with -- hypotheses. cons <- simplifyWithHypotheses hg cons0 traceM $ "simplified constraints cons = " ++ prettyShow cons -- Build a transitive graph forest from constraints. let gs0 = graphsFromConstraints cons traceM $ "constraint forest gs0 = " ++ prettyShow (map graphToList gs0) let gs1 = map transClos gs0 traceM $ "transitive forest gs1 = " ++ prettyShow (map graphToList gs1) -- Check for flexibles to be set to infinity let (xss,gs) = unzip $ map infinityFlexs gs1 xs = concat xss unless (null xs) $ do traceM $ "flexibles to set to oo = " ++ prettyShow xs traceM $ "forest after oo-subst = " ++ prettyShow (map graphToList gs) -- Ensure none has negative loops. when (negative gs) $ Left $ "size constraint graph has negative loop" traceM $ "we are free of negative loops" -- Ensure it does not constrain the hypotheses. forM_ gs $ \ g -> unless (hg `implies` g) $ Left $ "size constraint graph constrains size hypotheses" traceM $ "any constraint between rigids is implied by the hypotheses" return (xs, gs) -- | If we have an edge @X + n <= X@ (with n >= 0), we must set @X = oo@. infinityFlexs :: (Ord r, Ord f) => ConGraph r f -> ([f], ConGraph r f) infinityFlexs g = (infFlexs, setToInfty infFlexs g) where -- get the flexibles that need to be set to infinity infFlexs = mapMaybe flexNeg $ Graph.diagonal g flexNeg e = do guard $ negative e isFlexNode (src e) class SetToInfty f a where setToInfty :: [f] -> a -> a instance (Eq f) => SetToInfty f (Node r f) where setToInfty xs (NodeFlex x) | x `elem` xs = NodeInfty setToInfty xs n = n instance (Eq f) => SetToInfty f (Edge' r f a) where setToInfty xs (Edge n1 n2 l) = Edge (setToInfty xs n1) (setToInfty xs n2) l instance (Ord r, Ord f) => SetToInfty f (ConGraph r f) where setToInfty xs = graphFromList . filter h . map (setToInfty xs) . graphToList where -- filter out edges @oo + k <= oo@ h (Edge NodeInfty NodeInfty (Label Le _)) = False h _ = True -- * Compute solution from constraint graph. instance Plus Offset Weight Weight where plus e Infinity = Infinity plus e (Offset x) = Offset $ plus e x instance Plus (SizeExpr' r f) Weight (SizeExpr' r f) where plus e Infinity = Infty plus e (Offset x) = plus e x instance Plus (SizeExpr' r f) Label (SizeExpr' r f) where plus e l = plus e (toWeight l) -- | Lower or upper bound for a flexible variable type Bound r f = Map f (Set (SizeExpr' r f)) emptyBound :: Bound r f emptyBound = Map.empty data Bounds r f = Bounds { lowerBounds :: Bound r f , upperBounds :: Bound r f , mustBeFinite :: Set f -- ^ These metas are < ∞. } -- | Compute a lower bound for a flexible from an edge. edgeToLowerBound :: LabelledEdge r f -> Maybe (f, SizeExpr' r f) edgeToLowerBound e = case e of (Edge n1 n2 LInf) -> __IMPOSSIBLE__ (Edge NodeZero (NodeFlex x) (Label Le o)) | o >= 0 -> Just (x, Const 0) (Edge NodeZero (NodeFlex x) (Label Lt o)) | o >= 1 -> Just (x, Const 0) (Edge n1 (NodeFlex x) l) -> Just (x, nodeToSizeExpr n1 `plus` (- (toWeight l))) _ -> Nothing -- | Compute an upper bound for a flexible from an edge. edgeToUpperBound :: LabelledEdge r f -> Maybe (f, Cmp, SizeExpr' r f) edgeToUpperBound e = case e of (Edge n1 n2 LInf) -> __IMPOSSIBLE__ (Edge n1 NodeInfty (Label Le _)) -> Nothing (Edge (NodeFlex x) NodeInfty (Label Lt _)) -> Just (x, Lt, Infty) (Edge (NodeFlex x) n2 l ) -> Just (x, Le, nodeToSizeExpr n2 `plus` (toWeight l)) _ -> Nothing -- | Compute the lower bounds for all flexibles in a graph. graphToLowerBounds :: (Ord r, Ord f) => [LabelledEdge r f] -> Bound r f graphToLowerBounds = flip foldl emptyBound $ \ bs e -> case edgeToLowerBound e of Nothing -> bs Just (x, Flex{}) -> bs -- ignore flexible bounds Just (x, a) -> Map.insertWith Set.union x (Set.singleton a) bs -- | Compute the upper bounds for all flexibles in a graph. graphToUpperBounds :: (Ord r, Ord f) => [LabelledEdge r f] -> (Bound r f, Set f) graphToUpperBounds = flip foldl (emptyBound, Set.empty) $ \ (bs, fs) e -> case edgeToUpperBound e of Nothing -> (bs, fs) Just (x, _, Flex{}) -> (bs, fs) -- ignore flexible bounds Just (x, Lt, Infty) -> (bs, Set.insert x fs) Just (x, Le, a) -> (Map.insertWith Set.union x (Set.singleton a) bs, fs) _ -> __IMPOSSIBLE__ -- | Compute the bounds for all flexibles in a graph. bounds :: (Ord r, Ord f) => ConGraph r f -> Bounds r f bounds g = Bounds lbs ubs fs where edges = graphToList g lbs = graphToLowerBounds edges (ubs, fs) = graphToUpperBounds edges -- | Compute the relative minima in a set of nodes (those that do not have -- a predecessor in the set). smallest ::(Ord r, Ord f) => HypGraph r f -> [Node r f] -> [Node r f] smallest hg ns | NodeZero `elem` ns = [NodeZero] | otherwise = filter hasNoPred ns where hasNoPred NodeInfty = False hasNoPred n = null $ mapMaybe strictEdge ns where -- is there an edge n -l-> n' with l <= 0 strictEdge n' = do guard (n /= n') -- exclude loops l <- lookupEdge hg n' n guard (toWeight l <= 0) return () -- | Compute the relative maxima in a set of nodes (those that do not have -- a successor in the set). largest ::(Ord r, Ord f) => HypGraph r f -> [Node r f] -> [Node r f] largest hg ns | NodeInfty `elem` ns = [NodeInfty] | otherwise = filter hasNoSucc ns where hasNoSucc NodeZero = False hasNoSucc n = null $ mapMaybe strictEdge ns where -- is there an edge n -l-> n' with l <= 0 strictEdge n' = do guard (n /= n') -- exclude loops l <- lookupEdge hg n n' guard (toWeight l <= 0) return () {-| Given source nodes n1,n2,... find all target nodes m1,m2, such that for all j, there are edges n_i --l_ij--> m_j for all i. Return these edges as a map from target notes to a list of edges. We assume the graph is reflexive-transitive. -} commonSuccs :: (Ord r, Ord f) => Graph r f a -> [Node r f] -> Map (Node r f) [Edge' r f a] commonSuccs hg srcs = intersectAll $ map (buildmap . outgoing hg) srcs where buildmap = Map.fromList . map (\ e -> (dest e, [e])) intersectAll [] = Map.empty intersectAll (m:ms) = foldl (Map.intersectionWith (++)) m ms {-| Given target nodes m1,m2,... find all source nodes n1,n2, such that for all j, there are edges n_i --l_ij--> m_j for all i. Return these edges as a map from target notes to a list of edges. We assume the graph is reflexive-transitive. -} commonPreds :: (Ord r, Ord f) => Graph r f a -> [Node r f] -> Map (Node r f) [Edge' r f a] commonPreds hg tgts = intersectAll $ map (buildmap . incoming hg) tgts where buildmap = Map.fromList . map (\ e -> (src e, [e])) intersectAll [] = Map.empty intersectAll (m:ms) = foldl (Map.intersectionWith (++)) m ms -- | Compute the sup of two different rigids or a rigid and a constant. lub' :: forall r f . (Ord r, Ord f, Pretty r, Pretty f, Show r, Show f) => HypGraph r f -> (Node r f, Offset) -> (Node r f, Offset) -> Maybe (SizeExpr' r f) lub' hg (node1, n) (node2, m) = do let sucs = commonSuccs hg [node1, node2] sucNodes = smallest hg $ Map.keys sucs traceM ("lub': sucs = " ++ show sucs) -- FIXME: prettyShow case sucNodes of -- there is a unique smallest common successor n0 of node1 and node2 [n0] -> do -- then there are exactly two edges node1 --l1--> n0 and node2 --l2--> n0 -- Andreas, 2017-04-28, issue #2558: The following invariant does not hold always -- -- with non-positive weights l1, l2 let es = fromMaybe __IMPOSSIBLE__ $ Map.lookup n0 sucs case es of [ Edge node1x n1 l1 , Edge node2x n2 l2 ] -> do unless (n0 == n1) __IMPOSSIBLE__ unless (n0 == n2) __IMPOSSIBLE__ unless (node1 == node1x) __IMPOSSIBLE__ unless (node2 == node2x) __IMPOSSIBLE__ -- Andreas, 2017-04-28, issue #2558: The following invariant does not hold always -- unless (toWeight l1 <= 0) __IMPOSSIBLE__ -- unless (toWeight l2 <= 0) __IMPOSSIBLE__ let o :: Weight o = max (n `plus` toWeight l1) (m `plus` toWeight l2) return $ nodeToSizeExpr n0 `plus` o _ -> __IMPOSSIBLE__ -- otherwise, we cannot compute the sup _ -> do let a1 :: SizeExpr' r f = nodeToSizeExpr node1 `plus` n let a2 :: SizeExpr' r f = nodeToSizeExpr node2 `plus` m traceM ("cannot compute lub of " ++ prettyShow a1 ++ " and " ++ prettyShow a2 ++ " because sucNodes = " ++ prettyShow sucNodes) Nothing -- | Compute the inf of two different rigids or a rigid and a constant. glb' :: forall r f . (Ord r, Ord f, Pretty r, Pretty f, Show r, Show f) => HypGraph r f -> (Node r f, Offset) -> (Node r f, Offset) -> Maybe (SizeExpr' r f) glb' hg (node1, n) (node2, m) = do let preds = commonPreds hg [node1, node2] predNodes = largest hg $ Map.keys preds traceM ("glb': preds = " ++ show preds) -- FIXME: prettyShow case predNodes of -- there is a unique greatest common predecessor n0 of node1 and node2 [n0] -> do -- then there are exactly two edges n0 --l1--> node1 and n0 --l2--> node2 -- Andreas, 2017-04-28, issue #2558: The following invariant may not hold always -- -- with non-positive weigths l1, l2 let es = fromMaybe __IMPOSSIBLE__ $ Map.lookup n0 preds case es of [ Edge n1 node1x l1 , Edge n2 node2x l2] -> do unless (n0 == n1) __IMPOSSIBLE__ unless (n0 == n2) __IMPOSSIBLE__ unless (node1 == node1x) __IMPOSSIBLE__ unless (node2 == node2x) __IMPOSSIBLE__ -- Andreas, 2017-04-28, issue #2558: The following invariant may not hold always -- unless (toWeight l1 <= 0) __IMPOSSIBLE__ -- unless (toWeight l2 <= 0) __IMPOSSIBLE__ let o :: Weight o = max (n `plus` toWeight l1) (m `plus` toWeight l2) return $ nodeToSizeExpr n0 `plus` o _ -> __IMPOSSIBLE__ -- otherwise, we cannot compute the sup _ -> do let a1 :: SizeExpr' r f = nodeToSizeExpr node1 `plus` n let a2 :: SizeExpr' r f = nodeToSizeExpr node2 `plus` m traceM ("cannot compute glb of " ++ prettyShow a1 ++ " and " ++ prettyShow a2 ++ " because predNodes = " ++ prettyShow predNodes) Nothing -- | Compute the least upper bound (sup). lub :: (Ord r, Ord f, Pretty r, Pretty f, Show r, Show f) => HypGraph r f -> SizeExpr' r f -> SizeExpr' r f -> Maybe (SizeExpr' r f) lub hg a1 a2 = case (a1, a2) of (Flex{}, _) -> __IMPOSSIBLE__ (_, Flex{}) -> __IMPOSSIBLE__ (Infty, a2) -> Just Infty (a1, Infty) -> Just Infty (Const n , Const m ) -> Just $ Const $ max n m (Const n , Rigid j m) | m >= n -> Just a2 | otherwise -> lub' hg (NodeZero, n) (NodeRigid j, m) (Rigid i n, Const m ) | n >= m -> Just a1 | otherwise -> lub' hg (NodeRigid i, n) (NodeZero, m) (Rigid i n, Rigid j m) | i == j -> Just $ Rigid i $ max n m | otherwise -> lub' hg (NodeRigid i, n) (NodeRigid j, m) {- Finding the glb of two rigid size expressions in hypotheses graph a1 = Rigid i n a2 = Rigid j m Find the topological predecessors of (NodeRigid i) Find the topological predecessors of (NodeRigid j) -} -- | Compute the greatest lower bound (inf) of size expressions relative -- to a hypotheses graph. glb :: (Ord r, Ord f, Pretty r, Pretty f, Show r, Show f) => HypGraph r f -> SizeExpr' r f -> SizeExpr' r f -> Maybe (SizeExpr' r f) glb hg a1 a2 = case (a1, a2) of (Flex{}, _) -> __IMPOSSIBLE__ (_, Flex{}) -> __IMPOSSIBLE__ (Infty, a2) -> Just a2 (a1, Infty) -> Just a1 (Const n , Const m ) -> Just $ Const $ min n m (Const n , Rigid i m) | n <= m -> Just a1 | otherwise -> glb' hg (NodeZero, n) (NodeRigid i, m) (Rigid i n, Const m ) | m <= n -> Just a2 | otherwise -> glb' hg (NodeRigid i, n) (NodeZero, m) (Rigid i n, Rigid j m) | i == j -> Just $ Rigid i $ min n m | otherwise -> glb' hg (NodeRigid i, n) (NodeRigid j, m) {- (Rigid i n, Rigid j m) -> do let iLeqj = Map.lookup (Edge (NodeRigid i) (NodeRigid j) ()) hg jLeqi = Map.lookup (Edge (NodeRigid j) (NodeRigid i) ()) hg case (iLeqj, jLeqi) of (Nothing, Nothing) -> Nothing -- maximum as size expression (Just l, Nothing) | Offset k <- toWeight l -> if k + n <= m then Just a1 else Nothing -- no guaranteed infimum (Nothing, Just l) | Offset k <- toWeight l -> if k + m <= n then Just a2 else Nothing (Just{}, Just{}) -> Nothing {- let lbi = incoming hg (NodeRigid i) lbj = incoming hg (NodeRigid j) srci = Set.fromList $ map src lbi srcj = Set.fromList $ map src lbj srcs = Set.intersection srci srcj -} _ -> trace ("cannot compute glb of " ++ prettyShow a1 ++ " and " ++ prettyShow a2) $ Nothing -- TODO! -} findRigidBelow :: (Ord r, Ord f) => HypGraph r f -> (SizeExpr' r f) -> Maybe (SizeExpr' r f) findRigidBelow hg (Rigid i m) | m < 0 = do let v = NodeRigid i preds = incoming hg v filt e@(Edge n n' l) | n' == v = case toWeight l of Infinity -> Nothing Offset o -> if o <= m then Just (n, o) else Nothing | otherwise = __IMPOSSIBLE__ -- error $ "findRigidBelow: impossible: " ++ prettyShow e cands = mapMaybe filt preds (n, o) <- do case cands of [] -> Nothing [c] -> return c _ -> return $ List.maximumBy (compare `on` snd) $ filter ((NodeZero /=) . fst) cands let offset = m - o unless (offset >= 0) __IMPOSSIBLE__ return $ nodeToSizeExpr n `plus` offset findRigidBelow hg e = __IMPOSSIBLE__ -- error $ "findRigidBelow: impossible: " ++ prettyShow e solveGraph :: (Ord r, Ord f, Pretty r, Pretty f, Show r, Show f) => Polarities f -> HypGraph r f -> ConGraph r f -> Either String (Solution r f) solveGraph pols hg g = do let (Bounds lbs ubs fs) = bounds g -- flexibles to solve for xs = Set.toAscList $ Set.unions [ Map.keysSet lbs, Map.keysSet ubs, fs ] -- iterate over all flexible variables xas <- catMaybes <$> do forM xs $ \ x -> do -- get lower and upper bounds for flexible x let lx = Set.toList $ Map.findWithDefault Set.empty x lbs ux = Set.toList $ Map.findWithDefault Set.empty x ubs traceM ("lower bounds for " ++ prettyShow x ++ ": " ++ prettyShow lx) traceM ("upper bounds for " ++ prettyShow x ++ ": " ++ prettyShow ux) -- compute maximum of lower bounds lb <- do case lx of [] -> return $ Nothing (a:as) -> do case foldM (lub hg) a as of Nothing -> Left $ "inconsistent lower bound for " ++ prettyShow x Just l -> return $ Just $ truncateOffset l -- compute minimum of upper bounds ub <- do case ux of [] -> return $ Nothing (a:as) -> do case foldM (glb hg) a as of Just l | validOffset l -> return $ Just l | Just l' <- findRigidBelow hg l -> return $ Just l' _ -> Left $ "inconsistent upper bound for " ++ prettyShow x case (lb, ub) of (Just l, Nothing) -> return $ Just (x, l) -- solve x = lower bound (Nothing, Just u) -> return $ Just (x, u) -- solve x = upper bound (Just l, Just u) -> do traceM ("lower bound for " ++ prettyShow x ++ ": " ++ prettyShow l) traceM ("upper bound for " ++ prettyShow x ++ ": " ++ prettyShow u) case getPolarity pols x of Least -> return $ Just (x, l) Greatest -> return $ Just (x, u) _ -> return Nothing return $ Solution $ Map.fromList xas -- | Solve a forest of constraint graphs relative to a hypotheses graph. -- Concatenate individual solutions. solveGraphs :: (Ord r, Ord f, Pretty r, Pretty f, Show r, Show f) => Polarities f -> HypGraph r f -> ConGraphs r f -> Either String (Solution r f) solveGraphs pols hg gs = Solution . Map.unions <$> mapM (theSolution <.> solveGraph pols hg) gs -- * Verify solution -- | Check that after substitution of the solution, -- constraints are implied by hypotheses. verifySolution :: (Ord r, Ord f, Pretty r, Pretty f, Show r, Show f) => HypGraph r f -> [Constraint' r f] -> Solution r f -> Either String () verifySolution hg cs sol = do cs <- return $ subst sol cs traceM $ "substituted constraints " ++ prettyShow cs cs <- -- maybe (Left "solution produces inconsistency") Right $ concat <$> mapM (simplify1 $ \ c -> return [c]) cs traceM $ "simplified substituted constraints " ++ prettyShow cs -- cs <- maybe (Left "solution produces inconsistency") Right $ -- simplifyWithHypotheses hg cs let g = graphFromConstraints cs unless (hg `implies` g) $ Left "solution not implied by hypotheses" {- case simplifyWithHypotheses hg $ subst sol cs of Nothing -> Left "solution produces inconsistency" Just [] -> Right () Just cs -> Left $ "solution leaves constraints " ++ prettyShow cs -} -- | Iterate solver until no more metas can be solved. -- -- This might trigger a (wanted) error on the second iteration (see Issue 2096) -- which would otherwise go unnoticed. iterateSolver :: (Ord r, Ord f, Pretty r, Pretty f, Show r, Show f) => Polarities f -- ^ Meta variable polarities (prefer lower or upper solution?). -> HypGraph r f -- ^ Hypotheses (assumed to have no metas, so, fixed during iteration). -> [Constraint' r f] -- ^ Constraints to solve. -> Solution r f -- ^ Previous substitution (already applied to constraints). -> Either String (Solution r f) -- ^ Accumulated substition. iterateSolver pols hg cs sol0 = do g <- constraintGraph cs hg sol <- solveGraph pols hg g traceM $ "(partial) solution " ++ prettyShow sol if null sol then return sol0 else iterateSolver pols hg (subst sol cs) $ Solution $ Map.unionWith __IMPOSSIBLE__ (theSolution sol) $ theSolution $ subst sol sol0 -- * Tests testSuccs :: Ord f => Map (Node [Char] f) [Edge' [Char] f Label] testSuccs = commonSuccs hg [n1,n2] where n1 = NodeRigid "i" n2 = NodeRigid "j" n3 = NodeRigid "k" n4 = NodeRigid "l" n5 = NodeRigid "m" hg = Graph.fromList [ Graph.Edge n1 n3 $ Label Le 1 , Graph.Edge n1 n4 $ Label Le 2 , Graph.Edge n1 n5 $ Label Le 3 , Graph.Edge n2 n3 $ Label Le 4 , Graph.Edge n2 n4 $ Label Le 5 , Graph.Edge n2 n5 $ Label Le 6 ] -- testLub = smallest hg $ Map.keys $ commonSuccs hg [n1,n2] -- testLub :: (Pretty f, Ord f, Show f) => Maybe (SizeExpr' [Char] f) testLub = lub hg (Rigid "i" 0) (Rigid "j" 2) where n1 = NodeRigid "i" n2 = NodeRigid "j" n3 = NodeRigid "k" n4 = NodeRigid "l" n5 = NodeRigid "m" hg = Graph.fromList [ Graph.Edge n1 n3 $ Label Le 0 , Graph.Edge n1 n4 $ Label Le 2 , Graph.Edge n1 n5 $ Label Le 4 , Graph.Edge n2 n3 $ Label Le 1 , Graph.Edge n2 n4 $ Label Le 3 , Graph.Edge n2 n5 $ Label Le 5 , Graph.Edge n3 n4 $ Label Le 0 , Graph.Edge n3 n5 $ Label Lt 0 ] Agda-2.5.3/src/full/Agda/TypeChecking/Free/0000755000000000000000000000000013154613124016366 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Free/Lazy.hs0000644000000000000000000002616713154613124017655 0ustar0000000000000000{-# LANGUAGE UndecidableInstances #-} -- | Computing the free variables of a term lazily. -- -- We implement a reduce (traversal into monoid) over internal syntax -- for a generic collection (monoid with singletons). This should allow -- a more efficient test for the presence of a particular variable. -- -- Worst-case complexity does not change (i.e. the case when a variable -- does not occur), but best case-complexity does matter. For instance, -- see 'Agda.TypeChecking.Substitute.mkAbs': each time we construct -- a dependent function type, we check it is actually dependent. -- -- The distinction between rigid and strongly rigid occurrences comes from: -- Jason C. Reed, PhD thesis, 2009, page 96 (see also his LFMTP 2009 paper) -- -- The main idea is that x = t(x) is unsolvable if x occurs strongly rigidly -- in t. It might have a solution if the occurrence is not strongly rigid, e.g. -- -- x = \f -> suc (f (x (\ y -> k))) has x = \f -> suc (f (suc k)) -- -- [Jason C. Reed, PhD thesis, page 106] -- -- Under coinductive constructors, occurrences are never strongly rigid. -- Also, function types and lambdas do not establish strong rigidity. -- Only inductive constructors do so. -- (See issue 1271). module Agda.TypeChecking.Free.Lazy where import Control.Applicative hiding (empty) import Control.Monad.Reader import Data.Foldable (foldMap) import Data.IntMap (IntMap) import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend, mconcat) import Data.Set (Set) import Agda.Syntax.Common import Agda.Syntax.Internal -- import Agda.TypeChecking.Irrelevance import Agda.Utils.Functor import Agda.Utils.Monad import Agda.Utils.Singleton import Agda.Utils.Size type MetaSet = Set MetaId -- | Depending on the surrounding context of a variable, -- it's occurrence can be classified as flexible or rigid, -- with finer distinctions. -- -- The constructors are listed in increasing order (wrt. information content). data FlexRig = Flexible MetaSet -- ^ In arguments of metas. | WeaklyRigid -- ^ In arguments to variables and definitions. | Unguarded -- ^ In top position, or only under inductive record constructors. | StronglyRigid -- ^ Under at least one and only inductive constructors. deriving (Eq, Ord, Show) -- | 'FlexRig' composition. For accumulating the context of a variable. -- -- 'Flexible' is dominant. Once we are under a meta, we are flexible -- regardless what else comes. -- -- 'WeaklyRigid' is next in strength. Destroys strong rigidity. -- -- 'StronglyRigid' is still dominant over 'Unguarded'. -- -- 'Unguarded' is the unit. It is the top (identity) context. composeFlexRig :: FlexRig -> FlexRig -> FlexRig composeFlexRig o o' = case (o, o') of (Flexible ms1, Flexible ms2) -> Flexible $ ms1 `mappend` ms2 (Flexible ms1, _) -> Flexible ms1 (_, Flexible ms2) -> Flexible ms2 (WeaklyRigid, _) -> WeaklyRigid (_, WeaklyRigid) -> WeaklyRigid (StronglyRigid, _) -> StronglyRigid (_, StronglyRigid) -> StronglyRigid (Unguarded, Unguarded) -> Unguarded -- -- | 'FlexRig' supremum. Extract the most information about a variable. -- -- -- -- We make this the default 'Monoid' for 'FlexRig'. -- instance Monoid FlexRig where -- mempty = minBound -- mappend = max -- | Occurrence of free variables is classified by several dimensions. -- Currently, we have 'FlexRig' and 'Relevance'. data VarOcc = VarOcc { varFlexRig :: FlexRig , varRelevance :: Relevance } deriving (Eq, Show) -- | When we extract information about occurrence, we care most about -- about 'StronglyRigid' 'Relevant' occurrences. maxVarOcc :: VarOcc -> VarOcc -> VarOcc maxVarOcc (VarOcc o r) (VarOcc o' r') = VarOcc (max o o') (min r r') topVarOcc :: VarOcc topVarOcc = VarOcc StronglyRigid Relevant botVarOcc :: VarOcc botVarOcc = VarOcc (Flexible mempty) Irrelevant -- | First argument is the outer occurrence and second is the inner. composeVarOcc :: VarOcc -> VarOcc -> VarOcc composeVarOcc (VarOcc o r) (VarOcc o' r') = VarOcc (composeFlexRig o o') (max r r') -- | Any representation of a set of variables need to be able to be modified by -- a variable occurrence. This is to ensure that free variable analysis is -- compositional. For instance, it should be possible to compute `fv (v [u/x])` -- from `fv v` and `fv u`. class (Semigroup a, Monoid a) => IsVarSet a where -- | Laws -- * Respects monoid operations: -- ``` -- withVarOcc o mempty == mempty -- withVarOcc o (x <> y) == withVarOcc o x <> withVarOcc o y -- ``` -- * Respects VarOcc composition -- ``` -- withVarOcc (composeVarOcc o1 o2) = withVarOcc o1 . withVarOcc o2 -- ``` withVarOcc :: VarOcc -> a -> a type VarMap = IntMap VarOcc instance IsVarSet VarMap where withVarOcc o = fmap (composeVarOcc o) -- * Collecting free variables. -- | Where should we skip sorts in free variable analysis? data IgnoreSorts = IgnoreNot -- ^ Do not skip. | IgnoreInAnnotations -- ^ Skip when annotation to a type. | IgnoreAll -- ^ Skip unconditionally. deriving (Eq, Show) -- | The current context. data FreeEnv c = FreeEnv { feIgnoreSorts :: !IgnoreSorts -- ^ Ignore free variables in sorts. , feFlexRig :: !FlexRig -- ^ Are we flexible or rigid? , feRelevance :: !Relevance -- ^ What is the current relevance? , feSingleton :: Maybe Variable -> c -- ^ Method to return a single variable. } type Variable = Int type SingleVar c = Variable -> c -- | The initial context. initFreeEnv :: Monoid c => SingleVar c -> FreeEnv c initFreeEnv sing = FreeEnv { feIgnoreSorts = IgnoreNot , feFlexRig = Unguarded , feRelevance = Relevant , feSingleton = maybe mempty sing } type FreeM c = Reader (FreeEnv c) c -- | Run function for FreeM. runFreeM :: IsVarSet c => SingleVar c -> IgnoreSorts -> FreeM c -> c runFreeM single i m = runReader m $ (initFreeEnv single) { feIgnoreSorts = i } instance Semigroup c => Semigroup (FreeM c) where (<>) = liftA2 (<>) instance (Semigroup c, Monoid c) => Monoid (FreeM c) where mempty = pure mempty mappend = (<>) mconcat = mconcat <.> sequence -- instance Singleton a c => Singleton a (FreeM c) where -- singleton = pure . singleton -- | Base case: a variable. variable :: IsVarSet c => Int -> FreeM c variable n = do o <- asks feFlexRig r <- asks feRelevance s <- asks feSingleton pure $ withVarOcc (VarOcc o r) (s $ Just n) -- | Subtract, but return Nothing if result is negative. subVar :: Int -> Maybe Variable -> Maybe Variable subVar n x = x >>= \ i -> (i - n) <$ guard (n <= i) -- | Going under a binder. bind :: FreeM a -> FreeM a bind = bind' 1 bind' :: Nat -> FreeM a -> FreeM a bind' n = local $ \ e -> e { feSingleton = feSingleton e . subVar n } -- | Changing the 'FlexRig' context. go :: FlexRig -> FreeM a -> FreeM a go o = local $ \ e -> e { feFlexRig = composeFlexRig o $ feFlexRig e } -- | Changing the 'Relevance'. goRel :: Relevance-> FreeM a -> FreeM a goRel r = local $ \ e -> e { feRelevance = composeRelevance r $ feRelevance e } -- | What happens to the variables occurring under a constructor? underConstructor :: ConHead -> FreeM a -> FreeM a underConstructor (ConHead c i fs) = case (i,fs) of -- Coinductive (record) constructors admit infinite cycles: (CoInductive, _) -> go WeaklyRigid -- Inductive data constructors do not admit infinite cycles: (Inductive, []) -> go StronglyRigid -- Inductive record constructors do not admit infinite cycles, -- but this cannot be proven inside Agda. -- Thus, unification should not prove it either. (Inductive, (_:_)) -> id -- | Gather free variables in a collection. class Free a where -- Misplaced SPECIALIZE pragma: -- {-# SPECIALIZE freeVars' :: a -> FreeM Any #-} -- So you cannot specialize all instances in one go. :( freeVars' :: IsVarSet c => a -> FreeM c instance Free Term where -- SPECIALIZE instance does not work as well, see -- https://ghc.haskell.org/trac/ghc/ticket/10434#ticket -- {-# SPECIALIZE instance Free Term All #-} -- {-# SPECIALIZE freeVars' :: Term -> FreeM Any #-} -- {-# SPECIALIZE freeVars' :: Term -> FreeM All #-} -- {-# SPECIALIZE freeVars' :: Term -> FreeM VarSet #-} freeVars' t = case t of Var n ts -> variable n `mappend` do go WeaklyRigid $ freeVars' ts -- λ is not considered guarding, as -- we cannot prove that x ≡ λy.x is impossible. Lam _ t -> freeVars' t Lit _ -> mempty Def _ ts -> go WeaklyRigid $ freeVars' ts -- because we are not in TCM -- we cannot query whether we are dealing with a data/record (strongly r.) -- or a definition by pattern matching (weakly rigid) -- thus, we approximate, losing that x = List x is unsolvable Con c _ ts -> underConstructor c $ freeVars' ts -- Pi is not guarding, since we cannot prove that A ≡ B → A is impossible. -- Even as we do not permit infinite type expressions, -- we cannot prove their absence (as Set is not inductive). -- Also, this is incompatible with univalence (HoTT). Pi a b -> freeVars' (a,b) Sort s -> freeVars' s Level l -> freeVars' l MetaV m ts -> go (Flexible $ singleton m) $ freeVars' ts DontCare mt -> goRel Irrelevant $ freeVars' mt Shared p -> freeVars' (derefPtr p) instance Free a => Free (Type' a) where freeVars' (El s t) = ifM ((IgnoreNot ==) <$> asks feIgnoreSorts) {- then -} (freeVars' (s, t)) {- else -} (freeVars' t) instance Free Sort where freeVars' s = ifM ((IgnoreAll ==) <$> asks feIgnoreSorts) mempty $ {- else -} case s of Type a -> freeVars' a Prop -> mempty Inf -> mempty SizeUniv -> mempty DLub s1 s2 -> go WeaklyRigid $ freeVars' (s1, s2) instance Free Level where freeVars' (Max as) = freeVars' as instance Free PlusLevel where freeVars' ClosedLevel{} = mempty freeVars' (Plus _ l) = freeVars' l instance Free LevelAtom where freeVars' l = case l of MetaLevel m vs -> go (Flexible $ singleton m) $ freeVars' vs NeutralLevel _ v -> freeVars' v BlockedLevel _ v -> freeVars' v UnreducedLevel v -> freeVars' v instance Free a => Free [a] where freeVars' = foldMap freeVars' instance Free a => Free (Maybe a) where freeVars' = foldMap freeVars' instance (Free a, Free b) => Free (a, b) where freeVars' (x,y) = freeVars' x `mappend` freeVars' y instance Free a => Free (Elim' a) where freeVars' (Apply a) = freeVars' a freeVars' (Proj{} ) = mempty instance Free a => Free (Arg a) where freeVars' a = goRel (getRelevance a) $ freeVars' $ unArg a instance Free a => Free (Dom a) where freeVars' = freeVars' . unDom instance Free a => Free (Abs a) where freeVars' (Abs _ b) = bind $ freeVars' b freeVars' (NoAbs _ b) = freeVars' b instance Free a => Free (Tele a) where freeVars' EmptyTel = mempty freeVars' (ExtendTel a tel) = freeVars' (a, tel) instance Free Clause where freeVars' cl = bind' (size $ clauseTel cl) $ freeVars' $ clauseBody cl instance Free EqualityView where freeVars' (OtherType t) = freeVars' t freeVars' (EqualityType s _eq l t a b) = freeVars' s `mappend` freeVars' (l ++ [t, a, b]) Agda-2.5.3/src/full/Agda/TypeChecking/Free/Old.hs0000644000000000000000000002776513154613124017461 0ustar0000000000000000 -- | Computing the free variables of a term. -- -- This is the old version of ''Agda.TypeChecking.Free'', using -- 'IntSet's for the separate variable categories. -- We keep it as a specification. -- -- The distinction between rigid and strongly rigid occurrences comes from: -- Jason C. Reed, PhD thesis, 2009, page 96 (see also his LFMTP 2009 paper) -- -- The main idea is that x = t(x) is unsolvable if x occurs strongly rigidly -- in t. It might have a solution if the occurrence is not strongly rigid, e.g. -- -- x = \f -> suc (f (x (\ y -> k))) has x = \f -> suc (f (suc k)) -- -- [Jason C. Reed, PhD thesis, page 106] -- -- Under coinductive constructors, occurrences are never strongly rigid. -- Also, function types and lambdas do not establish strong rigidity. -- Only inductive constructors do so. -- (See issue 1271). module Agda.TypeChecking.Free.Old ( FreeVars(..) , Free , IgnoreSorts(..) , freeVars , freeVarsIgnore , allVars , relevantVars , rigidVars , freeIn, isBinderUsed , freeInIgnoringSorts, freeInIgnoringSortAnn , relevantIn, relevantInIgnoringSortAnn , Occurrence(..) , occurrence ) where import Control.Applicative hiding (empty) import Control.Monad.Reader import Data.Foldable (foldMap) import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend, mconcat) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Utils.Functor import Agda.Utils.Monad import Agda.Utils.Size import Agda.Utils.VarSet (VarSet) import qualified Agda.Utils.VarSet as Set -- | Free variables of a term, (disjointly) partitioned into strongly and -- and weakly rigid variables, flexible variables and irrelevant variables. data FreeVars = FV { stronglyRigidVars :: VarSet -- ^ Variables under only and at least one inductive constructor(s). , unguardedVars :: VarSet -- ^ Variables at top or only under inductive record constructors -- λs and Πs. -- The purpose of recording these separately is that they -- can still become strongly rigid if put under a constructor -- whereas weakly rigid ones stay weakly rigid. , weaklyRigidVars :: VarSet -- ^ Ordinary rigid variables, e.g., in arguments of variables. , flexibleVars :: VarSet -- ^ Variables occuring in arguments of metas. -- These are only potentially free, depending how the meta variable is instantiated. , irrelevantVars :: VarSet -- ^ Variables in irrelevant arguments and under a @DontCare@, i.e., -- in irrelevant positions. , unusedVars :: VarSet -- ^ Variables in 'UnusedArg'uments. } deriving (Eq, Show) -- | Rigid variables: either strongly rigid, unguarded, or weakly rigid. rigidVars :: FreeVars -> VarSet rigidVars fv = Set.unions [ stronglyRigidVars fv , unguardedVars fv , weaklyRigidVars fv ] -- | All but the irrelevant variables. relevantVars :: FreeVars -> VarSet relevantVars fv = Set.unions [rigidVars fv, flexibleVars fv] -- | @allVars fv@ includes irrelevant variables. allVars :: FreeVars -> VarSet allVars fv = Set.unions [relevantVars fv, irrelevantVars fv, unusedVars fv] data Occurrence = NoOccurrence | Irrelevantly | StronglyRigid -- ^ Under at least one and only inductive constructors. | Unguarded -- ^ In top position, or only under inductive record constructors. | WeaklyRigid -- ^ In arguments to variables and definitions. | Flexible -- ^ In arguments of metas. | Unused deriving (Eq,Show) {- NO LONGER -- | @occurrence x fv@ ignores irrelevant variables in @fv@ -} occurrence :: Nat -> FreeVars -> Occurrence occurrence x fv | x `Set.member` stronglyRigidVars fv = StronglyRigid | x `Set.member` unguardedVars fv = Unguarded | x `Set.member` weaklyRigidVars fv = WeaklyRigid | x `Set.member` flexibleVars fv = Flexible | x `Set.member` irrelevantVars fv = Irrelevantly | x `Set.member` unusedVars fv = Unused | otherwise = NoOccurrence -- | Mark variables as flexible. Useful when traversing arguments of metas. flexible :: FreeVars -> FreeVars flexible fv = fv { stronglyRigidVars = Set.empty , unguardedVars = Set.empty , weaklyRigidVars = Set.empty , flexibleVars = relevantVars fv } -- | Mark rigid variables as non-strongly. Useful when traversion arguments of variables. weakly :: FreeVars -> FreeVars weakly fv = fv { stronglyRigidVars = Set.empty , unguardedVars = Set.empty , weaklyRigidVars = rigidVars fv } -- | Mark unguarded variables as strongly rigid. Useful when traversion arguments of inductive constructors. strongly :: FreeVars -> FreeVars strongly fv = fv { stronglyRigidVars = stronglyRigidVars fv `Set.union` unguardedVars fv , unguardedVars = Set.empty } -- | What happens to the variables occurring under a constructor? underConstructor :: ConHead -> FreeVars -> FreeVars underConstructor (ConHead c i fs) = case (i,fs) of -- Coinductive (record) constructors admit infinite cycles: (CoInductive, _) -> weakly -- Inductive data constructors do not admit infinite cycles: (Inductive, []) -> strongly -- Inductive record constructors do not admit infinite cycles, -- but this cannot be proven inside Agda. -- Thus, unification should not prove it either. (Inductive, (_:_)) -> id -- | Mark all free variables as irrelevant. irrelevantly :: FreeVars -> FreeVars irrelevantly fv = empty { irrelevantVars = allVars fv } -- | Mark all free variables as unused, except for irrelevant vars. unused :: FreeVars -> FreeVars unused fv = empty { irrelevantVars = irrelevantVars fv , unusedVars = Set.unions [ rigidVars fv, flexibleVars fv, unusedVars fv ] } -- | Pointwise union. union :: FreeVars -> FreeVars -> FreeVars union (FV sv1 gv1 rv1 fv1 iv1 uv1) (FV sv2 gv2 rv2 fv2 iv2 uv2) = FV (Set.union sv1 sv2) (Set.union gv1 gv2) (Set.union rv1 rv2) (Set.union fv1 fv2) (Set.union iv1 iv2) (Set.union uv1 uv2) unions :: [FreeVars] -> FreeVars unions = foldr union empty empty :: FreeVars empty = FV Set.empty Set.empty Set.empty Set.empty Set.empty Set.empty -- | Free variable sets form a monoid under 'union'. instance Semigroup FreeVars where (<>) = union instance Monoid FreeVars where mempty = empty mappend = (<>) mconcat = unions -- | @delete x fv@ deletes variable @x@ from variable set @fv@. delete :: Nat -> FreeVars -> FreeVars delete n (FV sv gv rv fv iv uv) = FV (Set.delete n sv) (Set.delete n gv) (Set.delete n rv) (Set.delete n fv) (Set.delete n iv) (Set.delete n uv) -- | @subtractFV n fv@ subtracts $n$ from each free variable in @fv@. subtractFV :: Nat -> FreeVars -> FreeVars subtractFV n (FV sv gv rv fv iv uv) = FV (Set.subtract n sv) (Set.subtract n gv) (Set.subtract n rv) (Set.subtract n fv) (Set.subtract n iv) (Set.subtract n uv) -- | A single unguarded variable. singleton :: Nat -> FreeVars singleton x = empty { unguardedVars = Set.singleton x } -- * Collecting free variables. -- | Where should we skip sorts in free variable analysis? data IgnoreSorts = IgnoreNot -- ^ Do not skip. | IgnoreInAnnotations -- ^ Skip when annotation to a type. | IgnoreAll -- ^ Skip unconditionally. deriving (Eq, Show) data FreeConf = FreeConf { fcIgnoreSorts :: !IgnoreSorts -- ^ Ignore free variables in sorts. , fcContext :: !Int -- ^ Under how many binders have we stepped? } initFreeConf :: FreeConf initFreeConf = FreeConf { fcIgnoreSorts = IgnoreNot , fcContext = 0 } -- | Doesn't go inside solved metas, but collects the variables from a -- metavariable application @X ts@ as @flexibleVars@. freeVars :: Free a => a -> FreeVars freeVars t = freeVars' t `runReader` initFreeConf freeVarsIgnore :: Free a => IgnoreSorts -> a -> FreeVars freeVarsIgnore i t = freeVars' t `runReader` initFreeConf{ fcIgnoreSorts = i } -- | Return type of fold over syntax. type FreeT = Reader FreeConf FreeVars instance Semigroup FreeT where (<>) = liftA2 mappend instance Monoid FreeT where mempty = pure mempty mappend = (<>) mconcat = mconcat <.> sequence -- | Base case: a variable. variable :: Int -> FreeT variable n = do m <- (n -) <$> asks fcContext if m >= 0 then pure $ singleton m else mempty -- | Going under a binder. bind :: FreeT -> FreeT bind = bind' 1 -- | Going under n binders. bind' :: Nat -> FreeT -> FreeT bind' n = local $ \ e -> e { fcContext = n + fcContext e } class Free a where freeVars' :: a -> FreeT instance Free Term where freeVars' t = case t of Var n ts -> variable n `mappend` do weakly <$> freeVars' ts -- λ is not considered guarding, as -- we cannot prove that x ≡ λy.x is impossible. Lam _ t -> freeVars' t Lit _ -> mempty Def _ ts -> weakly <$> freeVars' ts -- because we are not in TCM -- we cannot query whether we are dealing with a data/record (strongly r.) -- or a definition by pattern matching (weakly rigid) -- thus, we approximate, losing that x = List x is unsolvable Con c _ ts -> underConstructor c <$> freeVars' ts -- Pi is not guarding, since we cannot prove that A ≡ B → A is impossible. -- Even as we do not permit infinite type expressions, -- we cannot prove their absence (as Set is not inductive). -- Also, this is incompatible with univalence (HoTT). Pi a b -> freeVars' (a,b) Sort s -> freeVars' s Level l -> freeVars' l MetaV _ ts -> flexible <$> freeVars' ts DontCare mt -> irrelevantly <$> freeVars' mt Shared p -> freeVars' (derefPtr p) instance Free Type where freeVars' (El s t) = ifM ((IgnoreNot ==) <$> asks fcIgnoreSorts) {- then -} (freeVars' (s, t)) {- else -} (freeVars' t) instance Free Sort where freeVars' s = ifM ((IgnoreAll ==) <$> asks fcIgnoreSorts) mempty $ {- else -} case s of Type a -> freeVars' a Prop -> mempty Inf -> mempty SizeUniv -> mempty DLub s1 s2 -> weakly <$> freeVars' (s1, s2) instance Free Level where freeVars' (Max as) = freeVars' as instance Free PlusLevel where freeVars' ClosedLevel{} = mempty freeVars' (Plus _ l) = freeVars' l instance Free LevelAtom where freeVars' l = case l of MetaLevel _ vs -> flexible <$> freeVars' vs NeutralLevel _ v -> freeVars' v BlockedLevel _ v -> freeVars' v UnreducedLevel v -> freeVars' v instance Free a => Free [a] where freeVars' = foldMap freeVars' instance Free a => Free (Maybe a) where freeVars' = foldMap freeVars' instance (Free a, Free b) => Free (a,b) where freeVars' (x,y) = freeVars' x `mappend` freeVars' y instance Free a => Free (Elim' a) where freeVars' (Apply a) = freeVars' a freeVars' (Proj{} ) = mempty instance Free a => Free (Arg a) where freeVars' a = f <$> freeVars' (unArg a) where f = case getRelevance a of Irrelevant -> irrelevantly _ -> id instance Free a => Free (Dom a) where freeVars' = freeVars' . unDom instance Free a => Free (Abs a) where freeVars' (Abs _ b) = bind $ freeVars' b freeVars' (NoAbs _ b) = freeVars' b instance Free a => Free (Tele a) where freeVars' EmptyTel = mempty freeVars' (ExtendTel a tel) = freeVars' (a, tel) instance Free Clause where freeVars' cl = bind' (size $ clauseTel cl) $ freeVars' $ clauseBody cl freeIn :: Free a => Nat -> a -> Bool freeIn v t = v `Set.member` allVars (freeVars t) freeInIgnoringSorts :: Free a => Nat -> a -> Bool freeInIgnoringSorts v t = v `Set.member` allVars (freeVarsIgnore IgnoreAll t) freeInIgnoringSortAnn :: Free a => Nat -> a -> Bool freeInIgnoringSortAnn v t = v `Set.member` allVars (freeVarsIgnore IgnoreInAnnotations t) relevantInIgnoringSortAnn :: Free a => Nat -> a -> Bool relevantInIgnoringSortAnn v t = v `Set.member` relevantVars (freeVarsIgnore IgnoreInAnnotations t) relevantIn :: Free a => Nat -> a -> Bool relevantIn v t = v `Set.member` relevantVars (freeVarsIgnore IgnoreAll t) -- | Is the variable bound by the abstraction actually used? isBinderUsed :: Free a => Abs a -> Bool isBinderUsed NoAbs{} = False isBinderUsed (Abs _ x) = 0 `freeIn` x Agda-2.5.3/src/full/Agda/TypeChecking/MetaVars/0000755000000000000000000000000013154613124017227 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/MetaVars/Occurs.hs0000644000000000000000000007123213154613124021026 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NoMonomorphismRestriction #-} {-# LANGUAGE NondecreasingIndentation #-} {-# LANGUAGE UndecidableInstances #-} {- | The occurs check for unification. Does pruning on the fly. When hitting a meta variable: - Compute flex/rigid for its arguments. - Compare to allowed variables. - Mark arguments with rigid occurrences of disallowed variables for deletion. - Attempt to delete marked arguments. - We don't need to check for success, we can just continue occurs checking. -} module Agda.TypeChecking.MetaVars.Occurs where import Control.Applicative import Control.Monad import Control.Monad.Reader import Data.Foldable (foldMap) import Data.Monoid import Data.Set (Set) import qualified Data.Set as Set import Data.Traversable (traverse) import qualified Agda.Benchmarking as Bench import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TypeChecking.Reduce import Agda.TypeChecking.Pretty import Agda.TypeChecking.Free hiding (Occurrence(..)) import Agda.TypeChecking.Substitute import Agda.TypeChecking.Datatypes import Agda.TypeChecking.Records import {-# SOURCE #-} Agda.TypeChecking.MetaVars -- import Agda.TypeChecking.MetaVars import Agda.Utils.Either import Agda.Utils.Except ( ExceptT , MonadError(catchError, throwError) , runExceptT ) import Agda.Utils.Lens import Agda.Utils.List (downFrom) import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Permutation import Agda.Utils.Pretty (prettyShow) import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible {- To address issue 585 (meta var occurrences in mutual defs) data B : Set where inn : A -> B out : B -> A out (inn a) = a postulate P : (y : A) (z : Unit -> B) → Set p : (x : Unit -> B) → P (out (x unit)) x mutual d : Unit -> B d unit = inn _ -- Y g : P (out (d unit)) d g = p _ -- X -- Agda solves d unit = inn (out (d unit)) -- -- out (X unit) = out (d unit) = out (inn Y) = Y -- X = d When doing the occurs check on d, we need to look at the definition of d to discover that it mentions X. To this end, we extend the state by names of definitions that have to be checked when they occur. At the beginning, this is initialized with the names in the current mutual block. Each time we encounter a name in the list during occurs check, we delete it (if check is successful). This way, we do not duplicate work. -} modifyOccursCheckDefs :: (Set QName -> Set QName) -> TCM () modifyOccursCheckDefs f = stOccursCheckDefs %= f -- | Set the names of definitions to be looked at -- to the defs in the current mutual block. initOccursCheck :: MetaVariable -> TCM () initOccursCheck mv = modifyOccursCheckDefs . const =<< if (miMetaOccursCheck (mvInfo mv) == DontRunMetaOccursCheck) then do reportSLn "tc.meta.occurs" 20 $ "initOccursCheck: we do not look into definitions" return Set.empty else do reportSLn "tc.meta.occurs" 20 $ "initOccursCheck: we look into the following definitions:" mb <- asks envMutualBlock case mb of Nothing -> do reportSLn "tc.meta.occurs" 20 $ "(none)" return Set.empty Just b -> do ds <- mutualNames <$> lookupMutualBlock b reportSDoc "tc.meta.occurs" 20 $ sep $ map prettyTCM $ Set.toList ds return ds -- | Is a def in the list of stuff to be checked? defNeedsChecking :: QName -> TCM Bool defNeedsChecking d = Set.member d <$> use stOccursCheckDefs -- | Remove a def from the list of defs to be looked at. tallyDef :: QName -> TCM () tallyDef d = modifyOccursCheckDefs $ \ s -> Set.delete d s data OccursCtx = Flex -- ^ We are in arguments of a meta. | Rigid -- ^ We are not in arguments of a meta but a bound var. | StronglyRigid -- ^ We are at the start or in the arguments of a constructor. | Top -- ^ We are at the term root (this turns into @StronglyRigid@). | Irrel -- ^ We are in an irrelevant argument. deriving (Eq, Show) data UnfoldStrategy = YesUnfold | NoUnfold deriving (Eq, Show) defArgs :: UnfoldStrategy -> OccursCtx -> OccursCtx defArgs NoUnfold _ = Flex defArgs YesUnfold ctx = weakly ctx unfold :: UnfoldStrategy -> Term -> TCM (Blocked Term) unfold NoUnfold v = notBlocked <$> instantiate v unfold YesUnfold v = reduceB v -- | Leave the top position. leaveTop :: OccursCtx -> OccursCtx leaveTop Top = StronglyRigid leaveTop ctx = ctx -- | Leave the strongly rigid position. weakly :: OccursCtx -> OccursCtx weakly Top = Rigid weakly StronglyRigid = Rigid weakly ctx = ctx strongly :: OccursCtx -> OccursCtx strongly Rigid = StronglyRigid strongly ctx = ctx patternViolation' :: Int -> String -> TCM a patternViolation' n err = do reportSLn "tc.meta.occurs" n err patternViolation abort :: OccursCtx -> TypeError -> TCM a abort Top err = typeError err abort StronglyRigid err = typeError err -- here, throw an uncatchable error (unsolvable constraint) abort Flex err = patternViolation' 70 (show err) -- throws a PatternErr, which leads to delayed constraint abort Rigid err = patternViolation' 70 (show err) abort Irrel err = patternViolation' 70 (show err) -- | Distinguish relevant and irrelevant variables in occurs check. type Vars = ([Nat],[Nat]) goIrrelevant :: Vars -> Vars goIrrelevant (relVs, irrVs) = (irrVs ++ relVs, []) allowedVar :: Nat -> Vars -> Bool allowedVar i (relVs, irrVs) = i `elem` relVs takeRelevant :: Vars -> [Nat] takeRelevant = fst liftUnderAbs :: Vars -> Vars liftUnderAbs (relVs, irrVs) = (0 : map (1+) relVs, map (1+) irrVs) -- | Extended occurs check. class Occurs t where occurs :: UnfoldStrategy -> OccursCtx -> MetaId -> Vars -> t -> TCM t metaOccurs :: MetaId -> t -> TCM () -- raise exception if meta occurs in t -- | When assigning @m xs := v@, check that @m@ does not occur in @v@ -- and that the free variables of @v@ are contained in @xs@. occursCheck :: (Occurs a, InstantiateFull a, PrettyTCM a) => MetaId -> Vars -> a -> TCM a occursCheck m xs v = disableDestructiveUpdate $ Bench.billTo [ Bench.Typing, Bench.OccursCheck ] $ do mv <- lookupMeta m initOccursCheck mv -- TODO: Can we do this in a better way? let redo m = m -- disableDestructiveUpdate m >> m -- First try without normalising the term redo (occurs NoUnfold Top m xs v) `catchError` \_ -> do initOccursCheck mv redo (occurs YesUnfold Top m xs v) `catchError` \err -> case err of -- Produce nicer error messages TypeError _ cl -> case clValue cl of MetaOccursInItself{} -> typeError . GenericError . show =<< fsep [ text ("Refuse to construct infinite term by instantiating " ++ prettyShow m ++ " to") , prettyTCM =<< instantiateFull v ] MetaCannotDependOn _ _ i -> ifM (isSortMeta m `and2M` (not <$> hasUniversePolymorphism)) ( typeError . GenericError . show =<< fsep [ text ("Cannot instantiate the metavariable " ++ prettyShow m ++ " to") , prettyTCM v , text "since universe polymorphism is disabled" ] ) {- else -} ( typeError . GenericError . show =<< fsep [ text ("Cannot instantiate the metavariable " ++ prettyShow m ++ " to solution") , prettyTCM v , text "since it contains the variable" , enterClosure cl $ \_ -> prettyTCM (Var i []) , text $ "which is not in scope of the metavariable or irrelevant in the metavariable but relevant in the solution" ] ) _ -> throwError err _ -> throwError err instance Occurs Term where occurs red ctx m xs v = do v <- unfold red v -- occurs' ctx $ ignoreBlocking v -- fails test/succeed/DontPruneBlocked case v of -- Don't fail on blocked terms or metas NotBlocked _ v -> occurs' ctx v -- Blocked _ v@MetaV{} -> occurs' ctx v -- does not help with issue 856 Blocked _ v -> occurs' Flex v where occurs' ctx v = do reportSDoc "tc.meta.occurs" 45 $ text ("occursCheck " ++ prettyShow m ++ " (" ++ show ctx ++ ") of ") <+> prettyTCM v reportSDoc "tc.meta.occurs" 70 $ nest 2 $ text $ show v case v of Var i es -> do if (i `allowedVar` xs) then Var i <$> occ (weakly ctx) es else do -- if the offending variable is of singleton type, -- eta-expand it away reportSDoc "tc.meta.occurs" 35 $ text "offending variable: " <+> prettyTCM (var i) t <- typeOfBV i reportSDoc "tc.meta.occurs" 35 $ nest 2 $ text "of type " <+> prettyTCM t isST <- isSingletonType t reportSDoc "tc.meta.occurs" 35 $ nest 2 $ text "(after singleton test)" case isST of -- cannot decide, blocked by meta-var Left mid -> patternViolation' 70 $ "Disallowed var " ++ show i ++ " not obviously singleton" -- not a singleton type Right Nothing -> -- abort Rigid turns this error into PatternErr abort (strongly ctx) $ MetaCannotDependOn m (takeRelevant xs) i -- is a singleton type with unique inhabitant sv Right (Just sv) -> return $ sv `applyE` es Lam h f -> Lam h <$> occ (leaveTop ctx) f Level l -> Level <$> occ ctx l -- stay in Top Lit l -> return v DontCare v -> dontCare <$> occurs red Irrel m (goIrrelevant xs) v Def d es -> Def d <$> occDef d (leaveTop ctx) es Con c ci vs -> Con c ci <$> occ (leaveTop ctx) vs -- if strongly rigid, remain so Pi a b -> uncurry Pi <$> occ (leaveTop ctx) (a,b) Sort s -> Sort <$> occ (leaveTop ctx) s v@Shared{} -> updateSharedTerm (occ ctx) v MetaV m' es -> do -- Check for loop -- don't fail hard on this, since we might still be on the top-level -- after some killing (Issue 442) -- -- Andreas, 2013-02-18 Issue 795 demonstrates that a recursive -- occurrence of a meta could be solved by the identity. -- ? (Q A) = Q (? A) -- So, do not throw an error. -- I guess the error was there from times when occurrence check -- was done after the "lhs=linear variables" check, but now -- occurrence check comes first. -- WAS: -- when (m == m') $ if ctx == Top then patternViolation else -- abort ctx $ MetaOccursInItself m' when (m == m') $ patternViolation' 50 $ "occursCheck failed: Found " ++ prettyShow m -- The arguments of a meta are in a flexible position (MetaV m' <$> occurs red Flex m xs es) `catchError` \err -> do reportSDoc "tc.meta.kill" 25 $ vcat [ text $ "error during flexible occurs check, we are " ++ show ctx , text $ show err ] case err of -- On pattern violations try to remove offending -- flexible occurrences (if not already in a flexible context) PatternErr{} | ctx /= Flex -> do reportSLn "tc.meta.kill" 20 $ "oops, pattern violation for " ++ prettyShow m' -- Andreas, 2014-03-02, see issue 1070: -- Do not prune when meta is projected! caseMaybe (allApplyElims es) (throwError err) $ \ vs -> do killResult <- prune m' vs (takeRelevant xs) if (killResult == PrunedEverything) -- after successful pruning, restart occurs check then occurs red ctx m xs =<< instantiate (MetaV m' es) else throwError err _ -> throwError err where occ ctx v = occurs red ctx m xs v -- a data or record type constructor propagates strong occurrences -- since e.g. x = List x is unsolvable occDef d ctx vs = do metaOccurs m d ifM (isJust <$> isDataOrRecordType d) {-then-} (occ ctx vs) {-else-} (occ (defArgs red ctx) vs) metaOccurs m v = do v <- instantiate v case v of Var i vs -> metaOccurs m vs Lam h f -> metaOccurs m f Level l -> metaOccurs m l Lit l -> return () DontCare v -> metaOccurs m v Def d vs -> metaOccurs m d >> metaOccurs m vs Con c _ vs -> metaOccurs m vs Pi a b -> metaOccurs m (a,b) Sort s -> metaOccurs m s Shared p -> metaOccurs m $ derefPtr p MetaV m' vs | m == m' -> patternViolation' 50 $ "Found occurrence of " ++ prettyShow m | otherwise -> metaOccurs m vs instance Occurs QName where occurs red ctx m xs d = __IMPOSSIBLE__ metaOccurs m d = whenM (defNeedsChecking d) $ do tallyDef d reportSLn "tc.meta.occurs" 30 $ "Checking for occurrences in " ++ show d metaOccurs m . theDef =<< ignoreAbstractMode (getConstInfo d) instance Occurs Defn where occurs red ctx m xs def = __IMPOSSIBLE__ metaOccurs m Axiom{} = return () metaOccurs m Function{ funClauses = cls } = metaOccurs m cls -- since a datatype is isomorphic to the sum of its constructor types -- we check the constructor types metaOccurs m Datatype{ dataCons = cs } = mapM_ mocc cs where mocc c = metaOccurs m . defType =<< getConstInfo c metaOccurs m Record{ recConHead = c } = metaOccurs m . defType =<< getConstInfo (conName c) metaOccurs m Constructor{} = return () metaOccurs m Primitive{} = return () metaOccurs m AbstractDefn{} = __IMPOSSIBLE__ instance Occurs Clause where occurs red ctx m xs cl = __IMPOSSIBLE__ metaOccurs m = metaOccurs m . clauseBody instance Occurs Level where occurs red ctx m xs (Max as) = Max <$> occurs red ctx m xs as metaOccurs m (Max as) = metaOccurs m as instance Occurs PlusLevel where occurs red ctx m xs l@ClosedLevel{} = return l occurs red ctx m xs (Plus n l) = Plus n <$> occurs red ctx' m xs l where ctx' | n == 0 = ctx | otherwise = leaveTop ctx -- we leave Top only if we encounter at least one successor metaOccurs m ClosedLevel{} = return () metaOccurs m (Plus n l) = metaOccurs m l instance Occurs LevelAtom where occurs red ctx m xs l = do l <- case red of YesUnfold -> reduce l NoUnfold -> instantiate l case l of MetaLevel m' args -> do MetaV m' args <- ignoreSharing <$> occurs red ctx m xs (MetaV m' args) return $ MetaLevel m' args NeutralLevel r v -> NeutralLevel r <$> occurs red ctx m xs v BlockedLevel m' v -> BlockedLevel m' <$> occurs red Flex m xs v UnreducedLevel v -> UnreducedLevel <$> occurs red ctx m xs v metaOccurs m l = do l <- instantiate l case l of MetaLevel m' args -> metaOccurs m $ MetaV m' args NeutralLevel _ v -> metaOccurs m v BlockedLevel _ v -> metaOccurs m v UnreducedLevel v -> metaOccurs m v instance Occurs Type where occurs red ctx m xs (El s v) = uncurry El <$> occurs red ctx m xs (s,v) metaOccurs m (El s v) = metaOccurs m (s,v) instance Occurs Sort where occurs red ctx m xs s = do s' <- case red of YesUnfold -> reduce s NoUnfold -> instantiate s case s' of DLub s1 s2 -> uncurry DLub <$> occurs red (weakly ctx) m xs (s1,s2) Type a -> Type <$> occurs red ctx m xs a Prop -> return s' Inf -> return s' SizeUniv -> return s' metaOccurs m s = do s <- instantiate s case s of DLub s1 s2 -> metaOccurs m (s1,s2) Type a -> metaOccurs m a Prop -> return () Inf -> return () SizeUniv -> return () instance Occurs a => Occurs (Elim' a) where occurs red ctx m xs e@Proj{} = return e occurs red ctx m xs (Apply a) = Apply <$> occurs red ctx m xs a metaOccurs m (Proj{} ) = return () metaOccurs m (Apply a) = metaOccurs m a instance (Occurs a, Subst t a) => Occurs (Abs a) where occurs red ctx m xs b@(Abs s x) = Abs s <$> underAbstraction_ b (occurs red ctx m (liftUnderAbs xs)) occurs red ctx m xs b@(NoAbs s x) = NoAbs s <$> occurs red ctx m xs x metaOccurs m (Abs s x) = metaOccurs m x metaOccurs m (NoAbs s x) = metaOccurs m x instance Occurs a => Occurs (Arg a) where occurs red ctx m xs (Arg info x) | isIrrelevant info = Arg info <$> occurs red Irrel m (goIrrelevant xs) x occurs red ctx m xs (Arg info x) = Arg info <$> occurs red ctx m xs x metaOccurs m a = metaOccurs m (unArg a) instance Occurs a => Occurs (Dom a) where occurs red ctx m xs (Dom info x) = Dom info <$> occurs red ctx m xs x metaOccurs m = metaOccurs m . unDom instance (Occurs a, Occurs b) => Occurs (a,b) where occurs red ctx m xs (x,y) = (,) <$> occurs red ctx m xs x <*> occurs red ctx m xs y metaOccurs m (x,y) = metaOccurs m x >> metaOccurs m y instance Occurs a => Occurs [a] where occurs red ctx m xs ys = mapM (occurs red ctx m xs) ys metaOccurs m ys = mapM_ (metaOccurs m) ys instance Occurs a => Occurs (Maybe a) where occurs red ctx m mx my = traverse (occurs red ctx m mx) my metaOccurs m = maybe (return ()) (metaOccurs m) -- * Getting rid of flexible occurrences -- | @prune m' vs xs@ attempts to remove all arguments from @vs@ whose -- free variables are not contained in @xs@. -- If successful, @m'@ is solved by the new, pruned meta variable and we -- return @True@ else @False@. -- -- Issue 1147: -- If any of the meta args @vs@ is matchable, e.g., is a constructor term, -- we cannot prune, because the offending variables could be removed by -- reduction for a suitable instantiation of the meta variable. prune :: MetaId -> Args -> [Nat] -> TCM PruneResult prune m' vs xs = do caseEitherM (runExceptT $ mapM (hasBadRigid xs) $ map unArg vs) (const $ return PrunedNothing) $ \ kills -> do reportSDoc "tc.meta.kill" 10 $ vcat [ text "attempting kills" , nest 2 $ vcat [ text "m' =" <+> pretty m' , text "xs =" <+> prettyList (map (prettyTCM . var) xs) , text "vs =" <+> prettyList (map prettyTCM vs) , text "kills =" <+> text (show kills) ] ] killArgs kills m' -- | @hasBadRigid xs v = Just True@ iff one of the rigid variables in @v@ is not in @xs@. -- Actually we can only prune if a bad variable is in the head. See issue 458. -- Or in a non-eliminateable position (see succeed/PruningNonMillerPattern). -- -- @hasBadRigid xs v = Nothing@ means that -- we cannot prune at all as one of the meta args is matchable. -- (See issue 1147.) hasBadRigid :: [Nat] -> Term -> ExceptT () TCM Bool hasBadRigid xs t = do -- We fail if we encounter a matchable argument. let failure = throwError () tb <- liftTCM $ reduceB t let t = ignoreBlocking tb case ignoreSharing t of Var x _ -> return $ notElem x xs -- Issue 1153: A lambda has to be considered matchable. -- Lam _ v -> hasBadRigid (0 : map (+1) xs) (absBody v) Lam _ v -> failure DontCare v -> hasBadRigid xs v -- The following types of arguments cannot be eliminated by a pattern -- match: data, record, Pi, levels, sorts -- Thus, their offending rigid variables are bad. v@(Def f es) -> ifNotM (isNeutral tb f es) failure $ {- else -} do es `rigidVarsNotContainedIn` xs -- Andreas, 2012-05-03: There is room for further improvement. -- We could also consider a defined f which is not blocked by a meta. Pi a b -> (a,b) `rigidVarsNotContainedIn` xs Level v -> v `rigidVarsNotContainedIn` xs Sort s -> s `rigidVarsNotContainedIn` xs -- Since constructors can be eliminated by pattern-matching, -- offending variables under a constructor could be removed by -- the right instantiation of the meta variable. -- Thus, they are not rigid. Con c _ args -> do ifM (liftTCM $ isEtaCon (conName c)) -- in case of a record con, we can in principle prune -- (but not this argument; the meta could become a projection!) (and <$> mapM (hasBadRigid xs . unArg) args) -- not andM, we need to force the exceptions! failure Lit{} -> failure -- matchable MetaV{} -> failure -- potentially matchable Shared p -> __IMPOSSIBLE__ -- | Check whether a term @Def f es@ is finally stuck. -- Currently, we give only a crude approximation. isNeutral :: MonadTCM tcm => Blocked t -> QName -> Elims -> tcm Bool isNeutral b f es = liftTCM $ do let yes = return True no = return False def <- getConstInfo f if defMatchable def then no else do case theDef def of AbstractDefn{} -> yes Axiom{} -> yes Datatype{} -> yes Record{} -> yes Function{} -> case b of NotBlocked StuckOn{} _ -> yes NotBlocked AbsurdMatch _ -> yes _ -> no _ -> no -- | Check whether any of the variables (given as de Bruijn indices) -- occurs *definitely* in the term in a rigid position. -- Reduces the term successively to remove variables in dead subterms. -- This fixes issue 1386. rigidVarsNotContainedIn :: (MonadTCM tcm, FoldRigid a) => a -> [Nat] -> tcm Bool rigidVarsNotContainedIn v is = liftTCM $ do n0 <- getContextSize let -- allowed variables as de Bruijn levels levels = Set.fromList $ map (n0-1 -) is -- test if index is forbidden by converting it to level test i = do n <- getContextSize -- get de Bruijn level for i let l = n-1 - i -- If l >= n0 then it is a bound variable and can be -- ignored. Otherwise, it has to be in the allowed levels. forbidden = l < n0 && not (l `Set.member` levels) when forbidden $ reportSLn "tc.meta.kill" 20 $ "found forbidden de Bruijn level " ++ show l return $ Any forbidden getAny <$> foldRigid test v -- | Collect the *definitely* rigid variables in a monoid. -- We need to successively reduce the expression to do this. class FoldRigid a where -- foldRigid :: (MonadTCM tcm, Monoid (tcm m)) => (Nat -> tcm m) -> a -> tcm m foldRigid :: (Monoid (TCM m)) => (Nat -> TCM m) -> a -> TCM m instance FoldRigid Term where foldRigid f t = do b <- liftTCM $ reduceB t case ignoreSharing $ ignoreBlocking b of Var i es -> f i `mappend` fold es Lam _ t -> fold t Lit{} -> mempty Def _ es -> case b of Blocked{} -> mempty NotBlocked MissingClauses _ -> mempty _ -> fold es Con _ _ ts -> fold ts Pi a b -> fold (a,b) Sort s -> fold s Level l -> fold l MetaV{} -> mempty DontCare{} -> mempty Shared{} -> __IMPOSSIBLE__ where fold = foldRigid f instance FoldRigid Type where foldRigid f (El s t) = foldRigid f (s,t) instance FoldRigid Sort where foldRigid f s = case s of Type l -> fold l Prop -> mempty Inf -> mempty SizeUniv -> mempty DLub s1 s2 -> fold (s1, s2) where fold = foldRigid f instance FoldRigid Level where foldRigid f (Max ls) = foldRigid f ls instance FoldRigid PlusLevel where foldRigid f ClosedLevel{} = mempty foldRigid f (Plus _ l) = foldRigid f l instance FoldRigid LevelAtom where foldRigid f l = case l of MetaLevel{} -> mempty NeutralLevel MissingClauses _ -> mempty NeutralLevel _ l -> fold l BlockedLevel _ l -> fold l UnreducedLevel l -> fold l where fold = foldRigid f instance (Subst t a, FoldRigid a) => FoldRigid (Abs a) where foldRigid f b = underAbstraction_ b $ foldRigid f instance FoldRigid a => FoldRigid (Arg a) where foldRigid f a = case getRelevance a of Irrelevant -> mempty _ -> foldRigid f $ unArg a instance FoldRigid a => FoldRigid (Dom a) where foldRigid f dom = foldRigid f $ unDom dom instance FoldRigid a => FoldRigid (Elim' a) where foldRigid f (Apply a) = foldRigid f a foldRigid f Proj{} = mempty instance FoldRigid a => FoldRigid [a] where foldRigid f = foldMap $ foldRigid f instance (FoldRigid a, FoldRigid b) => FoldRigid (a,b) where foldRigid f (a,b) = foldRigid f a `mappend` foldRigid f b data PruneResult = NothingToPrune -- ^ the kill list is empty or only @False@s | PrunedNothing -- ^ there is no possible kill (because of type dep.) | PrunedSomething -- ^ managed to kill some args in the list | PrunedEverything -- ^ all prescribed kills where performed deriving (Eq, Show) -- | @killArgs [k1,...,kn] X@ prunes argument @i@ from metavar @X@ if @ki==True@. -- Pruning is carried out whenever > 0 arguments can be pruned. killArgs :: [Bool] -> MetaId -> TCM PruneResult killArgs kills _ | not (or kills) = return NothingToPrune -- nothing to kill killArgs kills m = do mv <- lookupMeta m allowAssign <- asks envAssignMetas if mvFrozen mv == Frozen || not allowAssign then return PrunedNothing else do -- Andreas 2011-04-26, we allow pruning in MetaV and MetaS let a = jMetaType $ mvJudgement mv TelV tel b <- telView' <$> instantiateFull a let args = zip (telToList tel) (kills ++ repeat False) (kills', a') = killedType args b dbg kills' a a' -- If there is any prunable argument, perform the pruning if not (any unArg kills') then return PrunedNothing else do performKill kills' m a' -- Only successful if all occurrences were killed -- Andreas, 2011-05-09 more precisely, check that at least -- the in 'kills' prescribed kills were carried out return $ if (and $ zipWith implies kills $ map unArg kills') then PrunedEverything else PrunedSomething where implies :: Bool -> Bool -> Bool implies False _ = True implies True x = x dbg kills' a a' = reportSDoc "tc.meta.kill" 10 $ vcat [ text "after kill analysis" , nest 2 $ vcat [ text "metavar =" <+> prettyTCM m , text "kills =" <+> text (show kills) , text "kills' =" <+> text (show kills') , text "oldType =" <+> prettyTCM a , text "newType =" <+> prettyTCM a' ] ] -- | @killedType [((x1,a1),k1)..((xn,an),kn)] b = ([k'1..k'n],t')@ -- (ignoring @Dom@). Let @t' = (xs:as) -> b@. -- Invariant: @k'i == True@ iff @ki == True@ and pruning the @i@th argument from -- type @b@ is possible without creating unbound variables. -- @t'@ is type @t@ after pruning all @k'i==True@. killedType :: [(Dom (ArgName, Type), Bool)] -> Type -> ([Arg Bool], Type) killedType [] b = ([], b) killedType ((arg@(Dom info _), kill) : kills) b | dontKill = (Arg info False : args, mkPi arg b') | otherwise = (Arg info True : args, strengthen __IMPOSSIBLE__ b') where (args, b') = killedType kills b dontKill = not kill || 0 `freeIn` b' -- | Instantiate a meta variable with a new one that only takes -- the arguments which are not pruneable. performKill :: [Arg Bool] -- ^ Arguments to old meta var in left to right order -- with @Bool@ indicating whether they can be pruned. -> MetaId -- ^ The old meta var to receive pruning. -> Type -- ^ The pruned type of the new meta var. -> TCM () performKill kills m a = do mv <- lookupMeta m when (mvFrozen mv == Frozen) __IMPOSSIBLE__ -- Arity of the old meta. let n = size kills -- The permutation of the new meta picks the arguments -- which are not pruned in left to right order -- (de Bruijn level order). let perm = Perm n [ i | (i, Arg _ False) <- zip [0..] kills ] m' <- newMeta (mvInfo mv) (mvPriority mv) perm (HasType __IMPOSSIBLE__ a) -- Andreas, 2010-10-15 eta expand new meta variable if necessary etaExpandMetaSafe m' let -- Arguments to new meta (de Bruijn indices) -- in left to right order. vars = [ Arg info (var i) | (i, Arg info False) <- zip (downFrom n) kills ] u = MetaV m' $ map Apply vars -- Arguments to the old meta (just arg infos and name hints) -- in left to right order. tel = map ("v" <$) kills dbg m' u assignTerm m tel u -- m tel := u where dbg m' u = reportSDoc "tc.meta.kill" 10 $ vcat [ text "actual killing" , nest 2 $ vcat [ text "new meta:" <+> pretty m' , text "kills :" <+> text (show kills) , text "inst :" <+> pretty m <+> text ":=" <+> prettyTCM u ] ] Agda-2.5.3/src/full/Agda/TypeChecking/MetaVars/Mention.hs0000644000000000000000000000753413154613124021205 0ustar0000000000000000 module Agda.TypeChecking.MetaVars.Mention where import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad class MentionsMeta t where mentionsMeta :: MetaId -> t -> Bool instance MentionsMeta Term where mentionsMeta x v = case v of Var _ args -> mm args Lam _ b -> mm b Lit{} -> False Def _ args -> mm args Con _ _ args -> mm args Pi a b -> mm (a, b) Sort s -> mm s Level l -> mm l DontCare v -> False -- we don't have to look inside don't cares when deciding to wake constraints MetaV y args -> x == y || mm args -- TODO: we really only have to look one level deep at meta args Shared p -> mm $ derefPtr p where mm v = mentionsMeta x v instance MentionsMeta Level where mentionsMeta x (Max as) = mentionsMeta x as instance MentionsMeta PlusLevel where mentionsMeta x ClosedLevel{} = False mentionsMeta x (Plus _ a) = mentionsMeta x a instance MentionsMeta LevelAtom where mentionsMeta x l = case l of MetaLevel m vs -> x == m || mentionsMeta x vs BlockedLevel m _ -> x == m -- if it's blocked on a different meta it doesn't matter if it mentions the meta somewhere else UnreducedLevel l -> mentionsMeta x l NeutralLevel _ l -> mentionsMeta x l instance MentionsMeta Type where mentionsMeta x (El s t) = mentionsMeta x (s, t) instance MentionsMeta Sort where mentionsMeta x s = case s of Type l -> mentionsMeta x l Prop -> False Inf -> False SizeUniv -> False DLub s1 s2 -> mentionsMeta x (s1, s2) instance MentionsMeta t => MentionsMeta (Abs t) where mentionsMeta x = mentionsMeta x . unAbs instance MentionsMeta t => MentionsMeta (Arg t) where mentionsMeta x a | isIrrelevant a = False -- ^ we don't have to look inside irrelevant arguments when deciding to wake constraints mentionsMeta x a = mentionsMeta x (unArg a) instance MentionsMeta t => MentionsMeta (Dom t) where mentionsMeta x = mentionsMeta x . unDom instance MentionsMeta t => MentionsMeta [t] where mentionsMeta x = any (mentionsMeta x) instance MentionsMeta t => MentionsMeta (Maybe t) where mentionsMeta x = maybe False (mentionsMeta x) instance (MentionsMeta a, MentionsMeta b) => MentionsMeta (a, b) where mentionsMeta x (a, b) = mentionsMeta x a || mentionsMeta x b instance (MentionsMeta a, MentionsMeta b, MentionsMeta c) => MentionsMeta (a, b, c) where mentionsMeta x (a, b, c) = mentionsMeta x a || mentionsMeta x b || mentionsMeta x c instance MentionsMeta a => MentionsMeta (Closure a) where mentionsMeta x cl = mentionsMeta x (clValue cl) instance MentionsMeta Elim where mentionsMeta x Proj{} = False mentionsMeta x (Apply v) = mentionsMeta x v instance MentionsMeta a => MentionsMeta (Tele a) where mentionsMeta x EmptyTel = False mentionsMeta x (ExtendTel a b) = mentionsMeta x (a, b) instance MentionsMeta ProblemConstraint where mentionsMeta x = mentionsMeta x . theConstraint instance MentionsMeta Constraint where mentionsMeta x c = case c of ValueCmp _ t u v -> mm (t, u, v) ElimCmp _ t v as bs -> mm ((t, v), (as, bs)) LevelCmp _ u v -> mm (u, v) TypeCmp _ a b -> mm (a, b) TelCmp a b _ u v -> mm ((a, b), (u, v)) SortCmp _ a b -> mm (a, b) Guarded{} -> False -- This gets woken up when the problem it's guarded by is solved UnBlock _ -> True -- this might be a postponed typechecking -- problem and we don't have a handle on -- what metas it depends on FindInScope{} -> True -- this needs to be woken up for any meta IsEmpty r t -> mm t CheckSizeLtSat t -> mm t where mm v = mentionsMeta x v -- instance (Ord k, MentionsMeta e) => MentionsMeta (Map k e) where -- mentionsMeta = traverse mentionsMeta Agda-2.5.3/src/full/Agda/TypeChecking/CompiledClause/0000755000000000000000000000000013154613124020376 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/CompiledClause/Match.hs0000644000000000000000000001736013154613124021775 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.CompiledClause.Match where import Control.Applicative import Control.Monad.Reader (asks) import qualified Data.List as List import qualified Data.Map as Map import Agda.Syntax.Internal import Agda.Syntax.Common import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Monad import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce import Agda.TypeChecking.Reduce.Monad as RedM import Agda.TypeChecking.Substitute import Agda.Utils.Maybe #include "undefined.h" import Agda.Utils.Impossible matchCompiled :: CompiledClauses -> MaybeReducedArgs -> ReduceM (Reduced (Blocked Args) Term) matchCompiled c args = do r <- matchCompiledE c $ map (fmap Apply) args case r of YesReduction simpl v -> return $ YesReduction simpl v NoReduction bes -> return $ NoReduction $ fmap (map argFromElim) bes -- | @matchCompiledE c es@ takes a function given by case tree @c@ and -- and a spine @es@ and tries to apply the function to @es@. matchCompiledE :: CompiledClauses -> MaybeReducedElims -> ReduceM (Reduced (Blocked Elims) Term) matchCompiledE c args = match' [(c, args, id)] -- | A stack entry is a triple consisting of -- 1. the part of the case tree to continue matching, -- 2. the current argument vector, and -- 3. a patch function taking the current argument vector back -- to the original argument vector. type Frame = (CompiledClauses, MaybeReducedElims, Elims -> Elims) type Stack = [Frame] -- | @match'@ tries to solve the matching problems on the @Stack@. -- In each iteration, the top problem is removed and handled. -- -- If the top problem was a @Done@, we succeed. -- -- If the top problem was a @Case n@ and the @n@th argument of the problem -- is not a constructor or literal, we are stuck, thus, fail. -- -- If we have a branch for the constructor/literal, we put it on the stack -- to continue. -- If we do not have a branch, we fall through to the next problem, which -- should be the corresponding catch-all branch. -- -- An empty stack is an exception that can come only from an incomplete -- function definition. -- TODO: literal/constructor pattern conflict (for Nat) match' :: Stack -> ReduceM (Reduced (Blocked Elims) Term) match' ((c, es, patch) : stack) = do let no blocking es = return $ NoReduction $ blocking $ patch $ map ignoreReduced es yes t = flip YesReduction t <$> asks envSimplification do shared <- sharedFun case c of -- impossible case Fail -> no (NotBlocked AbsurdMatch) es -- done matching Done xs t -- if the function was partially applied, return a lambda | m < n -> yes $ applySubst (toSubst es) $ foldr lam t (drop m xs) -- otherwise, just apply instantiation to body -- apply the result to any extra arguments | otherwise -> yes $ applySubst (toSubst es0) t `applyE` map ignoreReduced es1 where n = length xs m = length es -- at least the first @n@ elims must be @Apply@s, so we can -- turn them into a subsitution toSubst = parallelS . reverse . map (unArg . argFromElim . ignoreReduced) -- Andreas, 2013-05-21 why introduce sharing only here, -- and not in underapplied case also? (es0, es1) = splitAt n $ map (fmap $ fmap shared) es lam x t = Lam (argInfo x) (Abs (unArg x) t) -- splitting on the @n@th elimination Case (Arg _ n) bs -> do case splitAt n es of -- if the @n@th elimination is not supplied, no match (_, []) -> no (NotBlocked Underapplied) es -- if the @n@th elimination is @e0@ (es0, MaybeRed red e0 : es1) -> do -- get the reduced form of @e0@ eb :: Blocked Elim <- do case red of Reduced b -> return $ e0 <$ b NotReduced -> unfoldCorecursionE e0 let e = ignoreBlocking eb -- replace the @n@th argument by its reduced form es' = es0 ++ [MaybeRed (Reduced $ () <$ eb) e] ++ es1 -- if a catch-all clause exists, put it on the stack catchAllFrame stack = maybe stack (\c -> (c, es', patch) : stack) (catchAllBranch bs) -- If our argument is @Lit l@, we push @litFrame l@ onto the stack. litFrame l stack = case Map.lookup l (litBranches bs) of Nothing -> stack Just cc -> (cc, es0 ++ es1, patchLit) : stack -- If our argument (or its constructor form) is @Con c ci vs@ -- we push @conFrame c vs@ onto the stack. conFrame c ci vs stack = case Map.lookup (conName c) (conBranches bs) of Nothing -> stack Just cc -> ( content cc , es0 ++ map (MaybeRed NotReduced . Apply) vs ++ es1 , patchCon c ci (length vs) ) : stack -- If our argument is @Proj p@, we push @projFrame p@ onto the stack. projFrame p stack = case Map.lookup p (conBranches bs) of Nothing -> stack Just cc -> (content cc, es0 ++ es1, patchLit) : stack -- The new patch function restores the @n@th argument to @v@: -- In case we matched a literal, just put @v@ back. patchLit es = patch (es0 ++ [e] ++ es1) where (es0, es1) = splitAt n es -- In case we matched constructor @c@ with @m@ arguments, -- contract these @m@ arguments @vs@ to @Con c ci vs@. patchCon c ci m es = patch (es0 ++ [Con c ci vs <$ e] ++ es2) where (es0, rest) = splitAt n es (es1, es2) = splitAt m rest vs = map argFromElim es1 -- Now do the matching on the @n@ths argument: id $ case fmap ignoreSharing <$> eb of Blocked x _ -> no (Blocked x) es' NotBlocked _ (Apply (Arg info (MetaV x _))) -> no (Blocked x) es' -- In case of a literal, try also its constructor form NotBlocked _ (Apply (Arg info v@(Lit l))) -> performedSimplification $ do cv <- constructorForm v let cFrame stack = case ignoreSharing cv of Con c ci vs -> conFrame c ci vs stack _ -> stack match' $ litFrame l $ cFrame $ catchAllFrame stack -- In case of a constructor, push the conFrame NotBlocked _ (Apply (Arg info (Con c ci vs))) -> performedSimplification $ match' $ conFrame c ci vs $ catchAllFrame $ stack -- In case of a projection, push the projFrame NotBlocked _ (Proj _ p) -> performedSimplification $ match' $ projFrame p $ stack -- catchAllFrame $ stack -- Issue #1986: no catch-all for copattern matching! -- Otherwise, we are stuck. If we were stuck before, -- we keep the old reason, otherwise we give reason StuckOn here. NotBlocked blocked e -> no (NotBlocked $ stuckOn e blocked) es' -- If we reach the empty stack, then pattern matching was incomplete match' [] = {- new line here since __IMPOSSIBLE__ does not like the ' in match' -} caseMaybeM (asks envAppDef) __IMPOSSIBLE__ $ \ f -> do pds <- getPartialDefs if f `elem` pds then return (NoReduction $ NotBlocked MissingClauses []) else do traceSLn "impossible" 10 ("Incomplete pattern matching when applying " ++ show f) __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/CompiledClause/Compile.hs-boot0000644000000000000000000000035113154613124023262 0ustar0000000000000000module Agda.TypeChecking.CompiledClause.Compile where import Agda.Syntax.Internal import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Monad.Base compileClauses :: Maybe (QName, Type) -> [Clause] -> TCM CompiledClauses Agda-2.5.3/src/full/Agda/TypeChecking/CompiledClause/Compile.hs0000644000000000000000000002646613154613124022340 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.CompiledClause.Compile where import Prelude hiding (null) import Control.Monad import Data.Maybe import Data.Monoid import qualified Data.Map as Map import Data.List (nubBy) import Data.Function import Debug.Trace import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Coverage import Agda.TypeChecking.Coverage.SplitTree import Agda.TypeChecking.Monad import Agda.TypeChecking.RecordPatterns import Agda.TypeChecking.Substitute import Agda.TypeChecking.Pretty (prettyTCM, nest, sep, text) import Agda.Utils.Functor import Agda.Utils.Maybe import Agda.Utils.Null import Agda.Utils.List import Agda.Utils.Pretty (Pretty(..), prettyShow) import qualified Agda.Utils.Pretty as P #include "undefined.h" import Agda.Utils.Impossible -- | Process function clauses into case tree. -- This involves: -- 1. Coverage checking, generating a split tree. -- 2. Translation of lhs record patterns into rhs uses of projection. -- Update the split tree. -- 3. Generating a case tree from the split tree. -- Phases 1. and 2. are skipped if @Nothing@. compileClauses :: Maybe (QName, Type) -- ^ Translate record patterns and coverage check with given type? -> [Clause] -> TCM CompiledClauses compileClauses mt cs = do -- Construct clauses with pattern variables bound in left-to-right order. -- Discard de Bruijn indices in patterns. let unBruijn cs = [ Cl (map (fmap (fmap dbPatVarName . namedThing)) $ namedClausePats c) (compiledClauseBody c) | c <- cs ] shared <- sharedFun case mt of Nothing -> compile shared . unBruijn <$> normaliseProjP cs Just (q, t) -> do splitTree <- coverageCheck q t cs -- The coverage checker might have added some clauses (#2288)! -- Throw away the unreachable clauses (#2723). let notUnreachable = (Just True /=) . clauseUnreachable cs <- normaliseProjP =<< filter notUnreachable . defClauses <$> getConstInfo q let cls = unBruijn cs reportSDoc "tc.cc" 30 $ sep $ do (text "clauses patterns before compilation") : do map (prettyTCM . map unArg . clPats) cls reportSDoc "tc.cc" 50 $ do sep [ text "clauses before compilation" , (nest 2 . text . show) cs ] let cc = compileWithSplitTree shared splitTree cls reportSDoc "tc.cc" 12 $ sep [ text "compiled clauses (still containing record splits)" , nest 2 $ return $ P.pretty cc ] cc <- translateCompiledClauses cc return cc -- | Stripped-down version of 'Agda.Syntax.Internal.Clause' -- used in clause compiler. data Cl = Cl { clPats :: [Arg Pattern] -- ^ Pattern variables are considered in left-to-right order. , clBody :: Maybe Term } deriving (Show) instance Pretty Cl where pretty (Cl ps b) = P.prettyList ps P.<+> P.text "->" P.<+> maybe (P.text "_|_") pretty b type Cls = [Cl] compileWithSplitTree :: (Term -> Term) -> SplitTree -> Cls -> CompiledClauses compileWithSplitTree shared t cs = case t of SplitAt i ts -> Case i $ compiles ts $ splitOn (length ts == 1) (unArg i) cs -- if there is just one case, we force expansion of catch-alls -- this is needed to generate a sound tree on which we can -- collapse record pattern splits SplittingDone n -> compile shared cs -- after end of split tree, continue with left-to-right strategy where compiles :: SplitTrees -> Case Cls -> Case CompiledClauses compiles ts br@Branches{ projPatterns = cop , conBranches = cons , litBranches = lits , catchAllBranch = catchAll } = Branches { projPatterns = cop , conBranches = updCons cons , litBranches = compile shared <$> lits , catchAllBranch = compile shared <$> catchAll } where updCons = Map.mapWithKey $ \ c cl -> caseMaybe (lookup c ts) (compile shared) (compileWithSplitTree shared) <$> cl -- When the split tree is finished, we continue with @compile@. compile :: (Term -> Term) -> Cls -> CompiledClauses compile shared cs = case nextSplit cs of Just (isRecP, n)-> Case n $ fmap (compile shared) $ splitOn isRecP (unArg n) cs Nothing -> case clBody c of -- It's possible to get more than one clause here due to -- catch-all expansion. Just t -> Done (map (fmap name) $ clPats c) (shared t) Nothing -> Fail where -- If there are more than one clauses, take the first one. c = headWithDefault __IMPOSSIBLE__ cs name (VarP x) = x name (DotP _) = underscore name AbsurdP{} = absurdPatternName name ConP{} = __IMPOSSIBLE__ name LitP{} = __IMPOSSIBLE__ name ProjP{} = __IMPOSSIBLE__ -- | Get the index of the next argument we need to split on. -- This the number of the first pattern that does a match in the first clause. nextSplit :: Cls -> Maybe (Bool, Arg Int) nextSplit [] = __IMPOSSIBLE__ nextSplit (Cl ps _ : _) = headMaybe $ catMaybes $ zipWith (\ (Arg ai p) n -> (, Arg ai n) <$> properSplit p) ps [0..] -- | Is is not a variable pattern? -- And if yes, is it a record pattern? properSplit :: Pattern' a -> Maybe Bool properSplit (ConP _ cpi _) = Just $ isJust $ conPRecord cpi properSplit LitP{} = Just False properSplit ProjP{} = Just False properSplit VarP{} = Nothing properSplit AbsurdP{} = Nothing -- for purposes of compilation properSplit DotP{} = Nothing -- | Is this a variable pattern? -- -- Maintain invariant: @isVar = isNothing . properSplit@! isVar :: Pattern' a -> Bool isVar VarP{} = True isVar DotP{} = True isVar AbsurdP{} = True isVar ConP{} = False isVar LitP{} = False isVar ProjP{} = False -- | @splitOn single n cs@ will force expansion of catch-alls -- if @single@. splitOn :: Bool -> Int -> Cls -> Case Cls splitOn single n cs = mconcat $ map (fmap (:[]) . splitC n) $ -- (\ cs -> trace ("splitting on " ++ show n ++ " after expandCatchAlls " ++ show single ++ ": " ++ prettyShow (P.prettyList cs)) cs) $ expandCatchAlls single n cs splitC :: Int -> Cl -> Case Cl splitC n (Cl ps b) = caseMaybe mp fallback $ \case ProjP _ d -> projCase d $ Cl (ps0 ++ ps1) b ConP c _ qs -> conCase (conName c) $ WithArity (length qs) $ Cl (ps0 ++ map (fmap namedThing) qs ++ ps1) b LitP l -> litCase l $ Cl (ps0 ++ ps1) b VarP{} -> fallback DotP{} -> fallback AbsurdP{} -> fallback where (ps0, rest) = splitAt n ps mp = unArg <$> headMaybe rest ps1 = drop 1 rest fallback = catchAll $ Cl ps b -- | Expand catch-alls that appear before actual matches. -- -- Example: -- -- @ -- true y -- x false -- false y -- @ -- -- will expand the catch-all @x@ to @false@. -- -- Catch-alls need also to be expanded if -- they come before/after a record pattern, otherwise we get into -- trouble when we want to eliminate splits on records later. -- -- Another example (see Issue 1650): -- @ -- f (x, (y, z)) true = a -- f _ false = b -- @ -- Split tree: -- @ -- 0 (first argument of f) -- \- 1 (second component of the pair) -- \- 3 (last argument of f) -- \-- true -> a -- \- false -> b -- @ -- We would like to get the following case tree: -- @ -- case 0 of -- _,_ -> case 1 of -- _,_ -> case 3 of true -> a; false -> b -- _ -> case 3 of true -> a; false -> b -- _ -> case 3 of true -> a; false -> b -- @ -- -- Example from issue #2168: -- @ -- f x false = a -- f false = \ _ -> b -- f x true = c -- @ -- case tree: -- @ -- f x y = case y of -- true -> case x of -- true -> c -- false -> b -- false -> a -- @ expandCatchAlls :: Bool -> Int -> Cls -> Cls expandCatchAlls single n cs = -- Andreas, 2013-03-22 -- if there is a single case (such as for record splits) -- we force expansion if single then doExpand =<< cs else case cs of _ | all (isCatchAllNth . clPats) cs -> cs c@(Cl ps b) : cs | not (isCatchAllNth ps) -> c : expandCatchAlls False n cs | otherwise -> map (expand c) expansions ++ c : expandCatchAlls False n cs _ -> __IMPOSSIBLE__ where -- In case there is only one branch in the split tree, we expand all -- catch-alls for this position -- The @expansions@ are collected from all the clauses @cs@ then. -- Note: @expansions@ could be empty, so we keep the orignal clause. doExpand c@(Cl ps _) | exCatchAllNth ps = map (expand c) expansions ++ [c] | otherwise = [c] -- True if nth pattern is variable or there are less than n patterns. isCatchAllNth ps = all (isVar . unArg) $ take 1 $ drop n ps -- True if nth pattern exists and is variable. exCatchAllNth ps = any (isVar . unArg) $ take 1 $ drop n ps classify (LitP l) = Left l classify (ConP c _ _) = Right c classify _ = __IMPOSSIBLE__ -- All non-catch-all patterns following this one (at position n). -- These are the cases the wildcard needs to be expanded into. expansions = nubBy ((==) `on` (classify . unArg . snd)) . mapMaybe (notVarNth . clPats) $ cs notVarNth :: [Arg Pattern] -> Maybe ([Arg Pattern] -- First @n@ patterns. , Arg Pattern) -- @n+1@st pattern, not a variable notVarNth ps = do let (ps1, ps2) = splitAt n ps p <- headMaybe ps2 guard $ not $ isVar $ unArg p return (ps1, p) expand cl (qs, q) = case unArg q of ConP c mt qs' -> Cl (ps0 ++ [q $> ConP c mt conPArgs] ++ ps1) (substBody n' m (Con c ci conArgs) b) where ci = fromConPatternInfo mt m = length qs' -- replace all direct subpatterns of q by _ conPArgs = map (fmap ($> VarP "_")) qs' conArgs = zipWith (\ q' i -> q' $> var i) qs' $ downFrom m LitP l -> Cl (ps0 ++ [q $> LitP l] ++ ps1) (substBody n' 0 (Lit l) b) _ -> __IMPOSSIBLE__ where -- Andreas, 2016-09-19 issue #2168 -- Due to varying function arity, some clauses might be eta-contracted. -- Thus, we eta-expand them. Cl ps b = ensureNPatterns (n + 1) (map getArgInfo $ qs ++ [q]) cl -- The following pattern match cannot fail (by construction of @ps@). (ps0, _:ps1) = splitAt n ps n' = countVars ps1 countVars = sum . map (count . unArg) count VarP{} = 1 count (ConP _ _ ps) = countVars $ map (fmap namedThing) ps count DotP{} = 1 -- dot patterns are treated as variables in the clauses count (AbsurdP p) = count p count _ = 0 -- | Make sure (by eta-expansion) that clause has arity at least @n@ -- where @n@ is also the length of the provided list. ensureNPatterns :: Int -> [ArgInfo] -> Cl -> Cl ensureNPatterns n ais0 cl@(Cl ps b) | m <= 0 = cl | otherwise = Cl (ps ++ ps') (raise m b `apply` args) where k = length ps ais = drop k ais0 -- m = Number of arguments to add m = n - k ps' = for ais $ \ ai -> Arg ai $ VarP "_" args = zipWith (\ i ai -> Arg ai $ var i) (downFrom m) ais substBody :: (Subst t a) => Int -> Int -> t -> a -> a substBody n m v = applySubst $ liftS n $ v :# raiseS m Agda-2.5.3/src/full/Agda/TypeChecking/CompiledClause/Match.hs-boot0000644000000000000000000000057413154613124022735 0ustar0000000000000000 module Agda.TypeChecking.CompiledClause.Match where -- import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.CompiledClause matchCompiled :: CompiledClauses -> MaybeReducedArgs -> ReduceM (Reduced (Blocked Args) Term) matchCompiledE :: CompiledClauses -> MaybeReducedElims -> ReduceM (Reduced (Blocked [Elim]) Term) Agda-2.5.3/src/full/Agda/TypeChecking/Rules/0000755000000000000000000000000013154613124016577 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Def.hs0000644000000000000000000010743713154613124017645 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.Rules.Def where import Prelude hiding (mapM) import Control.Arrow ((***)) import Control.Applicative import Control.Monad.State hiding (forM, mapM) import Control.Monad.Reader hiding (forM, mapM) import Data.Function import Data.Maybe import Data.Traversable import qualified Data.Set as Set import Agda.Syntax.Common import qualified Agda.Syntax.Concrete as C import Agda.Syntax.Concrete (exprFieldA) import Agda.Syntax.Position import Agda.Syntax.Abstract.Pattern ( containsAbsurdPattern ) import qualified Agda.Syntax.Abstract as A import qualified Agda.Syntax.Abstract.Views as A import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Pattern as I import qualified Agda.Syntax.Info as Info import Agda.Syntax.Fixity import Agda.Syntax.Translation.InternalToAbstract import Agda.Syntax.Info import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TypeChecking.Constraints import Agda.TypeChecking.Conversion import Agda.TypeChecking.MetaVars import Agda.TypeChecking.Reduce import Agda.TypeChecking.Patterns.Abstract (expandPatternSynonyms) import Agda.TypeChecking.Pretty import Agda.TypeChecking.Substitute import Agda.TypeChecking.Free import Agda.TypeChecking.CheckInternal (checkType, inferSort) import Agda.TypeChecking.With import Agda.TypeChecking.Telescope import Agda.TypeChecking.Injectivity import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.SizedTypes.Solve import Agda.TypeChecking.RecordPatterns import Agda.TypeChecking.CompiledClause (CompiledClauses'(..)) import Agda.TypeChecking.CompiledClause.Compile import Agda.TypeChecking.Primitive hiding (Nat) import Agda.TypeChecking.Rules.Term ( checkExpr, inferExpr, inferExprForWith, checkDontExpandLast, checkTelescope ) import Agda.TypeChecking.Rules.LHS ( checkLeftHandSide, LHSResult(..), bindAsPatterns ) import Agda.TypeChecking.Rules.LHS.Problem ( AsBinding(..) ) import {-# SOURCE #-} Agda.TypeChecking.Rules.Decl ( checkDecls ) import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.Maybe ( whenNothing ) import Agda.Utils.Monad import Agda.Utils.Permutation import Agda.Utils.Pretty ( prettyShow ) import qualified Agda.Utils.Pretty as P import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Definitions by pattern matching --------------------------------------------------------------------------- checkFunDef :: Delayed -> Info.DefInfo -> QName -> [A.Clause] -> TCM () checkFunDef delayed i name cs = do -- Get the type and relevance of the function t <- typeOfConst name info <- flip setRelevance defaultArgInfo <$> relOfConst name case isAlias cs t of Just (e, mc, x) -> traceCall (CheckFunDef (getRange i) (qnameName name) cs) $ do -- Andreas, 2012-11-22: if the alias is in an abstract block -- it has been frozen. We unfreeze it to enable type inference. -- See issue 729. whenM (isFrozen x) $ unfreezeMeta x checkAlias t info delayed i name e mc _ -> checkFunDef' t info delayed Nothing Nothing i name cs -- If it's a macro check that it ends in Term → TC ⊤ ismacro <- isMacro . theDef <$> getConstInfo name when (ismacro || Info.defMacro i == MacroDef) $ checkMacroType t checkMacroType :: Type -> TCM () checkMacroType t = do t' <- normalise t TelV tel tr <- telView t' let telList = telToList tel resType = abstract (telFromList (drop (length telList - 1) telList)) tr expectedType <- el primAgdaTerm --> el (primAgdaTCM <#> primLevelZero <@> primUnit) equalType resType expectedType `catchError` \ _ -> typeError . GenericDocError =<< sep [ text "Result type of a macro must be" , nest 2 $ prettyTCM expectedType ] -- | A single clause without arguments and without type signature is an alias. isAlias :: [A.Clause] -> Type -> Maybe (A.Expr, Maybe C.Expr, MetaId) isAlias cs t = case trivialClause cs of -- if we have just one clause without pattern matching and -- without a type signature, then infer, to allow -- "aliases" for things starting with hidden abstractions Just (e, mc) | Just x <- isMeta (ignoreSharing $ unEl t) -> Just (e, mc, x) _ -> Nothing where isMeta (MetaV x _) = Just x isMeta _ = Nothing trivialClause [A.Clause (A.LHS i (A.LHSHead f []) []) _ _ (A.RHS e mc) [] _] = Just (e, mc) trivialClause _ = Nothing -- | Check a trivial definition of the form @f = e@ checkAlias :: Type -> ArgInfo -> Delayed -> Info.DefInfo -> QName -> A.Expr -> Maybe C.Expr -> TCM () checkAlias t' ai delayed i name e mc = atClause name 0 (A.RHS e mc) $ do reportSDoc "tc.def.alias" 10 $ text "checkAlias" <+> vcat [ text (prettyShow name) <+> colon <+> prettyTCM t' , text (prettyShow name) <+> equals <+> prettyTCM e ] {- -- Infer the type of the rhs (v, t) <- applyRelevanceToContext (argInfoRelevance ai) $ inferOrCheck e (Just t') -- v <- coerce v t t' -} -- Infer the type of the rhs v <- applyRelevanceToContext (getRelevance ai) $ checkDontExpandLast e t' let t = t' reportSDoc "tc.def.alias" 20 $ text "checkAlias: finished checking" solveSizeConstraints DontDefaultToInfty v <- instantiateFull v -- if we omit this, we loop (stdlib: Relation.Binary.Sum) -- or the termination checker might stumble over levels in sorts -- that cannot be converted to expressions without the level built-ins -- (test/succeed/Issue655.agda) -- compute body modification for irrelevant definitions, see issue 610 let bodyMod = case getRelevance ai of Irrelevant -> dontCare _ -> id -- Add the definition addConstant name $ defaultDefn ai name t $ set funMacro (Info.defMacro i == MacroDef) $ emptyFunction { funClauses = [ Clause -- trivial clause @name = v@ { clauseLHSRange = getRange i , clauseFullRange = getRange i , clauseTel = EmptyTel , namedClausePats = [] , clauseBody = Just $ bodyMod v , clauseType = Just $ Arg ai t , clauseCatchall = False , clauseUnreachable = Just False } ] , funCompiled = Just $ Done [] $ bodyMod v , funDelayed = delayed , funAbstr = Info.defAbstract i } -- Andreas, 2017-01-01, issue #2372: -- Add the definition to the instance table, if needed, to update its type. when (Info.defInstance i == InstanceDef) $ do addTypedInstance name t reportSDoc "tc.def.alias" 20 $ text "checkAlias: leaving" -- | Type check a definition by pattern matching. checkFunDef' :: Type -- ^ the type we expect the function to have -> ArgInfo -- ^ is it irrelevant (for instance) -> Delayed -- ^ are the clauses delayed (not unfolded willy-nilly) -> Maybe ExtLamInfo -- ^ does the definition come from an extended lambda -- (if so, we need to know some stuff about lambda-lifted args) -> Maybe QName -- ^ is it a with function (if so, what's the name of the parent function) -> Info.DefInfo -- ^ range info -> QName -- ^ the name of the function -> [A.Clause] -- ^ the clauses to check -> TCM () checkFunDef' t ai delayed extlam with i name cs = checkFunDefS t ai delayed extlam with i name Nothing cs -- | Type check a definition by pattern matching. checkFunDefS :: Type -- ^ the type we expect the function to have -> ArgInfo -- ^ is it irrelevant (for instance) -> Delayed -- ^ are the clauses delayed (not unfolded willy-nilly) -> Maybe ExtLamInfo -- ^ does the definition come from an extended lambda -- (if so, we need to know some stuff about lambda-lifted args) -> Maybe QName -- ^ is it a with function (if so, what's the name of the parent function) -> Info.DefInfo -- ^ range info -> QName -- ^ the name of the function -> Maybe Substitution -- ^ substitution (from with abstraction) that needs to be applied to module parameters -> [A.Clause] -- ^ the clauses to check -> TCM () checkFunDefS t ai delayed extlam with i name withSub cs = do traceCall (CheckFunDef (getRange i) (qnameName name) cs) $ do -- TODO!! (qnameName) reportSDoc "tc.def.fun" 10 $ sep [ text "checking body of" <+> prettyTCM name , nest 2 $ text ":" <+> prettyTCM t , nest 2 $ text "full type:" <+> (prettyTCM . defType =<< getConstInfo name) ] reportSDoc "tc.def.fun" 70 $ sep $ [ text "clauses:" ] ++ map (nest 2 . text . show . A.deepUnscope) cs cs <- return $ map A.lhsToSpine cs reportSDoc "tc.def.fun" 70 $ sep $ [ text "spine clauses:" ] ++ map (nest 2 . text . show . A.deepUnscope) cs -- Ensure that all clauses have the same number of trailing hidden patterns -- This is necessary since trailing implicits are no longer eagerly inserted. -- Andreas, 2013-10-13 -- Since we have flexible function arity, it is no longer necessary -- to patch clauses to same arity -- cs <- trailingImplicits t cs -- Check the clauses cs <- traceCall NoHighlighting $ do -- To avoid flicker. forM (zip cs [0..]) $ \ (c, clauseNo) -> do atClause name clauseNo (A.clauseRHS c) $ do c <- applyRelevanceToContext (argInfoRelevance ai) $ do checkClause t withSub c -- Andreas, 2013-11-23 do not solve size constraints here yet -- in case we are checking the body of an extended lambda. -- 2014-04-24: The size solver requires each clause to be -- checked individually, since otherwise we get constraints -- in typing contexts which are not prefixes of each other. whenNothing extlam $ solveSizeConstraints DontDefaultToInfty -- Andreas, 2013-10-27 add clause as soon it is type-checked -- TODO: instantiateFull? inTopContext $ addClauses name [c] return c reportSDoc "tc.def.fun" 70 $ inTopContext $ do sep $ [ text "checked clauses:" ] ++ map (nest 2 . text . show) cs -- After checking, remove the clauses again. -- (Otherwise, @checkInjectivity@ loops for issue 801). modifyFunClauses name (const []) reportSDoc "tc.cc" 25 $ inTopContext $ do sep [ text "clauses before injectivity test" , nest 2 $ prettyTCM $ map (QNamed name) cs -- broken, reify (QNamed n cl) expect cl to live at top level ] reportSDoc "tc.cc" 60 $ inTopContext $ do sep [ text "raw clauses: " , nest 2 $ sep $ map (text . show . QNamed name) cs ] -- Annotate the clauses with which arguments are actually used. cs <- instantiateFull {- =<< mapM rebindClause -} cs -- Andreas, 2010-11-12 -- rebindClause is the identity, and instantiateFull eta-contracts -- removing this eta-contraction fixes issue 361 -- however, Data.Star.Decoration.gmapAll no longer type-checks -- possibly due to missing eta-contraction!? -- Check if the function is injective. -- Andreas, 2015-07-01 we do it here in order to resolve metas -- in mutual definitions, e.g. the U/El definition in succeed/Issue439.agda -- We do it again for the mutual block after polarity analysis, see Rules.Decl. reportSLn "tc.inj.def" 20 $ "checkFunDef': checking injectivity..." inv <- Bench.billTo [Bench.Injectivity] $ checkInjectivity name cs reportSDoc "tc.cc" 15 $ inTopContext $ do sep [ text "clauses before compilation" , nest 2 $ sep $ map (prettyTCM . QNamed name) cs ] -- add clauses for the coverage checker (needs to reduce) inTopContext $ addClauses name cs fullType <- flip telePi t <$> getContextTelescope -- Coverage check and compile the clauses cc <- Bench.billTo [Bench.Coverage] $ inTopContext $ compileClauses (Just (name, fullType)) cs reportSDoc "tc.cc" 60 $ inTopContext $ do sep [ text "compiled clauses of" <+> prettyTCM name , nest 2 $ text (show cc) ] -- The macro tag might be on the type signature ismacro <- isMacro . theDef <$> getConstInfo name -- Add the definition inTopContext $ addConstant name =<< do -- If there was a pragma for this definition, we can set the -- funTerminates field directly. useTerPragma $ defaultDefn ai name fullType $ set funMacro (ismacro || Info.defMacro i == MacroDef) $ emptyFunction { funClauses = cs , funCompiled = Just cc , funDelayed = delayed , funInv = inv , funAbstr = Info.defAbstract i , funExtLam = extlam , funWith = with , funCopatternLHS = isCopatternLHS cs } reportSDoc "tc.def.fun" 10 $ do sep [ text "added " <+> prettyTCM name <+> text ":" , nest 2 $ prettyTCM . defType =<< getConstInfo name ] -- | Set 'funTerminates' according to termination info in 'TCEnv', -- which comes from a possible termination pragma. useTerPragma :: Definition -> TCM Definition useTerPragma def@Defn{ defName = name, theDef = fun@Function{}} = do tc <- asks envTerminationCheck let terminates = case tc of NonTerminating -> Just False Terminating -> Just True _ -> Nothing reportSLn "tc.fundef" 30 $ unlines $ [ "funTerminates of " ++ prettyShow name ++ " set to " ++ show terminates , " tc = " ++ show tc ] return $ def { theDef = fun { funTerminates = terminates }} useTerPragma def = return def -- | Insert some patterns in the in with-clauses LHS of the given RHS insertPatterns :: [A.Pattern] -> A.RHS -> A.RHS insertPatterns pats (A.WithRHS aux es cs) = A.WithRHS aux es (map insertToClause cs) where insertToClause (A.Clause (A.LHS i lhscore ps) dots sdots rhs ds catchall) = A.Clause (A.LHS i lhscore (pats ++ ps)) dots sdots (insertPatterns pats rhs) ds catchall insertPatterns pats (A.RewriteRHS qes rhs wh) = A.RewriteRHS qes (insertPatterns pats rhs) wh insertPatterns pats rhs = rhs -- | Parameters for creating a @with@-function. data WithFunctionProblem = NoWithFunction | WithFunction { wfParentName :: QName -- ^ Parent function name. , wfName :: QName -- ^ With function name. , wfParentType :: Type -- ^ Type of the parent function. , wfParentTel :: Telescope -- ^ Context of the parent patterns. , wfBeforeTel :: Telescope -- ^ Types of arguments to the with function before the with expressions (needed vars). , wfAfterTel :: Telescope -- ^ Types of arguments to the with function after the with expressions (unneeded vars). , wfExprs :: [Term] -- ^ With and rewrite expressions. , wfExprTypes :: [EqualityView] -- ^ Types of the with and rewrite expressions. , wfRHSType :: Type -- ^ Type of the right hand side. , wfParentPats :: [NamedArg DeBruijnPattern] -- ^ Parent patterns. , wfParentParams :: Nat -- ^ Number of module parameters in parent patterns , wfPermSplit :: Permutation -- ^ Permutation resulting from splitting the telescope into needed and unneeded vars. , wfPermParent :: Permutation -- ^ Permutation reordering the variables in the parent pattern. , wfPermFinal :: Permutation -- ^ Final permutation (including permutation for the parent clause). , wfClauses :: [A.Clause] -- ^ The given clauses for the with function } -- | Type check a function clause. checkClause :: Type -- ^ Type of function defined by this clause. -> Maybe Substitution -- ^ Module parameter substitution arising from with-abstraction. -> A.SpineClause -- ^ Clause. -> TCM Clause -- ^ Type-checked clause. checkClause t withSub c@(A.Clause (A.SpineLHS i x aps withPats) namedDots strippedDots rhs0 wh catchall) = do reportSDoc "tc.lhs.top" 30 $ text "Checking clause" $$ prettyA c unless (null withPats) $ typeError $ UnexpectedWithPatterns withPats traceCall (CheckClause t c) $ do aps <- expandPatternSynonyms aps cxtNames <- reverse . map (fst . unDom) <$> getContext when (not $ null namedDots) $ reportSDoc "tc.lhs.top" 50 $ text "namedDots:" <+> vcat [ prettyTCM x <+> text "=" <+> prettyTCM v <+> text ":" <+> prettyTCM a | A.NamedDot x v a <- namedDots ] -- Not really an as-pattern, but this does the right thing. bindAsPatterns [ AsB x v a | A.NamedDot x v a <- namedDots ] $ checkLeftHandSide (CheckPatternShadowing c) (Just x) aps t withSub strippedDots $ \ lhsResult@(LHSResult npars delta ps trhs patSubst asb) -> do -- Note that we might now be in irrelevant context, -- in case checkLeftHandSide walked over an irrelevant projection pattern. -- Subtle: checkRHS expects the function type to be the lambda lifted -- type. If we're checking a with-function that's already the case, -- otherwise we need to abstract over the module telescope. t' <- case withSub of Just{} -> return t Nothing -> do theta <- lookupSection (qnameModule x) return $ abstract theta t -- At this point we should update the named dots potential with-clauses -- in the right-hand side. When checking a clause we expect the named -- dots to live in the context of the closest parent lhs, but the named -- dots added by buildWithFunction live in the context of the -- with-function arguments before pattern matching. That's what we need -- patSubst for. let rhs = updateRHS rhs0 updateRHS rhs@A.RHS{} = rhs updateRHS rhs@A.AbsurdRHS{} = rhs updateRHS (A.WithRHS q es cs) = A.WithRHS q es (map updateClause cs) updateRHS (A.RewriteRHS qes rhs wh) = A.RewriteRHS qes (updateRHS rhs) wh updateClause (A.Clause f dots sdots rhs wh ca) = A.Clause f (applySubst patSubst dots) (applySubst patSubst sdots) (updateRHS rhs) wh ca (body, with) <- bindAsPatterns asb $ checkWhere wh $ checkRHS i x aps t' lhsResult rhs -- Note that the with function doesn't necessarily share any part of -- the context with the parent (but withSub will take you from parent -- to child). inTopContext $ Bench.billTo [Bench.Typing, Bench.With] $ checkWithFunction cxtNames with reportSDoc "tc.lhs.top" 10 $ vcat [ text "Clause before translation:" , nest 2 $ vcat [ text "delta =" <+> do escapeContext (size delta) $ prettyTCM delta , text "ps =" <+> do P.fsep <$> prettyTCMPatterns ps , text "body =" <+> maybe (text "_|_") prettyTCM body ] ] reportSDoc "tc.lhs.top" 60 $ escapeContext (size delta) $ vcat [ text "Clause before translation (raw):" , nest 2 $ vcat [ text "ps =" <+> text (show ps) , text "body =" <+> text (show body) ] ] -- compute body modification for irrelevant definitions, see issue 610 rel <- asks envRelevance let bodyMod body = case rel of Irrelevant -> dontCare <$> body _ -> body -- absurd clauses don't define computational behaviour, so it's fine to -- treat them as catchalls. let catchall' = catchall || isNothing body return $ Clause { clauseLHSRange = getRange i , clauseFullRange = getRange c , clauseTel = killRange delta , namedClausePats = ps , clauseBody = bodyMod body , clauseType = Just trhs , clauseCatchall = catchall' , clauseUnreachable = Nothing -- we don't know yet } -- | Type check the @with@ and @rewrite@ lhss and/or the rhs. checkRHS :: LHSInfo -- ^ Range of lhs. -> QName -- ^ Name of function. -> [NamedArg A.Pattern] -- ^ Patterns in lhs. -> Type -- ^ Top-level type of function. -> LHSResult -- ^ Result of type-checking patterns -> A.RHS -- ^ Rhs to check. -> TCM (Maybe Term, WithFunctionProblem) -- Note: the as-bindings are already bound (in checkClause) checkRHS i x aps t lhsResult@(LHSResult _ delta ps trhs _ _asb) rhs0 = handleRHS rhs0 where absurdPat = containsAbsurdPattern aps handleRHS rhs = case rhs of -- Case: ordinary RHS A.RHS e _ -> Bench.billTo [Bench.Typing, Bench.CheckRHS] $ do when absurdPat $ typeError $ AbsurdPatternRequiresNoRHS aps v <- checkExpr e $ unArg trhs return (Just v, NoWithFunction) -- Case: no RHS A.AbsurdRHS -> do unless absurdPat $ typeError $ NoRHSRequiresAbsurdPattern aps return (Nothing, NoWithFunction) -- Case: @rewrite@ -- Andreas, 2014-01-17, Issue 1402: -- If the rewrites are discarded since lhs=rhs, then -- we can actually have where clauses. A.RewriteRHS [] rhs wh -> checkWhere wh $ handleRHS rhs A.RewriteRHS ((qname,eq):qes) rhs wh -> do -- Action for skipping this rewrite. -- We do not want to create unsolved metas in case of -- a futile rewrite with a reflexive equation. -- Thus, we restore the state in this case, -- unless the rewrite expression contains questionmarks. st <- get let recurse = do st' <- get -- Comparing the whole stInteractionPoints maps is a bit -- wasteful, but we assume -- 1. rewriting with a reflexive equality to happen rarely, -- 2. especially with ?-holes in the rewrite expression -- 3. and a large overall number of ?s. let sameIP = (==) `on` (^.stInteractionPoints) when (sameIP st st') $ put st handleRHS $ A.RewriteRHS qes rhs wh -- Get value and type of rewrite-expression. (proof, eqt) <- inferExpr eq -- Andreas, 2016-04-14, see also Issue #1796 -- Run the size constraint solver to improve with-abstraction -- in case the with-expression contains size metas. solveSizeConstraints DefaultToInfty -- Check that the type is actually an equality (lhs ≡ rhs) -- and extract lhs, rhs, and their type. t' <- reduce =<< instantiateFull eqt (eqt,rewriteType,rewriteFrom,rewriteTo) <- equalityView t' >>= \case eqt@(EqualityType _s _eq _params (Arg _ dom) a b) -> do s <- inferSort dom return (eqt, El s dom, unArg a, unArg b) -- Note: the sort _s of the equality need not be the sort of the type @dom@! OtherType{} -> typeError . GenericDocError =<< do text "Cannot rewrite by equation of type" <+> prettyTCM t' -- Get the name of builtin REFL. Con reflCon _ [] <- ignoreSharing <$> primRefl reflInfo <- fmap (setOrigin Inserted) <$> getReflArgInfo reflCon -- Andreas, 2017-01-11: -- The test for refl is obsolete after fixes of #520 and #1740. -- -- Andreas, 2014-05-17 Issue 1110: -- -- Rewriting with @refl@ has no effect, but gives an -- -- incomprehensible error message about the generated -- -- with clause. Thus, we rather do simply nothing if -- -- rewriting with @refl@ is attempted. -- let isReflProof = do -- v <- reduce proof -- case ignoreSharing v of -- Con c _ [] | c == reflCon -> return True -- _ -> return False -- ifM isReflProof recurse $ {- else -} do -- Process 'rewrite' clause like a suitable 'with' clause. -- The REFL constructor might have an argument let reflPat = A.ConP (ConPatInfo ConOCon patNoRange) (AmbQ [conName reflCon]) $ maybeToList $ fmap (\ ai -> Arg ai $ unnamed $ A.WildP patNoRange) reflInfo -- Andreas, 2015-12-25 Issue #1740: -- After the fix of #520, rewriting with a reflexive equation -- has to be desugared as matching against refl. let isReflexive = tryConversion $ dontAssignMetas $ equalTerm rewriteType rewriteFrom rewriteTo (pats, withExpr, withType) <- do ifM isReflexive {-then-} (return ([ reflPat ], proof, OtherType t')) {-else-} (return ([ A.WildP patNoRange, reflPat ], proof, eqt)) let rhs' = insertPatterns pats rhs (rhs'', outerWhere) -- the where clauses should go on the inner-most with | null qes = (rhs', wh) | otherwise = (A.RewriteRHS qes rhs' wh, []) -- Andreas, 2014-03-05 kill range of copied patterns -- since they really do not have a source location. cs = [A.Clause (A.LHS i (A.LHSHead x (killRange aps)) pats) [] [] rhs'' outerWhere False] checkWithRHS x qname t lhsResult [withExpr] [withType] cs -- Case: @with@ A.WithRHS aux es cs -> do reportSDoc "tc.with.top" 15 $ vcat [ text "TC.Rules.Def.checkclause reached A.WithRHS" , sep $ prettyA aux : map (parens . prettyA) es ] reportSDoc "tc.with.top" 20 $ do nfv <- getCurrentModuleFreeVars m <- currentModule sep [ text "with function module:" <+> prettyList (map prettyTCM $ mnameToList m) , text $ "free variables: " ++ show nfv ] -- Infer the types of the with expressions (vs0, as) <- unzip <$> mapM inferExprForWith es -- Andreas, 2016-01-23, Issue #1796 -- Run the size constraint solver to improve with-abstraction -- in case the with-expression contains size metas. solveSizeConstraints DefaultToInfty checkWithRHS x aux t lhsResult vs0 (map OtherType as) cs checkWithRHS :: QName -- ^ Name of function. -> QName -- ^ Name of the with-function. -> Type -- ^ Type of function. -> LHSResult -- ^ Result of type-checking patterns -> [Term] -- ^ With-expressions. -> [EqualityView] -- ^ Types of with-expressions. -> [A.Clause] -- ^ With-clauses to check. -> TCM (Maybe Term, WithFunctionProblem) -- Note: as-bindings already bound (in checkClause) checkWithRHS x aux t (LHSResult npars delta ps trhs _ _asb) vs0 as cs = Bench.billTo [Bench.Typing, Bench.With] $ do let withArgs = withArguments vs0 as perm = fromMaybe __IMPOSSIBLE__ $ dbPatPerm ps (vs, as) <- normalise (vs0, as) -- Andreas, 2012-09-17: for printing delta, -- we should remove it from the context first reportSDoc "tc.with.top" 25 $ escapeContext (size delta) $ vcat [ text "delta =" <+> prettyTCM delta ] reportSDoc "tc.with.top" 25 $ vcat [ text "vs =" <+> prettyTCM vs , text "as =" <+> prettyTCM as , text "perm =" <+> text (show perm) ] -- Split the telescope into the part needed to type the with arguments -- and all the other stuff (delta1, delta2, perm', t', as, vs) <- return $ splitTelForWith delta (unArg trhs) as vs let finalPerm = composeP perm' perm reportSLn "tc.with.top" 75 $ "delta = " ++ show delta -- Andreas, 2012-09-17: for printing delta, -- we should remove it from the context first reportSDoc "tc.with.top" 25 $ escapeContext (size delta) $ vcat [ text "delta1 =" <+> prettyTCM delta1 , text "delta2 =" <+> addContext delta1 (prettyTCM delta2) ] reportSDoc "tc.with.top" 25 $ vcat [ text "perm' =" <+> text (show perm') , text "fPerm =" <+> text (show finalPerm) ] -- Create the body of the original function -- All the context variables us <- getContextArgs let n = size us m = size delta -- First the variables bound outside this definition (us0, us1') = splitAt (n - m) us -- Then permute the rest and grab those needed to for the with arguments (us1, us2) = splitAt (size delta1) $ permute perm' us1' -- Now stuff the with arguments in between and finish with the remaining variables v = Def aux $ map Apply $ us0 ++ us1 ++ map defaultArg withArgs ++ us2 -- Andreas, 2013-02-26 add with-name to signature for printing purposes addConstant aux =<< do useTerPragma $ defaultDefn defaultArgInfo aux typeDontCare emptyFunction -- Andreas, 2013-02-26 separate msgs to see which goes wrong reportSDoc "tc.with.top" 20 $ text " with arguments" <+> do escapeContext (size delta) $ addContext delta1 $ prettyList (map prettyTCM vs) reportSDoc "tc.with.top" 20 $ text " types" <+> do escapeContext (size delta) $ addContext delta1 $ prettyList (map prettyTCM as) reportSDoc "tc.with.top" 20 $ text "with function call" <+> prettyTCM v reportSDoc "tc.with.top" 20 $ text " context" <+> (prettyTCM =<< getContextTelescope) reportSDoc "tc.with.top" 20 $ text " delta" <+> do escapeContext (size delta) $ prettyTCM delta reportSDoc "tc.with.top" 20 $ text " delta1" <+> do escapeContext (size delta) $ prettyTCM delta1 reportSDoc "tc.with.top" 20 $ text " delta2" <+> do escapeContext (size delta) $ addContext delta1 $ prettyTCM delta2 reportSDoc "tc.with.top" 20 $ text " body" <+> prettyTCM v return (Just v, WithFunction x aux t delta delta1 delta2 vs as t' ps npars perm' perm finalPerm cs) -- | Invoked in empty context. checkWithFunction :: [Name] -> WithFunctionProblem -> TCM () checkWithFunction _ NoWithFunction = return () checkWithFunction cxtNames (WithFunction f aux t delta delta1 delta2 vs as b qs npars perm' perm finalPerm cs) = do let -- Δ₁ ws Δ₂ ⊢ withSub : Δ′ (where Δ′ is the context of the parent lhs) withSub :: Substitution withSub = liftS (size delta2) (wkS (countWithArgs as) idS) `composeS` renaming __IMPOSSIBLE__ (reverseP perm') reportSDoc "tc.with.top" 10 $ vcat [ text "checkWithFunction" , nest 2 $ vcat [ text "delta1 =" <+> prettyTCM delta1 , text "delta2 =" <+> addContext delta1 (prettyTCM delta2) , text "t =" <+> prettyTCM t , text "as =" <+> addContext delta1 (prettyTCM as) , text "vs =" <+> do addContext delta1 $ prettyTCM vs , text "b =" <+> do addContext delta1 $ addContext delta2 $ prettyTCM b , text "qs =" <+> do addContext delta $ prettyTCMPatternList qs , text "perm' =" <+> text (show perm') , text "perm =" <+> text (show perm) , text "fperm =" <+> text (show finalPerm) , text "withSub=" <+> text (show withSub) ] ] -- Add the type of the auxiliary function to the signature -- Generate the type of the with function delta1 <- normalise delta1 -- Issue 1332: checkInternal is picky about argInfo -- but module application is sloppy. -- We normalise to get rid of Def's coming -- from module applications. (withFunType, n) <- withFunctionType delta1 vs as delta2 b reportSDoc "tc.with.type" 10 $ sep [ text "with-function type:", nest 2 $ prettyTCM withFunType ] reportSDoc "tc.with.type" 50 $ sep [ text "with-function type:", nest 2 $ pretty withFunType ] -- Andreas, 2013-10-21 -- Check generated type directly in internal syntax. setCurrentRange cs (traceCall NoHighlighting $ -- To avoid flicker. checkType withFunType) `catchError` \err -> case err of TypeError s e -> do put s wt <- reify withFunType enterClosure e $ do traceCall (CheckWithFunctionType wt) . typeError err -> throwError err -- With display forms are closed df <- makeGlobal =<< withDisplayForm f aux delta1 delta2 n qs perm' perm reportSLn "tc.with.top" 20 "created with display form" case dget df of Display n ts dt -> reportSDoc "tc.with.top" 20 $ text "Display" <+> fsep [ text (show n) , prettyList $ map prettyTCM ts , prettyTCM dt ] addConstant aux =<< do useTerPragma $ (defaultDefn defaultArgInfo aux withFunType emptyFunction) { defDisplay = [df] } -- solveSizeConstraints -- Andreas, 2012-10-16 does not seem necessary reportSDoc "tc.with.top" 10 $ sep [ text "added with function" <+> (prettyTCM aux) <+> text "of type" , nest 2 $ prettyTCM withFunType , nest 2 $ text "-|" <+> (prettyTCM =<< getContextTelescope) ] reportSDoc "tc.with.top" 70 $ vcat [ nest 2 $ text $ "raw with func. type = " ++ show withFunType ] -- Construct the body for the with function cs <- return $ map (A.lhsToSpine) cs cs <- buildWithFunction cxtNames f aux t delta qs npars withSub finalPerm (size delta1) n cs cs <- return $ map (A.spineToLhs) cs -- Check the with function checkFunDefS withFunType defaultArgInfo NotDelayed Nothing (Just f) info aux (Just withSub) cs where info = Info.mkDefInfo (nameConcrete $ qnameName aux) noFixity' PublicAccess ConcreteDef (getRange cs) -- | Type check a where clause. checkWhere :: [A.Declaration] -- ^ Where-declarations to check. -> TCM a -- ^ Continuation. -> TCM a checkWhere ds ret = loop ds where loop ds = case ds of [] -> ret [A.ScopedDecl scope ds] -> withScope_ scope $ loop ds [A.Section _ m tel ds] -> newSection m tel $ do local (\ e -> e { envCheckingWhere = True }) $ do checkDecls ds ret _ -> __IMPOSSIBLE__ -- | Enter a new section during type-checking. newSection :: ModuleName -> A.Telescope -> TCM a -> TCM a newSection m tel cont = do reportSDoc "tc.section" 10 $ text "checking section" <+> prettyTCM m <+> fsep (map prettyAs tel) checkTelescope tel $ \ tel' -> do reportSDoc "tc.section" 10 $ text "adding section:" <+> prettyTCM m <+> text (show (size tel')) addSection m reportSDoc "tc.section" 10 $ inTopContext $ nest 4 $ text "actual tele:" <+> do prettyTCM =<< lookupSection m withCurrentModule m cont -- | Set the current clause number. atClause :: QName -> Int -> A.RHS -> TCM a -> TCM a atClause name i rhs = local $ \ e -> e { envClause = IPClause name i rhs } Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Term.hs0000644000000000000000000031076313154613124020054 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.Rules.Term where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Arrow ((&&&), (***), first, second) import Control.Monad.Trans import Control.Monad.Trans.Maybe import Control.Monad.State (get, put) import Control.Monad.Reader import Data.Maybe import Data.Either (partitionEithers) import Data.Monoid (mappend) import qualified Data.List as List import qualified Data.Map as Map import qualified Data.Set as Set import Data.Traversable (sequenceA) import Data.Void import Agda.Interaction.Options import Agda.Interaction.Highlighting.Generate (storeDisambiguatedName) import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Views as A import qualified Agda.Syntax.Info as A import Agda.Syntax.Concrete.Pretty () -- only Pretty instances import Agda.Syntax.Concrete (FieldAssignment'(..), nameFieldA, exprFieldA) import qualified Agda.Syntax.Concrete.Name as C import Agda.Syntax.Common import Agda.Syntax.Fixity import Agda.Syntax.Internal as I import Agda.Syntax.Position import Agda.Syntax.Literal import qualified Agda.Syntax.Reflected as R import Agda.Syntax.Scope.Base ( ThingsInScope, AbstractName , emptyScopeInfo , exportedNamesInScope) import Agda.Syntax.Scope.Monad (getNamedScope) import Agda.Syntax.Translation.InternalToAbstract (reify) import Agda.Syntax.Translation.ReflectedToAbstract (toAbstract_) import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Constraints import Agda.TypeChecking.Conversion import Agda.TypeChecking.Datatypes import Agda.TypeChecking.EtaContract import Agda.TypeChecking.Free (isBinderUsed) import Agda.TypeChecking.Implicit import Agda.TypeChecking.InstanceArguments import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.Level import Agda.TypeChecking.MetaVars import Agda.TypeChecking.Patterns.Abstract import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Pretty import Agda.TypeChecking.Primitive import Agda.TypeChecking.Quote import Agda.TypeChecking.Unquote import Agda.TypeChecking.RecordPatterns import Agda.TypeChecking.Records import Agda.TypeChecking.Reduce import Agda.TypeChecking.SizedTypes import Agda.TypeChecking.SizedTypes.Solve import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Rules.LHS import {-# SOURCE #-} Agda.TypeChecking.Empty (isEmptyType) import {-# SOURCE #-} Agda.TypeChecking.Rules.Decl (checkSectionApplication) import {-# SOURCE #-} Agda.TypeChecking.Rules.Def (checkFunDef, checkFunDef', useTerPragma) import Agda.Utils.Either import Agda.Utils.Except ( ExceptT , MonadError(catchError, throwError) , runExceptT ) import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Pretty ( prettyShow ) import qualified Agda.Utils.Pretty as P import Agda.Utils.Size import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Types --------------------------------------------------------------------------- -- | Check that an expression is a type. isType :: A.Expr -> Sort -> TCM Type isType e s = traceCall (IsTypeCall e s) $ do v <- checkExpr e (sort s) return $ El s v -- | Check that an expression is a type without knowing the sort. isType_ :: A.Expr -> TCM Type isType_ e = traceCall (IsType_ e) $ sharedType =<< do let fallback = isType e =<< do workOnTypes $ newSortMeta case unScope e of A.Fun i (Arg info t) b -> do a <- Dom info <$> isType_ t b <- isType_ b s <- ptsRule a b let t' = El s $ Pi a $ NoAbs underscore b noFunctionsIntoSize b t' return t' A.Pi _ tel e | null tel -> isType_ e A.Pi _ tel e -> do (t0, t') <- checkPiTelescope tel $ \ tel -> do t0 <- instantiateFull =<< isType_ e tel <- instantiateFull tel return (t0, telePi tel t0) noFunctionsIntoSize t0 t' return t' A.Set _ n -> do return $ sort (mkType n) A.App i s arg | visible arg, A.Set _ 0 <- unScope s -> ifNotM hasUniversePolymorphism (typeError $ GenericError "Use --universe-polymorphism to enable level arguments to Set") $ {- else -} do lvl <- levelType -- allow NonStrict variables when checking level -- Set : (NonStrict) Level -> Set\omega n <- levelView =<< do applyRelevanceToContext NonStrict $ checkNamedArg arg lvl return $ sort (Type n) -- Issue #707: Check an existing interaction point A.QuestionMark minfo ii -> caseMaybeM (lookupInteractionMeta ii) fallback $ \ x -> do -- -- | Just x <- A.metaNumber minfo -> do reportSDoc "tc.ip" 20 $ fsep [ text "Rechecking meta " , prettyTCM x , text $ " for interaction point " ++ show ii ] mv <- lookupMeta x let s0 = jMetaType . mvJudgement $ mv -- Andreas, 2016-10-14, issue #2257 -- The meta was created in a context of length @n@. let n = length . envContext . clEnv . miClosRange . mvInfo $ mv (vs, rest) <- splitAt n <$> getContextArgs reportSDoc "tc.ip" 20 $ vcat [ text " s0 = " <+> prettyTCM s0 , text " vs = " <+> prettyTCM vs , text " rest = " <+> prettyTCM rest ] -- We assume the meta variable use here is in an extension of the original context. -- If not we revert to the old buggy behavior of #707 (see test/Succeed/Issue2257b). if (length vs /= n) then fallback else do s1 <- piApplyM s0 vs case ignoreSharing $ unEl s1 of Sort s -> return $ El s $ MetaV x $ map Apply vs _ -> __IMPOSSIBLE__ _ -> fallback ptsRule :: (LensSort a, LensSort b) => a -> b -> TCM Sort ptsRule a b = pts <$> reduce (getSort a) <*> reduce (getSort b) -- | Ensure that a (freshly created) function type does not inhabit 'SizeUniv'. -- Precondition: When @noFunctionsIntoSize t tBlame@ is called, -- we are in the context of @tBlame@ in order to print it correctly. -- Not being in context of @t@ should not matter, as we are only -- checking whether its sort reduces to 'SizeUniv'. noFunctionsIntoSize :: Type -> Type -> TCM () noFunctionsIntoSize t tBlame = do reportSDoc "tc.fun" 20 $ do let El s (Pi dom b) = ignoreSharing <$> tBlame sep [ text "created function type " <+> prettyTCM tBlame , text "with pts rule" <+> prettyTCM (getSort dom, getSort b, s) ] s <- reduce $ getSort t when (s == SizeUniv) $ do -- Andreas, 2015-02-14 -- We have constructed a function type in SizeUniv -- which is illegal to prevent issue 1428. typeError $ FunctionTypeInSizeUniv $ unEl tBlame -- | Check that an expression is a type which is equal to a given type. isTypeEqualTo :: A.Expr -> Type -> TCM Type isTypeEqualTo e0 t = scopedExpr e0 >>= \case A.ScopedExpr{} -> __IMPOSSIBLE__ A.Underscore i | A.metaNumber i == Nothing -> return t e -> workOnTypes $ do t' <- isType e (getSort t) t' <$ leqType t t' leqType_ :: Type -> Type -> TCM () leqType_ t t' = workOnTypes $ leqType t t' --------------------------------------------------------------------------- -- * Telescopes --------------------------------------------------------------------------- -- | Type check a (module) telescope. -- Binds the variables defined by the telescope. checkTelescope :: A.Telescope -> (Telescope -> TCM a) -> TCM a checkTelescope = checkTelescope' LamNotPi -- | Type check the telescope of a dependent function type. -- Binds the resurrected variables defined by the telescope. -- The returned telescope is unmodified (not resurrected). checkPiTelescope :: A.Telescope -> (Telescope -> TCM a) -> TCM a checkPiTelescope = checkTelescope' PiNotLam -- | Flag to control resurrection on domains. data LamOrPi = LamNotPi -- ^ We are checking a module telescope. -- We pass into the type world to check the domain type. -- This resurrects the whole context. | PiNotLam -- ^ We are checking a telescope in a Pi-type. -- We stay in the term world, but add resurrected -- domains to the context to check the remaining -- domains and codomain of the Pi-type. deriving (Eq, Show) -- | Type check a telescope. Binds the variables defined by the telescope. checkTelescope' :: LamOrPi -> A.Telescope -> (Telescope -> TCM a) -> TCM a checkTelescope' lamOrPi [] ret = ret EmptyTel checkTelescope' lamOrPi (b : tel) ret = checkTypedBindings lamOrPi b $ \tel1 -> checkTelescope' lamOrPi tel $ \tel2 -> ret $ abstract tel1 tel2 -- | Check a typed binding and extends the context with the bound variables. -- The telescope passed to the continuation is valid in the original context. -- -- Parametrized by a flag wether we check a typed lambda or a Pi. This flag -- is needed for irrelevance. checkTypedBindings :: LamOrPi -> A.TypedBindings -> (Telescope -> TCM a) -> TCM a checkTypedBindings lamOrPi (A.TypedBindings i (Arg info b)) ret = checkTypedBinding lamOrPi info b $ \ bs -> ret $ telFromList bs checkTypedBinding :: LamOrPi -> ArgInfo -> A.TypedBinding -> (ListTel -> TCM a) -> TCM a checkTypedBinding lamOrPi info (A.TBind i xs e) ret = do -- Andreas, 2011-04-26 irrelevant function arguments may appear -- non-strictly in the codomain type -- 2011-10-04 if flag --experimental-irrelevance is set experimental <- optExperimentalIrrelevance <$> pragmaOptions t <- modEnv lamOrPi $ isType_ e let info' = mapRelevance (modRel lamOrPi experimental) info addContext' (xs, Dom info' t) $ ret $ bindsWithHidingToTel xs (Dom info t) where -- if we are checking a typed lambda, we resurrect before we check the -- types, but do not modify the new context entries -- otherwise, if we are checking a pi, we do not resurrect, but -- modify the new context entries modEnv LamNotPi = workOnTypes modEnv _ = id modRel PiNotLam xp = if xp then irrToNonStrict . nonStrictToRel else nonStrictToRel modRel _ _ = id checkTypedBinding lamOrPi info (A.TLet _ lbs) ret = do checkLetBindings lbs (ret []) --------------------------------------------------------------------------- -- * Lambda abstractions --------------------------------------------------------------------------- -- | Type check a lambda expression. checkLambda :: Arg A.TypedBinding -> A.Expr -> Type -> TCM Term checkLambda (Arg _ (A.TLet _ lbs)) body target = checkLetBindings lbs (checkExpr body target) checkLambda (Arg info (A.TBind _ xs typ)) body target = do reportSLn "tc.term.lambda" 60 $ "checkLambda xs = " ++ prettyShow xs let numbinds = length xs TelV tel btyp <- telViewUpTo numbinds target if size tel < numbinds || numbinds /= 1 then dontUseTargetType else useTargetType tel btyp where dontUseTargetType = do -- Checking λ (xs : argsT) → body : target verboseS "tc.term.lambda" 5 $ tick "lambda-no-target-type" -- First check that argsT is a valid type argsT <- workOnTypes $ Dom info <$> isType_ typ -- Andreas, 2015-05-28 Issue 1523 -- If argsT is a SizeLt, it must be non-empty to avoid non-termination. -- TODO: do we need to block checkExpr? checkSizeLtSat $ unEl $ unDom argsT -- In order to have as much type information as possible when checking -- body, we first unify (xs : argsT) → ?t₁ with the target type. If this -- is inconclusive we need to block the resulting term so we create a -- fresh problem for the check. let tel = telFromList $ bindsWithHidingToTel xs argsT reportSLn "tc.term.lambda" 60 $ "dontUseTargetType tel = " ++ show tel -- DONT USE tel for addContext, as it loses NameIds. -- WRONG: t1 <- addContext tel $ workOnTypes newTypeMeta_ t1 <- addContext (xs, argsT) $ workOnTypes newTypeMeta_ -- Do not coerce hidden lambdas if notVisible info || any notVisible xs then do pid <- newProblem_ $ leqType (telePi tel t1) target -- Now check body : ?t₁ -- WRONG: v <- addContext tel $ checkExpr body t1 v <- addContext' (xs, argsT) $ checkExpr body t1 -- Block on the type comparison blockTermOnProblem target (teleLam tel v) pid else do -- Now check body : ?t₁ -- WRONG: v <- addContext tel $ checkExpr body t1 v <- addContext' (xs, argsT) $ checkExpr body t1 -- Block on the type comparison coerce (teleLam tel v) (telePi tel t1) target useTargetType tel@(ExtendTel dom (Abs y EmptyTel)) btyp = do verboseS "tc.term.lambda" 5 $ tick "lambda-with-target-type" reportSLn "tc.term.lambda" 60 $ "useTargetType y = " ++ y -- merge in the hiding info of the TBind let [WithHiding h x] = xs info <- return $ mapHiding (mappend h) info unless (sameHiding dom info) $ typeError $ WrongHidingInLambda target -- Andreas, 2011-10-01 ignore relevance in lambda if not explicitly given info <- lambdaIrrelevanceCheck info dom -- Andreas, 2015-05-28 Issue 1523 -- Ensure we are not stepping under a possibly non-existing size. -- TODO: do we need to block checkExpr? let a = unDom dom checkSizeLtSat $ unEl a -- We only need to block the final term on the argument type -- comparison. The body will be blocked if necessary. We still want to -- compare the argument types first, so we spawn a new problem for that -- check. (pid, argT) <- newProblem $ isTypeEqualTo typ a -- Andreas, Issue 630: take name from function type if lambda name is "_" v <- lambdaAddContext x y (Dom info argT) $ checkExpr body btyp blockTermOnProblem target (Lam info $ Abs (nameToArgName x) v) pid useTargetType _ _ = __IMPOSSIBLE__ -- | Check that irrelevance info in lambda is compatible with irrelevance -- coming from the function type. -- If lambda has no user-given relevance, copy that of function type. lambdaIrrelevanceCheck :: LensRelevance dom => ArgInfo -> dom -> TCM ArgInfo lambdaIrrelevanceCheck info dom -- Case: no specific user annotation: use relevance of function type | isRelevant info = return $ setRelevance (getRelevance dom) info -- Case: explicit user annotation is taken seriously | otherwise = do let rPi = getRelevance dom -- relevance of function type let rLam = getRelevance info -- relevance of lambda -- Andreas, 2017-01-24, issue #2429 -- we should report an error if we try to check a relevant function -- against an irrelevant function type (subtyping violation) unless (moreRelevant rPi rLam) $ do -- @rLam == Relevant@ is impossible here -- @rLam == Irrelevant@ is impossible here (least relevant) -- this error can only happen if @rLam == NonStrict@ and @rPi == Irrelevant@ unless (rLam == NonStrict) __IMPOSSIBLE__ -- separate tests for separate line nums unless (rPi == Irrelevant) __IMPOSSIBLE__ typeError WrongIrrelevanceInLambda return info lambdaAddContext :: Name -> ArgName -> Dom Type -> TCM a -> TCM a lambdaAddContext x y dom | isNoName x = addContext' (notInScopeName y, dom) -- Note: String instance | otherwise = addContext' (x, dom) -- Name instance of addContext' -- | Checking a lambda whose domain type has already been checked. checkPostponedLambda :: Arg ([WithHiding Name], Maybe Type) -> A.Expr -> Type -> TCM Term checkPostponedLambda args@(Arg _ ([] , _ )) body target = do checkExpr body target checkPostponedLambda args@(Arg info (WithHiding h x : xs, mt)) body target = do let postpone _ t = postponeTypeCheckingProblem_ $ CheckLambda args body t lamHiding = mappend h $ getHiding info insertHiddenLambdas lamHiding target postpone $ \ t@(El _ (Pi dom b)) -> do -- Andreas, 2011-10-01 ignore relevance in lambda if not explicitly given info' <- setHiding lamHiding <$> lambdaIrrelevanceCheck info dom -- We only need to block the final term on the argument type -- comparison. The body will be blocked if necessary. We still want to -- compare the argument types first, so we spawn a new problem for that -- check. mpid <- caseMaybe mt (return Nothing) $ \ ascribedType -> Just <$> do newProblem_ $ leqType (unDom dom) ascribedType -- We type-check the body with the ascribedType given by the user -- to get better error messages. -- Using the type dom from the usage context would be more precise, -- though. let dom' = setRelevance (getRelevance info') . setHiding lamHiding $ maybe dom (dom $>) mt v <- lambdaAddContext x (absName b) dom' $ checkPostponedLambda (Arg info (xs, mt)) body $ absBody b let v' = Lam info' $ Abs (nameToArgName x) v maybe (return v') (blockTermOnProblem t v') mpid -- | Insert hidden lambda until the hiding info of the domain type -- matches the expected hiding info. -- Throws 'WrongHidingInLambda' insertHiddenLambdas :: Hiding -- ^ Expected hiding. -> Type -- ^ Expected to be a function type. -> (MetaId -> Type -> TCM Term) -- ^ Continuation on blocked type. -> (Type -> TCM Term) -- ^ Continuation when expected hiding found. -- The continuation may assume that the @Type@ -- is of the form @(El _ (Pi _ _))@. -> TCM Term -- ^ Term with hidden lambda inserted. insertHiddenLambdas h target postpone ret = do -- If the target type is blocked, we postpone, -- because we do not know if a hidden lambda needs to be inserted. ifBlockedType target postpone $ \ t0 -> do let t = ignoreSharing <$> t0 case unEl t of Pi dom b -> do let h' = getHiding dom -- Found expected hiding: return function type. if sameHiding h h' then ret t else do -- Found a visible argument but expected a hidden one: -- That's an error, as we cannot insert a visible lambda. if visible h' then typeError $ WrongHidingInLambda target else do -- Otherwise, we found a hidden argument that we can insert. let x = absName b Lam (domInfo dom) . Abs x <$> do addContext' (x, dom) $ insertHiddenLambdas h (absBody b) postpone ret _ -> typeError . GenericDocError =<< do text "Expected " <+> prettyTCM target <+> text " to be a function type" -- | @checkAbsurdLambda i h e t@ checks absurd lambda against type @t@. -- Precondition: @e = AbsurdLam i h@ checkAbsurdLambda :: A.LamInfo -> Hiding -> A.Expr -> Type -> TCM Term checkAbsurdLambda i h e t = do t <- instantiateFull t ifBlockedType t (\ m t' -> postponeTypeCheckingProblem_ $ CheckExpr e t') $ \ t' -> do case ignoreSharing $ unEl t' of Pi dom@(Dom info' a) b | not (sameHiding h info') -> typeError $ WrongHidingInLambda t' | not (null $ allMetas a) -> postponeTypeCheckingProblem (CheckExpr e t') $ null . allMetas <$> instantiateFull a | otherwise -> blockTerm t' $ do isEmptyType (getRange i) a -- Add helper function top <- currentModule aux <- qualify top <$> freshName_ (getRange i, absurdLambdaName) -- if we are in irrelevant position, the helper function -- is added as irrelevant rel <- asks envRelevance reportSDoc "tc.term.absurd" 10 $ vcat [ text "Adding absurd function" <+> prettyTCM rel <> prettyTCM aux , nest 2 $ text "of type" <+> prettyTCM t' ] addConstant aux $ (\ d -> (defaultDefn (setRelevance rel info') aux t' d) { defPolarity = [Nonvariant] , defArgOccurrences = [Unused] }) $ emptyFunction { funClauses = [ Clause { clauseLHSRange = getRange e , clauseFullRange = getRange e , clauseTel = telFromList [fmap ("()",) dom] , namedClausePats = [Arg info' $ Named (Just $ unranged $ absName b) $ AbsurdP $ debruijnNamedVar absurdPatternName 0] , clauseBody = Nothing , clauseType = Just $ setRelevance rel $ defaultArg $ absBody b , clauseCatchall = False , clauseUnreachable = Just True -- absurd clauses are unreachable } ] , funCompiled = Just Fail , funTerminates = Just True } -- Andreas 2012-01-30: since aux is lifted to toplevel -- it needs to be applied to the current telescope (issue 557) tel <- getContextTelescope return $ Def aux $ map Apply $ teleArgs tel _ -> typeError $ ShouldBePi t' -- | @checkExtendedLambda i di qname cs e t@ check pattern matching lambda. -- Precondition: @e = ExtendedLam i di qname cs@ checkExtendedLambda :: A.LamInfo -> A.DefInfo -> QName -> [A.Clause] -> A.Expr -> Type -> TCM Term checkExtendedLambda i di qname cs e t = do -- Andreas, 2016-06-16 issue #2045 -- Try to get rid of unsolved size metas before we -- fix the type of the extended lambda auxiliary function solveSizeConstraints DontDefaultToInfty t <- instantiateFull t ifBlockedType t (\ m t' -> postponeTypeCheckingProblem_ $ CheckExpr e t') $ \ t -> do j <- currentOrFreshMutualBlock rel <- asks envRelevance let info = setRelevance rel defaultArgInfo -- Andreas, 2016-07-13, issue 2028. -- Save the state to rollback the changes to the signature. st <- get -- Andreas, 2013-12-28: add extendedlambda as @Function@, not as @Axiom@; -- otherwise, @addClause@ in @checkFunDef'@ fails (see issue 1009). addConstant qname =<< do useTerPragma $ (defaultDefn info qname t emptyFunction) { defMutual = j } reportSDoc "tc.term.exlam" 20 $ text (show $ A.defAbstract di) <+> text "extended lambda's implementation \"" <> prettyTCM qname <> text "\" has type: " $$ prettyTCM t -- <+> text " where clauses: " <+> text (show cs) args <- getContextArgs freevars <- getCurrentModuleFreeVars let argsNoParam = drop freevars args -- don't count module parameters let (hid, notHid) = List.partition notVisible argsNoParam reportSDoc "tc.term.exlam" 30 $ vcat $ [ text "dropped args: " <+> prettyTCM (take freevars args) , text "hidden args: " <+> prettyTCM hid , text "visible args: " <+> prettyTCM notHid ] -- Andreas, Ulf, 2016-02-02: We want to postpone type checking an extended lambda -- in case the lhs checker failed due to insufficient type info for the patterns. -- Issues 480, 1159, 1811. mx <- catchIlltypedPatternBlockedOnMeta $ abstract (A.defAbstract di) $ checkFunDef' t info NotDelayed (Just $ ExtLamInfo (length hid) (length notHid)) Nothing di qname cs case mx of -- Case: type checking succeeded, so we go ahead. Nothing -> return $ Def qname $ map Apply args -- Case: we could not check the extended lambda because we are blocked on a meta. -- In this case, we want to postpone. Just (err, x) -> do reportSDoc "tc.term.exlam" 50 $ vcat $ [ text "checking extended lambda got stuck on meta: " <+> text (show x) ] -- Note that we messed up the state a bit. We might want to unroll these state changes. -- However, they are mostly harmless: -- 1. We created a new mutual block id. -- 2. We added a constant without definition. -- In fact, they are not so harmless, see issue 2028! -- Thus, reset the state! put st -- The meta might not be known in the reset state, as it could have been created -- somewhere on the way to the type error. mm <- Map.lookup x <$> getMetaStore x' <- case mvInstantiation <$> mm of -- Case: we do not know the meta -- We mine the type of the extended lambda for a (possibly) blocking meta. Nothing -> do reportSDoc "tc.term.exlam" 50 $ vcat $ [ text "meta was not found in reset state" , text "trying to find meta in type of extlam..." ] case allMetas t of [] -> do reportSDoc "tc.term.exlam" 50 $ text "no meta found, giving up." throwError err (x:_) -> do reportSDoc "tc.term.exlam" 50 $ text $ "found meta: " ++ show x return x -- Case: we know the meta here. Just InstV{} -> __IMPOSSIBLE__ -- It cannot be instantiated yet. Just{} -> return x -- It has to be blocked on some meta, so we can postpone, -- being sure it will be retired when a meta is solved -- (which might be the blocking meta in which case we actually make progress). postponeTypeCheckingProblem (CheckExpr e t) $ isInstantiatedMeta x' where -- Concrete definitions cannot use information about abstract things. abstract ConcreteDef = inConcreteMode abstract AbstractDef = inAbstractMode -- | Run a computation. -- -- * If successful, return Nothing. -- -- * If @IlltypedPattern p a@ is thrown and type @a@ is blocked on some meta @x@ -- return @Just x@. -- -- * If @SplitError (UnificationStuck c tel us vs _)@ is thrown and the unification -- problem @us =?= vs : tel@ is blocked on some meta @x@ return @Just x@. -- -- * If another error was thrown or the type @a@ is not blocked, reraise the error. -- -- Note that the returned meta might only exists in the state where the error was -- thrown, thus, be an invalid 'MetaId' in the current state. -- catchIlltypedPatternBlockedOnMeta :: TCM () -> TCM (Maybe (TCErr, MetaId)) catchIlltypedPatternBlockedOnMeta m = (Nothing <$ do disableDestructiveUpdate m) `catchError` \ err -> do let reraise = throwError err case err of TypeError s cl@Closure{ clValue = IlltypedPattern p a } -> do mx <- localState $ do put s enterClosure cl $ \ _ -> do ifBlockedType a (\ x _ -> return $ Just x) $ {- else -} \ _ -> return Nothing caseMaybe mx reraise $ \ x -> return $ Just (err, x) TypeError s cl@Closure{ clValue = SplitError (UnificationStuck c tel us vs _) } -> do mx <- localState $ do put s enterClosure cl $ \ _ -> do problem <- reduce =<< instantiateFull (flattenTel tel, us, vs) -- over-approximating the set of metas actually blocking unification return $ listToMaybe $ allMetas problem caseMaybe mx reraise $ \ x -> return $ Just (err, x) _ -> reraise --------------------------------------------------------------------------- -- * Records --------------------------------------------------------------------------- expandModuleAssigns :: [Either A.Assign A.ModuleName] -> [C.Name] -> TCM A.Assigns expandModuleAssigns mfs exs = do let (fs , ms) = partitionEithers mfs exs' = exs List.\\ map (view nameFieldA) fs fs' <- forM exs' $ \ f -> do pms <- forM ms $ \ m -> do modScope <- getNamedScope m let names :: ThingsInScope AbstractName names = exportedNamesInScope modScope return $ case Map.lookup f names of Just [n] -> Just (m, FieldAssignment f (A.nameExpr n)) _ -> Nothing case catMaybes pms of [] -> return Nothing [(_, fa)] -> return (Just fa) mfas -> typeError . GenericDocError =<< do vcat $ [ text "Ambiguity: the field" <+> prettyTCM f <+> text "appears in the following modules: " ] ++ map (prettyTCM . fst) mfas return (fs ++ catMaybes fs') -- | @checkRecordExpression fs e t@ checks record construction against type @t@. -- Precondition @e = Rec _ fs@. checkRecordExpression :: A.RecordAssigns -> A.Expr -> Type -> TCM Term checkRecordExpression mfs e t = do reportSDoc "tc.term.rec" 10 $ sep [ text "checking record expression" , prettyA e ] ifBlockedType t (\ _ t -> guessRecordType t) {-else-} $ \ t -> do case ignoreSharing $ unEl t of -- Case: We know the type of the record already. Def r es -> do let ~(Just vs) = allApplyElims es reportSDoc "tc.term.rec" 20 $ text $ " r = " ++ prettyShow r reportSDoc "tc.term.rec" 30 $ text " xs = " <> do text =<< prettyShow . map unArg <$> getRecordFieldNames r reportSDoc "tc.term.rec" 30 $ text " ftel= " <> do prettyTCM =<< getRecordFieldTypes r reportSDoc "tc.term.rec" 30 $ text " con = " <> do text =<< prettyShow <$> getRecordConstructor r def <- getRecordDef r let -- Field names with ArgInfo. axs = recordFieldNames def exs = filter visible axs -- Just field names. xs = map unArg axs -- Record constructor. con = killRange $ recConHead def reportSDoc "tc.term.rec" 20 $ vcat [ text " xs = " <> return (P.pretty xs) , text " ftel= " <> prettyTCM (recTel def) , text " con = " <> return (P.pretty con) ] -- Compute the list of given fields, decorated with the ArgInfo from the record def. fs <- expandModuleAssigns mfs (map unArg exs) -- Compute a list of metas for the missing visible fields. scope <- getScope let re = getRange e meta x = A.Underscore $ A.MetaInfo re scope Nothing (prettyShow x) -- In @es@ omitted explicit fields are replaced by underscores. -- Omitted implicit or instance fields -- are still left out and inserted later by checkArguments_. es <- insertMissingFields r meta fs axs args <- checkArguments_ ExpandLast re es (recTel def `apply` vs) >>= \case (args, remainingTel) | null remainingTel -> return args _ -> __IMPOSSIBLE__ -- Don't need to block here! reportSDoc "tc.term.rec" 20 $ text $ "finished record expression" return $ Con con ConORec args _ -> typeError $ ShouldBeRecordType t where guessRecordType t = do let fields = [ x | Left (FieldAssignment x _) <- mfs ] rs <- findPossibleRecords fields case rs of -- If there are no records with the right fields we might as well fail right away. [] -> case fields of [] -> typeError $ GenericError "There are no records in scope" [f] -> typeError $ GenericError $ "There is no known record with the field " ++ prettyShow f _ -> typeError $ GenericError $ "There is no known record with the fields " ++ unwords (map prettyShow fields) -- If there's only one record with the appropriate fields, go with that. [r] -> do def <- getConstInfo r let rt = defType def vs <- newArgsMeta rt target <- reduce $ piApply rt vs s <- case ignoreSharing $ unEl target of Level l -> return $ Type l Sort s -> return s v -> do reportSDoc "impossible" 10 $ vcat [ text "The impossible happened when checking record expression against meta" , text "Candidate record type r = " <+> prettyTCM r , text "Type of r = " <+> prettyTCM rt , text "Ends in (should be sort)= " <+> prettyTCM v , text $ " Raw = " ++ show v ] __IMPOSSIBLE__ let inferred = El s $ Def r $ map Apply vs v <- checkExpr e inferred coerce v inferred t -- Andreas 2012-04-21: OLD CODE, WRONG DIRECTION, I GUESS: -- blockTerm t $ v <$ leqType_ t inferred -- If there are more than one possible record we postpone _:_:_ -> do reportSDoc "tc.term.expr.rec" 10 $ sep [ text "Postponing type checking of" , nest 2 $ prettyA e <+> text ":" <+> prettyTCM t ] postponeTypeCheckingProblem_ $ CheckExpr e t -- | @checkRecordUpdate ei recexpr fs e t@ -- Precondition @e = RecUpdate ei recexpr fs@. checkRecordUpdate :: A.ExprInfo -> A.Expr -> A.Assigns -> A.Expr -> Type -> TCM Term checkRecordUpdate ei recexpr fs e t = do case ignoreSharing $ unEl t of Def r vs -> do v <- checkExpr recexpr t name <- freshNoName (getRange recexpr) addLetBinding defaultArgInfo name v t $ do projs <- recFields <$> getRecordDef r axs <- getRecordFieldNames r scope <- getScope let xs = map unArg axs es <- orderFields r Nothing xs $ map (\ (FieldAssignment x e) -> (x, Just e)) fs let es' = zipWith (replaceFields name ei) projs es checkExpr (A.Rec ei [ Left (FieldAssignment x e) | (x, Just e) <- zip xs es' ]) t MetaV _ _ -> do inferred <- inferExpr recexpr >>= reduce . snd case ignoreSharing $ unEl inferred of MetaV _ _ -> postponeTypeCheckingProblem_ $ CheckExpr e t _ -> do v <- checkExpr e inferred coerce v inferred t _ -> typeError $ ShouldBeRecordType t where replaceFields :: Name -> A.ExprInfo -> Arg A.QName -> Maybe A.Expr -> Maybe A.Expr replaceFields n ei a@(Arg _ p) Nothing | visible a = Just $ A.App ei (A.Def p) $ defaultNamedArg $ A.Var n replaceFields _ _ (Arg _ _) Nothing = Nothing replaceFields _ _ _ (Just e) = Just $ e --------------------------------------------------------------------------- -- * Literal --------------------------------------------------------------------------- checkLiteral :: Literal -> Type -> TCM Term checkLiteral lit t = do t' <- litType lit coerce (Lit lit) t' t --------------------------------------------------------------------------- -- * Terms --------------------------------------------------------------------------- -- | @checkArguments' exph r args t0 t k@ tries @checkArguments exph args t0 t@. -- If it succeeds, it continues @k@ with the returned results. If it fails, -- it registers a postponed typechecking problem and returns the resulting new -- meta variable. -- -- Checks @e := ((_ : t0) args) : t@. checkArguments' :: ExpandHidden -> Range -> [NamedArg A.Expr] -> Type -> Type -> (Args -> Type -> TCM Term) -> TCM Term checkArguments' exph r args t0 t k = do z <- runExceptT $ checkArguments exph r args t0 t case z of Right (vs, t1) -> k vs t1 -- vs = evaluated args -- t1 = remaining type (needs to be subtype of t) Left (us, es, t0) -> do reportSDoc "tc.term.expr.args" 80 $ sep [ text "postponed checking arguments" , nest 4 $ prettyList (map (prettyA . namedThing . unArg) args) , nest 2 $ text "against" , nest 4 $ prettyTCM t0 ] $$ sep [ text "progress:" , nest 2 $ text "checked" <+> prettyList (map prettyTCM us) , nest 2 $ text "remaining" <+> sep [ prettyList (map (prettyA . namedThing . unArg) es) , nest 2 $ text ":" <+> prettyTCM t0 ] ] postponeTypeCheckingProblem_ (CheckArgs exph r es t0 t $ \vs t -> k (us ++ vs) t) -- if unsuccessful, postpone checking until t0 unblocks -- | Remove top layers of scope info of expression and set the scope accordingly -- in the 'TCState'. scopedExpr :: A.Expr -> TCM A.Expr scopedExpr (A.ScopedExpr scope e) = setScope scope >> scopedExpr e scopedExpr e = return e -- | Type check an expression. checkExpr :: A.Expr -> Type -> TCM Term checkExpr e t0 = verboseBracket "tc.term.expr.top" 5 "checkExpr" $ traceCall (CheckExprCall e t0) $ localScope $ doExpandLast $ shared =<< do reportSDoc "tc.term.expr.top" 15 $ text "Checking" <+> sep [ fsep [ prettyTCM e, text ":", prettyTCM t0 ] , nest 2 $ text "at " <+> (text . prettyShow =<< getCurrentRange) ] reportSDoc "tc.term.expr.top.detailed" 80 $ text "Checking" <+> fsep [ prettyTCM e, text ":", text (show t0) ] t <- reduce t0 reportSDoc "tc.term.expr.top" 15 $ text " --> " <+> prettyTCM t e <- scopedExpr e tryInsertHiddenLambda e t $ case e of A.ScopedExpr scope e -> __IMPOSSIBLE__ -- setScope scope >> checkExpr e t -- a meta variable without arguments: type check directly for efficiency A.QuestionMark i ii -> checkQuestionMark (newValueMeta' RunMetaOccursCheck) t0 i ii A.Underscore i -> checkUnderscore t0 i A.WithApp _ e es -> typeError $ NotImplemented "type checking of with application" -- check |- Set l : t (requires universe polymorphism) A.App i s arg@(Arg ai l) | A.Set _ 0 <- unScope s, visible ai -> ifNotM hasUniversePolymorphism (typeError $ GenericError "Use --universe-polymorphism to enable level arguments to Set") $ {- else -} do lvl <- levelType -- allow NonStrict variables when checking level -- Set : (NonStrict) Level -> Set\omega n <- levelView =<< do applyRelevanceToContext NonStrict $ checkNamedArg arg lvl -- check that Set (l+1) <= t reportSDoc "tc.univ.poly" 10 $ text "checking Set " <+> prettyTCM n <+> text "against" <+> prettyTCM t coerce (Sort $ Type n) (sort $ sSuc $ Type n) t e0@(A.App i q (Arg ai e)) | A.Quote _ <- unScope q, visible ai -> do let quoted (A.Def x) = return x quoted (A.Macro x) = return x quoted (A.Proj o (AmbQ [x])) = return x quoted (A.Proj o (AmbQ xs)) = typeError $ GenericError $ "quote: Ambigous name: " ++ prettyShow xs quoted (A.Con (AmbQ [x])) = return x quoted (A.Con (AmbQ xs)) = typeError $ GenericError $ "quote: Ambigous name: " ++ prettyShow xs quoted (A.ScopedExpr _ e) = quoted e quoted _ = typeError $ GenericError $ "quote: not a defined name" x <- quoted (namedThing e) ty <- qNameType coerce (quoteName x) ty t | A.QuoteTerm _ <- unScope q -> do (et, _) <- inferExpr (namedThing e) et' <- etaContract =<< instantiateFull et let metas = allMetas et' case metas of _:_ -> postponeTypeCheckingProblem (CheckExpr e0 t) $ andM $ map isInstantiatedMeta metas [] -> do q <- quoteTerm et' ty <- el primAgdaTerm coerce q ty t A.Quote _ -> typeError $ GenericError "quote must be applied to a defined name" A.QuoteTerm _ -> typeError $ GenericError "quoteTerm must be applied to a term" A.Unquote _ -> typeError $ GenericError "unquote must be applied to a term" A.AbsurdLam i h -> checkAbsurdLambda i h e t A.ExtendedLam i di qname cs -> checkExtendedLambda i di qname cs e t A.Lam i (A.DomainFull (A.TypedBindings _ b)) e -> checkLambda b e t A.Lam i (A.DomainFree info x) e0 -> checkExpr (A.Lam i (domainFree info x) e0) t A.Lit lit -> checkLiteral lit t A.Let i ds e -> checkLetBindings ds $ checkExpr e t A.Pi _ tel e | null tel -> checkExpr e t A.Pi _ tel e -> do (t0, t') <- checkPiTelescope tel $ \ tel -> do t0 <- instantiateFull =<< isType_ e tel <- instantiateFull tel return (t0, telePi tel t0) noFunctionsIntoSize t0 t' let s = getSort t' v = unEl t' when (s == Inf) $ reportSDoc "tc.term.sort" 20 $ vcat [ text ("reduced to omega:") , nest 2 $ text "t =" <+> prettyTCM t' , nest 2 $ text "cxt =" <+> (prettyTCM =<< getContextTelescope) ] coerce v (sort s) t A.Fun _ (Arg info a) b -> do a' <- isType_ a b' <- isType_ b s <- ptsRule a' b' let v = Pi (Dom info a') (NoAbs underscore b') noFunctionsIntoSize b' $ El s v coerce v (sort s) t A.Set _ n -> do coerce (Sort $ mkType n) (sort $ mkType $ n + 1) t A.Prop _ -> do typeError $ GenericError "Prop is no longer supported" A.Rec _ fs -> checkRecordExpression fs e t A.RecUpdate ei recexpr fs -> checkRecordUpdate ei recexpr fs e t A.DontCare e -> -- resurrect vars ifM ((Irrelevant ==) <$> asks envRelevance) (dontCare <$> do applyRelevanceToContext Irrelevant $ checkExpr e t) (internalError "DontCare may only appear in irrelevant contexts") e0@(A.QuoteGoal _ x e) -> do qg <- quoteGoal t case qg of Left metas -> postponeTypeCheckingProblem (CheckExpr e0 t) $ andM $ map isInstantiatedMeta metas Right quoted -> do tmType <- agdaTermType (v, ty) <- addLetBinding defaultArgInfo x quoted tmType (inferExpr e) coerce v ty t e0@(A.QuoteContext _) -> do qc <- quoteContext case qc of Left metas -> postponeTypeCheckingProblem (CheckExpr e0 t) $ andM $ map isInstantiatedMeta metas Right quotedContext -> do ctxType <- el $ list $ primArg <@> (unEl <$> agdaTypeType) coerce quotedContext ctxType t e0@(A.Tactic i e xs ys) -> do qc <- quoteContext qg <- quoteGoal t let postpone metas = postponeTypeCheckingProblem (CheckExpr e0 t) $ andM $ map isInstantiatedMeta metas case (qc, qg) of (Left metas1, Left metas2) -> postpone $ metas1 ++ metas2 (Left metas , Right _ ) -> postpone $ metas (Right _ , Left metas ) -> postpone $ metas (Right quotedCtx, Right quotedGoal) -> do quotedCtx <- defaultNamedArg <$> reify quotedCtx quotedGoal <- defaultNamedArg <$> reify quotedGoal let tac = foldl (A.App i) (A.App i (A.App i e quotedCtx) quotedGoal) xs result = foldl (A.App i) (A.Unquote i) (defaultNamedArg tac : ys) checkExpr result t A.ETel _ -> __IMPOSSIBLE__ A.Dot{} -> typeError $ GenericError $ "Invalid dotted expression" -- Application _ | Application hd args <- appView e -> checkApplication hd args e t where -- | Call checkExpr with an hidden lambda inserted if appropriate, -- else fallback. tryInsertHiddenLambda :: A.Expr -> Type -> TCM Term -> TCM Term tryInsertHiddenLambda e t fallback -- Insert hidden lambda if all of the following conditions are met: -- type is a hidden function type, {x : A} -> B or {{x : A}} -> B | Pi (Dom info a) b <- ignoreSharing $ unEl t , let h = getHiding info , notVisible h -- expression is not a matching hidden lambda or question mark , not (hiddenLambdaOrHole h e) = do let proceed = doInsert info $ absName b -- If we skip the lambda insertion for an introduction, -- we will hit a dead end, so proceed no matter what. if definitelyIntroduction then proceed else do -- Andreas, 2017-01-19, issue #2412: -- We do not want to insert a hidden lambda if A is -- possibly empty type of sizes, as this will produce an error. reduce a >>= isSizeType >>= \case Just (BoundedLt u) -> ifBlocked u (\ _ _ -> fallback) $ \ v -> do ifM (checkSizeNeverZero v) proceed fallback _ -> proceed | otherwise = fallback where re = getRange e rx = caseMaybe (rStart re) noRange $ \ pos -> posToRange pos pos doInsert info y = do x <- unshadowName <=< freshName rx $ notInScopeName y reportSLn "tc.term.expr.impl" 15 $ "Inserting implicit lambda" checkExpr (A.Lam (A.defaultLamInfo re) (domainFree info x) e) t hiddenLambdaOrHole h e = case e of A.AbsurdLam _ h' -> sameHiding h h' A.ExtendedLam _ _ _ cls -> any hiddenLHS cls A.Lam _ bind _ -> sameHiding h bind A.QuestionMark{} -> True _ -> False hiddenLHS (A.Clause (A.LHS _ (A.LHSHead _ (a : _)) _) _ _ _ _ _) = notVisible a hiddenLHS _ = False -- Things with are definitely introductions, -- thus, cannot be of hidden Pi-type, unless they are hidden lambdas. definitelyIntroduction = case e of A.Lam{} -> True A.AbsurdLam{} -> True A.Lit{} -> True A.Pi{} -> True A.Fun{} -> True A.Set{} -> True A.Prop{} -> True A.Rec{} -> True A.RecUpdate{} -> True A.ScopedExpr{} -> __IMPOSSIBLE__ A.ETel{} -> __IMPOSSIBLE__ _ -> False --------------------------------------------------------------------------- -- * Reflection --------------------------------------------------------------------------- -- | DOCUMENT ME! quoteGoal :: Type -> TCM (Either [MetaId] Term) quoteGoal t = do t' <- etaContract =<< instantiateFull t let metas = allMetas t' case metas of _:_ -> return $ Left metas [] -> do quotedGoal <- quoteTerm (unEl t') return $ Right quotedGoal -- | DOCUMENT ME! quoteContext :: TCM (Either [MetaId] Term) quoteContext = do contextTypes <- map (fmap snd) <$> getContext contextTypes <- etaContract =<< instantiateFull contextTypes let metas = allMetas contextTypes case metas of _:_ -> return $ Left metas [] -> do quotedContext <- buildList <*> mapM quoteDom contextTypes return $ Right quotedContext -- | Unquote a TCM computation in a given hole. unquoteM :: A.Expr -> Term -> Type -> TCM Term -> TCM Term unquoteM tac hole holeType k = do tac <- checkExpr tac =<< (el primAgdaTerm --> el (primAgdaTCM <#> primLevelZero <@> primUnit)) inFreshModuleIfFreeParams $ unquoteTactic tac hole holeType k -- | DOCUMENT ME! unquoteTactic :: Term -> Term -> Type -> TCM Term -> TCM Term unquoteTactic tac hole goal k = do ok <- runUnquoteM $ unquoteTCM tac hole case ok of Left (BlockedOnMeta oldState x) -> do put oldState mi <- Map.lookup x <$> getMetaStore (r, unblock) <- case mi of Nothing -> do -- fresh meta: need to block on something else! otherMetas <- allMetas <$> instantiateFull goal case otherMetas of [] -> return (noRange, return False) -- Nothing to block on, leave it yellow. Alternative: fail. x:_ -> return (noRange, isInstantiatedMeta x) -- range? Just mi -> return (getRange mi, isInstantiatedMeta x) setCurrentRange r $ postponeTypeCheckingProblem (UnquoteTactic tac hole goal) unblock Left err -> typeError $ UnquoteFailed err Right _ -> k --------------------------------------------------------------------------- -- * Projections --------------------------------------------------------------------------- -- | Inferring the type of an overloaded projection application. -- See 'inferOrCheckProjApp'. inferProjApp :: A.Expr -> ProjOrigin -> [QName] -> A.Args -> TCM (Term, Type) inferProjApp e o ds args0 = inferOrCheckProjApp e o ds args0 Nothing -- | Checking the type of an overloaded projection application. -- See 'inferOrCheckProjApp'. checkProjApp :: A.Expr -> ProjOrigin -> [QName] -> A.Args -> Type -> TCM Term checkProjApp e o ds args0 t = do (v, ti) <- inferOrCheckProjApp e o ds args0 (Just t) coerce v ti t -- | Inferring or Checking an overloaded projection application. -- -- The overloaded projection is disambiguated by inferring the type of its -- principal argument, which is the first visible argument. inferOrCheckProjApp :: A.Expr -- ^ The whole expression which constitutes the application. -> ProjOrigin -- ^ The origin of the projection involved in this projection application. -> [QName] -- ^ The projection name (potentially ambiguous). List must not be empty. -> A.Args -- ^ The arguments to the projection. -> Maybe Type -- ^ The expected type of the expression (if 'Nothing', infer it). -> TCM (Term, Type) -- ^ The type-checked expression and its type (if successful). inferOrCheckProjApp e o ds args mt = do reportSDoc "tc.proj.amb" 20 $ vcat [ text "checking ambiguous projection" , text $ " ds = " ++ prettyShow ds , text " args = " <+> sep (map prettyTCM args) , text " t = " <+> caseMaybe mt (text "Nothing") prettyTCM ] let refuse :: String -> TCM (Term, Type) refuse reason = typeError $ GenericError $ "Cannot resolve overloaded projection " ++ prettyShow (A.nameConcrete $ A.qnameName $ fromMaybe __IMPOSSIBLE__ $ headMaybe ds) ++ " because " ++ reason refuseNotApplied = refuse "it is not applied to a visible argument" refuseNoMatching = refuse "no matching candidate found" refuseNotRecordType = refuse "principal argument is not of record type" -- Postpone the whole type checking problem -- if type of principal argument (or the type where we get it from) -- is blocked by meta m. postpone m = do tc <- caseMaybe mt newTypeMeta_ return v <- postponeTypeCheckingProblem (CheckExpr e tc) $ isInstantiatedMeta m return (v, tc) -- The following cases need to be considered: -- 1. No arguments to the projection. -- 2. Arguments (parameters), but not the principal argument. -- 3. Argument(s) including the principal argument. -- For now, we only allow ambiguous projections if the first visible -- argument is the record value. case filter (visible . snd) $ zip [0..] args of -- Case: we have no visible argument to the projection. -- In inference mode, we really need the visible argument, postponing does not help [] -> caseMaybe mt refuseNotApplied $ \ t -> do -- If we have the type, we can try to get the type of the principal argument. -- It is the first visible argument. TelV _ptel core <- telViewUpTo' (-1) (not . visible) t ifBlockedType core (\ m _ -> postpone m) $ {-else-} \ core -> do ifNotPiType core (\ _ -> refuseNotApplied) $ {-else-} \ dom _b -> do ifBlockedType (unDom dom) (\ m _ -> postpone m) $ {-else-} \ ta -> do caseMaybeM (isRecordType ta) refuseNotRecordType $ \ (_q, _pars, defn) -> do case defn of Record { recFields = fs } -> do case catMaybes $ for fs $ \ (Arg _ f) -> List.find (f ==) ds of [] -> refuseNoMatching [d] -> do storeDisambiguatedName d (,t) <$> checkHeadApplication e t (A.Proj o $ AmbQ [d]) args _ -> __IMPOSSIBLE__ _ -> __IMPOSSIBLE__ -- Case: we have a visible argument ((k, arg) : _) -> do (v0, ta) <- inferExpr $ namedArg arg reportSDoc "tc.proj.amb" 25 $ vcat [ text " principal arg " <+> prettyTCM arg , text " has type " <+> prettyTCM ta ] -- ta should be a record type (after introducing the hidden args in v0) (vargs, ta) <- implicitArgs (-1) (not . visible) ta let v = v0 `apply` vargs ifBlockedType ta (\ m _ -> postpone m) {-else-} $ \ ta -> do caseMaybeM (isRecordType ta) refuseNotRecordType $ \ (q, _pars0, _) -> do -- try to project it with all of the possible projections let try d = do reportSDoc "tc.proj.amb" 30 $ vcat [ text $ "trying projection " ++ prettyShow d , text " td = " <+> caseMaybeM (getDefType d ta) (text "Nothing") prettyTCM ] -- get the original projection name isP <- isProjection d reportSDoc "tc.proj.amb" 40 $ vcat $ [ text $ " isProjection = " ++ caseMaybe isP "no" (const "yes") ] ++ caseMaybe isP [] (\ Projection{ projProper = proper, projOrig = orig } -> [ text $ " proper = " ++ show proper , text $ " orig = " ++ prettyShow orig ]) -- Andreas, 2017-01-21, issue #2422 -- The scope checker considers inherited projections (from nested records) -- as projections and allows overloading. However, since they are defined -- as *composition* of projections, the type checker does *not* recognize them, -- and @isP@ will be @Nothing@. -- However, we can ignore this, as we only need the @orig@inal projection name -- for removing false ambiguity. Thus, we skip these checks: -- Projection{ projProper = proper, projOrig = orig } <- MaybeT $ return isP -- guard $ isJust proper let orig = caseMaybe isP d projOrig -- try to eliminate (dom, u, tb) <- MaybeT (projectTyped v ta o d `catchError` \ _ -> return Nothing) reportSDoc "tc.proj.amb" 30 $ vcat [ text " dom = " <+> prettyTCM dom , text " u = " <+> prettyTCM u , text " tb = " <+> prettyTCM tb ] (q', pars, _) <- MaybeT $ isRecordType $ unDom dom reportSDoc "tc.proj.amb" 30 $ vcat [ text " q = " <+> prettyTCM q , text " q' = " <+> prettyTCM q' ] guard (q == q') -- Get the type of the projection and check -- that the first visible argument is the record value. tfull <- lift $ defType <$> getConstInfo d TelV tel _ <- lift $ telViewUpTo' (-1) (not . visible) tfull reportSDoc "tc.proj.amb" 30 $ vcat [ text $ " size tel = " ++ show (size tel) , text $ " size pars = " ++ show (size pars) ] -- See issue 1960 for when the following assertion fails for -- the correct disambiguation. -- guard (size tel == size pars) return (orig, (d, (pars, (dom, u, tb)))) cands <- groupOn fst . catMaybes <$> mapM (runMaybeT . try) ds case cands of [] -> refuseNoMatching [[]] -> refuseNoMatching (_:_:_) -> refuse $ "several matching candidates found: " ++ prettyShow (map (fst . snd) $ concat cands) -- case: just one matching projection d -- the term u = d v -- the type tb is the type of this application [ (_orig, (d, (pars, (_dom,u,tb)))) : _ ] -> do storeDisambiguatedName d -- Check parameters tfull <- typeOfConst d (_,_) <- checkKnownArguments (take k args) pars tfull -- Check remaining arguments let tc = fromMaybe typeDontCare mt let r = getRange e z <- runExceptT $ checkArguments ExpandLast r (drop (k+1) args) tb tc case z of Right (us, trest) -> return (u `apply` us, trest) -- We managed to check a part of es and got us1, but es2 remain. Left (us1, es2, trest1) -> do -- In the inference case: -- To create a postponed type checking problem, -- we do not use typeDontCare, but create a meta. tc <- caseMaybe mt newTypeMeta_ return v <- postponeTypeCheckingProblem_ $ CheckArgs ExpandLast r es2 trest1 tc $ \ us2 trest -> coerce (u `apply` us1 `apply` us2) trest tc return (v, tc) -- | @checkApplication hd args e t@ checks an application. -- Precondition: @Application hs args = appView e@ -- -- @checkApplication@ disambiguates constructors -- (and continues to 'checkConstructorApplication') -- and resolves pattern synonyms. checkApplication :: A.Expr -> A.Args -> A.Expr -> Type -> TCM Term checkApplication hd args e t = do reportSDoc "tc.check.app" 20 $ vcat [ text "checkApplication" , nest 2 $ text "hd = " <+> prettyA hd , nest 2 $ text "args = " <+> sep (map prettyA args) , nest 2 $ text "e = " <+> prettyA e , nest 2 $ text "t = " <+> prettyTCM t ] reportSDoc "tc.check.app" 70 $ vcat [ text "checkApplication (raw)" , nest 2 $ text $ "hd = " ++ show hd , nest 2 $ text $ "args = " ++ show (deepUnscope args) , nest 2 $ text $ "e = " ++ show (deepUnscope e) , nest 2 $ text $ "t = " ++ show t ] case unScope hd of A.Proj _ (AmbQ []) -> __IMPOSSIBLE__ -- Subcase: unambiguous projection A.Proj _ (AmbQ [_]) -> checkHeadApplication e t hd args -- Subcase: ambiguous projection A.Proj o (AmbQ ds@(_:_:_)) -> checkProjApp e o ds args t -- Subcase: ambiguous constructor A.Con (AmbQ cs0@(_:_:_)) -> do -- First we should figure out which constructor we want. reportSLn "tc.check.term" 40 $ "Ambiguous constructor: " ++ prettyShow cs0 -- Get the datatypes of the various constructors let getData Constructor{conData = d} = d getData _ = __IMPOSSIBLE__ reportSLn "tc.check.term" 40 $ " ranges before: " ++ show (getRange cs0) -- We use the reduced constructor when disambiguating, but -- the original constructor for type checking. This is important -- since they may have different types (different parameters). -- See issue 279. -- Andreas, 2017-08-13, issue #2686: ignore abstract constructors (cs, cons) <- unzip . snd . partitionEithers <$> do forM cs0 $ \ c -> mapRight (c,) <$> getConForm c reportSLn "tc.check.term" 40 $ " reduced: " ++ prettyShow cons case cons of [] -> typeError $ AbstractConstructorNotInScope $ fromMaybe __IMPOSSIBLE__ $ headMaybe cs0 [con] -> do let c = setConName (fromMaybe __IMPOSSIBLE__ $ headMaybe cs) con reportSLn "tc.check.term" 40 $ " only one non-abstract constructor: " ++ prettyShow c storeDisambiguatedName $ conName c checkConstructorApplication e t c args _ -> do dcs <- zipWithM (\ c con -> (, setConName c con) . getData . theDef <$> getConInfo con) cs cons -- Type error let badCon t = typeError $ flip DoesNotConstructAnElementOf t $ fromMaybe __IMPOSSIBLE__ $ headMaybe cs -- Lets look at the target type at this point let getCon :: TCM (Maybe ConHead) getCon = do TelV tel t1 <- telView t addContext tel $ do reportSDoc "tc.check.term.con" 40 $ nest 2 $ text "target type: " <+> prettyTCM t1 ifBlockedType t1 (\ m t -> return Nothing) $ \ t' -> caseMaybeM (isDataOrRecord $ unEl t') (badCon t') $ \ d -> case [ c | (d', c) <- dcs, d == d' ] of [c] -> do reportSLn "tc.check.term" 40 $ " decided on: " ++ prettyShow c storeDisambiguatedName $ conName c return $ Just c [] -> badCon $ t' $> Def d [] cs -> typeError $ CantResolveOverloadedConstructorsTargetingSameDatatype d $ map conName cs let unblock = isJust <$> getCon -- to unblock, call getCon later again mc <- getCon case mc of Just c -> checkConstructorApplication e t c args Nothing -> postponeTypeCheckingProblem (CheckExpr e t) unblock -- Subcase: non-ambiguous constructor A.Con (AmbQ [c]) -> do -- augment c with record fields, but do not revert to original name con <- fromRightM (sigError __IMPOSSIBLE_VERBOSE__ (typeError $ AbstractConstructorNotInScope c)) =<< getOrigConHead c checkConstructorApplication e t con args -- Subcase: pattern synonym A.PatternSyn n -> do (ns, p) <- lookupPatternSyn n p <- setRange (getRange n) . killRange <$> expandPatternSynonyms (vacuous p) -- expand recursive pattern synonyms -- Expand the pattern synonym by substituting for -- the arguments we have got and lambda-lifting -- over the ones we haven't. let meta r = A.Underscore $ A.emptyMetaInfo{ A.metaRange = r } -- TODO: name suggestion case A.insertImplicitPatSynArgs meta (getRange n) ns args of Nothing -> typeError $ BadArgumentsToPatternSynonym n Just (s, ns) -> do let p' = A.patternToExpr p e' = A.lambdaLiftExpr (map unArg ns) (A.substExpr s p') checkExpr e' t -- Subcase: macro A.Macro x -> do -- First go: no parameters TelV tel _ <- telView =<< normalise . defType =<< instantiateDef =<< getConstInfo x tTerm <- primAgdaTerm tName <- primQName let argTel = init $ telToList tel -- last argument is the hole term -- inspect macro type to figure out if arguments need to be wrapped in quote/quoteTerm mkArg :: Type -> NamedArg A.Expr -> NamedArg A.Expr mkArg t a | unEl t == tTerm = (fmap . fmap) (A.App (A.ExprRange (getRange a)) (A.QuoteTerm A.exprNoRange) . defaultNamedArg) a mkArg t a | unEl t == tName = (fmap . fmap) (A.App (A.ExprRange (getRange a)) (A.Quote A.exprNoRange) . defaultNamedArg) a mkArg t a | otherwise = a makeArgs :: [Dom (String, Type)] -> [NamedArg A.Expr] -> ([NamedArg A.Expr], [NamedArg A.Expr]) makeArgs [] args = ([], args) makeArgs _ [] = ([], []) makeArgs tel@(d : _) (arg : args) = case insertImplicit arg (map (fmap fst . argFromDom) tel) of ImpInsert is -> makeArgs (drop (length is) tel) (arg : args) BadImplicits -> (arg : args, []) -- fail later in checkHeadApplication NoSuchName{} -> (arg : args, []) -- ditto NoInsertNeeded -> first (mkArg (snd $ unDom d) arg :) $ makeArgs (tail tel) args (macroArgs, otherArgs) = makeArgs argTel args unq = A.App (A.ExprRange $ fuseRange x args) (A.Unquote A.exprNoRange) . defaultNamedArg desugared = A.app (unq $ unAppView $ Application (A.Def x) $ macroArgs) otherArgs checkExpr desugared t -- Subcase: unquote A.Unquote _ | [arg] <- args -> do (_, hole) <- newValueMeta RunMetaOccursCheck t unquoteM (namedArg arg) hole t $ return hole | arg : args <- args -> do -- Example: unquote v a b : A -- Create meta H : (x : X) (y : Y x) → Z x y for the hole -- Check a : X, b : Y a -- Unify Z a b == A -- Run the tactic on H tel <- metaTel args -- (x : X) (y : Y x) target <- addContext' tel newTypeMeta_ -- Z x y let holeType = telePi_ tel target -- (x : X) (y : Y x) → Z x y (vs, EmptyTel) <- checkArguments_ ExpandLast (getRange args) args tel -- a b : (x : X) (y : Y x) let rho = reverse (map unArg vs) ++# IdS -- [x := a, y := b] equalType (applySubst rho target) t -- Z a b == A (_, hole) <- newValueMeta RunMetaOccursCheck holeType unquoteM (namedArg arg) hole holeType $ return $ apply hole vs where metaTel :: [Arg a] -> TCM Telescope metaTel [] = pure EmptyTel metaTel (arg : args) = do a <- newTypeMeta_ let dom = a <$ domFromArg arg ExtendTel dom . Abs "x" <$> addContext' ("x", dom) (metaTel args) -- Subcase: defined symbol or variable. _ -> do v <- checkHeadApplication e t hd args reportSDoc "tc.term.app" 30 $ vcat [ text "checkApplication: checkHeadApplication returned" , nest 2 $ text "v = " <+> prettyTCM v ] return v --------------------------------------------------------------------------- -- * Meta variables --------------------------------------------------------------------------- -- | Check an interaction point without arguments. checkQuestionMark :: (Type -> TCM (MetaId, Term)) -> Type -> A.MetaInfo -> InteractionId -> TCM Term checkQuestionMark new t0 i ii = do reportSDoc "tc.interaction" 20 $ sep [ text "Found interaction point" , text (show ii) , text ":" , prettyTCM t0 ] reportSDoc "tc.interaction" 60 $ sep [ text "Raw:" , text (show t0) ] checkMeta (newQuestionMark' new ii) t0 i -- Andreas, 2013-05-22 use unreduced type t0! -- | Check an underscore without arguments. checkUnderscore :: Type -> A.MetaInfo -> TCM Term checkUnderscore = checkMeta (newValueMeta RunMetaOccursCheck) -- | Type check a meta variable. checkMeta :: (Type -> TCM (MetaId, Term)) -> Type -> A.MetaInfo -> TCM Term checkMeta newMeta t i = fst <$> checkOrInferMeta newMeta (Just t) i -- | Infer the type of a meta variable. -- If it is a new one, we create a new meta for its type. inferMeta :: (Type -> TCM (MetaId, Term)) -> A.MetaInfo -> TCM (Args -> Term, Type) inferMeta newMeta i = mapFst apply <$> checkOrInferMeta newMeta Nothing i -- | Type check a meta variable. -- If its type is not given, we return its type, or a fresh one, if it is a new meta. -- If its type is given, we check that the meta has this type, and we return the same -- type. checkOrInferMeta :: (Type -> TCM (MetaId, Term)) -> Maybe Type -> A.MetaInfo -> TCM (Term, Type) checkOrInferMeta newMeta mt i = do case A.metaNumber i of Nothing -> do setScope (A.metaScope i) t <- maybe (workOnTypes $ newTypeMeta_) return mt (x, v) <- newMeta t setMetaNameSuggestion x (A.metaNameSuggestion i) return (v, t) -- Rechecking an existing metavariable Just x -> do let v = MetaV x [] reportSDoc "tc.meta.check" 20 $ text "checking existing meta " <+> prettyTCM v t' <- jMetaType . mvJudgement <$> lookupMeta x reportSDoc "tc.meta.check" 20 $ nest 2 $ text "of type " <+> prettyTCM t' case mt of Nothing -> return (v, t') Just t -> (,t) <$> coerce v t' t -- | Turn a domain-free binding (e.g. lambda) into a domain-full one, -- by inserting an underscore for the missing type. domainFree :: ArgInfo -> A.Name -> A.LamBinding domainFree info x = A.DomainFull $ A.TypedBindings r $ Arg info $ A.TBind r [pure x] $ A.Underscore underscoreInfo where r = getRange x underscoreInfo = A.MetaInfo { A.metaRange = r , A.metaScope = emptyScopeInfo , A.metaNumber = Nothing , A.metaNameSuggestion = prettyShow $ A.nameConcrete x } --------------------------------------------------------------------------- -- * Applications --------------------------------------------------------------------------- inferHeadDef :: ProjOrigin -> QName -> TCM (Args -> Term, Type) inferHeadDef o x = do proj <- isProjection x let app = case proj of Nothing -> \ args -> Def x $ map Apply args Just p -> \ args -> projDropParsApply p o args mapFst apply <$> inferDef app x -- | Infer the type of a head thing (variable, function symbol, or constructor). -- We return a function that applies the head to arguments. -- This is because in case of a constructor we want to drop the parameters. inferHead :: A.Expr -> TCM (Args -> Term, Type) inferHead e = do case e of (A.Var x) -> do -- traceCall (InferVar x) $ do (u, a) <- getVarInfo x reportSDoc "tc.term.var" 20 $ hsep [ text "variable" , prettyTCM x , text "(" , text (show u) , text ")" , text "has type:" , prettyTCM a ] when (unusableRelevance $ getRelevance a) $ typeError $ VariableIsIrrelevant x return (apply u, unDom a) (A.Def x) -> inferHeadDef ProjPrefix x (A.Proj o (AmbQ [d])) -> inferHeadDef o d (A.Proj{}) -> __IMPOSSIBLE__ -- inferHead will only be called on unambiguous projections (A.Con (AmbQ [c])) -> do -- Constructors are polymorphic internally. -- So, when building the constructor term -- we should throw away arguments corresponding to parameters. -- First, inferDef will try to apply the constructor -- to the free parameters of the current context. We ignore that. con <- fromRightM (sigError __IMPOSSIBLE_VERBOSE__ (typeError $ AbstractConstructorNotInScope c)) =<< getOrigConHead c (u, a) <- inferDef (\ _ -> Con con ConOCon []) c -- Next get the number of parameters in the current context. Constructor{conPars = n} <- theDef <$> (instantiateDef =<< getConstInfo c) reportSLn "tc.term.con" 7 $ unwords [prettyShow c, "has", show n, "parameters."] -- So when applying the constructor throw away the parameters. return (apply u . drop n, a) (A.Con _) -> __IMPOSSIBLE__ -- inferHead will only be called on unambiguous constructors (A.QuestionMark i ii) -> inferMeta (newQuestionMark ii) i (A.Underscore i) -> inferMeta (newValueMeta RunMetaOccursCheck) i e -> do (term, t) <- inferExpr e return (apply term, t) inferDef :: (Args -> Term) -> QName -> TCM (Term, Type) inferDef mkTerm x = traceCall (InferDef x) $ do -- getConstInfo retrieves the *absolute* (closed) type of x -- instantiateDef relativizes it to the current context d <- instantiateDef =<< getConstInfo x -- irrelevant defs are only allowed in irrelevant position let drel = defRelevance d when (drel /= Relevant) $ do rel <- asks envRelevance reportSDoc "tc.irr" 50 $ vcat [ text "declaration relevance =" <+> text (show drel) , text "context relevance =" <+> text (show rel) ] unless (drel `moreRelevant` rel) $ typeError $ DefinitionIsIrrelevant x -- since x is considered living in the top-level, we have to -- apply it to the current context vs <- freeVarsToApply x reportSDoc "tc.term.def" 60 $ do text "freeVarsToApply to def " <+> hsep (map (text . show) vs) reportSDoc "tc.term.def" 10 $ do text "inferred def " <+> prettyTCM x <+> hsep (map prettyTCM vs) let t = defType d reportSDoc "tc.term.def" 10 $ nest 2 $ text " : " <+> prettyTCM t let v = mkTerm vs -- applies x to vs, dropping parameters reportSDoc "tc.term.def" 10 $ nest 2 $ text " --> " <+> prettyTCM v return (v, t) -- | Check the type of a constructor application. This is easier than -- a general application since the implicit arguments can be inserted -- without looking at the arguments to the constructor. checkConstructorApplication :: A.Expr -> Type -> ConHead -> [NamedArg A.Expr] -> TCM Term checkConstructorApplication org t c args = do reportSDoc "tc.term.con" 50 $ vcat [ text "entering checkConstructorApplication" , nest 2 $ vcat [ text "org =" <+> prettyTCM org , text "t =" <+> prettyTCM t , text "c =" <+> prettyTCM c , text "args =" <+> prettyTCM args ] ] let paramsGiven = checkForParams args if paramsGiven then fallback else do reportSDoc "tc.term.con" 50 $ text "checkConstructorApplication: no parameters explicitly supplied, continuing..." cdef <- getConInfo c let Constructor{conData = d, conPars = npars} = theDef cdef reportSDoc "tc.term.con" 50 $ nest 2 $ text "d =" <+> prettyTCM d -- Issue 661: t maybe an evaluated form of d .., so we evaluate d -- as well and then check wether we deal with the same datatype t0 <- reduce (Def d []) case (ignoreSharing t0, ignoreSharing $ unEl t) of -- Only fully applied constructors get special treatment (Def d0 _, Def d' es) -> do let ~(Just vs) = allApplyElims es reportSDoc "tc.term.con" 50 $ nest 2 $ text "d0 =" <+> prettyTCM d0 reportSDoc "tc.term.con" 50 $ nest 2 $ text "d' =" <+> prettyTCM d' reportSDoc "tc.term.con" 50 $ nest 2 $ text "vs =" <+> prettyTCM vs if d' /= d0 then fallback else do -- Issue 661: d' may take more parameters than d, in particular -- these additional parameters could be a module parameter telescope. -- Since we get the constructor type ctype from d but the parameters -- from t = Def d' vs, we drop the additional parameters. npars' <- getNumberOfParameters d' caseMaybe (sequenceA $ List2 (Just npars, npars')) fallback $ \ (List2 (n, n')) -> do reportSDoc "tc.term.con" 50 $ nest 2 $ text $ "n = " ++ show n reportSDoc "tc.term.con" 50 $ nest 2 $ text $ "n' = " ++ show n' when (n > n') -- preprocessor does not like ', so put on next line __IMPOSSIBLE__ let ps = take n $ drop (n' - n) vs ctype = defType cdef reportSDoc "tc.term.con" 20 $ vcat [ text "special checking of constructor application of" <+> prettyTCM c , nest 2 $ vcat [ text "ps =" <+> prettyTCM ps , text "ctype =" <+> prettyTCM ctype ] ] let ctype' = ctype `piApply` ps reportSDoc "tc.term.con" 20 $ nest 2 $ text "ctype' =" <+> prettyTCM ctype' -- get the parameter names let TelV ptel _ = telView'UpTo n ctype let pnames = map (fmap fst) $ telToList ptel -- drop the parameter arguments args' = dropArgs pnames args -- check the non-parameter arguments expandLast <- asks envExpandLast checkArguments' expandLast (getRange c) args' ctype' t $ \us t' -> do reportSDoc "tc.term.con" 20 $ nest 2 $ vcat [ text "us =" <+> prettyTCM us , text "t' =" <+> prettyTCM t' ] coerce (Con c ConOCon us) t' t _ -> do reportSDoc "tc.term.con" 50 $ nest 2 $ text "we are not at a datatype, falling back" fallback where fallback = checkHeadApplication org t (A.Con (AmbQ [conName c])) args -- Check if there are explicitly given hidden arguments, -- in which case we fall back to default type checking. -- We could work harder, but let's not for now. -- -- Andreas, 2012-04-18: if all inital args are underscores, ignore them checkForParams args = let (hargs, rest) = span (not . visible) args notUnderscore A.Underscore{} = False notUnderscore _ = True in any notUnderscore $ map (unScope . namedArg) hargs -- Drop the constructor arguments that correspond to parameters. dropArgs [] args = args dropArgs ps [] = args dropArgs ps args@(arg : args') | Just p <- name, Just ps' <- namedPar p ps = dropArgs ps' args' | Nothing <- name, Just ps' <- unnamedPar h ps = dropArgs ps' args' | otherwise = args where name = fmap rangedThing . nameOf $ unArg arg h = getHiding arg namedPar x = dropPar ((x ==) . unDom) unnamedPar h = dropPar (sameHiding h) dropPar this (p : ps) | this p = Just ps | otherwise = dropPar this ps dropPar _ [] = Nothing {- UNUSED CODE, BUT DON'T REMOVE (2012-04-18) -- Split the arguments to a constructor into those corresponding -- to parameters and those that don't. Dummy underscores are inserted -- for parameters that are not given explicitly. splitArgs [] args = ([], args) splitArgs ps [] = (map (const dummyUnderscore) ps, args) splitArgs ps args@(Arg NotHidden _ _ : _) = (map (const dummyUnderscore) ps, args) splitArgs (p:ps) (arg : args) | elem mname [Nothing, Just p] = mapFst (arg :) $ splitArgs ps args | otherwise = mapFst (dummyUnderscore :) $ splitArgs ps (arg:args) where mname = nameOf (unArg arg) dummyUnderscore = Arg Hidden Relevant (unnamed $ A.Underscore $ A.MetaInfo noRange emptyScopeInfo Nothing) -} -- | @checkHeadApplication e t hd args@ checks that @e@ has type @t@, -- assuming that @e@ has the form @hd args@. The corresponding -- type-checked term is returned. -- -- If the head term @hd@ is a coinductive constructor, then a -- top-level definition @fresh tel = hd args@ (where the clause is -- delayed) is added, where @tel@ corresponds to the current -- telescope. The returned term is @fresh tel@. -- -- Precondition: The head @hd@ has to be unambiguous, and there should -- not be any need to insert hidden lambdas. checkHeadApplication :: A.Expr -> Type -> A.Expr -> [NamedArg A.Expr] -> TCM Term checkHeadApplication e t hd args = do kit <- coinductionKit case hd of A.Con (AmbQ [c]) | Just c == (nameOfSharp <$> kit) -> do -- Type checking # generated #-wrapper. The # that the user can write will be a Def, -- but the sharp we generate in the body of the wrapper is a Con. defaultResult A.Con (AmbQ [c]) -> do (f, t0) <- inferHead hd reportSDoc "tc.term.con" 5 $ vcat [ text "checkHeadApplication inferred" <+> prettyTCM c <+> text ":" <+> prettyTCM t0 ] expandLast <- asks envExpandLast checkArguments' expandLast (getRange hd) args t0 t $ \vs t1 -> do TelV eTel eType <- telView t -- If the expected type @eType@ is a metavariable we have to make -- sure it's instantiated to the proper pi type TelV fTel fType <- telViewUpTo (size eTel) t1 -- We know that the target type of the constructor (fType) -- does not depend on fTel so we can compare fType and eType -- first. when (size eTel > size fTel) $ typeError $ UnequalTypes CmpLeq t1 t -- switch because of contravariance -- Andreas, 2011-05-10 report error about types rather telescopes -- compareTel CmpLeq eTel fTel >> return () -- This will fail! reportSDoc "tc.term.con" 10 $ addContext eTel $ vcat [ text "checking" <+> prettyTCM fType <+> text "?<=" <+> prettyTCM eType ] blockTerm t $ f vs <$ workOnTypes (do addContext' eTel $ leqType fType eType compareTel t t1 CmpLeq eTel fTel) (A.Def c) | Just c == (nameOfSharp <$> kit) -> do arg <- case args of [a] | visible a -> return $ namedArg a _ -> typeError $ GenericError $ prettyShow c ++ " must be applied to exactly one argument." -- The name of the fresh function. i <- fresh :: TCM Int let name = filter (/= '_') (prettyShow $ A.nameConcrete $ A.qnameName c) ++ "-" ++ show i kit <- coinductionKit' let flat = nameOfFlat kit inf = nameOfInf kit -- Add the type signature of the fresh function to the -- signature. -- To make sure we can type check the generated function we have to make -- sure that its type is \inf. The reason for this is that we don't yet -- postpone checking of patterns when we don't know their types (Issue480). forcedType <- do lvl <- levelType (_, l) <- newValueMeta RunMetaOccursCheck lvl lv <- levelView l (_, a) <- newValueMeta RunMetaOccursCheck (sort $ Type lv) return $ El (Type lv) $ Def inf [Apply $ setHiding Hidden $ defaultArg l, Apply $ defaultArg a] wrapper <- inFreshModuleIfFreeParams $ do c' <- setRange (getRange c) <$> liftM2 qualify (killRange <$> currentModule) (freshName_ name) -- Define and type check the fresh function. rel <- asks envRelevance abs <- aModeToDef <$> asks envAbstractMode let info = A.mkDefInfo (A.nameConcrete $ A.qnameName c') noFixity' PublicAccess abs noRange core = A.LHSProj { A.lhsDestructor = AmbQ [flat] , A.lhsFocus = defaultNamedArg $ A.LHSHead c' [] , A.lhsPatsRight = [] } clause = A.Clause (A.LHS (A.LHSRange noRange) core []) [] [] (A.RHS arg Nothing) [] False i <- currentOrFreshMutualBlock -- If we are in irrelevant position, add definition irrelevantly. -- TODO: is this sufficient? addConstant c' =<< do let ai = setRelevance rel defaultArgInfo useTerPragma $ (defaultDefn ai c' forcedType emptyFunction) { defMutual = i } checkFunDef NotDelayed info c' [clause] reportSDoc "tc.term.expr.coind" 15 $ do def <- theDef <$> getConstInfo c' vcat $ [ text "The coinductive wrapper" , nest 2 $ prettyTCM rel <> prettyTCM c' <+> text ":" , nest 4 $ prettyTCM t , nest 2 $ prettyA clause , text "The definition is" <+> text (show $ funDelayed def) <> text "." ] return c' -- The application of the fresh function to the relevant -- arguments. e' <- Def wrapper . map Apply <$> getContextArgs reportSDoc "tc.term.expr.coind" 15 $ vcat $ [ text "The coinductive constructor application" , nest 2 $ prettyTCM e , text "was translated into the application" , nest 2 $ prettyTCM e' ] blockTerm t $ e' <$ workOnTypes (leqType forcedType t) A.Con _ -> __IMPOSSIBLE__ _ -> defaultResult where defaultResult = do (f, t0) <- inferHead hd expandLast <- asks envExpandLast checkArguments' expandLast (getRange hd) args t0 t $ \vs t1 -> do coerce (f vs) t1 t traceCallE :: Call -> ExceptT e TCM r -> ExceptT e TCM r traceCallE call m = do z <- lift $ traceCall call $ runExceptT m case z of Right e -> return e Left err -> throwError err -- | Check arguments whose value we already know. -- -- This function can be used to check user-supplied parameters -- we have already computed by inference. -- -- Precondition: The type @t@ of the head has enough domains. checkKnownArguments :: [NamedArg A.Expr] -- ^ User-supplied arguments (hidden ones may be missing). -> Args -- ^ Inferred arguments (including hidden ones). -> Type -- ^ Type of the head (must be Pi-type with enough domains). -> TCM (Args, Type) -- ^ Remaining inferred arguments, remaining type. checkKnownArguments [] vs t = return (vs, t) checkKnownArguments (arg : args) vs t = do (vs', t') <- traceCall (SetRange $ getRange arg) $ checkKnownArgument arg vs t checkKnownArguments args vs' t' -- | Check an argument whose value we already know. checkKnownArgument :: NamedArg A.Expr -- ^ User-supplied argument. -> Args -- ^ Inferred arguments (including hidden ones). -> Type -- ^ Type of the head (must be Pi-type with enough domains). -> TCM (Args, Type) -- ^ Remaining inferred arguments, remaining type. checkKnownArgument arg [] _ = genericDocError =<< do text "Invalid projection parameter " <+> prettyA arg checkKnownArgument arg@(Arg info e) (Arg _infov v : vs) t = do (Dom info' a, b) <- mustBePi t -- Skip the arguments from vs that do not correspond to e if not (sameHiding info info' && (visible info || maybe True ((absName b ==) . rangedThing) (nameOf e))) -- Continue with the next one then checkKnownArgument arg vs (b `absApp` v) -- Found the right argument else do u <- checkNamedArg arg a equalTerm a u v return (vs, b `absApp` v) -- | Check a single argument. checkNamedArg :: NamedArg A.Expr -> Type -> TCM Term checkNamedArg arg@(Arg info e0) t0 = do let e = namedThing e0 let x = maybe "" rangedThing $ nameOf e0 traceCall (CheckExprCall e t0) $ do reportSDoc "tc.term.args.named" 15 $ do text "Checking named arg" <+> sep [ fsep [ prettyTCM arg, text ":", prettyTCM t0 ] ] reportSLn "tc.term.args.named" 75 $ " arg = " ++ show (deepUnscope arg) -- Ulf, 2017-03-24: (#2172) Always treat explicit _ and ? as implicit -- argument (i.e. solve with unification). let checkU = checkMeta (newMetaArg (setHiding Hidden info) x) t0 let checkQ = checkQuestionMark (newInteractionMetaArg (setHiding Hidden info) x) t0 if not $ isHole e then checkExpr e t0 else localScope $ do -- Note: we need localScope here, -- as scopedExpr manipulates the scope in the state. -- However, we may not pull localScope over checkExpr! -- This is why we first test for isHole, and only do -- scope manipulations if we actually handle the checking -- of e here (and not pass it to checkExpr). scopedExpr e >>= \case A.Underscore i -> checkU i A.QuestionMark i ii -> checkQ i ii _ -> __IMPOSSIBLE__ where isHole A.Underscore{} = True isHole A.QuestionMark{} = True isHole (A.ScopedExpr _ e) = isHole e isHole _ = False -- | Check a list of arguments: @checkArgs args t0 t1@ checks that -- @t0 = Delta -> t0'@ and @args : Delta@. Inserts hidden arguments to -- make this happen. Returns the evaluated arguments @vs@, the remaining -- type @t0'@ (which should be a subtype of @t1@) and any constraints @cs@ -- that have to be solved for everything to be well-formed. checkArguments :: ExpandHidden -> Range -> [NamedArg A.Expr] -> Type -> Type -> ExceptT (Args, [NamedArg A.Expr], Type) TCM (Args, Type) -- Case: no arguments, do not insert trailing hidden arguments: We are done. checkArguments DontExpandLast _ [] t0 t1 = return ([], t0) -- Case: no arguments, but need to insert trailing hiddens. checkArguments exh r [] t0 t1 = traceCallE (CheckArguments r [] t0 t1) $ lift $ do t1' <- unEl <$> reduce t1 implicitArgs (-1) (expand t1') t0 where expand (Pi (Dom info _) _) Hidden = not (hidden info) && exh == ExpandLast expand _ Hidden = exh == ExpandLast expand (Pi (Dom info _) _) Instance{} = not $ isInstance info expand _ Instance{} = True expand _ NotHidden = False -- Case: argument given. checkArguments exh r args0@(arg@(Arg info e) : args) t0 t1 = traceCallE (CheckArguments r args0 t0 t1) $ do lift $ reportSDoc "tc.term.args" 30 $ sep [ text "checkArguments" -- , text " args0 =" <+> prettyA args0 , nest 2 $ vcat [ text "e =" <+> prettyA e , text "t0 =" <+> prettyTCM t0 , text "t1 =" <+> prettyTCM t1 ] ] -- First, insert implicit arguments, depending on current argument @arg@. let hx = getHiding info -- hiding of current argument mx = fmap rangedThing $ nameOf e -- name of current argument -- do not insert visible arguments expand NotHidden y = False -- insert a hidden argument if arg is not hidden or has different name -- insert an instance argument if arg is not instance or has different name expand hy y = not (sameHiding hy hx) || maybe False (y /=) mx (nargs, t) <- lift $ implicitNamedArgs (-1) expand t0 -- Separate names from args. let (mxs, us) = unzip $ map (\ (Arg ai (Named mx u)) -> (mx, Arg ai u)) nargs xs = catMaybes mxs -- We are done inserting implicit args. Now, try to check @arg@. ifBlockedType t (\ m t -> throwError (us, args0, t)) $ \ t0' -> do -- What can go wrong? -- 1. We ran out of function types. let shouldBePi -- a) It is an explicit argument, but we ran out of function types. | visible info = lift $ typeError $ ShouldBePi t0' -- b) It is an implicit argument, and we did not insert any implicits. -- Thus, the type was not a function type to start with. | null xs = lift $ typeError $ ShouldBePi t0' -- c) We did insert implicits, but we ran out of implicit function types. -- Then, we should inform the user that we did not find his one. | otherwise = lift $ typeError $ WrongNamedArgument arg -- 2. We have a function type left, but it is the wrong one. -- Our argument must be implicit, case a) is impossible. -- (Otherwise we would have ran out of function types instead.) let wrongPi -- b) We have not inserted any implicits. | null xs = lift $ typeError $ WrongHidingInApplication t0' -- c) We inserted implicits, but did not find his one. | otherwise = lift $ typeError $ WrongNamedArgument arg -- t0' <- lift $ forcePi (getHiding info) (maybe "_" rangedThing $ nameOf e) t0' case ignoreSharing $ unEl t0' of Pi (Dom info' a) b | sameHiding info info' && (visible info || maybe True ((absName b ==) . rangedThing) (nameOf e)) -> do u <- lift $ applyRelevanceToContext (getRelevance info') $ do -- Andreas, 2014-05-30 experiment to check non-dependent arguments -- after the spine has been processed. Allows to propagate type info -- from ascribed type into extended-lambdas. Would solve issue 1159. -- However, leaves unsolved type checking problems in the test suite. -- I do not know what I am doing wrong here. -- Could be extreme order-sensitivity or my abuse of the postponing -- mechanism. -- Andreas, 2016-02-02: Ulf says unless there is actually some meta -- blocking a postponed type checking problem, we might never retry, -- since the trigger for retrying constraints is solving a meta. -- Thus, the following naive use violates some invariant. -- if not $ isBinderUsed b -- then postponeTypeCheckingProblem (CheckExpr (namedThing e) a) (return True) else let e' = e { nameOf = maybe (Just $ unranged $ absName b) Just (nameOf e) } checkNamedArg (Arg info' e') a -- save relevance info' from domain in argument addCheckedArgs us (Arg info' u) $ checkArguments exh (fuseRange r e) args (absApp b u) t1 | otherwise -> do reportSDoc "error" 10 $ nest 2 $ vcat [ text $ "info = " ++ show info , text $ "info' = " ++ show info' , text $ "absName b = " ++ absName b , text $ "nameOf e = " ++ show (nameOf e) ] wrongPi _ -> shouldBePi where addCheckedArgs us u rec = (mapFst ((us ++) . (u :)) <$> rec) `catchError` \(vs, es, t) -> throwError (us ++ u : vs, es, t) -- | Check that a list of arguments fits a telescope. -- Inserts hidden arguments as necessary. -- Returns the type-checked arguments and the remaining telescope. checkArguments_ :: ExpandHidden -- ^ Eagerly insert trailing hidden arguments? -> Range -- ^ Range of application. -> [NamedArg A.Expr] -- ^ Arguments to check. -> Telescope -- ^ Telescope to check arguments against. -> TCM (Args, Telescope) -- ^ Checked arguments and remaining telescope if successful. checkArguments_ exh r args tel = do z <- runExceptT $ checkArguments exh r args (telePi tel typeDontCare) typeDontCare case z of Right (args, t) -> do let TelV tel' _ = telView' t return (args, tel') Left _ -> __IMPOSSIBLE__ -- type cannot be blocked as it is generated by telePi -- | Infer the type of an expression. Implemented by checking against a meta -- variable. Except for neutrals, for them a polymorphic type is inferred. inferExpr :: A.Expr -> TCM (Term, Type) -- inferExpr e = inferOrCheck e Nothing inferExpr = inferExpr' DontExpandLast inferExpr' :: ExpandHidden -> A.Expr -> TCM (Term, Type) inferExpr' exh e = do let Application hd args = appView e reportSDoc "tc.infer" 30 $ vcat [ text "inferExpr': appView of " <+> prettyA e , text " hd = " <+> prettyA hd , text " args = " <+> prettyAs args ] reportSDoc "tc.infer" 60 $ vcat [ text $ " hd (raw) = " ++ show hd ] if not $ defOrVar hd then fallback else traceCall (InferExpr e) $ do case unScope $ hd of A.Proj o (AmbQ ds@(_:_:_)) -> inferProjApp e o ds args _ -> do (f, t0) <- inferHead hd res <- runExceptT $ checkArguments exh (getRange hd) args t0 (sort Prop) case res of Right (vs, t1) -> return (f vs, t1) Left t1 -> fallback -- blocked on type t1 where fallback = do t <- workOnTypes $ newTypeMeta_ v <- checkExpr e t return (v,t) defOrVar :: A.Expr -> Bool defOrVar A.Var{} = True defOrVar A.Def{} = True defOrVar A.Proj{} = True defOrVar (A.ScopedExpr _ e) = defOrVar e defOrVar _ = False -- | Used to check aliases @f = e@. -- Switches off 'ExpandLast' for the checking of top-level application. checkDontExpandLast :: A.Expr -> Type -> TCM Term checkDontExpandLast e t = case e of _ | Application hd args <- appView e, defOrVar hd -> traceCall (CheckExprCall e t) $ localScope $ dontExpandLast $ shared =<< do checkApplication hd args e t _ -> checkExpr e t -- note that checkExpr always sets ExpandLast {- Andreas, 2013-03-15 UNUSED, but don't remove inferOrCheck :: A.Expr -> Maybe Type -> TCM (Term, Type) inferOrCheck e mt = case e of _ | Application hd args <- appView e, defOrVar hd -> traceCall (InferExpr e) $ do (f, t0) <- inferHead hd res <- runErrorT $ checkArguments DontExpandLast (getRange hd) args t0 $ fromMaybe (sort Prop) mt case res of Right (vs, t1) -> maybe (return (f vs, t1)) (\ t -> (,t) <$> coerce (f vs) t1 t) mt Left t1 -> fallback -- blocked on type t1 _ -> fallback where fallback = do t <- maybe (workOnTypes $ newTypeMeta_) return mt v <- checkExpr e t return (v,t) -} -- | Check whether a de Bruijn index is bound by a module telescope. isModuleFreeVar :: Int -> TCM Bool isModuleFreeVar i = do params <- moduleParamsToApply =<< currentModule return $ any ((== Var i []) . unArg) params -- | Infer the type of an expression, and if it is of the form -- @{tel} -> D vs@ for some datatype @D@ then insert the hidden -- arguments. Otherwise, leave the type polymorphic. inferExprForWith :: A.Expr -> TCM (Term, Type) inferExprForWith e = do reportSDoc "tc.with.infer" 20 $ text "inferExprforWith " <+> prettyTCM e reportSLn "tc.with.infer" 80 $ "inferExprforWith " ++ show (deepUnscope e) traceCall (InferExpr e) $ do -- With wants type and term fully instantiated! (v, t) <- instantiateFull =<< inferExpr e v0 <- reduce v -- Andreas 2014-11-06, issue 1342. -- Check that we do not `with` on a module parameter! case ignoreSharing v0 of Var i [] -> whenM (isModuleFreeVar i) $ do reportSDoc "tc.with.infer" 80 $ vcat [ text $ "with expression is variable " ++ show i , text "current modules = " <+> do text . show =<< currentModule , text "current module free vars = " <+> do text . show =<< getCurrentModuleFreeVars , text "context size = " <+> do text . show =<< getContextSize , text "current context = " <+> do prettyTCM =<< getContextTelescope ] typeError $ WithOnFreeVariable e v0 _ -> return () -- Possibly insert hidden arguments. TelV tel t0 <- telViewUpTo' (-1) (not . visible) t case ignoreSharing $ unEl t0 of Def d vs -> do res <- isDataOrRecordType d case res of Nothing -> return (v, t) Just{} -> do (args, t1) <- implicitArgs (-1) notVisible t return (v `apply` args, t1) _ -> return (v, t) --------------------------------------------------------------------------- -- * Let bindings --------------------------------------------------------------------------- checkLetBindings :: [A.LetBinding] -> TCM a -> TCM a checkLetBindings = foldr (.) id . map checkLetBinding checkLetBinding :: A.LetBinding -> TCM a -> TCM a checkLetBinding b@(A.LetBind i info x t e) ret = traceCallCPS_ (CheckLetBinding b) ret $ \ret -> do t <- isType_ t v <- applyRelevanceToContext (getRelevance info) $ checkDontExpandLast e t addLetBinding info x v t ret checkLetBinding b@(A.LetPatBind i p e) ret = traceCallCPS_ (CheckLetBinding b) ret $ \ret -> do p <- expandPatternSynonyms p (v, t) <- inferExpr' ExpandLast e let -- construct a type t -> dummy for use in checkLeftHandSide t0 = El (getSort t) $ Pi (Dom defaultArgInfo t) (NoAbs underscore typeDontCare) p0 = Arg defaultArgInfo (Named Nothing p) reportSDoc "tc.term.let.pattern" 10 $ vcat [ text "let-binding pattern p at type t" , nest 2 $ vcat [ text "p (A) =" <+> text (show p) -- prettyTCM p , text "t =" <+> prettyTCM t ] ] fvs <- getContextSize checkLeftHandSide (CheckPattern p EmptyTel t) Nothing [p0] t0 Nothing [] $ \ (LHSResult _ delta0 ps _t _ asb) -> bindAsPatterns asb $ do -- After dropping the free variable patterns there should be a single pattern left. let p = case drop fvs ps of [p] -> namedArg p; _ -> __IMPOSSIBLE__ -- Also strip the context variables from the telescope delta = telFromList $ drop fvs $ telToList delta0 reportSDoc "tc.term.let.pattern" 20 $ nest 2 $ vcat [ text "p (I) =" <+> text (show p) , text "delta =" <+> text (show delta) ] -- We translate it into a list of projections. fs <- recordPatternToProjections p -- We remove the bindings for the pattern variables from the context. cxt0 <- getContext let (binds, cxt) = splitAt (size delta) cxt0 toDrop = length binds -- We create a substitution for the let-bound variables -- (unfortunately, we cannot refer to x in internal syntax -- so we have to copy v). sigma = zipWith ($) fs (repeat v) -- We apply the types of the let bound-variables to this substitution. -- The 0th variable in a context is the last one, so we reverse. -- Further, we need to lower all other de Bruijn indices by -- the size of delta, so we append the identity substitution. sub = parallelS (reverse sigma) -- Outer let-bindings will have been rebound by checkLeftHandSide, so -- we need to strenghten those as well. Don't use a strengthening -- subsititution since @-patterns in the pattern binding will reference -- the pattern variables. subLetBind (OpenThing cxt va) = OpenThing (drop toDrop cxt) (applySubst sub va) escapeContext toDrop $ updateModuleParameters sub $ locally eLetBindings (fmap subLetBind) $ do reportSDoc "tc.term.let.pattern" 20 $ nest 2 $ vcat [ text "delta =" <+> prettyTCM delta , text "binds =" <+> text (show binds) -- prettyTCM binds ] {- WE CANNOT USE THIS BINDING -- We add a first let-binding for the value of e. x <- freshNoName (getRange e) addLetBinding Relevant x v t $ do -} let fdelta = flattenTel delta reportSDoc "tc.term.let.pattern" 20 $ nest 2 $ vcat [ text "fdelta =" <+> text (show fdelta) ] let tsl = applySubst sub fdelta -- We get a list of types let ts = map unDom tsl -- and relevances. let infos = map domInfo tsl -- We get list of names of the let-bound vars from the context. let xs = map (fst . unDom) (reverse binds) -- We add all the bindings to the context. foldr (uncurry4 addLetBinding) ret $ List.zip4 infos xs sigma ts checkLetBinding (A.LetApply i x modapp copyInfo _adir) ret = do -- Any variables in the context that doesn't belong to the current -- module should go with the new module. -- Example: @f x y = let open M t in u@. -- There are 2 @new@ variables, @x@ and @y@, going into the anonynous module -- @module _ (x : _) (y : _) = M t@. fv <- getCurrentModuleFreeVars n <- getContextSize let new = n - fv reportSLn "tc.term.let.apply" 10 $ "Applying " ++ show modapp ++ " with " ++ show new ++ " free variables" reportSDoc "tc.term.let.apply" 20 $ vcat [ text "context =" <+> (prettyTCM =<< getContextTelescope) , text "module =" <+> (prettyTCM =<< currentModule) , text "fv =" <+> (text $ show fv) ] checkSectionApplication i x modapp copyInfo withAnonymousModule x new ret -- LetOpen and LetDeclaredVariable are only used for highlighting. checkLetBinding A.LetOpen{} ret = ret checkLetBinding (A.LetDeclaredVariable _) ret = ret Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Term.hs-boot0000644000000000000000000000150013154613124020777 0ustar0000000000000000 module Agda.TypeChecking.Rules.Term where import Agda.Syntax.Common (WithHiding, NamedArg, Arg) import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Internal import Agda.Syntax.Position import Agda.TypeChecking.Monad.Base import Agda.Utils.Except ( ExceptT ) isType_ :: A.Expr -> TCM Type checkExpr :: A.Expr -> Type -> TCM Term inferExpr :: A.Expr -> TCM (Term, Type) checkArguments :: ExpandHidden -> Range -> [NamedArg A.Expr] -> Type -> Type -> ExceptT (Args, [NamedArg A.Expr], Type) TCM (Args, Type) checkArguments' :: ExpandHidden -> Range -> [NamedArg A.Expr] -> Type -> Type -> (Args -> Type -> TCM Term) -> TCM Term checkPostponedLambda :: Arg ([WithHiding Name], Maybe Type) -> A.Expr -> Type -> TCM Term unquoteTactic :: Term -> Term -> Type -> TCM Term -> TCM Term Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Data.hs0000644000000000000000000004701213154613124020010 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE MultiWayIf #-} module Agda.TypeChecking.Rules.Data where import Control.Applicative import Control.Monad import Data.List (genericTake) import Data.Maybe (fromMaybe) import qualified Data.Set as Set import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Views (deepUnscope) import Agda.Syntax.Internal import Agda.Syntax.Common import Agda.Syntax.Position import qualified Agda.Syntax.Info as Info import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin (primLevel) import Agda.TypeChecking.Conversion import Agda.TypeChecking.Substitute import Agda.TypeChecking.MetaVars import Agda.TypeChecking.Reduce import Agda.TypeChecking.Pretty import Agda.TypeChecking.Free import Agda.TypeChecking.Forcing import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.Telescope import {-# SOURCE #-} Agda.TypeChecking.Rules.Term ( isType_ ) import Agda.Interaction.Options import Agda.Utils.Except import Agda.Utils.List import Agda.Utils.Monad import Agda.Utils.Permutation import qualified Agda.Utils.Pretty as P import Agda.Utils.Size import Agda.Utils.Tuple import qualified Agda.Utils.VarSet as VarSet #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Datatypes --------------------------------------------------------------------------- -- | Type check a datatype definition. Assumes that the type has already been -- checked. checkDataDef :: Info.DefInfo -> QName -> [A.LamBinding] -> [A.Constructor] -> TCM () checkDataDef i name ps cs = traceCall (CheckDataDef (getRange name) (qnameName name) ps cs) $ do -- TODO!! (qnameName) -- Add the datatype module addSection (qnameToMName name) -- Look up the type of the datatype. t <- instantiateFull =<< typeOfConst name -- Make sure the shape of the type is visible let unTelV (TelV tel a) = telePi tel a t <- unTelV <$> telView t -- Top level free vars freeVars <- getContextSize -- The parameters are in scope when checking the constructors. dataDef <- bindParameters ps t $ \tel t0 -> do -- Parameters are always hidden in constructors let tel' = hideAndRelParams <$> tel -- let tel' = hideTel tel -- The type we get from bindParameters is Θ -> s where Θ is the type of -- the indices. We count the number of indices and return s. -- We check that s is a sort. let TelV ixTel s0 = telView' t0 nofIxs = size ixTel s <- workOnTypes $ do -- Andreas, 2016-11-02 issue #2290 -- Trying to unify the sort with a fresh sort meta which is -- defined outside the index telescope is the most robust way -- to check independence of the indices. -- However, it might give the dreaded "Cannot instantiate meta..." -- error which we replace by a more understandable error -- in case of a suspected dependency. s <- newSortMetaBelowInf catchError_ (addContext ixTel $ equalType s0 $ raise nofIxs $ sort s) $ \ err -> if any (`freeIn` s0) [0..nofIxs - 1] then typeError . GenericDocError =<< fsep [ text "The sort of" <+> prettyTCM name , text "cannot depend on its indices in the type" , prettyTCM t0 ] else throwError err return s -- the small parameters are taken into consideration for --without-K smallPars <- smallParams tel s reportSDoc "tc.data.sort" 20 $ vcat [ text "checking datatype" <+> prettyTCM name , nest 2 $ vcat [ text "type (parameters instantiated): " <+> prettyTCM t0 , text "type (full): " <+> prettyTCM t , text "sort: " <+> prettyTCM s , text "indices:" <+> text (show nofIxs) , text "params:" <+> text (show $ deepUnscope ps) , text "small params:" <+> text (show smallPars) ] ] let npars = size tel -- Change the datatype from an axiom to a datatype with no constructors. let dataDef = Datatype { dataPars = npars , dataSmallPars = Perm npars smallPars , dataNonLinPars = Drop 0 $ Perm npars [] , dataIxs = nofIxs , dataInduction = Inductive , dataClause = Nothing , dataCons = [] -- Constructors are added later , dataSort = s , dataAbstr = Info.defAbstract i , dataMutual = Nothing } escapeContext npars $ do addConstant name $ defaultDefn defaultArgInfo name t dataDef -- polarity and argOcc.s determined by the positivity checker -- Check the types of the constructors -- collect the non-linear parameters of each constructor nonLins <- mapM (checkConstructor name tel' nofIxs s) cs -- compute the ascending list of non-linear parameters of the data type let nonLinPars0 = Set.toAscList $ Set.unions $ map Set.fromList nonLins -- The constructors are analyzed in the absolute context, -- but the data definition happens in the relative module context, -- so we apply to the free module variables. -- Unfortunately, we lose precision here, since 'abstract', which -- is then performed by addConstant, cannot restore the linearity info. nonLinPars = Drop freeVars $ Perm (npars + freeVars) nonLinPars0 -- Return the data definition return dataDef{ dataNonLinPars = nonLinPars } let s = dataSort dataDef cons = map A.axiomName cs -- get constructor names -- If proof irrelevance is enabled we have to check that datatypes in -- Prop contain at most one element. do proofIrr <- proofIrrelevance case (proofIrr, s, cs) of (True, Prop, _:_:_) -> setCurrentRange cons $ typeError PropMustBeSingleton _ -> return () -- Add the datatype to the signature with its constructors. -- It was previously added without them. addConstant name $ defaultDefn defaultArgInfo name t $ dataDef{ dataCons = cons } -- | Ensure that the type is a sort. -- If it is not directly a sort, compare it to a 'newSortMetaBelowInf'. forceSort :: Type -> TCM Sort forceSort t = case ignoreSharing $ unEl t of Sort s -> return s _ -> do s <- newSortMetaBelowInf equalType t (sort s) return s -- | A parameter is small if its sort fits into the data sort. -- @smallParams@ overapproximates the small parameters (in doubt: small). smallParams :: Telescope -> Sort -> TCM [Int] smallParams tel s = do -- get the types of the parameters let as = map (snd . unDom) $ telToList tel -- get the big parameters concat <$> do forM (zip [0..] as) $ \ (i, a) -> do -- A type is small if it is not Level or its sort is <= the data sort. -- In doubt (unsolvable constraints), a type is small. -- So, only if we have a solid error, the type is big. localTCState $ do ([] <$ do equalTerm topSort (unEl a) =<< primLevel) -- NB: if primLevel fails, the next alternative is picked <|> ([i] <$ (getSort a `leqSort` s)) <|> return [] where (<|>) m1 m2 = m1 `catchError_` (const m2) -- | Type check a constructor declaration. Checks that the constructor targets -- the datatype and that it fits inside the declared sort. -- Returns the non-linear parameters. checkConstructor :: QName -- ^ Name of data type. -> Telescope -- ^ Parameter telescope. -> Nat -- ^ Number of indices of the data type. -> Sort -- ^ Sort of the data type. -> A.Constructor -- ^ Constructor declaration (type signature). -> TCM [Int] -- ^ Non-linear parameters. checkConstructor d tel nofIxs s (A.ScopedDecl scope [con]) = do setScope scope checkConstructor d tel nofIxs s con checkConstructor d tel nofIxs s con@(A.Axiom _ i ai Nothing c e) = traceCall (CheckConstructor d tel s con) $ do {- WRONG -- Andreas, 2011-04-26: the following happens to the right of ':' -- we may use irrelevant arguments in a non-strict way in types t' <- workOnTypes $ do -} debugEnter c e -- check that we are relevant case getRelevance ai of Relevant -> return () Irrelevant -> typeError $ GenericError $ "Irrelevant constructors are not supported" _ -> __IMPOSSIBLE__ -- check that the type of the constructor is well-formed t <- isType_ e -- check that the type of the constructor ends in the data type n <- getContextSize debugEndsIn t d n nonLinPars <- constructs n t d debugNonLinPars nonLinPars -- check which constructor arguments are determined by the type ('forcing') t' <- addForcingAnnotations t -- check that the sort (universe level) of the constructor type -- is contained in the sort of the data type -- (to avoid impredicative existential types) debugFitsIn s arity <- t' `fitsIn` s debugAdd c t' -- add parameters to constructor type and put into signature let con = ConHead c Inductive [] -- data constructors have no projectable fields and are always inductive escapeContext (size tel) $ addConstant c $ defaultDefn defaultArgInfo c (telePi tel t') $ Constructor { conPars = size tel , conArity = arity , conSrcCon = con , conData = d , conAbstr = Info.defAbstract i , conInd = Inductive , conErased = [] -- computed during compilation to treeless } -- Add the constructor to the instance table, if needed when (Info.defInstance i == InstanceDef) $ do addNamedInstance c d return nonLinPars where debugEnter c e = reportSDoc "tc.data.con" 5 $ vcat [ text "checking constructor" <+> prettyTCM c <+> text ":" <+> prettyTCM e ] debugEndsIn t d n = reportSDoc "tc.data.con" 15 $ vcat [ sep [ text "checking that" , nest 2 $ prettyTCM t , text "ends in" <+> prettyTCM d ] , nest 2 $ text "nofPars =" <+> text (show n) ] debugNonLinPars ks = reportSDoc "tc.data.con" 15 $ text "these constructor parameters are non-linear:" <+> text (show ks) debugFitsIn s = reportSDoc "tc.data.con" 15 $ sep [ text "checking that the type fits in" , nest 2 $ prettyTCM s ] debugAdd c t = reportSDoc "tc.data.con" 5 $ vcat [ text "adding constructor" <+> prettyTCM c <+> text ":" <+> prettyTCM t ] checkConstructor _ _ _ _ _ = __IMPOSSIBLE__ -- constructors are axioms -- | Bind the parameters of a datatype. -- -- We allow omission of hidden parameters at the definition site. -- Example: -- @ -- data D {a} (A : Set a) : Set a -- data D A where -- c : A -> D A -- @ bindParameters :: [A.LamBinding] -> Type -> (Telescope -> Type -> TCM a) -> TCM a bindParameters = bindParameters' [] -- | Auxiliary function for 'bindParameters'. bindParameters' :: [Type] -- ^ @n@ replicas of type if @LamBinding@s are @DomainFree@s -- that came from a @DomainFull@ of @n@ binders. -- Should be comsumed whenever a @DomainFree@s are consumed. -> [A.LamBinding] -- ^ Bindings from definition site. -> Type -- ^ Pi-type of bindings coming from signature site. -> (Telescope -> Type -> TCM a) -- ^ Continuation, accepting parameter telescope and rest of type. -- The parameters are part of the context when the continutation is invoked. -> TCM a bindParameters' _ [] a ret = ret EmptyTel a bindParameters' ts (A.DomainFull (A.TypedBindings _ (Arg info (A.TBind _ xs e))) : bs) a ret = do unless (null ts) __IMPOSSIBLE__ t <- workOnTypes $ isType_ e bindParameters' (t <$ xs) (map (mergeHiding . fmap (A.DomainFree info)) xs ++ bs) a ret bindParameters' _ (A.DomainFull (A.TypedBindings _ (Arg _ A.TLet{})) : _) _ _ = -- line break! __IMPOSSIBLE__ bindParameters' ts0 ps0@(A.DomainFree info x : ps) t ret = do case ignoreSharing $ unEl t of -- Andreas, 2011-04-07 ignore relevance information in binding?! Pi arg@(Dom info' a) b -> do if | info == info' -> do -- Andreas, 2016-12-30, issue #1886: -- If type for binding is present, check its correctness. ts <- caseList ts0 (return []) $ \ t0 ts -> do equalType t0 a return ts continue ts ps x | visible info, notVisible info' -> continue ts0 ps0 =<< freshName_ (absName b) | otherwise -> __IMPOSSIBLE__ -- Andreas, 2016-12-30 Concrete.Definition excludes this case where continue ts ps x = do addContext' (x, arg) $ bindParameters' (raise 1 ts) ps (absBody b) $ \ tel s -> ret (ExtendTel arg $ Abs (nameToArgName x) tel) s _ -> __IMPOSSIBLE__ -- | Check that the arguments to a constructor fits inside the sort of the datatype. -- The first argument is the type of the constructor. -- -- As a side effect, return the arity of the constructor. fitsIn :: Type -> Sort -> TCM Int fitsIn t s = do reportSDoc "tc.data.fits" 10 $ sep [ text "does" <+> prettyTCM t , text "of sort" <+> prettyTCM (getSort t) , text "fit in" <+> prettyTCM s <+> text "?" ] -- The code below would be simpler, but doesn't allow datatypes -- to be indexed by the universe level. -- s' <- instantiateFull (getSort t) -- noConstraints $ s' `leqSort` s t <- reduce t case ignoreSharing $ unEl t of Pi dom b -> do withoutK <- optWithoutK <$> pragmaOptions -- Forced constructor arguments are ignored in size-checking. when (withoutK || notForced (getRelevance dom)) $ do sa <- reduce $ getSort dom unless (sa == SizeUniv) $ sa `leqSort` s addContext (absName b, dom) $ do succ <$> fitsIn (absBody b) (raise 1 s) _ -> return 0 -- getSort t `leqSort` s -- Andreas, 2013-04-13 not necessary since constructor type ends in data type where notForced Forced{} = False notForced _ = True -- | Return the parameters that share variables with the indices -- nonLinearParameters :: Int -> Type -> TCM [Int] -- nonLinearParameters nPars t = -- | Check that a type constructs something of the given datatype. The first -- argument is the number of parameters to the datatype. -- -- As a side effect, return the parameters that occur free in indices. -- E.g. in @data Eq (A : Set)(a : A) : A -> Set where refl : Eq A a a@ -- this would include parameter @a@, but not @A@. -- -- TODO: what if there's a meta here? constructs :: Int -> Type -> QName -> TCM [Int] constructs nofPars t q = constrT 0 t where constrT :: Nat -> Type -> TCM [Int] constrT n t = do t <- reduce t case ignoreSharing $ unEl t of Pi _ (NoAbs _ b) -> constrT n b Pi a b -> underAbstraction a b $ constrT (n + 1) -- OR: addCxtString (absName b) a $ constrT (n + 1) (absBody b) Def d es | d == q -> do let vs = fromMaybe __IMPOSSIBLE__ $ allApplyElims es (pars, ixs) <- normalise $ splitAt nofPars vs -- check that the constructor parameters are the data parameters checkParams n pars -- compute the non-linear parameters m <- getContextSize -- Note: n /= m if NoAbs encountered let nl = nonLinearParams m pars ixs -- assert that these are correct indices into the parameter vector when (any (< 0) nl) __IMPOSSIBLE__ when (any (>= nofPars) nl) __IMPOSSIBLE__ return nl MetaV{} -> do def <- getConstInfo q xs <- newArgsMeta $ defType def let t' = El (dataSort $ theDef def) $ Def q $ map Apply xs equalType t t' constrT n t' _ -> typeError $ ShouldEndInApplicationOfTheDatatype t checkParams n vs = zipWithM_ sameVar vs ps where nvs = size vs ps = genericTake nvs $ downFrom (n + nvs) sameVar arg i -- skip irrelevant parameters | isIrrelevant arg = return () | otherwise = do t <- typeOfBV i equalTerm t (unArg arg) (var i) -- return the parameters (numbered 0,1,...,size pars-1 from left to right) -- that occur relevantly in the indices nonLinearParams n pars ixs = -- compute the free de Bruijn indices in the data indices -- ALT: Ignore all sorts? let fv = allRelevantVarsIgnoring IgnoreInAnnotations ixs -- keep relevant ones, convert to de Bruijn levels -- note: xs is descending list xs = map ((n-1) -) $ VarSet.toList fv -- keep those that correspond to parameters -- in ascending list in reverse $ filter (< size pars) xs {- UNUSED, Andreas 2012-09-13 -- | Force a type to be a specific datatype. forceData :: QName -> Type -> TCM Type forceData d (El s0 t) = liftTCM $ do t' <- reduce t d <- canonicalName d case ignoreSharing t' of Def d' _ | d == d' -> return $ El s0 t' | otherwise -> fail $ "wrong datatype " ++ show d ++ " != " ++ show d' MetaV m vs -> do Defn {defType = t, theDef = Datatype{dataSort = s}} <- getConstInfo d ps <- newArgsMeta t noConstraints $ leqType (El s0 t') (El s (Def d ps)) -- TODO: need equalType? reduce $ El s0 t' _ -> typeError $ ShouldBeApplicationOf (El s0 t) d -} -- | Is the type coinductive? Returns 'Nothing' if the answer cannot -- be determined. isCoinductive :: Type -> TCM (Maybe Bool) isCoinductive t = do El s t <- reduce t case ignoreSharing t of Def q _ -> do def <- getConstInfo q case theDef def of Axiom {} -> return (Just False) Function {} -> return Nothing Datatype { dataInduction = CoInductive } -> return (Just True) Datatype { dataInduction = Inductive } -> return (Just False) Record { recInduction = Just CoInductive } -> return (Just True) Record { recInduction = _ } -> return (Just False) Constructor {} -> __IMPOSSIBLE__ Primitive {} -> __IMPOSSIBLE__ AbstractDefn{} -> __IMPOSSIBLE__ Var {} -> return Nothing Lam {} -> __IMPOSSIBLE__ Lit {} -> __IMPOSSIBLE__ Level {} -> __IMPOSSIBLE__ Con {} -> __IMPOSSIBLE__ Pi {} -> return (Just False) Sort {} -> return (Just False) MetaV {} -> return Nothing Shared{} -> __IMPOSSIBLE__ DontCare{} -> __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Decl.hs-boot0000644000000000000000000000061613154613124020746 0ustar0000000000000000 module Agda.TypeChecking.Rules.Decl where import Agda.Syntax.Info (ModuleInfo) import Agda.Syntax.Abstract (QName, Declaration, ModuleName, ModuleApplication, ScopeCopyInfo) import Agda.TypeChecking.Monad (TCM) checkDecls :: [Declaration] -> TCM () checkDecl :: Declaration -> TCM () checkSectionApplication :: ModuleInfo -> ModuleName -> ModuleApplication -> ScopeCopyInfo -> TCM () Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Def.hs-boot0000644000000000000000000000075513154613124020601 0ustar0000000000000000module Agda.TypeChecking.Rules.Def where import Agda.Syntax.Abstract as A import Agda.Syntax.Common import Agda.Syntax.Info import Agda.TypeChecking.Monad import qualified Agda.Syntax.Internal as I checkFunDef :: Delayed -> DefInfo -> QName -> [Clause] -> TCM () checkFunDef' :: I.Type -> ArgInfo -> Delayed -> Maybe ExtLamInfo -> Maybe QName -> DefInfo -> QName -> [Clause] -> TCM () newSection :: ModuleName -> A.Telescope -> TCM a -> TCM a useTerPragma :: Definition -> TCM Definition Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Record.hs0000644000000000000000000004523213154613124020357 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Rules.Record where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad import Data.Maybe import qualified Data.Set as Set import Agda.Interaction.Options import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.Syntax.Position import qualified Agda.Syntax.Info as Info import Agda.TypeChecking.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Reduce import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Pretty import Agda.TypeChecking.Polarity import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.CompiledClause.Compile import Agda.TypeChecking.Rules.Data ( bindParameters, fitsIn, forceSort) import Agda.TypeChecking.Rules.Term ( isType_ ) import {-# SOURCE #-} Agda.TypeChecking.Rules.Decl (checkDecl) import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Permutation import qualified Agda.Utils.Pretty as P import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Records --------------------------------------------------------------------------- -- | @checkRecDef i name con ps contel fields@ -- -- [@name@] Record type identifier. -- -- [@con@] Maybe constructor name and info. -- -- [@ps@] Record parameters. -- -- [@contel@] Approximate type of constructor (@fields@ -> Set). -- Does not include record parameters. -- -- [@fields@] List of field signatures. -- checkRecDef :: Info.DefInfo -- ^ Position and other info. -> QName -- ^ Record type identifier. -> Maybe (Ranged Induction) -- ^ Optional: (co)inductive declaration. -> Maybe Bool -- ^ Optional: user specified eta/no-eta -> Maybe QName -- ^ Optional: constructor name. -> [A.LamBinding] -- ^ Record parameters. -> A.Expr -- ^ Approximate type of constructor (@fields@ -> Set). -- Does not include record parameters. -> [A.Field] -- ^ Field signatures. -> TCM () checkRecDef i name ind eta con ps contel fields = traceCall (CheckRecDef (getRange name) (qnameName name) ps fields) $ do reportSDoc "tc.rec" 10 $ vcat [ text "checking record def" <+> prettyTCM name , nest 2 $ text "ps =" <+> prettyList (map prettyAs ps) , nest 2 $ text "contel =" <+> prettyA contel , nest 2 $ text "fields =" <+> prettyA (map Constr fields) ] -- get type of record t <- instantiateFull =<< typeOfConst name bindParameters ps t $ \tel t0 -> do -- Generate type of constructor from field telescope @contel@, -- which is the approximate constructor type (target missing). -- Check and evaluate field types. reportSDoc "tc.rec" 15 $ text "checking fields" -- WRONG: contype <- workOnTypes $ killRange <$> (instantiateFull =<< isType_ contel) contype <- instantiateFull =<< isType_ contel reportSDoc "tc.rec" 20 $ vcat [ text "contype = " <+> prettyTCM contype ] -- compute the field telescope (does not include record parameters) let TelV ftel _ = telView' contype -- Compute correct type of constructor -- t = tel -> t0 where t0 must be a sort s TelV idxTel s <- telView t0 unless (null idxTel) $ typeError $ ShouldBeASort t0 s <- forceSort s reportSDoc "tc.rec" 20 $ do gamma <- getContextTelescope -- the record params (incl. module params) text "gamma = " <+> inTopContext (prettyTCM gamma) -- record type (name applied to parameters) rect <- El s . Def name . map Apply <$> getContextArgs -- Put in @rect@ as correct target of constructor type. -- Andreas, 2011-05-10 use telePi_ instead of telePi to preserve -- even names of non-dependent fields in constructor type (Issue 322). let contype = telePi_ ftel (raise (size ftel) rect) -- NB: contype does not contain the parameter telescope -- Obtain name of constructor (if present). (hasNamedCon, conName, conInfo) <- case con of Just c -> return (True, c, i) Nothing -> do m <- killRange <$> currentModule c <- qualify m <$> freshName_ ("recCon-NOT-PRINTED" :: String) return (False, c, i) -- Add record type to signature. reportSDoc "tc.rec" 15 $ text "adding record type to signature" etaenabled <- etaEnabled let getName :: A.Declaration -> [Arg QName] getName (A.Field _ x arg) = [x <$ arg] getName (A.ScopedDecl _ [f]) = getName f getName _ = [] fs = concatMap getName fields -- indCo is what the user wrote: inductive/coinductive/Nothing. -- We drop the Range. indCo = rangedThing <$> ind -- A constructor is inductive unless declared coinductive. conInduction = fromMaybe Inductive indCo -- Andreas, 2016-09-20, issue #2197. -- Eta is inferred by the positivity checker. -- We should turn it off until it is proven to be safe. haveEta = maybe (Inferred False) Specified eta -- haveEta = maybe (Inferred $ conInduction == Inductive && etaenabled) Specified eta con = ConHead conName conInduction $ map unArg fs -- A record is irrelevant if all of its fields are. -- In this case, the associated module parameter will be irrelevant. -- See issue 392. -- Unless it's been declared coinductive or no-eta-equality (#2607). recordRelevance | eta == Just False = Relevant | conInduction == CoInductive = Relevant | otherwise = minimum $ Irrelevant : (map getRelevance $ telToList ftel) -- Andreas, 2017-01-26, issue #2436 -- Disallow coinductive records with eta-equality when (conInduction == CoInductive && etaEqualityToBool haveEta == True) $ do typeError . GenericDocError =<< do sep [ text "Agda doesn't like coinductive records with eta-equality." , text "If you must, use pragma" , text "{-# ETA" <+> prettyTCM name <+> text "#-}" ] reportSDoc "tc.rec" 30 $ text "record constructor is " <+> prettyTCM con -- Add the record definition. -- Andreas, 2016-06-17, Issue #2018: -- Do not rely on @addConstant@ to put in the record parameters, -- as they might be renamed in the context. -- By putting them ourselves (e.g. by using the original type @t@) -- we make sure we get the original names! let npars = size tel telh = fmap hideAndRelParams tel escapeContext npars $ do addConstant name $ defaultDefn defaultArgInfo name t $ Record { recPars = npars , recClause = Nothing , recConHead = con , recNamedCon = hasNamedCon , recFields = fs , recTel = telh `abstract` ftel , recAbstr = Info.defAbstract i , recEtaEquality' = haveEta , recInduction = indCo -- We retain the original user declaration [(co)inductive] -- in case the record turns out to be recursive. -- Determined by positivity checker: , recMutual = Nothing } -- Add record constructor to signature addConstant conName $ defaultDefn defaultArgInfo conName (telh `abstract` contype) $ Constructor { conPars = npars , conArity = size fs , conSrcCon = con , conData = name , conAbstr = Info.defAbstract conInfo , conInd = conInduction , conErased = [] } -- Declare the constructor as eligible for instance search when (Info.defInstance i == InstanceDef) $ do addNamedInstance conName name -- Check that the fields fit inside the sort _ <- contype `fitsIn` s {- Andreas, 2011-04-27 WRONG because field types are checked again and then non-stricts should not yet be irrelevant -- make record parameters hidden and non-stricts irrelevant -- ctx <- (reverse . map hideAndRelParams . take (size tel)) <$> getContext -} {- Andreas, 2013-09-13 DEBUGGING the debug printout reportSDoc "tc.rec" 80 $ sep [ text "current module record telescope" , nest 2 $ (prettyTCM =<< getContextTelescope) ] reportSDoc "tc.rec" 80 $ sep [ text "current module record telescope" , nest 2 $ (text . show =<< getContextTelescope) ] reportSDoc "tc.rec" 80 $ sep [ text "current module record telescope" , nest 2 $ (inTopContext . prettyTCM =<< getContextTelescope) ] reportSDoc "tc.rec" 80 $ sep [ text "current module record telescope" , nest 2 $ do tel <- getContextTelescope text (show tel) $+$ do inTopContext $ do prettyTCM tel $+$ do telA <- reify tel text (show telA) $+$ do ctx <- getContextTelescope text "should be empty:" <+> prettyTCM ctx ] -} let info = setRelevance recordRelevance defaultArgInfo addRecordVar = addContext' ("", Dom info rect) -- the record variable has the empty name by intention, see issue 208 let m = qnameToMName name -- Name of record module. -- Andreas, 2016-02-09 setting all parameters hidden in the record -- section telescope changes the semantics, see e.g. -- test/Succeed/RecordInParModule. -- Ulf, 2016-03-02 but it's the right thing to do (#1759) modifyContext (modifyContextEntries hideOrKeepInstance) $ addRecordVar $ do -- Add the record section. reportSDoc "tc.rec.def" 10 $ sep [ text "record section:" , nest 2 $ sep [ prettyTCM m <+> (inTopContext . prettyTCM =<< getContextTelescope) , fsep $ punctuate comma $ map (return . P.pretty . getName) fields ] ] reportSDoc "tc.rec.def" 15 $ nest 2 $ vcat [ text "field tel =" <+> escapeContext 1 (prettyTCM ftel) ] addSection m -- Andreas, 2016-02-09, Issue 1815 (see also issue 1759). -- For checking the record declarations, hide the record parameters -- and the parameters of the parent modules. modifyContext (modifyContextEntries hideOrKeepInstance) $ addRecordVar $ do -- Check the types of the fields and the other record declarations. withCurrentModule m $ do -- Andreas, 2013-09-13, 2016-01-06. -- Argument telescope for the projections: all parameters are hidden. -- This means parameters of the parent modules and of the current -- record type. -- See test/Succeed/ProjectionsTakeModuleTelAsParameters.agda. tel' <- getContextTelescope setDefaultModuleParameters m checkRecordProjections m name hasNamedCon con tel' (raise 1 ftel) fields return () {-| @checkRecordProjections m r q tel ftel fs@. [@m@ ] name of the generated module [@r@ ] name of the record type [@con@ ] name of the record constructor [@tel@ ] parameters and record variable r ("self") [@ftel@ ] telescope of fields [@fs@ ] the fields to be checked -} checkRecordProjections :: ModuleName -> QName -> Bool -> ConHead -> Telescope -> Telescope -> [A.Declaration] -> TCM () checkRecordProjections m r hasNamedCon con tel ftel fs = do checkProjs EmptyTel ftel fs where checkProjs :: Telescope -> Telescope -> [A.Declaration] -> TCM () checkProjs _ _ [] = return () checkProjs ftel1 ftel2 (A.ScopedDecl scope fs' : fs) = setScope scope >> checkProjs ftel1 ftel2 (fs' ++ fs) checkProjs ftel1 (ExtendTel (Dom ai t) ftel2) (A.Field info x _ : fs) = traceCall (CheckProjection (getRange info) x t) $ do -- Andreas, 2012-06-07: -- Issue 387: It is wrong to just type check field types again -- because then meta variables are created again. -- Instead, we take the field type t from the field telescope. reportSDoc "tc.rec.proj" 5 $ sep [ text "checking projection" <+> prettyTCM x , nest 2 $ vcat [ text "top =" <+> (inTopContext . prettyTCM =<< getContextTelescope) , text "tel =" <+> (inTopContext . prettyTCM $ tel) , text "ftel1 =" <+> prettyTCM ftel1 , text "t =" <+> prettyTCM t , text "ftel2 =" <+> addContext ftel1 (underAbstraction_ ftel2 prettyTCM) , text "abstr =" <+> (text . show) (Info.defAbstract info) ] ] -- Andreas, 2010-09-09 The following comments are misleading, TODO: update -- in fact, tel includes the variable of record type as last one -- e.g. for cartesion product it is -- -- tel = {A' : Set} {B' : Set} (r : Prod A' B') -- create the projection functions (instantiate the type with the values -- of the previous fields) {- what are the contexts? Γ, tel ⊢ t Γ, tel, r ⊢ vs Γ, tel, r, ftel₁ ⊢ raiseFrom (size ftel₁) 1 t -} -- The type of the projection function should be -- {tel} -> (r : R Δ) -> t -- where Δ = Γ, tel is the current context let finalt = telePi (replaceEmptyName "r" tel) t projname = qualify m $ qnameName x projcall o = Var 0 [Proj o projname] rel = getRelevance ai -- the recursive call recurse = checkProjs (abstract ftel1 $ ExtendTel (Dom ai t) $ Abs (nameToArgName $ qnameName projname) EmptyTel) (ftel2 `absApp` projcall ProjSystem) fs reportSDoc "tc.rec.proj" 25 $ nest 2 $ text "finalt=" <+> do inTopContext $ prettyTCM finalt -- Andreas, 2012-02-20 do not add irrelevant projections if -- disabled by --no-irrelevant-projections ifM (return (rel == Irrelevant) `and2M` do not . optIrrelevantProjections <$> pragmaOptions) recurse $ do reportSDoc "tc.rec.proj" 10 $ sep [ text "adding projection" , nest 2 $ prettyTCM projname <+> text ":" <+> inTopContext (prettyTCM finalt) ] -- The body should be -- P.xi {tel} (r _ .. x .. _) = x -- Ulf, 2011-08-22: actually we're dropping the parameters from the -- projection functions so the body is now -- P.xi (r _ .. x .. _) = x -- Andreas, 2012-01-12: irrelevant projections get translated to -- P.xi (r _ .. x .. _) = irrAxiom {level of t} {t} x -- PROBLEM: because of dropped parameters, cannot refer to t -- 2012-04-02: DontCare instead of irrAxiom -- compute body modification for irrelevant projections let bodyMod = case rel of Relevant -> id NonStrict -> id Irrelevant -> DontCare _ -> __IMPOSSIBLE__ let -- Andreas, 2010-09-09: comment for existing code -- split the telescope into parameters (ptel) and the type or the record -- (rt) which should be R ptel telList = telToList tel (_ptel,[rt]) = splitAt (size tel - 1) telList cpo = if hasNamedCon then ConOCon else ConORec cpi = ConPatternInfo (Just cpo) (Just $ argFromDom $ fmap snd rt) conp = defaultArg $ ConP con cpi $ [ Arg ai' $ unnamed $ varP "x" | Dom ai' _ <- telToList ftel ] body = Just $ bodyMod $ var (size ftel2) cltel = ftel clause = Clause { clauseLHSRange = getRange info , clauseFullRange = getRange info , clauseTel = killRange cltel , namedClausePats = [Named Nothing <$> numberPatVars __IMPOSSIBLE__ (idP $ size ftel) conp] , clauseBody = body , clauseType = Just $ Arg ai t , clauseCatchall = False , clauseUnreachable = Just False } let projection = Projection { projProper = Just r , projOrig = projname -- name of the record type: , projFromType = defaultArg r -- index of the record argument (in the type), -- start counting with 1: , projIndex = size tel -- which is @size ptel + 1@ , projLams = ProjLams $ map (\ (Dom ai (x,_)) -> Arg ai x) telList } reportSDoc "tc.rec.proj" 80 $ sep [ text "adding projection" , nest 2 $ prettyTCM projname <+> text (show clause) ] reportSDoc "tc.rec.proj" 70 $ sep [ text "adding projection" , nest 2 $ prettyTCM projname <+> text (show (clausePats clause)) <+> text "=" <+> inTopContext (addContext ftel (maybe (text "_|_") prettyTCM (clauseBody clause))) ] reportSDoc "tc.rec.proj" 10 $ sep [ text "adding projection" , nest 2 $ prettyTCM (QNamed projname clause) ] -- Record patterns should /not/ be translated when the -- projection functions are defined. Record pattern -- translation is defined in terms of projection -- functions. cc <- compileClauses Nothing [clause] reportSDoc "tc.cc" 60 $ do sep [ text "compiled clauses of " <+> prettyTCM projname , nest 2 $ text (show cc) ] escapeContext (size tel) $ do addConstant projname $ (defaultDefn ai projname (killRange finalt) emptyFunction { funClauses = [clause] , funCompiled = Just cc , funProjection = Just projection , funTerminates = Just True , funCopatternLHS = isCopatternLHS [clause] }) { defArgOccurrences = [StrictPos] } computePolarity [projname] when (Info.defInstance info == InstanceDef) $ addTypedInstance projname finalt recurse checkProjs ftel1 ftel2 (d : fs) = do checkDecl d checkProjs ftel1 ftel2 fs Agda-2.5.3/src/full/Agda/TypeChecking/Rules/LHS.hs0000644000000000000000000013460613154613124017573 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Rules.LHS where import Prelude hiding (mapM, sequence) import Data.Maybe import Control.Applicative import Control.Arrow (first, second, (***)) import Control.Monad hiding (mapM, forM, sequence) import Control.Monad.State hiding (mapM, forM, sequence) import Control.Monad.Reader hiding (mapM, forM, sequence) import Control.Monad.Trans.Maybe import Data.Function (on) import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import Data.List (delete, sortBy, stripPrefix) import Data.Monoid import Data.Traversable import Data.Map (Map) import qualified Data.Map as Map import Agda.Interaction.Options import Agda.Interaction.Options.Lenses import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Pattern import Agda.Syntax.Abstract (IsProjP(..)) import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Views (asView, deepUnscope) import Agda.Syntax.Common as Common import Agda.Syntax.Info as A import Agda.Syntax.Position import Agda.Syntax.Scope.Base (ScopeInfo, emptyScopeInfo) import Agda.TypeChecking.Monad import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TypeChecking.Conversion import Agda.TypeChecking.Constraints import Agda.TypeChecking.Datatypes import Agda.TypeChecking.Irrelevance import {-# SOURCE #-} Agda.TypeChecking.Empty import Agda.TypeChecking.Patterns.Abstract import Agda.TypeChecking.Pretty import Agda.TypeChecking.Records import Agda.TypeChecking.Reduce import Agda.TypeChecking.Rewriting import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import {-# SOURCE #-} Agda.TypeChecking.Rules.Term (checkExpr) import Agda.TypeChecking.Rules.LHS.AsPatterns import Agda.TypeChecking.Rules.LHS.Problem hiding (Substitution) import Agda.TypeChecking.Rules.LHS.ProblemRest import Agda.TypeChecking.Rules.LHS.Unify import Agda.TypeChecking.Rules.LHS.Split import Agda.TypeChecking.Rules.LHS.Implicit import Agda.TypeChecking.Rules.LHS.Instantiate import Agda.TypeChecking.Rules.Data import Agda.Utils.Except (MonadError(..)) import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.ListT import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Permutation import Agda.Utils.Pretty (prettyShow) import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | Compute the set of flexible patterns in a list of patterns. The result is -- the deBruijn indices of the flexible patterns. flexiblePatterns :: [NamedArg A.Pattern] -> TCM FlexibleVars flexiblePatterns nps = do forMaybeM (zip (downFrom $ length nps) nps) $ \ (i, Arg ai p) -> do runMaybeT $ (\ f -> FlexibleVar (getHiding ai) (getOrigin ai) f (Just i) i) <$> maybeFlexiblePattern p -- | A pattern is flexible if it is dotted or implicit, or a record pattern -- with only flexible subpatterns. class IsFlexiblePattern a where maybeFlexiblePattern :: a -> MaybeT TCM FlexibleVarKind isFlexiblePattern :: a -> TCM Bool isFlexiblePattern p = maybe False notOtherFlex <$> runMaybeT (maybeFlexiblePattern p) where notOtherFlex = \case RecordFlex fls -> all notOtherFlex fls ImplicitFlex -> True DotFlex -> True OtherFlex -> False instance IsFlexiblePattern A.Pattern where maybeFlexiblePattern p = do reportSDoc "tc.lhs.flex" 30 $ text "maybeFlexiblePattern" <+> prettyA p reportSDoc "tc.lhs.flex" 60 $ text "maybeFlexiblePattern (raw) " <+> (text . show . deepUnscope) p case p of A.DotP{} -> return DotFlex A.VarP{} -> return ImplicitFlex A.WildP{} -> return ImplicitFlex A.AsP _ _ p -> maybeFlexiblePattern p A.ConP _ (A.AmbQ [c]) qs -> ifM (isNothing <$> isRecordConstructor c) (return OtherFlex) {-else-} (maybeFlexiblePattern qs) A.LitP{} -> return OtherFlex _ -> mzero instance IsFlexiblePattern (I.Pattern' a) where maybeFlexiblePattern p = case p of I.DotP{} -> return DotFlex I.ConP _ i ps | Just ConOSystem <- conPRecord i -> return ImplicitFlex -- expanded from ImplicitP | Just _ <- conPRecord i -> maybeFlexiblePattern ps | otherwise -> mzero I.VarP{} -> mzero I.AbsurdP{} -> mzero I.LitP{} -> mzero I.ProjP{} -> mzero -- | Lists of flexible patterns are 'RecordFlex'. instance IsFlexiblePattern a => IsFlexiblePattern [a] where maybeFlexiblePattern ps = RecordFlex <$> mapM maybeFlexiblePattern ps instance IsFlexiblePattern a => IsFlexiblePattern (Arg a) where maybeFlexiblePattern = maybeFlexiblePattern . unArg instance IsFlexiblePattern a => IsFlexiblePattern (Common.Named name a) where maybeFlexiblePattern = maybeFlexiblePattern . namedThing -- | Update the in patterns according to the given substitution, collecting -- new dot pattern instantiations in the process. updateInPatterns :: [Dom Type] -- ^ the types of the old pattern variables, -- relative to the new telescope -> [NamedArg A.Pattern] -- ^ old in patterns -> [Arg DeBruijnPattern] -- ^ patterns to be substituted, living in the -- new telescope -> TCM ([NamedArg A.Pattern] -- new in patterns ,[DotPatternInst]) -- new dot pattern instantiations updateInPatterns as ps qs = do reportSDoc "tc.lhs.top" 20 $ text "updateInPatterns" <+> nest 2 (vcat [ text "as =" <+> prettyList (map prettyTCM as) , text "ps =" <+> prettyList (map prettyA ps) , text "qs =" <+> prettyList (map pretty qs) ]) first (map snd . IntMap.toDescList) <$> updates as ps qs where updates :: [Dom Type] -> [NamedArg A.Pattern] -> [Arg DeBruijnPattern] -> TCM (IntMap (NamedArg A.Pattern), [DotPatternInst]) updates as ps qs = mconcat <$> sequence (zipWith3 update as ps qs) update :: Dom Type -> NamedArg A.Pattern -> Arg DeBruijnPattern -> TCM (IntMap (NamedArg A.Pattern), [DotPatternInst]) update a p q = case unArg q of -- Case: the unifier did not instantiate the variable VarP x -> return (IntMap.singleton (dbPatVarIndex x) p, []) -- Case: the unifier did instantiate the variable DotP u -> case snd $ asView $ namedThing (unArg p) of A.DotP _ _ e -> return (IntMap.empty, [DPI Nothing (Just e) u a]) A.WildP _ -> return (IntMap.empty, [DPI Nothing Nothing u a]) A.VarP x -> return (IntMap.empty, [DPI (Just x) Nothing u a]) p@(A.ConP _ (A.AmbQ [c]) qs) -> ifM (isNothing <$> isRecordConstructor c) (return (IntMap.empty, [DPI Nothing (Just $ A.patternToExpr p) u a])) (do Def r es <- ignoreSharing <$> reduce (unEl $ unDom a) let vs = fromMaybe __IMPOSSIBLE__ $ allApplyElims es (ftel, us) <- etaExpandRecord r vs u qs <- insertImplicitPatterns ExpandLast qs ftel reportSDoc "tc.lhs.imp" 20 $ text "insertImplicitPatternsT returned" <+> fsep (map prettyA qs) let instTel EmptyTel _ = [] instTel (ExtendTel arg tel) (u : us) = arg : instTel (absApp tel u) us instTel ExtendTel{} [] = __IMPOSSIBLE__ bs0 = instTel ftel (map unArg us) -- Andreas, 2012-09-19 propagate relevance info to dot patterns bs = map (mapRelevance (composeRelevance (getRelevance a))) bs0 updates bs qs (map (DotP . unArg) us `withArgsFrom` teleArgNames ftel)) p@A.ConP{} -> return (IntMap.empty, [DPI Nothing (Just $ A.patternToExpr p) u a]) p@A.LitP{} -> return (IntMap.empty, [DPI Nothing (Just $ A.patternToExpr p) u a]) A.AsP _ _ _ -> __IMPOSSIBLE__ A.RecP _ _ -> __IMPOSSIBLE__ A.ProjP _ _ _ -> __IMPOSSIBLE__ A.DefP _ _ _ -> __IMPOSSIBLE__ A.AbsurdP _ -> __IMPOSSIBLE__ A.PatternSynP _ _ _ -> __IMPOSSIBLE__ -- Case: the unifier eta-expanded the variable ConP _c _cpi qs -> do Def r es <- ignoreSharing <$> reduce (unEl $ unDom a) def <- theDef <$> getConstInfo r let pars = fromMaybe __IMPOSSIBLE__ $ allApplyElims es fs = killRange $ recFields def tel = recTel def `apply` pars as = applyPatSubst (parallelS $ map (namedThing . unArg) qs) $ flattenTel tel -- If the user wrote a dot pattern or variable but the unifier -- eta-expanded it, add the corresponding instantiation. dpi :: [DotPatternInst] dpi = mkDPI $ patternToTerm $ unArg q where mkDPI v = case namedThing $ unArg p of A.DotP _ _ e -> [DPI Nothing (Just e) v a] A.VarP x -> [DPI (Just x) Nothing v a] _ -> [] second (dpi++) <$> updates as (projectInPat p fs) (map (fmap namedThing) qs) AbsurdP{} -> __IMPOSSIBLE__ LitP _ -> __IMPOSSIBLE__ ProjP{} -> __IMPOSSIBLE__ projectInPat :: NamedArg A.Pattern -> [Arg QName] -> [NamedArg A.Pattern] projectInPat p fs = case namedThing (unArg p) of A.VarP x -> map (makeWildField (PatRange $ getRange x)) fs A.ConP _ _ nps -> nps A.WildP pi -> map (makeWildField pi) fs A.DotP pi o e -> map (makeDotField pi o) fs A.ProjP _ _ _ -> __IMPOSSIBLE__ A.DefP _ _ _ -> __IMPOSSIBLE__ A.AsP _ _ _ -> __IMPOSSIBLE__ A.AbsurdP _ -> __IMPOSSIBLE__ A.LitP _ -> __IMPOSSIBLE__ A.PatternSynP _ _ _ -> __IMPOSSIBLE__ A.RecP _ _ -> __IMPOSSIBLE__ where makeWildField pi (Arg fi f) = Arg fi $ unnamed $ A.WildP pi makeDotField pi o (Arg fi f) = Arg fi $ unnamed $ A.DotP pi o $ A.Underscore underscoreInfo where underscoreInfo = A.MetaInfo { A.metaRange = getRange pi , A.metaScope = emptyScopeInfo , A.metaNumber = Nothing , A.metaNameSuggestion = prettyShow $ qnameName f } -- | Check if a problem is solved. -- That is, if the patterns are all variables, -- and there is no 'problemRest'. isSolvedProblem :: Problem -> Bool isSolvedProblem problem = null (restPats $ problemRest problem) && problemAllVariables problem -- | Check if a problem consists only of variable patterns. -- (Includes the 'problemRest'). problemAllVariables :: Problem -> Bool problemAllVariables problem = all (isSolved . snd . asView . namedArg) $ restPats (problemRest problem) ++ problemInPat problem where -- need further splitting: isSolved A.ConP{} = False isSolved A.LitP{} = False isSolved A.ProjP{} = False isSolved A.RecP{} = False -- record pattern isSolved A.AbsurdP{} = False -- solved: isSolved A.VarP{} = True isSolved A.WildP{} = True isSolved A.DotP{} = True -- impossible: isSolved A.DefP{} = __IMPOSSIBLE__ isSolved A.AsP{} = __IMPOSSIBLE__ -- removed by asView isSolved A.PatternSynP{} = __IMPOSSIBLE__ -- expanded before -- | For each user-defined pattern variable in the 'Problem', check -- that the corresponding data type (if any) does not contain a -- constructor of the same name (which is not in scope); this -- \"shadowing\" could indicate an error, and is not allowed. -- -- Precondition: The problem has to be solved. noShadowingOfConstructors :: Call -- ^ Trace, e.g., @CheckPatternShadowing clause@ -> Problem -> TCM () noShadowingOfConstructors mkCall problem = traceCall mkCall $ do let pat = map (snd . asView . namedArg) $ problemInPat problem tel = map (unEl . snd . unDom) $ telToList $ problemTel problem zipWithM_ noShadowing pat tel -- TODO: does not work for flexible arity and projection patterns return () where noShadowing (A.WildP {}) t = return () noShadowing (A.AbsurdP {}) t = return () noShadowing (A.ConP {}) t = return () -- only happens for eta expanded record patterns noShadowing (A.RecP {}) t = return () -- record pattern noShadowing (A.ProjP {}) t = return () -- projection pattern noShadowing (A.DefP {}) t = __IMPOSSIBLE__ noShadowing (A.DotP {}) t = return () noShadowing (A.AsP {}) t = __IMPOSSIBLE__ -- removed by asView noShadowing (A.LitP {}) t = __IMPOSSIBLE__ noShadowing (A.PatternSynP {}) t = __IMPOSSIBLE__ noShadowing (A.VarP x) t = do reportSDoc "tc.lhs.shadow" 30 $ vcat [ text $ "checking whether pattern variable " ++ prettyShow x ++ " shadows a constructor" , nest 2 $ text "type of variable =" <+> prettyTCM t ] reportSDoc "tc.lhs.shadow" 70 $ nest 2 $ text "t =" <+> pretty t t <- reduce t case t of Def t _ -> do d <- theDef <$> getConstInfo t case d of Datatype { dataCons = cs } -> do case filter ((A.nameConcrete x ==) . A.nameConcrete . A.qnameName) cs of [] -> return () (c : _) -> setCurrentRange x $ typeError $ PatternShadowsConstructor x c AbstractDefn{} -> return () -- Abstract constructors cannot be brought into scope, -- even by a bigger import list. -- Thus, they cannot be confused with variables. -- Alternatively, we could do getConstInfo in ignoreAbstractMode, -- then Agda would complain if a variable shadowed an abstract constructor. Axiom {} -> return () Function {} -> return () Record {} -> return () Constructor {} -> __IMPOSSIBLE__ -- TODO: in the future some stuck primitives might allow constructors Primitive {} -> return () Var {} -> return () Pi {} -> return () Sort {} -> return () Shared p -> noShadowing (A.VarP x) $ derefPtr p MetaV {} -> return () -- TODO: If the type is a meta-variable, should the test be -- postponed? If there is a problem, then it will be caught when -- the completed module is type checked, so it is safe to skip -- the test here. However, users may be annoyed if they get an -- error in code which has already passed the type checker. Lam {} -> __IMPOSSIBLE__ Lit {} -> __IMPOSSIBLE__ Level {} -> __IMPOSSIBLE__ Con {} -> __IMPOSSIBLE__ DontCare{} -> __IMPOSSIBLE__ -- | Check that a dot pattern matches it's instantiation. checkDotPattern :: DotPatternInst -> TCM () checkDotPattern (DPI _ (Just e) v (Dom info a)) = traceCall (CheckDotPattern e v) $ do reportSDoc "tc.lhs.dot" 15 $ sep [ text "checking dot pattern" , nest 2 $ prettyA e , nest 2 $ text "=" <+> prettyTCM v , nest 2 $ text ":" <+> prettyTCM a ] applyRelevanceToContext (argInfoRelevance info) $ do u <- checkExpr e a reportSDoc "tc.lhs.dot" 50 $ sep [ text "equalTerm" , nest 2 $ pretty a , nest 2 $ pretty u , nest 2 $ pretty v ] -- Should be ok to do noConstraints here noConstraints $ equalTerm a u v checkDotPattern (DPI _ Nothing _ _) = return () -- | Temporary data structure for 'checkLeftoverPatterns' type Projectn = (ProjOrigin, QName) type Projectns = [Projectn] -- | Checks whether the dot patterns left over after splitting can be covered -- by shuffling around the dots from implicit positions. Returns the updated -- user patterns (without dot patterns). checkLeftoverDotPatterns :: [NamedArg A.Pattern] -- ^ Leftover patterns after splitting is completed -> [Int] -- ^ De Bruijn indices of leftover variable patterns -- computed by splitting -> [Dom Type] -- ^ Types of leftover patterns -> [DotPatternInst] -- ^ Instantiations computed by unifier -> TCM () checkLeftoverDotPatterns ps vs as dpi = do reportSDoc "tc.lhs.dot" 15 $ text "checking leftover dot patterns..." idv <- sortBy (compare `on` length . snd) . concat <$> traverse gatherImplicitDotVars dpi reportSDoc "tc.lhs.dot" 30 $ nest 2 $ text "implicit dotted variables:" <+> prettyList (map (\(i,fs) -> prettyTCM $ Var i (map (uncurry Proj) fs)) idv) checkUserDots ps vs as idv reportSDoc "tc.lhs.dot" 15 $ text "all leftover dot patterns ok!" where checkUserDots :: [NamedArg A.Pattern] -> [Int] -> [Dom Type] -> [(Int,Projectns)] -> TCM () checkUserDots [] [] [] idv = return () checkUserDots [] (_:_) _ idv = __IMPOSSIBLE__ checkUserDots [] _ (_:_) idv = __IMPOSSIBLE__ checkUserDots (_:_) [] _ idv = __IMPOSSIBLE__ checkUserDots (_:_) _ [] idv = __IMPOSSIBLE__ checkUserDots (p:ps) (v:vs) (a:as) idv = do idv' <- checkUserDot p v a idv checkUserDots ps vs as idv' checkUserDot :: NamedArg A.Pattern -> Int -> Dom Type -> [(Int,Projectns)] -> TCM [(Int,Projectns)] checkUserDot p v a idv = case namedArg p of A.DotP i o e | o == Inserted -> return idv -- Jesper, 2016-12-08 (Issue 1605): if the origin is Inserted, this -- means the dot pattern was created by expanding '...', so we don't -- have to complain here. A.DotP i o e -> do reportSDoc "tc.lhs.dot" 30 $ nest 2 $ text "checking user dot pattern: " <+> prettyA e caseMaybeM (undotImplicitVar (v,[],unDom a) idv) (traceCall (CheckPattern (namedArg p) EmptyTel (unDom a)) $ typeError $ UninstantiatedDotPattern e) (\idv' -> do u <- checkExpr e (unDom a) reportSDoc "tc.lhs.dot" 30 $ nest 2 $ text "checked expression: " <+> prettyTCM u noConstraints $ equalTerm (unDom a) u (var v) return idv') A.VarP _ -> return idv A.WildP _ -> return idv A.AbsurdP _ -> return idv -- Andreas, 2017-01-18, issue #2413, AsP is not __IMPOSSIBLE__ A.AsP _ _ p0 -> checkUserDot (setNamedArg p p0) v a idv A.ConP _ _ _ -> __IMPOSSIBLE__ A.LitP _ -> __IMPOSSIBLE__ A.ProjP _ _ _-> __IMPOSSIBLE__ A.DefP _ _ _ -> __IMPOSSIBLE__ A.RecP _ _ -> __IMPOSSIBLE__ A.PatternSynP _ _ _ -> __IMPOSSIBLE__ gatherImplicitDotVars :: DotPatternInst -> TCM [(Int,Projectns)] gatherImplicitDotVars (DPI _ (Just _) _ _) = return [] -- Not implicit gatherImplicitDotVars (DPI _ Nothing u _) = gatherVars u where gatherVars :: Term -> TCM [(Int,Projectns)] gatherVars u = case ignoreSharing u of Var i es -> return $ (i,) <$> maybeToList (allProjElims es) Con c _ us -> ifM (isEtaCon $ conName c) {-then-} (concat <$> traverse (gatherVars . unArg) us) {-else-} (return []) _ -> return [] lookupImplicitDotVar :: (Int,Projectns) -> [(Int,Projectns)] -> Maybe Projectns lookupImplicitDotVar (i,fs) [] = Nothing lookupImplicitDotVar (i,fs) ((j,gs):js) -- Andreas, 2016-09-20, issue #2196 -- We need to ignore the ProjOrigin! | i == j , Just hs <- stripPrefixBy ((==) `on` snd) fs gs = Just hs | otherwise = lookupImplicitDotVar (i,fs) js undotImplicitVar :: (Int,Projectns,Type) -> [(Int,Projectns)] -> TCM (Maybe [(Int,Projectns)]) undotImplicitVar (i,fs,a) idv = do reportSDoc "tc.lhs.dot" 40 $ vcat [ text "undotImplicitVar" , nest 2 $ vcat [ text "i =" <+> pshow i , text "fs =" <+> sep (map (prettyTCM . snd) fs) , text "a =" <+> prettyTCM a , text "raw=" <+> pretty a , text "idv=" <+> pshow idv ] ] case lookupImplicitDotVar (i,fs) idv of Nothing -> return Nothing Just [] -> return $ Just $ delete (i,fs) idv Just rs -> caseMaybeM (isEtaRecordType a) (return Nothing) $ \(d,pars) -> do gs <- recFields . theDef <$> getConstInfo d let u = Var i (map (uncurry Proj) fs) is <- forM gs $ \(Arg _ g) -> do (_,_,b) <- fromMaybe __IMPOSSIBLE__ <$> projectTyped u a ProjSystem g return (i,fs++[(ProjSystem,g)],b) undotImplicitVars is idv undotImplicitVars :: [(Int,Projectns,Type)] -> [(Int,Projectns)] -> TCM (Maybe [(Int,Projectns)]) undotImplicitVars [] idv = return $ Just idv undotImplicitVars (i:is) idv = caseMaybeM (undotImplicitVar i idv) (return Nothing) (\idv' -> undotImplicitVars is idv') -- | Bind the variables in a left hand side and check that 'Hiding' of -- the patterns matches the hiding info in the type. -- -- Precondition: the patterns should -- all be 'A.VarP', 'A.WildP', or 'A.AbsurdP' and the -- telescope should have the same size as the pattern list. -- There could also be 'A.ConP's resulting from eta expanded implicit record -- patterns. bindLHSVars :: [NamedArg A.Pattern] -> Telescope -> TCM a -> TCM a bindLHSVars [] tel@ExtendTel{} _ = do reportSDoc "impossible" 10 $ text "bindLHSVars: no patterns left, but tel =" <+> prettyTCM tel __IMPOSSIBLE__ bindLHSVars (_ : _) EmptyTel _ = __IMPOSSIBLE__ bindLHSVars [] EmptyTel ret = ret bindLHSVars (p : ps) tel0@(ExtendTel a tel) ret = do -- see test/Fail/WronHidingInLHS: unless (sameHiding p a) $ typeError WrongHidingInLHS case namedArg p of A.VarP x -> addContext (x, a) $ bindLHSVars ps (absBody tel) ret A.WildP _ -> bindDummy (absName tel) -- @bindDummy underscore@ does not fix issue 819, but -- introduces unwanted underscores in error messages -- (Andreas, 2015-05-28) A.DotP _ _ _ -> bindDummy (absName tel) A.AbsurdP pi -> __IMPOSSIBLE__ -- Andreas, 2017-01-18, issue #2413 -- A.AsP is not __IMPOSSIBLE__ A.AsP _ _ p0 -> bindLHSVars (setNamedArg p p0 : ps) tel0 ret A.ConP{} -> __IMPOSSIBLE__ A.RecP{} -> __IMPOSSIBLE__ A.ProjP{} -> __IMPOSSIBLE__ A.DefP{} -> __IMPOSSIBLE__ A.LitP{} -> __IMPOSSIBLE__ A.PatternSynP{} -> __IMPOSSIBLE__ where bindDummy s = do x <- if isUnderscore s then freshNoName_ else unshadowName =<< freshName_ ("." ++ argNameToString s) addContext (x, a) $ bindLHSVars ps (absBody tel) ret -- | Bind as patterns bindAsPatterns :: [AsBinding] -> TCM a -> TCM a bindAsPatterns [] ret = ret bindAsPatterns (AsB x v a : asb) ret = do reportSDoc "tc.lhs.as" 10 $ text "as pattern" <+> prettyTCM x <+> sep [ text ":" <+> prettyTCM a , text "=" <+> prettyTCM v ] addLetBinding defaultArgInfo x v a $ bindAsPatterns asb ret -- | Result of checking the LHS of a clause. data LHSResult = LHSResult { lhsParameters :: Nat -- ^ The number of original module parameters. These are present in the -- the patterns. , lhsVarTele :: Telescope -- ^ Δ : The types of the pattern variables, in internal dependency order. -- Corresponds to 'clauseTel'. , lhsPatterns :: [NamedArg DeBruijnPattern] -- ^ The patterns in internal syntax. , lhsBodyType :: Arg Type -- ^ The type of the body. Is @bσ@ if @Γ@ is defined. -- 'Irrelevant' to indicate the rhs must be checked in irrelevant mode. , lhsPatSubst :: Substitution -- ^ Substitution version of @lhsPatterns@, only up to the first projection -- pattern. @Δ |- lhsPatSubst : Γ@. Where @Γ@ is the argument telescope of -- the function. This is used to update inherited dot patterns in -- with-function clauses. , lhsAsBindings :: [AsBinding] -- ^ As-bindings from the left-hand side. Return instead of bound since we -- want them in where's and right-hand sides, but not in with-clauses -- (Issue 2303). } instance InstantiateFull LHSResult where instantiateFull' (LHSResult n tel ps t sub as) = LHSResult n <$> instantiateFull' tel <*> instantiateFull' ps <*> instantiateFull' t <*> instantiateFull' sub <*> instantiateFull' as -- | Check a LHS. Main function. -- -- @checkLeftHandSide a ps a ret@ checks that user patterns @ps@ eliminate -- the type @a@ of the defined function, and calls continuation @ret@ -- if successful. checkLeftHandSide :: Call -- ^ Trace, e.g. @CheckPatternShadowing clause@ -> Maybe QName -- ^ The name of the definition we are checking. -> [NamedArg A.Pattern] -- ^ The patterns. -> Type -- ^ The expected type @a = Γ → b@. -> Maybe Substitution -- ^ Module parameter substitution from with-abstraction. -> [A.StrippedDotPattern] -- ^ Dot patterns that have been stripped away by with-desugaring. -> (LHSResult -> TCM a) -- ^ Continuation. -> TCM a checkLeftHandSide c f ps a withSub' strippedDots = Bench.billToCPS [Bench.Typing, Bench.CheckLHS] $ \ ret -> do -- To allow module parameters to be refined by matching, we're adding the -- context arguments as wildcard patterns and extending the type with the -- context telescope. cxt <- reverse <$> getContext let tel = telFromList' prettyShow cxt cps = [ unnamed . A.VarP . fst <$> setOrigin Inserted (argFromDom d) | d <- cxt ] problem0 <- problemFromPats (cps ++ ps) (telePi tel a) -- Andreas, 2013-03-15 deactivating the following test allows -- flexible arity -- unless (noProblemRest problem) $ typeError $ TooManyArgumentsInLHS a -- We need to grab all let-bindings here (while we still have the old -- context). They will be rebound below once we have the new context set up. -- Subtle: if we're checking a with the context will be empty so we can't use -- 'getOpen'. On the other hand, if we're checking a with the let bindings -- lives in the right context already so we can use 'openThing'. let openLet | isNothing withSub' = getOpen | otherwise = return . openThing oldLets <- asks $ Map.toList . envLetBindings reportSDoc "tc.lhs.top" 70 $ vcat [ text "context =" <+> inTopContext (prettyTCM tel) , text "cIds =" <+> (text . show =<< getContextId) , text "oldLets =" <+> text (show oldLets) ] oldLets <- sequence [ (x,) <$> openLet b | (x, b) <- oldLets ] -- doing the splits: inTopContext $ do LHSState problem@(Problem pxs qs delta rest) dpi sbe <- checkLHS f $ LHSState problem0 [] [] unless (null $ restPats rest) $ typeError $ TooManyArgumentsInLHS a addContext delta $ do noShadowingOfConstructors c problem noPatternMatchingOnCodata qs -- f is Nothing when checking let pattern-bindings. In that case there can -- be no copatterns, so we don't need to worry about self. let self = Def (fromMaybe __IMPOSSIBLE__ f) [] asb <- addContext delta $ recoverAsPatterns delta (telePi tel a) self (cps ++ ps) qs reportSDoc "tc.lhs.top" 10 $ vcat [ text "checked lhs:" , nest 2 $ vcat [ text "pxs = " <+> fsep (map prettyA pxs) , text "delta = " <+> prettyTCM delta , text "dpi = " <+> addContext delta (brackets $ fsep $ punctuate comma $ map prettyTCM dpi) , text "asb = " <+> addContext delta (brackets $ fsep $ punctuate comma $ map prettyTCM asb) , text "qs = " <+> prettyList (map pretty qs) ] ] let b' = restType rest bindLHSVars (filter (isNothing . isProjP) pxs) delta $ do let -- Find the variable patterns that have been refined refinedParams = [ AsB x v (unDom a) | DPI (Just x) _ v a <- dpi ] asb' = refinedParams ++ asb reportSDoc "tc.lhs.top" 10 $ text "asb' = " <+> (brackets $ fsep $ punctuate comma $ map prettyTCM asb') reportSDoc "tc.lhs.top" 10 $ text "bound pattern variables" reportSDoc "tc.lhs.top" 60 $ nest 2 $ text "context = " <+> (pretty =<< getContextTelescope) reportSDoc "tc.lhs.top" 10 $ nest 2 $ text "type = " <+> prettyTCM b' reportSDoc "tc.lhs.top" 60 $ nest 2 $ text "type = " <+> pretty b' let notProj ProjP{} = False notProj _ = True -- Note: This works because we can't change the number of -- arguments in the lhs of a with-function relative to -- the parent function. numPats = length $ takeWhile (notProj . namedArg) qs -- In the case of a non-with function the pattern substitution -- should be weakened by the number of non-parameter patterns to -- get the paramSub. withSub = fromMaybe (wkS (numPats - length cxt) idS) withSub' -- At this point we need to update the module parameters for all -- parent modules. patSub = (map (patternToTerm . namedArg) $ reverse $ take numPats qs) ++# (EmptyS __IMPOSSIBLE__) paramSub = composeS patSub withSub lhsResult = LHSResult (length cxt) delta qs b' patSub asb' reportSDoc "tc.lhs.top" 20 $ nest 2 $ text "patSub = " <+> pretty patSub reportSDoc "tc.lhs.top" 20 $ nest 2 $ text "withSub = " <+> pretty withSub reportSDoc "tc.lhs.top" 20 $ nest 2 $ text "paramSub = " <+> pretty paramSub let newLets = [ AsB x (applySubst paramSub v) (applySubst paramSub $ unDom a) | (x, (v, a)) <- oldLets ] reportSDoc "tc.lhs.top" 50 $ text "old let-bindings:" <+> text (show oldLets) reportSDoc "tc.lhs.top" 50 $ text "new let-bindings:" <+> (brackets $ fsep $ punctuate comma $ map prettyTCM newLets) bindAsPatterns newLets $ applyRelevanceToContext (getRelevance b') $ updateModuleParameters paramSub $ do bindAsPatterns asb' $ do -- Check dot patterns mapM_ checkDotPattern dpi mapM_ (uncurry isEmptyType) sbe checkLeftoverDotPatterns pxs (downFrom $ size delta) (flattenTel delta) dpi -- Type check dot patterns that have been thrown away by -- with-desugaring. mapM_ checkStrippedDotPattern $ applySubst paramSub strippedDots -- Issue2303: don't bind asb' for the continuation (return in lhsResult instead) ret lhsResult -- | The loop (tail-recursive): split at a variable in the problem until problem is solved checkLHS :: Maybe QName -- ^ The name of the definition we are checking. -> LHSState -- ^ The current state. -> TCM LHSState -- ^ The final state after all splitting is completed checkLHS f st@(LHSState problem dpi sbe) = do problem <- insertImplicitProblem problem -- Note: inserting implicits no longer preserve solvedness, -- since we might insert eta expanded record patterns. if isSolvedProblem problem then return $ st { lhsProblem = problem } else do unlessM (optPatternMatching <$> gets getPragmaOptions) $ unless (problemAllVariables problem) $ typeError $ GenericError $ "Pattern matching is disabled" foldListT trySplit nothingToSplit $ splitProblem f problem where nothingToSplit :: TCM LHSState nothingToSplit = do reportSLn "tc.lhs.split" 50 $ "checkLHS: nothing to split in problem " ++ show problem nothingToSplitError problem -- Split problem rest (projection pattern, does not fail as there is no call to unifier) trySplit :: SplitProblem -> TCM LHSState -> TCM LHSState trySplit (SplitRest projPat o projType) _ = do -- Compute the new problem let Problem ps1 ip delta (ProblemRest (p:ps2) b) = problem -- ps' = ps1 ++ [p] ps' = ps1 -- drop the projection pattern (already splitted) rest = ProblemRest ps2 (projPat $> projType) ip' = ip ++ [fmap (Named Nothing . ProjP o) projPat] problem' = Problem ps' ip' delta rest -- Jump the trampolin st' <- updateProblemRest (LHSState problem' dpi sbe) -- If the field is irrelevant, we need to continue in irr. cxt. -- (see Issue 939). applyRelevanceToContext (getRelevance projPat) $ do checkLHS f st' -- Split on literal pattern (does not fail as there is no call to unifier) trySplit (Split p0 (Arg _ (LitFocus lit ip a)) p1) _ = do -- substitute the literal in p1 and dpi let delta1 = problemTel p0 delta2 = absApp (fmap problemTel p1) (Lit lit) rho = singletonS (size delta2) (LitP lit) -- Andreas, 2015-06-13 Literals are closed, so need to raise them! -- rho = liftS (size delta2) $ singletonS 0 (Lit lit) -- rho = [ var i | i <- [0..size delta2 - 1] ] -- ++ [ raise (size delta2) $ Lit lit ] -- ++ [ var i | i <- [size delta2 ..] ] dpi' = applyPatSubst rho dpi sbe' = map (second $ applyPatSubst rho) sbe ip' = applySubst rho ip rest' = applyPatSubst rho (problemRest problem) -- Compute the new problem let ps' = problemInPat p0 ++ problemInPat (absBody p1) delta' = abstract delta1 delta2 problem' = Problem ps' ip' delta' rest' st' <- updateProblemRest (LHSState problem' dpi' sbe') checkLHS f st' -- Split on absurd pattern (adding type to list of types that should be empty) trySplit (Split p0 (Arg info (AbsurdFocus pi i a)) p1) _ = do let tel = problemTel problem reportSDoc "tc.lhs.split.absurd" 10 $ sep [ text "splitting on absurd pattern" , nest 2 $ text "tel =" <+> prettyTCM tel , nest 2 $ text "var =" <+> addContext tel (prettyTCM $ var i) , nest 2 $ text "type =" <+> addContext tel (prettyTCM a) ] let rho = liftS i $ consS (AbsurdP $ VarP $ DBPatVar "()" 0) $ raiseS 1 checkLHS f $ st { lhsProblem = problem { problemInPat = (problemInPat p0) ++ [Arg info $ unnamed $ A.WildP pi] ++ problemInPat (absBody p1) , problemOutPat = applySubst rho $ problemOutPat problem } , lhsShouldBeEmptyTypes = (getRange pi , a) : lhsShouldBeEmptyTypes st } -- Split on constructor pattern (unifier might fail) trySplit (Split p0 (Arg info (Focus { focusCon = c , focusPatOrigin= porigin , focusConArgs = qs , focusRange = r , focusOutPat = ip , focusDatatype = d , focusParams = vs , focusIndices = ws , focusType = a } )) p1) tryNextSplit = do traceCall (CheckPattern (A.ConP (ConPatInfo porigin $ PatRange r) (A.AmbQ [c]) qs) (problemTel p0) (El Prop $ Def d $ map Apply $ vs ++ ws)) $ do let delta1 = problemTel p0 delta2 = problemTel $ absBody p1 let typeOfSplitVar = Arg info a reportSDoc "tc.lhs.split" 10 $ sep [ text "checking lhs" , nest 2 $ text "tel =" <+> prettyTCM (problemTel problem) , nest 2 $ text "rel =" <+> (text $ show $ argInfoRelevance info) ] reportSDoc "tc.lhs.split" 15 $ sep [ text "split problem" , nest 2 $ vcat [ text "delta1 = " <+> prettyTCM delta1 , text "typeOfSplitVar =" <+> addContext delta1 (prettyTCM typeOfSplitVar) , text "focusOutPat =" <+> pretty ip , text "delta2 = " <+> addContext delta1 (addContext ("x",domFromArg typeOfSplitVar) (prettyTCM delta2)) ] ] c <- either (sigError __IMPOSSIBLE_VERBOSE__ (typeError $ AbstractConstructorNotInScope c)) (return . (`withRangeOf` c)) =<< getConForm c ca <- defType <$> getConInfo c reportSDoc "tc.lhs.split" 20 $ nest 2 $ vcat [ text "ca =" <+> prettyTCM ca , text "vs =" <+> prettyList (map prettyTCM vs) ] -- Lookup the type of the constructor at the given parameters let a = ca `piApply` vs -- It will end in an application of the datatype (gamma', ca, d', us) <- do TelV gamma' ca@(El _ def) <- telView a let Def d' es = ignoreSharing def Just us = allApplyElims es return (gamma', ca, d', us) -- This should be the same datatype as we split on unless (d == d') $ typeError $ ShouldBeApplicationOf ca d' reportSDoc "tc.lhs.top" 20 $ addContext delta1 $ nest 2 $ vcat [ text "gamma' =" <+> prettyTCM gamma' ] -- Andreas 2010-09-07 propagate relevance info to new vars let updRel = composeRelevance (getRelevance info) gamma' <- return $ mapRelevance updRel <$> gamma' -- Insert implicit patterns qs' <- insertImplicitPatterns ExpandLast qs gamma' reportSDoc "tc.lhs.imp" 20 $ text "insertImplicitPatternsT returned" <+> fsep (map prettyA qs') unless ((size qs' :: Int) == size gamma') $ typeError $ WrongNumberOfConstructorArguments (conName c) (size gamma') (size qs') let gamma = useNamesFromPattern qs' gamma' -- Get the type of the datatype. da <- (`piApply` vs) . defType <$> getConstInfo d reportSDoc "tc.lhs.split" 30 $ text " da = " <+> prettyTCM da -- Compute the flexible variables flex <- flexiblePatterns (problemInPat p0 ++ qs') reportSDoc "tc.lhs.split" 30 $ text "computed flexible variables" -- Compute the constructor indices by dropping the parameters let us' = drop (size vs) us -- Raise given indices over constructor telescope let ws' = raise (size gamma) ws reportSDoc "tc.lhs.top" 15 $ addContext delta1 $ sep [ text "preparing to unify" , nest 2 $ vcat [ text "c =" <+> prettyTCM c <+> text ":" <+> prettyTCM a , text "d =" <+> prettyTCM (Def d (map Apply $ vs++ws)) <+> text ":" <+> prettyTCM da , text "gamma =" <+> prettyTCM gamma , text "gamma' =" <+> prettyTCM gamma' , text "vs =" <+> brackets (fsep $ punctuate comma $ map prettyTCM vs) , text "us' =" <+> addContext gamma (brackets (fsep $ punctuate comma $ map prettyTCM us')) , text "ws =" <+> brackets (fsep $ punctuate comma $ map prettyTCM ws) ] ] -- Unify constructor target and given type (in Δ₁Γ) -- Given: Δ₁ ⊢ D vs : Φ → Setᵢ -- Δ₁ ⊢ c : Γ → D vs' us' -- Δ₁ ⊢ ws : Φ -- Δ₁Γ ⊢ ws' : Φ -- (where vs' = raise Γ vs and ws' = raise Γ ws) -- unification of us' and ws' in context Δ₁Γ gives us a telescope Δ₁' -- and a substitution ρ₀ such that -- Δ₁' ⊢ ρ₀ : Δ₁Γ -- Δ₁' ⊢ (us')ρ₀ ≡ (ws')ρ₀ : Φρ₀ -- We can split ρ₀ into two parts ρ₁ and ρ₂, giving -- Δ₁' ⊢ ρ₁ : Δ₁ -- Δ₁' ⊢ ρ₂ : Γρ₁ -- Application of the constructor c gives -- Δ₁' ⊢ c ρ₂ : (D vs' us')(ρ₁;ρ₂) -- We have -- us'(ρ₁;ρ₂) -- ≡ us'ρ₀ (since ρ₀=ρ₁;ρ₂) -- ≡ ws'ρ₀ (by unification) -- ≡ ws ρ₁ (since ws doesn't actually depend on Γ) -- so Δ₁' ⊢ c ρ₂ : D (vs)ρ₁ (ws)ρ₁ -- Putting this together with ρ₁ gives ρ₃ = ρ₁;c ρ₂ -- Δ₁' ⊢ ρ₁;c ρ₂ : Δ₁(x : D vs ws) -- and lifting over Δ₂ gives the final substitution ρ = ρ₃;Δ₂ -- from Δ' = Δ₁';Δ₂ρ₃ -- Δ' ⊢ ρ : Δ₁(x : D vs ws)Δ₂ res <- unifyIndices (delta1 `abstract` gamma) flex (raise (size gamma) da) us' ws' case res of -- Mismatch. Report and abort. NoUnify neg -> typeError $ ImpossibleConstructor (conName c) neg -- Unclear situation. Try next split. -- If no split works, give error from first split. -- This is conservative, but might not be the best behavior. -- It might be better to collect all the errors and print all of them. DontKnow errs -> tryNextSplit `catchError` \ _ -> typeError $ SplitError $ UnificationStuck (conName c) (delta1 `abstract` gamma) us' ws' errs -- Success. Unifies (delta1',rho0,es) -> do reportSDoc "tc.lhs.top" 15 $ text "unification successful" reportSDoc "tc.lhs.top" 20 $ nest 2 $ vcat [ text "delta1' =" <+> prettyTCM delta1' , text "rho0 =" <+> addContext delta1' (prettyTCM rho0) , text "es =" <+> addContext delta1' (prettyTCM $ (fmap . fmap . fmap) patternToTerm es) ] -- Andreas 2014-11-25 clear 'Forced' and 'Unused' -- Andreas 2015-01-19 ... only after unification delta1' <- return $ mapRelevance ignoreForced <$> delta1' -- compute in patterns for delta1' let newPats = applySubst rho0 $ teleArgs $ delta1 `abstract` gamma -- oldTypes are the types of the old pattern variables, but relative -- to the *new* telescope delta1'. These are needed to compute the -- correct types of new dot pattern instantiations. oldTypes = applyPatSubst rho0 $ flattenTel $ delta1 `abstract` gamma (p0',newDpi) <- addContext delta1' $ updateInPatterns oldTypes (problemInPat p0 ++ qs') newPats reportSDoc "tc.lhs.top" 20 $ addContext delta1' $ nest 2 $ vcat [ text "p0' =" <+> text (show $ deepUnscope p0') , text "newDpi =" <+> brackets (fsep $ punctuate comma $ map prettyTCM newDpi) ] -- split substitution into part for Δ₁ and part for Γ let (rho1,rho2) = splitS (size gamma) rho0 reportSDoc "tc.lhs.top" 20 $ addContext delta1' $ nest 2 $ vcat [ text "rho1 =" <+> prettyTCM rho1 , text "rho2 =" <+> prettyTCM rho2 ] -- Andreas, 2010-09-09, save the type. -- It is relative to Δ₁, but it should be relative to Δ₁' let storedPatternType = applyPatSubst rho1 typeOfSplitVar -- Also remember if we are a record pattern and from an implicit pattern. isRec <- isRecord d let cpi = ConPatternInfo (isRec $> porigin) (Just storedPatternType) -- compute final context and permutation let crho2 = ConP c cpi $ applySubst rho2 $ teleNamedArgs gamma `useOriginFrom` qs' rho3 = consS crho2 rho1 delta2' = applyPatSubst rho3 delta2 delta' = delta1' `abstract` delta2' rho = liftS (size delta2) rho3 reportSDoc "tc.lhs.top" 20 $ addContext delta1' $ nest 2 $ vcat [ text "crho2 =" <+> prettyTCM crho2 , text "rho3 =" <+> prettyTCM rho3 , text "delta2' =" <+> prettyTCM delta2' ] reportSDoc "tc.lhs.top" 70 $ addContext delta1' $ nest 2 $ vcat [ text "crho2 =" <+> pretty crho2 , text "rho3 =" <+> pretty rho3 , text "delta2' =" <+> pretty delta2' ] reportSDoc "tc.lhs.top" 15 $ nest 2 $ vcat [ text "delta' =" <+> prettyTCM delta' , text "rho =" <+> addContext delta' (prettyTCM rho) ] -- compute new in patterns let ps' = p0' ++ problemInPat (absBody p1) reportSDoc "tc.lhs.top" 15 $ addContext delta' $ nest 2 $ vcat [ text "ps' =" <+> brackets (fsep $ punctuate comma $ map prettyA ps') ] -- The final dpis are the new ones plus the old ones substituted by ρ let dpi' = applyPatSubst rho dpi ++ raise (size delta2') newDpi sbe' = map (second $ applyPatSubst rho) sbe reportSDoc "tc.lhs.top" 15 $ addContext delta' $ nest 2 $ vcat [ text "dpi' =" <+> brackets (fsep $ punctuate comma $ map prettyTCM dpi') , text "sbe' =" <+> brackets (fsep $ punctuate comma $ map prettyTCM sbe') ] -- Apply the substitution let ip' = applySubst rho ip rest' = applyPatSubst rho (problemRest problem) reportSDoc "tc.lhs.top" 15 $ addContext delta' $ nest 2 $ vcat [ text "ip' =" <+> pretty ip ] -- Construct the new problem let problem' = Problem ps' ip' delta' rest' -- if rest type reduces, -- extend the split problem by previously not considered patterns st'@(LHSState problem'@(Problem ps' ip' delta' rest') dpi' sbe') <- updateProblemRest $ LHSState problem' dpi' sbe' reportSDoc "tc.lhs.top" 12 $ sep [ text "new problem from rest" , nest 2 $ vcat [ text "ps' =" <+> fsep (map prettyA ps') , text "delta' =" <+> prettyTCM delta' , text "ip' =" <+> pretty ip' ] ] checkLHS f st' -- | Ensures that we are not performing pattern matching on codata. noPatternMatchingOnCodata :: [NamedArg DeBruijnPattern] -> TCM () noPatternMatchingOnCodata = mapM_ (check . namedArg) where check (VarP {}) = return () check (DotP {}) = return () check (AbsurdP{}) = return () check (ProjP{}) = return () check (LitP {}) = return () -- Literals are assumed not to be coinductive. check (ConP con _ ps) = do TelV _ t <- telView' . defType <$> do getConstInfo $ conName con c <- isCoinductive t case c of Nothing -> __IMPOSSIBLE__ Just False -> mapM_ (check . namedArg) ps Just True -> typeError $ GenericError "Pattern matching on coinductive types is not allowed" -- | Type check dot pattern stripped from a with function. checkStrippedDotPattern :: A.StrippedDotPattern -> TCM () checkStrippedDotPattern (A.StrippedDot e v a) = do reportSDoc "tc.with.dot" 30 $ vcat [ text "Checking stripped dot pattern" , nest 2 $ vcat [ text "e =" <+> prettyTCM e , text "v =" <+> prettyTCM v , text "a =" <+> prettyTCM a , text "Γ =" <+> (inTopContext . prettyTCM =<< getContextTelescope) ] ] u <- checkExpr e a equalTerm a u v Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Decl.hs0000644000000000000000000012321513154613124020006 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.Rules.Decl where import Prelude hiding (null) import Control.Monad import Control.Monad.Reader import Control.Monad.State (modify, gets, get) import Control.Monad.Writer (tell) import Data.Either (partitionEithers) import qualified Data.Foldable as Fold import qualified Data.List as List import Data.Maybe import Data.Map (Map) import qualified Data.Sequence as Seq import qualified Data.Set as Set import Data.Set (Set) import Agda.Interaction.Options import Agda.Interaction.Highlighting.Generate import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Views (deepUnscopeDecl, deepUnscopeDecls) import Agda.Syntax.Internal import qualified Agda.Syntax.Reflected as R import qualified Agda.Syntax.Info as Info import Agda.Syntax.Position import Agda.Syntax.Common import Agda.Syntax.Literal import Agda.Syntax.Translation.InternalToAbstract import Agda.Syntax.Translation.ReflectedToAbstract import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TypeChecking.Constraints import Agda.TypeChecking.Conversion import Agda.TypeChecking.Errors import Agda.TypeChecking.Injectivity import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.Positivity import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Polarity import Agda.TypeChecking.Pretty import Agda.TypeChecking.Primitive import Agda.TypeChecking.ProjectionLike import Agda.TypeChecking.Quote import Agda.TypeChecking.Unquote import Agda.TypeChecking.Records import Agda.TypeChecking.RecordPatterns import Agda.TypeChecking.Reduce import Agda.TypeChecking.Rewriting import Agda.TypeChecking.SizedTypes.Solve import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Unquote import Agda.TypeChecking.Warnings import Agda.TypeChecking.Rules.Term import Agda.TypeChecking.Rules.Data ( checkDataDef ) import Agda.TypeChecking.Rules.Record ( checkRecDef ) import Agda.TypeChecking.Rules.Def ( checkFunDef, newSection, useTerPragma ) import Agda.TypeChecking.Rules.Builtin import Agda.TypeChecking.Rules.Display ( checkDisplayPragma ) import Agda.Termination.TermCheck import Agda.Utils.Except import Agda.Utils.Functor import Agda.Utils.Function import Agda.Utils.Lens import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Pretty (prettyShow) import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | Cached checkDecl checkDeclCached :: A.Declaration -> TCM () checkDeclCached d@A.ScopedDecl{} = checkDecl d checkDeclCached d@(A.Section minfo mname tbinds _) = do e <- readFromCachedLog reportSLn "cache.decl" 10 $ "checkDeclCached: " ++ show (isJust e) case e of Just (EnterSection minfo' mname' tbinds', _) | killRange minfo == killRange minfo' && mname == mname' && tbinds == tbinds' -> do return () _ -> do cleanCachedLog writeToCurrentLog $ EnterSection minfo mname tbinds checkDecl d e' <- readFromCachedLog case e' of Just (LeaveSection mname', _) | mname == mname' -> do return () _ -> do cleanCachedLog writeToCurrentLog $ LeaveSection mname checkDeclCached d = do e <- readFromCachedLog reportSLn "cache.decl" 10 $ "checkDeclCached: " ++ show (isJust e) case e of (Just (Decl d',s)) | compareDecl d d' -> do restorePostScopeState s reportSLn "cache.decl" 50 $ "range: " ++ show (getRange d) printSyntaxInfo (getRange d) _ -> do cleanCachedLog checkDeclWrap d writeToCurrentLog $ Decl d where compareDecl A.Section{} A.Section{} = __IMPOSSIBLE__ compareDecl A.ScopedDecl{} A.ScopedDecl{} = __IMPOSSIBLE__ compareDecl x y = x == y -- changes to CS inside a RecDef or Mutual ought not happen, -- but they do happen, so we discard them. ignoreChanges m = do cs <- gets $ stLoadedFileCache . stPersistentState cleanCachedLog _ <- m modifyPersistentState $ \st -> st{stLoadedFileCache = cs} checkDeclWrap d@A.RecDef{} = ignoreChanges $ checkDecl d checkDeclWrap d@A.Mutual{} = ignoreChanges $ checkDecl d checkDeclWrap d = checkDecl d -- | Type check a sequence of declarations. checkDecls :: [A.Declaration] -> TCM () checkDecls ds = do reportSLn "tc.decl" 45 $ "Checking " ++ show (length ds) ++ " declarations..." mapM_ checkDecl ds -- Andreas, 2011-05-30, unfreezing moved to Interaction/Imports -- whenM onTopLevel unfreezeMetas -- | Type check a single declaration. checkDecl :: A.Declaration -> TCM () checkDecl d = setCurrentRange d $ do reportSDoc "tc.decl" 10 $ text "checking declaration" debugPrintDecl d reportSDoc "tc.decl" 90 $ (text . show) (deepUnscopeDecl d) reportSDoc "tc.decl" 10 $ prettyA d -- Might loop, see e.g. Issue 1597 -- Issue 418 fix: freeze metas before checking an abstract thing -- when_ isAbstract freezeMetas -- WAS IN PLACE 2012-2016, but too crude -- applyWhen isAbstract withFreezeMetas $ do -- WRONG let -- What kind of final checks/computations should be performed -- if we're not inside a mutual block? none m = m $> Nothing -- skip all checks meta m = m $> Just (return ()) -- do the usual checks mutual i ds m = m <&> Just . uncurry (mutualChecks i d ds) impossible m = m $> __IMPOSSIBLE__ -- We're definitely inside a mutual block. finalChecks <- case d of A.Axiom{} -> meta $ checkTypeSignature d A.Field{} -> typeError FieldOutsideRecord A.Primitive i x e -> meta $ checkPrimitive i x e A.Mutual i ds -> mutual i ds $ checkMutual i ds A.Section i x tel ds -> meta $ checkSection i x tel ds A.Apply i x modapp ci _adir -> meta $ checkSectionApplication i x modapp ci A.Import i x _adir -> none $ checkImport i x A.Pragma i p -> none $ checkPragma i p A.ScopedDecl scope ds -> none $ setScope scope >> mapM_ checkDeclCached ds A.FunDef i x delayed cs -> impossible $ check x i $ checkFunDef delayed i x cs A.DataDef i x ps cs -> impossible $ check x i $ checkDataDef i x ps cs A.RecDef i x ind eta c ps tel cs -> mutual empty [d] $ check x i $ do checkRecDef i x ind eta c ps tel cs blockId <- mutualBlockOf x -- Andreas, 2016-10-01 testing whether -- envMutualBlock is set correctly. -- Apparently not. verboseS "tc.decl.mutual" 70 $ do current <- asks envMutualBlock unless (Just blockId == current) $ do reportSLn "" 0 $ unlines [ "mutual block id discrepancy for " ++ prettyShow x , " current mut. bl. = " ++ show current , " calculated mut. bl. = " ++ show blockId ] return (blockId, Set.singleton x) A.DataSig i x ps t -> impossible $ checkSig i x ps t A.RecSig i x ps t -> none $ checkSig i x ps t -- A record signature is always followed by a -- record definition. Metas should not be -- frozen until after the definition has been -- checked. NOTE: Metas are not frozen -- immediately after the last field. Perhaps -- they should be (unless we're in a mutual -- block). A.Open{} -> none $ return () A.PatternSynDef{} -> none $ return () -- Open and PatternSynDef are just artifacts -- from the concrete syntax, retained for -- highlighting purposes. A.UnquoteDecl mi i x e -> checkUnquoteDecl mi i x e A.UnquoteDef i x e -> impossible $ checkUnquoteDef i x e whenNothingM (asks envMutualBlock) $ do -- Syntax highlighting. highlight_ d -- Post-typing checks. whenJust finalChecks $ \ theMutualChecks -> do reportSLn "tc.decl" 20 $ "Attempting to solve constraints before freezing." wakeupConstraints_ -- solve emptiness and instance constraints checkingWhere <- asks envCheckingWhere solveSizeConstraints $ if checkingWhere then DontDefaultToInfty else DefaultToInfty wakeupConstraints_ -- Size solver might have unblocked some constraints reportSLn "tc.decl" 20 $ "Freezing all metas." _ <- freezeMetas theMutualChecks where -- check record or data type signature checkSig i x ps t = checkTypeSignature $ A.Axiom A.NoFunSig i defaultArgInfo Nothing x (A.Pi (Info.ExprRange (fuseRange ps t)) ps t) check x i m = Bench.billTo [Bench.Definition x] $ do reportSDoc "tc.decl" 5 $ text "Checking" <+> prettyTCM x <> text "." reportSLn "tc.decl.abstract" 25 $ show (Info.defAbstract i) r <- abstract (Info.defAbstract i) m reportSDoc "tc.decl" 5 $ text "Checked" <+> prettyTCM x <> text "." return r isAbstract = fmap Info.defAbstract (A.getDefInfo d) == Just AbstractDef -- Concrete definitions cannot use information about abstract things. abstract ConcreteDef = inConcreteMode abstract AbstractDef = inAbstractMode -- Some checks that should be run at the end of a mutual -- block (or non-mutual record declaration). The set names -- contains the names defined in the mutual block. mutualChecks :: Info.MutualInfo -> A.Declaration -> [A.Declaration] -> MutualId -> Set QName -> TCM () mutualChecks mi d ds mid names = do -- Andreas, 2014-04-11: instantiate metas in definition types let nameList = Set.toList names mapM_ instantiateDefinitionType nameList -- Andreas, 2017-03-23: check positivity before termination. -- This allows us to reuse the information about SCCs -- to skip termination of non-recursive functions. modifyAllowedReductions (List.delete UnconfirmedReductions) $ checkPositivity_ mi names -- Andreas, 2013-02-27: check termination before injectivity, -- to avoid making the injectivity checker loop. local (\ e -> e { envMutualBlock = Just mid }) $ checkTermination_ d revisitRecordPatternTranslation nameList -- Andreas, 2016-11-19 issue #2308 -- Andreas, 2015-03-26 Issue 1470: -- Restricting coinduction to recursive does not solve the -- actual problem, and prevents interesting sound applications -- of sized types. -- checkCoinductiveRecords ds -- Andreas, 2012-09-11: Injectivity check stores clauses -- whose 'Relevance' is affected by polarity computation, -- so do it here (again). -- Andreas, 2015-07-01: In particular, 'UnusedArg's of local functions -- are only recognized after the polarity computation. -- See Issue 1366 for an example where injectivity of a local function -- is used to solve metas. It fails if we do injectivity analysis -- before polarity only. checkInjectivity_ names checkProjectionLikeness_ names -- | Check if there is a inferred eta record type in the mutual block. -- If yes, repeat the record pattern translation for all function definitions -- in the block. -- This is necessary since the original record pattern translation will -- have skipped record patterns of the new record types (as eta was off for them). -- See issue #2308 (and #2197). revisitRecordPatternTranslation :: [QName] -> TCM () revisitRecordPatternTranslation qs = do -- rs: inferred eta record types of this mutual block -- qccs: compiled clauses of definitions (rs, qccs) <- partitionEithers . catMaybes <$> mapM classify qs unless (null rs) $ forM_ qccs $ \(q,cc) -> do cc <- translateCompiledClauses cc modifySignature $ updateDefinition q $ updateTheDef $ updateCompiledClauses $ const $ Just cc where -- Walk through the definitions and return the set of inferred eta record types -- and the set of function definitions in the mutual block classify q = inConcreteOrAbstractMode q $ \ def -> do case theDef def of Record{ recEtaEquality' = Inferred True } -> return $ Just $ Left q Function { funProjection = Nothing -- Andreas, 2017-08-10, issue #2664: -- Do not record pattern translate record projection definitions! , funCompiled = Just cc } -> return $ Just $ Right (q, cc) _ -> return Nothing type FinalChecks = Maybe (TCM ()) checkUnquoteDecl :: Info.MutualInfo -> [Info.DefInfo] -> [QName] -> A.Expr -> TCM FinalChecks checkUnquoteDecl mi is xs e = do reportSDoc "tc.unquote.decl" 20 $ text "Checking unquoteDecl" <+> sep (map prettyTCM xs) Nothing <$ unquoteTop xs e checkUnquoteDef :: [Info.DefInfo] -> [QName] -> A.Expr -> TCM () checkUnquoteDef _ xs e = do reportSDoc "tc.unquote.decl" 20 $ text "Checking unquoteDef" <+> sep (map prettyTCM xs) () <$ unquoteTop xs e -- | Run a reflected TCM computatation expected to define a given list of -- names. unquoteTop :: [QName] -> A.Expr -> TCM [QName] unquoteTop xs e = do tcm <- primAgdaTCM unit <- primUnit lzero <- primLevelZero let vArg = defaultArg hArg = setHiding Hidden . vArg m <- checkExpr e $ El (mkType 0) $ apply tcm [hArg lzero, vArg unit] res <- runUnquoteM $ tell xs >> evalTCM m case res of Left err -> typeError $ UnquoteFailed err Right (_, xs) -> return xs -- | Instantiate all metas in 'Definition' associated to 'QName'. -- Makes sense after freezing metas. Some checks, like free variable -- analysis, are not in 'TCM', so they will be more precise (see issue 1099) -- after meta instantiation. -- Precondition: name has been added to signature already. instantiateDefinitionType :: QName -> TCM () instantiateDefinitionType q = do reportSLn "tc.decl.inst" 20 $ "instantiating type of " ++ prettyShow q t <- defType . fromMaybe __IMPOSSIBLE__ . lookupDefinition q <$> getSignature t' <- instantiateFull t modifySignature $ updateDefinition q $ updateDefType $ const t' reportSDoc "tc.decl.inst" 30 $ vcat [ text " t = " <+> prettyTCM t , text " t' = " <+> prettyTCM t' ] -- Andreas, 2014-04-11 -- UNUSED, costs a couple of sec on the std-lib -- -- | Instantiate all metas in 'Definition' associated to 'QName'. -- -- Makes sense after freezing metas. -- -- Some checks, like free variable analysis, are not in 'TCM', -- -- so they will be more precise (see issue 1099) after meta instantiation. -- -- -- -- Precondition: name has been added to signature already. -- instantiateDefinition :: QName -> TCM () -- instantiateDefinition q = do -- reportSLn "tc.decl.inst" 20 $ "instantiating " ++ prettyShow q -- sig <- getSignature -- let def = fromMaybe __IMPOSSIBLE__ $ lookupDefinition q sig -- def <- instantiateFull def -- modifySignature $ updateDefinition q $ const def -- | Highlight a declaration. highlight_ :: A.Declaration -> TCM () highlight_ d = do let highlight d = generateAndPrintSyntaxInfo d Full True Bench.billTo [Bench.Highlighting] $ case d of A.Axiom{} -> highlight d A.Field{} -> __IMPOSSIBLE__ A.Primitive{} -> highlight d A.Mutual i ds -> mapM_ highlight_ $ deepUnscopeDecls ds A.Apply{} -> highlight d A.Import{} -> highlight d A.Pragma{} -> highlight d A.ScopedDecl{} -> return () A.FunDef{} -> highlight d A.DataDef{} -> highlight d A.DataSig{} -> highlight d A.Open{} -> highlight d A.PatternSynDef{} -> highlight d A.UnquoteDecl{} -> highlight d A.UnquoteDef{} -> highlight d A.Section i x tel _ -> highlight (A.Section i x tel []) -- Each block in the section has already been highlighted, -- all that remains is the module declaration. A.RecSig{} -> highlight d A.RecDef i x ind eta c ps tel cs -> highlight (A.RecDef i x ind eta c [] dummy (fields cs)) -- The telescope and all record module declarations except -- for the fields have already been highlighted. where fields (A.ScopedDecl _ ds1 : ds2) = fields ds1 ++ fields ds2 fields (d@A.Field{} : ds) = d : fields ds fields (_ : ds) = fields ds fields [] = [] -- Andreas, 2016-01-22, issue 1791 -- The expression denoting the record constructor type -- is replace by a dummy expression in order to /not/ -- generate highlighting from it. -- Simply because all the highlighting info is wrong -- in the record constructor type: -- * fields become bound variables, -- * declarations become let-bound variables. -- We do not need that crap. dummy = A.Lit $ LitString noRange $ "do not highlight construct(ed/or) type" -- | Termination check a declaration. checkTermination_ :: A.Declaration -> TCM () checkTermination_ d = Bench.billTo [Bench.Termination] $ do reportSLn "tc.decl" 20 $ "checkDecl: checking termination..." case d of -- Record module definitions should not be termination-checked twice. A.RecDef {} -> return () _ -> disableDestructiveUpdate $ do termErrs <- termDecl d -- If there are some termination errors, we collect them in -- the state. -- The termination checker already marked non-terminating functions as such. unless (null termErrs) $ do warning $ TerminationIssue termErrs -- | Check a set of mutual names for positivity. checkPositivity_ :: Info.MutualInfo -> Set QName -> TCM () checkPositivity_ mi names = Bench.billTo [Bench.Positivity] $ do -- Positivity checking. reportSLn "tc.decl" 20 $ "checkDecl: checking positivity..." checkStrictlyPositive mi names -- Andreas, 2012-02-13: Polarity computation uses info from -- positivity check, so it needs happen after positivity -- check. computePolarity $ Set.toList names -- | Check that all coinductive records are actually recursive. -- (Otherwise, one can implement invalid recursion schemes just like -- for the old coinduction.) checkCoinductiveRecords :: [A.Declaration] -> TCM () checkCoinductiveRecords ds = forM_ ds $ \case A.RecDef _ q (Just (Ranged r CoInductive)) _ _ _ _ _ -> setCurrentRange r $ do unlessM (isRecursiveRecord q) $ typeError $ GenericError $ "Only recursive records can be coinductive" _ -> return () -- | Check a set of mutual names for constructor-headedness. checkInjectivity_ :: Set QName -> TCM () checkInjectivity_ names = Bench.billTo [Bench.Injectivity] $ do reportSLn "tc.decl" 20 $ "checkDecl: checking injectivity..." -- Andreas, 2015-07-01, see Issue1366b: -- Injectivity check needs also to be run for abstract definitions. -- Fold.forM_ names $ \ q -> ignoreAbstractMode $ do -- NOT NECESSARY after all Fold.forM_ names $ \ q -> inConcreteOrAbstractMode q $ \ def -> do -- For abstract q, we should be inAbstractMode, -- otherwise getConstInfo returns Axiom. -- -- Andreas, 2015-07-01: -- Quite surprisingly, inAbstractMode does not allow us to look -- at a local definition (@where@ block) of an abstract definition. -- This is because the local definition is defined in a strict submodule. -- We can only see through abstract definitions in the current module -- or super modules inAbstractMode. -- I changed that in Monad.Signature.treatAbstractly', so we can see -- our own local definitions. case theDef def of d@Function{ funClauses = cs, funTerminates = term } -> do case term of Just True -> do inv <- checkInjectivity q cs modifySignature $ updateDefinition q $ updateTheDef $ const $ d { funInv = inv } _ -> reportSLn "tc.inj.check" 20 $ prettyShow q ++ " is not verified as terminating, thus, not considered for injectivity" _ -> do abstr <- asks envAbstractMode reportSLn "tc.inj.check" 20 $ "we are in " ++ show abstr ++ " and " ++ prettyShow q ++ " is abstract or not a function, thus, not considered for injectivity" -- | Check a set of mutual names for projection likeness. -- -- Only a single, non-abstract function can be projection-like. -- Making an abstract function projection-like would break the -- invariant that the type of the principle argument of a projection-like -- function is always inferable. checkProjectionLikeness_ :: Set QName -> TCM () checkProjectionLikeness_ names = Bench.billTo [Bench.ProjectionLikeness] $ do -- Non-mutual definitions can be considered for -- projection likeness let ds = Set.toList names reportSLn "tc.proj.like" 20 $ "checkDecl: checking projection-likeness of " ++ prettyShow ds case ds of [d] -> do def <- getConstInfo d -- For abstract identifiers, getConstInfo returns Axiom. -- Thus, abstract definitions are not considered for projection-likeness. case theDef def of Function{} -> makeProjection (defName def) _ -> reportSLn "tc.proj.like" 25 $ prettyShow d ++ " is abstract or not a function, thus, not considered for projection-likeness" _ -> reportSLn "tc.proj.like" 25 $ "mutual definitions are not considered for projection-likeness" -- | Freeze metas created by given computation if in abstract mode. whenAbstractFreezeMetasAfter :: Info.DefInfo -> TCM a -> TCM a whenAbstractFreezeMetasAfter Info.DefInfo{ defAccess, defAbstract} m = do let pubAbs = defAccess == PublicAccess && defAbstract == AbstractDef if not pubAbs then m else do (a, ms) <- metasCreatedBy m xs <- freezeMetas' $ (`Set.member` ms) reportSDoc "tc.decl.ax" 20 $ vcat [ text "Abstract type signature produced new metas: " <+> sep (map prettyTCM $ Set.toList ms) , text "We froze the following ones of these: " <+> sep (map prettyTCM xs) ] return a -- | Type check an axiom. checkAxiom :: A.Axiom -> Info.DefInfo -> ArgInfo -> Maybe [Occurrence] -> QName -> A.Expr -> TCM () checkAxiom funSig i info0 mp x e = whenAbstractFreezeMetasAfter i $ do -- Andreas, 2016-07-19 issues #418 #2102: -- We freeze metas in type signatures of abstract definitions, to prevent -- leakage of implementation details. -- Andreas, 2012-04-18 if we are in irrelevant context, axioms is irrelevant -- even if not declared as such (Issue 610). rel <- max (getRelevance info0) <$> asks envRelevance let info = setRelevance rel info0 -- rel <- ifM ((Irrelevant ==) <$> asks envRelevance) (return Irrelevant) (return rel0) t <- workOnTypes $ isType_ e reportSDoc "tc.decl.ax" 10 $ sep [ text $ "checked type signature" , nest 2 $ prettyTCM rel <> prettyTCM x <+> text ":" <+> prettyTCM t , nest 2 $ text "of sort " <+> prettyTCM (getSort t) ] -- Andreas, 2015-03-17 Issue 1428: Do not postulate sizes in parametrized -- modules! when (funSig == A.NoFunSig) $ do whenM ((== SizeUniv) <$> do reduce $ getSort t) $ do whenM ((> 0) <$> getContextSize) $ do typeError $ GenericError $ "We don't like postulated sizes in parametrized modules." -- Ensure that polarity pragmas do not contain too many occurrences. (occs, pols) <- case mp of Nothing -> return ([], []) Just occs -> do TelV tel _ <- telView t let n = length (telToList tel) when (n < length occs) $ typeError $ TooManyPolarities x n let pols = map polFromOcc occs reportSLn "tc.polarity.pragma" 10 $ "Setting occurrences and polarity for " ++ prettyShow x ++ ":\n " ++ prettyShow occs ++ "\n " ++ prettyShow pols return (occs, pols) -- Not safe. See Issue 330 -- t <- addForcingAnnotations t addConstant x =<< do useTerPragma $ (defaultDefn info x t $ case funSig of A.FunSig -> set funMacro (Info.defMacro i == MacroDef) emptyFunction A.NoFunSig -> Axiom) -- NB: used also for data and record type sigs { defArgOccurrences = occs , defPolarity = pols } -- Add the definition to the instance table, if needed when (Info.defInstance i == InstanceDef) $ do addTypedInstance x t traceCall (IsType_ e) $ do -- need Range for error message -- Andreas, 2016-06-21, issue #2054 -- Do not default size metas to ∞ in local type signatures checkingWhere <- asks envCheckingWhere solveSizeConstraints $ if checkingWhere then DontDefaultToInfty else DefaultToInfty -- Andreas, 2011-05-31, that freezing below is probably wrong: -- when_ (Info.defAbstract i == AbstractDef) $ freezeMetas -- | Type check a primitive function declaration. checkPrimitive :: Info.DefInfo -> QName -> A.Expr -> TCM () checkPrimitive i x e = traceCall (CheckPrimitive (getRange i) (qnameName x) e) $ do -- TODO!! (qnameName) (name, PrimImpl t' pf) <- lookupPrimitiveFunctionQ x -- Primitive functions on nats are BUILTIN not 'primitive' let builtinPrimitives = [ "primNatPlus", "primNatMinus" , "primNatTimes" , "primNatDivSucAux", "primNatModSucAux" , "primNatEquality", "primNatLess" ] when (elem name builtinPrimitives) $ typeError $ NoSuchPrimitiveFunction name t <- isType_ e noConstraints $ equalType t t' let s = prettyShow $ qnameName x bindPrimitive s pf addConstant x $ defaultDefn defaultArgInfo x t $ Primitive (Info.defAbstract i) s [] Nothing assertCurrentModule :: QName -> String -> TCM () assertCurrentModule x err = do def <- getConstInfo x m <- currentModule let m' = qnameModule $ defName def unless (m == m' || isSubModuleOf m' m) $ typeError $ GenericError err -- | Check a pragma. checkPragma :: Range -> A.Pragma -> TCM () checkPragma r p = traceCall (CheckPragma r p) $ case p of A.BuiltinPragma x e -> bindBuiltin x e A.BuiltinNoDefPragma b x -> bindBuiltinNoDef b x A.RewritePragma q -> addRewriteRule q A.CompiledTypePragma x hs -> do def <- getConstInfo x case theDef def of Axiom{} -> addHaskellType x hs _ -> typeError $ GenericError "COMPILED_TYPE directive only works on postulates" A.CompiledDataPragma x hs hcs -> do def <- getConstInfo x -- Check that the pragma appears in the same module -- as the datatype. assertCurrentModule x $ "COMPILED_DATA directives must appear in the same module " ++ "as their corresponding datatype definition," case theDef def of Datatype{dataCons = cs} -> addHaskellData x hs hcs Record{recConHead = ch} -> addHaskellData x hs hcs _ -> typeError $ GenericError "COMPILED_DATA on non datatype" A.CompilePragma b x s -> do assertCurrentModule x $ "COMPILE pragmas must appear in the same module " ++ "as their corresponding definitions," addPragma b x s A.CompiledPragma x hs -> do def <- getConstInfo x let addCompiled = addHaskellCode x hs case theDef def of Axiom{} -> addCompiled Function{} -> addCompiled _ -> typeError $ GenericError "COMPILED directive only works on postulates and functions" A.CompiledExportPragma x hs -> do def <- getConstInfo x let correct = case theDef def of Function{} -> True Constructor{} -> False _ -> False if not correct then typeError $ GenericError "COMPILED_EXPORT directive only works on functions" else addHaskellExport x hs A.CompiledJSPragma x ep -> addJSCode x ep A.CompiledUHCPragma x cr -> do def <- getConstInfo x case theDef def of Axiom{} -> addCoreCode x cr _ -> typeError $ GenericError "COMPILED_UHC directive only works on postulates" -- only allow postulates for the time being A.CompiledDataUHCPragma x crd crcs -> do -- TODO mostly copy-paste from the CompiledDataPragma, should be refactored into a seperate function def <- getConstInfo x -- Check that the pragma appears in the same module -- as the datatype. m <- currentModule let m' = qnameModule $ defName def unless (m == m') $ typeError $ GenericError $ "COMPILED_DATA_UHC directives must appear in the same module " ++ "as their corresponding datatype definition," case theDef def of Datatype{dataCons = cs} -> addCoreType x crd crcs _ -> typeError $ GenericError "COMPILED_DATA_UHC on non datatype" A.StaticPragma x -> do def <- getConstInfo x case theDef def of Function{} -> markStatic x _ -> typeError $ GenericError "STATIC directive only works on functions" A.InjectivePragma x -> markInjective x A.InlinePragma x -> do def <- getConstInfo x case theDef def of Function{} -> markInline x _ -> typeError $ GenericError "INLINE directive only works on functions" A.OptionsPragma{} -> typeError $ GenericError $ "OPTIONS pragma only allowed at beginning of file, before top module declaration" A.DisplayPragma f ps e -> checkDisplayPragma f ps e A.EtaPragma r -> do let noRecord = typeError $ GenericError $ "ETA pragma is only applicable to coinductive records" caseMaybeM (isRecord r) noRecord $ \case Record{ recInduction = ind, recEtaEquality' = eta } -> do unless (ind == Just CoInductive) $ noRecord when (eta == Specified False) $ typeError $ GenericError $ "ETA pragram conflicts with no-eta-equality declaration" _ -> __IMPOSSIBLE__ modifySignature $ updateDefinition r $ updateTheDef $ \case def@Record{} -> def { recEtaEquality' = Specified True } _ -> __IMPOSSIBLE__ -- | Type check a bunch of mutual inductive recursive definitions. -- -- All definitions which have so far been assigned to the given mutual -- block are returned. checkMutual :: Info.MutualInfo -> [A.Declaration] -> TCM (MutualId, Set QName) checkMutual i ds = inMutualBlock $ \ blockId -> do verboseS "tc.decl.mutual" 20 $ do reportSDoc "tc.decl.mutual" 20 $ vcat $ (text "Checking mutual block" <+> text (show blockId) <> text ":") : map (nest 2 . prettyA) ds insertMutualBlockInfo blockId i local (\e -> e { envTerminationCheck = () <$ Info.mutualTermCheck i }) $ mapM_ checkDecl ds (blockId, ) . mutualNames <$> lookupMutualBlock blockId -- | Type check the type signature of an inductive or recursive definition. checkTypeSignature :: A.TypeSignature -> TCM () checkTypeSignature (A.ScopedDecl scope ds) = do setScope scope mapM_ checkTypeSignature ds checkTypeSignature (A.Axiom funSig i info mp x e) = Bench.billTo [Bench.Definition x] $ Bench.billTo [Bench.Typing, Bench.TypeSig] $ let abstr = case Info.defAccess i of PrivateAccess{} | Info.defAbstract i == AbstractDef -> inAbstractMode -- Issue #2321, only go to AbstractMode for abstract definitions | otherwise -> inConcreteMode PublicAccess -> inConcreteMode OnlyQualified -> __IMPOSSIBLE__ in abstr $ checkAxiom funSig i info mp x e checkTypeSignature _ = __IMPOSSIBLE__ -- type signatures are always axioms -- | Type check a module. checkSection :: Info.ModuleInfo -> ModuleName -> A.Telescope -> [A.Declaration] -> TCM () checkSection _ x tel ds = newSection x tel $ mapM_ checkDeclCached ds -- | Helper for 'checkSectionApplication'. -- -- Matches the arguments of the module application with the -- module parameters. -- -- Returns the remaining module parameters as an open telescope. -- Warning: the returned telescope is /not/ the final result, -- an actual instantiation of the parameters does not occur. checkModuleArity :: ModuleName -- ^ Name of applied module. -> Telescope -- ^ The module parameters. -> [NamedArg A.Expr] -- ^ The arguments this module is applied to. -> TCM Telescope -- ^ The remaining module parameters (has free de Bruijn indices!). checkModuleArity m tel args = check tel args where bad = typeError $ ModuleArityMismatch m tel args check tel [] = return tel check EmptyTel (_:_) = bad check (ExtendTel (Dom info _) btel) args0@(Arg info' (Named rname _) : args) = let name = fmap rangedThing rname y = absName btel tel = absBody btel in case (argInfoHiding info, argInfoHiding info', name) of (Instance{}, NotHidden, _) -> check tel args0 (Instance{}, Hidden, _) -> check tel args0 (Instance{}, Instance{}, Nothing) -> check tel args (Instance{}, Instance{}, Just x) | x == y -> check tel args | otherwise -> check tel args0 (Hidden, NotHidden, _) -> check tel args0 (Hidden, Instance{}, _) -> check tel args0 (Hidden, Hidden, Nothing) -> check tel args (Hidden, Hidden, Just x) | x == y -> check tel args | otherwise -> check tel args0 (NotHidden, NotHidden, _) -> check tel args (NotHidden, Hidden, _) -> bad (NotHidden, Instance{}, _) -> bad -- | Check an application of a section (top-level function, includes @'traceCall'@). checkSectionApplication :: Info.ModuleInfo -> ModuleName -- ^ Name @m1@ of module defined by the module macro. -> A.ModuleApplication -- ^ The module macro @λ tel → m2 args@. -> A.ScopeCopyInfo -- ^ Imported names and modules -> TCM () checkSectionApplication i m1 modapp copyInfo = traceCall (CheckSectionApplication (getRange i) m1 modapp) $ checkSectionApplication' i m1 modapp copyInfo -- | Check an application of a section. checkSectionApplication' :: Info.ModuleInfo -> ModuleName -- ^ Name @m1@ of module defined by the module macro. -> A.ModuleApplication -- ^ The module macro @λ tel → m2 args@. -> A.ScopeCopyInfo -- ^ Imported names and modules -> TCM () checkSectionApplication' i m1 (A.SectionApp ptel m2 args) copyInfo = do -- Module applications can appear in lets, in which case we treat -- lambda-bound variables as additional parameters to the module. extraParams <- do mfv <- getCurrentModuleFreeVars fv <- getContextSize return (fv - mfv) when (extraParams > 0) $ reportSLn "tc.mod.apply" 30 $ "Extra parameters to " ++ prettyShow m1 ++ ": " ++ show extraParams -- Type-check the LHS (ptel) of the module macro. checkTelescope ptel $ \ ptel -> do -- We are now in the context @ptel@. -- Get the correct parameter telescope of @m2@. This is the fully lifted -- telescope obtained by `lookupSection` instantiated with the module -- parameters of `m2` currently in scope. For instance -- ``` -- module _ (A : Set) where -- module M (B : Set) where ... -- module M' = M B -- ``` -- In the application `M' = M B`, `tel = (A B : Set)` and -- `moduleParamsToApply M = [A]`, so the resulting parameter telescope is -- `tel' = (B : Set)`. tel <- lookupSection m2 vs <- moduleParamsToApply m2 let tel' = apply tel vs -- Compute the remaining parameter telescope after stripping of -- the initial parameters that are determined by the @args@. -- Warning: @etaTel@ is not well-formed in @ptel@, since -- the actual application has not happened. etaTel <- checkModuleArity m2 tel' args -- Take the module parameters that will be instantiated by @args@. let tel'' = telFromList $ take (size tel' - size etaTel) $ telToList tel' reportSDoc "tc.mod.apply" 15 $ vcat [ text "applying section" <+> prettyTCM m2 , nest 2 $ text "args =" <+> sep (map prettyA args) , nest 2 $ text "ptel =" <+> escapeContext (size ptel) (prettyTCM ptel) , nest 2 $ text "tel =" <+> prettyTCM tel , nest 2 $ text "tel' =" <+> prettyTCM tel' , nest 2 $ text "tel''=" <+> prettyTCM tel'' , nest 2 $ text "eta =" <+> escapeContext (size ptel) (addContext tel'' $ prettyTCM etaTel) ] -- Now, type check arguments. ts <- (noConstraints $ checkArguments_ DontExpandLast (getRange i) args tel') >>= \case (ts, etaTel') | (size etaTel == size etaTel') -> return ts _ -> __IMPOSSIBLE__ -- Perform the application of the module parameters. let aTel = tel' `apply` ts reportSDoc "tc.mod.apply" 15 $ vcat [ nest 2 $ text "aTel =" <+> prettyTCM aTel ] -- Andreas, 2014-04-06, Issue 1094: -- Add the section with well-formed telescope. addContext (KeepNames aTel) $ do reportSDoc "tc.mod.apply" 80 $ text "addSection" <+> prettyTCM m1 <+> (getContextTelescope >>= \ tel -> inTopContext (prettyTCM tel)) addSection m1 reportSDoc "tc.mod.apply" 20 $ vcat [ sep [ text "applySection", prettyTCM m1, text "=", prettyTCM m2, fsep $ map prettyTCM (vs ++ ts) ] , nest 2 $ pretty copyInfo ] args <- instantiateFull $ vs ++ ts let n = size aTel etaArgs <- inTopContext $ addContext aTel getContextArgs addContext' (KeepNames aTel) $ applySection m1 (ptel `abstract` aTel) m2 (raise n args ++ etaArgs) copyInfo checkSectionApplication' i m1 (A.RecordModuleIFS x) copyInfo = do let name = mnameToQName x tel' <- lookupSection x vs <- moduleParamsToApply x let tel = tel' `apply` vs args = teleArgs tel telInst :: Telescope telInst = instFinal tel -- Locate last (rightmost) parameter and make it @Instance@. instFinal :: Telescope -> Telescope -- Telescopes do not have @NoAbs@. instFinal (ExtendTel _ NoAbs{}) = __IMPOSSIBLE__ -- Found last parameter: switch it to @Instance@. instFinal (ExtendTel (Dom info t) (Abs n EmptyTel)) = ExtendTel (Dom ifo' t) (Abs n EmptyTel) where ifo' = makeInstance info -- Otherwise, keep searching for last parameter: instFinal (ExtendTel arg (Abs n tel)) = ExtendTel arg (Abs n (instFinal tel)) -- Before instFinal is invoked, we have checked that the @tel@ is not empty. instFinal EmptyTel = __IMPOSSIBLE__ reportSDoc "tc.mod.apply" 20 $ vcat [ sep [ text "applySection", prettyTCM name, text "{{...}}" ] , nest 2 $ text "x =" <+> prettyTCM x , nest 2 $ text "name =" <+> prettyTCM name , nest 2 $ text "tel =" <+> prettyTCM tel , nest 2 $ text "telInst =" <+> prettyTCM telInst , nest 2 $ text "vs =" <+> sep (map prettyTCM vs) -- , nest 2 $ text "args =" <+> sep (map prettyTCM args) ] reportSDoc "tc.mod.apply" 60 $ vcat [ nest 2 $ text "vs =" <+> text (show vs) -- , nest 2 $ text "args =" <+> text (show args) ] when (tel == EmptyTel) $ typeError $ GenericError $ prettyShow (qnameToConcrete name) ++ " is not a parameterised section" addContext' telInst $ do vs <- moduleParamsToApply $ qnameModule name reportSDoc "tc.mod.apply" 20 $ vcat [ nest 2 $ text "vs =" <+> sep (map prettyTCM vs) , nest 2 $ text "args =" <+> sep (map (parens . prettyTCM) args) ] reportSDoc "tc.mod.apply" 60 $ vcat [ nest 2 $ text "vs =" <+> text (show vs) , nest 2 $ text "args =" <+> text (show args) ] addSection m1 applySection m1 telInst x (vs ++ args) copyInfo -- | Type check an import declaration. Actually doesn't do anything, since all -- the work is done when scope checking. checkImport :: Info.ModuleInfo -> ModuleName -> TCM () checkImport i x = return () ------------------------------------------------------------------------ -- * Debugging ------------------------------------------------------------------------ class ShowHead a where showHead :: a -> String instance ShowHead A.Declaration where showHead d = case d of A.Axiom {} -> "Axiom" A.Field {} -> "Field" A.Primitive {} -> "Primitive" A.Mutual {} -> "Mutual" A.Section {} -> "Section" A.Apply {} -> "Apply" A.Import {} -> "Import" A.Pragma {} -> "Pragma" A.Open {} -> "Open" A.FunDef {} -> "FunDef" A.DataSig {} -> "DataSig" A.DataDef {} -> "DataDef" A.RecSig {} -> "RecSig" A.RecDef {} -> "RecDef" A.PatternSynDef{} -> "PatternSynDef" A.UnquoteDecl {} -> "UnquoteDecl" A.ScopedDecl {} -> "ScopedDecl" A.UnquoteDef {} -> "UnquoteDef" debugPrintDecl :: A.Declaration -> TCM () debugPrintDecl d = do verboseS "tc.decl" 45 $ do reportSLn "tc.decl" 45 $ "checking a " ++ showHead d case d of A.Section info mname tel ds -> do reportSLn "tc.decl" 45 $ "section " ++ prettyShow mname ++ " has " ++ show (length tel) ++ " parameters and " ++ show (length ds) ++ " declarations" reportSDoc "tc.decl" 45 $ prettyA $ A.Section info mname tel [] forM_ ds $ \ d -> do reportSDoc "tc.decl" 45 $ prettyA d _ -> return () Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Builtin.hs0000644000000000000000000007663613154613124020563 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Rules.Builtin ( bindBuiltin , bindBuiltinNoDef , bindPostulatedName , isUntypedBuiltin , bindUntypedBuiltin ) where import Control.Applicative hiding (empty) import Control.Monad import Control.Monad.Reader (ask) import Control.Monad.State (get) import Data.List (find) import qualified Agda.Syntax.Abstract as A import qualified Agda.Syntax.Abstract.Views as A import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Position import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Monad.SizedTypes ( builtinSizeHook ) import qualified Agda.TypeChecking.CompiledClause as CC import Agda.TypeChecking.Conversion import Agda.TypeChecking.Constraints import Agda.TypeChecking.EtaContract import Agda.TypeChecking.Functions import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.Primitive import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Rules.Term ( checkExpr , inferExpr ) import Agda.TypeChecking.Warnings import {-# SOURCE #-} Agda.TypeChecking.Rules.Builtin.Coinduction import {-# SOURCE #-} Agda.TypeChecking.Rewriting import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Checking builtin pragmas --------------------------------------------------------------------------- builtinPostulate :: TCM Type -> BuiltinDescriptor builtinPostulate = BuiltinPostulate Relevant coreBuiltins :: [BuiltinInfo] coreBuiltins = [ (builtinList |-> BuiltinData (tset --> tset) [builtinNil, builtinCons]) , (builtinArg |-> BuiltinData (tset --> tset) [builtinArgArg]) , (builtinAbs |-> BuiltinData (tset --> tset) [builtinAbsAbs]) , (builtinArgInfo |-> BuiltinData tset [builtinArgArgInfo]) , (builtinBool |-> BuiltinData tset [builtinTrue, builtinFalse]) , (builtinNat |-> BuiltinData tset [builtinZero, builtinSuc]) , (builtinUnit |-> BuiltinData tset [builtinUnitUnit]) -- actually record, but they are treated the same , (builtinAgdaLiteral |-> BuiltinData tset [builtinAgdaLitNat, builtinAgdaLitFloat, builtinAgdaLitChar, builtinAgdaLitString, builtinAgdaLitQName, builtinAgdaLitMeta]) , (builtinAgdaPattern |-> BuiltinData tset [builtinAgdaPatVar, builtinAgdaPatCon, builtinAgdaPatDot, builtinAgdaPatLit, builtinAgdaPatProj, builtinAgdaPatAbsurd]) , (builtinAgdaPatVar |-> BuiltinDataCons (tstring --> tpat)) , (builtinAgdaPatCon |-> BuiltinDataCons (tqname --> tlist (targ tpat) --> tpat)) , (builtinAgdaPatDot |-> BuiltinDataCons tpat) , (builtinAgdaPatLit |-> BuiltinDataCons (tliteral --> tpat)) , (builtinAgdaPatProj |-> BuiltinDataCons (tqname --> tpat)) , (builtinAgdaPatAbsurd |-> BuiltinDataCons tpat) , (builtinLevel |-> builtinPostulate tset) , (builtinInteger |-> BuiltinData tset [builtinIntegerPos, builtinIntegerNegSuc]) , (builtinIntegerPos |-> BuiltinDataCons (tnat --> tinteger)) , (builtinIntegerNegSuc |-> BuiltinDataCons (tnat --> tinteger)) , (builtinFloat |-> builtinPostulate tset) , (builtinChar |-> builtinPostulate tset) , (builtinString |-> builtinPostulate tset) , (builtinQName |-> builtinPostulate tset) , (builtinAgdaMeta |-> builtinPostulate tset) , (builtinIO |-> builtinPostulate (tset --> tset)) , (builtinAgdaSort |-> BuiltinData tset [builtinAgdaSortSet, builtinAgdaSortLit, builtinAgdaSortUnsupported]) , (builtinAgdaTerm |-> BuiltinData tset [ builtinAgdaTermVar, builtinAgdaTermLam, builtinAgdaTermExtLam , builtinAgdaTermDef, builtinAgdaTermCon , builtinAgdaTermPi, builtinAgdaTermSort , builtinAgdaTermLit, builtinAgdaTermMeta , builtinAgdaTermUnsupported]) , builtinAgdaErrorPart |-> BuiltinData tset [ builtinAgdaErrorPartString, builtinAgdaErrorPartTerm, builtinAgdaErrorPartName ] , builtinAgdaErrorPartString |-> BuiltinDataCons (tstring --> terrorpart) , builtinAgdaErrorPartTerm |-> BuiltinDataCons (tterm --> terrorpart) , builtinAgdaErrorPartName |-> BuiltinDataCons (tqname --> terrorpart) -- Andreas, 2017-01-12, issue #2386: special handling of builtinEquality -- , (builtinEquality |-> BuiltinData (hPi "a" (el primLevel) $ -- hPi "A" (return $ sort $ varSort 0) $ -- (El (varSort 1) <$> varM 0) --> -- (El (varSort 1) <$> varM 0) --> -- return (sort $ varSort 1)) -- [builtinRefl]) , (builtinHiding |-> BuiltinData tset [builtinHidden, builtinInstance, builtinVisible]) -- Relevance , (builtinRelevance |-> BuiltinData tset [builtinRelevant, builtinIrrelevant]) , (builtinRelevant |-> BuiltinDataCons trelevance) , (builtinIrrelevant |-> BuiltinDataCons trelevance) -- Associativity , builtinAssoc |-> BuiltinData tset [builtinAssocLeft, builtinAssocRight, builtinAssocNon] , builtinAssocLeft |-> BuiltinDataCons tassoc , builtinAssocRight |-> BuiltinDataCons tassoc , builtinAssocNon |-> BuiltinDataCons tassoc -- Precedence , builtinPrecedence |-> BuiltinData tset [builtinPrecRelated, builtinPrecUnrelated] , builtinPrecRelated |-> BuiltinDataCons (tint --> tprec) , builtinPrecUnrelated |-> BuiltinDataCons tprec -- Fixity , builtinFixity |-> BuiltinData tset [builtinFixityFixity] , builtinFixityFixity |-> BuiltinDataCons (tassoc --> tprec --> tfixity) -- Andreas, 2017-01-12, issue #2386: special handling of builtinEquality -- , (builtinRefl |-> BuiltinDataCons (hPi "a" (el primLevel) $ -- hPi "A" (return $ sort $ varSort 0) $ -- hPi "x" (El (varSort 1) <$> varM 0) $ -- El (varSort 2) <$> primEquality <#> varM 2 <#> varM 1 <@> varM 0 <@> varM 0)) , (builtinRewrite |-> BuiltinUnknown Nothing verifyBuiltinRewrite) , (builtinNil |-> BuiltinDataCons (hPi "A" tset (el (list v0)))) , (builtinCons |-> BuiltinDataCons (hPi "A" tset (tv0 --> el (list v0) --> el (list v0)))) , (builtinZero |-> BuiltinDataCons tnat) , (builtinSuc |-> BuiltinDataCons (tnat --> tnat)) , (builtinTrue |-> BuiltinDataCons tbool) , (builtinFalse |-> BuiltinDataCons tbool) , (builtinArgArg |-> BuiltinDataCons (hPi "A" tset (targinfo --> tv0 --> targ tv0))) , (builtinAbsAbs |-> BuiltinDataCons (hPi "A" tset (tstring --> tv0 --> tabs tv0))) , (builtinArgArgInfo |-> BuiltinDataCons (thiding --> trelevance --> targinfo)) , (builtinAgdaTermVar |-> BuiltinDataCons (tnat --> targs --> tterm)) , (builtinAgdaTermLam |-> BuiltinDataCons (thiding --> tabs tterm --> tterm)) , (builtinAgdaTermExtLam |-> BuiltinDataCons (tlist tclause --> targs --> tterm)) , (builtinAgdaTermDef |-> BuiltinDataCons (tqname --> targs --> tterm)) , (builtinAgdaTermCon |-> BuiltinDataCons (tqname --> targs --> tterm)) , (builtinAgdaTermPi |-> BuiltinDataCons (targ ttype --> tabs ttype --> tterm)) , (builtinAgdaTermSort |-> BuiltinDataCons (tsort --> tterm)) , (builtinAgdaTermLit |-> BuiltinDataCons (tliteral --> tterm)) , (builtinAgdaTermMeta |-> BuiltinDataCons (tmeta --> targs --> tterm)) , (builtinAgdaTermUnsupported|-> BuiltinDataCons tterm) , (builtinAgdaLitNat |-> BuiltinDataCons (tnat --> tliteral)) , (builtinAgdaLitFloat |-> BuiltinDataCons (tfloat --> tliteral)) , (builtinAgdaLitChar |-> BuiltinDataCons (tchar --> tliteral)) , (builtinAgdaLitString |-> BuiltinDataCons (tstring --> tliteral)) , (builtinAgdaLitQName |-> BuiltinDataCons (tqname --> tliteral)) , (builtinAgdaLitMeta |-> BuiltinDataCons (tmeta --> tliteral)) , (builtinHidden |-> BuiltinDataCons thiding) , (builtinInstance |-> BuiltinDataCons thiding) , (builtinVisible |-> BuiltinDataCons thiding) , (builtinSizeUniv |-> builtinPostulate tSizeUniv) -- SizeUniv : SizeUniv -- See comment on tSizeUniv: the following does not work currently. -- , (builtinSizeUniv |-> builtinPostulate tSetOmega) -- SizeUniv : Setω , (builtinSize |-> builtinPostulate tSizeUniv) , (builtinSizeLt |-> builtinPostulate (tsize ..--> tSizeUniv)) , (builtinSizeSuc |-> builtinPostulate (tsize --> tsize)) , (builtinSizeInf |-> builtinPostulate tsize) -- postulate max : {i : Size} -> Size< i -> Size< i -> Size< i , (builtinSizeMax |-> builtinPostulate (tsize --> tsize --> tsize)) -- (hPi "i" tsize $ let a = el $ primSizeLt <@> v0 in (a --> a --> a))) , (builtinAgdaSortSet |-> BuiltinDataCons (tterm --> tsort)) , (builtinAgdaSortLit |-> BuiltinDataCons (tnat --> tsort)) , (builtinAgdaSortUnsupported|-> BuiltinDataCons tsort) , (builtinNatPlus |-> BuiltinPrim "primNatPlus" verifyPlus) , (builtinNatMinus |-> BuiltinPrim "primNatMinus" verifyMinus) , (builtinNatTimes |-> BuiltinPrim "primNatTimes" verifyTimes) , (builtinNatDivSucAux |-> BuiltinPrim "primNatDivSucAux" verifyDivSucAux) , (builtinNatModSucAux |-> BuiltinPrim "primNatModSucAux" verifyModSucAux) , (builtinNatEquals |-> BuiltinPrim "primNatEquality" verifyEquals) , (builtinNatLess |-> BuiltinPrim "primNatLess" verifyLess) , (builtinLevelZero |-> BuiltinPrim "primLevelZero" (const $ return ())) , (builtinLevelSuc |-> BuiltinPrim "primLevelSuc" (const $ return ())) , (builtinLevelMax |-> BuiltinPrim "primLevelMax" verifyMax) , (builtinAgdaClause |-> BuiltinData tset [builtinAgdaClauseClause, builtinAgdaClauseAbsurd]) , (builtinAgdaClauseClause |-> BuiltinDataCons (tlist (targ tpat) --> tterm --> tclause)) , (builtinAgdaClauseAbsurd |-> BuiltinDataCons (tlist (targ tpat) --> tclause)) , (builtinAgdaDefinition |-> BuiltinData tset [builtinAgdaDefinitionFunDef ,builtinAgdaDefinitionDataDef ,builtinAgdaDefinitionDataConstructor ,builtinAgdaDefinitionRecordDef ,builtinAgdaDefinitionPostulate ,builtinAgdaDefinitionPrimitive]) , (builtinAgdaDefinitionFunDef |-> BuiltinDataCons (tlist tclause --> tdefn)) , (builtinAgdaDefinitionDataDef |-> BuiltinDataCons (tnat --> tlist tqname --> tdefn)) , (builtinAgdaDefinitionDataConstructor |-> BuiltinDataCons (tqname --> tdefn)) , (builtinAgdaDefinitionRecordDef |-> BuiltinDataCons (tqname --> tlist (targ tqname) --> tdefn)) , (builtinAgdaDefinitionPostulate |-> BuiltinDataCons tdefn) , (builtinAgdaDefinitionPrimitive |-> BuiltinDataCons tdefn) , builtinAgdaTCM |-> builtinPostulate (hPi "a" tlevel $ tsetL 0 --> tsetL 0) , builtinAgdaTCMReturn |-> builtinPostulate (hPi "a" tlevel $ hPi "A" (tsetL 0) $ elV 1 (varM 0) --> tTCM 1 (varM 0)) , builtinAgdaTCMBind |-> builtinPostulate (hPi "a" tlevel $ hPi "b" tlevel $ hPi "A" (tsetL 1) $ hPi "B" (tsetL 1) $ tTCM 3 (varM 1) --> (elV 3 (varM 1) --> tTCM 2 (varM 0)) --> tTCM 2 (varM 0)) , builtinAgdaTCMUnify |-> builtinPostulate (tterm --> tterm --> tTCM_ primUnit) , builtinAgdaTCMTypeError |-> builtinPostulate (hPi "a" tlevel $ hPi "A" (tsetL 0) $ tlist terrorpart --> tTCM 1 (varM 0)) , builtinAgdaTCMInferType |-> builtinPostulate (tterm --> tTCM_ primAgdaTerm) , builtinAgdaTCMCheckType |-> builtinPostulate (tterm --> ttype --> tTCM_ primAgdaTerm) , builtinAgdaTCMNormalise |-> builtinPostulate (tterm --> tTCM_ primAgdaTerm) , builtinAgdaTCMReduce |-> builtinPostulate (tterm --> tTCM_ primAgdaTerm) , builtinAgdaTCMCatchError |-> builtinPostulate (hPi "a" tlevel $ hPi "A" (tsetL 0) $ tTCM 1 (varM 0) --> tTCM 1 (varM 0) --> tTCM 1 (varM 0)) , builtinAgdaTCMGetContext |-> builtinPostulate (tTCM_ (unEl <$> tlist (targ ttype))) , builtinAgdaTCMExtendContext |-> builtinPostulate (hPi "a" tlevel $ hPi "A" (tsetL 0) $ targ ttype --> tTCM 1 (varM 0) --> tTCM 1 (varM 0)) , builtinAgdaTCMInContext |-> builtinPostulate (hPi "a" tlevel $ hPi "A" (tsetL 0) $ tlist (targ ttype) --> tTCM 1 (varM 0) --> tTCM 1 (varM 0)) , builtinAgdaTCMFreshName |-> builtinPostulate (tstring --> tTCM_ primQName) , builtinAgdaTCMDeclareDef |-> builtinPostulate (targ tqname --> ttype --> tTCM_ primUnit) , builtinAgdaTCMDefineFun |-> builtinPostulate (tqname --> tlist tclause --> tTCM_ primUnit) , builtinAgdaTCMGetType |-> builtinPostulate (tqname --> tTCM_ primAgdaTerm) , builtinAgdaTCMGetDefinition |-> builtinPostulate (tqname --> tTCM_ primAgdaDefinition) , builtinAgdaTCMQuoteTerm |-> builtinPostulate (hPi "a" tlevel $ hPi "A" (tsetL 0) $ elV 1 (varM 0) --> tTCM_ primAgdaTerm) , builtinAgdaTCMUnquoteTerm |-> builtinPostulate (hPi "a" tlevel $ hPi "A" (tsetL 0) $ tterm --> tTCM 1 (varM 0)) , builtinAgdaTCMBlockOnMeta |-> builtinPostulate (hPi "a" tlevel $ hPi "A" (tsetL 0) $ tmeta --> tTCM 1 (varM 0)) , builtinAgdaTCMCommit |-> builtinPostulate (tTCM_ primUnit) , builtinAgdaTCMIsMacro |-> builtinPostulate (tqname --> tTCM_ primBool) , builtinAgdaTCMWithNormalisation |-> builtinPostulate (hPi "a" tlevel $ hPi "A" (tsetL 0) $ tbool --> tTCM 1 (varM 0) --> tTCM 1 (varM 0)) , builtinAgdaTCMDebugPrint |-> builtinPostulate (tstring --> tnat --> tlist terrorpart --> tTCM_ primUnit) ] where (|->) = BuiltinInfo v0 = varM 0 v1 = varM 1 v2 = varM 2 v3 = varM 3 tv0,tv1 :: TCM Type tv0 = el v0 tv1 = el v1 tv2 = el v2 tv3 = el v3 arg :: TCM Term -> TCM Term arg t = primArg <@> t elV x a = El (varSort x) <$> a tsetL l = return $ sort (varSort l) tlevel = el primLevel tlist x = el $ list (fmap unEl x) targ x = el (arg (fmap unEl x)) tabs x = el (primAbs <@> fmap unEl x) targs = el (list (arg primAgdaTerm)) tterm = el primAgdaTerm terrorpart = el primAgdaErrorPart tnat = el primNat tint = el primInteger tunit = el primUnit tinteger = el primInteger tfloat = el primFloat tchar = el primChar tstring = el primString tqname = el primQName tmeta = el primAgdaMeta tsize = El sSizeUniv <$> primSize tbool = el primBool thiding = el primHiding trelevance = el primRelevance tassoc = el primAssoc tprec = el primPrecedence tfixity = el primFixity -- tcolors = el (list primAgdaTerm) -- TODO guilhem targinfo = el primArgInfo ttype = el primAgdaTerm tsort = el primAgdaSort tdefn = el primAgdaDefinition tliteral = el primAgdaLiteral tpat = el primAgdaPattern tclause = el primAgdaClause tTCM l a = elV l (primAgdaTCM <#> varM l <@> a) tTCM_ a = el (primAgdaTCM <#> primLevelZero <@> a) verifyPlus plus = verify ["n","m"] $ \(@@) zero suc (==) (===) choice -> do let m = var 0 n = var 1 x + y = plus @@ x @@ y -- We allow recursion on any argument choice [ do n + zero == n n + suc m == suc (n + m) , do suc n + m == suc (n + m) zero + m == m ] verifyMinus minus = verify ["n","m"] $ \(@@) zero suc (==) (===) choice -> do let m = var 0 n = var 1 x - y = minus @@ x @@ y -- We allow recursion on any argument zero - zero == zero zero - suc m == zero suc n - zero == suc n suc n - suc m == (n - m) verifyTimes times = do plus <- primNatPlus verify ["n","m"] $ \(@@) zero suc (==) (===) choice -> do let m = var 0 n = var 1 x + y = plus @@ x @@ y x * y = times @@ x @@ y choice [ do n * zero == zero choice [ (n * suc m) == (n + (n * m)) , (n * suc m) == ((n * m) + n) ] , do zero * n == zero choice [ (suc n * m) == (m + (n * m)) , (suc n * m) == ((n * m) + m) ] ] verifyDivSucAux dsAux = verify ["k","m","n","j"] $ \(@@) zero suc (==) (===) choice -> do let aux k m n j = dsAux @@ k @@ m @@ n @@ j k = var 0 m = var 1 n = var 2 j = var 3 aux k m zero j == k aux k m (suc n) zero == aux (suc k) m n m aux k m (suc n) (suc j) == aux k m n j verifyModSucAux dsAux = verify ["k","m","n","j"] $ \(@@) zero suc (==) (===) choice -> do let aux k m n j = dsAux @@ k @@ m @@ n @@ j k = var 0 m = var 1 n = var 2 j = var 3 aux k m zero j == k aux k m (suc n) zero == aux zero m n m aux k m (suc n) (suc j) == aux (suc k) m n j verifyEquals eq = verify ["n","m"] $ \(@@) zero suc (==) (===) choice -> do true <- primTrue false <- primFalse let x == y = eq @@ x @@ y m = var 0 n = var 1 (zero == zero ) === true (suc n == suc m) === (n == m) (suc n == zero ) === false (zero == suc n) === false verifyLess leq = verify ["n","m"] $ \(@@) zero suc (==) (===) choice -> do true <- primTrue false <- primFalse let x < y = leq @@ x @@ y m = var 0 n = var 1 (n < zero) === false (suc n < suc m) === (n < m) (zero < suc m) === true verifyMax maxV = return () -- TODO: make max a postulate verify xs = verify' primNat primZero primSuc xs verify' :: TCM Term -> TCM Term -> TCM Term -> [String] -> ( (Term -> Term -> Term) -> Term -> (Term -> Term) -> (Term -> Term -> TCM ()) -> (Term -> Term -> TCM ()) -> ([TCM ()] -> TCM ()) -> TCM a) -> TCM a verify' pNat pZero pSuc xs f = do nat <- El (mkType 0) <$> pNat zero <- pZero s <- pSuc let x == y = noConstraints $ equalTerm nat x y -- Andreas: 2013-10-21 I put primBool here on the inside -- since some Nat-builtins do not require Bool-builtins x === y = do bool <- El (mkType 0) <$> primBool noConstraints $ equalTerm bool x y suc n = s `apply1` n choice = foldr1 (\x y -> x `catchError` \_ -> y) xs <- mapM freshName_ xs addContext (xs, domFromArg $ defaultArg nat) $ f apply1 zero suc (==) (===) choice -- | Checks that builtin with name @b : String@ of type @t : Term@ -- is a data type or inductive record with @n : Int@ constructors. -- Returns the name of the data/record type. inductiveCheck :: String -> Int -> Term -> TCM (QName, Definition) inductiveCheck b n t = do t <- etaContract =<< normalise t case ignoreSharing t of Def q _ -> do def <- getConstInfo q let yes = return (q, def) case theDef def of Datatype { dataInduction = Inductive, dataCons = cs } | length cs == n -> yes | otherwise -> no Record { recInduction = ind } | n == 1 && ind /= Just CoInductive -> yes _ -> no _ -> no where no | n == 1 = typeError $ GenericError $ unwords [ "The builtin", b , "must be a datatype with a single constructor" , "or an (inductive) record type" ] | otherwise = typeError $ GenericError $ unwords [ "The builtin", b , "must be a datatype with", show n , "constructors" ] -- | @bindPostulatedName builtin e m@ checks that @e@ is a postulated -- name @q@, and binds the builtin @builtin@ to the term @m q def@, -- where @def@ is the current 'Definition' of @q@. bindPostulatedName :: String -> A.Expr -> (QName -> Definition -> TCM Term) -> TCM () bindPostulatedName builtin e m = do q <- getName e def <- getConstInfo q case theDef def of Axiom {} -> bindBuiltinName builtin =<< m q def _ -> err where err = typeError $ GenericError $ "The argument to BUILTIN " ++ builtin ++ " must be a postulated name" getName (A.Def q) = return q getName (A.ScopedExpr _ e) = getName e getName _ = err -- REPLACED by TC.Functions.getDef -- getDef :: Term -> TCM QName -- getDef t = do -- t <- etaContract =<< normalise t -- case ignoreSharing t of -- Def d _ -> return d -- _ -> __IMPOSSIBLE__ addHaskellPragma :: QName -> String -> TCM () addHaskellPragma = addPragma ghcBackendName bindAndSetHaskellCode :: String -> String -> Term -> TCM () bindAndSetHaskellCode b hs t = do d <- fromMaybe __IMPOSSIBLE__ <$> getDef t addHaskellPragma d hs bindBuiltinName b t bindBuiltinBool :: Term -> TCM () bindBuiltinBool = bindAndSetHaskellCode builtinBool "= type Bool" bindBuiltinInt :: Term -> TCM () bindBuiltinInt = bindAndSetHaskellCode builtinInteger "= type Integer" bindBuiltinNat :: Term -> TCM () bindBuiltinNat t = do nat <- fromMaybe __IMPOSSIBLE__ <$> getDef t def <- theDef <$> getConstInfo nat case def of Datatype { dataCons = [c1, c2] } -> do bindBuiltinName builtinNat t let getArity c = arity <$> (normalise . defType =<< getConstInfo c) [a1, a2] <- mapM getArity [c1, c2] let (zero, suc) | a2 > a1 = (c1, c2) | otherwise = (c2, c1) tnat = el primNat rerange = setRange (getRange nat) addHaskellPragma nat "= type Integer" bindBuiltinInfo (BuiltinInfo builtinZero $ BuiltinDataCons tnat) (A.Con $ AmbQ [rerange zero]) bindBuiltinInfo (BuiltinInfo builtinSuc $ BuiltinDataCons (tnat --> tnat)) (A.Con $ AmbQ [rerange suc]) _ -> __IMPOSSIBLE__ bindBuiltinUnit :: Term -> TCM () bindBuiltinUnit t = do unit <- fromMaybe __IMPOSSIBLE__ <$> getDef t def <- theDef <$> getConstInfo unit case def of Record { recFields = [], recConHead = con } -> do bindBuiltinName builtinUnit t bindBuiltinName builtinUnitUnit (Con con ConOSystem []) _ -> genericError "Builtin UNIT must be a singleton record type" -- | Bind BUILTIN EQUALITY and BUILTIN REFL. bindBuiltinEquality :: A.Expr -> TCM () bindBuiltinEquality (A.ScopedExpr scope e) = do setScope scope bindBuiltinEquality e bindBuiltinEquality e = do (v, _t) <- inferExpr e -- Equality needs to be a data type with 1 constructor (eq, def) <- inductiveCheck builtinEquality 1 v -- Check that the type is the type of a polymorphic relation, i.e., -- Γ → (A : Set _) → A → A → Set _ TelV eqTel eqCore <- telView $ defType def let no = genericError "The type of BUILTIN EQUALITY must be a polymorphic relation" -- The target is a sort since eq is a data type. unless (isJust $ isSort $ unEl eqCore) __IMPOSSIBLE__ -- The types of the last two arguments must be the third-last argument unless (size eqTel >= 3) no let (a, b) = fromMaybe __IMPOSSIBLE__ $ last2 $ telToList eqTel [a,b] <- reduce $ map (unEl . snd . unDom) [a,b] unless (deBruijnView a == Just 0) no unless (deBruijnView b == Just 1) no -- Get the single constructor. case theDef def of Datatype { dataCons = [c] } -> do bindBuiltinName builtinEquality v -- Check type of REFL. It has to be of the form -- pars → (x : A) → Eq ... x x -- Check the arguments cdef <- getConstInfo c TelV conTel conCore <- telView $ defType cdef ts <- reduce $ map (unEl . snd . unDom) $ drop (conPars $ theDef cdef) $ telToList conTel -- After dropping the parameters, there should be maximally one argument. unless (length ts <= 1) wrongRefl unless (all ((Just 0 ==) . deBruijnView) ts) wrongRefl -- Check the target case ignoreSharing $ unEl conCore of Def _ es -> do let vs = map unArg $ fromMaybe __IMPOSSIBLE__ $ allApplyElims es (a,b) <- reduce $ fromMaybe __IMPOSSIBLE__ $ last2 vs unless (deBruijnView a == Just 0) wrongRefl unless (deBruijnView b == Just 0) wrongRefl bindBuiltinName builtinRefl (Con (ConHead c Inductive []) ConOSystem []) _ -> __IMPOSSIBLE__ _ -> genericError "Builtin EQUALITY must be a data type with a single constructor" where wrongRefl = genericError "Wrong type of constructor of BUILTIN EQUALITY" bindBuiltinInfo :: BuiltinInfo -> A.Expr -> TCM () bindBuiltinInfo i (A.ScopedExpr scope e) = setScope scope >> bindBuiltinInfo i e bindBuiltinInfo (BuiltinInfo s d) e = do case d of BuiltinData t cs -> do v <- checkExpr e =<< t unless (s == builtinUnit) $ do void $ inductiveCheck s (length cs) v if | s == builtinEquality -> __IMPOSSIBLE__ -- bindBuiltinEquality v | s == builtinBool -> bindBuiltinBool v | s == builtinNat -> bindBuiltinNat v | s == builtinInteger -> bindBuiltinInt v | s == builtinUnit -> bindBuiltinUnit v | otherwise -> bindBuiltinName s v BuiltinDataCons t -> do let name (Lam h b) = name (absBody b) name (Con c ci _) = Con c ci [] name (Shared p) = name $ ignoreSharing (derefPtr p) name _ = __IMPOSSIBLE__ v0 <- checkExpr e =<< t case e of A.Con{} -> return () _ -> typeError $ BuiltinMustBeConstructor s e let v@(Con h _ []) = name v0 c = conName h bindBuiltinName s v BuiltinPrim pfname axioms -> do case e of A.Def qx -> do PrimImpl t pf <- lookupPrimitiveFunction pfname v <- checkExpr e t axioms v info <- getConstInfo qx let cls = defClauses info a = defAbstract info mcc = defCompiled info bindPrimitive pfname $ pf { primFunName = qx } addConstant qx $ info { theDef = Primitive a pfname cls mcc } -- needed? yes, for checking equations for mul bindBuiltinName s v _ -> typeError $ GenericError $ "Builtin " ++ s ++ " must be bound to a function" BuiltinPostulate rel t -> do t' <- t v <- applyRelevanceToContext rel $ checkExpr e t' let err = typeError $ GenericError $ "The argument to BUILTIN " ++ s ++ " must be a postulated name" case e of A.Def q -> do def <- getConstInfo q case theDef def of Axiom {} -> do builtinSizeHook s q t' when (s == builtinChar) $ addHaskellPragma q "= type Char" when (s == builtinString) $ addHaskellPragma q "= type Data.Text.Text" when (s == builtinFloat) $ addHaskellPragma q "= type Double" bindBuiltinName s v _ -> err _ -> err BuiltinUnknown mt f -> do (v, t) <- caseMaybe mt (inferExpr e) $ \ tcmt -> do t <- tcmt (,t) <$> checkExpr e t f v t bindBuiltinName s v -- | Bind a builtin thing to an expression. bindBuiltin :: String -> A.Expr -> TCM () bindBuiltin b e = do unlessM ((0 ==) <$> getContextSize) $ typeError $ BuiltinInParameterisedModule b if | b == builtinRefl -> warning $ OldBuiltin b builtinEquality | b == builtinZero -> nowNat b | b == builtinSuc -> nowNat b | b == builtinInf -> bindBuiltinInf e | b == builtinSharp -> bindBuiltinSharp e | b == builtinFlat -> bindBuiltinFlat e | b == builtinEquality -> bindBuiltinEquality e | Just i <- find ((b ==) . builtinName) coreBuiltins -> bindBuiltinInfo i e | otherwise -> typeError $ NoSuchBuiltinName b where nowNat b = warning $ OldBuiltin b builtinNat isUntypedBuiltin :: String -> Bool isUntypedBuiltin b = elem b [builtinFromNat, builtinFromNeg, builtinFromString] bindUntypedBuiltin :: String -> A.Expr -> TCM () bindUntypedBuiltin b e = case A.unScope e of A.Def q -> bindBuiltinName b (Def q []) A.Proj _ (AmbQ [q]) -> bindBuiltinName b (Def q []) e -> genericError $ "The argument to BUILTIN " ++ b ++ " must be a defined unambiguous name" -- | Bind a builtin thing to a new name. bindBuiltinNoDef :: String -> A.QName -> TCM () bindBuiltinNoDef b q = do unlessM ((0 ==) <$> getContextSize) $ typeError $ BuiltinInParameterisedModule b case lookup b $ map (\ (BuiltinInfo b i) -> (b, i)) coreBuiltins of Just (BuiltinPostulate rel mt) -> do t <- mt addConstant q $ defaultDefn (setRelevance rel defaultArgInfo) q t def builtinSizeHook b q t bindBuiltinName b $ Def q [] where -- Andreas, 2015-02-14 -- Special treatment of SizeUniv, should maybe be a primitive. def | b == builtinSizeUniv = emptyFunction { funClauses = [ (empty :: Clause) { clauseBody = Just $ Sort sSizeUniv } ] , funCompiled = Just (CC.Done [] $ Sort sSizeUniv) , funTerminates = Just True } | otherwise = Axiom Just{} -> __IMPOSSIBLE__ Nothing -> __IMPOSSIBLE__ -- typeError $ NoSuchBuiltinName b Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Display.hs0000644000000000000000000001103613154613124020541 0ustar0000000000000000 module Agda.TypeChecking.Rules.Display (checkDisplayPragma) where import Control.Applicative import Data.Maybe import qualified Data.List as List import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Views import Agda.Syntax.Internal as I import Agda.Syntax.Common import Agda.TypeChecking.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Pretty checkDisplayPragma :: QName -> [NamedArg A.Pattern] -> A.Expr -> TCM () checkDisplayPragma f ps e = inTopContext $ do pappToTerm f id ps $ \n args -> do let lhs = map I.Apply args v <- exprToTerm e let df = Display n lhs (DTerm v) reportSLn "tc.display.pragma" 20 $ "Adding display form for " ++ show f ++ "\n " ++ show df escapeContext n $ addDisplayForm f df -- Compute a left-hand side for a display form. Inserts implicits, but no type -- checking so does the wrong thing if implicitness is computed. Binds variables. displayLHS :: Telescope -> [NamedArg A.Pattern] -> (Int -> [Term] -> TCM a) -> TCM a displayLHS tel ps ret = patternsToTerms tel ps $ \n vs -> ret n (map unArg vs) patternsToTerms :: Telescope -> [NamedArg A.Pattern] -> (Int -> Args -> TCM a) -> TCM a patternsToTerms _ [] ret = ret 0 [] patternsToTerms EmptyTel (p : ps) ret = patternToTerm (namedArg p) $ \n v -> patternsToTerms EmptyTel ps $ \m vs -> ret (n + m) (inheritHiding p v : vs) patternsToTerms (ExtendTel a tel) (p : ps) ret = do let isMatch = sameHiding p a && (visible p || isNothing (nameOf (unArg p)) || Just (absName tel) == (rangedThing <$> nameOf (unArg p))) case isMatch of True -> patternToTerm (namedArg p) $ \n v -> patternsToTerms (unAbs tel) ps $ \m vs -> ret (n + m) (inheritHiding p v : vs) False -> bindWild $ patternsToTerms (unAbs tel) (p : ps) $ \n vs -> ret (1 + n) (inheritHiding a (Var 0 []) : vs) inheritHiding :: LensHiding a => a -> b -> Arg b inheritHiding a b = setHiding (getHiding a) (defaultArg b) pappToTerm :: QName -> (Args -> b) -> [NamedArg A.Pattern] -> (Int -> b -> TCM a) -> TCM a pappToTerm x f ps ret = do def <- getConstInfo x TelV tel _ <- telView $ defType def let dropTel n = telFromList . drop n . telToList pars = case theDef def of Constructor { conPars = p } -> p Function { funProjection = Just Projection{projIndex = i} } | i > 0 -> i - 1 _ -> 0 patternsToTerms (dropTel pars tel) ps $ \n vs -> ret n (f vs) patternToTerm :: A.Pattern -> (Nat -> Term -> TCM a) -> TCM a patternToTerm p ret = case p of A.VarP x -> bindVar x $ ret 1 (Var 0 []) A.ConP _ (AmbQ [c]) ps -> pappToTerm c (Con (ConHead c Inductive []) ConOCon) ps ret A.ConP _ (AmbQ cs) _ -> genericError $ "Ambiguous constructor: " ++ List.intercalate ", " (map show cs) A.ProjP _ _ (AmbQ [d]) -> ret 0 (Def d []) A.ProjP _ _ (AmbQ ds) -> genericError $ "Ambiguous projection: " ++ List.intercalate ", " (map show ds) A.DefP _ (AmbQ [f]) ps -> pappToTerm f (Def f . map Apply) ps ret A.DefP _ (AmbQ ds) _ -> genericError $ "Ambiguous DefP: " ++ List.intercalate ", " (map show ds) A.LitP l -> ret 0 (Lit l) A.WildP _ -> bindWild $ ret 1 (Var 0 []) _ -> do doc <- prettyA p typeError $ GenericError $ "Pattern not allowed in DISPLAY pragma:\n" ++ show doc bindWild :: TCM a -> TCM a bindWild ret = do x <- freshNoName_ bindVar x ret bindVar :: Name -> TCM a -> TCM a bindVar x ret = addContext x ret exprToTerm :: A.Expr -> TCM Term exprToTerm e = case unScope e of A.Var x -> fst <$> getVarInfo x A.Def f -> pure $ Def f [] A.Con (AmbQ (c:_)) -> pure $ Con (ConHead c Inductive []) ConOCon [] -- Don't care too much about ambiguity here A.Lit l -> pure $ Lit l A.App _ e arg -> apply <$> exprToTerm e <*> ((:[]) . inheritHiding arg <$> exprToTerm (namedArg arg)) A.Proj _ (AmbQ (f:_)) -> pure $ Def f [] -- only for printing so we don't have to worry too much here A.PatternSyn f -> pure $ Def f [] A.Macro f -> pure $ Def f [] A.WithApp{} -> notAllowed "with application" A.QuestionMark{} -> notAllowed "holes" A.Underscore{} -> notAllowed "metavariables" A.Lam{} -> notAllowed "lambdas" A.AbsurdLam{} -> notAllowed "lambdas" A.ExtendedLam{} -> notAllowed "lambdas" _ -> typeError $ GenericError $ "TODO: exprToTerm " ++ show e where notAllowed s = typeError $ GenericError $ "Not allowed in DISPLAY pragma right-hand side: " ++ s Agda-2.5.3/src/full/Agda/TypeChecking/Rules/LHS/0000755000000000000000000000000013154613124017225 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Rules/LHS/ProblemRest.hs0000644000000000000000000001524413154613124022025 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Rules.LHS.ProblemRest where import Control.Arrow (first, second) import Data.Functor ((<$)) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Abstract.Pattern import qualified Agda.Syntax.Abstract as A import Agda.TypeChecking.Monad import Agda.TypeChecking.Pretty import Agda.TypeChecking.Telescope import Agda.TypeChecking.Substitute import Agda.TypeChecking.Rules.LHS.Problem import Agda.TypeChecking.Rules.LHS.Implicit import Agda.Utils.Functor (($>)) import Agda.Utils.Size import Agda.Utils.Permutation #include "undefined.h" import Agda.Utils.Impossible -- MOVED from LHS: -- | Rename the variables in a telescope using the names from a given pattern useNamesFromPattern :: [NamedArg A.Pattern] -> Telescope -> Telescope useNamesFromPattern ps = telFromList . zipWith ren (map namedArg ps ++ repeat dummy) . telToList where dummy = A.WildP __IMPOSSIBLE__ ren (A.VarP x) (Dom info (_, a)) | visible info && not (isNoName x) = Dom info (nameToArgName x, a) -- Andreas, 2013-03-13: inserted the following line in the hope to fix issue 819 -- but it does not do the job, instead, it puts a lot of "_"s -- instead of more sensible names into error messages. -- ren A.WildP{} (Dom info (_, a)) | visible info = Dom info ("_", a) ren A.PatternSynP{} _ = __IMPOSSIBLE__ -- ensure there are no syns left -- Andreas, 2016-05-10, issue 1848: if context variable has no name, call it "x" ren _ (Dom info (x, a)) | visible info && isNoName x = Dom info (stringToArgName "x", a) ren _ a = a useOriginFrom :: (LensOrigin a, LensOrigin b) => [a] -> [b] -> [a] useOriginFrom = zipWith $ \x y -> setOrigin (getOrigin y) x -- | Are there any untyped user patterns left? noProblemRest :: Problem -> Bool noProblemRest (Problem _ _ _ (ProblemRest ps _)) = null ps -- | Construct an initial 'split' 'Problem' from user patterns. -- Example: -- @ -- -- Case : {A : Set} → Maybe A → Set → Set → Set -- Case nothing B C = B -- Case (just _) B C = C -- -- sample : {A : Set} (m : Maybe A) → Case m Bool (Maybe A → Bool) -- sample (just a) (just b) = true -- sample (just a) nothing = false -- sample nothing = true -- @ -- The problem generated for the first clause of @sample@ -- with patterns @just a, just b@ would be: -- @ -- problemInPat = ["_", "just a"] -- problemOutPat = ["A", "m"] -- problemTel = [A : Set, m : Maybe A] -- problemRest = -- restPats = ["just b"] -- restType = "Case m Bool (Maybe A -> Bool)" -- @ problemFromPats :: [NamedArg A.Pattern] -- ^ The user patterns. -> Type -- ^ The type the user patterns eliminate. -> TCM Problem -- ^ The initial problem constructed from the user patterns. problemFromPats ps0 a = do -- Andreas, 2017-01-18, issue #819: We set all A.WildP origins to Inserted -- in order to guide the pattern printer to discard variable names it made up. let ps = (`mapNamedArgPattern` ps0) $ \case p | A.WildP{} <- namedArg p -> setOrigin Inserted p p -> p -- For the initial problem, do not insert trailing implicits. -- This has the effect of not including trailing hidden domains in the problem telescope. -- In all later call to insertImplicitPatterns, we can then use ExpandLast. -- Ulf, 2016-04-25: Actually we do need to ExpandLast because where blocks -- need the implicits. ps <- insertImplicitPatternsT ExpandLast ps a reportSDoc "tc.lhs.imp" 20 $ text "insertImplicitPatternsT returned" <+> fsep (map prettyA ps) -- Redo the telView, in order to *not* normalize the clause type further than necessary. -- (See issue 734.) TelV tel0 b <- telViewUpTo (length ps) a let gamma = useNamesFromPattern ps tel0 as = telToList gamma (ps1,ps2) = splitAt (size as) ps -- now (gamma -> b) = a and |gamma| = |ps1| pr = ProblemRest ps2 $ defaultArg b -- internal patterns start as all variables let ips = teleNamedArgs gamma `useOriginFrom` ps -- the initial problem for starting the splitting problem = Problem ps1 ips gamma pr :: Problem reportSDoc "tc.lhs.problem" 10 $ vcat [ text "checking lhs -- generated an initial split problem:" , nest 2 $ vcat [ text "ps =" <+> fsep (map prettyA ps) , text "a =" <+> prettyTCM a , text "xs =" <+> text (show $ map (fst . unDom) as) , text "ps1 =" <+> fsep (map prettyA ps1) -- , text "ips =" <+> prettyTCM ips -- no prettyTCM instance , text "gamma =" <+> prettyTCM gamma , text "ps2 =" <+> fsep (map prettyA ps2) , text "b =" <+> addContext gamma (prettyTCM b) ] ] return problem -- | Try to move patterns from the problem rest into the problem. -- Possible if type of problem rest has been updated to a function type. updateProblemRest_ :: Problem -> TCM (Nat, Problem) updateProblemRest_ p@(Problem ps0 qs0 tel0 (ProblemRest ps a)) = do ps <- insertImplicitPatternsT ExpandLast ps $ unArg a reportSDoc "tc.lhs.imp" 20 $ text "insertImplicitPatternsT returned" <+> fsep (map prettyA ps) -- (Issue 734: Do only the necessary telView to preserve clause types as much as possible.) TelV tel b <- telViewUpTo (length ps) $ unArg a let gamma = useNamesFromPattern ps tel as = telToList gamma (ps1,ps2) = splitAt (size as) ps tel1 = telFromList $ telToList tel0 ++ as pr = ProblemRest ps2 (a $> b) qs1 = teleNamedArgs gamma `useOriginFrom` ps n = size as reportSDoc "tc.lhs.problem" 10 $ addContext tel0 $ vcat [ text "checking lhs -- updated split problem:" , nest 2 $ vcat [ text "ps =" <+> fsep (map prettyA ps) , text "a =" <+> prettyTCM a , text "xs =" <+> text (show $ map (fst . unDom) as) , text "ps1 =" <+> fsep (map prettyA ps1) , text "gamma =" <+> prettyTCM gamma , text "ps2 =" <+> fsep (map prettyA ps2) , text "b =" <+> addContext gamma (prettyTCM b) ] ] return $ (n,) $ Problem (ps0 ++ ps1) (applySubst (raiseS n) qs0 ++ qs1) tel1 pr updateProblemRest :: LHSState -> TCM LHSState updateProblemRest st@LHSState { lhsProblem = p } = do (n, p') <- updateProblemRest_ p if (n == 0) then return st else do let tau = raiseS n return $ LHSState { lhsProblem = p' , lhsDPI = applyPatSubst tau (lhsDPI st) , lhsShouldBeEmptyTypes = map (second $ applyPatSubst tau) (lhsShouldBeEmptyTypes st) } Agda-2.5.3/src/full/Agda/TypeChecking/Rules/LHS/AsPatterns.hs0000644000000000000000000000673613154613124021661 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Rules.LHS.AsPatterns ( recoverAsPatterns ) where import Control.Applicative import Control.Monad.Writer hiding ((<>)) import qualified Data.Foldable as Fold import Agda.Syntax.Common import Agda.Syntax.Concrete () import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.Syntax.Abstract.Pattern ( containsAsPattern ) import qualified Agda.Syntax.Abstract as A import Agda.TypeChecking.Monad import Agda.TypeChecking.Telescope import Agda.TypeChecking.Rules.LHS.Problem import Agda.TypeChecking.Rules.LHS.Implicit import Agda.TypeChecking.Datatypes import Agda.TypeChecking.Records import Agda.TypeChecking.Substitute import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce import Agda.Utils.Functor import Agda.Utils.Maybe import Agda.Utils.Size import Agda.Utils.Impossible #include "undefined.h" recoverAsPatterns :: Telescope -> Type -> Term -> [NamedArg A.Pattern] -> [NamedArg DeBruijnPattern] -> TCM [AsBinding] recoverAsPatterns delta a self ps qs = do let es = patternsToElims qs as <- typeElims (raise (size delta) a) self es ps <- insertImplicitPatternsT DontExpandLast ps a reportSDoc "tc.lhs.as" 30 $ vcat [ text "recovering as patterns" , nest 2 $ vcat [ text "es =" <+> prettyList (map prettyTCM es) , text "as =" <+> prettyList (map prettyTCM as) , text "ps =" <+> prettyList (map prettyA ps) ] ] execWriterT $ asPatterns as ps es asPatterns :: [ElimType] -> [NamedArg A.Pattern] -> [Elim] -> WriterT [AsBinding] TCM () asPatterns _ [] _ = return () asPatterns (ProjT _ a : as) (p : ps) (Proj{} : vs) = do unless (isJust $ A.maybePostfixProjP p) __IMPOSSIBLE__ -- sanity check ps <- lift $ insertImplicitPatternsT DontExpandLast ps a asPatterns as ps vs asPatterns (ArgT dom : as) (p : ps) (Apply v : vs) | not $ containsAsPattern p = asPatterns as ps vs | otherwise = do let a = unDom dom case namedArg p of A.AsP _ x p' -> do tell [AsB x (unArg v) a] asPatterns (ArgT dom : as) (fmap (p' <$) p : ps) (Apply v : vs) A.ConP _ _ ps' -> do (_, _, tel, as', args) <- lift $ conPattern a (unArg v) ps' <- lift $ insertImplicitPatterns ExpandLast ps' tel asPatterns (map ArgT as' ++ as) (ps' ++ ps) (map Apply args ++ vs) A.RecP i fps -> do (r, c, _, as', args) <- lift $ conPattern a (unArg v) let fs = zipWith (<$) (map (nameConcrete . qnameName) $ conFields c) args ps' <- lift $ insertMissingFields r (const $ A.WildP i) fps fs asPatterns (map ArgT as' ++ as) (ps' ++ ps) (map Apply args ++ vs) A.DefP{} -> __IMPOSSIBLE__ -- ? _ -> __IMPOSSIBLE__ asPatterns _ _ _ = __IMPOSSIBLE__ -- | Given a fully applied constructor term and its type, -- deconstruct it and return, amongst others, the types of its arguments. conPattern :: Type -- ^ Type need not be reduced. -> Term -- ^ Fully applied constructor. -> TCM (QName, ConHead, Telescope, [Dom Type], Args) -- ^ Data/record type name, -- constructor name, -- argument telescope, -- types of arguments. -- arguments. conPattern a (Con c ci args) = do -- @getFullyAppliedConType@ works since @c@ is fully applied. ((d, _, _), ca) <- fromMaybe __IMPOSSIBLE__ <.> getFullyAppliedConType c =<< reduce a TelV tel _ <- telView ca let as = fromMaybe __IMPOSSIBLE__ $ typeArgsWithTel tel $ map unArg args return (d, c, tel, as, args) conPattern _ _ = __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/Rules/LHS/Instantiate.hs0000644000000000000000000001471313154613124022052 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Rules.LHS.Instantiate where import Agda.Syntax.Common import Agda.Syntax.Internal as I hiding (Substitution) import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Views ( asView ) import Agda.TypeChecking.Monad import Agda.TypeChecking.Substitute hiding (Substitution) import qualified Agda.TypeChecking.Substitute as S (Substitution) import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce import Agda.TypeChecking.Telescope import Agda.TypeChecking.Rules.LHS.Problem import Agda.Utils.List import Agda.Utils.Permutation import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | Instantiate a telescope with a substitution. Might reorder the telescope. -- @instantiateTel (Γ : Tel)(σ : Γ --> Γ) = Γσ~@ -- Monadic only for debugging purposes. instantiateTel :: Substitution -> Telescope -> TCM (Telescope, Permutation, S.Substitution, [Dom Type]) instantiateTel s tel = liftTCM $ do reportSDoc "tc.lhs.inst" 10 $ vcat [ text "instantiateTel " , nest 2 $ text "s =" <+> do addContext tel $ fsep $ punctuate comma $ map (maybe (text "_") prettyTCM) s , nest 2 $ text "tel =" <+> prettyTCM tel -- , nest 2 $ text "tel =" <+> text (show tel) ] {- -- Andreas, 2013-10-27 -- Why is normalization necessary? Issue 234 seems to need it. -- But it is better done right before where it is needed (see below). tel <- normalise tel reportSDoc "tc.lhs.inst" 15 $ vcat [ nest 2 $ text "tel (normalized)=" <+> prettyTCM tel ] -} -- Shrinking permutation (removing Justs) (and its complement, and reverse) let n = size s {- OLD CODE, leave as documentation ps = Perm n [ i | (i, Nothing) <- zip [0..] $ reverse s ] psR = reverseP ps psC = Perm n [ i | (i, Just _) <- zip [0..] $ reverse s ] -} deal (i, Nothing) = Left i deal (i, Just _ ) = Right i (is, isC) = mapEither deal $ zip [0..] $ reverse s ps = Perm n is psR = reverseP ps psC = Perm n isC reportSDoc "tc.lhs.inst" 10 $ vcat [ nest 2 $ text $ "ps = " ++ show ps , nest 2 $ text $ "psR = " ++ show psR , nest 2 $ text $ "psC = " ++ show psC ] -- s' : Substitution Γσ let s' = {-'-} renameP __IMPOSSIBLE__ psR s reportSDoc "tc.lhs.inst" 15 $ nest 2 $ text "s' =" <+> fsep (punctuate comma $ map (maybe (text "_") prettyTCM) s') -- rho : [Tm Γσ]Γ let rho = mkSubst s' reportSDoc "tc.lhs.inst" 15 $ nest 2 $ text "rho = " <+> text (show rho) -- tel1 : [Type Γ]Γ let tel1 = flattenTel tel names1 = teleNames tel reportSDoc "tc.lhs.inst" 15 $ nest 2 $ vcat [ text "tel1 =" <+> brackets (fsep $ punctuate comma $ map prettyTCM tel1) -- , text "tel1 =" <+> text (show tel1) ] -- tel2 : [Type Γσ]Γ let tel2 = applySubst rho tel1 reportSDoc "tc.lhs.inst" 15 $ nest 2 $ text "tel2 =" <+> brackets (fsep $ punctuate comma $ map prettyTCM tel2) -- tel3 : [Type Γσ]Γσ -- -- Andreas, 2013-10-27 -- @reorderTel@ below uses free variable analysis, so @tel3@ should be -- fully instantiated and normalized. (See issue 234.) -- Ulf, 2014-02-05: Only normalise if reordering fails! tel3 <- instantiateFull $ permute ps tel2 let names3 = permute ps names1 reportSDoc "tc.lhs.inst" 15 $ nest 2 $ text "tel3 =" <+> brackets (fsep $ punctuate comma $ map prettyTCM tel3) -- Raise error if telescope cannot be ordered. let failToReorder = inTopContext $ addContext names3 $ do err <- sep [ text "Recursive telescope in left hand side:" , fsep [ parens (text (argNameToString x) <+> text ":" <+> prettyTCM t) | (x, t) <- zip names3 tel3 ] ] typeError $ GenericError $ show err tryNormalisedReorder = do tel3 <- normalise tel3 reportSDoc "tc.lhs.inst" 30 $ text "failed to reorder unnormalised, trying again with" $$ nest 2 (text "norm =" <+> brackets (fsep $ punctuate comma $ map prettyTCM tel3)) p <- maybe failToReorder return . reorderTel =<< normalise tel3 return (p, tel3) -- p : Permutation (Γσ -> Γσ ~) (p, tel3) <- maybe tryNormalisedReorder (\p -> return (p, tel3)) $ reorderTel tel3 reportSLn "tc.lhs.inst" 10 $ " p = " ++ show p -- rho' : [Term Γσ~]Γσ let rho' = {-'-} renaming __IMPOSSIBLE__ (reverseP p) -- tel4 : [Type Γσ~]Γσ~ let tel4 = applySubst rho' (permute p tel3) names4 = permute p names3 reportSDoc "tc.lhs.inst" 15 $ nest 2 $ text "tel4 =" <+> brackets (fsep $ punctuate comma $ map prettyTCM tel4) -- tel5 = Γσ~ let tel5 = unflattenTel names4 tel4 reportSDoc "tc.lhs.inst" 15 $ nest 2 $ text "tel5 =" <+> prettyTCM tel5 -- remember the types of the instantiations -- itypes : [Type Γσ~]Γ* let itypes = applySubst rho' $ permute psC tel2 return (tel5, composeP p ps, applySubst rho' rho, itypes) where -- Turn a Substitution ([Maybe Term]) into a substitution (S.Substitution) mkSubst :: [Maybe Term] -> S.Substitution mkSubst s = rho 0 s' where s' = s rho i (Nothing : s) = consS (var i) $ rho (i + 1) s rho i (Just u : s) = consS u $ rho i s rho i [] = raiseS i -- | Produce a nice error message when splitting failed nothingToSplitError :: Problem -> TCM a nothingToSplitError (Problem ps _ tel pr) = splitError ps tel where splitError [] EmptyTel = do if null $ restPats pr then __IMPOSSIBLE__ else do typeError $ GenericError $ "Arguments left we cannot split on. TODO: better error message" splitError (_:_) EmptyTel = __IMPOSSIBLE__ splitError [] ExtendTel{} = __IMPOSSIBLE__ splitError (p : ps) (ExtendTel a tel) | isBad p = traceCall (CheckPattern (strip p) EmptyTel (unDom a)) $ typeError $ IlltypedPattern (strip p) (unDom a) | otherwise = underAbstraction a tel $ \tel -> splitError ps tel where strip = snd . asView . namedArg isBad p = case strip p of A.DotP{} -> True A.ConP{} -> True A.LitP{} -> True A.RecP{} -> True A.VarP{} -> False A.WildP{} -> False A.AbsurdP{} -> False A.ProjP{} -> __IMPOSSIBLE__ -- Projection pattern gives CannotEliminateWithPattern A.DefP{} -> __IMPOSSIBLE__ A.AsP{} -> __IMPOSSIBLE__ A.PatternSynP{} -> __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/Rules/LHS/Implicit.hs0000644000000000000000000001435613154613124021344 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Rules.LHS.Implicit where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad (forM) import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Syntax.Info import Agda.Syntax.Internal as I import Agda.Syntax.Abstract (IsProjP(..)) import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Translation.InternalToAbstract (reify) import Agda.TypeChecking.Monad import Agda.TypeChecking.Implicit import Agda.TypeChecking.Substitute import Agda.TypeChecking.Pretty import Agda.TypeChecking.Records import Agda.TypeChecking.Reduce import Agda.TypeChecking.Telescope import Agda.TypeChecking.Rules.LHS.Problem import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.Maybe import Agda.Utils.Monad #include "undefined.h" import Agda.Utils.Impossible -- | Insert implicit patterns in a problem. insertImplicitProblem :: Problem -> TCM Problem insertImplicitProblem (Problem ps qs tel pr) = do reportSDoc "tc.lhs.imp" 15 $ sep [ text "insertImplicits" , nest 2 $ text "ps = " <+> do brackets $ fsep $ punctuate comma $ map prettyA ps , nest 2 $ text "tel = " <+> prettyTCM tel ] ps' <- insertImplicitPatterns ExpandLast ps tel reportSDoc "tc.lhs.imp" 15 $ sep [ text "insertImplicits finished" , nest 2 $ text "ps' = " <+> do brackets $ fsep $ punctuate comma $ map prettyA ps' ] return $ Problem ps' qs tel pr -- | Eta-expand implicit pattern if of record type. expandImplicitPattern :: Type -> NamedArg A.Pattern -> TCM (NamedArg A.Pattern) expandImplicitPattern a p = maybe (return p) return =<< expandImplicitPattern' a p -- | Try to eta-expand implicit pattern. -- Returns 'Nothing' unless dealing with a record type that has eta-expansion -- and a constructor @c@. In this case, it returns 'Just' @c _ _ ... _@ -- (record constructor applied to as many implicit patterns as there are fields). expandImplicitPattern' :: Type -> NamedArg A.Pattern -> TCM (Maybe (NamedArg A.Pattern)) expandImplicitPattern' a p | A.WildP{} <- namedArg p, not (isInstance p) = do -- Eta expand implicit patterns of record type (issue 473), -- but not instance arguments since then they won't be found -- by the instance search caseMaybeM (isEtaRecordType =<< reduce a) (return Nothing) $ \ (d, _) -> do -- Andreas, 2012-06-10: only expand guarded records, -- otherwise we might run into an infinite loop def <- getRecordDef d -- Andreas, 2015-07-20 Since we have record patterns now, we can expand -- even records that do not have a constructor. -- -- Andreas, 2013-03-21: only expand records that have a constructor: -- if not (recNamedCon def) then return Nothing else do do -- generate one implicit pattern for each field let qs = for (recFields def) $ \ f -> implicitP $ argInfo f -- generate the pattern (c _ _ ... _) let q = A.ConP (ConPatInfo ConOSystem patNoRange) (A.AmbQ [recCon def]) qs -- equip it with the name/arginfo of the original implicit pattern p' = updateNamedArg (const q) p -- WAS: ((q <$) <$> p) -- Andreas, 2013-03-21 forbiddingly cryptic return $ Just p' | otherwise = return Nothing implicitP :: ArgInfo -> NamedArg A.Pattern implicitP info = Arg (setOrigin Inserted info) $ unnamed $ A.WildP $ PatRange $ noRange -- | Insert implicit patterns in a list of patterns. -- Even if 'DontExpandLast', trailing SIZELT patterns are inserted. insertImplicitPatterns :: ExpandHidden -> [NamedArg A.Pattern] -> Telescope -> TCM [NamedArg A.Pattern] insertImplicitPatterns exh ps tel = insertImplicitPatternsT exh ps (telePi tel typeDontCare) -- | Insert trailing SizeLt patterns, if any. insertImplicitSizeLtPatterns :: Type -> TCM [NamedArg A.Pattern] insertImplicitSizeLtPatterns t = do -- Testing for SizeLt. In case of blocked type, we return no. -- We assume that on the LHS, we know the type. (TODO: Sufficient?) isSize <- isSizeTypeTest let isBounded BoundedNo = False isBounded BoundedLt{} = True isSizeLt t = maybe False isBounded . isSize . unEl <$> reduce t -- Search for the last SizeLt type among the hidden arguments. TelV tel _ <- telView t let ts = reverse $ takeWhile (not . visible) $ telToList tel keep <- reverse <$> dropWhileM (not <.> isSizeLt . snd . unDom) ts -- Insert implicit patterns upto (including) the last SizeLt type. return [ implicitP ai | Dom ai _ <- keep ] -- | Insert implicit patterns in a list of patterns. -- Even if 'DontExpandLast', trailing SIZELT patterns are inserted. insertImplicitPatternsT :: ExpandHidden -> [NamedArg A.Pattern] -> Type -> TCM [NamedArg A.Pattern] insertImplicitPatternsT DontExpandLast [] a = insertImplicitSizeLtPatterns a insertImplicitPatternsT exh ps a = do TelV tel b <- telViewUpTo' (-1) (not . visible) a reportSDoc "tc.lhs.imp" 20 $ sep [ text "insertImplicitPatternsT" , nest 2 $ text "ps = " <+> do brackets $ fsep $ punctuate comma $ map prettyA ps , nest 2 $ text "tel = " <+> prettyTCM tel , nest 2 $ text "b = " <+> addContext tel (prettyTCM b) ] case ps of [] -> insImp dummy tel p : ps -> do -- Andreas, 2015-05-11. -- If p is a projection pattern, make it visible for the purpose of -- calling insImp / insertImplicit, to get correct behavior. let p' = applyWhen (isJust $ isProjP p) (setHiding NotHidden) p hs <- insImp p' tel case hs of [] -> do a <- reduce a case ignoreSharing $ unEl a of Pi arg b -> do (p :) <$> insertImplicitPatternsT exh ps (absBody b) _ -> return (p : ps) hs -> insertImplicitPatternsT exh (hs ++ p : ps) (telePi tel b) where dummy = defaultNamedArg (A.VarP __IMPOSSIBLE__) insImp p EmptyTel = return [] insImp p tel = case insertImplicit p $ map (argFromDom . fmap fst) $ telToList tel of BadImplicits -> typeError WrongHidingInLHS NoSuchName x -> typeError WrongHidingInLHS ImpInsert n -> return $ map implicitArg n NoInsertNeeded -> return [] implicitArg h = implicitP $ setHiding h $ defaultArgInfo Agda-2.5.3/src/full/Agda/TypeChecking/Rules/LHS/Unify.hs0000644000000000000000000013116513154613124020662 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE NondecreasingIndentation #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE ViewPatterns #-} -- | Unification algorithm for specializing datatype indices, as described in -- \"Unifiers as Equivalences: Proof-Relevant Unification of Dependently -- Typed Data\" by Jesper Cockx, Dominique Devriese, and Frank Piessens -- (ICFP 2016). -- -- This is the unification algorithm used for checking the left-hand side -- of clauses (see @Agda.TypeChecking.Rules.LHS@), coverage checking (see -- @Agda.TypeChecking.Coverage@) and indirectly also for interactive case -- splitting (see @Agda.Interaction.MakeCase@). -- -- A unification problem (of type @UnifyState@) consists of: -- 1. A telescope @varTel@ of free variables, some or all of which are -- flexible (as indicated by @flexVars@). -- 2. A telescope @eqTel@ containing the types of the equations. -- 3. Left- and right-hand sides for each equation: -- @varTel ⊢ eqLHS : eqTel@ and @varTel ⊢ eqRHS : eqTel@. -- -- The unification algorithm can end in three different ways: -- (type @UnificationResult@): -- - A *positive success* @Unifies (tel, sigma, ps)@ with @tel ⊢ sigma : varTel@, -- @tel ⊢ eqLHS [ varTel ↦ sigma ] ≡ eqRHS [ varTel ↦ sigma ] : eqTel@, -- and @tel ⊢ ps : eqTel@. In this case, @sigma;ps@ is an *equivalence* -- between the telescopes @tel@ and @varTel(eqLHS ≡ eqRHS)@. -- - A *negative success* @NoUnify err@ means that a conflicting equation -- was found (e.g an equation between two distinct constructors or a cycle). -- - A *failure* @DontKnow err@ means that the unifier got stuck. -- -- The unification algorithm itself consists of two parts: -- 1. A *unification strategy* takes a unification problem and produces a -- list of suggested unification rules (of type @UnifyStep@). Strategies -- can be constructed by composing simpler strategies (see for example the -- definition of @completeStrategyAt@). -- 2. The *unification engine* @unifyStep@ takes a unification rule and tries -- to apply it to the given state, writing the result to the UnifyOutput -- on a success. -- -- The unification steps (of type @UnifyStep@) are the following: -- - *Deletion* removes a reflexive equation @u =?= v : a@ if the left- and -- right-hand side @u@ and @v@ are (definitionally) equal. This rule results -- in a failure if --without-K is enabled (see \"Pattern Matching Without K\" -- by Jesper Cockx, Dominique Devriese, and Frank Piessens (ICFP 2014). -- - *Solution* solves an equation if one side is (eta-equivalent to) a -- flexible variable. In case both sides are flexible variables, the -- unification strategy makes a choice according to the @chooseFlex@ -- function in @Agda.TypeChecking.Rules.LHS.Problem@. -- - *Injectivity* decomposes an equation of the form -- @c us =?= c vs : D pars is@ where @c : Δc → D pars js@ is a constructor -- of the inductive datatype @D@ into a sequence of equations -- @us =?= vs : delta@. In case @D@ is an indexed datatype, -- *higher-dimensional unification* is applied (see below). -- - *Conflict* detects absurd equations of the form -- @c₁ us =?= c₂ vs : D pars is@ where @c₁@ and @c₂@ are two distinct -- constructors of the datatype @D@. -- - *Cycle* detects absurd equations of the form @x =?= v : D pars is@ where -- @x@ is a variable of the datatype @D@ that occurs strongly rigid in @v@. -- - *EtaExpandVar* eta-expands a single flexible variable @x : R@ where @R@ -- is a (eta-expandable) record type, replacing it by one variable for each -- field of @R@. -- - *EtaExpandEquation* eta-expands an equation @u =?= v : R@ where @R@ is a -- (eta-expandable) record type, replacing it by one equation for each field -- of @R@. The left- and right-hand sides of these equations are the -- projections of @u@ and @v@. -- - *LitConflict* detects absurd equations of the form @l₁ =?= l₂ : A@ where -- @l₁@ and @l₂@ are distinct literal terms. -- - *StripSizeSuc* simplifies an equation of the form -- @sizeSuc x =?= sizeSuc y : Size@ to @x =?= y : Size@. -- - *SkipIrrelevantEquation@ removes an equation between irrelevant terms. -- - *TypeConInjectivity* decomposes an equation of the form -- @D us =?= D vs : Set i@ where @D@ is a datatype. This rule is only used -- if --injective-type-constructors is enabled. -- -- Higher-dimensional unification (new, does not yet appear in any paper): -- If an equation of the form @c us =?= c vs : D pars is@ is encountered where -- @c : Δc → D pars js@ is a constructor of an indexed datatype -- @D pars : Φ → Set ℓ@, it is in general unsound to just simplify this -- equation to @us =?= vs : Δc@. For this reason, the injectivity rule in the -- paper restricts the indices @is@ to be distinct variables that are bound in -- the telescope @eqTel@. But we can be more general by introducing new -- variables @ks@ to the telescope @eqTel@ and equating these to @is@: -- @ -- Δ₁(x : D pars is)Δ₂ -- ≃ -- Δ₁(ks : Φ)(x : D pars ks)(ps : is ≡Φ ks)Δ₂ -- @ -- Since @ks@ are distinct variables, it's now possible to apply injectivity -- to the equation @x@, resulting in the following new equation telescope: -- @ -- Δ₁(ys : Δc)(ps : is ≡Φ js[Δc ↦ ys])Δ₂ -- @ -- Now we can solve the equations @ps@ by recursively calling the unification -- algorithm with flexible variables @Δ₁(ys : Δc)@. This is called -- *higher-dimensional unification* since we are unifying equality proofs -- rather than terms. If the higher-dimensional unification succeeds, the -- resulting telescope serves as the new equation telescope for the original -- unification problem. module Agda.TypeChecking.Rules.LHS.Unify ( UnificationResult , UnificationResult'(..) , unifyIndices ) where import Prelude hiding (null) import Control.Arrow ((***)) import Control.Applicative hiding (empty) import Control.Monad import Control.Monad.Plus import Control.Monad.State import Control.Monad.Trans.Maybe import Control.Monad.Reader import Control.Monad.Writer (WriterT(..), MonadWriter(..), Monoid(..)) import Data.Map (Map) import qualified Data.Map as Map import Data.Semigroup hiding (Arg) import qualified Data.List as List import Data.Typeable (Typeable) import Data.Foldable (Foldable) import Data.Traversable (Traversable,traverse) import qualified Data.Traversable as Trav import Agda.Interaction.Options (optInjectiveTypeConstructors) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.Syntax.Literal import Agda.Syntax.Position import Agda.TypeChecking.Monad import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TypeChecking.Monad.Builtin (constructorForm) import Agda.TypeChecking.Conversion -- equalTerm import Agda.TypeChecking.Constraints import Agda.TypeChecking.Datatypes import Agda.TypeChecking.DropArgs import Agda.TypeChecking.Level (reallyUnLevelView) import Agda.TypeChecking.Reduce import qualified Agda.TypeChecking.Patterns.Match as Match import Agda.TypeChecking.Pretty hiding ((<>)) import Agda.TypeChecking.SizedTypes (compareSizes) import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Free import Agda.TypeChecking.Records import Agda.TypeChecking.MetaVars (assignV, newArgsMetaCtx) import Agda.TypeChecking.EtaContract import Agda.Interaction.Options (optInjectiveTypeConstructors, optWithoutK) import Agda.TypeChecking.Rules.LHS.Problem hiding (Substitution) -- import Agda.TypeChecking.SyntacticEquality import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.Either import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.ListT import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | Result of 'unifyIndices'. type UnificationResult = UnificationResult' ( Telescope -- @tel@ , PatternSubstitution -- @sigma@ s.t. @tel ⊢ sigma : varTel@ , [NamedArg DeBruijnPattern] -- @ps@ s.t. @tel ⊢ ps : eqTel @ ) data UnificationResult' a = Unifies a -- ^ Unification succeeded. | NoUnify NegativeUnification -- ^ Terms are not unifiable. | DontKnow [UnificationFailure] -- ^ Some other error happened, unification got stuck. deriving (Typeable, Show, Functor, Foldable, Traversable) -- | Unify indices. -- -- In @unifyIndices gamma flex us vs@, -- -- @us@ and @vs@ are the argument lists to unify, -- -- @gamma@ is the telescope of free variables in @us@ and @vs@. -- -- @flex@ is the set of flexible (instantiable) variabes in @us@ and @vs@. -- -- The result is the most general unifier of @us@ and @vs@. unifyIndices :: MonadTCM tcm => Telescope -> FlexibleVars -> Type -> Args -> Args -> tcm UnificationResult unifyIndices tel flex a [] [] = return $ Unifies (tel, idS, []) unifyIndices tel flex a us vs = liftTCM $ Bench.billTo [Bench.Typing, Bench.CheckLHS, Bench.UnifyIndices] $ do reportSDoc "tc.lhs.unify" 10 $ sep [ text "unifyIndices" , nest 2 $ prettyTCM tel , nest 2 $ addContext tel $ text $ show $ map flexVar flex , nest 2 $ addContext tel $ parens (prettyTCM a) , nest 2 $ addContext tel $ prettyList $ map prettyTCM us , nest 2 $ addContext tel $ prettyList $ map prettyTCM vs ] initialState <- initUnifyState tel flex a us vs reportSDoc "tc.lhs.unify" 20 $ text "initial unifyState:" <+> prettyTCM initialState reportSDoc "tc.lhs.unify" 70 $ text "initial unifyState:" <+> text (show initialState) (result,output) <- runUnifyM $ unify initialState rightToLeftStrategy let ps = applySubst (unifyProof output) $ teleNamedArgs (eqTel initialState) return $ fmap (\s -> (varTel s , unifySubst output , ps)) result ---------------------------------------------------- -- Equalities ---------------------------------------------------- data Equality = Equal { eqType :: Type , eqLeft :: Term , eqRight :: Term } instance Reduce Equality where reduce' (Equal a u v) = Equal <$> reduce' a <*> reduce' u <*> reduce' v eqConstructorForm :: Equality -> TCM Equality eqConstructorForm (Equal a u v) = Equal a <$> constructorForm u <*> constructorForm v eqUnLevel :: Equality -> TCM Equality eqUnLevel (Equal a u v) = Equal a <$> unLevel u <*> unLevel v where unLevel (Level l) = reallyUnLevelView l unLevel u = return u ---------------------------------------------------- -- Unify state ---------------------------------------------------- data UnifyState = UState { varTel :: Telescope , flexVars :: FlexibleVars , eqTel :: Telescope , eqLHS :: [Arg Term] , eqRHS :: [Arg Term] } deriving (Show) instance Reduce UnifyState where reduce' (UState var flex eq lhs rhs) = UState <$> reduce' var <*> pure flex <*> reduce' eq <*> reduce' lhs <*> reduce' rhs reduceVarTel :: UnifyState -> TCM UnifyState reduceVarTel s@UState{ varTel = tel } = do tel <- reduce tel return $ s { varTel = tel } reduceEqTel :: UnifyState -> TCM UnifyState reduceEqTel s@UState{ eqTel = tel } = do tel <- reduce tel return $ s { eqTel = tel } instance Normalise UnifyState where normalise' (UState var flex eq lhs rhs) = UState <$> normalise' var <*> pure flex <*> normalise' eq <*> normalise' lhs <*> normalise' rhs normaliseVarTel :: UnifyState -> TCM UnifyState normaliseVarTel s@UState{ varTel = tel } = do tel <- normalise tel return $ s { varTel = tel } normaliseEqTel :: UnifyState -> TCM UnifyState normaliseEqTel s@UState{ eqTel = tel } = do tel <- normalise tel return $ s { eqTel = tel } instance PrettyTCM UnifyState where prettyTCM state = text "UnifyState" $$ nest 2 (vcat $ [ text "variable tel: " <+> prettyTCM gamma , text "flexible vars: " <+> prettyTCM (map flexVar $ flexVars state) , text "equation tel: " <+> addContext gamma (prettyTCM delta) , text "equations: " <+> addContext gamma (prettyList_ (zipWith prettyEquality (eqLHS state) (eqRHS state))) ]) where gamma = varTel state delta = eqTel state prettyEquality x y = prettyTCM x <+> text "=?=" <+> prettyTCM y initUnifyState :: Telescope -> FlexibleVars -> Type -> Args -> Args -> TCM UnifyState initUnifyState tel flex a lhs rhs = do let n = size lhs unless (n == size rhs) __IMPOSSIBLE__ TelV eqTel _ <- telView a unless (n == size eqTel) __IMPOSSIBLE__ reduce $ UState tel flex eqTel lhs rhs isUnifyStateSolved :: UnifyState -> Bool isUnifyStateSolved = null . eqTel varCount :: UnifyState -> Int varCount = size . varTel -- | Get the type of the i'th variable in the given state getVarType :: Int -> UnifyState -> Type getVarType i s = if i < 0 then __IMPOSSIBLE__ else unDom $ (flattenTel $ varTel s) !! i getVarTypeUnraised :: Int -> UnifyState -> Type getVarTypeUnraised i s = if i < 0 then __IMPOSSIBLE__ else snd . unDom $ (telToList $ varTel s) !! i eqCount :: UnifyState -> Int eqCount = size . eqTel -- | Get the k'th equality in the given state. The left- and right-hand sides -- of the equality live in the varTel telescope, and the type of the equality -- lives in the varTel++eqTel telescope getEquality :: Int -> UnifyState -> Equality getEquality k UState { eqTel = eqs, eqLHS = lhs, eqRHS = rhs } = if k < 0 then __IMPOSSIBLE__ else Equal (unDom $ (flattenTel eqs) !! k) (unArg $ lhs !! k) (unArg $ rhs !! k) -- | As getEquality, but with the unraised type getEqualityUnraised :: Int -> UnifyState -> Equality getEqualityUnraised k UState { eqTel = eqs, eqLHS = lhs, eqRHS = rhs } = if k < 0 then __IMPOSSIBLE__ else Equal (snd . unDom $ (telToList eqs) !! k) (unArg $ lhs !! k) (unArg $ rhs !! k) getEqInfo :: Int -> UnifyState -> ArgInfo getEqInfo k UState { eqTel = eqs } = if k < 0 then __IMPOSSIBLE__ else domInfo $ telToList eqs !! k -- | Add a list of equations to the front of the equation telescope addEqs :: Telescope -> [Arg Term] -> [Arg Term] -> UnifyState -> UnifyState addEqs tel us vs s = s { eqTel = tel `abstract` eqTel s , eqLHS = us ++ eqLHS s , eqRHS = vs ++ eqRHS s } where k = size tel addEq :: Type -> Arg Term -> Arg Term -> UnifyState -> UnifyState addEq a u v = addEqs (ExtendTel (defaultDom a) (Abs underscore EmptyTel)) [u] [v] -- | Instantiate the k'th variable with the given value. -- Returns Nothing if there is a cycle. solveVar :: Int -> Term -> UnifyState -> Maybe (UnifyState, PatternSubstitution) solveVar k u s = case instantiateTelescope (varTel s) k u of Nothing -> Nothing Just (tel' , sigma , rho) -> Just $ (,sigma) $ UState { varTel = tel' , flexVars = permuteFlex (reverseP rho) $ flexVars s , eqTel = applyPatSubst sigma $ eqTel s , eqLHS = applyPatSubst sigma $ eqLHS s , eqRHS = applyPatSubst sigma $ eqRHS s } where permuteFlex :: Permutation -> FlexibleVars -> FlexibleVars permuteFlex perm = mapMaybe $ \(FlexibleVar h o k p x) -> FlexibleVar h o k p <$> List.findIndex (x==) (permPicks perm) applyUnder :: Int -> Telescope -> Term -> Telescope applyUnder k tel u | k < 0 = __IMPOSSIBLE__ | k == 0 = tel `apply1` u | otherwise = case tel of EmptyTel -> __IMPOSSIBLE__ ExtendTel a tel' -> ExtendTel a $ Abs (absName tel') $ applyUnder (k-1) (absBody tel') u dropAt :: Int -> [a] -> [a] dropAt _ [] = __IMPOSSIBLE__ dropAt k (x:xs) | k < 0 = __IMPOSSIBLE__ | k == 0 = xs | otherwise = x : dropAt (k-1) xs -- | Solve the k'th equation with the given value, which can depend on -- regular variables but not on other equation variables. solveEq :: Int -> Term -> UnifyState -> (UnifyState, PatternSubstitution) solveEq k u s = (,sigma) $ s { eqTel = applyUnder k (eqTel s) u' , eqLHS = dropAt k $ eqLHS s , eqRHS = dropAt k $ eqRHS s } where u' = raise k u n = eqCount s sigma = liftS (n-k-1) $ consS (DotP u') idS -- | Simplify the k'th equation with the given value (which can depend on other -- equation variables). Returns Nothing if there is a cycle. simplifyEq :: Int -> Term -> UnifyState -> Maybe (UnifyState, PatternSubstitution) simplifyEq k u s = case instantiateTelescope (eqTel s) k u of Nothing -> Nothing Just (tel' , sigma , rho) -> Just $ (,sigma) $ UState { varTel = varTel s , flexVars = flexVars s , eqTel = tel' , eqLHS = permute rho $ eqLHS s , eqRHS = permute rho $ eqRHS s } ---------------------------------------------------- -- Unification strategies ---------------------------------------------------- data UnifyStep = Deletion { deleteAt :: Int , deleteType :: Type , deleteLeft :: Term , deleteRight :: Term } | Solution { solutionAt :: Int , solutionType :: Type , solutionVar :: Int , solutionTerm :: Term } | Injectivity { injectAt :: Int , injectType :: Type , injectDatatype :: QName , injectParameters :: Args , injectIndices :: Args , injectConstructor :: ConHead } | Conflict { conflictAt :: Int , conflictDatatype :: QName , conflictParameters :: Args , conflictLeft :: Term , conflictRight :: Term } | Cycle { cycleAt :: Int , cycleDatatype :: QName , cycleParameters :: Args , cycleVar :: Int , cycleOccursIn :: Term } | EtaExpandVar { expandVar :: FlexibleVar Int , expandVarRecordType :: QName , expandVarParameters :: Args } | EtaExpandEquation { expandAt :: Int , expandRecordType :: QName , expandParameters :: Args } | LitConflict { litConflictAt :: Int , litType :: Type , litConflictLeft :: Literal , litConflictRight :: Literal } | StripSizeSuc { stripAt :: Int , stripArgLeft :: Term , stripArgRight :: Term } | SkipIrrelevantEquation { skipIrrelevantAt :: Int } | TypeConInjectivity { typeConInjectAt :: Int , typeConstructor :: QName , typeConArgsLeft :: Args , typeConArgsRight :: Args } deriving (Show) instance PrettyTCM UnifyStep where prettyTCM step = case step of Deletion k a u v -> text "Deletion" $$ nest 2 (vcat $ [ text "position: " <+> text (show k) , text "type: " <+> prettyTCM a , text "lhs: " <+> prettyTCM u , text "rhs: " <+> prettyTCM v ]) Solution k a i u -> text "Solution" $$ nest 2 (vcat $ [ text "position: " <+> text (show k) , text "type: " <+> prettyTCM a , text "variable: " <+> text (show i) , text "term: " <+> prettyTCM u ]) Injectivity k a d pars ixs c -> text "Injectivity" $$ nest 2 (vcat $ [ text "position: " <+> text (show k) , text "type: " <+> prettyTCM a , text "datatype: " <+> prettyTCM d , text "parameters: " <+> prettyList_ (map prettyTCM pars) , text "indices: " <+> prettyList_ (map prettyTCM ixs) , text "constructor:" <+> prettyTCM c ]) Conflict k d pars u v -> text "Conflict" $$ nest 2 (vcat $ [ text "position: " <+> text (show k) , text "datatype: " <+> prettyTCM d , text "parameters: " <+> prettyList_ (map prettyTCM pars) , text "lhs: " <+> prettyTCM u , text "rhs: " <+> prettyTCM v ]) Cycle k d pars i u -> text "Cycle" $$ nest 2 (vcat $ [ text "position: " <+> text (show k) , text "datatype: " <+> prettyTCM d , text "parameters: " <+> prettyList_ (map prettyTCM pars) , text "variable: " <+> text (show i) , text "term: " <+> prettyTCM u ]) EtaExpandVar fi r pars -> text "EtaExpandVar" $$ nest 2 (vcat $ [ text "variable: " <+> text (show fi) , text "record type:" <+> prettyTCM r , text "parameters: " <+> prettyTCM pars ]) EtaExpandEquation k r pars -> text "EtaExpandVar" $$ nest 2 (vcat $ [ text "position: " <+> text (show k) , text "record type:" <+> prettyTCM r , text "parameters: " <+> prettyTCM pars ]) LitConflict k a u v -> text "LitConflict" $$ nest 2 (vcat $ [ text "position: " <+> text (show k) , text "type: " <+> prettyTCM a , text "lhs: " <+> prettyTCM u , text "rhs: " <+> prettyTCM v ]) StripSizeSuc k u v -> text "StripSizeSuc" $$ nest 2 (vcat $ [ text "position: " <+> text (show k) , text "lhs: " <+> prettyTCM u , text "rhs: " <+> prettyTCM v ]) SkipIrrelevantEquation k -> text "SkipIrrelevantEquation" $$ nest 2 (vcat $ [ text "position: " <+> text (show k) ]) TypeConInjectivity k d us vs -> text "TypeConInjectivity" $$ nest 2 (vcat $ [ text "position: " <+> text (show k) , text "datatype: " <+> prettyTCM d , text "lhs: " <+> prettyList_ (map prettyTCM us) , text "rhs: " <+> prettyList_ (map prettyTCM vs) ]) type UnifyStrategy = UnifyState -> ListT TCM UnifyStep leftToRightStrategy :: UnifyStrategy leftToRightStrategy s = msum (for [0..n-1] $ \k -> completeStrategyAt k s) where n = size $ eqTel s rightToLeftStrategy :: UnifyStrategy rightToLeftStrategy s = msum (for (downFrom n) $ \k -> completeStrategyAt k s) where n = size $ eqTel s completeStrategyAt :: Int -> UnifyStrategy completeStrategyAt k s = msum $ map (\strat -> strat k s) $ [ skipIrrelevantStrategy , basicUnifyStrategy , literalStrategy , dataStrategy , etaExpandVarStrategy , etaExpandEquationStrategy , injectiveTypeConStrategy , injectivePragmaStrategy , simplifySizesStrategy , checkEqualityStrategy ] -- | Returns true if the variables 0..k-1 don't occur in x isHom :: (Free a, Subst Term a) => Int -> a -> Maybe a isHom n x = do guard $ getAll $ runFree (All . (>= n)) IgnoreNot x return $ raise (-n) x -- | Checks whether the given term (of the given type) is beta-eta-equivalent -- to a variable. Returns just the de Bruijn-index of the variable if it is, -- or nothing otherwise. isEtaVar :: Term -> Type -> TCM (Maybe Int) isEtaVar u a = runMaybeT $ isEtaVarG u a Nothing [] where -- Checks whether the term u (of type a) is beta-eta-equivalent to -- `Var i es`, and returns i if it is. If the argument mi is `Just i'`, -- then i and i' are also required to be equal (else Nothing is returned). isEtaVarG :: Term -> Type -> Maybe Int -> [Elim' Int] -> MaybeT TCM Int isEtaVarG u a mi es = do (u, a) <- liftTCM $ reduce (u, a) liftTCM $ reportSDoc "tc.lhs.unify" 80 $ text "isEtaVarG" <+> nest 2 (sep [ text "u = " <+> text (show u) , text "a = " <+> prettyTCM a , text "mi = " <+> text (show mi) , text "es = " <+> prettyList (map (text . show) es) ]) case (ignoreSharing u, ignoreSharing $ unEl a) of (Var i' es', _) -> do guard $ mi == (i' <$ mi) b <- liftTCM $ typeOfBV i' areEtaVarElims (var i') b es' es return i' (_, Def d pars) -> do guard =<< do liftTCM $ isEtaRecord d fs <- liftTCM $ map unArg . recFields . theDef <$> getConstInfo d is <- forM fs $ \f -> do let o = ProjSystem (_, _, fa) <- MaybeT $ projectTyped u a o f isEtaVarG (u `applyE` [Proj o f]) fa mi (es ++ [Proj o f]) case (mi, is) of (Just i, _) -> return i (Nothing, []) -> mzero (Nothing, i:is) -> guard (all (==i) is) >> return i (_, Pi dom cod) -> addContext dom $ do let u' = raise 1 u `apply` [argFromDom dom $> var 0] a' = absBody cod mi' = fmap (+1) mi es' = (fmap . fmap) (+1) es ++ [Apply $ argFromDom dom $> 0] (-1+) <$> isEtaVarG u' a' mi' es' _ -> mzero -- `areEtaVarElims u a es es'` checks whether the given elims es (as applied -- to the term u of type a) are beta-eta-equal to either projections or -- variables with de Bruijn indices given by es'. areEtaVarElims :: Term -> Type -> Elims -> [Elim' Int] -> MaybeT TCM () areEtaVarElims u a [] [] = return () areEtaVarElims u a [] (_:_) = mzero areEtaVarElims u a (_:_) [] = mzero areEtaVarElims u a (Proj o f : es) (Proj _ f' : es') = do guard $ f == f' a <- liftTCM $ reduce a (_, _, fa) <- MaybeT $ projectTyped u a o f areEtaVarElims (u `applyE` [Proj o f]) fa es es' -- These two cases can occur only when we're looking at two different -- variables (i.e. one of function type and the other of record type) so -- it's definitely not the variable we're looking for (or someone is playing -- Jedi mind tricks on us) areEtaVarElims u a (Proj{} : _ ) (Apply _ : _ ) = mzero areEtaVarElims u a (Apply _ : _ ) (Proj{} : _ ) = mzero areEtaVarElims u a (Apply v : es) (Apply i : es') = do ifNotPiType a (const mzero) $ \dom cod -> do _ <- isEtaVarG (unArg v) (unDom dom) (Just $ unArg i) [] areEtaVarElims (u `apply` [fmap var i]) (cod `absApp` var (unArg i)) es es' findFlexible :: Int -> FlexibleVars -> Maybe (FlexibleVar Nat) findFlexible i flex = let flex' = map flexVar flex flexible i = i `elem` flex' in List.find ((i ==) . flexVar) flex basicUnifyStrategy :: Int -> UnifyStrategy basicUnifyStrategy k s = do Equal a u v <- liftTCM $ eqUnLevel (getEquality k s) ha <- mfromMaybe $ isHom n a (mi, mj) <- liftTCM $ addContext (varTel s) $ (,) <$> isEtaVar u ha <*> isEtaVar v ha liftTCM $ reportSDoc "tc.lhs.unify" 30 $ text "isEtaVar results: " <+> text (show [mi,mj]) case (mi, mj) of (Just i, Just j) | i == j -> mzero -- Taken care of by checkEqualityStrategy (Just i, Just j) | Just fi <- findFlexible i flex , Just fj <- findFlexible j flex -> do let choice = chooseFlex fi fj firstTryLeft = msum [ return (Solution k ha i v) , return (Solution k ha j u)] firstTryRight = msum [ return (Solution k ha j u) , return (Solution k ha i v)] liftTCM $ reportSDoc "tc.lhs.unify" 40 $ text "fi = " <+> text (show fi) liftTCM $ reportSDoc "tc.lhs.unify" 40 $ text "fj = " <+> text (show fj) liftTCM $ reportSDoc "tc.lhs.unify" 40 $ text "chooseFlex: " <+> text (show choice) case choice of ChooseLeft -> firstTryLeft ChooseRight -> firstTryRight ExpandBoth -> mzero -- This should be taken care of by etaExpandEquationStrategy ChooseEither -> firstTryRight (Just i, _) | Just _ <- findFlexible i flex -> return $ Solution k ha i v (_, Just j) | Just _ <- findFlexible j flex -> return $ Solution k ha j u _ -> mzero where flex = flexVars s n = eqCount s dataStrategy :: Int -> UnifyStrategy dataStrategy k s = do Equal a u v <- liftTCM $ eqConstructorForm =<< eqUnLevel (getEqualityUnraised k s) case ignoreSharing $ unEl a of Def d es -> do npars <- mcatMaybes $ liftTCM $ getNumberOfParameters d let (pars,ixs) = splitAt npars $ fromMaybe __IMPOSSIBLE__ $ allApplyElims es hpars <- mfromMaybe $ isHom k pars liftTCM $ reportSDoc "tc.lhs.unify" 40 $ addContext (varTel s) $ text "Found equation at datatype " <+> prettyTCM d <+> text " with (homogeneous) parameters " <+> prettyTCM hpars case (ignoreSharing u, ignoreSharing v) of (Con c _ _ , Con c' _ _ ) | c == c' -> return $ Injectivity k a d hpars ixs c (Con c _ _ , Con c' _ _ ) -> return $ Conflict k d hpars u v (Var i [] , v ) -> ifOccursStronglyRigid i v $ return $ Cycle k d hpars i v (u , Var j [] ) -> ifOccursStronglyRigid j u $ return $ Cycle k d hpars j u _ -> mzero _ -> mzero where ifOccursStronglyRigid i u ret = case occurrence i u of StronglyRigid -> ret NoOccurrence -> mzero _ -> do u <- liftTCM $ normalise u case occurrence i u of StronglyRigid -> ret _ -> mzero checkEqualityStrategy :: Int -> UnifyStrategy checkEqualityStrategy k s = do let Equal a u v = getEquality k s n = eqCount s ha <- mfromMaybe $ isHom n a return $ Deletion k ha u v literalStrategy :: Int -> UnifyStrategy literalStrategy k s = do let n = eqCount s Equal a u v <- liftTCM $ eqUnLevel $ getEquality k s ha <- mfromMaybe $ isHom n a case (u , v) of (Lit l1 , Lit l2) | l1 == l2 -> return $ Deletion k ha u v | otherwise -> return $ LitConflict k ha l1 l2 _ -> mzero etaExpandVarStrategy :: Int -> UnifyStrategy etaExpandVarStrategy k s = do Equal a u v <- liftTCM $ eqUnLevel (getEquality k s) shouldEtaExpand u a s `mplus` shouldEtaExpand v a s where -- TODO: use IsEtaVar to check if the term is a variable shouldEtaExpand :: Term -> Type -> UnifyStrategy shouldEtaExpand (Var i es) a s = do fi <- mfromMaybe $ findFlexible i (flexVars s) liftTCM $ reportSDoc "tc.lhs.unify" 50 $ text "Found flexible variable " <+> text (show i) ps <- mfromMaybe $ allProjElims es guard $ not $ null ps liftTCM $ reportSDoc "tc.lhs.unify" 50 $ text "with projections " <+> prettyTCM (map snd ps) let b = getVarTypeUnraised (varCount s - 1 - i) s (d, pars) <- mcatMaybes $ liftTCM $ isEtaRecordType b liftTCM $ reportSDoc "tc.lhs.unify" 50 $ text "at record type " <+> prettyTCM d return $ EtaExpandVar fi d pars shouldEtaExpand _ _ _ = mzero etaExpandEquationStrategy :: Int -> UnifyStrategy etaExpandEquationStrategy k s = do let Equal a u v = getEqualityUnraised k s (d, pars) <- mcatMaybes $ liftTCM $ addContext tel $ isEtaRecordType a sing <- liftTCM $ (Right True ==) <$> isSingletonRecord d pars projLeft <- liftTCM $ shouldProject u projRight <- liftTCM $ shouldProject v guard $ sing || projLeft || projRight return $ EtaExpandEquation k d pars where shouldProject :: Term -> TCM Bool shouldProject u = case ignoreSharing u of Def f es -> usesCopatterns f Con c _ _ -> isJust <$> isRecordConstructor (conName c) Var _ _ -> return False Lam _ _ -> __IMPOSSIBLE__ Lit _ -> __IMPOSSIBLE__ Pi _ _ -> __IMPOSSIBLE__ Sort _ -> __IMPOSSIBLE__ Level _ -> __IMPOSSIBLE__ MetaV _ _ -> return False DontCare _ -> return False Shared _ -> __IMPOSSIBLE__ tel = varTel s `abstract` telFromList (take k $ telToList $ eqTel s) simplifySizesStrategy :: Int -> UnifyStrategy simplifySizesStrategy k s = do isSizeName <- liftTCM isSizeNameTest let Equal a u v = getEquality k s case ignoreSharing $ unEl a of Def d _ -> do guard $ isSizeName d su <- liftTCM $ sizeView u sv <- liftTCM $ sizeView v case (su, sv) of (SizeSuc u, SizeSuc v) -> return $ StripSizeSuc k u v (SizeSuc u, SizeInf ) -> return $ StripSizeSuc k u v (SizeInf , SizeSuc v) -> return $ StripSizeSuc k u v _ -> mzero _ -> mzero injectiveTypeConStrategy :: Int -> UnifyStrategy injectiveTypeConStrategy k s = do injTyCon <- liftTCM $ optInjectiveTypeConstructors <$> pragmaOptions guard injTyCon eq <- liftTCM $ eqUnLevel $ getEquality k s case eq of Equal a u@(ignoreSharing -> Def d es) v@(ignoreSharing -> Def d' es') | d == d' -> do -- d must be a data, record or axiom def <- liftTCM $ getConstInfo d guard $ case theDef def of Datatype{} -> True Record{} -> True Axiom{} -> True AbstractDefn{} -> False -- True triggers issue #2250 Function{} -> False Primitive{} -> False Constructor{}-> __IMPOSSIBLE__ -- Never a type! let us = fromMaybe __IMPOSSIBLE__ $ allApplyElims es vs = fromMaybe __IMPOSSIBLE__ $ allApplyElims es' return $ TypeConInjectivity k d us vs _ -> mzero injectivePragmaStrategy :: Int -> UnifyStrategy injectivePragmaStrategy k s = do eq <- liftTCM $ eqUnLevel $ getEquality k s case eq of Equal a u@(ignoreSharing -> Def d es) v@(ignoreSharing -> Def d' es') | d == d' -> do -- d must have an injective pragma def <- liftTCM $ getConstInfo d guard $ defInjective def let us = fromMaybe __IMPOSSIBLE__ $ allApplyElims es vs = fromMaybe __IMPOSSIBLE__ $ allApplyElims es' return $ TypeConInjectivity k d us vs _ -> mzero skipIrrelevantStrategy :: Int -> UnifyStrategy skipIrrelevantStrategy k s = do let i = getEqInfo k s guard $ isIrrelevant i return $ SkipIrrelevantEquation k ---------------------------------------------------- -- Actually doing the unification ---------------------------------------------------- data UnifyLogEntry = UnificationDone UnifyState | UnificationStep UnifyState UnifyStep type UnifyLog = [UnifyLogEntry] data UnifyOutput = UnifyOutput { unifySubst :: PatternSubstitution , unifyProof :: PatternSubstitution , unifyLog :: UnifyLog } instance Semigroup UnifyOutput where x <> y = UnifyOutput { unifySubst = unifySubst y `composeS` unifySubst x , unifyProof = unifyProof y `composeS` unifyProof x , unifyLog = unifyLog x ++ unifyLog y } instance Monoid UnifyOutput where mempty = UnifyOutput IdS IdS [] mappend = (<>) type UnifyM a = WriterT UnifyOutput TCM a tellUnifySubst :: PatternSubstitution -> UnifyM () tellUnifySubst sub = tell $ UnifyOutput sub IdS [] tellUnifyProof :: PatternSubstitution -> UnifyM () tellUnifyProof sub = tell $ UnifyOutput IdS sub [] writeUnifyLog :: UnifyLogEntry -> UnifyM () writeUnifyLog x = tell $ UnifyOutput IdS IdS [x] runUnifyM :: UnifyM a -> TCM (a,UnifyOutput) runUnifyM = runWriterT unifyStep :: UnifyState -> UnifyStep -> UnifyM (UnificationResult' UnifyState) unifyStep s Deletion{ deleteAt = k , deleteType = a , deleteLeft = u , deleteRight = v } = do -- Check definitional equality of u and v isReflexive <- liftTCM $ addContext (varTel s) $ do dontAssignMetas $ disableDestructiveUpdate $ noConstraints $ equalTerm a u v return Nothing `catchError` \err -> return $ Just err withoutK <- liftTCM $ optWithoutK <$> pragmaOptions case isReflexive of Just err -> return $ DontKnow [] _ | withoutK -> return $ DontKnow [UnifyReflexiveEq (varTel s) a u] _ -> do let (s', sigma) = solveEq k u s tellUnifyProof sigma Unifies <$> liftTCM (reduceEqTel s') unifyStep s Solution{ solutionAt = k , solutionType = a , solutionVar = i , solutionTerm = u } = do let m = varCount s -- Check that the type of the variable is equal to the type of the equation -- (not just a subtype), otherwise we cannot instantiate (see Issue 2407). let a' = getVarType (m-1-i) s equalTypes <- liftTCM $ addContext (varTel s) $ do reportSDoc "tc.lhs.unify" 45 $ text "Equation type: " <+> prettyTCM a reportSDoc "tc.lhs.unify" 45 $ text "Variable type: " <+> prettyTCM a' dontAssignMetas $ disableDestructiveUpdate $ noConstraints $ equalType a a' return Nothing `catchError` \err -> return $ Just err case equalTypes of Just err -> return $ DontKnow [] Nothing -> caseMaybeM (trySolveVar (m-1-i) u s) (return $ DontKnow [UnifyRecursiveEq (varTel s) a i u]) (\(s',sub) -> do tellUnifySubst sub let (s'', sigma) = solveEq k (applyPatSubst sub u) s' tellUnifyProof sigma Unifies <$> liftTCM (reduce s'')) where trySolveVar i u s = case solveVar i u s of Just x -> return $ Just x Nothing -> do u <- liftTCM $ normalise u s <- liftTCM $ normaliseVarTel s return $ solveVar i u s unifyStep s (Injectivity k a d pars ixs c) = do withoutK <- liftTCM $ optWithoutK <$> pragmaOptions let n = eqCount s -- Get constructor telescope and target indices ctype <- (`piApply` pars) . defType <$> liftTCM (getConInfo c) addContext (varTel s) $ reportSDoc "tc.lhs.unify" 40 $ text "Constructor type: " <+> prettyTCM ctype TelV ctel ctarget <- liftTCM $ telView ctype let cixs = case ignoreSharing $ unEl ctarget of Def d' es | d == d' -> let args = fromMaybe __IMPOSSIBLE__ $ allApplyElims es in drop (length pars) args _ -> __IMPOSSIBLE__ -- Get index telescope of the datatype dtype <- (`piApply` pars) . defType <$> liftTCM (getConstInfo d) addContext (varTel s) $ reportSDoc "tc.lhs.unify" 40 $ text "Datatype type: " <+> prettyTCM dtype -- Split equation telescope into parts before and after current equation let (eqListTel1, _ : eqListTel2) = splitAt k $ telToList $ eqTel s (eqTel1, eqTel2) = (telFromList eqListTel1, telFromList eqListTel2) -- This is where the magic of higher-dimensional unification happens -- We need to generalize the indices `ixs` to the target indices of the -- constructor `cixs`. This is done by calling the unification algorithm -- recursively (this doesn't get stuck in a loop because a type should -- never be indexed over itself). Note the similarity with the -- computeNeighbourhood function in Agda.TypeChecking.Coverage. let hduTel = eqTel1 `abstract` raise (size eqTel1) ctel res <- liftTCM $ addContext (varTel s) $ unifyIndices hduTel (allFlexVars hduTel) (raise (size hduTel) dtype) (raise (size ctel) ixs) (raiseFrom (size ctel) (size eqTel1) cixs) case res of -- Higher-dimensional unification can never end in a conflict, -- because `cong c1 ...` and `cong c2 ...` don't even have the -- same type for distinct constructors c1 and c2. NoUnify _ -> __IMPOSSIBLE__ -- TODO: we could still make progress here if not --without-K, -- but I'm not sure if it's necessary. DontKnow _ -> let n = eqCount s Equal a u v = getEquality k s in return $ DontKnow [UnifyIndicesNotVars (varTel s `abstract` eqTel s) a (raise n u) (raise n v) (raise (n-k) ixs)] Unifies (eqTel1', rho0, _) -> do -- Split ps0 into parts for eqTel1 and ctel let (rho1, rho2) = splitS (size ctel) rho0 -- Compute new equation telescope context and substitution let ceq = ConP c noConPatternInfo $ applySubst rho2 $ teleNamedArgs ctel rho3 = consS ceq rho1 eqTel2' = applyPatSubst rho3 eqTel2 eqTel' = eqTel1' `abstract` eqTel2' rho = liftS (size eqTel2) rho3 tellUnifyProof rho eqTel' <- liftTCM $ reduce eqTel' -- Compute new lhs and rhs by matching the old ones against rho (lhs', rhs') <- liftTCM . reduce =<< do let ps = applySubst rho $ teleNamedArgs $ eqTel s (lhsMatch, _) <- liftTCM $ runReduceM $ Match.matchPatterns ps $ eqLHS s (rhsMatch, _) <- liftTCM $ runReduceM $ Match.matchPatterns ps $ eqRHS s case (lhsMatch, rhsMatch) of (Match.Yes _ lhs', Match.Yes _ rhs') -> return (reverse $ Match.matchedArgs __IMPOSSIBLE__ (size eqTel') lhs', reverse $ Match.matchedArgs __IMPOSSIBLE__ (size eqTel') rhs') _ -> __IMPOSSIBLE__ return $ Unifies $ s { eqTel = eqTel' , eqLHS = lhs' , eqRHS = rhs' } unifyStep s Conflict { conflictLeft = u , conflictRight = v } = return $ NoUnify $ UnifyConflict (varTel s) u v unifyStep s Cycle { cycleVar = i , cycleOccursIn = u } = return $ NoUnify $ UnifyCycle (varTel s) i u unifyStep s EtaExpandVar{ expandVar = fi, expandVarRecordType = d , expandVarParameters = pars } = do delta <- liftTCM $ (`apply` pars) <$> getRecordFieldTypes d c <- liftTCM $ getRecordConstructor d let nfields = size delta (varTel', rho) = expandTelescopeVar (varTel s) (m-1-i) delta c projectFlexible = [ FlexibleVar (flexHiding fi) (flexOrigin fi) (projFlexKind j) (flexPos fi) (i+j) | j <- [0..nfields-1] ] tellUnifySubst $ rho Unifies <$> liftTCM (reduce $ UState { varTel = varTel' , flexVars = projectFlexible ++ liftFlexibles nfields (flexVars s) , eqTel = applyPatSubst rho $ eqTel s , eqLHS = applyPatSubst rho $ eqLHS s , eqRHS = applyPatSubst rho $ eqRHS s }) where i = flexVar fi m = varCount s n = eqCount s projFlexKind :: Int -> FlexibleVarKind projFlexKind j = case flexKind fi of RecordFlex ks -> fromMaybe ImplicitFlex $ ks !!! j ImplicitFlex -> ImplicitFlex DotFlex -> DotFlex OtherFlex -> OtherFlex liftFlexible :: Int -> Int -> Maybe Int liftFlexible n j = if j == i then Nothing else Just (if j > i then j + (n-1) else j) liftFlexibles :: Int -> FlexibleVars -> FlexibleVars liftFlexibles n fs = catMaybes $ map (traverse $ liftFlexible n) fs unifyStep s EtaExpandEquation{ expandAt = k, expandRecordType = d, expandParameters = pars } = do delta <- liftTCM $ (`apply` pars) <$> getRecordFieldTypes d c <- liftTCM $ getRecordConstructor d lhs <- expandKth $ eqLHS s rhs <- expandKth $ eqRHS s let (tel, sigma) = expandTelescopeVar (eqTel s) k delta c tellUnifyProof sigma Unifies <$> liftTCM (reduceEqTel $ s { eqTel = tel , eqLHS = lhs , eqRHS = rhs }) where expandKth us = do let (us1,v:us2) = fromMaybe __IMPOSSIBLE__ $ splitExactlyAt k us vs <- liftTCM $ snd <$> etaExpandRecord d pars (unArg v) vs <- liftTCM $ reduce vs return $ us1 ++ vs ++ us2 unifyStep s LitConflict { litType = a , litConflictLeft = l , litConflictRight = l' } = return $ NoUnify $ UnifyConflict (varTel s) (Lit l) (Lit l') unifyStep s (StripSizeSuc k u v) = do sizeTy <- liftTCM sizeType sizeSu <- liftTCM $ sizeSuc 1 (var 0) let n = eqCount s sub = liftS (n-k-1) $ consS sizeSu $ raiseS 1 eqFlatTel = flattenTel $ eqTel s eqFlatTel' = applySubst sub $ updateAt k (fmap $ const sizeTy) $ eqFlatTel eqTel' = unflattenTel (teleNames $ eqTel s) eqFlatTel' -- TODO: tellUnifyProof sub -- but sizeSu is not a constructor, so sub is not a PatternSubstitution! return $ Unifies $ s { eqTel = eqTel' , eqLHS = updateAt k (const $ defaultArg u) $ eqLHS s , eqRHS = updateAt k (const $ defaultArg v) $ eqRHS s } unifyStep s (SkipIrrelevantEquation k) = do let lhs = eqLHS s (s', sigma) = solveEq k (DontCare $ unArg $ lhs !! k) s tellUnifyProof sigma return $ Unifies s' unifyStep s (TypeConInjectivity k d us vs) = do dtype <- defType <$> liftTCM (getConstInfo d) TelV dtel _ <- liftTCM $ telView dtype let n = eqCount s m = size dtel deq = Def d $ map Apply $ teleArgs dtel -- TODO: tellUnifyProof ??? -- but d is not a constructor... Unifies <$> liftTCM (reduceEqTel $ s { eqTel = dtel `abstract` applyUnder k (eqTel s) (raise k deq) , eqLHS = us ++ dropAt k (eqLHS s) , eqRHS = vs ++ dropAt k (eqRHS s) }) unify :: UnifyState -> UnifyStrategy -> UnifyM (UnificationResult' UnifyState) unify s strategy = if isUnifyStateSolved s then return $ Unifies s else tryUnifyStepsAndContinue (strategy s) where tryUnifyStepsAndContinue :: ListT TCM UnifyStep -> UnifyM (UnificationResult' UnifyState) tryUnifyStepsAndContinue steps = do x <- foldListT tryUnifyStep failure $ liftListT lift steps case x of Unifies s' -> unify s' strategy NoUnify err -> return $ NoUnify err DontKnow err -> return $ DontKnow err tryUnifyStep :: UnifyStep -> UnifyM (UnificationResult' UnifyState) -> UnifyM (UnificationResult' UnifyState) tryUnifyStep step fallback = do addContext (varTel s) $ reportSDoc "tc.lhs.unify" 20 $ text "trying unifyStep" <+> prettyTCM step x <- unifyStep s step case x of Unifies s' -> do reportSDoc "tc.lhs.unify" 20 $ text "unifyStep successful." reportSDoc "tc.lhs.unify" 20 $ text "new unifyState:" <+> prettyTCM s' writeUnifyLog $ UnificationStep s step return x NoUnify{} -> return x DontKnow err1 -> do y <- fallback case y of DontKnow err2 -> return $ DontKnow $ err1 ++ err2 _ -> return y failure :: UnifyM (UnificationResult' a) failure = return $ DontKnow [] Agda-2.5.3/src/full/Agda/TypeChecking/Rules/LHS/Problem.hs0000644000000000000000000002653613154613124021175 0ustar0000000000000000-- {-# LANGUAGE CPP #-} module Agda.TypeChecking.Rules.LHS.Problem where import Prelude hiding (null) import Control.Applicative hiding (empty) import Data.Foldable ( Foldable ) import Data.Maybe ( fromMaybe ) import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend, mconcat) import Data.Traversable import Agda.Syntax.Common import Agda.Syntax.Info import Agda.Syntax.Literal import Agda.Syntax.Position import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import qualified Agda.Syntax.Abstract as A import Agda.TypeChecking.Substitute import Agda.TypeChecking.Reduce import qualified Agda.TypeChecking.Pretty as P import Agda.TypeChecking.Pretty hiding ((<>)) import Agda.Utils.List import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Size import qualified Agda.Utils.Pretty as PP type Substitution = [Maybe Term] type FlexibleVars = [FlexibleVar Nat] -- | When we encounter a flexible variable in the unifier, where did it come from? -- The alternatives are ordered such that we will assign the higher one first, -- i.e., first we try to assign a @DotFlex@, then... data FlexibleVarKind = RecordFlex [FlexibleVarKind] -- ^ From a record pattern ('ConP'). -- Saves the 'FlexibleVarKind' of its subpatterns. | ImplicitFlex -- ^ From a hidden formal argument or underscore ('WildP'). | DotFlex -- ^ From a dot pattern ('DotP'). | OtherFlex -- ^ From a non-record constructor or literal ('ConP' or 'LitP'). deriving (Eq, Show) -- | Flexible variables are equipped with information where they come from, -- in order to make a choice which one to assign when two flexibles are unified. data FlexibleVar a = FlexibleVar { flexHiding :: Hiding , flexOrigin :: Origin , flexKind :: FlexibleVarKind , flexPos :: Maybe Int , flexVar :: a } deriving (Eq, Show, Functor, Foldable, Traversable) instance LensHiding (FlexibleVar a) where getHiding = flexHiding mapHiding f x = x { flexHiding = f (flexHiding x) } instance LensOrigin (FlexibleVar a) where getOrigin = flexOrigin mapOrigin f x = x { flexOrigin = f (flexOrigin x) } -- UNUSED -- defaultFlexibleVar :: a -> FlexibleVar a -- defaultFlexibleVar a = FlexibleVar Hidden Inserted ImplicitFlex Nothing a -- UNUSED -- flexibleVarFromHiding :: Hiding -> a -> FlexibleVar a -- flexibleVarFromHiding h a = FlexibleVar h ImplicitFlex Nothing a allFlexVars :: Telescope -> FlexibleVars allFlexVars tel = zipWith makeFlex (downFrom $ size tel) $ telToList tel where makeFlex i d = FlexibleVar (getHiding d) (getOrigin d) ImplicitFlex (Just i) i data FlexChoice = ChooseLeft | ChooseRight | ChooseEither | ExpandBoth deriving (Eq, Show) instance Semigroup FlexChoice where ExpandBoth <> _ = ExpandBoth _ <> ExpandBoth = ExpandBoth ChooseEither <> y = y x <> ChooseEither = x ChooseLeft <> ChooseRight = ExpandBoth -- If there's dot patterns on both sides, ChooseRight <> ChooseLeft = ExpandBoth -- we need to eta-expand ChooseLeft <> ChooseLeft = ChooseLeft ChooseRight <> ChooseRight = ChooseRight instance Monoid FlexChoice where mempty = ChooseEither mappend = (<>) class ChooseFlex a where chooseFlex :: a -> a -> FlexChoice instance ChooseFlex FlexibleVarKind where chooseFlex DotFlex DotFlex = ChooseEither chooseFlex DotFlex _ = ChooseLeft chooseFlex _ DotFlex = ChooseRight chooseFlex (RecordFlex xs) (RecordFlex ys) = chooseFlex xs ys chooseFlex (RecordFlex xs) y = chooseFlex xs (repeat y) chooseFlex x (RecordFlex ys) = chooseFlex (repeat x) ys chooseFlex ImplicitFlex ImplicitFlex = ChooseEither chooseFlex ImplicitFlex _ = ChooseLeft chooseFlex _ ImplicitFlex = ChooseRight chooseFlex OtherFlex OtherFlex = ChooseEither instance ChooseFlex a => ChooseFlex [a] where chooseFlex xs ys = mconcat $ zipWith chooseFlex xs ys instance ChooseFlex a => ChooseFlex (Maybe a) where chooseFlex Nothing Nothing = ChooseEither chooseFlex Nothing (Just y) = ChooseLeft chooseFlex (Just x) Nothing = ChooseRight chooseFlex (Just x) (Just y) = chooseFlex x y instance ChooseFlex Hiding where chooseFlex Hidden Hidden = ChooseEither chooseFlex Hidden _ = ChooseLeft chooseFlex _ Hidden = ChooseRight chooseFlex Instance{} Instance{} = ChooseEither chooseFlex Instance{} _ = ChooseLeft chooseFlex _ Instance{} = ChooseRight chooseFlex _ _ = ChooseEither instance ChooseFlex Origin where chooseFlex Inserted Inserted = ChooseEither chooseFlex Inserted _ = ChooseLeft chooseFlex _ Inserted = ChooseRight chooseFlex Reflected Reflected = ChooseEither chooseFlex Reflected _ = ChooseLeft chooseFlex _ Reflected = ChooseRight chooseFlex _ _ = ChooseEither instance ChooseFlex Int where chooseFlex x y = case compare x y of LT -> ChooseLeft EQ -> ChooseEither GT -> ChooseRight instance (ChooseFlex a) => ChooseFlex (FlexibleVar a) where chooseFlex (FlexibleVar h1 o1 f1 p1 i1) (FlexibleVar h2 o2 f2 p2 i2) = firstChoice [ chooseFlex f1 f2, chooseFlex o1 o2, chooseFlex h1 h2 , chooseFlex p1 p2, chooseFlex i1 i2] where firstChoice :: [FlexChoice] -> FlexChoice firstChoice [] = ChooseEither firstChoice (ChooseEither : xs) = firstChoice xs firstChoice (x : _ ) = x -- | State of typechecking a LHS; input to 'split'. -- [Ulf Norell's PhD, page. 35] -- -- In @Problem ps p delta@, -- @ps@ are the user patterns of supposed type @delta@. -- @p@ is the pattern resulting from the splitting. data Problem' p = Problem { problemInPat :: [NamedArg A.Pattern] -- ^ User patterns. , problemOutPat :: p -- ^ Patterns after splitting. , problemTel :: Telescope -- ^ Type of in patterns. , problemRest :: ProblemRest -- ^ Patterns that cannot be typed yet. } deriving Show -- | The de Bruijn indices in the pattern refer to positions -- in the list of abstract patterns in the problem, counted -- from the back. type Problem = Problem' [NamedArg DeBruijnPattern] type ProblemPart = Problem' () -- | User patterns that could not be given a type yet. -- -- Example: -- @ -- f : (b : Bool) -> if b then Nat else Nat -> Nat -- f true = zero -- f false zero = zero -- f false (suc n) = n -- @ -- In this sitation, for clause 2, we construct an initial problem -- @ -- problemInPat = [false] -- problemTel = (b : Bool) -- problemRest.restPats = [zero] -- problemRest.restType = if b then Nat else Nat -> Nat -- @ -- As we instantiate @b@ to @false@, the 'restType' reduces to -- @Nat -> Nat@ and we can move pattern @zero@ over to @problemInPat@. data ProblemRest = ProblemRest { restPats :: [NamedArg A.Pattern] -- ^ List of user patterns which could not yet be typed. , restType :: Arg Type -- ^ Type eliminated by 'restPats'. -- Can be 'Irrelevant' to indicate that we came by -- an irrelevant projection and, hence, the rhs must -- be type-checked in irrelevant mode. } deriving Show data Focus = Focus { focusCon :: QName , focusPatOrigin:: ConOrigin -- ^ Do we come from an implicit or record pattern? , focusConArgs :: [NamedArg A.Pattern] , focusRange :: Range , focusOutPat :: [NamedArg DeBruijnPattern] , focusDatatype :: QName , focusParams :: [Arg Term] , focusIndices :: [Arg Term] , focusType :: Type -- ^ Type of variable we are splitting, kept for record patterns. } | LitFocus Literal [NamedArg DeBruijnPattern] Type | AbsurdFocus { absurdFocusRange :: PatInfo , absurdFocusVar :: Int , absurdFocusType :: Type } -- | Result of 'splitProblem': Determines position for the next split. data SplitProblem = -- | Split on constructor pattern. Split { splitLPats :: ProblemPart -- ^ The typed user patterns left of the split position. -- Invariant: @'problemRest' == empty@. , splitFocus :: Arg Focus -- ^ How to split the variable at the split position. , splitRPats :: Abs ProblemPart -- ^ The typed user patterns right of the split position. } | -- | Split on projection pattern. SplitRest { splitProjection :: Arg QName -- ^ The projection could be belonging to an irrelevant record field. , splitProjOrigin :: ProjOrigin , splitRestType :: Type } -- | Put a typed pattern on the very left of a @SplitProblem@. consSplitProblem :: NamedArg A.Pattern -- ^ @p@ A pattern. -> ArgName -- ^ @x@ The name of the argument (from its type). -> Dom Type -- ^ @t@ Its type. -> SplitProblem -- ^ The split problem, containing 'splitLPats' @ps;xs:ts@. -> SplitProblem -- ^ The result, now containing 'splitLPats' @(p,ps);(x,xs):(t,ts)@. consSplitProblem p x dom s@SplitRest{} = s consSplitProblem p x dom s@Split{ splitLPats = ps } = s{ splitLPats = consProblem' ps } where consProblem' (Problem ps () tel pr) = Problem (p:ps) () (ExtendTel dom $ Abs x tel) pr -- | Instantiations of a dot pattern with a term. -- `Maybe e` if the user wrote a dot pattern .e -- `Nothing` if this is an instantiation of an implicit argument or a name. data DotPatternInst = DPI { dotPatternName :: Maybe A.Name , dotPatternUserExpr :: Maybe A.Expr , dotPatternInst :: Term , dotPatternType :: Dom Type } data AsBinding = AsB Name Term Type -- | State worked on during the main loop of checking a lhs. data LHSState = LHSState { lhsProblem :: Problem , lhsDPI :: [DotPatternInst] , lhsShouldBeEmptyTypes :: [(Range,Type)] } instance Subst Term ProblemRest where applySubst rho p = p { restType = applySubst rho $ restType p } instance Subst Term (Problem' p) where applySubst rho p = p { problemTel = applySubst rho $ problemTel p , problemRest = applySubst rho $ problemRest p } instance Subst Term DotPatternInst where applySubst rho (DPI x e v a) = uncurry (DPI x e) $ applySubst rho (v,a) instance Subst Term AsBinding where applySubst rho (AsB x v a) = uncurry (AsB x) $ applySubst rho (v, a) instance PrettyTCM DotPatternInst where prettyTCM (DPI mx me v a) = sep [ x <+> text "=" <+> text "." P.<> prettyA e , nest 2 $ prettyTCM v <+> text ":" , nest 2 $ prettyTCM a ] where x = maybe (text "_") prettyA mx e = fromMaybe underscore me instance PrettyTCM AsBinding where prettyTCM (AsB x v a) = sep [ prettyTCM x P.<> text "@" P.<> parens (prettyTCM v) , nest 2 $ text ":" <+> prettyTCM a ] instance PP.Pretty AsBinding where pretty (AsB x v a) = PP.pretty x PP.<+> PP.text "=" PP.<+> PP.hang (PP.pretty v PP.<+> PP.text ":") 2 (PP.pretty a) instance InstantiateFull AsBinding where instantiateFull' (AsB x v a) = AsB x <$> instantiateFull' v <*> instantiateFull' a instance Null ProblemRest where null = null . restPats empty = ProblemRest { restPats = [], restType = defaultArg typeDontCare } instance Null a => Null (Problem' a) where null p = null (problemInPat p) && null (problemRest p) empty = Problem empty empty empty empty Agda-2.5.3/src/full/Agda/TypeChecking/Rules/LHS/Split.hs0000644000000000000000000005430213154613124020660 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Rules.LHS.Split ( splitProblem ) where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad.Trans ( lift ) import Control.Monad.Trans.Maybe import Data.Either import Data.Maybe (fromMaybe) import qualified Data.List as List import Data.Traversable hiding (mapM, sequence) import Data.Foldable (msum) import Agda.Interaction.Options import Agda.Interaction.Highlighting.Generate (storeDisambiguatedName) import Agda.Syntax.Common import Agda.Syntax.Concrete (FieldAssignment'(..), nameFieldA) import Agda.Syntax.Literal import Agda.Syntax.Position import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Pattern import Agda.Syntax.Abstract (IsProjP(..), MaybePostfixProjP(..)) import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Views (asView) import qualified Agda.Syntax.Info as A import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Constraints import Agda.TypeChecking.Conversion import Agda.TypeChecking.Datatypes import Agda.TypeChecking.Errors (dropTopLevelModule) import Agda.TypeChecking.Free import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.MetaVars import Agda.TypeChecking.Patterns.Abstract import Agda.TypeChecking.Pretty import Agda.TypeChecking.Records import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Rules.LHS.Problem import Agda.Utils.Either import Agda.Utils.Except (catchError) import Agda.Utils.Functor ((<.>)) import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.ListT import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Pretty (prettyShow) import qualified Agda.Utils.Pretty as P import Agda.Utils.Size import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- | Split a problem at the first constructor pattern which is -- actually of datatype type. -- -- Or, if there is no constructor pattern left and the rest type -- is a record type and the first rest pattern is a projection -- pattern, split the rest type. -- -- Implicit patterns should have been inserted. splitProblem :: Maybe QName -- ^ The definition we are checking at the moment. -> Problem -- ^ The current state of the lhs patterns. -> ListT TCM SplitProblem splitProblem mf (Problem ps qs tel pr) = do lift $ do reportSLn "tc.lhs.split" 20 $ "initiating splitting" ++ maybe "" ((" for definition " ++) . prettyShow) mf reportSDoc "tc.lhs.split" 30 $ sep [ nest 2 $ text "ps =" <+> sep (map (P.parens <.> prettyA) ps) , nest 2 $ text "qs =" <+> sep (map (P.parens <.> prettyTCM . namedArg) qs) , nest 2 $ text "tel =" <+> prettyTCM tel ] reportSDoc "tc.lhs.split" 60 $ sep [ nest 2 $ text "ps (raw) =" <+> sep (map (P.parens <.> text . show) ps) , nest 2 $ text "qs (raw) =" <+> sep (map (P.parens <.> text . show . namedArg) qs) , nest 2 $ text "tel (raw) =" <+> (text . show) tel ] splitP ps tel where -- Result splitting splitRest :: ProblemRest -> ListT TCM SplitProblem splitRest (ProblemRest (p : ps) b) | Just f <- mf = do lift $ reportSDoc "tc.lhs.split" 20 $ sep [ text "splitting problem rest" , nest 2 $ text "pattern p =" <+> prettyA p , nest 2 $ text "eliminates type b =" <+> prettyTCM b ] lift $ reportSDoc "tc.lhs.split" 80 $ sep [ nest 2 $ text $ "pattern (raw) p = " ++ show p ] -- If the pattern is not a projection pattern, that's an error. -- Probably then there were too many arguments. caseMaybe (maybePostfixProjP p) failure $ \ (o, AmbQ ds) -> do -- So it is a projection pattern (d = projection name), is it? projs <- lift $ mapMaybeM (\ d -> fmap (d,) <$> isProjection d) ds when (null projs) notProjP -- If the target is not a record type, that's an error. -- It could be a meta, but since we cannot postpone lhs checking, we crash here. caseMaybeM (lift $ isRecordType $ unArg b) notRecord $ \(r, vs, def) -> case def of Record{ recFields = fs } -> do lift $ reportSDoc "tc.lhs.split" 20 $ sep [ text $ "we are of record type r = " ++ prettyShow r , text "applied to parameters vs = " <+> prettyTCM vs , text $ "and have fields fs = " ++ prettyShow fs ] -- The record "self" is the definition f applied to the patterns let es = patternsToElims qs -- Note: the module parameters are already part of qs let self = defaultArg $ Def f [] `applyE` es ai = getArgInfo p -- Try the projection candidates. -- Fail hard for the last candidate. msum $ mapAwareLast (tryProj o ai self fs r vs $ length ds >= 2) projs -- -- This fails softly on all (if more than one) candidates. -- msum $ map (tryProj o ai self fs r vs (length projs >= 2)) projs _ -> __IMPOSSIBLE__ where failure = lift $ typeError $ CannotEliminateWithPattern p $ unArg b notProjP = lift $ typeError $ NotAProjectionPattern p notRecord = failure -- lift $ typeError $ ShouldBeRecordType $ unArg b wrongHiding :: MonadTCM tcm => QName -> tcm a wrongHiding d = typeError . GenericDocError =<< do liftTCM $ text "Wrong hiding used for projection " <+> prettyTCM d -- Issue #2423: error which reports the disambiguation wrongProj :: MonadTCM tcm => QName -> Bool -> tcm a wrongProj d amb = typeError . GenericDocError =<< do liftTCM $ sep [ text "Cannot eliminate type " , prettyTCM (unArg b) , text " with projection " , if amb then text . prettyShow =<< dropTopLevelModule d else prettyTCM d ] -- | Pass 'True' unless last element of the list. mapAwareLast :: forall a b. (Bool -> a -> b) -> [a] -> [b] mapAwareLast f [] = [] mapAwareLast f [a] = [f False a] mapAwareLast f (a:as) = f True a : mapAwareLast f as tryProj :: ProjOrigin -- ^ Origin of projection pattern. -> ArgInfo -- ^ ArgInfo of projection pattern. -> Arg Term -- ^ Self: value we are eliminating. -> [Arg QName] -- ^ Fields of record type under consideration. -> QName -- ^ Name of record type we are eliminating. -> Args -- ^ Parameters of record type we are eliminating. -> Bool -- ^ Did we start out with an ambiguous projection in the beginning? -> Bool -- ^ More than 1 candidates? If yes, fail softly. -> (QName, Projection) -- ^ Current candidate. -> ListT TCM SplitProblem tryProj o ai self fs r vs amb soft (d0, proj) = do -- Recoverable errors are those coming from the projection. -- If we have several projections we fail @soft@ly and just try the next one. let ambErr err = if soft then mzero else err ambTry m | soft = unlessM (liftTCM $ tryConversion m) mzero -- succeed without constraints -- This would leave constraints: -- -- | amb = whenNothingM (liftTCM $ tryMaybe $ disableDestructiveUpdate m) mzero | otherwise = liftTCM $ noConstraints m case proj of -- Andreas, 2015-05-06 issue 1413 projProper=Nothing is not impossible Projection{projProper = Nothing} -> ambErr notProjP Projection{projProper = Just qr, projOrig = d, projLams = lams} -> do let ai = projArgInfo proj -- If projIndex==0, then the projection is already applied -- to the record value (like in @open R r@), and then it -- is no longer a projection but a record field. when (null lams) $ ambErr notProjP lift $ reportSLn "tc.lhs.split" 90 "we are a projection pattern" -- If the target is not a record type, that's an error. -- It could be a meta, but since we cannot postpone lhs checking, we crash here. lift $ reportSDoc "tc.lhs.split" 20 $ sep [ text $ "proj d0 = " ++ prettyShow d0 , text $ "original proj d = " ++ prettyShow d ] -- Get the field decoration. -- If the projection pattern name @d@ is not a field name, -- we have to try the next projection name. -- If this was not an ambiguous projection, that's an error. argd <- maybe (ambErr $ wrongProj d amb) return $ List.find ((d ==) . unArg) fs let ai' = setRelevance (getRelevance argd) ai -- Andreas, 2016-12-31, issue #2374: -- We can also disambiguate by hiding info. unless (sameHiding p ai) $ ambErr $ wrongHiding d -- Andreas, 2016-12-31, issue #1976: -- Check parameters. ambTry $ checkParameters qr r vs -- From here, we have the correctly disambiguated projection. -- Thus, we no longer catch errors. -- For highlighting, we remember which name we disambiguated to. -- This is safe here (fingers crossed) as we won't decide on a -- different projection even if we backtrack and come here again. lift $ storeDisambiguatedName d0 -- Get the type of projection d applied to "self" dType <- lift $ defType <$> getConstInfo d -- full type! lift $ reportSDoc "tc.lhs.split" 20 $ sep [ text "we are self = " <+> prettyTCM (unArg self) , text "being projected by dType = " <+> prettyTCM dType ] -- This should succeed, as we have the correctly disambiguated. lift $ SplitRest (Arg ai' d0) o <$> dType `piApplyM` (vs ++ [self]) -- if there are no more patterns left in the problem rest, there is nothing to split: splitRest _ = mzero -- | In @splitP aps iqs tel@, -- @aps@ are the user patterns on which we are splitting (inPats), -- @ips@ are the one-hole patterns of the current split state (outPats) -- in one-to-one correspondence with the pattern variables -- recorded in @tel@. splitP :: [NamedArg A.Pattern] -> Telescope -> ListT TCM SplitProblem -- no more patterns? pull them from the rest splitP [] _ = splitRest pr -- patterns but no more types? that's an error splitP (_:_) EmptyTel = __IMPOSSIBLE__ -- (we can never have an ExtendTel without Abs) splitP _ (ExtendTel _ NoAbs{}) = __IMPOSSIBLE__ -- pattern with type? Let's get to work: splitP ps0@(p : ps) tel0@(ExtendTel dom@(Dom ai a) xtel@(Abs x tel)) = do liftTCM $ reportSDoc "tc.lhs.split" 30 $ sep [ text "splitP looking at pattern" , nest 2 $ text "p =" <+> prettyA p , nest 2 $ text "dom =" <+> prettyTCM dom ] -- Andreas, 2016-06-30, issue #2075: need test here! unless (sameHiding p ai) $ typeError WrongHidingInLHS -- Possible reinvokations: let -- 1. Redo this argument (after meta instantiation). tryAgain = splitP ps0 tel0 -- 2. Try to split on next argument. keepGoing = consSplitProblem p x dom <$> do underAbstraction dom xtel $ \ tel -> splitP ps tel p <- lift $ expandLitPattern p case snd $ asView $ namedArg p of -- Case: projection pattern. That's an error. A.ProjP{} -> typeError $ CannotEliminateWithPattern p (telePi tel0 $ unArg $ restType pr) -- Case: literal pattern. p@(A.LitP lit) -> do -- Note that, in the presence of --without-K, this branch is -- based on the assumption that the types of literals are -- not indexed. -- Andreas, 2010-09-07 cannot split on irrelevant args when (unusableRelevance $ getRelevance ai) $ typeError $ SplitOnIrrelevant p dom -- Succeed if the split type is (already) equal to the type of the literal. ifNotM (lift $ tryConversion $ equalType a =<< litType lit) {- then -} keepGoing $ {- else -} return Split { splitLPats = empty , splitFocus = Arg ai $ LitFocus lit qs a , splitRPats = Abs x $ Problem ps () tel __IMPOSSIBLE__ } `mplus` keepGoing -- Case: record pattern p@(A.RecP _patInfo fs) -> do res <- lift $ tryRecordType a case res of -- Subcase: blocked Left Nothing -> keepGoing -- Subcase: not a record type or blocked on variable. Left (Just a') -> keepGoing -- If not record type, error will be given later. -- typeError . GenericDocError =<< do -- lift $ text "Record pattern at non-record type " <+> prettyTCM a' -- Subcase: a record type (d vs) Right (d, vs, def) -> do let np = recPars def let (pars, ixs) = splitAt np vs lift $ reportSDoc "tc.lhs.split" 10 $ vcat [ sep [ text "splitting on" , nest 2 $ fsep [ prettyA p, text ":", prettyTCM dom ] ] , nest 2 $ text "pars =" <+> fsep (punctuate comma $ map prettyTCM pars) , nest 2 $ text "ixs =" <+> fsep (punctuate comma $ map prettyTCM ixs) ] let c = killRange $ conName $ recConHead def let -- Field names with ArgInfo. axs = recordFieldNames def -- In es omitted explicit fields are replaced by underscores -- (from missingExplicits). Omitted implicit or instance fields -- are still left out and inserted later by computeNeighborhood. args <- lift $ insertMissingFields d (const $ A.WildP A.patNoRange) fs axs (return Split { splitLPats = empty , splitFocus = Arg ai $ Focus c ConORec args (getRange p) qs d pars ixs a , splitRPats = Abs x $ Problem ps () tel __IMPOSSIBLE__ }) `mplus` keepGoing -- Case: absurd pattern. p@(A.AbsurdP info) -> do lift $ reportSDoc "tc.lhs.split.absurd" 30 $ text "split AbsurdP: type is " <+> prettyTCM a let i = size tel (return Split { splitLPats = empty , splitFocus = Arg ai $ AbsurdFocus info i $ raise (i+1) a , splitRPats = Abs x $ Problem ps () tel __IMPOSSIBLE__ }) `mplus` keepGoing -- Case: constructor pattern. p@(A.ConP ci (A.AmbQ cs) args) -> do let tryInstantiate a' | [c] <- cs = do lift $ reportSDoc "tc.lhs.split" 30 $ text "split ConP: type is blocked" -- Type is blocked by a meta and constructor is unambiguous, -- in this case try to instantiate the meta. ok <- lift $ do Constructor{ conData = d } <- theDef <$> getConstInfo c dt <- defType <$> getConstInfo d vs <- newArgsMeta dt Sort s <- ignoreSharing . unEl <$> reduce (piApply dt vs) tryConversion $ equalType a' (El s $ Def d $ map Apply vs) if ok then tryAgain else keepGoing | otherwise = do lift $ reportSDoc "tc.lhs.split" 30 $ text "split ConP: type is blocked and constructor is ambiguous" keepGoing -- ifBlockedType reduces the type ifBlockedType a (const tryInstantiate) $ \ a' -> do lift $ reportSDoc "tc.lhs.split" 30 $ text "split ConP: type is " <+> prettyTCM a' case ignoreSharing $ unEl a' of -- Subcase: split type is a Def. Def d es -> (liftTCM $ theDef <$> getConstInfo d) >>= \case -- Issue #2253: the data type could be abstract. AbstractDefn{} -> liftTCM $ traceCall (CheckPattern p EmptyTel a) $ do typeError . GenericDocError =<< do text "Cannot split on abstract data type" <+> prettyTCM d def -> do -- We cannot split on (shape-)irrelevant non-records. -- Andreas, 2011-10-04 unless allowed by option lift $ reportSLn "tc.lhs.split" 30 $ "split ConP: relevance is " ++ show ai unless (defIsRecord def) $ when (unusableRelevance $ getRelevance ai) $ unlessM (liftTCM $ optExperimentalIrrelevance <$> pragmaOptions) $ typeError $ SplitOnIrrelevant p dom -- Check that we are at record or data type and return -- the number of parameters. let mp = case def of Datatype{dataPars = np} -> Just np Record{recPars = np} -> Just np _ -> Nothing case mp of Nothing -> keepGoing Just np -> do let vs = fromMaybe __IMPOSSIBLE__ $ allApplyElims es traceCall (CheckPattern p EmptyTel a) $ do -- TODO: wrong telescope -- Check that we construct something in the right datatype c <- lift $ do -- Andreas, 2017-08-13, issue #2686: ignore abstract constructors (cs1, cs') <- unzip . snd . partitionEithers <$> do forM cs $ \ c -> mapRight ((c,) . conName) <$> getConHead c when (null cs1) $ typeError $ AbstractConstructorNotInScope $ head cs d' <- canonicalName d cs0 <- (theDef <$> getConstInfo d') <&> \case Datatype{dataCons = cs0} -> cs0 Record{recConHead = c0} -> [conName c0] _ -> __IMPOSSIBLE__ case [ c | (c, c') <- zip cs1 cs', elem c' cs0 ] of [c] -> do -- If constructor pattern was ambiguous, -- remember our choice for highlighting info. when (length cs >= 2) $ storeDisambiguatedName c return c [] -> typeError $ ConstructorPatternInWrongDatatype (head cs1) d cs3 -> -- if there are more than one we give up (they might have different types) typeError $ CantResolveOverloadedConstructorsTargetingSameDatatype d cs3 let (pars, ixs) = splitAt np vs lift $ reportSDoc "tc.lhs.split" 10 $ vcat [ sep [ text "splitting on" , nest 2 $ fsep [ prettyA p, text ":", prettyTCM dom ] ] , nest 2 $ text "pars =" <+> fsep (punctuate comma $ map prettyTCM pars) , nest 2 $ text "ixs =" <+> fsep (punctuate comma $ map prettyTCM ixs) ] -- Andreas, 2013-03-22 fixing issue 279 -- To resolve ambiguous constructors, Agda always looks up -- their original definition and reconstructs the parameters -- from the type @Def d vs@ we check against. -- However, the constructor could come from a module instantiation -- with some of the parameters already fixed. -- Agda did not make sure the two parameter lists coincide, -- so we add a check here. -- I guess this issue could be solved more systematically, -- but the extra check here is non-invasive to the existing code. -- Andreas, 2016-12-31 fixing issue #1975 -- Do this also for constructors which were originally ambiguous. checkConstructorParameters c d pars (return Split { splitLPats = empty , splitFocus = Arg ai $ Focus c (A.patOrigin ci) args (getRange p) qs d pars ixs a , splitRPats = Abs x $ Problem ps () tel __IMPOSSIBLE__ }) `mplus` keepGoing -- Subcase: split type is not a Def. _ -> keepGoing -- Case: neither literal nor constructor pattern. _ -> keepGoing -- | @checkConstructorParameters c d pars@ checks that the data/record type -- behind @c@ is has initial parameters (coming e.g. from a module instantiation) -- that coincide with an prefix of @pars@. checkConstructorParameters :: MonadTCM tcm => QName -> QName -> Args -> tcm () checkConstructorParameters c d pars = do dc <- liftTCM $ getConstructorData c checkParameters dc d pars -- | Check that given parameters match the parameters of the inferred -- constructor/projection. checkParameters :: MonadTCM tcm => QName -- ^ The record/data type name of the chosen constructor/projection. -> QName -- ^ The record/data type name as supplied by the type signature. -> Args -- ^ The parameters. -> tcm () checkParameters dc d pars = liftTCM $ do a <- reduce (Def dc []) case ignoreSharing a of Def d0 es -> do -- compare parameters let vs = fromMaybe __IMPOSSIBLE__ $ allApplyElims es reportSDoc "tc.lhs.split" 40 $ vcat [ nest 2 $ text "d =" <+> (text . prettyShow) d , nest 2 $ text "d0 (should be == d) =" <+> (text . prettyShow) d0 , nest 2 $ text "dc =" <+> (text . prettyShow) dc , nest 2 $ text "vs =" <+> prettyTCM vs ] -- when (d0 /= d) __IMPOSSIBLE__ -- d could have extra qualification t <- typeOfConst d compareArgs [] t (Def d []) vs (take (length vs) pars) _ -> __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Builtin/0000755000000000000000000000000013154613124020205 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Builtin/Coinduction.hs0000644000000000000000000001474013154613124023025 0ustar0000000000000000-- {-# LANGUAGE CPP #-} ------------------------------------------------------------------------ -- | Handling of the INFINITY, SHARP and FLAT builtins. ------------------------------------------------------------------------ module Agda.TypeChecking.Rules.Builtin.Coinduction where import Control.Applicative import qualified Data.Map as Map import qualified Data.Set as Set import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Position import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Level import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Primitive import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.Rules.Builtin import Agda.TypeChecking.Rules.Term import Agda.Utils.Permutation -- | The type of @∞@. typeOfInf :: TCM Type typeOfInf = hPi "a" (el primLevel) $ (return . sort $ varSort 0) --> (return . sort $ varSort 0) -- | The type of @♯_@. typeOfSharp :: TCM Type typeOfSharp = hPi "a" (el primLevel) $ hPi "A" (return . sort $ varSort 0) $ (El (varSort 1) <$> varM 0) --> (El (varSort 1) <$> primInf <#> varM 1 <@> varM 0) -- | The type of @♭@. typeOfFlat :: TCM Type typeOfFlat = hPi "a" (el primLevel) $ hPi "A" (return . sort $ varSort 0) $ (El (varSort 1) <$> primInf <#> varM 1 <@> varM 0) --> (El (varSort 1) <$> varM 0) -- | Binds the INFINITY builtin, but does not change the type's -- definition. bindBuiltinInf :: A.Expr -> TCM () bindBuiltinInf e = bindPostulatedName builtinInf e $ \inf _ -> instantiateFull =<< checkExpr (A.Def inf) =<< typeOfInf -- | Binds the SHARP builtin, and changes the definitions of INFINITY -- and SHARP. -- The following (no longer supported) definition is used: -- -- codata ∞ {a} (A : Set a) : Set a where -- ♯_ : (x : A) → ∞ A bindBuiltinSharp :: A.Expr -> TCM () bindBuiltinSharp e = bindPostulatedName builtinSharp e $ \sharp sharpDefn -> do sharpType <- typeOfSharp TelV fieldTel _ <- telView sharpType sharpE <- instantiateFull =<< checkExpr (A.Def sharp) sharpType Def inf _ <- ignoreSharing <$> primInf infDefn <- getConstInfo inf addConstant (defName infDefn) $ infDefn { defPolarity = [] -- not monotone , defArgOccurrences = [Unused, StrictPos] , theDef = Record { recPars = 2 , recInduction = Just CoInductive , recClause = Nothing , recConHead = ConHead sharp CoInductive [] -- flat is added later , recNamedCon = True , recFields = [] -- flat is added later , recTel = fieldTel , recEtaEquality' = Inferred False , recMutual = Just [] , recAbstr = ConcreteDef } } addConstant sharp $ sharpDefn { theDef = Constructor { conPars = 2 , conArity = 1 , conSrcCon = ConHead sharp CoInductive [] -- flat is added as field later , conData = defName infDefn , conAbstr = ConcreteDef , conInd = CoInductive , conErased = [] } } return sharpE -- | Binds the FLAT builtin, and changes its definition. -- The following (no longer supported) definition is used: -- -- ♭ : ∀ {a} {A : Set a} → ∞ A → A -- ♭ (♯ x) = x bindBuiltinFlat :: A.Expr -> TCM () bindBuiltinFlat e = bindPostulatedName builtinFlat e $ \ flat flatDefn -> do flatE <- instantiateFull =<< checkExpr (A.Def flat) =<< typeOfFlat Def sharp _ <- ignoreSharing <$> primSharp kit <- requireLevels Def inf _ <- ignoreSharing <$> primInf let sharpCon = ConHead sharp CoInductive [flat] level = El (mkType 0) $ Def (typeName kit) [] tel :: Telescope tel = ExtendTel (domH $ level) $ Abs "a" $ ExtendTel (domH $ sort $ varSort 0) $ Abs "A" $ ExtendTel (domN $ El (varSort 1) $ var 0) $ Abs "x" $ EmptyTel infA = El (varSort 2) $ Def inf [ Apply $ defaultArg $ var 1 ] cpi = ConPatternInfo Nothing $ Just $ defaultArg infA let clause = Clause { clauseLHSRange = noRange , clauseFullRange = noRange , clauseTel = tel , namedClausePats = [ argN $ Named Nothing $ ConP sharpCon cpi [ argN $ Named Nothing $ debruijnNamedVar "x" 0 ] ] , clauseBody = Just $ var 0 , clauseType = Just $ defaultArg $ El (varSort 2) $ var 1 , clauseCatchall = False , clauseUnreachable = Just False } cc = Case (defaultArg 0) $ Branches False (Map.singleton sharp $ WithArity 1 $ Done [defaultArg "x"] $ var 0) Map.empty Nothing projection = Projection { projProper = Just inf , projOrig = flat , projFromType = defaultArg inf , projIndex = 3 , projLams = ProjLams $ [ argH "a" , argH "A" , argN "x" ] } addConstant flat $ flatDefn { defPolarity = [] , defArgOccurrences = [StrictPos] -- changing that to [Mixed] destroys monotonicity of 'Rec' in test/succeed/GuardednessPreservingTypeConstructors , theDef = emptyFunction { funClauses = [clause] , funCompiled = Just $ cc , funProjection = Just projection , funTerminates = Just True , funCopatternLHS = isCopatternLHS [clause] } } -- register flat as record field for constructor sharp modifySignature $ updateDefinition sharp $ updateTheDef $ \ def -> def { conSrcCon = sharpCon } modifySignature $ updateDefinition inf $ updateTheDef $ \ def -> def { recConHead = sharpCon, recFields = [defaultArg flat] } return flatE -- The coinductive primitives. -- moved to TypeChecking.Monad.Builtin Agda-2.5.3/src/full/Agda/TypeChecking/Rules/Builtin/Coinduction.hs-boot0000644000000000000000000000070113154613124023756 0ustar0000000000000000module Agda.TypeChecking.Rules.Builtin.Coinduction where import Agda.Syntax.Abstract import Agda.TypeChecking.Monad bindBuiltinInf :: Expr -> TCM () bindBuiltinSharp :: Expr -> TCM () bindBuiltinFlat :: Expr -> TCM () {- MOVED to TypeChecking.Monad.Builtin data CoinductionKit nameOfInf :: CoinductionKit -> QName nameOfSharp :: CoinductionKit -> QName nameOfFlat :: CoinductionKit -> QName coinductionKit :: TCM (Maybe CoinductionKit) -} Agda-2.5.3/src/full/Agda/TypeChecking/Patterns/0000755000000000000000000000000013154613124017305 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Patterns/Match.hs0000644000000000000000000002433713154613124020706 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} -- | Pattern matcher used in the reducer for clauses that -- have not been compiled to case trees yet. module Agda.TypeChecking.Patterns.Match where import Prelude hiding (null) import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import Data.Monoid import Data.Traversable (traverse) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Reduce import Agda.TypeChecking.Reduce.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Monad import Agda.TypeChecking.Pretty import Agda.TypeChecking.Records import Agda.TypeChecking.Datatypes import Agda.Utils.Empty import Agda.Utils.Functor (for, ($>)) import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Singleton import Agda.Utils.Size import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- | If matching is inconclusive (@DontKnow@) we want to know whether -- it is due to a particular meta variable. data Match a = Yes Simplification (IntMap (Arg a)) | No | DontKnow (Blocked ()) deriving Functor instance Null (Match a) where empty = Yes empty empty null (Yes simpl as) = null simpl && null as null _ = False matchedArgs :: Empty -> Int -> IntMap (Arg a) -> [Arg a] matchedArgs err n vs = map get [0..n-1] where get k = fromMaybe (absurd err) $ IntMap.lookup k vs -- | Builds a proper substitution from an IntMap produced by match(Co)patterns buildSubstitution :: (DeBruijn a) => Empty -> Int -> IntMap (Arg a) -> Substitution' a buildSubstitution err n vs = parallelS $ map unArg $ matchedArgs err n vs -- 'mappend' is UNUSED. -- -- instance Monoid (Match a) where -- mempty = Yes mempty [] -- Yes s us `mappend` Yes s' vs = Yes (s `mappend` s') (us ++ vs) -- Yes _ _ `mappend` No = No -- Yes _ _ `mappend` DontKnow m = DontKnow m -- No `mappend` _ = No -- -- @NotBlocked (StuckOn e)@ means blocked by a variable. -- -- In this case, no instantiation of -- -- meta-variables will make progress. -- DontKnow b `mappend` DontKnow b' = DontKnow $ b `mappend` b' -- -- One could imagine DontKnow _ `mappend` No = No, but would break the -- -- equivalence to case-trees. -- DontKnow m `mappend` _ = DontKnow m -- | Instead of 'zipWithM', we need to use this lazy version -- of combining pattern matching computations. -- Andreas, 2014-05-08, see Issue 1124: -- -- Due to a bug in TypeChecking.Patterns.Match -- a failed match of (C n b) against (C O unit) -- turned into (C n unit). -- This was because all patterns were matched in -- parallel, and evaluations of successfull matches -- (and a record constructor like unit can always -- be successfully matched) were returned, leading -- to a reassembly of (C n b) as (C n unit) which is -- illtyped. -- Now patterns are matched left to right and -- upon failure, no further matching is performed. foldMatch :: forall p v . (p -> v -> ReduceM (Match Term, v)) -> [p] -> [v] -> ReduceM (Match Term, [v]) foldMatch match = loop where loop :: [p] -> [v] -> ReduceM (Match Term, [v]) loop ps0 vs0 = do case (ps0, vs0) of ([], []) -> return (empty, []) (p : ps, v : vs) -> do (r, v') <- match p v case r of No -> return (No , v' : vs) DontKnow m -> return (DontKnow m, v' : vs) Yes s us -> do (r', vs') <- loop ps vs let vs1 = v' : vs' case r' of Yes s' us' -> return (Yes (s `mappend` s') (us `mappend` us'), vs1) No -> return (No , vs1) DontKnow m -> return (DontKnow m , vs1) _ -> __IMPOSSIBLE__ -- | @matchCopatterns ps es@ matches spine @es@ against copattern spine @ps@. -- -- Returns 'Yes' and a substitution for the pattern variables -- (in form of IntMap Term) if matching was successful. -- -- Returns 'No' if there was a constructor or projection mismatch. -- -- Returns 'DontKnow' if an argument could not be evaluated to -- constructor form because of a blocking meta variable. -- -- In any case, also returns spine @es@ in reduced form -- (with all the weak head reductions performed that were necessary -- to come to a decision). matchCopatterns :: [NamedArg DeBruijnPattern] -> [Elim] -> ReduceM (Match Term, [Elim]) matchCopatterns ps vs = do traceSDoc "tc.match" 50 (vcat [ text "matchCopatterns" , nest 2 $ text "ps =" <+> fsep (punctuate comma $ map (prettyTCM . namedArg) ps) , nest 2 $ text "vs =" <+> fsep (punctuate comma $ map prettyTCM vs) ]) $ do -- Buggy, see issue 1124: -- mapFst mconcat . unzip <$> zipWithM' (matchCopattern . namedArg) ps vs foldMatch (matchCopattern . namedArg) ps vs -- | Match a single copattern. matchCopattern :: DeBruijnPattern -> Elim -> ReduceM (Match Term, Elim) matchCopattern pat@ProjP{} elim@(Proj _ q) = do ProjP _ p <- normaliseProjP pat q <- getOriginalProjection q return $ if p == q then (Yes YesSimplification empty, elim) else (No, elim) matchCopattern ProjP{} Apply{} = __IMPOSSIBLE__ matchCopattern _ Proj{} = __IMPOSSIBLE__ matchCopattern p (Apply v) = mapSnd Apply <$> matchPattern p v matchPatterns :: [NamedArg DeBruijnPattern] -> [Arg Term] -> ReduceM (Match Term, [Arg Term]) matchPatterns ps vs = do traceSDoc "tc.match" 50 (vcat [ text "matchPatterns" , nest 2 $ text "ps =" <+> fsep (punctuate comma $ map (text . show) ps) , nest 2 $ text "vs =" <+> fsep (punctuate comma $ map prettyTCM vs) ]) $ do -- Buggy, see issue 1124: -- (ms,vs) <- unzip <$> zipWithM' (matchPattern . namedArg) ps vs -- return (mconcat ms, vs) foldMatch (matchPattern . namedArg) ps vs -- | Match a single pattern. matchPattern :: DeBruijnPattern -> Arg Term -> ReduceM (Match Term, Arg Term) matchPattern p u = case (p, u) of (ProjP{}, _ ) -> __IMPOSSIBLE__ (VarP x , arg ) -> return (Yes NoSimplification entry, arg) where entry = singleton (dbPatVarIndex x, arg) (DotP _ , arg@(Arg _ v)) -> return (Yes NoSimplification empty, arg) (AbsurdP _ , arg@(Arg _ v)) -> return (Yes NoSimplification empty, arg) (LitP l , arg@(Arg _ v)) -> do w <- reduceB' v let arg' = arg $> ignoreBlocking w case ignoreSharing <$> w of NotBlocked _ (Lit l') | l == l' -> return (Yes YesSimplification empty , arg') | otherwise -> return (No , arg') NotBlocked _ (MetaV x _) -> return (DontKnow $ Blocked x () , arg') Blocked x _ -> return (DontKnow $ Blocked x () , arg') NotBlocked r t -> return (DontKnow $ NotBlocked r' () , arg') where r' = stuckOn (Apply arg') r -- Case constructor pattern. (ConP c cpi ps, Arg info v) -> do if isNothing $ conPRecord cpi then fallback else do isEtaRecordCon (conName c) >>= \case Nothing -> fallback Just fs -> do -- Case: Eta record constructor. -- This case is necessary if we want to use the clauses before -- record pattern translation (e.g., in type-checking definitions by copatterns). unless (size fs == size ps) __IMPOSSIBLE__ mapSnd (Arg info . Con c (fromConPatternInfo cpi)) <$> do matchPatterns ps $ for fs $ \ (Arg ai f) -> Arg ai $ v `applyE` [Proj ProjSystem f] where isEtaRecordCon :: QName -> ReduceM (Maybe [Arg QName]) isEtaRecordCon c = do (theDef <$> getConstInfo c) >>= \case Constructor{ conData = d } -> do (theDef <$> getConstInfo d) >>= \case r@Record{ recFields = fs } | recEtaEquality r -> return $ Just fs _ -> return Nothing _ -> __IMPOSSIBLE__ -- Default: not an eta record constructor. fallback = do w <- reduceB' v -- Unfold delayed (corecursive) definitions one step. This is -- only necessary if c is a coinductive constructor, but -- 1) it does not hurt to do it all the time, and -- 2) whatInduction c sometimes crashes because c may point to -- an axiom at this stage (if we are checking the -- projection functions for a record type). {- w <- case ignoreSharing <$> w of NotBlocked r (Def f es) -> -- Andreas, 2014-06-12 TODO: r == ReallyNotBlocked sufficient? unfoldDefinitionE True reduceB' (Def f []) f es -- reduceB is used here because some constructors -- are actually definitions which need to be -- unfolded (due to open public). _ -> return w -} -- Jesper, 23-06-2016: Note that unfoldCorecursion may destroy -- constructor forms, so we only call constructorForm after. w <- traverse constructorForm =<< case w of NotBlocked r u -> unfoldCorecursion u -- Andreas, 2014-06-12 TODO: r == ReallyNotBlocked sufficient? _ -> return w let v = ignoreBlocking w arg = Arg info v -- the reduced argument case ignoreSharing <$> w of NotBlocked _ (Con c' ci vs) | c == c' -> do (m, vs) <- yesSimplification <$> matchPatterns ps vs return (m, Arg info $ Con c' ci vs) | otherwise -> return (No , arg) NotBlocked _ (MetaV x vs) -> return (DontKnow $ Blocked x () , arg) Blocked x _ -> return (DontKnow $ Blocked x () , arg) NotBlocked r _ -> return (DontKnow $ NotBlocked r' () , arg) where r' = stuckOn (Apply arg) r -- ASR (08 November 2014). The type of the function could be -- -- @(Match Term, [Arg Term]) -> (Match Term, [Arg Term])@. yesSimplification :: (Match a, b) -> (Match a, b) yesSimplification (Yes _ vs, us) = (Yes YesSimplification vs, us) yesSimplification r = r Agda-2.5.3/src/full/Agda/TypeChecking/Patterns/Match.hs-boot0000644000000000000000000000117213154613124021637 0ustar0000000000000000 module Agda.TypeChecking.Patterns.Match where import Data.IntMap (IntMap) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad import {-# SOURCE #-} Agda.TypeChecking.Pretty (PrettyTCM) import Agda.TypeChecking.Substitute (DeBruijn) import Agda.Utils.Empty data Match a = Yes Simplification (IntMap (Arg a)) | No | DontKnow (Blocked ()) buildSubstitution :: (DeBruijn a) => Empty -> Int -> IntMap (Arg a) -> Substitution' a matchPatterns :: [NamedArg DeBruijnPattern] -> Args -> ReduceM (Match Term, Args) matchCopatterns :: [NamedArg DeBruijnPattern] -> Elims -> ReduceM (Match Term, Elims) Agda-2.5.3/src/full/Agda/TypeChecking/Patterns/Abstract.hs0000644000000000000000000000774213154613124021416 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE TypeFamilies #-} -- | Tools to manipulate patterns in abstract syntax -- in the TCM (type checking monad). module Agda.TypeChecking.Patterns.Abstract where import qualified Data.List as List import Data.Traversable hiding (mapM, sequence) import Data.Void import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Pattern import Agda.Syntax.Abstract.Views import Agda.Syntax.Concrete (FieldAssignment') import Agda.Syntax.Common import Agda.Syntax.Info as A import Agda.Syntax.Internal as I import Agda.Syntax.Literal import Agda.Syntax.Position import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.Utils.Functor #include "undefined.h" import Agda.Utils.Impossible -- | Expand literal integer pattern into suc/zero constructor patterns. -- expandLitPattern :: NamedArg A.Pattern -> TCM (NamedArg A.Pattern) expandLitPattern p = traverse (traverse expand) p where expand p = case asView p of (xs, A.LitP (LitNat r n)) | n < 0 -> negLit -- Andreas, issue #2365, negative literals not yet supported. | n > 20 -> tooBig | otherwise -> do Con z _ _ <- ignoreSharing <$> primZero Con s _ _ <- ignoreSharing <$> primSuc let zero = A.ConP cinfo (A.AmbQ [setRange r $ conName z]) [] suc p = A.ConP cinfo (A.AmbQ [setRange r $ conName s]) [defaultNamedArg p] info = A.PatRange r cinfo = A.ConPatInfo ConOCon info p' = foldr ($) zero $ List.genericReplicate n suc return $ foldr (A.AsP info) p' xs _ -> return p tooBig = typeError $ GenericError $ "Matching on natural number literals is done by expanding " ++ "the literal to the corresponding constructor pattern, so " ++ "you probably don't want to do it this way." negLit = typeError $ GenericError $ "Negative literals are not supported in patterns" -- | Expand away (deeply) all pattern synonyms in a pattern. -- Unfortunately, the more general type signature -- -- expandPatternSynonyms :: forall a p . APatternLike a p => p -> TCM p -- -- is rejected by GHC 7.10 -- -- Could not deduce (APatternLike A.Expr p) -- arising from a use of ‘postTraverseAPatternM’ -- -- I am mystified (Andreas, 2017-07-27) -- expandPatternSynonyms :: forall a p . APatternLike a p => p -> TCM p -- As a workaround, we define this function only for a = A.Exp, p = A.Pattern' -- and keep the type class ExpandPatternSynonyms (which would otherwise be superfluous). expandPatternSynonyms' :: A.Pattern -> TCM A.Pattern expandPatternSynonyms' = postTraverseAPatternM $ \case A.PatternSynP i x as -> setCurrentRange i $ do (ns, p) <- killRange <$> lookupPatternSyn x -- Must expand arguments before instantiating otherwise pattern -- synonyms could get into dot patterns (which is __IMPOSSIBLE__). p :: A.Pattern <- expandPatternSynonyms' (vacuous p :: A.Pattern) case A.insertImplicitPatSynArgs (A.WildP . PatRange) (getRange x) ns as of Nothing -> typeError $ BadArgumentsToPatternSynonym x Just (_, _:_) -> typeError $ TooFewArgumentsToPatternSynonym x Just (s, []) -> return $ setRange (getRange i) $ A.substPattern s p p -> return p class ExpandPatternSynonyms a where expandPatternSynonyms :: a -> TCM a default expandPatternSynonyms :: (Traversable f, ExpandPatternSynonyms b, f b ~ a) => a -> TCM a expandPatternSynonyms = traverse expandPatternSynonyms instance ExpandPatternSynonyms a => ExpandPatternSynonyms (Maybe a) where instance ExpandPatternSynonyms a => ExpandPatternSynonyms [a] where instance ExpandPatternSynonyms a => ExpandPatternSynonyms (Arg a) where instance ExpandPatternSynonyms a => ExpandPatternSynonyms (Named n a) where instance ExpandPatternSynonyms a => ExpandPatternSynonyms (FieldAssignment' a) where instance ExpandPatternSynonyms A.Pattern where expandPatternSynonyms = expandPatternSynonyms' Agda-2.5.3/src/full/Agda/TypeChecking/Serialise/0000755000000000000000000000000013154613124017425 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Serialise/Instances.hs0000644000000000000000000000151713154613124021714 0ustar0000000000000000{-# OPTIONS_GHC -fno-warn-orphans #-} -- Only instances exported module Agda.TypeChecking.Serialise.Instances () where import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Serialise.Base import Agda.TypeChecking.Serialise.Instances.Abstract () import Agda.TypeChecking.Serialise.Instances.Common () import Agda.TypeChecking.Serialise.Instances.Compilers () import Agda.TypeChecking.Serialise.Instances.Highlighting () import Agda.TypeChecking.Serialise.Instances.Internal () import Agda.TypeChecking.Serialise.Instances.Errors () instance EmbPrj Interface where icod_ (Interface a b c d e f g h i j k l m) = icodeN' Interface a b c d e f g h i j k l m value = vcase valu where valu [a, b, c, d, e, f, g, h, i, j, k, l, m] = valuN Interface a b c d e f g h i j k l m valu _ = malformed Agda-2.5.3/src/full/Agda/TypeChecking/Serialise/Base.hs0000644000000000000000000003465713154613124020652 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-} #if __GLASGOW_HASKELL__ <= 708 {-# OPTIONS_GHC -fcontext-stack=30 #-} #endif module Agda.TypeChecking.Serialise.Base where import Control.Applicative import Control.Monad import Control.Monad.Reader import Control.Monad.State.Strict (StateT, gets) import Data.Proxy import Data.Array.IArray import qualified Data.ByteString.Lazy as L import Data.Hashable import qualified Data.HashTable.IO as H import Data.Int (Int32) import Data.Maybe import qualified Data.Binary as B import qualified Data.Binary.Get as B import Data.Typeable ( cast, Typeable, typeOf, TypeRep ) import Agda.Syntax.Common (NameId) import Agda.Syntax.Internal (Term, QName(..), ModuleName(..), nameId) import Agda.TypeChecking.Monad.Base (TypeError(GenericError), ModuleToSource) import Agda.Utils.FileName import Agda.Utils.IORef import Agda.Utils.Lens import Agda.Utils.Monad import Agda.Utils.Pointer import Agda.Utils.Except (ExceptT, throwError) import Agda.Utils.TypeLevel -- | Constructor tag (maybe omitted) and argument indices. type Node = [Int32] -- | The type of hashtables used in this module. -- -- A very limited amount of testing indicates that 'H.CuckooHashTable' -- is somewhat slower than 'H.BasicHashTable', and that -- 'H.LinearHashTable' and the hashtables from "Data.Hashtable" are -- much slower. #if defined(mingw32_HOST_OS) && defined(x86_64_HOST_ARCH) type HashTable k v = H.CuckooHashTable k v #else type HashTable k v = H.BasicHashTable k v #endif -- | Structure providing fresh identifiers for hash map -- and counting hash map hits (i.e. when no fresh identifier required). data FreshAndReuse = FreshAndReuse { farFresh :: !Int32 -- ^ Number of hash map misses. , farReuse :: !Int32 -- ^ Number of hash map hits. } farEmpty :: FreshAndReuse farEmpty = FreshAndReuse 0 0 lensFresh :: Lens' Int32 FreshAndReuse lensFresh f r = f (farFresh r) <&> \ i -> r { farFresh = i } lensReuse :: Lens' Int32 FreshAndReuse lensReuse f r = f (farReuse r) <&> \ i -> r { farReuse = i } -- | Two 'QName's are equal if their @QNameId@ is equal. type QNameId = [NameId] -- | Computing a qualified names composed ID. qnameId :: QName -> QNameId qnameId (QName (MName ns) n) = map nameId $ n:ns -- | State of the the encoder. data Dict = Dict -- Dictionaries which are serialized: { nodeD :: !(HashTable Node Int32) -- ^ Written to interface file. , stringD :: !(HashTable String Int32) -- ^ Written to interface file. , bstringD :: !(HashTable L.ByteString Int32) -- ^ Written to interface file. , integerD :: !(HashTable Integer Int32) -- ^ Written to interface file. , doubleD :: !(HashTable Double Int32) -- ^ Written to interface file. -- Dicitionaries which are not serialized, but provide -- short cuts to speed up serialization: , termD :: !(HashTable (Ptr Term) Int32) -- ^ Not written to interface file. -- Andreas, Makoto, AIM XXI -- Memoizing A.Name does not buy us much if we already memoize A.QName. , nameD :: !(HashTable NameId Int32) -- ^ Not written to interface file. , qnameD :: !(HashTable QNameId Int32) -- ^ Not written to interface file. -- Fresh UIDs and reuse statistics: , nodeC :: !(IORef FreshAndReuse) -- counters for fresh indexes , stringC :: !(IORef FreshAndReuse) , bstringC :: !(IORef FreshAndReuse) , integerC :: !(IORef FreshAndReuse) , doubleC :: !(IORef FreshAndReuse) , termC :: !(IORef FreshAndReuse) , nameC :: !(IORef FreshAndReuse) , qnameC :: !(IORef FreshAndReuse) , stats :: !(HashTable String Int) , collectStats :: Bool -- ^ If @True@ collect in @stats@ the quantities of -- calls to @icode@ for each @Typeable a@. , absPathD :: !(HashTable AbsolutePath Int32) -- ^ Not written to interface file. } -- | Creates an empty dictionary. emptyDict :: Bool -- ^ Collect statistics for @icode@ calls? -> IO Dict emptyDict collectStats = Dict <$> H.new <*> H.new <*> H.new <*> H.new <*> H.new <*> H.new <*> H.new <*> H.new <*> newIORef farEmpty <*> newIORef farEmpty <*> newIORef farEmpty <*> newIORef farEmpty <*> newIORef farEmpty <*> newIORef farEmpty <*> newIORef farEmpty <*> newIORef farEmpty <*> H.new <*> pure collectStats <*> H.new -- | Universal type, wraps everything. data U = forall a . Typeable a => U !a -- | Univeral memo structure, to introduce sharing during decoding type Memo = HashTable (Int32, TypeRep) U -- (node index, type rep) -- | State of the decoder. data St = St { nodeE :: !(Array Int32 Node) -- ^ Obtained from interface file. , stringE :: !(Array Int32 String) -- ^ Obtained from interface file. , bstringE :: !(Array Int32 L.ByteString) -- ^ Obtained from interface file. , integerE :: !(Array Int32 Integer) -- ^ Obtained from interface file. , doubleE :: !(Array Int32 Double) -- ^ Obtained from interface file. , nodeMemo :: !Memo -- ^ Created and modified by decoder. -- Used to introduce sharing while deserializing objects. , modFile :: !ModuleToSource -- ^ Maps module names to file names. Constructed by the decoder. , includes :: [AbsolutePath] -- ^ The include directories. , mkShared :: Term -> Term } -- | Monad used by the encoder. type S a = ReaderT Dict IO a -- | Monad used by the decoder. -- -- 'TCM' is not used because the associated overheads would make -- decoding slower. type R a = ExceptT TypeError (StateT St IO) a -- | Throws an error which is suitable when the data stream is -- malformed. malformed :: R a malformed = throwError $ GenericError "Malformed input." class Typeable a => EmbPrj a where icode :: a -> S Int32 -- ^ Serialization (wrapper). icod_ :: a -> S Int32 -- ^ Serialization (worker). value :: Int32 -> R a -- ^ Deserialization. icode a = do tickICode a icod_ a -- | Increase entry for @a@ in 'stats'. tickICode :: forall a. Typeable a => a -> S () tickICode _ = whenM (asks collectStats) $ do let key = "icode " ++ show (typeOf (undefined :: a)) hmap <- asks stats liftIO $ do n <- fromMaybe 0 <$> H.lookup hmap key H.insert hmap key $! n + 1 -- | Data.Binary.runGetState is deprecated in favour of runGetIncremental. -- Reimplementing it in terms of the new function. The new Decoder type contains -- strict byte strings so we need to be careful not to feed the entire lazy byte -- string to the decoder at once. runGetState :: B.Get a -> L.ByteString -> B.ByteOffset -> (a, L.ByteString, B.ByteOffset) runGetState g s n = feed (B.runGetIncremental g) (L.toChunks s) where feed (B.Done s n' x) ss = (x, L.fromChunks (s : ss), n + n') feed (B.Fail _ _ err) _ = error err feed (B.Partial f) (s : ss) = feed (f $ Just s) ss feed (B.Partial f) [] = feed (f Nothing) [] -- Specializing icodeX leads to Warning like -- src/full/Agda/TypeChecking/Serialise.hs:1297:1: Warning: -- RULE left-hand side too complicated to desugar -- case cobox_aQY5 of _ [Occ=Dead] { ghc-prim:GHC.Types.Eq# cobox -> -- icodeX @ String $dEq_aQY3 $dHashable_aQY4 -- } -- -- type ICodeX k -- = (Dict -> HashTable k Int32) -- -> (Dict -> IORef Int32) -- -> k -> S Int32 -- {-# SPECIALIZE icodeX :: ICodeX String #-} -- {-# SPECIALIZE icodeX :: ICodeX Integer #-} -- {-# SPECIALIZE icodeX :: ICodeX Double #-} -- {-# SPECIALIZE icodeX :: ICodeX Node #-} -- Andreas, 2014-10-16 AIM XX: -- Inlining this increases Serialization time by 10% -- Makoto's theory: code size increase might lead to -- instruction cache misses. -- {-# INLINE icodeX #-} icodeX :: (Eq k, Hashable k) => (Dict -> HashTable k Int32) -> (Dict -> IORef FreshAndReuse) -> k -> S Int32 icodeX dict counter key = do d <- asks dict c <- asks counter liftIO $ do mi <- H.lookup d key case mi of Just i -> do modifyIORef' c $ over lensReuse (+1) return i Nothing -> do fresh <- (^.lensFresh) <$> do readModifyIORef' c $ over lensFresh (+1) H.insert d key fresh return fresh -- Instead of inlining icodeX, we manually specialize it to -- its four uses: Integer, String, Double, Node. -- Not a great gain (hardly noticeable), but not harmful. icodeInteger :: Integer -> S Int32 icodeInteger key = do d <- asks integerD c <- asks integerC liftIO $ do mi <- H.lookup d key case mi of Just i -> do modifyIORef' c $ over lensReuse (+1) return i Nothing -> do fresh <- (^.lensFresh) <$> do readModifyIORef' c $ over lensFresh (+1) H.insert d key fresh return fresh icodeDouble :: Double -> S Int32 icodeDouble key = do d <- asks doubleD c <- asks doubleC liftIO $ do mi <- H.lookup d key case mi of Just i -> do modifyIORef' c $ over lensReuse (+1) return i Nothing -> do fresh <- (^.lensFresh) <$> do readModifyIORef' c $ over lensFresh (+1) H.insert d key fresh return fresh icodeString :: String -> S Int32 icodeString key = do d <- asks stringD c <- asks stringC liftIO $ do mi <- H.lookup d key case mi of Just i -> do modifyIORef' c $ over lensReuse (+1) return i Nothing -> do fresh <- (^.lensFresh) <$> do readModifyIORef' c $ over lensFresh (+1) H.insert d key fresh return fresh icodeNode :: Node -> S Int32 icodeNode key = do d <- asks nodeD c <- asks nodeC liftIO $ do mi <- H.lookup d key case mi of Just i -> do modifyIORef' c $ over lensReuse (+1) return i Nothing -> do fresh <- (^.lensFresh) <$> do readModifyIORef' c $ over lensFresh (+1) H.insert d key fresh return fresh -- icodeN :: [Int32] -> S Int32 -- icodeN = icodeX nodeD nodeC -- | @icode@ only if thing has not seen before. icodeMemo :: (Ord a, Hashable a) => (Dict -> HashTable a Int32) -- ^ Memo structure for thing of key @a@. -> (Dict -> IORef FreshAndReuse) -- ^ Statistics. -> a -- ^ Key to the thing. -> S Int32 -- ^ Fallback computation to encode the thing. -> S Int32 -- ^ Encoded thing. icodeMemo getDict getCounter a icodeP = do h <- asks getDict mi <- liftIO $ H.lookup h a st <- asks getCounter case mi of Just i -> liftIO $ do modifyIORef' st $ over lensReuse (+ 1) return i Nothing -> do liftIO $ modifyIORef' st $ over lensFresh (+1) i <- icodeP liftIO $ H.insert h a i return i {-# INLINE vcase #-} -- | @vcase value ix@ decodes thing represented by @ix :: Int32@ -- via the @valu@ function and stores it in 'nodeMemo'. -- If @ix@ is present in 'nodeMemo', @valu@ is not used, but -- the thing is read from 'nodeMemo' instead. vcase :: forall a . EmbPrj a => (Node -> R a) -> Int32 -> R a vcase valu = \ix -> do memo <- gets nodeMemo -- compute run-time representation of type a let aTyp = typeOf (undefined :: a) -- to introduce sharing, see if we have seen a thing -- represented by ix before maybeU <- liftIO $ H.lookup memo (ix, aTyp) case maybeU of -- yes, we have seen it before, use the version from memo Just (U u) -> maybe malformed return (cast u) -- no, it's new, so generate it via valu and insert it into memo Nothing -> do v <- valu . (! ix) =<< gets nodeE liftIO $ H.insert memo (ix, aTyp) (U v) return v -- | @icodeArgs proxy (a1, ..., an)@ maps @icode@ over @a1@, ..., @an@ -- and returns the corresponding list of @Int32@. class ICODE t b where icodeArgs :: IsBase t ~ b => All EmbPrj (Domains t) => Proxy t -> Products (Domains t) -> S [Int32] instance IsBase t ~ 'True => ICODE t 'True where icodeArgs _ _ = return [] instance ICODE t (IsBase t) => ICODE (a -> t) 'False where icodeArgs _ (a , as) = icode a >>= \ hd -> (hd :) <$> icodeArgs (Proxy :: Proxy t) as -- | @icodeN tag t a1 ... an@ serialises the arguments @a1@, ..., @an@ of the -- constructor @t@ together with a tag @tag@ picked to disambiguate between -- different constructors. -- It corresponds to @icodeNode . (tag :) =<< mapM icode [a1, ..., an]@ {-# INLINE icodeN #-} icodeN :: forall t. ICODE t (IsBase t) => Currying (Domains t) (S Int32) => All EmbPrj (Domains t) => Int32 -> t -> Arrows (Domains t) (S Int32) icodeN tag _ = currys (Proxy :: Proxy (Domains t)) (Proxy :: Proxy (S Int32)) $ \ args -> icodeNode . (tag :) =<< icodeArgs (Proxy :: Proxy t) args -- | @icodeN'@ is the same as @icodeN@ except that there is no tag {-# INLINE icodeN' #-} icodeN' :: forall t. ICODE t (IsBase t) => Currying (Domains t) (S Int32) => All EmbPrj (Domains t) => t -> Arrows (Domains t) (S Int32) icodeN' _ = currys (Proxy :: Proxy (Domains t)) (Proxy :: Proxy (S Int32)) $ \ args -> icodeNode =<< icodeArgs (Proxy :: Proxy t) args -- Instead of having up to 25 versions of @valu N@, we define -- the class VALU which generates them by typeclass resolution. -- All of these should get inlined at compile time. class VALU t b where valuN' :: b ~ IsBase t => All EmbPrj (Domains t) => t -> Products (Constant Int32 (Domains t)) -> R (CoDomain t) valueArgs :: b ~ IsBase t => All EmbPrj (CoDomain t ': Domains t) => Proxy t -> Node -> Maybe (Products (Constant Int32 (Domains t))) instance VALU t 'True where valuN' c () = return c valueArgs _ xs = case xs of [] -> Just () _ -> Nothing instance VALU t (IsBase t) => VALU (a -> t) 'False where valuN' c (a, as) = value a >>= \ v -> valuN' (c v) as valueArgs _ xs = case xs of (x : xs') -> (x,) <$> valueArgs (Proxy :: Proxy t) xs' _ -> Nothing {-# INLINE valuN #-} valuN :: forall t. VALU t (IsBase t) => Currying (Constant Int32 (Domains t)) (R (CoDomain t)) => All EmbPrj (Domains t) => t -> Arrows (Constant Int32 (Domains t)) (R (CoDomain t)) valuN f = currys (Proxy :: Proxy (Constant Int32 (Domains t))) (Proxy :: Proxy (R (CoDomain t))) (valuN' f) {-# INLINE valueN #-} valueN :: forall t. VALU t (IsBase t) => All EmbPrj (CoDomain t ': Domains t) => t -> Int32 -> R (CoDomain t) valueN t = vcase valu where valu int32s = case valueArgs (Proxy :: Proxy t) int32s of Nothing -> malformed Just vs -> valuN' t vs Agda-2.5.3/src/full/Agda/TypeChecking/Serialise/Instances/0000755000000000000000000000000013154613124021354 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Serialise/Instances/Common.hs0000644000000000000000000003427413154613124023152 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE OverlappingInstances #-} #endif {-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.TypeChecking.Serialise.Instances.Common (SerialisedRange(..)) where import Prelude hiding (mapM) import Control.Applicative import Control.Monad.Reader hiding (mapM) import Control.Monad.State.Strict (gets, modify) import Control.Exception import Data.Array.IArray import Data.Word import qualified Data.ByteString.Lazy as L import qualified Data.Foldable as Fold import Data.Hashable import qualified Data.HashTable.IO as H import Data.Int (Int32) import Data.Maybe import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import Data.Sequence (Seq) import qualified Data.Sequence as Seq import Data.Traversable ( mapM ) #if __GLASGOW_HASKELL__ <= 708 import Data.Typeable ( Typeable ) #endif import Data.Void import Agda.Syntax.Common import Agda.Syntax.Concrete.Name as C import qualified Agda.Syntax.Concrete as C import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Position as P import Agda.Syntax.Fixity import Agda.Syntax.Notation import Agda.Syntax.Literal import Agda.Interaction.FindFile import Agda.TypeChecking.Serialise.Base import Agda.Utils.BiMap (BiMap) import qualified Agda.Utils.BiMap as BiMap import Agda.Utils.HashMap (HashMap) import qualified Agda.Utils.HashMap as HMap import Agda.Utils.FileName import Agda.Utils.Maybe import qualified Agda.Utils.Maybe.Strict as Strict import Agda.Utils.Trie import Agda.Utils.Except import Agda.Utils.Empty (Empty) import qualified Agda.Utils.Empty as Empty #include "undefined.h" import Agda.Utils.Impossible #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} EmbPrj String where #else instance EmbPrj String where #endif icod_ = icodeString value i = (! i) `fmap` gets stringE instance EmbPrj L.ByteString where icod_ = icodeX bstringD bstringC value i = (! i) `fmap` gets bstringE instance EmbPrj Integer where icod_ = icodeInteger value i = (! i) `fmap` gets integerE instance EmbPrj Word64 where icod_ i = icodeN' (undefined :: Int32 -> Int32 -> Int32) (int32 q) (int32 r) where (q, r) = quotRem i (2^32) int32 :: Word64 -> Int32 int32 = fromIntegral value = vcase valu where valu [a, b] = return $ n * mod (fromIntegral a) n + mod (fromIntegral b) n valu _ = malformed n = 2^32 instance EmbPrj Int32 where icod_ i = return i value i = return i instance EmbPrj Int where icod_ i = return (fromIntegral i) value i = return (fromIntegral i) instance EmbPrj Char where icod_ c = return (fromIntegral $ fromEnum c) value i = return (toEnum $ fromInteger $ toInteger i) instance EmbPrj Double where icod_ = icodeDouble value i = (! i) `fmap` gets doubleE instance EmbPrj Void where icod_ = absurd value = vcase valu where valu _ = malformed instance EmbPrj () where icod_ () = icodeN' () value = vcase valu where valu [] = valuN () valu _ = malformed instance (EmbPrj a, EmbPrj b) => EmbPrj (a, b) where icod_ (a, b) = icodeN' (,) a b value = valueN (,) instance (EmbPrj a, EmbPrj b, EmbPrj c) => EmbPrj (a, b, c) where icod_ (a, b, c) = icodeN' (,,) a b c value = valueN (,,) instance (EmbPrj a, EmbPrj b) => EmbPrj (Either a b) where icod_ (Left x) = icodeN 0 Left x icod_ (Right x) = icodeN 1 Right x value = vcase valu where valu [0, x] = valuN Left x valu [1, x] = valuN Right x valu _ = malformed instance EmbPrj a => EmbPrj (Maybe a) where icod_ Nothing = icodeN' Nothing icod_ (Just x) = icodeN' Just x value = vcase valu where valu [] = valuN Nothing valu [x] = valuN Just x valu _ = malformed instance EmbPrj a => EmbPrj (Strict.Maybe a) where icod_ m = icode (Strict.toLazy m) value m = Strict.toStrict `fmap` value m instance EmbPrj Bool where icod_ True = icodeN' True icod_ False = icodeN 0 False value = vcase valu where valu [] = valuN True valu [0] = valuN False valu _ = malformed instance EmbPrj DataOrRecord where icod_ IsData = icodeN' IsData icod_ IsRecord = icodeN 0 IsRecord value = vcase $ \case [] -> valuN IsData [0] -> valuN IsRecord _ -> malformed instance EmbPrj AbsolutePath where icod_ file = do d <- asks absPathD liftIO $ flip fromMaybeM (H.lookup d file) $ do -- The path @file@ should be cached in the dictionary @d@. -- This seems not to be the case, thus, crash here. -- But leave some hints for the posterity why things could go so wrong. -- reportSLn "impossible" 10 -- does not work here putStrLn $ unlines $ [ "Panic while serializing absolute path: " ++ show file , "The path could not be found in the dictionary:" ] putStrLn . show =<< H.toList d __IMPOSSIBLE__ value m = do m :: TopLevelModuleName <- value m mf <- gets modFile incs <- gets includes (r, mf) <- liftIO $ findFile'' incs m mf modify $ \s -> s { modFile = mf } case r of Left err -> throwError $ findErrorToTypeError m err Right f -> return f instance EmbPrj a => EmbPrj (Position' a) where icod_ (P.Pn file pos line col) = icodeN' P.Pn file pos line col value = valueN P.Pn instance EmbPrj TopLevelModuleName where icod_ (TopLevelModuleName a b) = icodeN' TopLevelModuleName a b value = valueN TopLevelModuleName #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPABLE #-} EmbPrj a => EmbPrj [a] where #else instance EmbPrj a => EmbPrj [a] where #endif icod_ xs = icodeNode =<< mapM icode xs value = vcase (mapM value) -- icode [] = icode0' -- icode (x : xs) = icode2' x xs -- value = vcase valu where valu [] = valu0 [] -- valu [x, xs] = valu2 (:) x xs -- valu _ = malformed instance (Ord a, Ord b, EmbPrj a, EmbPrj b) => EmbPrj (BiMap a b) where icod_ m = icode (BiMap.toList m) value m = BiMap.fromList <$> value m instance (Ord a, EmbPrj a, EmbPrj b) => EmbPrj (Map a b) where icod_ m = icode (Map.toList m) value m = Map.fromList `fmap` value m instance (Ord a, EmbPrj a) => EmbPrj (Set a) where icod_ s = icode (Set.toList s) value s = Set.fromList `fmap` value s instance (Ord a, EmbPrj a, EmbPrj b) => EmbPrj (Trie a b) where icod_ (Trie a b)= icodeN' Trie a b value = valueN Trie instance EmbPrj a => EmbPrj (Seq a) where icod_ s = icode (Fold.toList s) value s = Seq.fromList `fmap` value s instance EmbPrj a => EmbPrj (P.Interval' a) where icod_ (P.Interval p q) = icodeN' P.Interval p q value = valueN P.Interval -- | Ranges are always deserialised as 'noRange'. instance EmbPrj Range where icod_ _ = icodeN' () value _ = return noRange -- | Ranges that should be serialised properly. newtype SerialisedRange = SerialisedRange { underlyingRange :: Range } #if __GLASGOW_HASKELL__ <= 708 deriving (Typeable) #endif instance EmbPrj SerialisedRange where icod_ (SerialisedRange r) = icodeN' (undefined :: SrcFile -> [IntervalWithoutFile] -> SerialisedRange) (P.rangeFile r) (P.rangeIntervals r) value = vcase valu where valu [a, b] = SerialisedRange <$> valuN P.intervalsToRange a b valu _ = malformed instance EmbPrj C.Name where icod_ (C.NoName a b) = icodeN 0 C.NoName a b icod_ (C.Name r xs) = icodeN' C.Name r xs value = vcase valu where valu [0, a, b] = valuN C.NoName a b valu [r, xs] = valuN C.Name r xs valu _ = malformed instance EmbPrj NamePart where icod_ Hole = icodeN' Hole icod_ (Id a) = icodeN' Id a value = vcase valu where valu [] = valuN Hole valu [a] = valuN Id a valu _ = malformed instance EmbPrj C.QName where icod_ (Qual a b) = icodeN' Qual a b icod_ (C.QName a ) = icodeN' C.QName a value = vcase valu where valu [a, b] = valuN Qual a b valu [a] = valuN C.QName a valu _ = malformed instance EmbPrj Agda.Syntax.Fixity.Associativity where icod_ LeftAssoc = icodeN' LeftAssoc icod_ RightAssoc = icodeN 1 RightAssoc icod_ NonAssoc = icodeN 2 NonAssoc value = vcase valu where valu [] = valuN LeftAssoc valu [1] = valuN RightAssoc valu [2] = valuN NonAssoc valu _ = malformed instance EmbPrj Agda.Syntax.Fixity.PrecedenceLevel where icod_ Unrelated = icodeN' Unrelated icod_ (Related a) = icodeN' Related a value = vcase valu where valu [] = valuN Unrelated valu [a] = valuN Related a valu _ = malformed instance EmbPrj Agda.Syntax.Fixity.Fixity where icod_ (Fixity a b c) = icodeN' Fixity a b c value = valueN Fixity instance EmbPrj Agda.Syntax.Fixity.Fixity' where icod_ (Fixity' a b r) = icodeN' (\ a b -> Fixity' a b r) a b -- discard theNameRange value = valueN (\ f n -> Fixity' f n noRange) instance EmbPrj GenPart where icod_ (BindHole a) = icodeN 0 BindHole a icod_ (NormalHole a) = icodeN 1 NormalHole a icod_ (WildHole a) = icodeN 2 WildHole a icod_ (IdPart a) = icodeN' IdPart a value = vcase valu where valu [0, a] = valuN BindHole a valu [1, a] = valuN NormalHole a valu [2, a] = valuN WildHole a valu [a] = valuN IdPart a valu _ = malformed instance EmbPrj MetaId where icod_ (MetaId n) = icod_ n value i = MetaId <$> value i instance EmbPrj A.QName where icod_ n@(A.QName a b) = icodeMemo qnameD qnameC (qnameId n) $ icodeN' A.QName a b value = valueN A.QName instance EmbPrj A.AmbiguousQName where icod_ (A.AmbQ a) = icode a value n = A.AmbQ `fmap` value n instance EmbPrj A.ModuleName where icod_ (A.MName a) = icode a value n = A.MName `fmap` value n instance EmbPrj A.Name where icod_ (A.Name a b c d) = icodeMemo nameD nameC a $ icodeN' (\ a b -> A.Name a b . underlyingRange) a b (SerialisedRange c) d value = valueN (\a b c -> A.Name a b (underlyingRange c)) instance EmbPrj a => EmbPrj (C.FieldAssignment' a) where icod_ (C.FieldAssignment a b) = icodeN' C.FieldAssignment a b value = valueN C.FieldAssignment instance (EmbPrj s, EmbPrj t) => EmbPrj (Named s t) where icod_ (Named a b) = icodeN' Named a b value = valueN Named instance EmbPrj a => EmbPrj (Ranged a) where icod_ (Ranged r x) = icodeN' Ranged r x value = valueN Ranged instance EmbPrj ArgInfo where icod_ (ArgInfo h r o) = icodeN' ArgInfo h r o value = valueN ArgInfo instance EmbPrj NameId where icod_ (NameId a b) = icodeN' NameId a b value = valueN NameId instance (Eq k, Hashable k, EmbPrj k, EmbPrj v) => EmbPrj (HashMap k v) where icod_ m = icode (HMap.toList m) value m = HMap.fromList `fmap` value m instance EmbPrj a => EmbPrj (WithHiding a) where icod_ (WithHiding a b) = icodeN' WithHiding a b value = valueN WithHiding instance EmbPrj a => EmbPrj (Arg a) where icod_ (Arg i e) = icodeN' Arg i e value = valueN Arg instance EmbPrj a => EmbPrj (Dom a) where icod_ (Dom i e) = icodeN' Dom i e value = valueN Dom instance EmbPrj Induction where icod_ Inductive = icodeN' Inductive icod_ CoInductive = icodeN 1 CoInductive value = vcase valu where valu [] = valuN Inductive valu [1] = valuN CoInductive valu _ = malformed instance EmbPrj Hiding where icod_ Hidden = return 0 icod_ NotHidden = return 1 icod_ (Instance NoOverlap) = return 2 icod_ (Instance YesOverlap) = return 3 value 0 = return Hidden value 1 = return NotHidden value 2 = return (Instance NoOverlap) value 3 = return (Instance YesOverlap) value _ = malformed instance EmbPrj Relevance where icod_ Relevant = return 0 icod_ Irrelevant = return 1 icod_ (Forced Small) = return 2 icod_ (Forced Big) = return 3 icod_ NonStrict = return 4 value 0 = return Relevant value 1 = return Irrelevant value 2 = return (Forced Small) value 3 = return (Forced Big) value 4 = return NonStrict value _ = malformed instance EmbPrj Origin where icod_ UserWritten = return 0 icod_ Inserted = return 1 icod_ Reflected = return 2 icod_ CaseSplit = return 3 value 0 = return UserWritten value 1 = return Inserted value 2 = return Reflected value 3 = return CaseSplit value _ = malformed instance EmbPrj ConOrigin where icod_ ConOSystem = return 0 icod_ ConOCon = return 1 icod_ ConORec = return 2 icod_ ConOSplit = return 3 value 0 = return ConOSystem value 1 = return ConOCon value 2 = return ConORec value 3 = return ConOSplit value _ = malformed instance EmbPrj ProjOrigin where icod_ ProjPrefix = return 0 icod_ ProjPostfix = return 1 icod_ ProjSystem = return 2 value 0 = return ProjPrefix value 1 = return ProjPostfix value 2 = return ProjSystem value _ = malformed instance EmbPrj Agda.Syntax.Literal.Literal where icod_ (LitNat a b) = icodeN' LitNat a b icod_ (LitFloat a b) = icodeN 1 LitFloat a b icod_ (LitString a b) = icodeN 2 LitString a b icod_ (LitChar a b) = icodeN 3 LitChar a b icod_ (LitQName a b) = icodeN 5 LitQName a b icod_ (LitMeta a b c) = icodeN 6 LitMeta a b c value = vcase valu where valu [a, b] = valuN LitNat a b valu [1, a, b] = valuN LitFloat a b valu [2, a, b] = valuN LitString a b valu [3, a, b] = valuN LitChar a b valu [5, a, b] = valuN LitQName a b valu [6, a, b, c] = valuN LitMeta a b c valu _ = malformed instance EmbPrj IsAbstract where icod_ AbstractDef = icodeN 0 AbstractDef icod_ ConcreteDef = icodeN' ConcreteDef value = vcase valu where valu [0] = valuN AbstractDef valu [] = valuN ConcreteDef valu _ = malformed instance EmbPrj Delayed where icod_ Delayed = icodeN 0 Delayed icod_ NotDelayed = icodeN' NotDelayed value = vcase valu where valu [0] = valuN Delayed valu [] = valuN NotDelayed valu _ = malformed instance EmbPrj Impossible where icod_ (Impossible a b) = icodeN 0 Impossible a b icod_ (Unreachable a b) = icodeN 1 Unreachable a b value = vcase valu where valu [0, a, b] = valuN Impossible a b valu [1, a, b] = valuN Unreachable a b valu _ = malformed instance EmbPrj Empty where icod_ a = icod_ =<< lift (Empty.toImpossible a) value = fmap throwImpossible . value Agda-2.5.3/src/full/Agda/TypeChecking/Serialise/Instances/Internal.hs0000644000000000000000000003020113154613124023460 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.TypeChecking.Serialise.Instances.Internal where import Control.Applicative import Control.Monad.State.Strict import Agda.Syntax.Internal as I import Agda.Syntax.Position as P import Agda.TypeChecking.Serialise.Base import Agda.TypeChecking.Serialise.Instances.Common () import Agda.TypeChecking.Serialise.Instances.Compilers () import Agda.TypeChecking.Monad import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Positivity.Occurrence import Agda.Utils.Permutation #include "undefined.h" import Agda.Utils.Impossible instance EmbPrj Signature where icod_ (Sig a b c) = icodeN' Sig a b c value = valueN Sig instance EmbPrj Section where icod_ (Section a) = icodeN' Section a value = valueN Section instance EmbPrj a => EmbPrj (Tele a) where icod_ EmptyTel = icodeN' EmptyTel icod_ (ExtendTel a b) = icodeN' ExtendTel a b value = vcase valu where valu [] = valuN EmptyTel valu [a, b] = valuN ExtendTel a b valu _ = malformed instance EmbPrj Permutation where icod_ (Perm a b) = icodeN' Perm a b value = valueN Perm instance EmbPrj a => EmbPrj (Drop a) where icod_ (Drop a b) = icodeN' Drop a b value = valueN Drop instance EmbPrj a => EmbPrj (Elim' a) where icod_ (Apply a) = icodeN' Apply a icod_ (Proj a b) = icodeN 0 Proj a b value = vcase valu where valu [a] = valuN Apply a valu [0, a, b] = valuN Proj a b valu _ = malformed instance EmbPrj I.ConHead where icod_ (ConHead a b c) = icodeN' ConHead a b c value = valueN ConHead instance (EmbPrj a) => EmbPrj (I.Type' a) where icod_ (El a b) = icodeN' El a b value = valueN El instance EmbPrj a => EmbPrj (I.Abs a) where icod_ (NoAbs a b) = icodeN 0 NoAbs a b icod_ (Abs a b) = icodeN' Abs a b value = vcase valu where valu [a, b] = valuN Abs a b valu [0, a, b] = valuN NoAbs a b valu _ = malformed instance EmbPrj I.Term where icod_ (Var a []) = icodeN' (\ a -> Var a []) a icod_ (Var a b) = icodeN 0 Var a b icod_ (Lam a b) = icodeN 1 Lam a b icod_ (Lit a ) = icodeN 2 Lit a icod_ (Def a b) = icodeN 3 Def a b icod_ (Con a b c) = icodeN 4 Con a b c icod_ (Pi a b) = icodeN 5 Pi a b icod_ (Sort a ) = icodeN 7 Sort a icod_ (MetaV a b) = __IMPOSSIBLE__ icod_ (DontCare a ) = icodeN 8 DontCare a icod_ (Level a ) = icodeN 9 Level a icod_ (Shared p) = icodeMemo termD termC p $ icode (derefPtr p) value r = vcase valu' r where valu' xs = gets mkShared <*> valu xs valu [a] = valuN var a valu [0, a, b] = valuN Var a b valu [1, a, b] = valuN Lam a b valu [2, a] = valuN Lit a valu [3, a, b] = valuN Def a b valu [4, a, b, c] = valuN Con a b c valu [5, a, b] = valuN Pi a b valu [7, a] = valuN Sort a valu [8, a] = valuN DontCare a valu [9, a] = valuN Level a valu _ = malformed instance EmbPrj Level where icod_ (Max a) = icodeN' Max a value = valueN Max instance EmbPrj PlusLevel where icod_ (ClosedLevel a) = icodeN' ClosedLevel a icod_ (Plus a b) = icodeN' Plus a b value = vcase valu where valu [a] = valuN ClosedLevel a valu [a, b] = valuN Plus a b valu _ = malformed instance EmbPrj LevelAtom where icod_ (NeutralLevel r a) = icodeN' (NeutralLevel r) a icod_ (UnreducedLevel a) = icodeN 1 UnreducedLevel a icod_ (MetaLevel a b) = __IMPOSSIBLE__ icod_ BlockedLevel{} = __IMPOSSIBLE__ value = vcase valu where valu [a] = valuN UnreducedLevel a -- we forget that we are a NeutralLevel, -- since we do not want do (de)serialize -- the reason for neutrality valu [1, a] = valuN UnreducedLevel a valu _ = malformed instance EmbPrj I.Sort where icod_ (Type a ) = icodeN 0 Type a icod_ Prop = icodeN' Prop icod_ SizeUniv = icodeN 1 SizeUniv icod_ Inf = icodeN 2 Inf icod_ (DLub a b) = icodeN 3 DLub a b -- Andreas, 2017-01-18: not __IMPOSSIBLE__ see #2408 value = vcase valu where valu [] = valuN Prop valu [0, a] = valuN Type a valu [1] = valuN SizeUniv valu [2] = valuN Inf valu [3, a, b] = valuN DLub a b valu _ = malformed instance EmbPrj DisplayForm where icod_ (Display a b c) = icodeN' Display a b c value = valueN Display instance EmbPrj a => EmbPrj (Open a) where icod_ (OpenThing a b) = icodeN' OpenThing a b value = valueN OpenThing instance EmbPrj a => EmbPrj (Local a) where icod_ (Local a b) = icodeN' Local a b icod_ (Global a) = icodeN' Global a value = vcase valu where valu [a, b] = valuN Local a b valu [a] = valuN Global a valu _ = malformed instance EmbPrj CtxId where icod_ (CtxId a) = icode a value n = CtxId `fmap` value n instance EmbPrj DisplayTerm where icod_ (DTerm a ) = icodeN' DTerm a icod_ (DDot a ) = icodeN 1 DDot a icod_ (DCon a b c) = icodeN 2 DCon a b c icod_ (DDef a b) = icodeN 3 DDef a b icod_ (DWithApp a b c) = icodeN 4 DWithApp a b c value = vcase valu where valu [a] = valuN DTerm a valu [1, a] = valuN DDot a valu [2, a, b, c] = valuN DCon a b c valu [3, a, b] = valuN DDef a b valu [4, a, b, c] = valuN DWithApp a b c valu _ = malformed instance EmbPrj MutualId where icod_ (MutId a) = icode a value n = MutId `fmap` value n instance EmbPrj Definition where icod_ (Defn a b c d e f g h i j k l m) = icodeN' Defn a b (P.killRange c) d e f g h i j k l m value = valueN Defn instance EmbPrj NLPat where icod_ (PVar a b) = icodeN 0 PVar a b icod_ (PWild) = icodeN 1 PWild icod_ (PDef a b) = icodeN 2 PDef a b icod_ (PLam a b) = icodeN 3 PLam a b icod_ (PPi a b) = icodeN 4 PPi a b icod_ (PBoundVar a b) = icodeN 5 PBoundVar a b icod_ (PTerm a) = icodeN 6 PTerm a value = vcase valu where valu [0, a, b] = valuN PVar a b valu [1] = valuN PWild valu [2, a, b] = valuN PDef a b valu [3, a, b] = valuN PLam a b valu [4, a, b] = valuN PPi a b valu [5, a, b] = valuN PBoundVar a b valu [6, a] = valuN PTerm a valu _ = malformed instance EmbPrj NLPType where icod_ (NLPType a b) = icodeN' NLPType a b value = valueN NLPType instance EmbPrj RewriteRule where icod_ (RewriteRule a b c d e f) = icodeN' RewriteRule a b c d e f value = valueN RewriteRule instance EmbPrj Projection where icod_ (Projection a b c d e) = icodeN' Projection a b c d e value = valueN Projection instance EmbPrj ProjLams where icod_ (ProjLams a) = icodeN' ProjLams a value = valueN ProjLams instance EmbPrj ExtLamInfo where icod_ (ExtLamInfo a b) = icodeN' ExtLamInfo a b value = valueN ExtLamInfo instance EmbPrj Polarity where icod_ Covariant = return 0 icod_ Contravariant = return 1 icod_ Invariant = return 2 icod_ Nonvariant = return 3 value 0 = return Covariant value 1 = return Contravariant value 2 = return Invariant value 3 = return Nonvariant value _ = malformed instance EmbPrj Occurrence where icod_ StrictPos = return 0 icod_ Mixed = return 1 icod_ Unused = return 2 icod_ GuardPos = return 3 icod_ JustPos = return 4 icod_ JustNeg = return 5 value 0 = return StrictPos value 1 = return Mixed value 2 = return Unused value 3 = return GuardPos value 4 = return JustPos value 5 = return JustNeg value _ = malformed instance EmbPrj EtaEquality where icod_ (Specified a) = icodeN 0 Specified a icod_ (Inferred a) = icodeN 1 Inferred a value = vcase valu where valu [0,a] = valuN Specified a valu [1,a] = valuN Inferred a valu _ = malformed instance EmbPrj Defn where icod_ Axiom = icodeN 0 Axiom icod_ (Function a b t c d e f g h i j k m) = icodeN 1 (\ a b -> Function a b t) a b c d e f g h i j k m icod_ (Datatype a b c d e f g h i j) = icodeN 2 Datatype a b c d e f g h i j icod_ (Record a b c d e f g h i j) = icodeN 3 Record a b c d e f g h i j icod_ (Constructor a b c d e f g) = icodeN 4 Constructor a b c d e f g icod_ (Primitive a b c d) = icodeN 5 Primitive a b c d icod_ AbstractDefn{} = __IMPOSSIBLE__ value = vcase valu where valu [0] = valuN Axiom valu [1, a, b, c, d, e, f, g, h, i, j, k, m] = valuN (\ a b -> Function a b Nothing) a b c d e f g h i j k m valu [2, a, b, c, d, e, f, g, h, i, j] = valuN Datatype a b c d e f g h i j valu [3, a, b, c, d, e, f, g, h, i, j] = valuN Record a b c d e f g h i j valu [4, a, b, c, d, e, f, g] = valuN Constructor a b c d e f g valu [5, a, b, c, d] = valuN Primitive a b c d valu _ = malformed instance EmbPrj FunctionFlag where icod_ FunStatic = icodeN 0 FunStatic icod_ FunInline = icodeN 1 FunInline icod_ FunMacro = icodeN 2 FunMacro value = vcase valu where valu [0] = valuN FunStatic valu [1] = valuN FunInline valu [2] = valuN FunMacro valu _ = malformed instance EmbPrj a => EmbPrj (WithArity a) where icod_ (WithArity a b) = icodeN' WithArity a b value = valueN WithArity instance EmbPrj a => EmbPrj (Case a) where icod_ (Branches a b c d) = icodeN' Branches a b c d value = valueN Branches instance EmbPrj CompiledClauses where icod_ Fail = icodeN' Fail icod_ (Done a b) = icodeN' Done a (P.killRange b) icod_ (Case a b) = icodeN 2 Case a b value = vcase valu where valu [] = valuN Fail valu [a, b] = valuN Done a b valu [2, a, b] = valuN Case a b valu _ = malformed instance EmbPrj a => EmbPrj (FunctionInverse' a) where icod_ NotInjective = icodeN' NotInjective icod_ (Inverse a) = icodeN' Inverse a value = vcase valu where valu [] = valuN NotInjective valu [a] = valuN Inverse a valu _ = malformed instance EmbPrj TermHead where icod_ SortHead = icodeN' SortHead icod_ PiHead = icodeN 1 PiHead icod_ (ConsHead a) = icodeN 2 ConsHead a value = vcase valu where valu [] = valuN SortHead valu [1] = valuN PiHead valu [2, a] = valuN ConsHead a valu _ = malformed instance EmbPrj I.Clause where icod_ (Clause a b c d e f g h) = icodeN' Clause a b c d e f g h value = valueN Clause instance EmbPrj I.ConPatternInfo where icod_ (ConPatternInfo a b) = icodeN' ConPatternInfo a b value = valueN ConPatternInfo instance EmbPrj I.DBPatVar where icod_ (DBPatVar a b) = icodeN' DBPatVar a b value = valueN DBPatVar instance EmbPrj a => EmbPrj (I.Pattern' a) where icod_ (VarP a ) = icodeN' VarP a icod_ (ConP a b c) = icodeN 1 ConP a b c icod_ (LitP a ) = icodeN 2 LitP a icod_ (DotP a ) = icodeN 3 DotP a icod_ (ProjP a b ) = icodeN 4 ProjP a b icod_ (AbsurdP a ) = icodeN 5 AbsurdP a value = vcase valu where valu [a] = valuN VarP a valu [1, a, b, c] = valuN ConP a b c valu [2, a] = valuN LitP a valu [3, a] = valuN DotP a valu [4, a, b] = valuN ProjP a b valu [5, a] = valuN AbsurdP a valu _ = malformed instance EmbPrj a => EmbPrj (Builtin a) where icod_ (Prim a) = icodeN' Prim a icod_ (Builtin a) = icodeN 1 Builtin a value = vcase valu where valu [a] = valuN Prim a valu [1, a] = valuN Builtin a valu _ = malformed instance EmbPrj a => EmbPrj (Substitution' a) where icod_ IdS = icodeN' IdS icod_ (EmptyS a) = icodeN 1 EmptyS a icod_ (a :# b) = icodeN 2 (:#) a b icod_ (Strengthen a b) = icodeN 3 Strengthen a b icod_ (Wk a b) = icodeN 4 Wk a b icod_ (Lift a b) = icodeN 5 Lift a b value = vcase valu where valu [] = valuN IdS valu [1, a] = valuN EmptyS a valu [2, a, b] = valuN (:#) a b valu [3, a, b] = valuN Strengthen a b valu [4, a, b] = valuN Wk a b valu [5, a, b] = valuN Lift a b valu _ = malformed Agda-2.5.3/src/full/Agda/TypeChecking/Serialise/Instances/Compilers.hs0000644000000000000000000000120213154613124023640 0ustar0000000000000000{-# OPTIONS_GHC -fno-warn-orphans #-} {-# OPTIONS_GHC -fwarn-unused-imports #-} module Agda.TypeChecking.Serialise.Instances.Compilers where import Agda.TypeChecking.Serialise.Base import Agda.TypeChecking.Serialise.Instances.Common import Agda.TypeChecking.Monad instance EmbPrj CompilerPragma where icod_ (CompilerPragma a b) = icodeN' (CompilerPragma . underlyingRange) (SerialisedRange a) b value = valueN (CompilerPragma . underlyingRange) instance EmbPrj ForeignCode where icod_ (ForeignCode r a) = icodeN' (ForeignCode . underlyingRange) (SerialisedRange r) a value = valueN (ForeignCode . underlyingRange) Agda-2.5.3/src/full/Agda/TypeChecking/Serialise/Instances/Highlighting.hs0000644000000000000000000000676113154613124024327 0ustar0000000000000000{-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.TypeChecking.Serialise.Instances.Highlighting where import qualified Agda.Interaction.Highlighting.Range as HR import qualified Agda.Interaction.Highlighting.Precise as HP import Agda.TypeChecking.Serialise.Base import Agda.TypeChecking.Serialise.Instances.Common () instance EmbPrj HR.Range where icod_ (HR.Range a b) = icodeN' HR.Range a b value = valueN HR.Range instance EmbPrj HP.NameKind where icod_ HP.Bound = icodeN' HP.Bound icod_ (HP.Constructor a) = icodeN 1 HP.Constructor a icod_ HP.Datatype = icodeN 2 () icod_ HP.Field = icodeN 3 () icod_ HP.Function = icodeN 4 () icod_ HP.Module = icodeN 5 () icod_ HP.Postulate = icodeN 6 () icod_ HP.Primitive = icodeN 7 () icod_ HP.Record = icodeN 8 () icod_ HP.Argument = icodeN 9 () icod_ HP.Macro = icodeN 10 () value = vcase valu where valu [] = valuN HP.Bound valu [1 , a] = valuN HP.Constructor a valu [2] = valuN HP.Datatype valu [3] = valuN HP.Field valu [4] = valuN HP.Function valu [5] = valuN HP.Module valu [6] = valuN HP.Postulate valu [7] = valuN HP.Primitive valu [8] = valuN HP.Record valu [9] = valuN HP.Argument valu [10] = valuN HP.Macro valu _ = malformed instance EmbPrj HP.Aspect where icod_ HP.Comment = icodeN 0 () icod_ HP.Option = icodeN 1 () icod_ HP.Keyword = icodeN 2 () icod_ HP.String = icodeN 3 () icod_ HP.Number = icodeN 4 () icod_ HP.Symbol = icodeN' HP.Symbol icod_ HP.PrimitiveType = icodeN 6 () icod_ (HP.Name mk b) = icodeN 7 HP.Name mk b value = vcase valu where valu [0] = valuN HP.Comment valu [1] = valuN HP.Option valu [2] = valuN HP.Keyword valu [3] = valuN HP.String valu [4] = valuN HP.Number valu [] = valuN HP.Symbol valu [6] = valuN HP.PrimitiveType valu [7, mk, b] = valuN HP.Name mk b valu _ = malformed instance EmbPrj HP.OtherAspect where icod_ HP.Error = icodeN 0 () icod_ HP.DottedPattern = icodeN' HP.DottedPattern icod_ HP.UnsolvedMeta = icodeN 2 () icod_ HP.TerminationProblem = icodeN 3 () icod_ HP.IncompletePattern = icodeN 4 () icod_ HP.TypeChecks = icodeN 5 () icod_ HP.UnsolvedConstraint = icodeN 6 () icod_ HP.PositivityProblem = icodeN 7 () icod_ HP.ReachabilityProblem = icodeN 8 () icod_ HP.CoverageProblem = icodeN 9 () icod_ HP.CatchallClause = icodeN 10 () value = vcase valu where valu [0] = valuN HP.Error valu [] = valuN HP.DottedPattern valu [2] = valuN HP.UnsolvedMeta valu [3] = valuN HP.TerminationProblem valu [4] = valuN HP.IncompletePattern valu [5] = valuN HP.TypeChecks valu [6] = valuN HP.UnsolvedConstraint valu [7] = valuN HP.PositivityProblem valu [8] = valuN HP.ReachabilityProblem valu [9] = valuN HP.CoverageProblem valu [10] = valuN HP.CatchallClause valu _ = malformed instance EmbPrj HP.Aspects where icod_ (HP.Aspects a b c d) = icodeN' HP.Aspects a b c d value = valueN HP.Aspects instance EmbPrj HP.DefinitionSite where icod_ (HP.DefinitionSite a b c d) = icodeN' HP.DefinitionSite a b c d value = valueN HP.DefinitionSite instance EmbPrj HP.CompressedFile where icod_ (HP.CompressedFile f) = icodeN' HP.CompressedFile f value = valueN HP.CompressedFile Agda-2.5.3/src/full/Agda/TypeChecking/Serialise/Instances/Abstract.hs0000644000000000000000000001260413154613124023456 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.TypeChecking.Serialise.Instances.Abstract where import Control.Applicative import qualified Data.Map as Map import qualified Data.Set as Set import Agda.Syntax.Common import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Info import Agda.Syntax.Scope.Base import Agda.Syntax.Position as P import Agda.Syntax.Fixity import Agda.TypeChecking.Serialise.Base import Agda.TypeChecking.Serialise.Instances.Common () import Agda.TypeChecking.Monad import Agda.Utils.Except #include "undefined.h" import Agda.Utils.Impossible instance EmbPrj Scope where icod_ (Scope a b c d e) = icodeN' Scope a b c d e value = valueN Scope instance EmbPrj NameSpaceId where icod_ PublicNS = icodeN' PublicNS icod_ PrivateNS = icodeN 1 PrivateNS icod_ ImportedNS = icodeN 2 ImportedNS icod_ OnlyQualifiedNS = icodeN 3 OnlyQualifiedNS value = vcase valu where valu [] = valuN PublicNS valu [1] = valuN PrivateNS valu [2] = valuN ImportedNS valu [3] = valuN OnlyQualifiedNS valu _ = malformed instance EmbPrj Access where icod_ (PrivateAccess UserWritten) = icodeN 0 () icod_ PrivateAccess{} = icodeN 1 () icod_ PublicAccess = icodeN' PublicAccess icod_ OnlyQualified = icodeN 2 () value = vcase valu where valu [0] = valuN $ PrivateAccess UserWritten valu [1] = valuN $ PrivateAccess Inserted valu [] = valuN PublicAccess valu [2] = valuN OnlyQualified valu _ = malformed instance EmbPrj NameSpace where icod_ (NameSpace a b c) = icodeN' NameSpace a b c value = valueN NameSpace instance EmbPrj WhyInScope where icod_ Defined = icodeN' Defined icod_ (Opened a b) = icodeN 0 Opened a b icod_ (Applied a b) = icodeN 1 Applied a b value = vcase valu where valu [] = valuN Defined valu [0, a, b] = valuN Opened a b valu [1, a, b] = valuN Applied a b valu _ = malformed instance EmbPrj AbstractName where icod_ (AbsName a b c) = icodeN' AbsName a b c value = valueN AbsName instance EmbPrj AbstractModule where icod_ (AbsModule a b) = icodeN' AbsModule a b value = valueN AbsModule instance EmbPrj KindOfName where icod_ DefName = icodeN' DefName icod_ ConName = icodeN 1 ConName icod_ FldName = icodeN 2 FldName icod_ PatternSynName = icodeN 3 PatternSynName icod_ QuotableName = icodeN 4 QuotableName icod_ MacroName = icodeN 5 MacroName value = vcase valu where valu [] = valuN DefName valu [1] = valuN ConName valu [2] = valuN FldName valu [3] = valuN PatternSynName valu [4] = valuN QuotableName valu [5] = valuN MacroName valu _ = malformed instance EmbPrj LocalVar where icod_ (LocalVar a b c) = icodeN' LocalVar a b c value = valueN LocalVar instance EmbPrj ConPatInfo where icod_ (ConPatInfo a _) = icod_ a value a = flip ConPatInfo patNoRange <$> value a -- Only for pattern synonyms (where a is Void) instance EmbPrj a => EmbPrj (A.Pattern' a) where icod_ (A.VarP a) = icodeN 0 A.VarP a icod_ (A.ConP a b c) = icodeN 1 A.ConP a b c icod_ (A.DefP p a b) = icodeN 2 (A.DefP p) a b icod_ t@(A.WildP p) = icodeN 3 t icod_ (A.AsP p a b) = icodeN 4 (A.AsP p) a b icod_ (A.DotP p a b) = icodeN 5 (A.DotP p) a b icod_ t@(A.AbsurdP _) = icodeN 6 t icod_ (A.LitP a) = icodeN 7 A.LitP a icod_ (A.ProjP p a b) = icodeN 8 (A.ProjP p) a b icod_ (A.PatternSynP p a b) = icodeN 9 (A.PatternSynP p) a b icod_ (A.RecP p a) = icodeN 10 (A.RecP p) a value = vcase valu where valu [0, a] = valuN A.VarP a valu [1, a, b, c] = valuN A.ConP a b c valu [2, a, b] = valuN (A.DefP i) a b valu [3] = valuN (A.WildP i) valu [4, a, b] = valuN (A.AsP i) a b valu [5, a, b] = valuN (A.DotP i) a b valu [6] = valuN (A.AbsurdP i) valu [7, a] = valuN (A.LitP) a valu [8, a, b] = valuN (A.ProjP i) a b valu [9, a, b] = valuN (A.PatternSynP i) a b valu [10, a] = valuN (A.RecP i) a valu _ = malformed i = patNoRange instance EmbPrj Precedence where icod_ TopCtx = icodeN' TopCtx icod_ FunctionSpaceDomainCtx = icodeN 1 FunctionSpaceDomainCtx icod_ (LeftOperandCtx a) = icodeN 2 LeftOperandCtx a icod_ (RightOperandCtx a) = icodeN 3 RightOperandCtx a icod_ FunctionCtx = icodeN 4 FunctionCtx icod_ ArgumentCtx = icodeN 5 ArgumentCtx icod_ InsideOperandCtx = icodeN 6 InsideOperandCtx icod_ WithFunCtx = icodeN 7 WithFunCtx icod_ WithArgCtx = icodeN 8 WithArgCtx icod_ DotPatternCtx = icodeN 9 DotPatternCtx value = vcase valu where valu [] = valuN TopCtx valu [1] = valuN FunctionSpaceDomainCtx valu [2, a] = valuN LeftOperandCtx a valu [3, a] = valuN RightOperandCtx a valu [4] = valuN FunctionCtx valu [5] = valuN ArgumentCtx valu [6] = valuN InsideOperandCtx valu [7] = valuN WithFunCtx valu [8] = valuN WithArgCtx valu [9] = valuN DotPatternCtx valu _ = malformed instance EmbPrj ScopeInfo where icod_ (ScopeInfo a b c d e f g) = icodeN' (\ a b c d -> ScopeInfo a b c d e f g) a b c d value = valueN (\ a b c d -> ScopeInfo a b c d Map.empty Map.empty Set.empty) Agda-2.5.3/src/full/Agda/TypeChecking/Serialise/Instances/Errors.hs0000644000000000000000000000766513154613124023202 0ustar0000000000000000{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE CPP #-} {-# OPTIONS_GHC -fno-warn-orphans #-} #if __GLASGOW_HASKELL__ <= 708 {-# OPTIONS_GHC -fcontext-stack=30 #-} #endif module Agda.TypeChecking.Serialise.Instances.Errors where #if __GLASGOW_HASKELL__ <= 708 import Agda.TypeChecking.Pretty () #endif import Agda.TypeChecking.Serialise.Base import Agda.TypeChecking.Serialise.Instances.Common import Agda.TypeChecking.Serialise.Instances.Internal () import Agda.TypeChecking.Serialise.Instances.Abstract () import Agda.Syntax.Common import Agda.Syntax.Concrete.Definitions (DeclarationWarning(..)) import Agda.Syntax.Abstract.Name (ModuleName) import Agda.TypeChecking.Monad.Base import Agda.Interaction.Options import Agda.Termination.CutOff import Agda.TypeChecking.Positivity.Occurrence () import Agda.Syntax.Parser.Monad (ParseWarning( OverlappingTokensWarning )) import Agda.Utils.Pretty import Agda.Utils.FileName () import Agda.Utils.Lens #include "undefined.h" import Agda.Utils.Impossible instance EmbPrj TCWarning where icod_ (TCWarning a b c) = icodeN' TCWarning a b c value = valueN TCWarning -- We don't need to serialise warnings that turn into errors instance EmbPrj Warning where icod_ (TerminationIssue a) = __IMPOSSIBLE__ icod_ (UnreachableClauses a b) = icodeN 0 UnreachableClauses a b icod_ (CoverageIssue a b) = __IMPOSSIBLE__ icod_ (CoverageNoExactSplit a b) = __IMPOSSIBLE__ icod_ (NotStrictlyPositive a b) = __IMPOSSIBLE__ icod_ (UnsolvedMetaVariables a) = __IMPOSSIBLE__ icod_ (UnsolvedInteractionMetas a) = __IMPOSSIBLE__ icod_ (UnsolvedConstraints a) = __IMPOSSIBLE__ icod_ (OldBuiltin a b) = icodeN 1 OldBuiltin a b icod_ EmptyRewritePragma = icodeN 2 EmptyRewritePragma icod_ UselessPublic = icodeN 3 UselessPublic icod_ (UselessInline a) = icodeN 4 UselessInline a icod_ (GenericWarning a) = icodeN 5 GenericWarning a icod_ (GenericNonFatalError a) = __IMPOSSIBLE__ icod_ (SafeFlagPostulate a) = __IMPOSSIBLE__ icod_ (SafeFlagPragma a) = __IMPOSSIBLE__ icod_ SafeFlagNonTerminating = __IMPOSSIBLE__ icod_ SafeFlagTerminating = __IMPOSSIBLE__ icod_ SafeFlagPrimTrustMe = __IMPOSSIBLE__ icod_ SafeFlagNoPositivityCheck = __IMPOSSIBLE__ icod_ SafeFlagPolarity = __IMPOSSIBLE__ icod_ (ParseWarning a) = __IMPOSSIBLE__ icod_ (DeprecationWarning a b c) = icodeN 6 DeprecationWarning a b c icod_ (NicifierIssue a) = icodeN 7 NicifierIssue a value = vcase valu where valu [0, a, b] = valuN UnreachableClauses a b valu [1, a, b] = valuN OldBuiltin a b valu [2] = valuN EmptyRewritePragma valu [3] = valuN UselessPublic valu [4, a] = valuN UselessInline a valu [5, a] = valuN GenericWarning a valu [6, a, b, c] = valuN DeprecationWarning a b c valu [7, a] = valuN NicifierIssue a valu _ = malformed instance EmbPrj DeclarationWarning where icod_ = \case UnknownNamesInFixityDecl a -> icodeN 0 UnknownNamesInFixityDecl a UnknownNamesInPolarityPragmas a -> icodeN 1 UnknownNamesInPolarityPragmas a PolarityPragmasButNotPostulates a -> icodeN 2 PolarityPragmasButNotPostulates a UselessPrivate a -> icodeN 3 UselessPrivate a UselessAbstract a -> icodeN 4 UselessAbstract a UselessInstance a -> icodeN 5 UselessInstance a value = vcase $ \case [0, a] -> valueN UnknownNamesInFixityDecl a [1, a] -> valueN UnknownNamesInPolarityPragmas a [2, a] -> valueN PolarityPragmasButNotPostulates a [3, a] -> valueN UselessPrivate a [4, a] -> valueN UselessAbstract a [5, a] -> valueN UselessInstance a _ -> malformed instance EmbPrj Doc where icod_ d = icodeN' (undefined :: String -> Doc) (render d) value = valueN text Agda-2.5.3/src/full/Agda/TypeChecking/Monad/0000755000000000000000000000000013154613124016543 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Options.hs-boot0000644000000000000000000000063013154613124021472 0ustar0000000000000000module Agda.TypeChecking.Monad.Options where import Control.Applicative import Control.Monad.Trans import Agda.Interaction.Options import Agda.TypeChecking.Monad.Base import Agda.Utils.FileName import Agda.Utils.Pretty getIncludeDirs :: TCM [AbsolutePath] type VerboseKey = String hasVerbosity :: HasOptions m => VerboseKey -> Int -> m Bool verboseS :: HasOptions m => VerboseKey -> Int -> m () -> m () Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Trace.hs0000644000000000000000000001200313154613124020131 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Monad.Trace where import Prelude hiding (null) import Control.Monad.Reader import {-# SOURCE #-} Agda.Interaction.Highlighting.Generate (highlightAsTypeChecked) import Agda.Syntax.Position import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Options import Agda.Utils.Function import Agda.Utils.Maybe import qualified Agda.Utils.Maybe.Strict as Strict import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Pretty (prettyShow) #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Trace --------------------------------------------------------------------------- interestingCall :: Closure Call -> Bool interestingCall cl = case clValue cl of InferVar{} -> False InferDef{} -> False CheckArguments _ [] _ _ -> False SetRange{} -> False NoHighlighting{} -> False _ -> True traceCallM :: (MonadTCM tcm, MonadDebug tcm) => tcm Call -> tcm a -> tcm a traceCallM mkCall m = flip traceCall m =<< mkCall -- | Record a function call in the trace. -- RULE left-hand side too complicated to desugar -- {-# SPECIALIZE traceCall :: Call -> TCM a -> TCM a #-} traceCall :: (MonadTCM tcm, MonadDebug tcm) => Call -> tcm a -> tcm a traceCall mkCall m = do let call = mkCall callRange = getRange call -- Andreas, 2016-09-13 issue #2177 -- Since the fix of #2092 we may report an error outside the current file. -- (For instance, if we import a module which then happens to have the -- wrong name.) -- Thus, we no longer crash, but just report the alien range. -- -- Andreas, 2015-02-09 Make sure we do not set a range -- -- outside the current file verboseS "check.ranges" 90 $ Strict.whenJust (rangeFile callRange) $ \f -> do currentFile <- asks envCurrentPath when (currentFile /= Just f) $ do reportSLn "check.ranges" 90 $ prettyShow call ++ " is setting the current range to " ++ show callRange ++ " which is outside of the current file " ++ show currentFile cl <- liftTCM $ buildClosure call let trace = local $ foldr (.) id $ [ \e -> e { envCall = Just cl } | interestingCall cl ] ++ [ \e -> e { envHighlightingRange = callRange } | callRange /= noRange && highlightCall call || isNoHighlighting call ] ++ [ \e -> e { envRange = callRange } | callRange /= noRange ] wrap <- ifM (do l <- envHighlightingLevel <$> ask return (l == Interactive && highlightCall call)) (do oldRange <- envHighlightingRange <$> ask return $ highlightAsTypeChecked oldRange callRange) (return id) wrap $ trace m where -- | Should the given call trigger interactive highlighting? highlightCall call = case call of CheckClause{} -> True CheckPattern{} -> True CheckLetBinding{} -> True InferExpr{} -> True CheckExprCall{} -> True CheckDotPattern{} -> True CheckPatternShadowing{} -> True IsTypeCall{} -> True IsType_{} -> True InferVar{} -> True InferDef{} -> True CheckArguments{} -> True CheckDataDef{} -> True CheckRecDef{} -> True CheckConstructor{} -> True CheckFunDef{} -> True CheckPragma{} -> True CheckPrimitive{} -> True CheckIsEmpty{} -> True CheckWithFunctionType{} -> True CheckSectionApplication{} -> True ScopeCheckExpr{} -> False ScopeCheckDeclaration{} -> False ScopeCheckLHS{} -> False NoHighlighting{} -> True CheckProjection{} -> False SetRange{} -> False ModuleContents{} -> False isNoHighlighting NoHighlighting{} = True isNoHighlighting _ = False -- RULE left-hand side too complicated to desugar -- {-# SPECIALIZE traceCallCPS :: Call -> (r -> TCM a) -> ((r -> TCM a) -> TCM b) -> TCM b #-} traceCallCPS :: (MonadTCM tcm, MonadDebug tcm) => Call -> (r -> tcm a) -> ((r -> tcm a) -> tcm b) -> tcm b traceCallCPS mkCall ret cc = traceCall mkCall (cc ret) -- RULE left-hand side too complicated to desugar -- {-# SPECIALIZE traceCallCPS_ :: Call -> TCM a -> (TCM a -> TCM b) -> TCM b #-} traceCallCPS_ :: (MonadTCM tcm, MonadDebug tcm) => Call -> tcm a -> (tcm a -> tcm b) -> tcm b traceCallCPS_ mkCall ret cc = traceCallCPS mkCall (const ret) (\k -> cc $ k ()) getCurrentRange :: TCM Range getCurrentRange = asks envRange -- | Sets the current range (for error messages etc.) to the range -- of the given object, if it has a range (i.e., its range is not 'noRange'). setCurrentRange :: HasRange x => x -> TCM a -> TCM a setCurrentRange x = applyUnless (null r) $ traceCall $ SetRange r where r = getRange x Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Debug.hs0000644000000000000000000001055713154613124020135 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Monad.Debug where import Control.Applicative import Control.Monad.IO.Class (MonadIO, liftIO) import Control.Monad.Reader import Control.Monad.State import Control.Monad.Trans.Maybe import Control.Monad.Writer import Data.Maybe import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend, Any(..)) import Data.Traversable import {-# SOURCE #-} Agda.TypeChecking.Errors import Agda.TypeChecking.Monad.Base import {-# SOURCE #-} Agda.TypeChecking.Monad.Options import Agda.Interaction.Response import Agda.Utils.Except import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.ListT import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible class (Functor m, Applicative m, Monad m) => MonadDebug m where displayDebugMessage :: Int -> String -> m () displayDebugMessage n s = traceDebugMessage n s $ return () traceDebugMessage :: Int -> String -> m a -> m a traceDebugMessage n s cont = displayDebugMessage n s >> cont formatDebugMessage :: VerboseKey -> Int -> TCM Doc -> m String instance (MonadIO m) => MonadDebug (TCMT m) where displayDebugMessage n s = liftTCM $ do cb <- gets $ stInteractionOutputCallback . stPersistentState liftIO $ cb (Resp_RunningInfo n s) formatDebugMessage k n d = liftTCM $ show <$> d `catchError` \ err -> (\ s -> (sep $ map text [ "Printing debug message" , k ++ ":" ++show n , "failed due to error:" ]) $$ (nest 2 $ text s)) <$> prettyError err instance MonadDebug m => MonadDebug (ExceptT e m) where displayDebugMessage n s = lift $ displayDebugMessage n s formatDebugMessage k n d = lift $ formatDebugMessage k n d instance MonadDebug m => MonadDebug (ListT m) where displayDebugMessage n s = lift $ displayDebugMessage n s formatDebugMessage k n d = lift $ formatDebugMessage k n d instance MonadDebug m => MonadDebug (MaybeT m) where displayDebugMessage n s = lift $ displayDebugMessage n s formatDebugMessage k n d = lift $ formatDebugMessage k n d instance MonadDebug m => MonadDebug (ReaderT r m) where displayDebugMessage n s = lift $ displayDebugMessage n s formatDebugMessage k n d = lift $ formatDebugMessage k n d instance MonadDebug m => MonadDebug (StateT s m) where displayDebugMessage n s = lift $ displayDebugMessage n s formatDebugMessage k n d = lift $ formatDebugMessage k n d instance (MonadDebug m, Monoid w) => MonadDebug (WriterT w m) where displayDebugMessage n s = lift $ displayDebugMessage n s formatDebugMessage k n d = lift $ formatDebugMessage k n d -- | Conditionally print debug string. {-# SPECIALIZE reportS :: VerboseKey -> Int -> String -> TCM () #-} reportS :: (HasOptions m, MonadDebug m) => VerboseKey -> Int -> String -> m () reportS k n s = verboseS k n $ displayDebugMessage n s -- | Conditionally println debug string. {-# SPECIALIZE reportSLn :: VerboseKey -> Int -> String -> TCM () #-} reportSLn :: (HasOptions m, MonadDebug m) => VerboseKey -> Int -> String -> m () reportSLn k n s = verboseS k n $ displayDebugMessage n (s ++ "\n") -- | Conditionally render debug 'Doc' and print it. {-# SPECIALIZE reportSDoc :: VerboseKey -> Int -> TCM Doc -> TCM () #-} reportSDoc :: (HasOptions m, MonadDebug m) => VerboseKey -> Int -> TCM Doc -> m () reportSDoc k n d = verboseS k n $ do displayDebugMessage n . (++ "\n") =<< formatDebugMessage k n d traceSLn :: (HasOptions m, MonadDebug m) => VerboseKey -> Int -> String -> m a -> m a traceSLn k n s cont = ifNotM (hasVerbosity k n) cont $ {- else -} do traceDebugMessage n (s ++ "\n") cont -- | Conditionally render debug 'Doc', print it, and then continue. traceSDoc :: (HasOptions m, MonadDebug m) => VerboseKey -> Int -> TCM Doc -> m a -> m a traceSDoc k n d cont = ifNotM (hasVerbosity k n) cont $ {- else -} do s <- formatDebugMessage k n d traceDebugMessage n (s ++ "\n") cont -- | Print brackets around debug messages issued by a computation. {-# SPECIALIZE verboseBracket :: VerboseKey -> Int -> String -> TCM a -> TCM a #-} verboseBracket :: (HasOptions m, MonadDebug m, MonadError err m) => VerboseKey -> Int -> String -> m a -> m a verboseBracket k n s m = ifNotM (hasVerbosity k n) m $ {- else -} do displayDebugMessage n $ "{ " ++ s ++ "\n" m `finally` displayDebugMessage n "}\n" Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Signature.hs-boot0000644000000000000000000000042313154613124022000 0ustar0000000000000000 module Agda.TypeChecking.Monad.Signature where import Agda.Syntax.Internal (ModuleName, Telescope) import Agda.TypeChecking.Monad.Base (TCM, ReadTCState) inFreshModuleIfFreeParams :: TCM a -> TCM a lookupSection :: (Functor m, ReadTCState m) => ModuleName -> m Telescope Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Context.hs0000644000000000000000000003165313154613124020533 0ustar0000000000000000{-# LANGUAGE CPP #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE OverlappingInstances #-} #endif module Agda.TypeChecking.Monad.Context where import Control.Applicative import Control.Monad.Reader import Control.Monad.State import qualified Data.List as List import Data.Map (Map) import qualified Data.Map as Map import Data.Monoid import Agda.Syntax.Abstract.Name import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Scope.Monad (getLocalVars, setLocalVars) import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Substitute import Agda.TypeChecking.Monad.Open import Agda.TypeChecking.Monad.Options import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List ((!!!), downFrom) import Agda.Utils.Pretty import Agda.Utils.Size -- * Modifying the context -- | Modify the 'ctxEntry' field of a 'ContextEntry'. modifyContextEntry :: (Dom (Name, Type) -> Dom (Name, Type)) -> ContextEntry -> ContextEntry modifyContextEntry f ce = ce { ctxEntry = f (ctxEntry ce) } -- | Modify all 'ContextEntry's. modifyContextEntries :: (Dom (Name, Type) -> Dom (Name, Type)) -> Context -> Context modifyContextEntries f = map (modifyContextEntry f) -- | Modify a 'Context' in a computation. {-# SPECIALIZE modifyContext :: (Context -> Context) -> TCM a -> TCM a #-} modifyContext :: MonadTCM tcm => (Context -> Context) -> tcm a -> tcm a modifyContext f = local $ \e -> e { envContext = f $ envContext e } {-# SPECIALIZE mkContextEntry :: Dom (Name, Type) -> TCM ContextEntry #-} mkContextEntry :: MonadTCM tcm => Dom (Name, Type) -> tcm ContextEntry mkContextEntry x = do i <- fresh return $ Ctx i x -- | Change to top (=empty) context. -- -- TODO: currently, this makes the @ModuleParamDict@ ill-formed! {-# SPECIALIZE inTopContext :: TCM a -> TCM a #-} inTopContext :: MonadTCM tcm => tcm a -> tcm a inTopContext cont = do locals <- liftTCM $ getLocalVars liftTCM $ setLocalVars [] a <- modifyContext (const []) cont liftTCM $ setLocalVars locals return a -- | Delete the last @n@ bindings from the context. -- -- TODO: currently, this makes the @ModuleParamDict@ ill-formed! {-# SPECIALIZE escapeContext :: Int -> TCM a -> TCM a #-} escapeContext :: MonadTCM tcm => Int -> tcm a -> tcm a escapeContext n = modifyContext $ drop n -- * Manipulating module parameters -- -- | Locally set module parameters for a computation. withModuleParameters :: ModuleParamDict -> TCM a -> TCM a withModuleParameters mp ret = do old <- use stModuleParameters stModuleParameters .= mp x <- ret stModuleParameters .= old return x -- | Apply a substitution to all module parameters. updateModuleParameters :: (MonadTCM tcm, MonadDebug tcm) => Substitution -> tcm a -> tcm a updateModuleParameters sub ret = do pm <- use stModuleParameters let showMP pref mps = List.intercalate "\n" $ [ p ++ show m ++ " : " ++ show (mpSubstitution mp) | (p, (m, mp)) <- zip (pref : repeat (map (const ' ') pref)) (Map.toList mps) ] verboseS "tc.cxt.param" 105 $ do cxt <- reverse <$> getContext reportSLn "tc.cxt.param" 105 $ unlines $ [ "updatingModuleParameters" , " sub = " ++ show sub , " cxt (last added last in list) = " ++ unwords (map (show . fst . unDom) cxt) , showMP " old = " pm ] let pm' = applySubst sub pm reportSLn "tc.cxt.param" 105 $ showMP " new = " pm' stModuleParameters .= pm' x <- ret -- We need to keep introduced modules around pm1 <- use stModuleParameters let pm'' = Map.union pm (defaultModuleParameters <$ Map.difference pm1 pm) stModuleParameters .= pm'' reportSLn "tc.cxt.param" 105 $ showMP " restored = " pm'' return x -- | Since the @ModuleParamDict@ is relative to the current context, -- this function should be called everytime the context is extended. -- weakenModuleParameters :: (MonadTCM tcm, MonadDebug tcm) => Nat -> tcm a -> tcm a weakenModuleParameters n = updateModuleParameters (raiseS n) -- | Get substitution @Γ ⊢ ρ : Γm@ where @Γ@ is the current context -- and @Γm@ is the module parameter telescope of module @m@. -- -- In case the current 'ModuleParamDict' does not know @m@, -- we return the identity substitution. -- This is ok for instance if we are outside module @m@ -- (in which case we have to supply all module parameters to any -- symbol defined within @m@ we want to refer). getModuleParameterSub :: (Functor m, ReadTCState m) => ModuleName -> m Substitution getModuleParameterSub m = do r <- (^. stModuleParameters) <$> getTCState case Map.lookup m r of Nothing -> return IdS Just mp -> return $ mpSubstitution mp -- * Adding to the context -- | @addCtx x arg cont@ add a variable to the context. -- -- Chooses an unused 'Name'. -- -- Warning: Does not update module parameter substitution! {-# SPECIALIZE addCtx :: Name -> Dom Type -> TCM a -> TCM a #-} addCtx :: MonadTCM tcm => Name -> Dom Type -> tcm a -> tcm a addCtx x a ret = do ce <- mkContextEntry $ (x,) <$> a modifyContext (ce :) ret -- let-bindings keep track of own their context -- | Pick a concrete name that doesn't shadow anything in the context. unshadowName :: MonadTCM tcm => Name -> tcm Name unshadowName x = do ctx <- map (nameConcrete . fst . unDom) <$> getContext return $ head $ filter (notTaken ctx) $ iterate nextName x where notTaken xs x = isNoName x || nameConcrete x `notElem` xs -- | Various specializations of @addCtx@. {-# SPECIALIZE addContext :: b -> TCM a -> TCM a #-} class AddContext b where addContext :: MonadTCM tcm => b -> tcm a -> tcm a contextSize :: b -> Nat -- | Since the module parameter substitution is relative to -- the current context, we need to weaken it when we -- extend the context. This function takes care of that. -- addContext' :: (MonadTCM tcm, MonadDebug tcm, AddContext b) => b -> tcm a -> tcm a addContext' cxt = addContext cxt . weakenModuleParameters (contextSize cxt) -- | Wrapper to tell 'addContext' not to 'unshadowName's. Used when adding a -- user-provided, but already type checked, telescope to the context. newtype KeepNames a = KeepNames a #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPABLE #-} AddContext a => AddContext [a] where #else instance AddContext a => AddContext [a] where #endif addContext = flip (foldr addContext) contextSize = sum . map contextSize instance AddContext (Name, Dom Type) where addContext = uncurry addCtx contextSize _ = 1 instance AddContext (Dom (Name, Type)) where addContext = addContext . distributeF contextSize _ = 1 instance AddContext (Dom (String, Type)) where addContext = addContext . distributeF contextSize _ = 1 instance AddContext ([Name], Dom Type) where addContext (xs, dom) = addContext (bindsToTel' id xs dom) contextSize (xs, _) = length xs instance AddContext ([WithHiding Name], Dom Type) where addContext ([] , dom) = id addContext (WithHiding h x : xs, dom) = addContext (x , mapHiding (mappend h) dom) . addContext (xs, raise 1 dom) contextSize (xs, _) = length xs instance AddContext (String, Dom Type) where addContext (s, dom) ret = do x <- unshadowName =<< freshName_ s addCtx x dom ret contextSize _ = 1 instance AddContext (KeepNames String, Dom Type) where addContext (KeepNames s, dom) ret = do x <- freshName_ s addCtx x dom ret contextSize _ = 1 instance AddContext (Dom Type) where addContext dom = addContext ("_", dom) contextSize _ = 1 instance AddContext Name where addContext x = addContext (x, dummyDom) contextSize _ = 1 #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} AddContext String where #else instance AddContext String where #endif addContext s = addContext (s, dummyDom) contextSize _ = 1 instance AddContext (KeepNames Telescope) where addContext (KeepNames tel) ret = loop tel where loop EmptyTel = ret loop (ExtendTel t tel) = underAbstraction' KeepNames t tel loop contextSize (KeepNames tel) = size tel instance AddContext Telescope where addContext tel ret = loop tel where loop EmptyTel = ret loop (ExtendTel t tel) = underAbstraction t tel loop contextSize = size -- | Context entries without a type have this dummy type. dummyDom :: Dom Type dummyDom = defaultDom typeDontCare -- | Go under an abstraction. {-# SPECIALIZE underAbstraction :: Subst t a => Dom Type -> Abs a -> (a -> TCM b) -> TCM b #-} underAbstraction :: (Subst t a, MonadTCM tcm) => Dom Type -> Abs a -> (a -> tcm b) -> tcm b underAbstraction = underAbstraction' id underAbstraction' :: (Subst t a, MonadTCM tcm, AddContext (name, Dom Type)) => (String -> name) -> Dom Type -> Abs a -> (a -> tcm b) -> tcm b underAbstraction' _ _ (NoAbs _ v) k = k v underAbstraction' wrap t a k = addContext (wrap $ realName $ absName a, t) $ k $ absBody a where realName s = if isNoName s then "x" else argNameToString s -- | Go under an abstract without worrying about the type to add to the context. {-# SPECIALIZE underAbstraction_ :: Subst t a => Abs a -> (a -> TCM b) -> TCM b #-} underAbstraction_ :: (Subst t a, MonadTCM tcm) => Abs a -> (a -> tcm b) -> tcm b underAbstraction_ = underAbstraction dummyDom -- | Add a let bound variable. {-# SPECIALIZE addLetBinding :: ArgInfo -> Name -> Term -> Type -> TCM a -> TCM a #-} addLetBinding :: MonadTCM tcm => ArgInfo -> Name -> Term -> Type -> tcm a -> tcm a addLetBinding info x v t0 ret = do let t = Dom info t0 vt <- liftTCM $ makeOpen (v, t) flip local ret $ \e -> e { envLetBindings = Map.insert x vt $ envLetBindings e } -- * Querying the context -- | Get the current context. {-# SPECIALIZE getContext :: TCM [Dom (Name, Type)] #-} getContext :: MonadReader TCEnv m => m [Dom (Name, Type)] getContext = asks $ map ctxEntry . envContext -- | Get the size of the current context. {-# SPECIALIZE getContextSize :: TCM Nat #-} getContextSize :: (Applicative m, MonadReader TCEnv m) => m Nat getContextSize = length <$> asks envContext -- | Generate @[var (n - 1), ..., var 0]@ for all declarations in the context. {-# SPECIALIZE getContextArgs :: TCM Args #-} getContextArgs :: (Applicative m, MonadReader TCEnv m) => m Args getContextArgs = reverse . zipWith mkArg [0..] <$> getContext where mkArg i (Dom info _) = Arg info $ var i -- | Generate @[var (n - 1), ..., var 0]@ for all declarations in the context. {-# SPECIALIZE getContextTerms :: TCM [Term] #-} getContextTerms :: (Applicative m, MonadReader TCEnv m) => m [Term] getContextTerms = map var . downFrom <$> getContextSize -- | Get the current context as a 'Telescope'. {-# SPECIALIZE getContextTelescope :: TCM Telescope #-} getContextTelescope :: (Applicative m, MonadReader TCEnv m) => m Telescope getContextTelescope = telFromList' nameToArgName . reverse <$> getContext -- | Check if we are in a compatible context, i.e. an extension of the given context. {-# SPECIALIZE getContextId :: TCM [CtxId] #-} getContextId :: MonadReader TCEnv m => m [CtxId] getContextId = asks $ map ctxId . envContext -- | Get the names of all declarations in the context. {-# SPECIALIZE getContextNames :: TCM [Name] #-} getContextNames :: (Applicative m, MonadReader TCEnv m) => m [Name] getContextNames = map (fst . unDom) <$> getContext -- | get type of bound variable (i.e. deBruijn index) -- {-# SPECIALIZE lookupBV :: Nat -> TCM (Dom (Name, Type)) #-} lookupBV :: MonadReader TCEnv m => Nat -> m (Dom (Name, Type)) lookupBV n = do ctx <- getContext let failure = fail $ "de Bruijn index out of scope: " ++ show n ++ " in context " ++ prettyShow (map (fst . unDom) ctx) maybe failure (return . fmap (raise $ n + 1)) $ ctx !!! n {-# SPECIALIZE typeOfBV' :: Nat -> TCM (Dom Type) #-} typeOfBV' :: (Applicative m, MonadReader TCEnv m) => Nat -> m (Dom Type) typeOfBV' n = fmap snd <$> lookupBV n {-# SPECIALIZE typeOfBV :: Nat -> TCM Type #-} typeOfBV :: (Applicative m, MonadReader TCEnv m) => Nat -> m Type typeOfBV i = unDom <$> typeOfBV' i {-# SPECIALIZE nameOfBV :: Nat -> TCM Name #-} nameOfBV :: (Applicative m, MonadReader TCEnv m) => Nat -> m Name nameOfBV n = fst . unDom <$> lookupBV n -- | Get the term corresponding to a named variable. If it is a lambda bound -- variable the deBruijn index is returned and if it is a let bound variable -- its definition is returned. {-# SPECIALIZE getVarInfo :: Name -> TCM (Term, Dom Type) #-} getVarInfo #if __GLASGOW_HASKELL__ <= 708 :: (Applicative m, MonadReader TCEnv m) #else :: MonadReader TCEnv m #endif => Name -> m (Term, Dom Type) getVarInfo x = do ctx <- getContext def <- asks envLetBindings case List.findIndex ((==x) . fst . unDom) ctx of Just n -> do t <- typeOfBV' n return (var n, t) _ -> case Map.lookup x def of Just vt -> getOpen vt _ -> fail $ "unbound variable " ++ prettyShow (nameConcrete x) Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Statistics.hs0000644000000000000000000000471013154613124021233 0ustar0000000000000000-- | Collect statistics. module Agda.TypeChecking.Monad.Statistics ( tick, tickN, tickMax, getStatistics, modifyStatistics, printStatistics ) where import qualified Data.Map as Map import qualified Text.PrettyPrint.Boxes as Boxes import Agda.Syntax.Concrete.Name as C import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Options import Agda.Utils.Lens import Agda.Utils.Maybe import Agda.Utils.Null import Agda.Utils.Pretty import Agda.Utils.String -- | Get the statistics. getStatistics :: TCM Statistics getStatistics = use stStatistics -- | Modify the statistics via given function. modifyStatistics :: (Statistics -> Statistics) -> TCM () modifyStatistics f = stStatistics %= f -- | Increase specified counter by @1@. tick :: String -> TCM () tick x = tickN x 1 -- | Increase specified counter by @n@. tickN :: String -> Integer -> TCM () tickN s n = modifyCounter s (n +) -- | Set the specified counter to the maximum of its current value and @n@. tickMax :: String -> Integer -> TCM () tickMax s n = modifyCounter s (max n) -- | Modify specified counter by a function @f@. modifyCounter :: String -> (Integer -> Integer) -> TCM () modifyCounter x f = modifyStatistics $ force . update where -- We need to be strict in the map. -- Andreas, 2014-03-22: Could we take Data.Map.Strict instead of this hack? -- Or force the map by looking up the very element we inserted? -- force m = Map.lookup x m `seq` m -- Or use insertLookupWithKey? -- update m = old `seq` m' where -- (old, m') = Map.insertLookupWithKey (\ _ new old -> f old) x dummy m force m = sum (Map.elems m) `seq` m update = Map.insertWith (\ new old -> f old) x dummy dummy = f 0 -- | Print the given statistics if verbosity "profile" is given. printStatistics :: Int -> Maybe C.TopLevelModuleName -> Statistics -> TCM () printStatistics vl mmname stats = verboseS "profile" vl $ do unlessNull (Map.toList stats) $ \ stats -> do let -- First column (left aligned) is accounts. col1 = Boxes.vcat Boxes.left $ map (Boxes.text . fst) stats -- Second column (right aligned) is numbers. col2 = Boxes.vcat Boxes.right $ map (Boxes.text . showThousandSep . snd) stats table = Boxes.hsep 1 Boxes.left [col1, col2] reportSLn "profile" 1 $ caseMaybe mmname "Accumlated statistics" $ \ mname -> "Statistics for " ++ prettyShow mname reportSLn "profile" 1 $ Boxes.render table Agda-2.5.3/src/full/Agda/TypeChecking/Monad/State.hs0000644000000000000000000004040013154613124020155 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE BangPatterns #-} -- | Lenses for 'TCState' and more. module Agda.TypeChecking.Monad.State where import Control.Arrow (first) import Control.Applicative import qualified Control.Exception as E import Control.Monad.Reader (asks) import Control.Monad.State (put, get, gets, modify, modify') import Control.Monad.Trans (liftIO) import Data.Maybe import Data.Map (Map) import qualified Data.Map as Map import Data.Monoid import Data.Set (Set) import qualified Data.Set as Set import Agda.Benchmarking -- import {-# SOURCE #-} Agda.Interaction.Response import Agda.Interaction.Response (InteractionOutputCallback, Response) import Agda.Syntax.Common import Agda.Syntax.Scope.Base import qualified Agda.Syntax.Concrete.Name as C import Agda.Syntax.Abstract (PatternSynDefn, PatternSynDefns) import Agda.Syntax.Abstract.Name import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Warnings import {-# SOURCE #-} Agda.TypeChecking.Monad.Debug import {-# SOURCE #-} Agda.TypeChecking.Monad.Options import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.CompiledClause import Agda.Utils.Hash import qualified Agda.Utils.HashMap as HMap import Agda.Utils.Lens import Agda.Utils.Monad (bracket_) import Agda.Utils.Pretty import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- | Resets the non-persistent part of the type checking state. resetState :: TCM () resetState = do pers <- gets stPersistentState put $ initState { stPersistentState = pers } -- | Resets all of the type checking state. -- -- Keep only 'Benchmark' and backend information. resetAllState :: TCM () resetAllState = do b <- getBenchmark backends <- use stBackends put $ updatePersistentState (\ s -> s { stBenchmark = b }) initState stBackends .= backends -- resetAllState = put initState -- | Restore 'TCState' after performing subcomputation. -- -- In contrast to 'Agda.Utils.Monad.localState', the 'Benchmark' -- info from the subcomputation is saved. localTCState :: TCM a -> TCM a localTCState = disableDestructiveUpdate . bracket_ get (\ s -> do b <- getBenchmark put s modifyBenchmark $ const b) -- | Same as 'localTCState' but also returns the state in which we were just -- before reverting it. localTCStateSaving :: TCM a -> TCM (a, TCState) localTCStateSaving compute = do oldState <- get result <- compute newState <- get do b <- getBenchmark put oldState modifyBenchmark $ const b return (result, newState) -- | A fresh TCM instance. -- -- The computation is run in a fresh state, with the exception that -- the persistent state is preserved. If the computation changes the -- state, then these changes are ignored, except for changes to the -- persistent state. (Changes to the persistent state are also ignored -- if errors other than type errors or IO exceptions are encountered.) freshTCM :: TCM a -> TCM (Either TCErr a) freshTCM m = do ps <- use lensPersistentState let s = set lensPersistentState ps initState r <- liftIO $ (Right <$> runTCM initEnv s m) `E.catch` (return . Left) case r of Right (a, s) -> do lensPersistentState .= s ^. lensPersistentState return $ Right a Left err -> do case err of TypeError { tcErrState = s } -> lensPersistentState .= s ^. lensPersistentState IOException s _ _ -> lensPersistentState .= s ^. lensPersistentState _ -> return () return $ Left err --------------------------------------------------------------------------- -- * Lens for persistent states and its fields --------------------------------------------------------------------------- lensPersistentState :: Lens' PersistentTCState TCState lensPersistentState f s = f (stPersistentState s) <&> \ p -> s { stPersistentState = p } updatePersistentState :: (PersistentTCState -> PersistentTCState) -> (TCState -> TCState) updatePersistentState f s = s { stPersistentState = f (stPersistentState s) } modifyPersistentState :: (PersistentTCState -> PersistentTCState) -> TCM () modifyPersistentState = modify . updatePersistentState -- | Lens for 'stAccumStatistics'. lensAccumStatisticsP :: Lens' Statistics PersistentTCState lensAccumStatisticsP f s = f (stAccumStatistics s) <&> \ a -> s { stAccumStatistics = a } lensAccumStatistics :: Lens' Statistics TCState lensAccumStatistics = lensPersistentState . lensAccumStatisticsP --------------------------------------------------------------------------- -- * Scope --------------------------------------------------------------------------- -- | Get the current scope. getScope :: TCM ScopeInfo getScope = use stScope -- | Set the current scope. setScope :: ScopeInfo -> TCM () setScope scope = modifyScope (const scope) -- | Modify the current scope without updating the inverse maps. modifyScope_ :: (ScopeInfo -> ScopeInfo) -> TCM () modifyScope_ f = stScope %= f -- | Modify the current scope. modifyScope :: (ScopeInfo -> ScopeInfo) -> TCM () modifyScope f = modifyScope_ (recomputeInverseScopeMaps . f) -- | Run a computation in a local scope. withScope :: ScopeInfo -> TCM a -> TCM (a, ScopeInfo) withScope s m = do s' <- getScope setScope s x <- m s'' <- getScope setScope s' return (x, s'') -- | Same as 'withScope', but discard the scope from the computation. withScope_ :: ScopeInfo -> TCM a -> TCM a withScope_ s m = fst <$> withScope s m -- | Discard any changes to the scope by a computation. localScope :: TCM a -> TCM a localScope m = do scope <- getScope x <- m setScope scope return x -- | Scope error. notInScope :: C.QName -> TCM a notInScope x = do printScope "unbound" 5 "" typeError $ NotInScope [x] -- | Debug print the scope. printScope :: String -> Int -> String -> TCM () printScope tag v s = verboseS ("scope." ++ tag) v $ do scope <- getScope reportSDoc ("scope." ++ tag) v $ return $ vcat [ text s, pretty scope ] --------------------------------------------------------------------------- -- * Signature --------------------------------------------------------------------------- -- ** Lens for 'stSignature' and 'stImports' modifySignature :: (Signature -> Signature) -> TCM () modifySignature f = stSignature %= f modifyImportedSignature :: (Signature -> Signature) -> TCM () modifyImportedSignature f = stImports %= f getSignature :: TCM Signature getSignature = use stSignature -- | Update a possibly imported definition. Warning: changes made to imported -- definitions (during type checking) will not persist outside the current -- module. This function is currently used to update the compiled -- representation of a function during compilation. modifyGlobalDefinition :: QName -> (Definition -> Definition) -> TCM () modifyGlobalDefinition q f = do modifySignature $ updateDefinition q f modifyImportedSignature $ updateDefinition q f setSignature :: Signature -> TCM () setSignature sig = modifySignature $ const sig -- | Run some computation in a different signature, restore original signature. withSignature :: Signature -> TCM a -> TCM a withSignature sig m = do sig0 <- getSignature setSignature sig r <- m setSignature sig0 return r -- ** Modifiers for rewrite rules addRewriteRulesFor :: QName -> RewriteRules -> [QName] -> Signature -> Signature addRewriteRulesFor f rews matchables = (over sigRewriteRules $ HMap.insertWith mappend f rews) . (updateDefinition f $ updateTheDef setNotInjective) . (foldr (.) id $ map (\g -> updateDefinition g setMatchable) matchables) where setNotInjective def@Function{} = def { funInv = NotInjective } setNotInjective def = def setMatchable def = def { defMatchable = True } -- ** Modifiers for parts of the signature lookupDefinition :: QName -> Signature -> Maybe Definition lookupDefinition q sig = HMap.lookup q $ sig ^. sigDefinitions updateDefinitions :: (Definitions -> Definitions) -> Signature -> Signature updateDefinitions = over sigDefinitions updateDefinition :: QName -> (Definition -> Definition) -> Signature -> Signature updateDefinition q f = updateDefinitions $ HMap.adjust f q updateTheDef :: (Defn -> Defn) -> (Definition -> Definition) updateTheDef f def = def { theDef = f (theDef def) } updateDefType :: (Type -> Type) -> (Definition -> Definition) updateDefType f def = def { defType = f (defType def) } updateDefArgOccurrences :: ([Occurrence] -> [Occurrence]) -> (Definition -> Definition) updateDefArgOccurrences f def = def { defArgOccurrences = f (defArgOccurrences def) } updateDefPolarity :: ([Polarity] -> [Polarity]) -> (Definition -> Definition) updateDefPolarity f def = def { defPolarity = f (defPolarity def) } updateDefCompiledRep :: (CompiledRepresentation -> CompiledRepresentation) -> (Definition -> Definition) updateDefCompiledRep f def = def { defCompiledRep = f (defCompiledRep def) } addCompilerPragma :: BackendName -> CompilerPragma -> Definition -> Definition addCompilerPragma backend pragma = updateDefCompiledRep $ Map.insertWith (++) backend [pragma] updateFunClauses :: ([Clause] -> [Clause]) -> (Defn -> Defn) updateFunClauses f def@Function{ funClauses = cs} = def { funClauses = f cs } updateFunClauses f _ = __IMPOSSIBLE__ updateCompiledClauses :: (Maybe CompiledClauses -> Maybe CompiledClauses) -> (Defn -> Defn) updateCompiledClauses f def@Function{ funCompiled = cc} = def { funCompiled = f cc } updateCompiledClauses f _ = __IMPOSSIBLE__ updateFunCopatternLHS :: (Bool -> Bool) -> Defn -> Defn updateFunCopatternLHS f def@Function{ funCopatternLHS = b } = def { funCopatternLHS = f b } updateFunCopatternLHS f _ = __IMPOSSIBLE__ --------------------------------------------------------------------------- -- * Top level module --------------------------------------------------------------------------- -- | Set the top-level module. This affects the global module id of freshly -- generated names. -- TODO: Is the hash-function collision-free? If not, then the -- implementation of 'setTopLevelModule' should be changed. setTopLevelModule :: C.QName -> TCM () setTopLevelModule x = stFreshNameId .= NameId 0 (hashString $ prettyShow x) -- | Use a different top-level module for a computation. Used when generating -- names for imported modules. withTopLevelModule :: C.QName -> TCM a -> TCM a withTopLevelModule x m = do next <- use stFreshNameId setTopLevelModule x y <- m stFreshNameId .= next return y --------------------------------------------------------------------------- -- * Foreign code --------------------------------------------------------------------------- addForeignCode :: BackendName -> String -> TCM () addForeignCode backend code = do r <- asks envRange -- can't use TypeChecking.Monad.Trace.getCurrentRange without cycle stForeignCode . key backend %= Just . (ForeignCode r code :) . fromMaybe [] --------------------------------------------------------------------------- -- * Temporary: Haskell imports -- These will go away when we remove the IMPORT and HASKELL pragmas in -- favour of the FOREIGN pragma. --------------------------------------------------------------------------- addDeprecatedForeignCode :: String -> BackendName -> String -> TCM () addDeprecatedForeignCode old backend code = do warning $ DeprecationWarning (unwords ["The", old, "pragma"]) foreignPragma "2.6" addForeignCode backend code where spc | length (lines code) > 1 = "\n" | otherwise = " " foreignPragma = "{-# FOREIGN " ++ backend ++ spc ++ code ++ spc ++ "#-}" -- | Tell the compiler to import the given Haskell module. addHaskellImport :: String -> TCM () addHaskellImport i = addDeprecatedForeignCode "IMPORT" ghcBackendName $ "import qualified " ++ i -- | Tell the compiler to import the given Haskell module. addHaskellImportUHC :: String -> TCM () addHaskellImportUHC i = addDeprecatedForeignCode "IMPORT_UHC" ghcBackendName $ "__IMPORT__ " ++ i addInlineHaskell :: String -> TCM () addInlineHaskell s = addDeprecatedForeignCode "HASKELL" ghcBackendName s --------------------------------------------------------------------------- -- * Interaction output callback --------------------------------------------------------------------------- getInteractionOutputCallback :: TCM InteractionOutputCallback getInteractionOutputCallback = gets $ stInteractionOutputCallback . stPersistentState appInteractionOutputCallback :: Response -> TCM () appInteractionOutputCallback r = getInteractionOutputCallback >>= \ cb -> liftIO $ cb r setInteractionOutputCallback :: InteractionOutputCallback -> TCM () setInteractionOutputCallback cb = modifyPersistentState $ \ s -> s { stInteractionOutputCallback = cb } --------------------------------------------------------------------------- -- * Pattern synonyms --------------------------------------------------------------------------- getPatternSyns :: TCM PatternSynDefns getPatternSyns = use stPatternSyns setPatternSyns :: PatternSynDefns -> TCM () setPatternSyns m = modifyPatternSyns (const m) -- | Lens for 'stPatternSyns'. modifyPatternSyns :: (PatternSynDefns -> PatternSynDefns) -> TCM () modifyPatternSyns f = stPatternSyns %= f getPatternSynImports :: TCM PatternSynDefns getPatternSynImports = use stPatternSynImports lookupPatternSyn :: QName -> TCM PatternSynDefn lookupPatternSyn x = do s <- getPatternSyns case Map.lookup x s of Just d -> return d Nothing -> do si <- getPatternSynImports case Map.lookup x si of Just d -> return d Nothing -> notInScope $ qnameToConcrete x --------------------------------------------------------------------------- -- * Benchmark --------------------------------------------------------------------------- -- | Lens getter for 'Benchmark' from 'TCState'. theBenchmark :: TCState -> Benchmark theBenchmark = stBenchmark . stPersistentState -- | Lens map for 'Benchmark'. updateBenchmark :: (Benchmark -> Benchmark) -> TCState -> TCState updateBenchmark f = updatePersistentState $ \ s -> s { stBenchmark = f (stBenchmark s) } -- | Lens getter for 'Benchmark' from 'TCM'. getBenchmark :: TCM Benchmark getBenchmark = gets $ theBenchmark -- | Lens modify for 'Benchmark'. modifyBenchmark :: (Benchmark -> Benchmark) -> TCM () modifyBenchmark = modify' . updateBenchmark --------------------------------------------------------------------------- -- * Instance definitions --------------------------------------------------------------------------- -- | Look through the signature and reconstruct the instance table. addImportedInstances :: Signature -> TCM () addImportedInstances sig = do let itable = Map.fromListWith Set.union [ (c, Set.singleton i) | (i, Defn{ defInstance = Just c }) <- HMap.toList $ sig ^. sigDefinitions ] stImportedInstanceDefs %= Map.unionWith Set.union itable -- | Lens for 'stInstanceDefs'. updateInstanceDefs :: (TempInstanceTable -> TempInstanceTable) -> (TCState -> TCState) updateInstanceDefs = over stInstanceDefs modifyInstanceDefs :: (TempInstanceTable -> TempInstanceTable) -> TCM () modifyInstanceDefs = modify . updateInstanceDefs getAllInstanceDefs :: TCM TempInstanceTable getAllInstanceDefs = do (table,xs) <- use stInstanceDefs itable <- use stImportedInstanceDefs let !table' = Map.unionWith Set.union itable table return (table', xs) getAnonInstanceDefs :: TCM (Set QName) getAnonInstanceDefs = snd <$> getAllInstanceDefs -- | Remove all instances whose type is still unresolved. clearAnonInstanceDefs :: TCM () clearAnonInstanceDefs = modifyInstanceDefs $ mapSnd $ const Set.empty -- | Add an instance whose type is still unresolved. addUnknownInstance :: QName -> TCM () addUnknownInstance x = do reportSLn "tc.decl.instance" 10 $ "adding definition " ++ prettyShow x ++ " to the instance table (the type is not yet known)" modifyInstanceDefs $ mapSnd $ Set.insert x -- | Add instance to some ``class''. addNamedInstance :: QName -- ^ Name of the instance. -> QName -- ^ Name of the class. -> TCM () addNamedInstance x n = do reportSLn "tc.decl.instance" 10 $ "adding definition " ++ prettyShow x ++ " to instance table for " ++ prettyShow n -- Mark x as instance for n. modifySignature $ updateDefinition x $ \ d -> d { defInstance = Just n } -- Add x to n's instances. modifyInstanceDefs $ mapFst $ Map.insertWith Set.union n $ Set.singleton x Agda-2.5.3/src/full/Agda/TypeChecking/Monad/MetaVars.hs0000644000000000000000000004213113154613124020622 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.Monad.MetaVars where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad.State import Control.Monad.Reader import Control.Monad.Writer import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import qualified Data.Foldable as Fold import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Generic import Agda.Syntax.Position import Agda.Syntax.Scope.Base import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Trace import Agda.TypeChecking.Monad.Closure import Agda.TypeChecking.Monad.Debug (reportSLn) import Agda.TypeChecking.Monad.Context import Agda.TypeChecking.Substitute import {-# SOURCE #-} Agda.TypeChecking.Telescope import Agda.Utils.Functor ((<.>)) import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Pretty (prettyShow) import Agda.Utils.Tuple import Agda.Utils.Size import qualified Agda.Utils.Maybe.Strict as Strict #include "undefined.h" import Agda.Utils.Impossible -- | Switch off assignment of metas. dontAssignMetas :: TCM a -> TCM a dontAssignMetas cont = do reportSLn "tc.meta" 45 $ "don't assign metas" local (\ env -> env { envAssignMetas = False }) cont -- | Get the meta store. getMetaStore :: TCM MetaStore getMetaStore = use stMetaStore modifyMetaStore :: (MetaStore -> MetaStore) -> TCM () modifyMetaStore f = stMetaStore %= f -- | Run a computation and record which new metas it created. metasCreatedBy :: TCM a -> TCM (a, Set MetaId) metasCreatedBy m = do before <- Map.keysSet <$> use stMetaStore a <- m after <- Map.keysSet <$> use stMetaStore return (a, after Set.\\ before) -- | Lookup a meta variable lookupMeta :: MetaId -> TCM MetaVariable lookupMeta m = fromMaybeM failure $ Map.lookup m <$> getMetaStore where failure = fail $ "no such meta variable " ++ prettyShow m updateMetaVar :: MetaId -> (MetaVariable -> MetaVariable) -> TCM () updateMetaVar m f = modifyMetaStore $ Map.adjust f m getMetaPriority :: MetaId -> TCM MetaPriority getMetaPriority = mvPriority <.> lookupMeta {- UNUSED getMetaRelevance :: MetaId -> TCM Relevance getMetaRelevance x = miRelevance . mvInfo <$> lookupMeta x -} isSortMeta :: MetaId -> TCM Bool isSortMeta m = isSortMeta_ <$> lookupMeta m isSortMeta_ :: MetaVariable -> Bool isSortMeta_ mv = case mvJudgement mv of HasType{} -> False IsSort{} -> True getMetaType :: MetaId -> TCM Type getMetaType m = do mv <- lookupMeta m return $ case mvJudgement mv of HasType{ jMetaType = t } -> t IsSort{} -> __IMPOSSIBLE__ -- | Given a meta, return the type applied to the current context. getMetaTypeInContext :: MetaId -> TCM Type getMetaTypeInContext m = do MetaVar{ mvJudgement = j, mvPermutation = p } <- lookupMeta m case j of HasType{ jMetaType = t } -> do vs <- getContextArgs piApplyM t $ permute (takeP (size vs) p) vs IsSort{} -> __IMPOSSIBLE__ -- | Check whether all metas are instantiated. -- Precondition: argument is a meta (in some form) or a list of metas. class IsInstantiatedMeta a where isInstantiatedMeta :: a -> TCM Bool instance IsInstantiatedMeta MetaId where isInstantiatedMeta m = isJust <$> isInstantiatedMeta' m instance IsInstantiatedMeta Term where isInstantiatedMeta = loop where loop v = case ignoreSharing v of MetaV x _ -> isInstantiatedMeta x DontCare v -> loop v Level l -> isInstantiatedMeta l Lam _ b -> isInstantiatedMeta b Con _ _ vs -> isInstantiatedMeta vs _ -> __IMPOSSIBLE__ instance IsInstantiatedMeta Level where isInstantiatedMeta (Max ls) = isInstantiatedMeta ls instance IsInstantiatedMeta PlusLevel where isInstantiatedMeta (Plus n l) | n == 0 = isInstantiatedMeta l isInstantiatedMeta _ = __IMPOSSIBLE__ instance IsInstantiatedMeta LevelAtom where isInstantiatedMeta (MetaLevel x es) = isInstantiatedMeta x isInstantiatedMeta _ = __IMPOSSIBLE__ instance IsInstantiatedMeta a => IsInstantiatedMeta [a] where isInstantiatedMeta = andM . map isInstantiatedMeta instance IsInstantiatedMeta a => IsInstantiatedMeta (Maybe a) where isInstantiatedMeta = isInstantiatedMeta . maybeToList instance IsInstantiatedMeta a => IsInstantiatedMeta (Arg a) where isInstantiatedMeta = isInstantiatedMeta . unArg -- | Does not worry about raising. instance IsInstantiatedMeta a => IsInstantiatedMeta (Abs a) where isInstantiatedMeta = isInstantiatedMeta . unAbs isInstantiatedMeta' :: MetaId -> TCM (Maybe Term) isInstantiatedMeta' m = do mv <- lookupMeta m return $ case mvInstantiation mv of InstV tel v -> Just $ foldr mkLam v tel _ -> Nothing -- | Returns every meta-variable occurrence in the given type, except -- for those in 'Sort's. allMetas :: TermLike a => a -> [MetaId] allMetas = foldTerm metas where metas (MetaV m _) = [m] metas (Level l) = levelMetas l metas _ = [] levelMetas (Max as) = concatMap plusLevelMetas as plusLevelMetas ClosedLevel{} = [] plusLevelMetas (Plus _ l) = levelAtomMetas l levelAtomMetas (MetaLevel m _) = [m] levelAtomMetas _ = [] -- | Create 'MetaInfo' in the current environment. createMetaInfo :: TCM MetaInfo createMetaInfo = createMetaInfo' RunMetaOccursCheck createMetaInfo' :: RunMetaOccursCheck -> TCM MetaInfo createMetaInfo' b = do r <- getCurrentRange cl <- buildClosure r return MetaInfo { miClosRange = cl , miMetaOccursCheck = b , miNameSuggestion = "" } setValueMetaName :: Term -> MetaNameSuggestion -> TCM () setValueMetaName v s = do case ignoreSharing v of MetaV mi _ -> setMetaNameSuggestion mi s u -> do reportSLn "tc.meta.name" 70 $ "cannot set meta name; newMeta returns " ++ show u return () getMetaNameSuggestion :: MetaId -> TCM MetaNameSuggestion getMetaNameSuggestion mi = miNameSuggestion . mvInfo <$> lookupMeta mi setMetaNameSuggestion :: MetaId -> MetaNameSuggestion -> TCM () setMetaNameSuggestion mi s = unless (null s || isUnderscore s) $ do reportSLn "tc.meta.name" 20 $ "setting name of meta " ++ prettyShow mi ++ " to " ++ s updateMetaVar mi $ \ mvar -> mvar { mvInfo = (mvInfo mvar) { miNameSuggestion = s }} updateMetaVarRange :: MetaId -> Range -> TCM () updateMetaVarRange mi r = updateMetaVar mi (setRange r) setMetaOccursCheck :: MetaId -> RunMetaOccursCheck -> TCM () setMetaOccursCheck mi b = updateMetaVar mi $ \ mvar -> mvar { mvInfo = (mvInfo mvar) { miMetaOccursCheck = b } } -- * Query and manipulate interaction points. modifyInteractionPoints :: (InteractionPoints -> InteractionPoints) -> TCM () modifyInteractionPoints f = stInteractionPoints %= f -- | Register an interaction point during scope checking. -- If there is no interaction id yet, create one. registerInteractionPoint :: Bool -> Range -> Maybe Nat -> TCM InteractionId registerInteractionPoint preciseRange r maybeId = do m <- use stInteractionPoints if not preciseRange then continue m else do -- If the range does not come from a file, it is not -- precise, so ignore it. Strict.caseMaybe (rangeFile r) (continue m) $ \ _ -> do -- First, try to find the interaction point by Range. caseMaybe (findInteractionPoint_ r m) (continue m) {-else-} return where continue m = do -- We did not find an interaction id with the same Range, so let's create one! ii <- case maybeId of Just i -> return $ InteractionId i Nothing -> fresh let ip = InteractionPoint { ipRange = r, ipMeta = Nothing, ipSolved = False, ipClause = IPNoClause } case Map.insertLookupWithKey (\ key new old -> old) ii ip m of -- If the interaction point is already present, we keep the old ip. -- However, it needs to be at the same range as the new one. (Just ip0, _) | ipRange ip /= ipRange ip0 -> __IMPOSSIBLE__ | otherwise -> return ii (Nothing, m') -> do modifyInteractionPoints (const m') return ii -- | Find an interaction point by 'Range' by searching the whole map. -- -- O(n): linear in the number of registered interaction points. findInteractionPoint_ :: Range -> InteractionPoints -> Maybe InteractionId findInteractionPoint_ r m = do guard $ not $ null r headMaybe $ mapMaybe sameRange $ Map.toList m where sameRange :: (InteractionId, InteractionPoint) -> Maybe InteractionId sameRange (ii, InteractionPoint r' _ _ _) | r == r' = Just ii sameRange _ = Nothing -- | Hook up meta variable to interaction point. connectInteractionPoint :: InteractionId -> MetaId -> TCM () connectInteractionPoint ii mi = do ipCl <- asks envClause m <- use stInteractionPoints let ip = InteractionPoint { ipRange = __IMPOSSIBLE__, ipMeta = Just mi, ipSolved = False, ipClause = ipCl } -- The interaction point needs to be present already, we just set the meta. case Map.insertLookupWithKey (\ key new old -> new { ipRange = ipRange old }) ii ip m of (Nothing, _) -> __IMPOSSIBLE__ (Just _, m') -> modifyInteractionPoints $ const m' -- | Mark an interaction point as solved. removeInteractionPoint :: InteractionId -> TCM () removeInteractionPoint ii = do stInteractionPoints %= Map.update (\ ip -> Just ip{ ipSolved = True }) ii -- | Get a list of interaction ids. getInteractionPoints :: TCM [InteractionId] getInteractionPoints = Map.keys <$> use stInteractionPoints -- | Get all metas that correspond to unsolved interaction ids. getInteractionMetas :: TCM [MetaId] getInteractionMetas = mapMaybe ipMeta . filter (not . ipSolved) . Map.elems <$> use stInteractionPoints -- | Get all metas that correspond to unsolved interaction ids. getInteractionIdsAndMetas :: TCM [(InteractionId,MetaId)] getInteractionIdsAndMetas = mapMaybe f . filter (not . ipSolved . snd) . Map.toList <$> use stInteractionPoints where f (ii, ip) = (ii,) <$> ipMeta ip -- | Does the meta variable correspond to an interaction point? -- -- Time: @O(n)@ where @n@ is the number of interaction metas. isInteractionMeta :: MetaId -> TCM (Maybe InteractionId) isInteractionMeta x = lookup x . map swap <$> getInteractionIdsAndMetas -- | Get the information associated to an interaction point. lookupInteractionPoint :: InteractionId -> TCM InteractionPoint lookupInteractionPoint ii = fromMaybeM err $ Map.lookup ii <$> use stInteractionPoints where err = fail $ "no such interaction point: " ++ show ii -- | Get 'MetaId' for an interaction point. -- Precondition: interaction point is connected. lookupInteractionId :: InteractionId -> TCM MetaId lookupInteractionId ii = fromMaybeM err2 $ ipMeta <$> lookupInteractionPoint ii where err2 = typeError $ GenericError $ "No type nor action available for hole " ++ show ii ++ ". Possible cause: the hole has not been reached during type checking (do you see yellow?)" -- | Check whether an interaction id is already associated with a meta variable. lookupInteractionMeta :: InteractionId -> TCM (Maybe MetaId) lookupInteractionMeta ii = lookupInteractionMeta_ ii <$> use stInteractionPoints lookupInteractionMeta_ :: InteractionId -> InteractionPoints -> Maybe MetaId lookupInteractionMeta_ ii m = ipMeta =<< Map.lookup ii m -- | Generate new meta variable. newMeta :: MetaInfo -> MetaPriority -> Permutation -> Judgement a -> TCM MetaId newMeta = newMeta' Open -- | Generate a new meta variable with some instantiation given. -- For instance, the instantiation could be a 'PostponedTypeCheckingProblem'. newMeta' :: MetaInstantiation -> MetaInfo -> MetaPriority -> Permutation -> Judgement a -> TCM MetaId newMeta' inst mi p perm j = do x <- fresh let j' = j { jMetaId = x } -- fill the identifier part of the judgement mv = MetaVar mi p perm j' inst Set.empty Instantiable -- printing not available (import cycle) -- reportSDoc "tc.meta.new" 50 $ text "new meta" <+> prettyTCM j' stMetaStore %= Map.insert x mv return x -- | Get the 'Range' for an interaction point. getInteractionRange :: InteractionId -> TCM Range getInteractionRange = ipRange <.> lookupInteractionPoint -- | Get the 'Range' for a meta variable. getMetaRange :: MetaId -> TCM Range getMetaRange = getRange <.> lookupMeta getInteractionScope :: InteractionId -> TCM ScopeInfo getInteractionScope = getMetaScope <.> lookupMeta <=< lookupInteractionId withMetaInfo' :: MetaVariable -> TCM a -> TCM a withMetaInfo' mv = withMetaInfo (miClosRange $ mvInfo mv) withMetaInfo :: Closure Range -> TCM a -> TCM a withMetaInfo mI cont = enterClosure mI $ \ r -> setCurrentRange r cont getInstantiatedMetas :: TCM [MetaId] getInstantiatedMetas = do store <- getMetaStore return [ i | (i, MetaVar{ mvInstantiation = mi }) <- Map.assocs store, isInst mi ] where isInst Open = False isInst OpenIFS = False isInst BlockedConst{} = False isInst PostponedTypeCheckingProblem{} = False isInst InstV{} = True getOpenMetas :: TCM [MetaId] getOpenMetas = do store <- getMetaStore return [ i | (i, MetaVar{ mvInstantiation = mi }) <- Map.assocs store, isOpenMeta mi ] isOpenMeta :: MetaInstantiation -> Bool isOpenMeta Open = True isOpenMeta OpenIFS = True isOpenMeta BlockedConst{} = True isOpenMeta PostponedTypeCheckingProblem{} = True isOpenMeta InstV{} = False -- | @listenToMeta l m@: register @l@ as a listener to @m@. This is done -- when the type of l is blocked by @m@. listenToMeta :: Listener -> MetaId -> TCM () listenToMeta l m = updateMetaVar m $ \mv -> mv { mvListeners = Set.insert l $ mvListeners mv } -- | Unregister a listener. unlistenToMeta :: Listener -> MetaId -> TCM () unlistenToMeta l m = updateMetaVar m $ \mv -> mv { mvListeners = Set.delete l $ mvListeners mv } -- | Get the listeners to a meta. getMetaListeners :: MetaId -> TCM [Listener] getMetaListeners m = Set.toList . mvListeners <$> lookupMeta m clearMetaListeners :: MetaId -> TCM () clearMetaListeners m = updateMetaVar m $ \mv -> mv { mvListeners = Set.empty } --------------------------------------------------------------------------- -- * Freezing and unfreezing metas. --------------------------------------------------------------------------- -- | Freeze all so far unfrozen metas for the duration of the given computation. withFreezeMetas :: TCM a -> TCM a withFreezeMetas cont = do ms <- Set.fromList <$> freezeMetas x <- cont unfreezeMetas' (`Set.member` ms) return x -- | Freeze all meta variables and return the list of metas that got frozen. freezeMetas :: TCM [MetaId] freezeMetas = freezeMetas' $ const True -- | Freeze some meta variables and return the list of metas that got frozen. freezeMetas' :: (MetaId -> Bool) -> TCM [MetaId] freezeMetas' p = execWriterT $ stMetaStore %== Map.traverseWithKey freeze where freeze :: Monad m => MetaId -> MetaVariable -> WriterT [MetaId] m MetaVariable freeze m mvar | p m && mvFrozen mvar /= Frozen = do tell [m] return $ mvar { mvFrozen = Frozen } | otherwise = return mvar -- | Thaw all meta variables. unfreezeMetas :: TCM () unfreezeMetas = unfreezeMetas' $ const True -- | Thaw some metas, as indicated by the passed condition. unfreezeMetas' :: (MetaId -> Bool) -> TCM () unfreezeMetas' cond = modifyMetaStore $ Map.mapWithKey unfreeze where unfreeze :: MetaId -> MetaVariable -> MetaVariable unfreeze m mvar | cond m = mvar { mvFrozen = Instantiable } | otherwise = mvar isFrozen :: MetaId -> TCM Bool isFrozen x = do mvar <- lookupMeta x return $ mvFrozen mvar == Frozen -- | Unfreeze meta and its type if this is a meta again. -- Does not unfreeze deep occurrences of metas. class UnFreezeMeta a where unfreezeMeta :: a -> TCM () instance UnFreezeMeta MetaId where unfreezeMeta x = do updateMetaVar x $ \ mv -> mv { mvFrozen = Instantiable } unfreezeMeta =<< do jMetaType . mvJudgement <$> lookupMeta x instance UnFreezeMeta Type where unfreezeMeta (El s t) = unfreezeMeta s >> unfreezeMeta t instance UnFreezeMeta Term where unfreezeMeta (Shared p) = unfreezeMeta $ derefPtr p unfreezeMeta (MetaV x _) = unfreezeMeta x unfreezeMeta (Sort s) = unfreezeMeta s unfreezeMeta (Level l) = unfreezeMeta l unfreezeMeta (DontCare t) = unfreezeMeta t unfreezeMeta (Lam _ v) = unfreezeMeta v unfreezeMeta _ = return () instance UnFreezeMeta Sort where unfreezeMeta (Type l) = unfreezeMeta l unfreezeMeta _ = return () instance UnFreezeMeta Level where unfreezeMeta (Max ls) = unfreezeMeta ls instance UnFreezeMeta PlusLevel where unfreezeMeta (Plus _ a) = unfreezeMeta a unfreezeMeta ClosedLevel{} = return () instance UnFreezeMeta LevelAtom where unfreezeMeta (MetaLevel x _) = unfreezeMeta x unfreezeMeta (BlockedLevel _ t) = unfreezeMeta t unfreezeMeta (NeutralLevel _ t) = unfreezeMeta t unfreezeMeta (UnreducedLevel t) = unfreezeMeta t instance UnFreezeMeta a => UnFreezeMeta [a] where unfreezeMeta = mapM_ unfreezeMeta instance UnFreezeMeta a => UnFreezeMeta (Abs a) where unfreezeMeta = Fold.mapM_ unfreezeMeta Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Base.hs0000644000000000000000000037106713154613124017767 0ustar0000000000000000{-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} module Agda.TypeChecking.Monad.Base where import Prelude hiding (null) import qualified Control.Concurrent as C import qualified Control.Exception as E import Control.Monad.State import Control.Monad.Reader import Control.Monad.Writer hiding ((<>)) import Control.Monad.Trans.Maybe import Control.Applicative hiding (empty) import Data.Function import Data.Int import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import qualified Data.List as List import Data.Maybe import Data.Map (Map) import qualified Data.Map as Map -- hiding (singleton, null, empty) import Data.Set (Set) import qualified Data.Set as Set -- hiding (singleton, null, empty) import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend, Any(..)) import Data.Data (Data, toConstr) import Data.Typeable (Typeable) import Data.Foldable (Foldable) import Data.Traversable import Data.IORef import qualified System.Console.Haskeline as Haskeline import Agda.Benchmarking (Benchmark, Phase) import Agda.Syntax.Concrete (TopLevelModuleName) import Agda.Syntax.Common import qualified Agda.Syntax.Concrete as C import Agda.Syntax.Concrete.Definitions (NiceDeclaration, DeclarationWarning) import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract (AllNames) import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Pattern () import Agda.Syntax.Internal.Generic (TermLike(..)) import Agda.Syntax.Literal import Agda.Syntax.Parser (PM(..), ParseWarning, runPMIO) import Agda.Syntax.Treeless (Compiled) import Agda.Syntax.Fixity import Agda.Syntax.Position import Agda.Syntax.Scope.Base import qualified Agda.Syntax.Info as Info import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Free.Lazy (Free(freeVars'), bind', bind) import {-# SOURCE #-} Agda.Compiler.Backend -- import {-# SOURCE #-} Agda.Interaction.FindFile import Agda.Interaction.Options import Agda.Interaction.Response (InteractionOutputCallback, defaultInteractionOutputCallback, Response(..)) import Agda.Interaction.Highlighting.Precise (CompressedFile, HighlightingInfo) import Agda.Utils.Except ( Error(strMsg) , ExceptT , MonadError(catchError, throwError) , runExceptT ) import Agda.Utils.Benchmark (MonadBench(..)) import Agda.Utils.FileName import Agda.Utils.HashMap (HashMap) import qualified Agda.Utils.HashMap as HMap import Agda.Utils.Hash import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.ListT import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Pretty hiding ((<>)) import qualified Agda.Utils.Pretty as P import Agda.Utils.Singleton import Agda.Utils.Functor import Agda.Utils.Function #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Type checking state --------------------------------------------------------------------------- data TCState = TCSt { stPreScopeState :: !PreScopeState -- ^ The state which is frozen after scope checking. , stPostScopeState :: !PostScopeState -- ^ The state which is modified after scope checking. , stPersistentState :: !PersistentTCState -- ^ State which is forever, like a diamond. } #if __GLASGOW_HASKELL__ <= 708 deriving Typeable #endif class Monad m => ReadTCState m where getTCState :: m TCState instance Show TCState where show _ = "TCSt{}" data PreScopeState = PreScopeState { stPreTokens :: !CompressedFile -- from lexer -- ^ Highlighting info for tokens (but not those tokens for -- which highlighting exists in 'stSyntaxInfo'). , stPreImports :: !Signature -- XX populated by scopec hecker -- ^ Imported declared identifiers. -- Those most not be serialized! , stPreImportedModules :: !(Set ModuleName) -- imports logic , stPreModuleToSource :: !ModuleToSource -- imports , stPreVisitedModules :: !VisitedModules -- imports , stPreScope :: !ScopeInfo -- generated by scope checker, current file: which modules you have, public definitions, current file, maps concrete names to abstract names. , stPrePatternSyns :: !A.PatternSynDefns -- ^ Pattern synonyms of the current file. Serialized. , stPrePatternSynImports :: !A.PatternSynDefns -- ^ Imported pattern synonyms. Must not be serialized! , stPrePragmaOptions :: !PragmaOptions -- ^ Options applying to the current file. @OPTIONS@ -- pragmas only affect this field. , stPreImportedBuiltins :: !(BuiltinThings PrimFun) , stPreImportedDisplayForms :: !DisplayForms -- ^ Display forms added by someone else to imported identifiers , stPreImportedInstanceDefs :: !InstanceTable , stPreForeignCode :: !(Map BackendName [ForeignCode]) -- ^ @{-# FOREIGN #-}@ code that should be included in the compiled output. -- Does not include code for imported modules. , stPreFreshInteractionId :: !InteractionId } type DisambiguatedNames = IntMap A.QName data PostScopeState = PostScopeState { stPostSyntaxInfo :: !CompressedFile -- ^ Highlighting info. , stPostDisambiguatedNames :: !DisambiguatedNames -- ^ Disambiguation carried out by the type checker. -- Maps position of first name character to disambiguated @'A.QName'@ -- for each @'A.AmbiguousQName'@ already passed by the type checker. , stPostMetaStore :: !MetaStore , stPostInteractionPoints :: !InteractionPoints -- scope checker first , stPostAwakeConstraints :: !Constraints , stPostSleepingConstraints :: !Constraints , stPostDirty :: !Bool -- local -- ^ Dirty when a constraint is added, used to prevent pointer update. -- Currently unused. , stPostOccursCheckDefs :: !(Set QName) -- local -- ^ Definitions to be considered during occurs check. -- Initialized to the current mutual block before the check. -- During occurs check, we remove definitions from this set -- as soon we have checked them. , stPostSignature :: !Signature -- ^ Declared identifiers of the current file. -- These will be serialized after successful type checking. , stPostModuleParameters :: !ModuleParamDict -- ^ TODO: can these be moved into the @TCEnv@? , stPostImportsDisplayForms :: !DisplayForms -- ^ Display forms we add for imported identifiers , stPostCurrentModule :: !(Maybe ModuleName) -- ^ The current module is available after it has been type -- checked. , stPostInstanceDefs :: !TempInstanceTable , stPostStatistics :: !Statistics -- ^ Counters to collect various statistics about meta variables etc. -- Only for current file. , stPostTCWarnings :: ![TCWarning] , stPostMutualBlocks :: !(Map MutualId MutualBlock) , stPostLocalBuiltins :: !(BuiltinThings PrimFun) , stPostFreshMetaId :: !MetaId , stPostFreshMutualId :: !MutualId , stPostFreshCtxId :: !CtxId , stPostFreshProblemId :: !ProblemId , stPostFreshInt :: !Int , stPostFreshNameId :: !NameId } -- | A mutual block of names in the signature. data MutualBlock = MutualBlock { mutualInfo :: Info.MutualInfo -- ^ The original info of the mutual block. , mutualNames :: Set QName } deriving (Show, Eq) instance Null MutualBlock where empty = MutualBlock empty empty -- | A part of the state which is not reverted when an error is thrown -- or the state is reset. data PersistentTCState = PersistentTCSt { stDecodedModules :: DecodedModules , stPersistentOptions :: CommandLineOptions , stInteractionOutputCallback :: InteractionOutputCallback -- ^ Callback function to call when there is a response -- to give to the interactive frontend. -- See the documentation of 'InteractionOutputCallback'. , stBenchmark :: !Benchmark -- ^ Structure to track how much CPU time was spent on which Agda phase. -- Needs to be a strict field to avoid space leaks! , stAccumStatistics :: !Statistics -- ^ Should be strict field. , stLoadedFileCache :: !(Maybe LoadedFileCache) -- ^ Cached typechecking state from the last loaded file. -- Should be Nothing when checking imports. , stPersistBackends :: [Backend] -- ^ Current backends with their options } data LoadedFileCache = LoadedFileCache { lfcCached :: !CachedTypeCheckLog , lfcCurrent :: !CurrentTypeCheckLog } -- | A log of what the type checker does and states after the action is -- completed. The cached version is stored first executed action first. type CachedTypeCheckLog = [(TypeCheckAction, PostScopeState)] -- | Like 'CachedTypeCheckLog', but storing the log for an ongoing type -- checking of a module. Stored in reverse order (last performed action -- first). type CurrentTypeCheckLog = [(TypeCheckAction, PostScopeState)] -- | A complete log for a module will look like this: -- -- * 'Pragmas' -- -- * 'EnterSection', entering the main module. -- -- * 'Decl'/'EnterSection'/'LeaveSection', for declarations and nested -- modules -- -- * 'LeaveSection', leaving the main module. data TypeCheckAction = EnterSection !Info.ModuleInfo !ModuleName ![A.TypedBindings] | LeaveSection !ModuleName | Decl !A.Declaration -- ^ Never a Section or ScopeDecl | Pragmas !PragmaOptions -- | Empty persistent state. initPersistentState :: PersistentTCState initPersistentState = PersistentTCSt { stPersistentOptions = defaultOptions , stDecodedModules = Map.empty , stInteractionOutputCallback = defaultInteractionOutputCallback , stBenchmark = empty , stAccumStatistics = Map.empty , stLoadedFileCache = Nothing , stPersistBackends = [] } -- | Empty state of type checker. initPreScopeState :: PreScopeState initPreScopeState = PreScopeState { stPreTokens = mempty , stPreImports = emptySignature , stPreImportedModules = Set.empty , stPreModuleToSource = Map.empty , stPreVisitedModules = Map.empty , stPreScope = emptyScopeInfo , stPrePatternSyns = Map.empty , stPrePatternSynImports = Map.empty , stPrePragmaOptions = defaultInteractionOptions , stPreImportedBuiltins = Map.empty , stPreImportedDisplayForms = HMap.empty , stPreImportedInstanceDefs = Map.empty , stPreForeignCode = Map.empty , stPreFreshInteractionId = 0 } initPostScopeState :: PostScopeState initPostScopeState = PostScopeState { stPostSyntaxInfo = mempty , stPostDisambiguatedNames = IntMap.empty , stPostMetaStore = Map.empty , stPostInteractionPoints = Map.empty , stPostAwakeConstraints = [] , stPostSleepingConstraints = [] , stPostDirty = False , stPostOccursCheckDefs = Set.empty , stPostSignature = emptySignature , stPostModuleParameters = Map.empty , stPostImportsDisplayForms = HMap.empty , stPostCurrentModule = Nothing , stPostInstanceDefs = (Map.empty , Set.empty) , stPostStatistics = Map.empty , stPostTCWarnings = [] , stPostMutualBlocks = Map.empty , stPostLocalBuiltins = Map.empty , stPostFreshMetaId = 0 , stPostFreshMutualId = 0 , stPostFreshCtxId = 0 , stPostFreshProblemId = 1 , stPostFreshInt = 0 , stPostFreshNameId = NameId 0 0 } initState :: TCState initState = TCSt { stPreScopeState = initPreScopeState , stPostScopeState = initPostScopeState , stPersistentState = initPersistentState } -- * st-prefixed lenses ------------------------------------------------------------------------ stTokens :: Lens' CompressedFile TCState stTokens f s = f (stPreTokens (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreTokens = x}} stImports :: Lens' Signature TCState stImports f s = f (stPreImports (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreImports = x}} stImportedModules :: Lens' (Set ModuleName) TCState stImportedModules f s = f (stPreImportedModules (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreImportedModules = x}} stModuleToSource :: Lens' ModuleToSource TCState stModuleToSource f s = f (stPreModuleToSource (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreModuleToSource = x}} stVisitedModules :: Lens' VisitedModules TCState stVisitedModules f s = f (stPreVisitedModules (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreVisitedModules = x}} stScope :: Lens' ScopeInfo TCState stScope f s = f (stPreScope (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreScope = x}} stPatternSyns :: Lens' A.PatternSynDefns TCState stPatternSyns f s = f (stPrePatternSyns (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPrePatternSyns = x}} stPatternSynImports :: Lens' A.PatternSynDefns TCState stPatternSynImports f s = f (stPrePatternSynImports (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPrePatternSynImports = x}} stPragmaOptions :: Lens' PragmaOptions TCState stPragmaOptions f s = f (stPrePragmaOptions (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPrePragmaOptions = x}} stImportedBuiltins :: Lens' (BuiltinThings PrimFun) TCState stImportedBuiltins f s = f (stPreImportedBuiltins (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreImportedBuiltins = x}} stForeignCode :: Lens' (Map BackendName [ForeignCode]) TCState stForeignCode f s = f (stPreForeignCode (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreForeignCode = x}} stFreshInteractionId :: Lens' InteractionId TCState stFreshInteractionId f s = f (stPreFreshInteractionId (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreFreshInteractionId = x}} stBackends :: Lens' [Backend] TCState stBackends f s = f (stPersistBackends (stPersistentState s)) <&> \x -> s {stPersistentState = (stPersistentState s) {stPersistBackends = x}} stFreshNameId :: Lens' NameId TCState stFreshNameId f s = f (stPostFreshNameId (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostFreshNameId = x}} stSyntaxInfo :: Lens' CompressedFile TCState stSyntaxInfo f s = f (stPostSyntaxInfo (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostSyntaxInfo = x}} stDisambiguatedNames :: Lens' DisambiguatedNames TCState stDisambiguatedNames f s = f (stPostDisambiguatedNames (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostDisambiguatedNames = x}} stMetaStore :: Lens' MetaStore TCState stMetaStore f s = f (stPostMetaStore (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostMetaStore = x}} stInteractionPoints :: Lens' InteractionPoints TCState stInteractionPoints f s = f (stPostInteractionPoints (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostInteractionPoints = x}} stAwakeConstraints :: Lens' Constraints TCState stAwakeConstraints f s = f (stPostAwakeConstraints (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostAwakeConstraints = x}} stSleepingConstraints :: Lens' Constraints TCState stSleepingConstraints f s = f (stPostSleepingConstraints (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostSleepingConstraints = x}} stDirty :: Lens' Bool TCState stDirty f s = f (stPostDirty (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostDirty = x}} stOccursCheckDefs :: Lens' (Set QName) TCState stOccursCheckDefs f s = f (stPostOccursCheckDefs (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostOccursCheckDefs = x}} stSignature :: Lens' Signature TCState stSignature f s = f (stPostSignature (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostSignature = x}} stModuleParameters :: Lens' (ModuleParamDict) TCState stModuleParameters f s = f (stPostModuleParameters (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostModuleParameters = x}} stImportsDisplayForms :: Lens' DisplayForms TCState stImportsDisplayForms f s = f (stPostImportsDisplayForms (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostImportsDisplayForms = x}} stImportedDisplayForms :: Lens' DisplayForms TCState stImportedDisplayForms f s = f (stPreImportedDisplayForms (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreImportedDisplayForms = x}} stCurrentModule :: Lens' (Maybe ModuleName) TCState stCurrentModule f s = f (stPostCurrentModule (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostCurrentModule = x}} stImportedInstanceDefs :: Lens' InstanceTable TCState stImportedInstanceDefs f s = f (stPreImportedInstanceDefs (stPreScopeState s)) <&> \x -> s {stPreScopeState = (stPreScopeState s) {stPreImportedInstanceDefs = x}} stInstanceDefs :: Lens' TempInstanceTable TCState stInstanceDefs f s = f (stPostInstanceDefs (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostInstanceDefs = x}} stStatistics :: Lens' Statistics TCState stStatistics f s = f (stPostStatistics (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostStatistics = x}} stTCWarnings :: Lens' [TCWarning] TCState stTCWarnings f s = f (stPostTCWarnings (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostTCWarnings = x}} stMutualBlocks :: Lens' (Map MutualId MutualBlock) TCState stMutualBlocks f s = f (stPostMutualBlocks (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostMutualBlocks = x}} stLocalBuiltins :: Lens' (BuiltinThings PrimFun) TCState stLocalBuiltins f s = f (stPostLocalBuiltins (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostLocalBuiltins = x}} stFreshMetaId :: Lens' MetaId TCState stFreshMetaId f s = f (stPostFreshMetaId (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostFreshMetaId = x}} stFreshMutualId :: Lens' MutualId TCState stFreshMutualId f s = f (stPostFreshMutualId (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostFreshMutualId = x}} stFreshCtxId :: Lens' CtxId TCState stFreshCtxId f s = f (stPostFreshCtxId (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostFreshCtxId = x}} stFreshProblemId :: Lens' ProblemId TCState stFreshProblemId f s = f (stPostFreshProblemId (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostFreshProblemId = x}} stFreshInt :: Lens' Int TCState stFreshInt f s = f (stPostFreshInt (stPostScopeState s)) <&> \x -> s {stPostScopeState = (stPostScopeState s) {stPostFreshInt = x}} stBuiltinThings :: TCState -> BuiltinThings PrimFun stBuiltinThings s = (s^.stLocalBuiltins) `Map.union` (s^.stImportedBuiltins) -- * Fresh things ------------------------------------------------------------------------ class Enum i => HasFresh i where freshLens :: Lens' i TCState nextFresh' :: i -> i nextFresh' = succ nextFresh :: HasFresh i => TCState -> (i, TCState) nextFresh s = let !c = s^.freshLens in (c, set freshLens (nextFresh' c) s) fresh :: (HasFresh i, MonadState TCState m) => m i fresh = do !s <- get let (!c , !s') = nextFresh s put s' return c instance HasFresh MetaId where freshLens = stFreshMetaId instance HasFresh MutualId where freshLens = stFreshMutualId instance HasFresh InteractionId where freshLens = stFreshInteractionId instance HasFresh NameId where freshLens = stFreshNameId -- nextFresh increments the current fresh name by 2 so @NameId@s used -- before caching starts do not overlap with the ones used after. nextFresh' = succ . succ instance HasFresh CtxId where freshLens = stFreshCtxId instance HasFresh Int where freshLens = stFreshInt newtype ProblemId = ProblemId Nat deriving (Typeable, Data, Eq, Ord, Enum, Real, Integral, Num) -- TODO: 'Show' should output Haskell-parseable representations. -- The following instance is deprecated, and Pretty[TCM] should be used -- instead. Later, simply derive Show for this type. -- ASR (28 December 2014). This instance is not used anymore (module -- the test suite) when reporting errors. See Issue 1293. -- This particular Show instance is ok because of the Num instance. instance Show ProblemId where show (ProblemId n) = show n instance Pretty ProblemId where pretty (ProblemId n) = pretty n instance HasFresh ProblemId where freshLens = stFreshProblemId freshName :: MonadState TCState m => Range -> String -> m Name freshName r s = do i <- fresh return $ mkName r i s freshNoName :: MonadState TCState m => Range -> m Name freshNoName r = do i <- fresh return $ Name i (C.NoName noRange i) r noFixity' freshNoName_ :: MonadState TCState m => m Name freshNoName_ = freshNoName noRange -- | Create a fresh name from @a@. class FreshName a where freshName_ :: MonadState TCState m => a -> m Name instance FreshName (Range, String) where freshName_ = uncurry freshName instance FreshName String where freshName_ = freshName noRange instance FreshName Range where freshName_ = freshNoName instance FreshName () where freshName_ () = freshNoName_ --------------------------------------------------------------------------- -- ** Managing file names --------------------------------------------------------------------------- -- | Maps top-level module names to the corresponding source file -- names. type ModuleToSource = Map TopLevelModuleName AbsolutePath -- | Maps source file names to the corresponding top-level module -- names. type SourceToModule = Map AbsolutePath TopLevelModuleName -- | Creates a 'SourceToModule' map based on 'stModuleToSource'. -- -- O(n log n). -- -- For a single reverse lookup in 'stModuleToSource', -- rather use 'lookupModuleFromSourse'. sourceToModule :: TCM SourceToModule sourceToModule = Map.fromList . List.map (\(m, f) -> (f, m)) . Map.toList <$> use stModuleToSource -- | Lookup an 'AbsolutePath' in 'sourceToModule'. -- -- O(n). lookupModuleFromSource :: AbsolutePath -> TCM (Maybe TopLevelModuleName) lookupModuleFromSource f = fmap fst . List.find ((f ==) . snd) . Map.toList <$> use stModuleToSource --------------------------------------------------------------------------- -- ** Interface --------------------------------------------------------------------------- data ModuleInfo = ModuleInfo { miInterface :: Interface , miWarnings :: Bool -- ^ 'True' if warnings were encountered when the module was type -- checked. } -- Note that the use of 'C.TopLevelModuleName' here is a potential -- performance problem, because these names do not contain unique -- identifiers. type VisitedModules = Map C.TopLevelModuleName ModuleInfo type DecodedModules = Map C.TopLevelModuleName Interface data ForeignCode = ForeignCode Range String deriving (Show, Typeable) data Interface = Interface { iSourceHash :: Hash -- ^ Hash of the source code. , iImportedModules :: [(ModuleName, Hash)] -- ^ Imported modules and their hashes. , iModuleName :: ModuleName -- ^ Module name of this interface. , iScope :: Map ModuleName Scope -- ^ Scope defined by this module. -- -- Andreas, AIM XX: Too avoid duplicate serialization, this field is -- not serialized, so if you deserialize an interface, @iScope@ -- will be empty. -- But 'constructIScope' constructs 'iScope' from 'iInsideScope'. , iInsideScope :: ScopeInfo -- ^ Scope after we loaded this interface. -- Used in 'Agda.Interaction.BasicOps.AtTopLevel' -- and 'Agda.Interaction.CommandLine.interactionLoop'. , iSignature :: Signature , iDisplayForms :: DisplayForms -- ^ Display forms added for imported identifiers. , iBuiltin :: BuiltinThings (String, QName) , iForeignCode :: Map BackendName [ForeignCode] , iHighlighting :: HighlightingInfo , iPragmaOptions :: [OptionsPragma] -- ^ Pragma options set in the file. , iPatternSyns :: A.PatternSynDefns , iWarnings :: [TCWarning] } deriving (Typeable, Show) instance Pretty Interface where pretty (Interface sourceH importedM moduleN scope insideS signature display builtin foreignCode highlighting pragmaO patternS warnings) = hang (text "Interface") 2 $ vcat [ text "source hash:" <+> (pretty . show) sourceH , text "imported modules:" <+> (pretty . show) importedM , text "module name:" <+> pretty moduleN , text "scope:" <+> (pretty . show) scope , text "inside scope:" <+> (pretty . show) insideS , text "signature:" <+> (pretty . show) signature , text "display:" <+> (pretty . show) display , text "builtin:" <+> (pretty . show) builtin , text "Foreign code:" <+> (pretty . show) foreignCode , text "highlighting:" <+> (pretty . show) highlighting , text "pragma options:" <+> (pretty . show) pragmaO , text "pattern syns:" <+> (pretty . show) patternS , text "warnings:" <+> (pretty . show) warnings ] -- | Combines the source hash and the (full) hashes of the imported modules. iFullHash :: Interface -> Hash iFullHash i = combineHashes $ iSourceHash i : List.map snd (iImportedModules i) --------------------------------------------------------------------------- -- ** Closure --------------------------------------------------------------------------- data Closure a = Closure { clSignature :: Signature , clEnv :: TCEnv , clScope :: ScopeInfo , clModuleParameters :: ModuleParamDict -- ^ Since module parameters are currently stored in 'TCState' -- not in 'TCEnv', we save them here. -- The map contains for each 'ModuleName' @M@ with module telescope @Γ_M@ -- a substitution @Γ ⊢ ρ_M : Γ_M@ from the current context @Γ = envContext (clEnv)@. , clValue :: a } deriving (Typeable, Data, Functor, Foldable) instance Show a => Show (Closure a) where show cl = "Closure { clValue = " ++ show (clValue cl) ++ " }" instance HasRange a => HasRange (Closure a) where getRange = getRange . clValue buildClosure :: a -> TCM (Closure a) buildClosure x = do env <- ask sig <- use stSignature scope <- use stScope pars <- use stModuleParameters return $ Closure sig env scope pars x --------------------------------------------------------------------------- -- ** Constraints --------------------------------------------------------------------------- type Constraints = [ProblemConstraint] data ProblemConstraint = PConstr { constraintProblems :: Set ProblemId , theConstraint :: Closure Constraint } deriving (Typeable, Data, Show) instance HasRange ProblemConstraint where getRange = getRange . theConstraint data Constraint = ValueCmp Comparison Type Term Term | ElimCmp [Polarity] Type Term [Elim] [Elim] | TypeCmp Comparison Type Type | TelCmp Type Type Comparison Telescope Telescope -- ^ the two types are for the error message only | SortCmp Comparison Sort Sort | LevelCmp Comparison Level Level -- | ShortCut MetaId Term Type -- -- ^ A delayed instantiation. Replaces @ValueCmp@ in 'postponeTypeCheckingProblem'. | UnBlock MetaId | Guarded Constraint ProblemId | IsEmpty Range Type -- ^ The range is the one of the absurd pattern. | CheckSizeLtSat Term -- ^ Check that the 'Term' is either not a SIZELT or a non-empty SIZELT. | FindInScope MetaId (Maybe MetaId) (Maybe [Candidate]) -- ^ the first argument is the instance argument, the second one is the meta -- on which the constraint may be blocked on and the third one is the list -- of candidates (or Nothing if we haven’t determined the list of -- candidates yet) deriving (Typeable, Data, Show) instance HasRange Constraint where getRange (IsEmpty r t) = r getRange _ = noRange {- no Range instances for Term, Type, Elm, Tele, Sort, Level, MetaId getRange (ValueCmp cmp a u v) = getRange (a,u,v) getRange (ElimCmp pol a v es es') = getRange (a,v,es,es') getRange (TypeCmp cmp a b) = getRange (a,b) getRange (TelCmp a b cmp tel tel') = getRange (a,b,tel,tel') getRange (SortCmp cmp s s') = getRange (s,s') getRange (LevelCmp cmp l l') = getRange (l,l') getRange (UnBlock x) = getRange x getRange (Guarded c pid) = getRange c getRange (FindInScope x cands) = getRange x -} instance Free Constraint where freeVars' c = case c of ValueCmp _ t u v -> freeVars' (t, (u, v)) ElimCmp _ t u es es' -> freeVars' ((t, u), (es, es')) TypeCmp _ t t' -> freeVars' (t, t') TelCmp _ _ _ tel tel' -> freeVars' (tel, tel') SortCmp _ s s' -> freeVars' (s, s') LevelCmp _ l l' -> freeVars' (l, l') UnBlock _ -> mempty Guarded c _ -> freeVars' c IsEmpty _ t -> freeVars' t CheckSizeLtSat u -> freeVars' u FindInScope _ _ cs -> freeVars' cs instance TermLike Constraint where foldTerm f = \case ValueCmp _ t u v -> foldTerm f (t, u, v) ElimCmp _ t u es es' -> foldTerm f (t, u, es, es') TypeCmp _ t t' -> foldTerm f (t, t') LevelCmp _ l l' -> foldTerm f (l, l') IsEmpty _ t -> foldTerm f t CheckSizeLtSat u -> foldTerm f u TelCmp _ _ _ tel1 tel2 -> __IMPOSSIBLE__ -- foldTerm f (tel1, tel2) -- Not yet implemented SortCmp _ s1 s2 -> __IMPOSSIBLE__ -- foldTerm f (s1, s2) -- Not yet implemented UnBlock _ -> __IMPOSSIBLE__ -- mempty -- Not yet implemented Guarded c _ -> __IMPOSSIBLE__ -- foldTerm c -- Not yet implemented FindInScope _ _ cs -> __IMPOSSIBLE__ -- Not yet implemented traverseTermM f c = __IMPOSSIBLE__ -- Not yet implemented data Comparison = CmpEq | CmpLeq deriving (Eq, Typeable, Data, Show) instance Pretty Comparison where pretty CmpEq = text "=" pretty CmpLeq = text "=<" -- | An extension of 'Comparison' to @>=@. data CompareDirection = DirEq | DirLeq | DirGeq deriving (Eq, Typeable, Show) instance Pretty CompareDirection where pretty = text . \case DirEq -> "=" DirLeq -> "=<" DirGeq -> ">=" -- | Embed 'Comparison' into 'CompareDirection'. fromCmp :: Comparison -> CompareDirection fromCmp CmpEq = DirEq fromCmp CmpLeq = DirLeq -- | Flip the direction of comparison. flipCmp :: CompareDirection -> CompareDirection flipCmp DirEq = DirEq flipCmp DirLeq = DirGeq flipCmp DirGeq = DirLeq -- | Turn a 'Comparison' function into a 'CompareDirection' function. -- -- Property: @dirToCmp f (fromCmp cmp) = f cmp@ dirToCmp :: (Comparison -> a -> a -> c) -> CompareDirection -> a -> a -> c dirToCmp cont DirEq = cont CmpEq dirToCmp cont DirLeq = cont CmpLeq dirToCmp cont DirGeq = flip $ cont CmpLeq --------------------------------------------------------------------------- -- * Open things --------------------------------------------------------------------------- -- | A thing tagged with the context it came from. data Open a = OpenThing { openThingCtxIds :: [CtxId], openThing :: a } deriving (Typeable, Data, Show, Functor, Foldable, Traversable) instance Decoration Open where traverseF f (OpenThing cxt x) = OpenThing cxt <$> f x data Local a = Local ModuleName a -- ^ Local to a given module, the value -- should have module parameters as free variables. | Global a -- ^ Global value, should be closed. deriving (Typeable, Data, Show, Functor, Foldable, Traversable) isGlobal :: Local a -> Bool isGlobal Global{} = True isGlobal Local{} = False instance Decoration Local where traverseF f (Local m x) = Local m <$> f x traverseF f (Global x) = Global <$> f x --------------------------------------------------------------------------- -- * Judgements -- -- Used exclusively for typing of meta variables. --------------------------------------------------------------------------- -- | Parametrized since it is used without MetaId when creating a new meta. data Judgement a = HasType { jMetaId :: a, jMetaType :: Type } | IsSort { jMetaId :: a, jMetaType :: Type } -- Andreas, 2011-04-26: type needed for higher-order sort metas deriving (Typeable) instance Show a => Show (Judgement a) where show (HasType a t) = show a ++ " : " ++ show t show (IsSort a t) = show a ++ " :sort " ++ show t --------------------------------------------------------------------------- -- ** Meta variables --------------------------------------------------------------------------- data MetaVariable = MetaVar { mvInfo :: MetaInfo , mvPriority :: MetaPriority -- ^ some metavariables are more eager to be instantiated , mvPermutation :: Permutation -- ^ a metavariable doesn't have to depend on all variables -- in the context, this "permutation" will throw away the -- ones it does not depend on , mvJudgement :: Judgement MetaId , mvInstantiation :: MetaInstantiation , mvListeners :: Set Listener -- ^ meta variables scheduled for eta-expansion but blocked by this one , mvFrozen :: Frozen -- ^ are we past the point where we can instantiate this meta variable? } deriving (Typeable) data Listener = EtaExpand MetaId | CheckConstraint Nat ProblemConstraint deriving (Typeable) instance Eq Listener where EtaExpand x == EtaExpand y = x == y CheckConstraint x _ == CheckConstraint y _ = x == y _ == _ = False instance Ord Listener where EtaExpand x `compare` EtaExpand y = x `compare` y CheckConstraint x _ `compare` CheckConstraint y _ = x `compare` y EtaExpand{} `compare` CheckConstraint{} = LT CheckConstraint{} `compare` EtaExpand{} = GT -- | Frozen meta variable cannot be instantiated by unification. -- This serves to prevent the completion of a definition by its use -- outside of the current block. -- (See issues 118, 288, 399). data Frozen = Frozen -- ^ Do not instantiate. | Instantiable deriving (Eq, Show) data MetaInstantiation = InstV [Arg String] Term -- ^ solved by term (abstracted over some free variables) | Open -- ^ unsolved | OpenIFS -- ^ open, to be instantiated as "implicit from scope" | BlockedConst Term -- ^ solution blocked by unsolved constraints | PostponedTypeCheckingProblem (Closure TypeCheckingProblem) (TCM Bool) deriving (Typeable) data TypeCheckingProblem = CheckExpr A.Expr Type | CheckArgs ExpandHidden Range [NamedArg A.Expr] Type Type (Args -> Type -> TCM Term) | CheckLambda (Arg ([WithHiding Name], Maybe Type)) A.Expr Type -- ^ @(λ (xs : t₀) → e) : t@ -- This is not an instance of 'CheckExpr' as the domain type -- has already been checked. -- For example, when checking -- @(λ (x y : Fin _) → e) : (x : Fin n) → ?@ -- we want to postpone @(λ (y : Fin n) → e) : ?@ where @Fin n@ -- is a 'Type' rather than an 'A.Expr'. | UnquoteTactic Term Term Type -- ^ First argument is computation and the others are hole and goal type deriving (Typeable) instance Show MetaInstantiation where show (InstV tel t) = "InstV " ++ show tel ++ " (" ++ show t ++ ")" show Open = "Open" show OpenIFS = "OpenIFS" show (BlockedConst t) = "BlockedConst (" ++ show t ++ ")" show (PostponedTypeCheckingProblem{}) = "PostponedTypeCheckingProblem (...)" -- | Meta variable priority: -- When we have an equation between meta-variables, which one -- should be instantiated? -- -- Higher value means higher priority to be instantiated. newtype MetaPriority = MetaPriority Int deriving ( Eq , Ord , Show #if __GLASGOW_HASKELL__ <= 708 , Typeable #endif ) data RunMetaOccursCheck = RunMetaOccursCheck | DontRunMetaOccursCheck deriving (Eq , Ord , Show #if __GLASGOW_HASKELL__ <= 708 , Typeable #endif ) -- | @MetaInfo@ is cloned from one meta to the next during pruning. data MetaInfo = MetaInfo { miClosRange :: Closure Range -- TODO: Not so nice. But we want both to have the environment of the meta (Closure) and its range. -- , miRelevance :: Relevance -- ^ Created in irrelevant position? , miMetaOccursCheck :: RunMetaOccursCheck -- ^ Run the extended occurs check that goes in definitions? , miNameSuggestion :: MetaNameSuggestion -- ^ Used for printing. -- @Just x@ if meta-variable comes from omitted argument with name @x@. } #if __GLASGOW_HASKELL__ <= 708 deriving Typeable #endif -- | Name suggestion for meta variable. Empty string means no suggestion. type MetaNameSuggestion = String -- | For printing, we couple a meta with its name suggestion. data NamedMeta = NamedMeta { nmSuggestion :: MetaNameSuggestion , nmid :: MetaId } instance Pretty NamedMeta where pretty (NamedMeta "" x) = pretty x pretty (NamedMeta "_" x) = pretty x pretty (NamedMeta s x) = text $ "_" ++ s ++ prettyShow x type MetaStore = Map MetaId MetaVariable instance HasRange MetaInfo where getRange = clValue . miClosRange instance HasRange MetaVariable where getRange m = getRange $ getMetaInfo m instance SetRange MetaInfo where setRange r m = m { miClosRange = (miClosRange m) { clValue = r }} instance SetRange MetaVariable where setRange r m = m { mvInfo = setRange r (mvInfo m) } normalMetaPriority :: MetaPriority normalMetaPriority = MetaPriority 0 lowMetaPriority :: MetaPriority lowMetaPriority = MetaPriority (-10) highMetaPriority :: MetaPriority highMetaPriority = MetaPriority 10 getMetaInfo :: MetaVariable -> Closure Range getMetaInfo = miClosRange . mvInfo getMetaScope :: MetaVariable -> ScopeInfo getMetaScope m = clScope $ getMetaInfo m getMetaEnv :: MetaVariable -> TCEnv getMetaEnv m = clEnv $ getMetaInfo m getMetaSig :: MetaVariable -> Signature getMetaSig m = clSignature $ getMetaInfo m getMetaRelevance :: MetaVariable -> Relevance getMetaRelevance = envRelevance . getMetaEnv --------------------------------------------------------------------------- -- ** Interaction meta variables --------------------------------------------------------------------------- -- | Interaction points are created by the scope checker who sets the range. -- The meta variable is created by the type checker and then hooked up to the -- interaction point. data InteractionPoint = InteractionPoint { ipRange :: Range -- ^ The position of the interaction point. , ipMeta :: Maybe MetaId -- ^ The meta variable, if any, holding the type etc. , ipSolved:: Bool -- ^ Has this interaction point already been solved? , ipClause:: IPClause -- ^ The clause of the interaction point (if any). -- Used for case splitting. } #if __GLASGOW_HASKELL__ <= 708 deriving Typeable #endif instance Eq InteractionPoint where (==) = (==) `on` ipMeta -- | Data structure managing the interaction points. -- -- We never remove interaction points from this map, only set their -- 'ipSolved' to @True@. (Issue #2368) type InteractionPoints = Map InteractionId InteractionPoint -- | Which clause is an interaction point located in? data IPClause = IPClause { ipcQName :: QName -- ^ The name of the function. , ipcClauseNo :: Int -- ^ The number of the clause of this function. , ipcClause :: A.RHS -- ^ The original AST clause rhs. } | IPNoClause -- ^ The interaction point is not in the rhs of a clause. deriving (Typeable, Data) instance Eq IPClause where IPNoClause == IPNoClause = True IPClause x i _ == IPClause x' i' _ = x == x' && i == i' _ == _ = False --------------------------------------------------------------------------- -- ** Signature --------------------------------------------------------------------------- data Signature = Sig { _sigSections :: Sections , _sigDefinitions :: Definitions , _sigRewriteRules:: RewriteRuleMap -- ^ The rewrite rules defined in this file. } deriving (Typeable, Data, Show) sigSections :: Lens' Sections Signature sigSections f s = f (_sigSections s) <&> \x -> s {_sigSections = x} sigDefinitions :: Lens' Definitions Signature sigDefinitions f s = f (_sigDefinitions s) <&> \x -> s {_sigDefinitions = x} sigRewriteRules :: Lens' RewriteRuleMap Signature sigRewriteRules f s = f (_sigRewriteRules s) <&> \x -> s {_sigRewriteRules = x} type Sections = Map ModuleName Section type Definitions = HashMap QName Definition type RewriteRuleMap = HashMap QName RewriteRules type DisplayForms = HashMap QName [LocalDisplayForm] newtype Section = Section { _secTelescope :: Telescope } deriving (Typeable, Data, Show) instance Pretty Section where pretty = pretty . _secTelescope secTelescope :: Lens' Telescope Section secTelescope f s = f (_secTelescope s) <&> \x -> s {_secTelescope = x} emptySignature :: Signature emptySignature = Sig Map.empty HMap.empty HMap.empty -- | A @DisplayForm@ is in essence a rewrite rule -- @ -- q ts --> dt -- @ -- for a defined symbol (could be a constructor as well) @q@. -- The right hand side is a 'DisplayTerm' which is used to -- 'reify' to a more readable 'Abstract.Syntax'. -- -- The patterns @ts@ are just terms, but @var 0@ is interpreted -- as a hole. Each occurrence of @var 0@ is a new hole (pattern var). -- For each *occurrence* of @var0@ the rhs @dt@ has a free variable. -- These are instantiated when matching a display form against a -- term @q vs@ succeeds. data DisplayForm = Display { dfFreeVars :: Nat -- ^ Number @n@ of free variables in 'dfRHS'. , dfPats :: Elims -- ^ Left hand side patterns, where @var 0@ stands for a pattern -- variable. There should be @n@ occurrences of @var0@ in -- 'dfPats'. -- The 'ArgInfo' is ignored in these patterns. , dfRHS :: DisplayTerm -- ^ Right hand side, with @n@ free variables. } deriving (Typeable, Data, Show) type LocalDisplayForm = Local DisplayForm -- | A structured presentation of a 'Term' for reification into -- 'Abstract.Syntax'. data DisplayTerm = DWithApp DisplayTerm [DisplayTerm] Elims -- ^ @(f vs | ws) es@. -- The first 'DisplayTerm' is the parent function @f@ with its args @vs@. -- The list of 'DisplayTerm's are the with expressions @ws@. -- The 'Elims' are additional arguments @es@ -- (possible in case the with-application is of function type) -- or projections (if it is of record type). | DCon ConHead ConInfo [Arg DisplayTerm] -- ^ @c vs@. | DDef QName [Elim' DisplayTerm] -- ^ @d vs@. | DDot Term -- ^ @.v@. | DTerm Term -- ^ @v@. deriving (Typeable, Data, Show) instance Free DisplayForm where freeVars' (Display n ps t) = bind (freeVars' ps) `mappend` bind' n (freeVars' t) instance Free DisplayTerm where freeVars' (DWithApp t ws es) = freeVars' (t, (ws, es)) freeVars' (DCon _ _ vs) = freeVars' vs freeVars' (DDef _ es) = freeVars' es freeVars' (DDot v) = freeVars' v freeVars' (DTerm v) = freeVars' v instance Pretty DisplayTerm where prettyPrec p v = case v of DTerm v -> prettyPrec p v DDot v -> text "." P.<> prettyPrec 10 v DDef f es -> pretty f `pApp` es DCon c _ vs -> pretty (conName c) `pApp` map Apply vs DWithApp h ws es -> mparens (p > 0) (sep [ pretty h , nest 2 $ fsep [ text "|" <+> pretty w | w <- ws ] ]) `pApp` es where pApp d els = mparens (not (null els) && p > 9) $ sep [d, nest 2 $ fsep (map (prettyPrec 10) els)] -- | By default, we have no display form. defaultDisplayForm :: QName -> [LocalDisplayForm] defaultDisplayForm c = [] defRelevance :: Definition -> Relevance defRelevance = argInfoRelevance . defArgInfo -- | Non-linear (non-constructor) first-order pattern. data NLPat = PVar !Int [Arg Int] -- ^ Matches anything (modulo non-linearity) that only contains bound -- variables that occur in the given arguments. | PWild -- ^ Matches anything (e.g. irrelevant terms). | PDef QName PElims -- ^ Matches @f es@ | PLam ArgInfo (Abs NLPat) -- ^ Matches @λ x → t@ | PPi (Dom NLPType) (Abs NLPType) -- ^ Matches @(x : A) → B@ | PBoundVar {-# UNPACK #-} !Int PElims -- ^ Matches @x es@ where x is a lambda-bound variable | PTerm Term -- ^ Matches the term modulo β (ideally βη). deriving (Typeable, Data, Show) type PElims = [Elim' NLPat] data NLPType = NLPType { nlpTypeLevel :: NLPat -- always PWild or PVar (with all bound variables in scope) , nlpTypeUnEl :: NLPat } deriving (Typeable, Data, Show) type RewriteRules = [RewriteRule] -- | Rewrite rules can be added independently from function clauses. data RewriteRule = RewriteRule { rewName :: QName -- ^ Name of rewrite rule @q : Γ → f ps ≡ rhs@ -- where @≡@ is the rewrite relation. , rewContext :: Telescope -- ^ @Γ@. , rewHead :: QName -- ^ @f@. , rewPats :: PElims -- ^ @Γ ⊢ f ps : t@. , rewRHS :: Term -- ^ @Γ ⊢ rhs : t@. , rewType :: Type -- ^ @Γ ⊢ t@. } deriving (Typeable, Data, Show) data Definition = Defn { defArgInfo :: ArgInfo -- ^ Hiding should not be used. , defName :: QName , defType :: Type -- ^ Type of the lifted definition. , defPolarity :: [Polarity] -- ^ Variance information on arguments of the definition. -- Does not include info for dropped parameters to -- projection(-like) functions and constructors. , defArgOccurrences :: [Occurrence] -- ^ Positivity information on arguments of the definition. -- Does not include info for dropped parameters to -- projection(-like) functions and constructors. -- Sometimes Agda looks up 'Occurrence's in these lists based on -- their position, so one might consider replacing the list -- with, say, an 'IntMap'. However, presumably these lists tend -- to be short, in which case 'IntMap's could be slower than -- lists. For instance, at one point the longest list -- encountered for the standard library (in serialised -- interfaces) had length 27. Distribution: -- -- Length, number of lists -- ----------------------- -- -- 0, 2444 -- 1, 721 -- 2, 433 -- 3, 668 -- 4, 602 -- 5, 624 -- 6, 626 -- 7, 484 -- 8, 375 -- 9, 264 -- 10, 305 -- 11, 188 -- 12, 171 -- 13, 108 -- 14, 84 -- 15, 80 -- 16, 38 -- 17, 23 -- 18, 16 -- 19, 8 -- 20, 7 -- 21, 5 -- 22, 2 -- 23, 3 -- 27, 1 , defDisplay :: [LocalDisplayForm] , defMutual :: MutualId , defCompiledRep :: CompiledRepresentation , defInstance :: Maybe QName -- ^ @Just q@ when this definition is an instance of class q , defCopy :: Bool -- ^ Has this function been created by a module -- instantiation? , defMatchable :: Bool -- ^ Is the def matched against in a rewrite rule? , defInjective :: Bool -- ^ Should the def be treated as injective by the pattern matching unifier? , theDef :: Defn } deriving (Typeable, Data, Show) theDefLens :: Lens' Defn Definition theDefLens f d = f (theDef d) <&> \ df -> d { theDef = df } -- | Create a definition with sensible defaults. defaultDefn :: ArgInfo -> QName -> Type -> Defn -> Definition defaultDefn info x t def = Defn { defArgInfo = info , defName = x , defType = t , defPolarity = [] , defArgOccurrences = [] , defDisplay = defaultDisplayForm x , defMutual = 0 , defCompiledRep = noCompiledRep , defInstance = Nothing , defCopy = False , defMatchable = False , defInjective = False , theDef = def } -- | Polarity for equality and subtype checking. data Polarity = Covariant -- ^ monotone | Contravariant -- ^ antitone | Invariant -- ^ no information (mixed variance) | Nonvariant -- ^ constant deriving (Typeable, Data, Show, Eq) instance Pretty Polarity where pretty = text . \case Covariant -> "+" Contravariant -> "-" Invariant -> "*" Nonvariant -> "_" -- | The backends are responsible for parsing their own pragmas. data CompilerPragma = CompilerPragma Range String deriving (Typeable, Data, Show, Eq) instance HasRange CompilerPragma where getRange (CompilerPragma r _) = r type BackendName = String -- Temporary: while we still parse the old pragmas we need to know the names of -- the corresponding backends. jsBackendName, ghcBackendName, uhcBackendName :: BackendName jsBackendName = "JS" ghcBackendName = "GHC" uhcBackendName = "UHC" type CompiledRepresentation = Map BackendName [CompilerPragma] noCompiledRep :: CompiledRepresentation noCompiledRep = Map.empty -- | Additional information for extended lambdas. data ExtLamInfo = ExtLamInfo { extLamNumHidden :: Int -- Number of hidden args to be dropped when printing. , extLamNumNonHid :: Int -- Number of visible args to be dropped when printing. } deriving (Typeable, Data, Eq, Ord, Show) -- | Additional information for projection 'Function's. data Projection = Projection { projProper :: Maybe QName -- ^ @Nothing@ if only projection-like, @Just r@ if record projection. -- The @r@ is the name of the record type projected from. -- This field is updated by module application. , projOrig :: QName -- ^ The original projection name -- (current name could be from module application). , projFromType :: Arg QName -- ^ Type projected from. Original record type if @projProper = Just{}@. -- Also stores @ArgInfo@ of the principal argument. -- This field is unchanged by module application. , projIndex :: Int -- ^ Index of the record argument. -- Start counting with 1, because 0 means that -- it is already applied to the record value. -- This can happen in module instantiation, but -- then either the record value is @var 0@, or @funProjection == Nothing@. , projLams :: ProjLams -- ^ Term @t@ to be be applied to record parameters and record value. -- The parameters will be dropped. -- In case of a proper projection, a postfix projection application -- will be created: @t = \ pars r -> r .p@ -- (Invariant: the number of abstractions equals 'projIndex'.) -- In case of a projection-like function, just the function symbol -- is returned as 'Def': @t = \ pars -> f@. } deriving (Typeable, Data, Show) -- | Abstractions to build projection function (dropping parameters). newtype ProjLams = ProjLams { getProjLams :: [Arg ArgName] } deriving (Typeable, Data, Show, Null) -- | Building the projection function (which drops the parameters). projDropPars :: Projection -> ProjOrigin -> Term -- Proper projections: projDropPars (Projection Just{} d _ _ lams) o = case initLast $ getProjLams lams of Nothing -> Def d [] Just (pars, Arg i y) -> let core = Lam i $ Abs y $ Var 0 [Proj o d] in List.foldr (\ (Arg ai x) -> Lam ai . NoAbs x) core pars -- Projection-like functions: projDropPars (Projection Nothing _ _ _ lams) o | null lams = __IMPOSSIBLE__ projDropPars (Projection Nothing d _ _ lams) o = List.foldr (\ (Arg ai x) -> Lam ai . NoAbs x) (Def d []) $ init $ getProjLams lams -- | The info of the principal (record) argument. projArgInfo :: Projection -> ArgInfo projArgInfo (Projection _ _ _ _ lams) = maybe __IMPOSSIBLE__ getArgInfo $ lastMaybe $ getProjLams lams -- | Should a record type admit eta-equality? data EtaEquality = Specified !Bool -- ^ User specifed 'eta-equality' or 'no-eta-equality'. | Inferred !Bool -- ^ Positivity checker inferred whether eta is safe. deriving (Typeable, Data, Show, Eq) etaEqualityToBool :: EtaEquality -> Bool etaEqualityToBool (Specified b) = b etaEqualityToBool (Inferred b) = b -- | Make sure we do not overwrite a user specification. setEtaEquality :: EtaEquality -> Bool -> EtaEquality setEtaEquality e@Specified{} _ = e setEtaEquality _ b = Inferred b data FunctionFlag = FunStatic -- ^ Should calls to this function be normalised at compile-time? | FunInline -- ^ Should calls to this function be inlined by the compiler? | FunMacro -- ^ Is this function a macro? deriving (Typeable, Data, Eq, Ord, Enum, Show) data Defn = Axiom -- ^ Postulate. | AbstractDefn Defn -- ^ Returned by 'getConstInfo' if definition is abstract. | Function { funClauses :: [Clause] , funCompiled :: Maybe CompiledClauses -- ^ 'Nothing' while function is still type-checked. -- @Just cc@ after type and coverage checking and -- translation to case trees. , funTreeless :: Maybe Compiled -- ^ Intermediate representation for compiler backends. , funInv :: FunctionInverse , funMutual :: Maybe [QName] -- ^ Mutually recursive functions, @data@s and @record@s. -- Does include this function. -- Empty list if not recursive. -- @Nothing@ if not yet computed (by positivity checker). , funAbstr :: IsAbstract , funDelayed :: Delayed -- ^ Are the clauses of this definition delayed? , funProjection :: Maybe Projection -- ^ Is it a record projection? -- If yes, then return the name of the record type and index of -- the record argument. Start counting with 1, because 0 means that -- it is already applied to the record. (Can happen in module -- instantiation.) This information is used in the termination -- checker. , funFlags :: Set FunctionFlag , funTerminates :: Maybe Bool -- ^ Has this function been termination checked? Did it pass? , funExtLam :: Maybe ExtLamInfo -- ^ Is this function generated from an extended lambda? -- If yes, then return the number of hidden and non-hidden lambda-lifted arguments , funWith :: Maybe QName -- ^ Is this a generated with-function? If yes, then what's the -- name of the parent function. , funCopatternLHS :: Bool -- ^ Is this a function defined by copatterns? } | Datatype { dataPars :: Nat -- ^ Number of parameters. , dataSmallPars :: Permutation -- ^ Parameters that are maybe small. , dataNonLinPars :: Drop Permutation -- ^ Parameters that appear in indices. , dataIxs :: Nat -- ^ Number of indices. , dataInduction :: Induction -- ^ @data@ or @codata@ (legacy). , dataClause :: (Maybe Clause) -- ^ This might be in an instantiated module. , dataCons :: [QName] -- ^ Constructor names. , dataSort :: Sort , dataMutual :: Maybe [QName] -- ^ Mutually recursive functions, @data@s and @record@s. -- Does include this data type. -- Empty if not recursive. -- @Nothing@ if not yet computed (by positivity checker). , dataAbstr :: IsAbstract } | Record { recPars :: Nat -- ^ Number of parameters. , recClause :: Maybe Clause -- ^ Was this record type created by a module application? -- If yes, the clause is its definition (linking back to the original record type). , recConHead :: ConHead -- ^ Constructor name and fields. , recNamedCon :: Bool -- ^ Does this record have a @constructor@? , recFields :: [Arg QName] -- ^ The record field names. , recTel :: Telescope -- ^ The record field telescope. (Includes record parameters.) -- Note: @TelV recTel _ == telView' recConType@. -- Thus, @recTel@ is redundant. , recMutual :: Maybe [QName] -- ^ Mutually recursive functions, @data@s and @record@s. -- Does include this record. -- Empty if not recursive. -- @Nothing@ if not yet computed (by positivity checker). , recEtaEquality' :: EtaEquality -- ^ Eta-expand at this record type? -- @False@ for unguarded recursive records and coinductive records -- unless the user specifies otherwise. , recInduction :: Maybe Induction -- ^ 'Inductive' or 'CoInductive'? Matters only for recursive records. -- 'Nothing' means that the user did not specify it, which is an error -- for recursive records. , recAbstr :: IsAbstract } | Constructor { conPars :: Int -- ^ Number of parameters. , conArity :: Int -- ^ Number of arguments (excluding parameters). , conSrcCon :: ConHead -- ^ Name of (original) constructor and fields. (This might be in a module instance.) , conData :: QName -- ^ Name of datatype or record type. , conAbstr :: IsAbstract , conInd :: Induction -- ^ Inductive or coinductive? , conErased :: [Bool] -- ^ Which arguments are erased at runtime (computed during compilation to treeless) } | Primitive { primAbstr :: IsAbstract , primName :: String , primClauses :: [Clause] -- ^ 'null' for primitive functions, @not null@ for builtin functions. , primCompiled :: Maybe CompiledClauses -- ^ 'Nothing' for primitive functions, -- @'Just' something@ for builtin functions. } -- ^ Primitive or builtin functions. deriving (Typeable, Data, Show) instance Pretty Definition where pretty Defn{..} = text "Defn {" vcat [ text "defArgInfo =" pshow defArgInfo , text "defName =" pretty defName , text "defType =" pretty defType , text "defPolarity =" pshow defPolarity , text "defArgOccurrences =" pshow defArgOccurrences , text "defDisplay =" pshow defDisplay -- TODO: pretty DisplayForm , text "defMutual =" pshow defMutual , text "defCompiledRep =" pshow defCompiledRep , text "defInstance =" pshow defInstance , text "defCopy =" pshow defCopy , text "defMatchable =" pshow defMatchable , text "defInjective =" pshow defInjective , text "theDef =" pretty theDef ] <+> text "}" instance Pretty Defn where pretty Axiom = text "Axiom" pretty (AbstractDefn def) = text "AbstractDefn" parens (pretty def) pretty Function{..} = text "Function {" vcat [ text "funClauses =" vcat (map pretty funClauses) , text "funCompiled =" pshow funCompiled , text "funTreeless =" pshow funTreeless , text "funInv =" pshow funInv , text "funMutual =" pshow funMutual , text "funAbstr =" pshow funAbstr , text "funDelayed =" pshow funDelayed , text "funProjection =" pshow funProjection , text "funFlags =" pshow funFlags , text "funTerminates =" pshow funTerminates , text "funWith =" pshow funWith , text "funCopatternLHS =" pshow funCopatternLHS ] text "}" pretty Datatype{..} = text "Datatype {" vcat [ text "dataPars =" pshow dataPars , text "dataSmallPars =" pshow dataSmallPars , text "dataNonLinPars =" pshow dataNonLinPars , text "dataIxs =" pshow dataIxs , text "dataInduction =" pshow dataInduction , text "dataClause =" pretty dataClause , text "dataCons =" pshow dataCons , text "dataSort =" pretty dataSort , text "dataMutual =" pshow dataMutual , text "dataAbstr =" pshow dataAbstr ] text "}" pretty Record{..} = text "Record {" vcat [ text "recPars =" pshow recPars , text "recClause =" pretty recClause , text "recConHead =" pshow recConHead , text "recNamedCon =" pshow recNamedCon , text "recFields =" pshow recFields , text "recTel =" pretty recTel , text "recMutual =" pshow recMutual , text "recEtaEquality' =" pshow recEtaEquality' , text "recInduction =" pshow recInduction , text "recAbstr =" pshow recAbstr ] text "}" pretty Constructor{..} = text "Constructor {" vcat [ text "conPars =" pshow conPars , text "conArity =" pshow conArity , text "conSrcCon =" pshow conSrcCon , text "conData =" pshow conData , text "conAbstr =" pshow conAbstr , text "conInd =" pshow conInd , text "conErased =" pshow conErased ] text "}" pretty Primitive{..} = text "Primitive {" vcat [ text "primAbstr =" pshow primAbstr , text "primName =" pshow primName , text "primClauses =" pshow primClauses , text "primCompiled =" pshow primCompiled ] text "}" -- | Is the record type recursive? recRecursive :: Defn -> Bool recRecursive (Record { recMutual = Just qs }) = not $ null qs recRecursive _ = __IMPOSSIBLE__ recEtaEquality :: Defn -> Bool recEtaEquality = etaEqualityToBool . recEtaEquality' -- | A template for creating 'Function' definitions, with sensible defaults. emptyFunction :: Defn emptyFunction = Function { funClauses = [] , funCompiled = Nothing , funTreeless = Nothing , funInv = NotInjective , funMutual = Nothing , funAbstr = ConcreteDef , funDelayed = NotDelayed , funProjection = Nothing , funFlags = Set.empty , funTerminates = Nothing , funExtLam = Nothing , funWith = Nothing , funCopatternLHS = False } funFlag :: FunctionFlag -> Lens' Bool Defn funFlag flag f def@Function{ funFlags = flags } = f (Set.member flag flags) <&> \ b -> def{ funFlags = (if b then Set.insert else Set.delete) flag flags } funFlag _ f def = f False <&> const def funStatic, funInline, funMacro :: Lens' Bool Defn funStatic = funFlag FunStatic funInline = funFlag FunInline funMacro = funFlag FunMacro isMacro :: Defn -> Bool isMacro = (^. funMacro) -- | Checking whether we are dealing with a function yet to be defined. isEmptyFunction :: Defn -> Bool isEmptyFunction def = case def of Function { funClauses = [] } -> True _ -> False isCopatternLHS :: [Clause] -> Bool isCopatternLHS = List.any (List.any (isJust . A.isProjP) . namedClausePats) recCon :: Defn -> QName recCon Record{ recConHead } = conName recConHead recCon _ = __IMPOSSIBLE__ defIsRecord :: Defn -> Bool defIsRecord Record{} = True defIsRecord _ = False defIsDataOrRecord :: Defn -> Bool defIsDataOrRecord Record{} = True defIsDataOrRecord Datatype{} = True defIsDataOrRecord _ = False defConstructors :: Defn -> [QName] defConstructors Datatype{dataCons = cs} = cs defConstructors Record{recConHead = c} = [conName c] defConstructors _ = __IMPOSSIBLE__ newtype Fields = Fields [(C.Name, Type)] deriving (Typeable, Null) -- | Did we encounter a simplifying reduction? -- In terms of CIC, that would be a iota-reduction. -- In terms of Agda, this is a constructor or literal -- pattern that matched. -- Just beta-reduction (substitution) or delta-reduction -- (unfolding of definitions) does not count as simplifying? data Simplification = YesSimplification | NoSimplification deriving (Typeable, Data, Eq, Show) instance Null Simplification where empty = NoSimplification null = (== NoSimplification) instance Semigroup Simplification where YesSimplification <> _ = YesSimplification NoSimplification <> s = s instance Monoid Simplification where mempty = NoSimplification mappend = (<>) data Reduced no yes = NoReduction no | YesReduction Simplification yes deriving (Typeable, Functor) -- | Three cases: 1. not reduced, 2. reduced, but blocked, 3. reduced, not blocked. data IsReduced = NotReduced | Reduced (Blocked ()) data MaybeReduced a = MaybeRed { isReduced :: IsReduced , ignoreReduced :: a } deriving (Functor) instance IsProjElim e => IsProjElim (MaybeReduced e) where isProjElim = isProjElim . ignoreReduced type MaybeReducedArgs = [MaybeReduced (Arg Term)] type MaybeReducedElims = [MaybeReduced Elim] notReduced :: a -> MaybeReduced a notReduced x = MaybeRed NotReduced x reduced :: Blocked (Arg Term) -> MaybeReduced (Arg Term) reduced b = case fmap ignoreSharing <$> b of NotBlocked _ (Arg _ (MetaV x _)) -> MaybeRed (Reduced $ Blocked x ()) v _ -> MaybeRed (Reduced $ () <$ b) v where v = ignoreBlocking b -- | Controlling 'reduce'. data AllowedReduction = ProjectionReductions -- ^ (Projection and) projection-like functions may be reduced. | InlineReductions -- ^ Functions marked INLINE may be reduced. | CopatternReductions -- ^ Copattern definitions may be reduced. | FunctionReductions -- ^ Non-recursive functions and primitives may be reduced. | RecursiveReductions -- ^ Even recursive functions may be reduced. | LevelReductions -- ^ Reduce @'Level'@ terms. | UnconfirmedReductions -- ^ Functions whose termination has not (yet) been confirmed. | NonTerminatingReductions -- ^ Functions that have failed termination checking. deriving (Show, Eq, Ord, Enum, Bounded, Typeable, Data) type AllowedReductions = [AllowedReduction] -- | Not quite all reductions (skip non-terminating reductions) allReductions :: AllowedReductions allReductions = [minBound..pred maxBound] data PrimFun = PrimFun { primFunName :: QName , primFunArity :: Arity , primFunImplementation :: [Arg Term] -> ReduceM (Reduced MaybeReducedArgs Term) } deriving (Typeable) defClauses :: Definition -> [Clause] defClauses Defn{theDef = Function{funClauses = cs}} = cs defClauses Defn{theDef = Primitive{primClauses = cs}} = cs defClauses Defn{theDef = Datatype{dataClause = Just c}} = [c] defClauses Defn{theDef = Record{recClause = Just c}} = [c] defClauses _ = [] defCompiled :: Definition -> Maybe CompiledClauses defCompiled Defn{theDef = Function {funCompiled = mcc}} = mcc defCompiled Defn{theDef = Primitive{primCompiled = mcc}} = mcc defCompiled _ = Nothing defParameters :: Definition -> Maybe Nat defParameters Defn{theDef = Datatype{dataPars = n}} = Just n defParameters Defn{theDef = Record {recPars = n}} = Just n defParameters _ = Nothing defCompilerPragmas :: BackendName -> Definition -> [CompilerPragma] defCompilerPragmas b = reverse . fromMaybe [] . Map.lookup b . defCompiledRep -- reversed because we add new pragmas to the front of the list -- | Are the clauses of this definition delayed? defDelayed :: Definition -> Delayed defDelayed Defn{theDef = Function{funDelayed = d}} = d defDelayed _ = NotDelayed -- | Has the definition failed the termination checker? defNonterminating :: Definition -> Bool defNonterminating Defn{theDef = Function{funTerminates = Just False}} = True defNonterminating _ = False -- | Has the definition not termination checked or did the check fail? defTerminationUnconfirmed :: Definition -> Bool defTerminationUnconfirmed Defn{theDef = Function{funTerminates = Just True}} = False defTerminationUnconfirmed Defn{theDef = Function{funTerminates = _ }} = True defTerminationUnconfirmed _ = False defAbstract :: Definition -> IsAbstract defAbstract d = case theDef d of Axiom{} -> ConcreteDef AbstractDefn{} -> AbstractDef Function{funAbstr = a} -> a Datatype{dataAbstr = a} -> a Record{recAbstr = a} -> a Constructor{conAbstr = a} -> a Primitive{primAbstr = a} -> a --------------------------------------------------------------------------- -- ** Injectivity --------------------------------------------------------------------------- type FunctionInverse = FunctionInverse' Clause data FunctionInverse' c = NotInjective | Inverse (Map TermHead c) deriving (Typeable, Data, Show, Functor) data TermHead = SortHead | PiHead | ConsHead QName deriving (Typeable, Data, Eq, Ord, Show) instance Pretty TermHead where pretty = \case SortHead -> text "SortHead" PiHead -> text "PiHead" ConsHead q-> text "ConsHead" <+> pretty q --------------------------------------------------------------------------- -- ** Mutual blocks --------------------------------------------------------------------------- newtype MutualId = MutId Int32 deriving (Typeable, Data, Eq, Ord, Show, Num, Enum) --------------------------------------------------------------------------- -- ** Statistics --------------------------------------------------------------------------- type Statistics = Map String Integer --------------------------------------------------------------------------- -- ** Trace --------------------------------------------------------------------------- data Call = CheckClause Type A.SpineClause | CheckPattern A.Pattern Telescope Type | CheckLetBinding A.LetBinding | InferExpr A.Expr | CheckExprCall A.Expr Type | CheckDotPattern A.Expr Term | CheckPatternShadowing A.SpineClause | CheckProjection Range QName Type | IsTypeCall A.Expr Sort | IsType_ A.Expr | InferVar Name | InferDef QName | CheckArguments Range [NamedArg A.Expr] Type Type | CheckDataDef Range Name [A.LamBinding] [A.Constructor] | CheckRecDef Range Name [A.LamBinding] [A.Constructor] | CheckConstructor QName Telescope Sort A.Constructor | CheckFunDef Range Name [A.Clause] | CheckPragma Range A.Pragma | CheckPrimitive Range Name A.Expr | CheckIsEmpty Range Type | CheckWithFunctionType A.Expr | CheckSectionApplication Range ModuleName A.ModuleApplication | ScopeCheckExpr C.Expr | ScopeCheckDeclaration NiceDeclaration | ScopeCheckLHS C.QName C.Pattern | NoHighlighting | ModuleContents -- ^ Interaction command: show module contents. | SetRange Range -- ^ used by 'setCurrentRange' deriving (Typeable, Data) instance Pretty Call where pretty CheckClause{} = text "CheckClause" pretty CheckPattern{} = text "CheckPattern" pretty InferExpr{} = text "InferExpr" pretty CheckExprCall{} = text "CheckExprCall" pretty CheckLetBinding{} = text "CheckLetBinding" pretty CheckProjection{} = text "CheckProjection" pretty IsTypeCall{} = text "IsTypeCall" pretty IsType_{} = text "IsType_" pretty InferVar{} = text "InferVar" pretty InferDef{} = text "InferDef" pretty CheckArguments{} = text "CheckArguments" pretty CheckDataDef{} = text "CheckDataDef" pretty CheckRecDef{} = text "CheckRecDef" pretty CheckConstructor{} = text "CheckConstructor" pretty CheckFunDef{} = text "CheckFunDef" pretty CheckPragma{} = text "CheckPragma" pretty CheckPrimitive{} = text "CheckPrimitive" pretty CheckWithFunctionType{} = text "CheckWithFunctionType" pretty ScopeCheckExpr{} = text "ScopeCheckExpr" pretty ScopeCheckDeclaration{} = text "ScopeCheckDeclaration" pretty ScopeCheckLHS{} = text "ScopeCheckLHS" pretty CheckDotPattern{} = text "CheckDotPattern" pretty CheckPatternShadowing{} = text "CheckPatternShadowing" pretty SetRange{} = text "SetRange" pretty CheckSectionApplication{} = text "CheckSectionApplication" pretty CheckIsEmpty{} = text "CheckIsEmpty" pretty NoHighlighting{} = text "NoHighlighting" pretty ModuleContents{} = text "ModuleContents" instance HasRange Call where getRange (CheckClause _ c) = getRange c getRange (CheckPattern p _ _) = getRange p getRange (InferExpr e) = getRange e getRange (CheckExprCall e _) = getRange e getRange (CheckLetBinding b) = getRange b getRange (CheckProjection r _ _) = r getRange (IsTypeCall e s) = getRange e getRange (IsType_ e) = getRange e getRange (InferVar x) = getRange x getRange (InferDef f) = getRange f getRange (CheckArguments r _ _ _) = r getRange (CheckDataDef i _ _ _) = getRange i getRange (CheckRecDef i _ _ _) = getRange i getRange (CheckConstructor _ _ _ c) = getRange c getRange (CheckFunDef i _ _) = getRange i getRange (CheckPragma r _) = r getRange (CheckPrimitive i _ _) = getRange i getRange CheckWithFunctionType{} = noRange getRange (ScopeCheckExpr e) = getRange e getRange (ScopeCheckDeclaration d) = getRange d getRange (ScopeCheckLHS _ p) = getRange p getRange (CheckDotPattern e _) = getRange e getRange (CheckPatternShadowing c) = getRange c getRange (SetRange r) = r getRange (CheckSectionApplication r _ _) = r getRange (CheckIsEmpty r _) = r getRange NoHighlighting = noRange getRange ModuleContents = noRange --------------------------------------------------------------------------- -- ** Instance table --------------------------------------------------------------------------- -- | The instance table is a @Map@ associating to every name of -- record/data type/postulate its list of instances type InstanceTable = Map QName (Set QName) -- | When typechecking something of the following form: -- -- instance -- x : _ -- x = y -- -- it's not yet known where to add @x@, so we add it to a list of -- unresolved instances and we'll deal with it later. type TempInstanceTable = (InstanceTable , Set QName) --------------------------------------------------------------------------- -- ** Builtin things --------------------------------------------------------------------------- data BuiltinDescriptor = BuiltinData (TCM Type) [String] | BuiltinDataCons (TCM Type) | BuiltinPrim String (Term -> TCM ()) | BuiltinPostulate Relevance (TCM Type) | BuiltinUnknown (Maybe (TCM Type)) (Term -> Type -> TCM ()) -- ^ Builtin of any kind. -- Type can be checked (@Just t@) or inferred (@Nothing@). -- The second argument is the hook for the verification function. data BuiltinInfo = BuiltinInfo { builtinName :: String , builtinDesc :: BuiltinDescriptor } type BuiltinThings pf = Map String (Builtin pf) data Builtin pf = Builtin Term | Prim pf deriving (Typeable, Show, Functor, Foldable, Traversable) --------------------------------------------------------------------------- -- * Highlighting levels --------------------------------------------------------------------------- -- | How much highlighting should be sent to the user interface? data HighlightingLevel = None | NonInteractive | Interactive -- ^ This includes both non-interactive highlighting and -- interactive highlighting of the expression that is currently -- being type-checked. deriving (Eq, Ord, Show, Read, Typeable, Data) -- | How should highlighting be sent to the user interface? data HighlightingMethod = Direct -- ^ Via stdout. | Indirect -- ^ Both via files and via stdout. deriving (Eq, Show, Read, Typeable, Data) -- | @ifTopLevelAndHighlightingLevelIs l b m@ runs @m@ when we're -- type-checking the top-level module and either the highlighting -- level is /at least/ @l@ or @b@ is 'True'. ifTopLevelAndHighlightingLevelIsOr :: MonadTCM tcm => HighlightingLevel -> Bool -> tcm () -> tcm () ifTopLevelAndHighlightingLevelIsOr l b m = do e <- ask when (envModuleNestingLevel e == 0 && (envHighlightingLevel e >= l || b)) m -- | @ifTopLevelAndHighlightingLevelIs l m@ runs @m@ when we're -- type-checking the top-level module and the highlighting level is -- /at least/ @l@. ifTopLevelAndHighlightingLevelIs :: MonadTCM tcm => HighlightingLevel -> tcm () -> tcm () ifTopLevelAndHighlightingLevelIs l = ifTopLevelAndHighlightingLevelIsOr l False --------------------------------------------------------------------------- -- * Type checking environment --------------------------------------------------------------------------- data ModuleParameters = ModuleParams { mpSubstitution :: Substitution -- ^ @Δ ⊢ σ : Γ@ for a @module M Γ@ where @Δ@ is the current context @envContext@. } deriving (Typeable, Data, Show) defaultModuleParameters :: ModuleParameters defaultModuleParameters = ModuleParams IdS type ModuleParamDict = Map ModuleName ModuleParameters -- ^ The map contains for each 'ModuleName' @M@ with module telescope @Γ_M@ -- a substitution @Γ ⊢ ρ_M : Γ_M@ from the current context @Γ = envContext (clEnv)@. data TCEnv = TCEnv { envContext :: Context , envLetBindings :: LetBindings , envCurrentModule :: ModuleName , envCurrentPath :: Maybe AbsolutePath -- ^ The path to the file that is currently being -- type-checked. 'Nothing' if we do not have a file -- (like in interactive mode see @CommandLine@). , envAnonymousModules :: [(ModuleName, Nat)] -- ^ anonymous modules and their number of free variables , envImportPath :: [C.TopLevelModuleName] -- ^ to detect import cycles , envMutualBlock :: Maybe MutualId -- ^ the current (if any) mutual block , envTerminationCheck :: TerminationCheck () -- ^ are we inside the scope of a termination pragma , envSolvingConstraints :: Bool -- ^ Are we currently in the process of solving active constraints? , envCheckingWhere :: Bool -- ^ Have we stepped into the where-declarations of a clause? -- Everything under a @where@ will be checked with this flag on. , envAssignMetas :: Bool -- ^ Are we allowed to assign metas? , envActiveProblems :: Set ProblemId , envAbstractMode :: AbstractMode -- ^ When checking the typesignature of a public definition -- or the body of a non-abstract definition this is true. -- To prevent information about abstract things leaking -- outside the module. , envRelevance :: Relevance -- ^ Are we checking an irrelevant argument? (=@Irrelevant@) -- Then top-level irrelevant declarations are enabled. -- Other value: @Relevant@, then only relevant decls. are avail. , envDisplayFormsEnabled :: Bool -- ^ Sometimes we want to disable display forms. , envRange :: Range , envHighlightingRange :: Range -- ^ Interactive highlighting uses this range rather -- than 'envRange'. , envClause :: IPClause -- ^ What is the current clause we are type-checking? -- Will be recorded in interaction points in this clause. , envCall :: Maybe (Closure Call) -- ^ what we're doing at the moment , envHighlightingLevel :: HighlightingLevel -- ^ Set to 'None' when imported modules are -- type-checked. , envHighlightingMethod :: HighlightingMethod , envModuleNestingLevel :: !Int -- ^ This number indicates how far away from the -- top-level module Agda has come when chasing -- modules. The level of a given module is not -- necessarily the same as the length, in the module -- dependency graph, of the shortest path from the -- top-level module; it depends on in which order -- Agda chooses to chase dependencies. , envAllowDestructiveUpdate :: Bool -- ^ When True, allows destructively shared updating terms -- during evaluation or unification. This is disabled when -- doing speculative checking, like solve instance metas, or -- when updating might break abstraction, as is the case when -- checking abstract definitions. , envExpandLast :: ExpandHidden -- ^ When type-checking an alias f=e, we do not want -- to insert hidden arguments in the end, because -- these will become unsolved metas. , envAppDef :: Maybe QName -- ^ We are reducing an application of this function. -- (For debugging of incomplete matches only.) , envSimplification :: Simplification -- ^ Did we encounter a simplification (proper match) -- during the current reduction process? , envAllowedReductions :: AllowedReductions , envCompareBlocked :: Bool -- ^ Can we compare blocked things during conversion? -- No by default. -- Yes for rewriting feature. , envPrintDomainFreePi :: Bool -- ^ When @True@, types will be omitted from printed pi types if they -- can be inferred. , envPrintMetasBare :: Bool -- ^ When @True@, throw away meta numbers and meta elims. -- This is used for reifying terms for feeding into the -- user's source code, e.g., for the interaction tactics @solveAll@. , envInsideDotPattern :: Bool -- ^ Used by the scope checker to make sure that certain forms -- of expressions are not used inside dot patterns: extended -- lambdas and let-expressions. , envUnquoteFlags :: UnquoteFlags , envInstanceDepth :: !Int -- ^ Until we get a termination checker for instance search (#1743) we -- limit the search depth to ensure termination. } deriving (Typeable, Data) initEnv :: TCEnv initEnv = TCEnv { envContext = [] , envLetBindings = Map.empty , envCurrentModule = noModuleName , envCurrentPath = Nothing , envAnonymousModules = [] , envImportPath = [] , envMutualBlock = Nothing , envTerminationCheck = TerminationCheck , envSolvingConstraints = False , envCheckingWhere = False , envActiveProblems = Set.empty , envAssignMetas = True , envAbstractMode = ConcreteMode -- Andreas, 2013-02-21: This was 'AbstractMode' until now. -- However, top-level checks for mutual blocks, such as -- constructor-headedness, should not be able to look into -- abstract definitions unless abstract themselves. -- (See also discussion on issue 796.) -- The initial mode should be 'ConcreteMode', ensuring you -- can only look into abstract things in an abstract -- definition (which sets 'AbstractMode'). , envRelevance = Relevant , envDisplayFormsEnabled = True , envRange = noRange , envHighlightingRange = noRange , envClause = IPNoClause , envCall = Nothing , envHighlightingLevel = None , envHighlightingMethod = Indirect , envModuleNestingLevel = -1 , envAllowDestructiveUpdate = True , envExpandLast = ExpandLast , envAppDef = Nothing , envSimplification = NoSimplification , envAllowedReductions = allReductions , envCompareBlocked = False , envPrintDomainFreePi = False , envPrintMetasBare = False , envInsideDotPattern = False , envUnquoteFlags = defaultUnquoteFlags , envInstanceDepth = 0 } disableDestructiveUpdate :: TCM a -> TCM a disableDestructiveUpdate = local $ \e -> e { envAllowDestructiveUpdate = False } data UnquoteFlags = UnquoteFlags { _unquoteNormalise :: Bool } deriving (Typeable, Data) defaultUnquoteFlags :: UnquoteFlags defaultUnquoteFlags = UnquoteFlags { _unquoteNormalise = False } unquoteNormalise :: Lens' Bool UnquoteFlags unquoteNormalise f e = f (_unquoteNormalise e) <&> \ x -> e { _unquoteNormalise = x } eUnquoteNormalise :: Lens' Bool TCEnv eUnquoteNormalise = eUnquoteFlags . unquoteNormalise -- * e-prefixed lenses ------------------------------------------------------------------------ eContext :: Lens' Context TCEnv eContext f e = f (envContext e) <&> \ x -> e { envContext = x } eLetBindings :: Lens' LetBindings TCEnv eLetBindings f e = f (envLetBindings e) <&> \ x -> e { envLetBindings = x } eCurrentModule :: Lens' ModuleName TCEnv eCurrentModule f e = f (envCurrentModule e) <&> \ x -> e { envCurrentModule = x } eCurrentPath :: Lens' (Maybe AbsolutePath) TCEnv eCurrentPath f e = f (envCurrentPath e) <&> \ x -> e { envCurrentPath = x } eAnonymousModules :: Lens' [(ModuleName, Nat)] TCEnv eAnonymousModules f e = f (envAnonymousModules e) <&> \ x -> e { envAnonymousModules = x } eImportPath :: Lens' [C.TopLevelModuleName] TCEnv eImportPath f e = f (envImportPath e) <&> \ x -> e { envImportPath = x } eMutualBlock :: Lens' (Maybe MutualId) TCEnv eMutualBlock f e = f (envMutualBlock e) <&> \ x -> e { envMutualBlock = x } eTerminationCheck :: Lens' (TerminationCheck ()) TCEnv eTerminationCheck f e = f (envTerminationCheck e) <&> \ x -> e { envTerminationCheck = x } eSolvingConstraints :: Lens' Bool TCEnv eSolvingConstraints f e = f (envSolvingConstraints e) <&> \ x -> e { envSolvingConstraints = x } eCheckingWhere :: Lens' Bool TCEnv eCheckingWhere f e = f (envCheckingWhere e) <&> \ x -> e { envCheckingWhere = x } eAssignMetas :: Lens' Bool TCEnv eAssignMetas f e = f (envAssignMetas e) <&> \ x -> e { envAssignMetas = x } eActiveProblems :: Lens' (Set ProblemId) TCEnv eActiveProblems f e = f (envActiveProblems e) <&> \ x -> e { envActiveProblems = x } eAbstractMode :: Lens' AbstractMode TCEnv eAbstractMode f e = f (envAbstractMode e) <&> \ x -> e { envAbstractMode = x } eRelevance :: Lens' Relevance TCEnv eRelevance f e = f (envRelevance e) <&> \ x -> e { envRelevance = x } eDisplayFormsEnabled :: Lens' Bool TCEnv eDisplayFormsEnabled f e = f (envDisplayFormsEnabled e) <&> \ x -> e { envDisplayFormsEnabled = x } eRange :: Lens' Range TCEnv eRange f e = f (envRange e) <&> \ x -> e { envRange = x } eHighlightingRange :: Lens' Range TCEnv eHighlightingRange f e = f (envHighlightingRange e) <&> \ x -> e { envHighlightingRange = x } eCall :: Lens' (Maybe (Closure Call)) TCEnv eCall f e = f (envCall e) <&> \ x -> e { envCall = x } eHighlightingLevel :: Lens' HighlightingLevel TCEnv eHighlightingLevel f e = f (envHighlightingLevel e) <&> \ x -> e { envHighlightingLevel = x } eHighlightingMethod :: Lens' HighlightingMethod TCEnv eHighlightingMethod f e = f (envHighlightingMethod e) <&> \ x -> e { envHighlightingMethod = x } eModuleNestingLevel :: Lens' Int TCEnv eModuleNestingLevel f e = f (envModuleNestingLevel e) <&> \ x -> e { envModuleNestingLevel = x } eAllowDestructiveUpdate :: Lens' Bool TCEnv eAllowDestructiveUpdate f e = f (envAllowDestructiveUpdate e) <&> \ x -> e { envAllowDestructiveUpdate = x } eExpandLast :: Lens' ExpandHidden TCEnv eExpandLast f e = f (envExpandLast e) <&> \ x -> e { envExpandLast = x } eAppDef :: Lens' (Maybe QName) TCEnv eAppDef f e = f (envAppDef e) <&> \ x -> e { envAppDef = x } eSimplification :: Lens' Simplification TCEnv eSimplification f e = f (envSimplification e) <&> \ x -> e { envSimplification = x } eAllowedReductions :: Lens' AllowedReductions TCEnv eAllowedReductions f e = f (envAllowedReductions e) <&> \ x -> e { envAllowedReductions = x } eCompareBlocked :: Lens' Bool TCEnv eCompareBlocked f e = f (envCompareBlocked e) <&> \ x -> e { envCompareBlocked = x } ePrintDomainFreePi :: Lens' Bool TCEnv ePrintDomainFreePi f e = f (envPrintDomainFreePi e) <&> \ x -> e { envPrintDomainFreePi = x } eInsideDotPattern :: Lens' Bool TCEnv eInsideDotPattern f e = f (envInsideDotPattern e) <&> \ x -> e { envInsideDotPattern = x } eUnquoteFlags :: Lens' UnquoteFlags TCEnv eUnquoteFlags f e = f (envUnquoteFlags e) <&> \ x -> e { envUnquoteFlags = x } eInstanceDepth :: Lens' Int TCEnv eInstanceDepth f e = f (envInstanceDepth e) <&> \ x -> e { envInstanceDepth = x } --------------------------------------------------------------------------- -- ** Context --------------------------------------------------------------------------- -- | The @Context@ is a stack of 'ContextEntry's. type Context = [ContextEntry] data ContextEntry = Ctx { ctxId :: CtxId , ctxEntry :: Dom (Name, Type) } deriving (Typeable, Data) newtype CtxId = CtxId Nat deriving (Typeable, Data, Eq, Ord, Show, Enum, Real, Integral, Num) --------------------------------------------------------------------------- -- ** Let bindings --------------------------------------------------------------------------- type LetBindings = Map Name (Open (Term, Dom Type)) --------------------------------------------------------------------------- -- ** Abstract mode --------------------------------------------------------------------------- data AbstractMode = AbstractMode -- ^ Abstract things in the current module can be accessed. | ConcreteMode -- ^ No abstract things can be accessed. | IgnoreAbstractMode -- ^ All abstract things can be accessed. deriving (Typeable, Data, Show, Eq) aDefToMode :: IsAbstract -> AbstractMode aDefToMode AbstractDef = AbstractMode aDefToMode ConcreteDef = ConcreteMode aModeToDef :: AbstractMode -> IsAbstract aModeToDef AbstractMode = AbstractDef aModeToDef ConcreteMode = ConcreteDef aModeToDef _ = __IMPOSSIBLE__ --------------------------------------------------------------------------- -- ** Insertion of implicit arguments --------------------------------------------------------------------------- data ExpandHidden = ExpandLast -- ^ Add implicit arguments in the end until type is no longer hidden 'Pi'. | DontExpandLast -- ^ Do not append implicit arguments. deriving (Eq, Typeable, Data) data ExplicitToInstance = ExplicitToInstance -- ^ Explicit arguments are considered as instance arguments | ExplicitStayExplicit deriving (Eq, Show, Typeable, Data) -- | A candidate solution for an instance meta is a term with its type. -- It may be the case that the candidate is not fully applied yet or -- of the wrong type, hence the need for the type. data Candidate = Candidate { candidateTerm :: Term , candidateType :: Type , candidateEti :: ExplicitToInstance , candidateOverlappable :: Bool } deriving (Show, Typeable, Data) instance Free Candidate where freeVars' (Candidate t u _ _) = freeVars' (t, u) --------------------------------------------------------------------------- -- * Type checking warnings (aka non-fatal errors) --------------------------------------------------------------------------- -- | A non-fatal error is an error which does not prevent us from -- checking the document further and interacting with the user. data Warning = NicifierIssue [DeclarationWarning] | TerminationIssue [TerminationError] | UnreachableClauses QName [[NamedArg DeBruijnPattern]] | CoverageIssue QName [(Telescope, [NamedArg DeBruijnPattern])] -- ^ `CoverageIssue f pss` means that `pss` are not covered in `f` | CoverageNoExactSplit QName [Clause] | NotStrictlyPositive QName OccursWhere | UnsolvedMetaVariables [Range] -- ^ Do not use directly with 'warning' | UnsolvedInteractionMetas [Range] -- ^ Do not use directly with 'warning' | UnsolvedConstraints Constraints -- ^ Do not use directly with 'warning' | OldBuiltin String String -- ^ In `OldBuiltin old new`, the BUILTIN old has been replaced by new | EmptyRewritePragma -- ^ If the user wrote just @{-# REWRITE #-}@. | UselessPublic -- ^ If the user opens a module public before the module header. -- (See issue #2377.) | UselessInline QName -- Generic warnings for one-off things | GenericWarning Doc -- ^ Harmless generic warning (not an error) | GenericNonFatalError Doc -- ^ Generic error which doesn't abort proceedings (not a warning) -- Safe flag errors | SafeFlagPostulate C.Name | SafeFlagPragma [String] | SafeFlagNonTerminating | SafeFlagTerminating | SafeFlagPrimTrustMe | SafeFlagNoPositivityCheck | SafeFlagPolarity | ParseWarning ParseWarning | DeprecationWarning String String String -- ^ `DeprecationWarning old new version`: -- `old` is deprecated, use `new` instead. This will be an error in Agda `version`. deriving ( Show , Data #if __GLASGOW_HASKELL__ <= 708 , Typeable #endif ) data TCWarning = TCWarning { tcWarningRange :: Range -- ^ Range where the warning was raised , tcWarning :: Warning -- ^ The warning itself , tcWarningPrintedWarning :: Doc -- ^ The warning printed in the state and environment where it was raised } deriving ( Show #if __GLASGOW_HASKELL__ <= 708 , Typeable #endif ) tcWarningOrigin :: TCWarning -> SrcFile tcWarningOrigin = rangeFile . tcWarningRange instance HasRange TCWarning where getRange = tcWarningRange -- used for merging lists of warnings instance Eq TCWarning where x == y = equalHeadConstructors (tcWarning x) (tcWarning y) && getRange x == getRange y equalHeadConstructors :: Warning -> Warning -> Bool equalHeadConstructors = (==) `on` toConstr getPartialDefs :: ReadTCState tcm => tcm [QName] getPartialDefs = do tcst <- getTCState return $ catMaybes . fmap (extractQName . tcWarning) $ tcst ^. stTCWarnings where extractQName :: Warning -> Maybe QName extractQName (CoverageIssue f _) = Just f extractQName _ = Nothing --------------------------------------------------------------------------- -- * Type checking errors --------------------------------------------------------------------------- -- | Information about a call. data CallInfo = CallInfo { callInfoTarget :: QName -- ^ Target function name. , callInfoRange :: Range -- ^ Range of the target function. , callInfoCall :: Closure Term -- ^ To be formatted representation of the call. } deriving (Typeable, Data, Show) -- no Eq, Ord instances: too expensive! (see issues 851, 852) -- | We only 'show' the name of the callee. instance Pretty CallInfo where pretty = pretty . callInfoTarget instance AllNames CallInfo where allNames = singleton . callInfoTarget -- UNUSED, but keep! -- -- | Call pathes are sequences of 'CallInfo's starting from a 'callSource'. -- data CallPath = CallPath -- { callSource :: QName -- -- ^ The originator of the first call. -- , callInfos :: [CallInfo] -- -- ^ The calls, in order from source to final target. -- } -- deriving (Show) -- -- | 'CallPath'es can be connected, but there is no empty callpath. -- -- Thus, they form a semigroup, but we choose to abuse 'Monoid'. -- instance Monoid CallPath where -- mempty = __IMPOSSIBLE__ -- mappend (CallPath src cs) (CallPath _ cs') = CallPath src $ cs ++ cs' -- | Information about a mutual block which did not pass the -- termination checker. data TerminationError = TerminationError { termErrFunctions :: [QName] -- ^ The functions which failed to check. (May not include -- automatically generated functions.) , termErrCalls :: [CallInfo] -- ^ The problematic call sites. } deriving (Typeable, Data, Show) -- | Error when splitting a pattern variable into possible constructor patterns. data SplitError = NotADatatype (Closure Type) -- ^ Neither data type nor record. | IrrelevantDatatype (Closure Type) -- ^ Data type, but in irrelevant position. | CoinductiveDatatype (Closure Type) -- ^ Split on codata not allowed. -- UNUSED, but keep! -- -- | NoRecordConstructor Type -- ^ record type, but no constructor | UnificationStuck { cantSplitConName :: QName -- ^ Constructor. , cantSplitTel :: Telescope -- ^ Context for indices. , cantSplitConIdx :: Args -- ^ Inferred indices (from type of constructor). , cantSplitGivenIdx :: Args -- ^ Expected indices (from checking pattern). , cantSplitFailures :: [UnificationFailure] -- ^ Reason(s) why unification got stuck. } | GenericSplitError String deriving (Show) data NegativeUnification = UnifyConflict Telescope Term Term | UnifyCycle Telescope Int Term deriving (Show) data UnificationFailure = UnifyIndicesNotVars Telescope Type Term Term Args -- ^ Failed to apply injectivity to constructor of indexed datatype | UnifyRecursiveEq Telescope Type Int Term -- ^ Can't solve equation because variable occurs in (type of) lhs | UnifyReflexiveEq Telescope Type Term -- ^ Can't solve reflexive equation because --without-K is enabled deriving (Show) data UnquoteError = BadVisibility String (Arg I.Term) | ConInsteadOfDef QName String String | DefInsteadOfCon QName String String | NonCanonical String I.Term | BlockedOnMeta TCState MetaId | UnquotePanic String deriving (Show) data TypeError = InternalError String | NotImplemented String | NotSupported String | CompilationError String | TerminationCheckFailed [TerminationError] | PropMustBeSingleton | DataMustEndInSort Term {- UNUSED | DataTooManyParameters -- ^ In @data D xs where@ the number of parameters @xs@ does not fit the -- the parameters given in the forward declaraion @data D Gamma : T@. -} | ShouldEndInApplicationOfTheDatatype Type -- ^ The target of a constructor isn't an application of its -- datatype. The 'Type' records what it does target. | ShouldBeAppliedToTheDatatypeParameters Term Term -- ^ The target of a constructor isn't its datatype applied to -- something that isn't the parameters. First term is the correct -- target and the second term is the actual target. | ShouldBeApplicationOf Type QName -- ^ Expected a type to be an application of a particular datatype. | ConstructorPatternInWrongDatatype QName QName -- ^ constructor, datatype | CantResolveOverloadedConstructorsTargetingSameDatatype QName [QName] -- ^ Datatype, constructors. | DoesNotConstructAnElementOf QName Type -- ^ constructor, type | DifferentArities -- ^ Varying number of arguments for a function. | WrongHidingInLHS -- ^ The left hand side of a function definition has a hidden argument -- where a non-hidden was expected. | WrongHidingInLambda Type -- ^ Expected a non-hidden function and found a hidden lambda. | WrongHidingInApplication Type -- ^ A function is applied to a hidden argument where a non-hidden was expected. | WrongNamedArgument (NamedArg A.Expr) -- ^ A function is applied to a hidden named argument it does not have. | WrongIrrelevanceInLambda -- ^ Wrong user-given relevance annotation in lambda. | WrongInstanceDeclaration -- ^ A term is declared as an instance but it’s not allowed | HidingMismatch Hiding Hiding -- ^ The given hiding does not correspond to the expected hiding. | RelevanceMismatch Relevance Relevance -- ^ The given relevance does not correspond to the expected relevane. | UninstantiatedDotPattern A.Expr | IlltypedPattern A.Pattern Type | IllformedProjectionPattern A.Pattern | CannotEliminateWithPattern (NamedArg A.Pattern) Type | TooManyArgumentsInLHS Type | WrongNumberOfConstructorArguments QName Nat Nat | ShouldBeEmpty Type [DeBruijnPattern] | ShouldBeASort Type -- ^ The given type should have been a sort. | ShouldBePi Type -- ^ The given type should have been a pi. | ShouldBeRecordType Type | ShouldBeRecordPattern DeBruijnPattern | NotAProjectionPattern (NamedArg A.Pattern) | NotAProperTerm | SetOmegaNotValidType | InvalidTypeSort Sort -- ^ This sort is not a type expression. | InvalidType Term -- ^ This term is not a type expression. | FunctionTypeInSizeUniv Term -- ^ This term, a function type constructor, lives in -- @SizeUniv@, which is not allowed. | SplitOnIrrelevant A.Pattern (Dom Type) | DefinitionIsIrrelevant QName | VariableIsIrrelevant Name -- | UnequalLevel Comparison Term Term -- UNUSED | UnequalTerms Comparison Term Term Type | UnequalTypes Comparison Type Type -- | UnequalTelescopes Comparison Telescope Telescope -- UNUSED | UnequalRelevance Comparison Term Term -- ^ The two function types have different relevance. | UnequalHiding Term Term -- ^ The two function types have different hiding. | UnequalSorts Sort Sort | UnequalBecauseOfUniverseConflict Comparison Term Term | NotLeqSort Sort Sort | MetaCannotDependOn MetaId [Nat] Nat -- ^ The arguments are the meta variable, the parameters it can -- depend on and the paratemeter that it wants to depend on. | MetaOccursInItself MetaId | GenericError String | GenericDocError Doc | BuiltinMustBeConstructor String A.Expr | NoSuchBuiltinName String | DuplicateBuiltinBinding String Term Term | NoBindingForBuiltin String | NoSuchPrimitiveFunction String | ShadowedModule C.Name [A.ModuleName] | BuiltinInParameterisedModule String | IllegalLetInTelescope C.TypedBinding | NoRHSRequiresAbsurdPattern [NamedArg A.Pattern] | AbsurdPatternRequiresNoRHS [NamedArg A.Pattern] | TooFewFields QName [C.Name] | TooManyFields QName [C.Name] | DuplicateFields [C.Name] | DuplicateConstructors [C.Name] | WithOnFreeVariable A.Expr Term | UnexpectedWithPatterns [A.Pattern] | WithClausePatternMismatch A.Pattern (NamedArg DeBruijnPattern) | FieldOutsideRecord | ModuleArityMismatch A.ModuleName Telescope [NamedArg A.Expr] -- Coverage errors -- UNUSED: | IncompletePatternMatching Term [Elim] -- can only happen if coverage checking is switched off | SplitError SplitError | ImpossibleConstructor QName NegativeUnification -- Positivity errors | TooManyPolarities QName Int -- Import errors | LocalVsImportedModuleClash ModuleName | SolvedButOpenHoles -- ^ Some interaction points (holes) have not been filled by user. -- There are not 'UnsolvedMetas' since unification solved them. -- This is an error, since interaction points are never filled -- without user interaction. | CyclicModuleDependency [C.TopLevelModuleName] | FileNotFound C.TopLevelModuleName [AbsolutePath] | OverlappingProjects AbsolutePath C.TopLevelModuleName C.TopLevelModuleName | AmbiguousTopLevelModuleName C.TopLevelModuleName [AbsolutePath] | ModuleNameUnexpected C.TopLevelModuleName C.TopLevelModuleName -- ^ Found module name, expected module name. | ModuleNameDoesntMatchFileName C.TopLevelModuleName [AbsolutePath] | ClashingFileNamesFor ModuleName [AbsolutePath] | ModuleDefinedInOtherFile C.TopLevelModuleName AbsolutePath AbsolutePath -- ^ Module name, file from which it was loaded, file which -- the include path says contains the module. -- Scope errors | BothWithAndRHS | AbstractConstructorNotInScope A.QName | NotInScope [C.QName] | NoSuchModule C.QName | AmbiguousName C.QName [A.QName] | AmbiguousModule C.QName [A.ModuleName] | UninstantiatedModule C.QName | ClashingDefinition C.QName A.QName | ClashingModule A.ModuleName A.ModuleName | ClashingImport C.Name A.QName | ClashingModuleImport C.Name A.ModuleName | PatternShadowsConstructor A.Name A.QName | ModuleDoesntExport C.QName [C.ImportedName] | DuplicateImports C.QName [C.ImportedName] | InvalidPattern C.Pattern | RepeatedVariablesInPattern [C.Name] -- Concrete to Abstract errors | NotAModuleExpr C.Expr -- ^ The expr was used in the right hand side of an implicit module -- definition, but it wasn't of the form @m Delta@. | NotAnExpression C.Expr | NotAValidLetBinding NiceDeclaration | NotValidBeforeField NiceDeclaration | NothingAppliedToHiddenArg C.Expr | NothingAppliedToInstanceArg C.Expr -- Pattern synonym errors | BadArgumentsToPatternSynonym A.QName | TooFewArgumentsToPatternSynonym A.QName | UnusedVariableInPatternSynonym -- Operator errors | NoParseForApplication [C.Expr] | AmbiguousParseForApplication [C.Expr] [C.Expr] | NoParseForLHS LHSOrPatSyn C.Pattern | AmbiguousParseForLHS LHSOrPatSyn C.Pattern [C.Pattern] | OperatorInformation [NotationSection] TypeError {- UNUSED | NoParseForPatternSynonym C.Pattern | AmbiguousParseForPatternSynonym C.Pattern [C.Pattern] -} -- Usage errors -- Implicit From Scope errors | IFSNoCandidateInScope Type -- Reflection errors | UnquoteFailed UnquoteError | DeBruijnIndexOutOfScope Nat Telescope [Name] -- Language option errors | NeedOptionCopatterns | NeedOptionRewriting -- Failure associated to warnings | NonFatalErrors [TCWarning] -- Instance search errors | InstanceSearchDepthExhausted Term Type Int deriving (Typeable, Show) -- | Distinguish error message when parsing lhs or pattern synonym, resp. data LHSOrPatSyn = IsLHS | IsPatSyn deriving (Eq, Show) -- | Type-checking errors. data TCErr = TypeError { tcErrState :: TCState -- ^ The state in which the error was raised. , tcErrClosErr :: Closure TypeError -- ^ The environment in which the error as raised plus the error. } | Exception Range Doc | IOException TCState Range E.IOException -- ^ The first argument is the state in which the error was -- raised. | PatternErr -- ^ The exception which is usually caught. -- Raised for pattern violations during unification ('assignV') -- but also in other situations where we want to backtrack. deriving (Typeable) instance Error TCErr where strMsg = Exception noRange . text . strMsg instance Show TCErr where show (TypeError _ e) = show (envRange $ clEnv e) ++ ": " ++ show (clValue e) show (Exception r d) = show r ++ ": " ++ render d show (IOException _ r e) = show r ++ ": " ++ show e show PatternErr{} = "Pattern violation (you shouldn't see this)" instance HasRange TCErr where getRange (TypeError _ cl) = envRange $ clEnv cl getRange (Exception r _) = r getRange (IOException s r _) = r getRange PatternErr{} = noRange instance E.Exception TCErr ----------------------------------------------------------------------------- -- * Accessing options ----------------------------------------------------------------------------- class (Functor m, Applicative m, Monad m) => HasOptions m where -- | Returns the pragma options which are currently in effect. pragmaOptions :: m PragmaOptions -- | Returns the command line options which are currently in effect. commandLineOptions :: m CommandLineOptions instance MonadIO m => HasOptions (TCMT m) where pragmaOptions = use stPragmaOptions commandLineOptions = do p <- use stPragmaOptions cl <- stPersistentOptions . stPersistentState <$> get return $ cl { optPragmaOptions = p } instance HasOptions m => HasOptions (ExceptT e m) where pragmaOptions = lift pragmaOptions commandLineOptions = lift commandLineOptions instance HasOptions m => HasOptions (ListT m) where pragmaOptions = lift pragmaOptions commandLineOptions = lift commandLineOptions instance HasOptions m => HasOptions (MaybeT m) where pragmaOptions = lift pragmaOptions commandLineOptions = lift commandLineOptions instance HasOptions m => HasOptions (ReaderT r m) where pragmaOptions = lift pragmaOptions commandLineOptions = lift commandLineOptions instance HasOptions m => HasOptions (StateT s m) where pragmaOptions = lift pragmaOptions commandLineOptions = lift commandLineOptions instance (HasOptions m, Monoid w) => HasOptions (WriterT w m) where pragmaOptions = lift pragmaOptions commandLineOptions = lift commandLineOptions ----------------------------------------------------------------------------- -- * The reduce monad ----------------------------------------------------------------------------- -- | Environment of the reduce monad. data ReduceEnv = ReduceEnv { redEnv :: TCEnv -- ^ Read only access to environment. , redSt :: TCState -- ^ Read only access to state (signature, metas...). } mapRedEnv :: (TCEnv -> TCEnv) -> ReduceEnv -> ReduceEnv mapRedEnv f s = s { redEnv = f (redEnv s) } mapRedSt :: (TCState -> TCState) -> ReduceEnv -> ReduceEnv mapRedSt f s = s { redSt = f (redSt s) } mapRedEnvSt :: (TCEnv -> TCEnv) -> (TCState -> TCState) -> ReduceEnv -> ReduceEnv mapRedEnvSt f g (ReduceEnv e s) = ReduceEnv (f e) (g s) newtype ReduceM a = ReduceM { unReduceM :: ReduceEnv -> a } -- deriving (Functor, Applicative, Monad) fmapReduce :: (a -> b) -> ReduceM a -> ReduceM b fmapReduce f (ReduceM m) = ReduceM $ \ e -> f $! m e {-# INLINE fmapReduce #-} apReduce :: ReduceM (a -> b) -> ReduceM a -> ReduceM b apReduce (ReduceM f) (ReduceM x) = ReduceM $ \ e -> f e $! x e {-# INLINE apReduce #-} bindReduce :: ReduceM a -> (a -> ReduceM b) -> ReduceM b bindReduce (ReduceM m) f = ReduceM $ \ e -> unReduceM (f $! m e) e {-# INLINE bindReduce #-} instance Functor ReduceM where fmap = fmapReduce instance Applicative ReduceM where pure x = ReduceM (const x) (<*>) = apReduce instance Monad ReduceM where return = pure (>>=) = bindReduce (>>) = (*>) instance ReadTCState ReduceM where getTCState = ReduceM redSt runReduceM :: ReduceM a -> TCM a runReduceM m = do e <- ask s <- get return $! unReduceM m (ReduceEnv e s) runReduceF :: (a -> ReduceM b) -> TCM (a -> b) runReduceF f = do e <- ask s <- get return $ \x -> unReduceM (f x) (ReduceEnv e s) instance MonadReader TCEnv ReduceM where ask = ReduceM redEnv local f (ReduceM m) = ReduceM (m . mapRedEnv f) --------------------------------------------------------------------------- -- * Type checking monad transformer --------------------------------------------------------------------------- newtype TCMT m a = TCM { unTCM :: IORef TCState -> TCEnv -> m a } -- TODO: make dedicated MonadTCEnv and MonadTCState service classes instance MonadIO m => MonadReader TCEnv (TCMT m) where ask = TCM $ \s e -> return e local f (TCM m) = TCM $ \s e -> m s (f e) instance MonadIO m => MonadState TCState (TCMT m) where get = TCM $ \s _ -> liftIO (readIORef s) put s = TCM $ \r _ -> liftIO (writeIORef r s) type TCM = TCMT IO class ( Applicative tcm, MonadIO tcm , MonadReader TCEnv tcm , MonadState TCState tcm , HasOptions tcm ) => MonadTCM tcm where liftTCM :: TCM a -> tcm a instance MonadIO m => ReadTCState (TCMT m) where getTCState = get instance MonadError TCErr (TCMT IO) where throwError = liftIO . E.throwIO catchError m h = TCM $ \r e -> do oldState <- liftIO (readIORef r) unTCM m r e `E.catch` \err -> do -- Reset the state, but do not forget changes to the persistent -- component. Not for pattern violations. case err of PatternErr -> return () _ -> liftIO $ do newState <- readIORef r writeIORef r $ oldState { stPersistentState = stPersistentState newState } unTCM (h err) r e -- | Interaction monad. type IM = TCMT (Haskeline.InputT IO) runIM :: IM a -> TCM a runIM = mapTCMT (Haskeline.runInputT Haskeline.defaultSettings) instance MonadError TCErr IM where throwError = liftIO . E.throwIO catchError m h = mapTCMT liftIO $ runIM m `catchError` (runIM . h) -- | Preserve the state of the failing computation. catchError_ :: TCM a -> (TCErr -> TCM a) -> TCM a catchError_ m h = TCM $ \r e -> unTCM m r e `E.catch` \err -> unTCM (h err) r e -- | Execute a finalizer even when an exception is thrown. -- Does not catch any errors. -- In case both the regular computation and the finalizer -- throw an exception, the one of the finalizer is propagated. finally_ :: TCM a -> TCM b -> TCM a finally_ m f = do x <- m `catchError_` \ err -> f >> throwError err _ <- f return x {-# SPECIALIZE INLINE mapTCMT :: (forall a. IO a -> IO a) -> TCM a -> TCM a #-} mapTCMT :: (forall a. m a -> n a) -> TCMT m a -> TCMT n a mapTCMT f (TCM m) = TCM $ \s e -> f (m s e) pureTCM :: MonadIO m => (TCState -> TCEnv -> a) -> TCMT m a pureTCM f = TCM $ \r e -> do s <- liftIO $ readIORef r return (f s e) {-# RULES "liftTCM/id" liftTCM = id #-} instance MonadIO m => MonadTCM (TCMT m) where liftTCM = mapTCMT liftIO instance MonadTCM tcm => MonadTCM (MaybeT tcm) where liftTCM = lift . liftTCM instance MonadTCM tcm => MonadTCM (ListT tcm) where liftTCM = lift . liftTCM instance MonadTCM tcm => MonadTCM (ExceptT err tcm) where liftTCM = lift . liftTCM instance (Monoid w, MonadTCM tcm) => MonadTCM (WriterT w tcm) where liftTCM = lift . liftTCM {- The following is not possible since MonadTCM needs to be a -- MonadState TCState and a MonadReader TCEnv instance (MonadTCM tcm) => MonadTCM (StateT s tcm) where liftTCM = lift . liftTCM instance (MonadTCM tcm) => MonadTCM (ReaderT r tcm) where liftTCM = lift . liftTCM -} instance MonadTrans TCMT where lift m = TCM $ \_ _ -> m -- We want a special monad implementation of fail. instance MonadIO m => Monad (TCMT m) where return = pure (>>=) = bindTCMT (>>) = (*>) fail = internalError -- One goal of the definitions and pragmas below is to inline the -- monad operations as much as possible. This doesn't seem to have a -- large effect on the performance of the normal executable, but (at -- least on one machine/configuration) it has a massive effect on the -- performance of the profiling executable [1], and reduces the time -- attributed to bind from over 90% to about 25%. -- -- [1] When compiled with -auto-all and run with -p: roughly 750% -- faster for one example. returnTCMT :: MonadIO m => a -> TCMT m a returnTCMT = \x -> TCM $ \_ _ -> return x {-# INLINE returnTCMT #-} bindTCMT :: MonadIO m => TCMT m a -> (a -> TCMT m b) -> TCMT m b bindTCMT = \(TCM m) k -> TCM $ \r e -> m r e >>= \x -> unTCM (k x) r e {-# INLINE bindTCMT #-} thenTCMT :: MonadIO m => TCMT m a -> TCMT m b -> TCMT m b thenTCMT = \(TCM m1) (TCM m2) -> TCM $ \r e -> m1 r e >> m2 r e {-# INLINE thenTCMT #-} instance MonadIO m => Functor (TCMT m) where fmap = fmapTCMT fmapTCMT :: MonadIO m => (a -> b) -> TCMT m a -> TCMT m b fmapTCMT = \f (TCM m) -> TCM $ \r e -> liftM f (m r e) {-# INLINE fmapTCMT #-} instance MonadIO m => Applicative (TCMT m) where pure = returnTCMT (<*>) = apTCMT apTCMT :: MonadIO m => TCMT m (a -> b) -> TCMT m a -> TCMT m b apTCMT = \(TCM mf) (TCM m) -> TCM $ \r e -> ap (mf r e) (m r e) {-# INLINE apTCMT #-} instance MonadIO m => MonadIO (TCMT m) where liftIO m = TCM $ \s e -> do let r = envRange e liftIO $ wrap s r $ do x <- m x `seq` return x where wrap s r m = E.catch m $ \e -> do s <- readIORef s E.throwIO $ IOException s r e -- | We store benchmark statistics in an IORef. -- This enables benchmarking pure computation, see -- "Agda.Benchmarking". instance MonadBench Phase TCM where getBenchmark = liftIO $ getBenchmark putBenchmark = liftIO . putBenchmark finally = finally_ instance Null (TCM Doc) where empty = return empty null = __IMPOSSIBLE__ -- | Short-cutting disjunction forms a monoid. instance Semigroup (TCM Any) where ma <> mb = Any <$> do (getAny <$> ma) `or2M` (getAny <$> mb) instance Monoid (TCM Any) where mempty = return mempty mappend = (<>) patternViolation :: TCM a patternViolation = throwError PatternErr internalError :: MonadTCM tcm => String -> tcm a internalError s = typeError $ InternalError s genericError :: MonadTCM tcm => String -> tcm a genericError = typeError . GenericError {-# SPECIALIZE genericDocError :: Doc -> TCM a #-} genericDocError :: MonadTCM tcm => Doc -> tcm a genericDocError = typeError . GenericDocError {-# SPECIALIZE typeError :: TypeError -> TCM a #-} typeError :: MonadTCM tcm => TypeError -> tcm a typeError err = liftTCM $ throwError =<< typeError_ err {-# SPECIALIZE typeError_ :: TypeError -> TCM TCErr #-} typeError_ :: MonadTCM tcm => TypeError -> tcm TCErr typeError_ err = liftTCM $ TypeError <$> get <*> buildClosure err -- | Running the type checking monad (most general form). {-# SPECIALIZE runTCM :: TCEnv -> TCState -> TCM a -> IO (a, TCState) #-} runTCM :: MonadIO m => TCEnv -> TCState -> TCMT m a -> m (a, TCState) runTCM e s m = do r <- liftIO $ newIORef s a <- unTCM m r e s <- liftIO $ readIORef r return (a, s) -- | Running the type checking monad on toplevel (with initial state). runTCMTop :: TCM a -> IO (Either TCErr a) runTCMTop m = (Right <$> runTCMTop' m) `E.catch` (return . Left) runTCMTop' :: MonadIO m => TCMT m a -> m a runTCMTop' m = do r <- liftIO $ newIORef initState unTCM m r initEnv -- | 'runSafeTCM' runs a safe 'TCM' action (a 'TCM' action which cannot fail) -- in the initial environment. runSafeTCM :: TCM a -> TCState -> IO (a, TCState) runSafeTCM m st = runTCM initEnv st m `E.catch` (\ (e :: TCErr) -> __IMPOSSIBLE__) -- runSafeTCM m st = either__IMPOSSIBLE__ return <$> do -- -- Errors must be impossible. -- runTCM $ do -- put st -- a <- m -- st <- get -- return (a, st) -- | Runs the given computation in a separate thread, with /a copy/ of -- the current state and environment. -- -- Note that Agda sometimes uses actual, mutable state. If the -- computation given to @forkTCM@ tries to /modify/ this state, then -- bad things can happen, because accesses are not mutually exclusive. -- The @forkTCM@ function has been added mainly to allow the thread to -- /read/ (a snapshot of) the current state in a convenient way. -- -- Note also that exceptions which are raised in the thread are not -- propagated to the parent, so the thread should not do anything -- important. forkTCM :: TCM a -> TCM () forkTCM m = do s <- get e <- ask liftIO $ void $ C.forkIO $ void $ runTCM e s m -- | Base name for extended lambda patterns extendedLambdaName :: String extendedLambdaName = ".extendedlambda" -- | Name of absurdLambda definitions. absurdLambdaName :: String absurdLambdaName = ".absurdlambda" -- | Check whether we have an definition from an absurd lambda. isAbsurdLambdaName :: QName -> Bool isAbsurdLambdaName = (absurdLambdaName ==) . prettyShow . qnameName --------------------------------------------------------------------------- -- * KillRange instances --------------------------------------------------------------------------- instance KillRange Signature where killRange (Sig secs defs rews) = killRange2 Sig secs defs rews instance KillRange Sections where killRange = fmap killRange instance KillRange Definitions where killRange = fmap killRange instance KillRange RewriteRuleMap where killRange = fmap killRange instance KillRange Section where killRange (Section tel) = killRange1 Section tel instance KillRange Definition where killRange (Defn ai name t pols occs displ mut compiled inst copy ma inj def) = killRange12 Defn ai name t pols occs displ mut compiled inst copy ma inj def -- TODO clarify: Keep the range in the defName field? instance KillRange CtxId where killRange (CtxId x) = killRange1 CtxId x instance KillRange NLPat where killRange (PVar x y) = killRange2 PVar x y killRange (PWild) = PWild killRange (PDef x y) = killRange2 PDef x y killRange (PLam x y) = killRange2 PLam x y killRange (PPi x y) = killRange2 PPi x y killRange (PBoundVar x y) = killRange2 PBoundVar x y killRange (PTerm x) = killRange1 PTerm x instance KillRange NLPType where killRange (NLPType s a) = killRange2 NLPType s a instance KillRange RewriteRule where killRange (RewriteRule q gamma f es rhs t) = killRange6 RewriteRule q gamma f es rhs t instance KillRange CompiledRepresentation where killRange = id instance KillRange EtaEquality where killRange = id instance KillRange ExtLamInfo where killRange = id instance KillRange FunctionFlag where killRange = id instance KillRange Defn where killRange def = case def of Axiom -> Axiom AbstractDefn{} -> __IMPOSSIBLE__ -- only returned by 'getConstInfo'! Function cls comp tt inv mut isAbs delayed proj flags term extlam with copat -> killRange13 Function cls comp tt inv mut isAbs delayed proj flags term extlam with copat Datatype a b c d e f g h i j -> killRange10 Datatype a b c d e f g h i j Record a b c d e f g h i j -> killRange10 Record a b c d e f g h i j Constructor a b c d e f g -> killRange7 Constructor a b c d e f g Primitive a b c d -> killRange4 Primitive a b c d instance KillRange MutualId where killRange = id instance KillRange c => KillRange (FunctionInverse' c) where killRange NotInjective = NotInjective killRange (Inverse m) = Inverse $ killRangeMap m instance KillRange TermHead where killRange SortHead = SortHead killRange PiHead = PiHead killRange (ConsHead q) = ConsHead $ killRange q instance KillRange Projection where killRange (Projection a b c d e) = killRange5 Projection a b c d e instance KillRange ProjLams where killRange = id instance KillRange a => KillRange (Open a) where killRange = fmap killRange instance KillRange a => KillRange (Local a) where killRange (Local a b) = killRange2 Local a b killRange (Global a) = killRange1 Global a instance KillRange DisplayForm where killRange (Display n es dt) = killRange3 Display n es dt instance KillRange Polarity where killRange = id instance KillRange DisplayTerm where killRange dt = case dt of DWithApp dt dts es -> killRange3 DWithApp dt dts es DCon q ci dts -> killRange3 DCon q ci dts DDef q dts -> killRange2 DDef q dts DDot v -> killRange1 DDot v DTerm v -> killRange1 DTerm v Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Local.hs0000644000000000000000000000241713154613124020135 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Monad.Local where import Control.Applicative import Control.Monad import Data.Monoid import Agda.Syntax.Internal import Agda.TypeChecking.Substitute import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Context import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Env import Agda.TypeChecking.Monad.Options import Agda.TypeChecking.Free import {-# SOURCE #-} Agda.TypeChecking.Monad.Signature (inFreshModuleIfFreeParams, lookupSection) import Agda.Utils.Size import Agda.Utils.Impossible #include "undefined.h" -- | Precondition: must not be called if the module parameter of the current -- module have been refined or (touched at all). makeLocal :: Free a => a -> TCM (Local a) makeLocal x | closed x = return $ Global x | otherwise = inFreshModuleIfFreeParams $ do m <- currentModule return (Local m x) makeGlobal :: Free a => a -> TCM (Local a) makeGlobal x | closed x = return $ Global x | otherwise = __IMPOSSIBLE__ getLocal :: Subst Term a => Local a -> TCM (Maybe a) getLocal (Global x) = return (Just x) getLocal l@(Local m x) = do m' <- currentModule if m' == m || isSubModuleOf m' m then Just . (`applySubst` x) <$> getModuleParameterSub m else return Nothing Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Open.hs0000644000000000000000000000325113154613124020001 0ustar0000000000000000-- {-# LANGUAGE CPP #-} module Agda.TypeChecking.Monad.Open ( makeOpen , makeClosed, isClosed , getOpen , tryOpen ) where import Control.Applicative import Control.Monad import Control.Monad.Reader import qualified Data.List as List import Agda.TypeChecking.Substitute import Agda.TypeChecking.Monad.Base import {-# SOURCE #-} Agda.TypeChecking.Monad.Context import Agda.Utils.Except ( MonadError(catchError) ) -- | Create an open term in the current context. makeOpen :: a -> TCM (Open a) makeOpen x = do ctx <- getContextId return $ OpenThing ctx x -- | Create an open term which is closed. makeClosed :: a -> Open a makeClosed = OpenThing [] -- | Check if an 'Open' is closed. isClosed :: Open a -> Bool isClosed (OpenThing cxt _) = null cxt -- | Extract the value from an open term. Must be done in an extension of the -- context in which the term was created. getOpen :: (Subst t a, MonadReader TCEnv m) => Open a -> m a getOpen (OpenThing [] x) = return x getOpen (OpenThing ctx x) = do ctx' <- getContextId unless (ctx `List.isSuffixOf` ctx') $ fail $ "thing out of context (" ++ show ctx ++ " is not a sub context of " ++ show ctx' ++ ")" return $ raise (length ctx' - length ctx) x -- | Try to use an 'Open' the current context. -- Returns 'Nothing' if current context is not an extension of the -- context in which the 'Open' was created. tryOpen :: (Subst t a, MonadReader TCEnv m) => Open a -> m (Maybe a) tryOpen (OpenThing [] x) = return $ Just x tryOpen (OpenThing ctx x) = do ctx' <- getContextId if (ctx `List.isSuffixOf` ctx') then return $ Just $ raise (length ctx' - length ctx) x else return Nothing Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Sharing.hs0000644000000000000000000000347313154613124020501 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Monad.Sharing where import Control.Applicative import Control.Monad.Reader import qualified Data.List as List import Data.Function import Data.Traversable import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Options import Agda.Utils.Monad #include "undefined.h" import Agda.Utils.Impossible updateSharedTerm :: MonadReader TCEnv m => (Term -> m Term) -> Term -> m Term updateSharedTerm f v = ifM (asks envAllowDestructiveUpdate) (updateSharedM f v) (f $ ignoreSharing v) updateSharedTermF #if __GLASGOW_HASKELL__ <= 708 :: (Applicative m, MonadReader TCEnv m, Traversable f) #else :: (MonadReader TCEnv m, Traversable f) #endif => (Term -> m (f Term)) -> Term -> m (f Term) updateSharedTermF f v = ifM (asks envAllowDestructiveUpdate) (updateSharedFM f v) (f $ ignoreSharing v) updateSharedTermT :: (MonadTCM tcm, MonadTrans t, Monad (t tcm)) => (Term -> t tcm Term) -> Term -> t tcm Term updateSharedTermT f v = ifM (lift $ asks envAllowDestructiveUpdate) (updateSharedM f v) (f $ ignoreSharing v) forceEqualTerms :: Term -> Term -> TCM () forceEqualTerms u v = whenM (asks envAllowDestructiveUpdate) $ when (null $ (List.intersect `on` pointerChain) u v) $ case (u, v) of (Shared p, Shared q) | p > q -> update u v (_, Shared{}) -> update v u (Shared{}, _) -> update u v _ -> return () where -- TODO: compress pointer chain update u@(Shared{}) v = do report u v setPtr v p `seq` compressPointerChain u `seq` return () where p = last $ pointerChain u update _ _ = __IMPOSSIBLE__ report u v = reportSLn "tc.ptr" 50 $ "setting " ++ show u ++ "\n to " ++ show v Agda-2.5.3/src/full/Agda/TypeChecking/Monad/SizedTypes.hs0000644000000000000000000002302013154613124021177 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Stuff for sized types that does not require modules -- "Agda.TypeChecking.Reduce" or "Agda.TypeChecking.Constraints" -- (which import "Agda.TypeChecking.Monad"). module Agda.TypeChecking.Monad.SizedTypes where import Control.Applicative import Agda.Interaction.Options import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Options import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Monad.State import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Substitute () import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.List import Agda.Utils.Monad #include "undefined.h" import Agda.Utils.Impossible ------------------------------------------------------------------------ -- * Testing for type 'Size' ------------------------------------------------------------------------ -- | Result of querying whether size variable @i@ is bounded by another -- size. data BoundedSize = BoundedLt Term -- ^ yes @i : Size< t@ | BoundedNo deriving (Eq, Show) -- | Check if a type is the 'primSize' type. The argument should be 'reduce'd. class IsSizeType a where isSizeType :: a -> TCM (Maybe BoundedSize) instance IsSizeType a => IsSizeType (Dom a) where isSizeType = isSizeType . unDom instance IsSizeType a => IsSizeType (b,a) where isSizeType = isSizeType . snd instance IsSizeType a => IsSizeType (Type' a) where isSizeType = isSizeType . unEl instance IsSizeType Term where isSizeType v = isSizeTypeTest <*> pure v isSizeTypeTest :: TCM (Term -> Maybe BoundedSize) isSizeTypeTest = flip (ifM (optSizedTypes <$> pragmaOptions)) (return $ const Nothing) $ do (size, sizelt) <- getBuiltinSize let testType (Def d []) | Just d == size = Just BoundedNo testType (Def d [Apply v]) | Just d == sizelt = Just $ BoundedLt $ unArg v testType _ = Nothing return $ testType . ignoreSharing getBuiltinDefName :: String -> TCM (Maybe QName) getBuiltinDefName s = fromDef . fmap ignoreSharing <$> getBuiltin' s where fromDef (Just (Def d [])) = Just d fromDef _ = Nothing getBuiltinSize :: TCM (Maybe QName, Maybe QName) getBuiltinSize = do size <- getBuiltinDefName builtinSize sizelt <- getBuiltinDefName builtinSizeLt return (size, sizelt) isSizeNameTest :: TCM (QName -> Bool) isSizeNameTest = ifM (optSizedTypes <$> pragmaOptions) isSizeNameTestRaw (return $ const False) isSizeNameTestRaw :: TCM (QName -> Bool) isSizeNameTestRaw = do (size, sizelt) <- getBuiltinSize return $ (`elem` [size, sizelt]) . Just -- | Test whether OPTIONS --sized-types and whether -- the size built-ins are defined. haveSizedTypes :: TCM Bool haveSizedTypes = do Def _ [] <- ignoreSharing <$> primSize Def _ [] <- ignoreSharing <$> primSizeInf Def _ [] <- ignoreSharing <$> primSizeSuc optSizedTypes <$> pragmaOptions `catchError` \_ -> return False -- | Add polarity info to a SIZE builtin. builtinSizeHook :: String -> QName -> Type -> TCM () builtinSizeHook s q t = do when (s `elem` [builtinSizeLt, builtinSizeSuc]) $ do modifySignature $ updateDefinition q $ updateDefPolarity (const [Covariant]) . updateDefArgOccurrences (const [StrictPos]) when (s == builtinSizeMax) $ do modifySignature $ updateDefinition q $ updateDefPolarity (const [Covariant, Covariant]) . updateDefArgOccurrences (const [StrictPos, StrictPos]) {- . updateDefType (const tmax) where -- TODO: max : (i j : Size) -> Size< (suc (max i j)) tmax = -} ------------------------------------------------------------------------ -- * Constructors ------------------------------------------------------------------------ -- | The sort of built-in types @SIZE@ and @SIZELT@. sizeSort :: Sort sizeSort = mkType 0 -- | The type of built-in types @SIZE@ and @SIZELT@. sizeUniv :: Type sizeUniv = sort $ sizeSort -- | The built-in type @SIZE@ with user-given name. sizeType_ :: QName -> Type sizeType_ size = El sizeSort $ Def size [] -- | The built-in type @SIZE@. sizeType :: TCM Type sizeType = El sizeSort <$> primSize -- | The name of @SIZESUC@. sizeSucName :: TCM (Maybe QName) sizeSucName = do ifM (not . optSizedTypes <$> pragmaOptions) (return Nothing) $ tryMaybe $ do Def x [] <- ignoreSharing <$> primSizeSuc return x sizeSuc :: Nat -> Term -> TCM Term sizeSuc n v | n < 0 = __IMPOSSIBLE__ | n == 0 = return v | otherwise = do Def suc [] <- ignoreSharing <$> primSizeSuc return $ case iterate (sizeSuc_ suc) v !!! n of Nothing -> __IMPOSSIBLE__ Just t -> t sizeSuc_ :: QName -> Term -> Term sizeSuc_ suc v = Def suc [Apply $ defaultArg v] -- | Transform list of terms into a term build from binary maximum. sizeMax :: [Term] -> TCM Term sizeMax vs = case vs of [] -> __IMPOSSIBLE__ -- we do not have a zero size [v] -> return v vs -> do Def max [] <- primSizeMax return $ foldr1 (\ u v -> Def max $ map (Apply . defaultArg) [u,v]) vs ------------------------------------------------------------------------ -- * Viewing and unviewing sizes ------------------------------------------------------------------------ -- | A useful view on sizes. data SizeView = SizeInf | SizeSuc Term | OtherSize Term -- | Expects argument to be 'reduce'd. sizeView :: Term -> TCM SizeView sizeView v = do Def inf [] <- ignoreSharing <$> primSizeInf Def suc [] <- ignoreSharing <$> primSizeSuc case ignoreSharing v of Def x [] | x == inf -> return SizeInf Def x [Apply u] | x == suc -> return $ SizeSuc (unArg u) _ -> return $ OtherSize v type Offset = Nat -- | A deep view on sizes. data DeepSizeView = DSizeInf | DSizeVar Nat Offset | DSizeMeta MetaId Elims Offset | DOtherSize Term deriving (Show) data SizeViewComparable a = NotComparable | YesAbove DeepSizeView a | YesBelow DeepSizeView a deriving (Functor) -- | @sizeViewComparable v w@ checks whether @v >= w@ (then @Left@) -- or @v <= w@ (then @Right@). If uncomparable, it returns @NotComparable@. sizeViewComparable :: DeepSizeView -> DeepSizeView -> SizeViewComparable () sizeViewComparable v w = case (v,w) of (DSizeInf, _) -> YesAbove w () (_, DSizeInf) -> YesBelow w () (DSizeVar x n, DSizeVar y m) | x == y -> if n >= m then YesAbove w () else YesBelow w () _ -> NotComparable sizeViewSuc_ :: QName -> DeepSizeView -> DeepSizeView sizeViewSuc_ suc v = case v of DSizeInf -> DSizeInf DSizeVar i n -> DSizeVar i (n + 1) DSizeMeta x vs n -> DSizeMeta x vs (n + 1) DOtherSize u -> DOtherSize $ sizeSuc_ suc u -- | @sizeViewPred k v@ decrements @v@ by @k@ (must be possible!). sizeViewPred :: Nat -> DeepSizeView -> DeepSizeView sizeViewPred 0 v = v sizeViewPred k v = case v of DSizeInf -> DSizeInf DSizeVar i n | n >= k -> DSizeVar i (n - k) DSizeMeta x vs n | n >= k -> DSizeMeta x vs (n - k) _ -> __IMPOSSIBLE__ -- | @sizeViewOffset v@ returns the number of successors or Nothing when infty. sizeViewOffset :: DeepSizeView -> Maybe Offset sizeViewOffset v = case v of DSizeInf -> Nothing DSizeVar i n -> Just n DSizeMeta x vs n -> Just n DOtherSize u -> Just 0 -- | Remove successors common to both sides. removeSucs :: (DeepSizeView, DeepSizeView) -> (DeepSizeView, DeepSizeView) removeSucs (v, w) = (sizeViewPred k v, sizeViewPred k w) where k = case (sizeViewOffset v, sizeViewOffset w) of (Just n, Just m) -> min n m (Just n, Nothing) -> n (Nothing, Just m) -> m (Nothing, Nothing) -> 0 -- | Turn a size view into a term. unSizeView :: SizeView -> TCM Term unSizeView SizeInf = primSizeInf unSizeView (SizeSuc v) = sizeSuc 1 v unSizeView (OtherSize v) = return v unDeepSizeView :: DeepSizeView -> TCM Term unDeepSizeView v = case v of DSizeInf -> primSizeInf DSizeVar i n -> sizeSuc n $ var i DSizeMeta x us n -> sizeSuc n $ MetaV x us DOtherSize u -> return u ------------------------------------------------------------------------ -- * View on sizes where maximum is pulled to the top ------------------------------------------------------------------------ type SizeMaxView = [DeepSizeView] maxViewMax :: SizeMaxView -> SizeMaxView -> SizeMaxView maxViewMax v w = case (v,w) of (DSizeInf : _, _) -> [DSizeInf] (_, DSizeInf : _) -> [DSizeInf] _ -> foldr maxViewCons w v -- | @maxViewCons v ws = max v ws@. It only adds @v@ to @ws@ if it is not -- subsumed by an element of @ws@. maxViewCons :: DeepSizeView -> SizeMaxView -> SizeMaxView maxViewCons _ [DSizeInf] = [DSizeInf] maxViewCons DSizeInf _ = [DSizeInf] maxViewCons v ws = case sizeViewComparableWithMax v ws of NotComparable -> v:ws YesAbove _ ws' -> v:ws' YesBelow{} -> ws -- | @sizeViewComparableWithMax v ws@ tries to find @w@ in @ws@ that compares with @v@ -- and singles this out. -- Precondition: @v /= DSizeInv@. sizeViewComparableWithMax :: DeepSizeView -> SizeMaxView -> SizeViewComparable SizeMaxView sizeViewComparableWithMax v ws = case ws of [] -> __IMPOSSIBLE__ [w] -> fmap (const []) $ sizeViewComparable v w (w:ws) -> case sizeViewComparable v w of NotComparable -> fmap (w:) $ sizeViewComparableWithMax v ws r -> fmap (const ws) r maxViewSuc_ :: QName -> SizeMaxView -> SizeMaxView maxViewSuc_ suc = map (sizeViewSuc_ suc) unMaxView :: SizeMaxView -> TCM Term unMaxView vs = sizeMax =<< mapM unDeepSizeView vs Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Base.hs-boot0000644000000000000000000000115613154613124020715 0ustar0000000000000000module Agda.TypeChecking.Monad.Base where import Control.Applicative (Applicative) import Control.Monad.IO.Class (MonadIO) import Data.IORef (IORef) import Data.Map (Map) import Agda.Syntax.Concrete.Name (TopLevelModuleName) import Agda.Utils.FileName (AbsolutePath) data HighlightingMethod data TCEnv data TCState newtype TCMT m a = TCM { unTCM :: IORef TCState -> TCEnv -> m a } instance MonadIO m => Applicative (TCMT m) instance MonadIO m => Functor (TCMT m) instance MonadIO m => Monad (TCMT m) instance MonadIO m => MonadIO (TCMT m) type TCM = TCMT IO type ModuleToSource = Map TopLevelModuleName AbsolutePath Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Context.hs-boot0000644000000000000000000000047413154613124021471 0ustar0000000000000000{-# LANGUAGE FlexibleContexts #-} module Agda.TypeChecking.Monad.Context where import Control.Monad.Reader import Agda.Syntax.Common (Dom) import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base getContext :: MonadReader TCEnv m => m [Dom (Name, Type)] getContextId :: MonadReader TCEnv m => m [CtxId] Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Builtin.hs0000644000000000000000000006216013154613124020512 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Monad.Builtin where import Control.Applicative import Control.Monad.State import Control.Monad.Trans.Maybe import qualified Data.Map as Map import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Syntax.Literal import Agda.Syntax.Internal as I import Agda.TypeChecking.Monad.Base -- import Agda.TypeChecking.Functions -- LEADS TO IMPORT CYCLE import Agda.TypeChecking.Substitute import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.Lens import Agda.Utils.Monad import Agda.Utils.Maybe import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible class (Functor m, Applicative m, Monad m) => HasBuiltins m where getBuiltinThing :: String -> m (Maybe (Builtin PrimFun)) instance HasBuiltins m => HasBuiltins (MaybeT m) where getBuiltinThing b = lift $ getBuiltinThing b litType :: Literal -> TCM Type litType l = case l of LitNat _ n -> do _ <- primZero when (n > 0) $ void $ primSuc el <$> primNat LitFloat _ _ -> el <$> primFloat LitChar _ _ -> el <$> primChar LitString _ _ -> el <$> primString LitQName _ _ -> el <$> primQName LitMeta _ _ _ -> el <$> primAgdaMeta where el t = El (mkType 0) t instance MonadIO m => HasBuiltins (TCMT m) where getBuiltinThing b = liftM2 mplus (Map.lookup b <$> use stLocalBuiltins) (Map.lookup b <$> use stImportedBuiltins) setBuiltinThings :: BuiltinThings PrimFun -> TCM () setBuiltinThings b = stLocalBuiltins .= b bindBuiltinName :: String -> Term -> TCM () bindBuiltinName b x = do builtin <- getBuiltinThing b case builtin of Just (Builtin y) -> typeError $ DuplicateBuiltinBinding b y x Just (Prim _) -> typeError $ NoSuchBuiltinName b Nothing -> stLocalBuiltins %= Map.insert b (Builtin x) bindPrimitive :: String -> PrimFun -> TCM () bindPrimitive b pf = do builtin <- use stLocalBuiltins setBuiltinThings $ Map.insert b (Prim pf) builtin getBuiltin :: String -> TCM Term getBuiltin x = fromMaybeM (typeError $ NoBindingForBuiltin x) $ getBuiltin' x getBuiltin' :: HasBuiltins m => String -> m (Maybe Term) getBuiltin' x = do builtin <- getBuiltinThing x case builtin of -- ignore sharing to make sure zero isn't reduced to Lit 0 Just (Builtin t) -> return $ Just $ ignoreSharing $ killRange t _ -> return Nothing getPrimitive' :: HasBuiltins m => String -> m (Maybe PrimFun) getPrimitive' x = (getPrim =<<) <$> getBuiltinThing x where getPrim (Prim pf) = return pf getPrim _ = Nothing getPrimitive :: String -> TCM PrimFun getPrimitive x = fromMaybeM (typeError $ NoSuchPrimitiveFunction x) $ getPrimitive' x -- | Rewrite a literal to constructor form if possible. constructorForm :: Term -> TCM Term constructorForm v = constructorForm' primZero primSuc v constructorForm' :: Applicative m => m Term -> m Term -> Term -> m Term constructorForm' pZero pSuc v = case ignoreSharing v of Lit (LitNat r n) | n == 0 -> pZero | n > 0 -> (`apply1` Lit (LitNat r $ n - 1)) <$> pSuc | otherwise -> pure v _ -> pure v --------------------------------------------------------------------------- -- * The names of built-in things --------------------------------------------------------------------------- primInteger, primIntegerPos, primIntegerNegSuc, primFloat, primChar, primString, primUnit, primUnitUnit, primBool, primTrue, primFalse, primList, primNil, primCons, primIO, primNat, primSuc, primZero, primNatPlus, primNatMinus, primNatTimes, primNatDivSucAux, primNatModSucAux, primNatEquality, primNatLess, primSizeUniv, primSize, primSizeLt, primSizeSuc, primSizeInf, primSizeMax, primInf, primSharp, primFlat, primEquality, primRefl, primRewrite, -- Name of rewrite relation primLevel, primLevelZero, primLevelSuc, primLevelMax, primFromNat, primFromNeg, primFromString, -- builtins for reflection: primQName, primArgInfo, primArgArgInfo, primArg, primArgArg, primAbs, primAbsAbs, primAgdaTerm, primAgdaTermVar, primAgdaTermLam, primAgdaTermExtLam, primAgdaTermDef, primAgdaTermCon, primAgdaTermPi, primAgdaTermSort, primAgdaTermLit, primAgdaTermUnsupported, primAgdaTermMeta, primAgdaErrorPart, primAgdaErrorPartString, primAgdaErrorPartTerm, primAgdaErrorPartName, primHiding, primHidden, primInstance, primVisible, primRelevance, primRelevant, primIrrelevant, primAssoc, primAssocLeft, primAssocRight, primAssocNon, primPrecedence, primPrecRelated, primPrecUnrelated, primFixity, primFixityFixity, primAgdaLiteral, primAgdaLitNat, primAgdaLitFloat, primAgdaLitString, primAgdaLitChar, primAgdaLitQName, primAgdaLitMeta, primAgdaSort, primAgdaSortSet, primAgdaSortLit, primAgdaSortUnsupported, primAgdaDefinition, primAgdaDefinitionFunDef, primAgdaDefinitionDataDef, primAgdaDefinitionRecordDef, primAgdaDefinitionPostulate, primAgdaDefinitionPrimitive, primAgdaDefinitionDataConstructor, primAgdaClause, primAgdaClauseClause, primAgdaClauseAbsurd, primAgdaPattern, primAgdaPatCon, primAgdaPatVar, primAgdaPatDot, primAgdaPatLit, primAgdaPatProj, primAgdaPatAbsurd, primAgdaMeta, primAgdaTCM, primAgdaTCMReturn, primAgdaTCMBind, primAgdaTCMUnify, primAgdaTCMTypeError, primAgdaTCMInferType, primAgdaTCMCheckType, primAgdaTCMNormalise, primAgdaTCMReduce, primAgdaTCMCatchError, primAgdaTCMGetContext, primAgdaTCMExtendContext, primAgdaTCMInContext, primAgdaTCMFreshName, primAgdaTCMDeclareDef, primAgdaTCMDefineFun, primAgdaTCMGetType, primAgdaTCMGetDefinition, primAgdaTCMQuoteTerm, primAgdaTCMUnquoteTerm, primAgdaTCMBlockOnMeta, primAgdaTCMCommit, primAgdaTCMIsMacro, primAgdaTCMWithNormalisation, primAgdaTCMDebugPrint :: TCM Term primInteger = getBuiltin builtinInteger primIntegerPos = getBuiltin builtinIntegerPos primIntegerNegSuc = getBuiltin builtinIntegerNegSuc primFloat = getBuiltin builtinFloat primChar = getBuiltin builtinChar primString = getBuiltin builtinString primBool = getBuiltin builtinBool primUnit = getBuiltin builtinUnit primUnitUnit = getBuiltin builtinUnitUnit primTrue = getBuiltin builtinTrue primFalse = getBuiltin builtinFalse primList = getBuiltin builtinList primNil = getBuiltin builtinNil primCons = getBuiltin builtinCons primIO = getBuiltin builtinIO primNat = getBuiltin builtinNat primSuc = getBuiltin builtinSuc primZero = getBuiltin builtinZero primNatPlus = getBuiltin builtinNatPlus primNatMinus = getBuiltin builtinNatMinus primNatTimes = getBuiltin builtinNatTimes primNatDivSucAux = getBuiltin builtinNatDivSucAux primNatModSucAux = getBuiltin builtinNatModSucAux primNatEquality = getBuiltin builtinNatEquals primNatLess = getBuiltin builtinNatLess primSizeUniv = getBuiltin builtinSizeUniv primSize = getBuiltin builtinSize primSizeLt = getBuiltin builtinSizeLt primSizeSuc = getBuiltin builtinSizeSuc primSizeInf = getBuiltin builtinSizeInf primSizeMax = getBuiltin builtinSizeMax primInf = getBuiltin builtinInf primSharp = getBuiltin builtinSharp primFlat = getBuiltin builtinFlat primEquality = getBuiltin builtinEquality primRefl = getBuiltin builtinRefl primRewrite = getBuiltin builtinRewrite primLevel = getBuiltin builtinLevel primLevelZero = getBuiltin builtinLevelZero primLevelSuc = getBuiltin builtinLevelSuc primLevelMax = getBuiltin builtinLevelMax primFromNat = getBuiltin builtinFromNat primFromNeg = getBuiltin builtinFromNeg primFromString = getBuiltin builtinFromString primQName = getBuiltin builtinQName primArg = getBuiltin builtinArg primArgArg = getBuiltin builtinArgArg primAbs = getBuiltin builtinAbs primAbsAbs = getBuiltin builtinAbsAbs primAgdaSort = getBuiltin builtinAgdaSort primHiding = getBuiltin builtinHiding primHidden = getBuiltin builtinHidden primInstance = getBuiltin builtinInstance primVisible = getBuiltin builtinVisible primRelevance = getBuiltin builtinRelevance primRelevant = getBuiltin builtinRelevant primIrrelevant = getBuiltin builtinIrrelevant primAssoc = getBuiltin builtinAssoc primAssocLeft = getBuiltin builtinAssocLeft primAssocRight = getBuiltin builtinAssocRight primAssocNon = getBuiltin builtinAssocNon primPrecedence = getBuiltin builtinPrecedence primPrecRelated = getBuiltin builtinPrecRelated primPrecUnrelated = getBuiltin builtinPrecUnrelated primFixity = getBuiltin builtinFixity primFixityFixity = getBuiltin builtinFixityFixity primArgInfo = getBuiltin builtinArgInfo primArgArgInfo = getBuiltin builtinArgArgInfo primAgdaSortSet = getBuiltin builtinAgdaSortSet primAgdaSortLit = getBuiltin builtinAgdaSortLit primAgdaSortUnsupported = getBuiltin builtinAgdaSortUnsupported primAgdaTerm = getBuiltin builtinAgdaTerm primAgdaTermVar = getBuiltin builtinAgdaTermVar primAgdaTermLam = getBuiltin builtinAgdaTermLam primAgdaTermExtLam = getBuiltin builtinAgdaTermExtLam primAgdaTermDef = getBuiltin builtinAgdaTermDef primAgdaTermCon = getBuiltin builtinAgdaTermCon primAgdaTermPi = getBuiltin builtinAgdaTermPi primAgdaTermSort = getBuiltin builtinAgdaTermSort primAgdaTermLit = getBuiltin builtinAgdaTermLit primAgdaTermUnsupported = getBuiltin builtinAgdaTermUnsupported primAgdaTermMeta = getBuiltin builtinAgdaTermMeta primAgdaErrorPart = getBuiltin builtinAgdaErrorPart primAgdaErrorPartString = getBuiltin builtinAgdaErrorPartString primAgdaErrorPartTerm = getBuiltin builtinAgdaErrorPartTerm primAgdaErrorPartName = getBuiltin builtinAgdaErrorPartName primAgdaLiteral = getBuiltin builtinAgdaLiteral primAgdaLitNat = getBuiltin builtinAgdaLitNat primAgdaLitFloat = getBuiltin builtinAgdaLitFloat primAgdaLitChar = getBuiltin builtinAgdaLitChar primAgdaLitString = getBuiltin builtinAgdaLitString primAgdaLitQName = getBuiltin builtinAgdaLitQName primAgdaLitMeta = getBuiltin builtinAgdaLitMeta primAgdaPattern = getBuiltin builtinAgdaPattern primAgdaPatCon = getBuiltin builtinAgdaPatCon primAgdaPatVar = getBuiltin builtinAgdaPatVar primAgdaPatDot = getBuiltin builtinAgdaPatDot primAgdaPatLit = getBuiltin builtinAgdaPatLit primAgdaPatProj = getBuiltin builtinAgdaPatProj primAgdaPatAbsurd = getBuiltin builtinAgdaPatAbsurd primAgdaClause = getBuiltin builtinAgdaClause primAgdaClauseClause = getBuiltin builtinAgdaClauseClause primAgdaClauseAbsurd = getBuiltin builtinAgdaClauseAbsurd primAgdaDefinitionFunDef = getBuiltin builtinAgdaDefinitionFunDef primAgdaDefinitionDataDef = getBuiltin builtinAgdaDefinitionDataDef primAgdaDefinitionRecordDef = getBuiltin builtinAgdaDefinitionRecordDef primAgdaDefinitionDataConstructor = getBuiltin builtinAgdaDefinitionDataConstructor primAgdaDefinitionPostulate = getBuiltin builtinAgdaDefinitionPostulate primAgdaDefinitionPrimitive = getBuiltin builtinAgdaDefinitionPrimitive primAgdaDefinition = getBuiltin builtinAgdaDefinition primAgdaMeta = getBuiltin builtinAgdaMeta primAgdaTCM = getBuiltin builtinAgdaTCM primAgdaTCMReturn = getBuiltin builtinAgdaTCMReturn primAgdaTCMBind = getBuiltin builtinAgdaTCMBind primAgdaTCMUnify = getBuiltin builtinAgdaTCMUnify primAgdaTCMTypeError = getBuiltin builtinAgdaTCMTypeError primAgdaTCMInferType = getBuiltin builtinAgdaTCMInferType primAgdaTCMCheckType = getBuiltin builtinAgdaTCMCheckType primAgdaTCMNormalise = getBuiltin builtinAgdaTCMNormalise primAgdaTCMReduce = getBuiltin builtinAgdaTCMReduce primAgdaTCMCatchError = getBuiltin builtinAgdaTCMCatchError primAgdaTCMGetContext = getBuiltin builtinAgdaTCMGetContext primAgdaTCMExtendContext = getBuiltin builtinAgdaTCMExtendContext primAgdaTCMInContext = getBuiltin builtinAgdaTCMInContext primAgdaTCMFreshName = getBuiltin builtinAgdaTCMFreshName primAgdaTCMDeclareDef = getBuiltin builtinAgdaTCMDeclareDef primAgdaTCMDefineFun = getBuiltin builtinAgdaTCMDefineFun primAgdaTCMGetType = getBuiltin builtinAgdaTCMGetType primAgdaTCMGetDefinition = getBuiltin builtinAgdaTCMGetDefinition primAgdaTCMQuoteTerm = getBuiltin builtinAgdaTCMQuoteTerm primAgdaTCMUnquoteTerm = getBuiltin builtinAgdaTCMUnquoteTerm primAgdaTCMBlockOnMeta = getBuiltin builtinAgdaTCMBlockOnMeta primAgdaTCMCommit = getBuiltin builtinAgdaTCMCommit primAgdaTCMIsMacro = getBuiltin builtinAgdaTCMIsMacro primAgdaTCMWithNormalisation = getBuiltin builtinAgdaTCMWithNormalisation primAgdaTCMDebugPrint = getBuiltin builtinAgdaTCMDebugPrint builtinNat, builtinSuc, builtinZero, builtinNatPlus, builtinNatMinus, builtinNatTimes, builtinNatDivSucAux, builtinNatModSucAux, builtinNatEquals, builtinNatLess, builtinInteger, builtinIntegerPos, builtinIntegerNegSuc, builtinFloat, builtinChar, builtinString, builtinUnit, builtinUnitUnit, builtinBool, builtinTrue, builtinFalse, builtinList, builtinNil, builtinCons, builtinIO, builtinSizeUniv, builtinSize, builtinSizeLt, builtinSizeSuc, builtinSizeInf, builtinSizeMax, builtinInf, builtinSharp, builtinFlat, builtinEquality, builtinRefl, builtinRewrite, builtinLevelMax, builtinLevel, builtinLevelZero, builtinLevelSuc, builtinFromNat, builtinFromNeg, builtinFromString, builtinQName, builtinAgdaSort, builtinAgdaSortSet, builtinAgdaSortLit, builtinAgdaSortUnsupported, builtinHiding, builtinHidden, builtinInstance, builtinVisible, builtinRelevance, builtinRelevant, builtinIrrelevant, builtinArg, builtinAssoc, builtinAssocLeft, builtinAssocRight, builtinAssocNon, builtinPrecedence, builtinPrecRelated, builtinPrecUnrelated, builtinFixity, builtinFixityFixity, builtinArgInfo, builtinArgArgInfo, builtinArgArg, builtinAbs, builtinAbsAbs, builtinAgdaTerm, builtinAgdaTermVar, builtinAgdaTermLam, builtinAgdaTermExtLam, builtinAgdaTermDef, builtinAgdaTermCon, builtinAgdaTermPi, builtinAgdaTermSort, builtinAgdaTermLit, builtinAgdaTermUnsupported, builtinAgdaTermMeta, builtinAgdaErrorPart, builtinAgdaErrorPartString, builtinAgdaErrorPartTerm, builtinAgdaErrorPartName, builtinAgdaLiteral, builtinAgdaLitNat, builtinAgdaLitFloat, builtinAgdaLitChar, builtinAgdaLitString, builtinAgdaLitQName, builtinAgdaLitMeta, builtinAgdaClause, builtinAgdaClauseClause, builtinAgdaClauseAbsurd, builtinAgdaPattern, builtinAgdaPatVar, builtinAgdaPatCon, builtinAgdaPatDot, builtinAgdaPatLit, builtinAgdaPatProj, builtinAgdaPatAbsurd, builtinAgdaDefinitionFunDef, builtinAgdaDefinitionDataDef, builtinAgdaDefinitionRecordDef, builtinAgdaDefinitionDataConstructor, builtinAgdaDefinitionPostulate, builtinAgdaDefinitionPrimitive, builtinAgdaDefinition, builtinAgdaMeta, builtinAgdaTCM, builtinAgdaTCMReturn, builtinAgdaTCMBind, builtinAgdaTCMUnify, builtinAgdaTCMTypeError, builtinAgdaTCMInferType, builtinAgdaTCMCheckType, builtinAgdaTCMNormalise, builtinAgdaTCMReduce, builtinAgdaTCMCatchError, builtinAgdaTCMGetContext, builtinAgdaTCMExtendContext, builtinAgdaTCMInContext, builtinAgdaTCMFreshName, builtinAgdaTCMDeclareDef, builtinAgdaTCMDefineFun, builtinAgdaTCMGetType, builtinAgdaTCMGetDefinition, builtinAgdaTCMQuoteTerm, builtinAgdaTCMUnquoteTerm, builtinAgdaTCMBlockOnMeta, builtinAgdaTCMCommit, builtinAgdaTCMIsMacro, builtinAgdaTCMWithNormalisation, builtinAgdaTCMDebugPrint :: String builtinNat = "NATURAL" builtinSuc = "SUC" builtinZero = "ZERO" builtinNatPlus = "NATPLUS" builtinNatMinus = "NATMINUS" builtinNatTimes = "NATTIMES" builtinNatDivSucAux = "NATDIVSUCAUX" builtinNatModSucAux = "NATMODSUCAUX" builtinNatEquals = "NATEQUALS" builtinNatLess = "NATLESS" builtinInteger = "INTEGER" builtinIntegerPos = "INTEGERPOS" builtinIntegerNegSuc = "INTEGERNEGSUC" builtinFloat = "FLOAT" builtinChar = "CHAR" builtinString = "STRING" builtinUnit = "UNIT" builtinUnitUnit = "UNITUNIT" builtinBool = "BOOL" builtinTrue = "TRUE" builtinFalse = "FALSE" builtinList = "LIST" builtinNil = "NIL" builtinCons = "CONS" builtinIO = "IO" builtinSizeUniv = "SIZEUNIV" builtinSize = "SIZE" builtinSizeLt = "SIZELT" builtinSizeSuc = "SIZESUC" builtinSizeInf = "SIZEINF" builtinSizeMax = "SIZEMAX" builtinInf = "INFINITY" builtinSharp = "SHARP" builtinFlat = "FLAT" builtinEquality = "EQUALITY" builtinRefl = "REFL" builtinRewrite = "REWRITE" builtinLevelMax = "LEVELMAX" builtinLevel = "LEVEL" builtinLevelZero = "LEVELZERO" builtinLevelSuc = "LEVELSUC" builtinFromNat = "FROMNAT" builtinFromNeg = "FROMNEG" builtinFromString = "FROMSTRING" builtinQName = "QNAME" builtinAgdaSort = "AGDASORT" builtinAgdaSortSet = "AGDASORTSET" builtinAgdaSortLit = "AGDASORTLIT" builtinAgdaSortUnsupported = "AGDASORTUNSUPPORTED" builtinHiding = "HIDING" builtinHidden = "HIDDEN" builtinInstance = "INSTANCE" builtinVisible = "VISIBLE" builtinRelevance = "RELEVANCE" builtinRelevant = "RELEVANT" builtinIrrelevant = "IRRELEVANT" builtinAssoc = "ASSOC" builtinAssocLeft = "ASSOCLEFT" builtinAssocRight = "ASSOCRIGHT" builtinAssocNon = "ASSOCNON" builtinPrecedence = "PRECEDENCE" builtinPrecRelated = "PRECRELATED" builtinPrecUnrelated = "PRECUNRELATED" builtinFixity = "FIXITY" builtinFixityFixity = "FIXITYFIXITY" builtinArg = "ARG" builtinArgInfo = "ARGINFO" builtinArgArgInfo = "ARGARGINFO" builtinArgArg = "ARGARG" builtinAbs = "ABS" builtinAbsAbs = "ABSABS" builtinAgdaTerm = "AGDATERM" builtinAgdaTermVar = "AGDATERMVAR" builtinAgdaTermLam = "AGDATERMLAM" builtinAgdaTermExtLam = "AGDATERMEXTLAM" builtinAgdaTermDef = "AGDATERMDEF" builtinAgdaTermCon = "AGDATERMCON" builtinAgdaTermPi = "AGDATERMPI" builtinAgdaTermSort = "AGDATERMSORT" builtinAgdaTermLit = "AGDATERMLIT" builtinAgdaTermUnsupported = "AGDATERMUNSUPPORTED" builtinAgdaTermMeta = "AGDATERMMETA" builtinAgdaErrorPart = "AGDAERRORPART" builtinAgdaErrorPartString = "AGDAERRORPARTSTRING" builtinAgdaErrorPartTerm = "AGDAERRORPARTTERM" builtinAgdaErrorPartName = "AGDAERRORPARTNAME" builtinAgdaLiteral = "AGDALITERAL" builtinAgdaLitNat = "AGDALITNAT" builtinAgdaLitFloat = "AGDALITFLOAT" builtinAgdaLitChar = "AGDALITCHAR" builtinAgdaLitString = "AGDALITSTRING" builtinAgdaLitQName = "AGDALITQNAME" builtinAgdaLitMeta = "AGDALITMETA" builtinAgdaClause = "AGDACLAUSE" builtinAgdaClauseClause = "AGDACLAUSECLAUSE" builtinAgdaClauseAbsurd = "AGDACLAUSEABSURD" builtinAgdaPattern = "AGDAPATTERN" builtinAgdaPatVar = "AGDAPATVAR" builtinAgdaPatCon = "AGDAPATCON" builtinAgdaPatDot = "AGDAPATDOT" builtinAgdaPatLit = "AGDAPATLIT" builtinAgdaPatProj = "AGDAPATPROJ" builtinAgdaPatAbsurd = "AGDAPATABSURD" builtinAgdaDefinitionFunDef = "AGDADEFINITIONFUNDEF" builtinAgdaDefinitionDataDef = "AGDADEFINITIONDATADEF" builtinAgdaDefinitionRecordDef = "AGDADEFINITIONRECORDDEF" builtinAgdaDefinitionDataConstructor = "AGDADEFINITIONDATACONSTRUCTOR" builtinAgdaDefinitionPostulate = "AGDADEFINITIONPOSTULATE" builtinAgdaDefinitionPrimitive = "AGDADEFINITIONPRIMITIVE" builtinAgdaDefinition = "AGDADEFINITION" builtinAgdaMeta = "AGDAMETA" builtinAgdaTCM = "AGDATCM" builtinAgdaTCMReturn = "AGDATCMRETURN" builtinAgdaTCMBind = "AGDATCMBIND" builtinAgdaTCMUnify = "AGDATCMUNIFY" builtinAgdaTCMTypeError = "AGDATCMTYPEERROR" builtinAgdaTCMInferType = "AGDATCMINFERTYPE" builtinAgdaTCMCheckType = "AGDATCMCHECKTYPE" builtinAgdaTCMNormalise = "AGDATCMNORMALISE" builtinAgdaTCMReduce = "AGDATCMREDUCE" builtinAgdaTCMCatchError = "AGDATCMCATCHERROR" builtinAgdaTCMGetContext = "AGDATCMGETCONTEXT" builtinAgdaTCMExtendContext = "AGDATCMEXTENDCONTEXT" builtinAgdaTCMInContext = "AGDATCMINCONTEXT" builtinAgdaTCMFreshName = "AGDATCMFRESHNAME" builtinAgdaTCMDeclareDef = "AGDATCMDECLAREDEF" builtinAgdaTCMDefineFun = "AGDATCMDEFINEFUN" builtinAgdaTCMGetType = "AGDATCMGETTYPE" builtinAgdaTCMGetDefinition = "AGDATCMGETDEFINITION" builtinAgdaTCMBlockOnMeta = "AGDATCMBLOCKONMETA" builtinAgdaTCMCommit = "AGDATCMCOMMIT" builtinAgdaTCMQuoteTerm = "AGDATCMQUOTETERM" builtinAgdaTCMUnquoteTerm = "AGDATCMUNQUOTETERM" builtinAgdaTCMIsMacro = "AGDATCMISMACRO" builtinAgdaTCMWithNormalisation = "AGDATCMWITHNORMALISATION" builtinAgdaTCMDebugPrint = "AGDATCMDEBUGPRINT" -- | Builtins that come without a definition in Agda syntax. -- These are giving names to Agda internal concepts which -- cannot be assigned an Agda type. -- -- An example would be a user-defined name for @Set@. -- -- {-# BUILTIN TYPE Type #-} -- -- The type of @Type@ would be @Type : Level → Setω@ -- which is not valid Agda. builtinsNoDef :: [String] builtinsNoDef = [ builtinSizeUniv , builtinSize , builtinSizeLt , builtinSizeSuc , builtinSizeInf , builtinSizeMax ] -- | The coinductive primitives. data CoinductionKit = CoinductionKit { nameOfInf :: QName , nameOfSharp :: QName , nameOfFlat :: QName } -- | Tries to build a 'CoinductionKit'. coinductionKit' :: TCM CoinductionKit coinductionKit' = do Def inf _ <- ignoreSharing <$> primInf Def sharp _ <- ignoreSharing <$> primSharp Def flat _ <- ignoreSharing <$> primFlat return $ CoinductionKit { nameOfInf = inf , nameOfSharp = sharp , nameOfFlat = flat } coinductionKit :: TCM (Maybe CoinductionKit) coinductionKit = tryMaybe coinductionKit' ------------------------------------------------------------------------ -- * Builtin equality ------------------------------------------------------------------------ -- | Get the name of the equality type. primEqualityName :: TCM QName -- primEqualityName = getDef =<< primEquality -- LEADS TO IMPORT CYCLE primEqualityName = do eq <- primEquality -- Andreas, 2014-05-17 moved this here from TC.Rules.Def -- Don't know why up to 2 hidden lambdas need to be stripped, -- but I left the code in place. -- Maybe it was intended that equality could be declared -- in three different ways: -- 1. universe and type polymorphic -- 2. type polymorphic only -- 3. monomorphic. let lamV (Lam i b) = mapFst (getHiding i :) $ lamV (unAbs b) lamV (Shared p) = lamV (derefPtr p) lamV v = ([], v) return $ case lamV eq of (_, Def equality _) -> equality -- OLD: -- ([Hidden, Hidden], Def equality _) -> equality -- ([Hidden], Def equality _) -> equality -- ([], Def equality _) -> equality _ -> __IMPOSSIBLE__ -- | Check whether the type is actually an equality (lhs ≡ rhs) -- and extract lhs, rhs, and their type. -- -- Precondition: type is reduced. equalityView :: Type -> TCM EqualityView equalityView t0@(El s t) = do equality <- primEqualityName case ignoreSharing t of Def equality' es | equality' == equality -> do let vs = fromMaybe __IMPOSSIBLE__ $ allApplyElims es let n = length vs unless (n >= 3) __IMPOSSIBLE__ let (pars, [ typ , lhs, rhs ]) = splitAt (n-3) vs return $ EqualityType s equality pars typ lhs rhs _ -> return $ OtherType t0 -- | Revert the 'EqualityView'. -- -- Postcondition: type is reduced. equalityUnview :: EqualityView -> Type equalityUnview (OtherType t) = t equalityUnview (EqualityType s equality l t lhs rhs) = El s $ Def equality $ map Apply (l ++ [t, lhs, rhs]) Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Constraints.hs0000644000000000000000000001661613154613124021420 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Monad.Constraints where import Control.Arrow ((&&&)) import Control.Applicative import Control.Monad.State import Control.Monad.Reader import qualified Data.List as List import Data.Set (Set) import qualified Data.Set as Set import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Closure import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Options import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Monad import Agda.Utils.Except #include "undefined.h" import Agda.Utils.Impossible -- | Add all constraints belonging to the given problem to the current problem(s). stealConstraints :: ProblemId -> TCM () stealConstraints pid = do current <- asks envActiveProblems reportSLn "tc.constr.steal" 50 $ "problem " ++ show (Set.toList current) ++ " is stealing problem " ++ show pid ++ "'s constraints!" -- Add current to any constraint in pid. let rename pc@(PConstr pids c) | Set.member pid pids = PConstr (Set.union current pids) c | otherwise = pc -- We should never steal from an active problem. whenM (Set.member pid <$> asks envActiveProblems) __IMPOSSIBLE__ modifyAwakeConstraints $ List.map rename modifySleepingConstraints $ List.map rename solvingProblem :: ProblemId -> TCM a -> TCM a solvingProblem pid = solvingProblems (Set.singleton pid) solvingProblems :: Set ProblemId -> TCM a -> TCM a solvingProblems pids m = verboseBracket "tc.constr.solve" 50 ("working on problems " ++ show (Set.toList pids)) $ do x <- local (\e -> e { envActiveProblems = pids `Set.union` envActiveProblems e }) m sequence_ [ ifNotM (isProblemSolved pid) (reportSLn "tc.constr.solve" 50 $ "problem " ++ show pid ++ " was not solved.") $ {- else -} do reportSLn "tc.constr.solve" 50 $ "problem " ++ show pid ++ " was solved!" wakeConstraints (return . blockedOn pid . clValue . theConstraint) | pid <- Set.toList pids ] return x where blockedOn pid (Guarded _ pid') = pid == pid' blockedOn _ _ = False isProblemSolved :: ProblemId -> TCM Bool isProblemSolved pid = and2M (not . Set.member pid <$> asks envActiveProblems) (all (not . Set.member pid . constraintProblems) <$> getAllConstraints) getConstraintsForProblem :: ProblemId -> TCM Constraints getConstraintsForProblem pid = List.filter (Set.member pid . constraintProblems) <$> getAllConstraints -- | Get the awake constraints getAwakeConstraints :: TCM Constraints getAwakeConstraints = use stAwakeConstraints wakeConstraints :: (ProblemConstraint-> TCM Bool) -> TCM () wakeConstraints wake = do c <- use stSleepingConstraints (wakeup, sleepin) <- partitionM wake c reportSLn "tc.constr.wake" 50 $ "waking up " ++ show (List.map (Set.toList . constraintProblems) wakeup) ++ "\n" ++ " still sleeping: " ++ show (List.map (Set.toList . constraintProblems) sleepin) modifySleepingConstraints $ const sleepin modifyAwakeConstraints (++ wakeup) -- danger... dropConstraints :: (ProblemConstraint -> Bool) -> TCM () dropConstraints crit = do let filt = List.filter $ not . crit modifySleepingConstraints filt modifyAwakeConstraints filt putConstraintsToSleep :: (ProblemConstraint -> Bool) -> TCM () putConstraintsToSleep sleepy = do awakeOnes <- use stAwakeConstraints let (gotoSleep, stayAwake) = List.partition sleepy awakeOnes modifySleepingConstraints $ (++ gotoSleep) modifyAwakeConstraints $ const stayAwake putAllConstraintsToSleep :: TCM () putAllConstraintsToSleep = putConstraintsToSleep (const True) data ConstraintStatus = AwakeConstraint | SleepingConstraint deriving (Eq, Show) -- | Suspend constraints matching the predicate during the execution of the -- second argument. Caution: held sleeping constraints will not be woken up -- by events that would normally trigger a wakeup call. holdConstraints :: (ConstraintStatus -> ProblemConstraint -> Bool) -> TCM a -> TCM a holdConstraints p m = do (holdAwake, stillAwake) <- List.partition (p AwakeConstraint) <$> use stAwakeConstraints (holdAsleep, stillAsleep) <- List.partition (p SleepingConstraint) <$> use stSleepingConstraints stAwakeConstraints .= stillAwake stSleepingConstraints .= stillAsleep let restore = do stAwakeConstraints %= (holdAwake ++) stSleepingConstraints %= (holdAsleep ++) catchError (m <* restore) (\ err -> restore *> throwError err) takeAwakeConstraint :: TCM (Maybe ProblemConstraint) takeAwakeConstraint = do cs <- getAwakeConstraints case cs of [] -> return Nothing c : cs -> do modifyAwakeConstraints $ const cs return $ Just c getAllConstraints :: TCM Constraints getAllConstraints = gets $ \s -> s^.stAwakeConstraints ++ s^.stSleepingConstraints withConstraint :: (Constraint -> TCM a) -> ProblemConstraint -> TCM a withConstraint f (PConstr pids c) = do -- We should preserve the problem stack and the isSolvingConstraint flag (pids', isSolving) <- asks $ envActiveProblems &&& envSolvingConstraints enterClosure c $ \c -> local (\e -> e { envActiveProblems = pids', envSolvingConstraints = isSolving }) $ solvingProblems pids (f c) buildProblemConstraint :: Set ProblemId -> Constraint -> TCM ProblemConstraint buildProblemConstraint pids c = PConstr pids <$> buildClosure c buildProblemConstraint_ :: Constraint -> TCM ProblemConstraint buildProblemConstraint_ = buildProblemConstraint Set.empty buildConstraint :: Constraint -> TCM ProblemConstraint buildConstraint c = flip buildProblemConstraint c =<< asks envActiveProblems -- | Add new a constraint addConstraint' :: Constraint -> TCM () addConstraint' = addConstraintTo stSleepingConstraints addAwakeConstraint' :: Constraint -> TCM () addAwakeConstraint' = addConstraintTo stAwakeConstraints addConstraintTo :: Lens' Constraints TCState -> Constraint -> TCM () addConstraintTo bucket c = do pc <- build stDirty .= True bucket %= (pc :) where build | isBlocking c = buildConstraint c | otherwise = buildProblemConstraint_ c isBlocking SortCmp{} = False isBlocking LevelCmp{} = False isBlocking ValueCmp{} = True isBlocking ElimCmp{} = True isBlocking TypeCmp{} = True isBlocking TelCmp{} = True isBlocking (Guarded c _) = isBlocking c isBlocking UnBlock{} = True isBlocking FindInScope{} = False isBlocking IsEmpty{} = True isBlocking CheckSizeLtSat{} = True -- | Add already awake constraints addAwakeConstraints :: Constraints -> TCM () addAwakeConstraints cs = modifyAwakeConstraints (cs ++) -- | Start solving constraints nowSolvingConstraints :: TCM a -> TCM a nowSolvingConstraints = local $ \e -> e { envSolvingConstraints = True } isSolvingConstraints :: TCM Bool isSolvingConstraints = asks envSolvingConstraints --------------------------------------------------------------------------- -- * Lenses --------------------------------------------------------------------------- mapAwakeConstraints :: (Constraints -> Constraints) -> TCState -> TCState mapAwakeConstraints = over stAwakeConstraints mapSleepingConstraints :: (Constraints -> Constraints) -> TCState -> TCState mapSleepingConstraints = over stSleepingConstraints modifyAwakeConstraints :: (Constraints -> Constraints) -> TCM () modifyAwakeConstraints = modify . mapAwakeConstraints modifySleepingConstraints :: (Constraints -> Constraints) -> TCM () modifySleepingConstraints = modify . mapSleepingConstraints Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Debug.hs-boot0000644000000000000000000000160713154613124021072 0ustar0000000000000000module Agda.TypeChecking.Monad.Debug where import Control.Applicative import Control.Monad.IO.Class (MonadIO) import Agda.TypeChecking.Monad.Base import {-# SOURCE #-} Agda.TypeChecking.Monad.Options import Agda.Utils.Pretty class (Functor m, Applicative m, Monad m) => MonadDebug m where displayDebugMessage :: Int -> String -> m () displayDebugMessage n s = traceDebugMessage n s $ return () traceDebugMessage :: Int -> String -> m a -> m a traceDebugMessage n s cont = displayDebugMessage n s >> cont formatDebugMessage :: VerboseKey -> Int -> TCM Doc -> m String instance (MonadIO m) => MonadDebug (TCMT m) reportS :: (HasOptions m, MonadDebug m) => VerboseKey -> Int -> String -> m () reportSLn :: (HasOptions m, MonadDebug m) => VerboseKey -> Int -> String -> m () reportSDoc :: (HasOptions m, MonadDebug m) => VerboseKey -> Int -> TCM Doc -> m () Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Signature.hs0000644000000000000000000013200313154613124021037 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} module Agda.TypeChecking.Monad.Signature where import Prelude hiding (null) import Control.Arrow (first, second, (***)) import Control.Applicative hiding (empty) import Control.Monad.State import Control.Monad.Reader import Control.Monad.Trans.Maybe import qualified Data.List as List import Data.Set (Set) import qualified Data.Set as Set import Data.Map (Map) import qualified Data.Map as Map import Data.Maybe import Data.Monoid import Agda.Syntax.Abstract.Name import Agda.Syntax.Abstract (Ren, ScopeCopyInfo(..)) import Agda.Syntax.Common import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Names import Agda.Syntax.Position import Agda.Syntax.Treeless (Compiled(..), TTerm) import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Context import Agda.TypeChecking.Monad.Options import Agda.TypeChecking.Monad.Env import Agda.TypeChecking.Monad.Mutual import Agda.TypeChecking.Monad.Open import Agda.TypeChecking.Monad.Local import Agda.TypeChecking.Monad.State import Agda.TypeChecking.Monad.Trace import Agda.TypeChecking.Warnings import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Substitute import {-# SOURCE #-} Agda.TypeChecking.Telescope import {-# SOURCE #-} Agda.TypeChecking.CompiledClause.Compile import {-# SOURCE #-} Agda.TypeChecking.Polarity import {-# SOURCE #-} Agda.TypeChecking.ProjectionLike import Agda.Utils.Except ( ExceptT ) import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Map as Map import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Pretty import Agda.Utils.Size import qualified Agda.Utils.HashMap as HMap #include "undefined.h" import Agda.Utils.Impossible -- | Add a constant to the signature. Lifts the definition to top level. addConstant :: QName -> Definition -> TCM () addConstant q d = do reportSLn "tc.signature" 20 $ "adding constant " ++ prettyShow q ++ " to signature" tel <- getContextTelescope let tel' = replaceEmptyName "r" $ killRange $ case theDef d of Constructor{} -> fmap hideOrKeepInstance tel Function{ funProjection = Just Projection{ projProper = Just{}, projIndex = n } } -> let fallback = fmap hideOrKeepInstance tel in if n > 0 then fallback else -- if the record value is part of the telescope, its hiding should left unchanged case initLast $ telToList tel of Nothing -> fallback Just (doms, dom) -> telFromList $ fmap hideOrKeepInstance doms ++ [dom] _ -> tel let d' = abstract tel' $ d { defName = q } reportSDoc "tc.signature" 60 $ return $ text "lambda-lifted definition =" pretty d' modifySignature $ updateDefinitions $ HMap.insertWith (+++) q d' i <- currentOrFreshMutualBlock setMutualBlock i q where new +++ old = new { defDisplay = defDisplay new ++ defDisplay old , defInstance = defInstance new `mplus` defInstance old } -- | Set termination info of a defined function symbol. setTerminates :: QName -> Bool -> TCM () setTerminates q b = modifySignature $ updateDefinition q $ updateTheDef $ \case def@Function{} -> def { funTerminates = Just b } def -> def -- | Modify the clauses of a function. modifyFunClauses :: QName -> ([Clause] -> [Clause]) -> TCM () modifyFunClauses q f = modifySignature $ updateDefinition q $ updateTheDef $ updateFunClauses f -- | Lifts clauses to the top-level and adds them to definition. -- Also adjusts the 'funCopatternLHS' field if necessary. addClauses :: QName -> [Clause] -> TCM () addClauses q cls = do tel <- getContextTelescope modifySignature $ updateDefinition q $ updateTheDef $ updateFunClauses (++ abstract tel cls) . updateFunCopatternLHS (|| isCopatternLHS cls) mkPragma :: String -> TCM CompilerPragma mkPragma s = CompilerPragma <$> getCurrentRange <*> pure s -- | Add a compiler pragma `{-# COMPILE #-}` addPragma :: BackendName -> QName -> String -> TCM () addPragma b q s = modifySignature . updateDefinition q . addCompilerPragma b =<< mkPragma s -- ** Temporary ** -- The functions below are only needed while we still parse the old COMPILED -- pragmas. type HaskellCode = String type HaskellType = String type JSCode = String type CoreCode = String addDeprecatedPragma :: String -> BackendName -> QName -> String -> TCM () addDeprecatedPragma old b q s = do let pq = prettyShow $ nameConcrete $ qnameName q warning $ DeprecationWarning (unwords ["The", old, "pragma"]) (unwords ["{-# COMPILE", b, pq, s, "#-}"]) "2.6" addPragma b q s dataFormat :: String -> [String] -> String dataFormat ty cons = "= data " ++ ty ++ " (" ++ List.intercalate " | " cons ++ ")" addHaskellCode :: QName -> HaskellCode -> TCM () addHaskellCode q hsCode = addDeprecatedPragma "COMPILED" ghcBackendName q $ "= " ++ hsCode addHaskellExport :: QName -> String -> TCM () addHaskellExport q hsName = addDeprecatedPragma "COMPILED_EXPORT" ghcBackendName q $ "as " ++ hsName addHaskellType :: QName -> HaskellType -> TCM () addHaskellType q hsTy = addDeprecatedPragma "COMPILED_TYPE" ghcBackendName q $ "= type " ++ hsTy addHaskellData :: QName -> HaskellType -> [HaskellCode] -> TCM () addHaskellData q hsTy hsCons = addDeprecatedPragma "COMPILED_DATA" ghcBackendName q $ dataFormat hsTy hsCons addJSCode :: QName -> JSCode -> TCM () addJSCode q jsDef = addDeprecatedPragma "COMPILED_JS" jsBackendName q ("= " ++ jsDef) addCoreCode :: QName -> CoreCode -> TCM () addCoreCode q crDef = addDeprecatedPragma "COMPILED_UHC" uhcBackendName q $ "= " ++ crDef addCoreType :: QName -> CoreCode -> [CoreCode] -> TCM () addCoreType q crTy crCons = addDeprecatedPragma "COMPILED_DATA_UHC" uhcBackendName q $ dataFormat crTy crCons -- ** End of temporary functions ** getUniqueCompilerPragma :: BackendName -> QName -> TCM (Maybe CompilerPragma) getUniqueCompilerPragma backend q = do ps <- defCompilerPragmas backend <$> getConstInfo q case ps of [] -> return Nothing [p] -> return $ Just p _ -> setCurrentRange (ps !! 1) $ genericDocError $ hang (text ("Conflicting " ++ backend ++ " pragmas for") <+> pretty q <+> text "at") 2 $ vcat [ text "-" <+> pretty (getRange p) | p <- ps ] setFunctionFlag :: FunctionFlag -> Bool -> QName -> TCM () setFunctionFlag flag val q = modifyGlobalDefinition q $ set (theDefLens . funFlag flag) val markStatic :: QName -> TCM () markStatic = setFunctionFlag FunStatic True markInline :: QName -> TCM () markInline = setFunctionFlag FunInline True markInjective :: QName -> TCM () markInjective q = modifyGlobalDefinition q $ \def -> def { defInjective = True } unionSignatures :: [Signature] -> Signature unionSignatures ss = foldr unionSignature emptySignature ss where unionSignature (Sig a b c) (Sig a' b' c') = Sig (Map.union a a') (HMap.union b b') -- definitions are unique (in at most one module) (HMap.unionWith mappend c c') -- rewrite rules are accumulated -- | Add a section to the signature. -- -- The current context will be stored as the cumulative module parameters -- for this section. addSection :: ModuleName -> TCM () addSection m = do tel <- getContextTelescope let sec = Section tel -- Make sure we do not overwrite an existing section! whenJustM (getSection m) $ \ sec' -> do -- At least not with different content! if (sec == sec') then do -- Andreas, 2015-12-02: test/Succeed/Issue1701II.agda -- reports a "redundantly adding existing section". reportSLn "tc.section" 10 $ "warning: redundantly adding existing section " ++ prettyShow m reportSLn "tc.section" 60 $ "with content " ++ prettyShow sec else do reportSLn "impossible" 10 $ "overwriting existing section " ++ prettyShow m reportSLn "impossible" 60 $ "of content " ++ prettyShow sec' reportSLn "impossible" 60 $ "with content " ++ prettyShow sec __IMPOSSIBLE__ -- Add the new section. setDefaultModuleParameters m modifySignature $ over sigSections $ Map.insert m sec setDefaultModuleParameters :: ModuleName -> TCM () setDefaultModuleParameters m = stModuleParameters %= Map.insert m defaultModuleParameters -- | Get a section. -- -- Why Maybe? The reason is that we look up all prefixes of a module to -- compute number of parameters, and for hierarchical top-level modules, -- A.B.C say, A and A.B do not exist. {-# SPECIALIZE getSection :: ModuleName -> TCM (Maybe Section) #-} {-# SPECIALIZE getSection :: ModuleName -> ReduceM (Maybe Section) #-} getSection :: (Functor m, ReadTCState m) => ModuleName -> m (Maybe Section) getSection m = do sig <- (^. stSignature . sigSections) <$> getTCState isig <- (^. stImports . sigSections) <$> getTCState return $ Map.lookup m sig `mplus` Map.lookup m isig -- | Lookup a section telescope. -- -- If it doesn't exist, like in hierarchical top-level modules, -- the section telescope is empty. {-# SPECIALIZE lookupSection :: ModuleName -> TCM Telescope #-} {-# SPECIALIZE lookupSection :: ModuleName -> ReduceM Telescope #-} lookupSection :: (Functor m, ReadTCState m) => ModuleName -> m Telescope lookupSection m = maybe EmptyTel (^. secTelescope) <$> getSection m -- Add display forms to all names @xn@ such that @x = x1 es1@, ... @xn-1 = xn esn@. addDisplayForms :: QName -> TCM () addDisplayForms x = do def <- getConstInfo x args <- drop (projectionArgs $ theDef def) <$> getContextArgs add args x x $ map Apply $ raise 1 args -- make room for the single match variable of the display form where add args top x es0 = do def <- getConstInfo x let cs = defClauses def isCopy = defCopy def case cs of [ cl ] -> do if not isCopy then noDispForm x "not a copy" else do if not $ all (isVar . namedArg) $ namedClausePats cl then noDispForm x "properly matching patterns" else do -- We have -- x ps = e -- and we're trying to generate a display form -- x es0 <-- e[es0/ps] -- Of course x es0 might be an over- or underapplication, hence the -- n/m arithmetic. let n = size $ namedClausePats cl (es1, es2) = splitAt n es0 m = n - size es1 vs1 = map unArg $ fromMaybe __IMPOSSIBLE__ $ allApplyElims es1 sub = parallelS $ reverse $ vs1 ++ replicate m (var 0) body = applySubst sub (compiledClauseBody cl) `applyE` es2 case unSpine <$> body of Just (Def y es) -> do let df = Display m es $ DTerm $ Def top $ map Apply args reportSLn "tc.display.section" 20 $ unlines [ "adding display form " ++ prettyShow y ++ " --> " ++ prettyShow top , show df ] addDisplayForm y df add args top y es Just v -> noDispForm x $ "not a def body, but " ++ show v Nothing -> noDispForm x $ "bad body" [] | Constructor{ conSrcCon = h } <- theDef def -> do let y = conName h df = Display 0 [] $ DTerm $ Con (h {conName = top }) ConOSystem [] reportSLn "tc.display.section" 20 $ unlines [ "adding display form " ++ prettyShow y ++ " --> " ++ prettyShow top , show df ] addDisplayForm y df [] -> noDispForm x "no clauses" (_:_:_) -> noDispForm x "many clauses" noDispForm x reason = reportSLn "tc.display.section" 30 $ "no display form from " ++ prettyShow x ++ " because " ++ reason isVar VarP{} = True isVar _ = False -- | Module application (followed by module parameter abstraction). applySection :: ModuleName -- ^ Name of new module defined by the module macro. -> Telescope -- ^ Parameters of new module. -> ModuleName -- ^ Name of old module applied to arguments. -> Args -- ^ Arguments of module application. -> ScopeCopyInfo -- ^ Imported names and modules -> TCM () applySection new ptel old ts ScopeCopyInfo{ renModules = rm, renNames = rd } = do rd <- closeConstructors rd applySection' new ptel old ts ScopeCopyInfo{ renModules = rm, renNames = rd } where -- If a datatype is being copied, all its constructors need to be copied, -- and if a constructor is copied its datatype needs to be. closeConstructors :: Ren QName -> TCM (Ren QName) closeConstructors rd = do ds <- List.nub . concat <$> mapM (constructorData . fst) rd cs <- List.nub . concat <$> mapM (dataConstructors . fst) rd new <- concat <$> mapM rename (ds ++ cs) reportSLn "tc.mod.apply.complete" 30 $ "also copying: " ++ prettyShow new return $ new ++ rd where rename :: QName -> TCM (Ren QName) rename x = case lookup x rd of Nothing -> do y <- freshName_ (prettyShow x) return [(x, qnameFromList [y])] Just{} -> return [] constructorData :: QName -> TCM [QName] constructorData x = do def <- theDef <$> getConstInfo x return $ case def of Constructor{ conData = d } -> [d] _ -> [] dataConstructors :: QName -> TCM [QName] dataConstructors x = do def <- theDef <$> getConstInfo x return $ case def of Datatype{ dataCons = cs } -> cs Record{ recConHead = h } -> [conName h] _ -> [] applySection' :: ModuleName -> Telescope -> ModuleName -> Args -> ScopeCopyInfo -> TCM () applySection' new ptel old ts ScopeCopyInfo{ renNames = rd, renModules = rm } = do reportSLn "tc.mod.apply" 10 $ render $ vcat [ text "applySection" , text "new =" <+> pretty new , text "ptel =" <+> pretty ptel , text "old =" <+> pretty old , text "ts =" <+> pretty ts ] mapM_ (copyDef ts) rd mapM_ (copySec ts) rm computePolarity (map snd rd) where -- Andreas, 2013-10-29 -- Here, if the name x is not imported, it persists as -- old, possibly out-of-scope name. -- This old name may used by the case split tactic, leading to -- names that cannot be printed properly. -- I guess it would make sense to mark non-imported names -- as such (out-of-scope) and let splitting fail if it would -- produce out-of-scope constructors. copyName x = fromMaybe x $ lookup x rd argsToUse x = do let m = commonParentModule old x reportSLn "tc.mod.apply" 80 $ "Common prefix: " ++ prettyShow m size <$> lookupSection m copyDef :: Args -> (QName, QName) -> TCM () copyDef ts (x, y) = do def <- getConstInfo x np <- argsToUse (qnameModule x) copyDef' np def where copyDef' np d = do reportSLn "tc.mod.apply" 60 $ "making new def for " ++ prettyShow y ++ " from " ++ prettyShow x ++ " with " ++ show np ++ " args " ++ show (defAbstract d) reportSLn "tc.mod.apply" 80 $ "args = " ++ show ts' ++ "\n" ++ "old type = " ++ prettyShow (defType d) reportSLn "tc.mod.apply" 80 $ "new type = " ++ prettyShow t addConstant y =<< nd y makeProjection y -- Issue1238: the copied def should be an 'instance' if the original -- def is one. Skip constructors since the original constructor will -- still work as an instance. unless isCon $ whenJust inst $ \ c -> addNamedInstance y c -- Set display form for the old name if it's not a constructor. {- BREAKS fail/Issue478 -- Andreas, 2012-10-20 and if we are not an anonymous module -- unless (isAnonymousModuleName new || isCon || size ptel > 0) $ do -} -- BREAKS fail/Issue1643a -- -- Andreas, 2015-09-09 Issue 1643: -- -- Do not add a display form for a bare module alias. -- when (not isCon && size ptel == 0 && not (null ts)) $ do when (size ptel == 0) $ do addDisplayForms y where ts' = take np ts t = defType d `piApply` ts' pol = defPolarity d `apply` ts' occ = defArgOccurrences d `apply` ts' inst = defInstance d -- the name is set by the addConstant function nd :: QName -> TCM Definition nd y = for def $ \ df -> Defn { defArgInfo = defArgInfo d , defName = y , defType = t , defPolarity = pol , defArgOccurrences = occ , defDisplay = [] , defMutual = -1 -- TODO: mutual block? , defCompiledRep = noCompiledRep , defInstance = inst , defCopy = True , defMatchable = False , defInjective = False , theDef = df } oldDef = theDef d isCon = case oldDef of { Constructor{} -> True ; _ -> False } mutual = case oldDef of { Function{funMutual = m} -> m ; _ -> Nothing } extlam = case oldDef of { Function{funExtLam = e} -> e ; _ -> Nothing } with = case oldDef of { Function{funWith = w} -> copyName <$> w ; _ -> Nothing } -- Andreas, 2015-05-11, to fix issue 1413: -- Even if we apply the record argument (must be @var 0@), we stay a projection. -- This is because we may abstract the record argument later again. -- See succeed/ProjectionNotNormalized.agda isVar0 t = case ignoreSharing $ unArg t of Var 0 [] -> True; _ -> False proj = case oldDef of Function{funProjection = Just p@Projection{projIndex = n}} | size ts' < n || (size ts' == n && maybe True isVar0 (lastMaybe ts')) -> Just $ p { projIndex = n - size ts' , projLams = projLams p `apply` ts' , projProper= fmap copyName $ projProper p } _ -> Nothing def = case oldDef of Constructor{ conPars = np, conData = d } -> return $ oldDef { conPars = np - size ts' , conData = copyName d } Datatype{ dataPars = np, dataCons = cs } -> return $ oldDef { dataPars = np - size ts' , dataClause = Just cl , dataCons = map copyName cs } Record{ recPars = np, recTel = tel } -> return $ oldDef { recPars = np - size ts' , recClause = Just cl , recTel = apply tel ts' } _ -> do cc <- compileClauses Nothing [cl] -- Andreas, 2012-10-07 non need for record pattern translation let newDef = set funMacro (oldDef ^. funMacro) $ set funStatic (oldDef ^. funStatic) $ set funInline True $ emptyFunction { funClauses = [cl] , funCompiled = Just cc , funMutual = mutual , funProjection = proj , funTerminates = Just True , funExtLam = extlam , funWith = with , funCopatternLHS = isCopatternLHS [cl] } reportSDoc "tc.mod.apply" 80 $ return $ (text "new def for" <+> pretty x) pretty newDef return newDef cl = Clause { clauseLHSRange = getRange $ defClauses d , clauseFullRange = getRange $ defClauses d , clauseTel = EmptyTel , namedClausePats = [] , clauseBody = Just $ case oldDef of Function{funProjection = Just p} -> projDropParsApply p ProjSystem ts' _ -> Def x $ map Apply ts' , clauseType = Just $ defaultArg t , clauseCatchall = False , clauseUnreachable = Just False -- definitely not unreachable } {- Example module Top Θ where module A Γ where module M Φ where module B Δ where module N Ψ where module O Ψ' where open A public -- introduces only M --> A.M into the *scope* module C Ξ = Top.B ts new section C tel = Ξ.(Θ.Δ)[ts] calls 1. copySec ts (Top.A.M, C.M) 2. copySec ts (Top.B.N, C.N) 3. copySec ts (Top.B.N.O, C.N.O) with old = Top.B For 1. Common prefix is: Top totalArgs = |Θ| (section Top) tel = Θ.Γ.Φ (section Top.A.M) ts' = take totalArgs ts Θ₂ = drop totalArgs Θ new section C.M tel = Θ₂.Γ.Φ[ts'] -} copySec :: Args -> (ModuleName, ModuleName) -> TCM () copySec ts (x, y) = do totalArgs <- argsToUse x tel <- lookupSection x let sectionTel = apply tel $ take totalArgs ts reportSLn "tc.mod.apply" 80 $ "Copying section " ++ prettyShow x ++ " to " ++ prettyShow y reportSLn "tc.mod.apply" 80 $ " ts = " ++ List.intercalate "; " (map prettyShow ts) reportSLn "tc.mod.apply" 80 $ " totalArgs = " ++ show totalArgs reportSLn "tc.mod.apply" 80 $ " tel = " ++ List.intercalate " " (map (fst . unDom) $ telToList tel) -- only names reportSLn "tc.mod.apply" 80 $ " sectionTel = " ++ List.intercalate " " (map (fst . unDom) $ telToList ptel) -- only names addContext sectionTel $ addSection y -- | Add a display form to a definition (could be in this or imported signature). addDisplayForm :: QName -> DisplayForm -> TCM () addDisplayForm x df = do d <- makeLocal df let add = updateDefinition x $ \ def -> def{ defDisplay = d : defDisplay def } ifM (isLocal x) {-then-} (modifySignature add) {-else-} (stImportsDisplayForms %= HMap.insertWith (++) x [d]) whenM (hasLoopingDisplayForm x) $ typeError . GenericDocError $ text "Cannot add recursive display form for" <+> pretty x isLocal :: QName -> TCM Bool isLocal x = HMap.member x <$> use (stSignature . sigDefinitions) getDisplayForms :: QName -> TCM [LocalDisplayForm] getDisplayForms q = do ds <- either (const []) defDisplay <$> getConstInfo' q ds1 <- HMap.lookupDefault [] q <$> use stImportsDisplayForms ds2 <- HMap.lookupDefault [] q <$> use stImportedDisplayForms ifM (isLocal q) (return $ ds ++ ds1 ++ ds2) (return $ ds1 ++ ds ++ ds2) -- | Find all names used (recursively) by display forms of a given name. chaseDisplayForms :: QName -> TCM (Set QName) chaseDisplayForms q = go Set.empty [q] where go used [] = pure used go used (q : qs) = do let rhs (Display _ _ e) = e -- Only look at names in the right-hand side (#1870) ds <- (`Set.difference` used) . Set.unions . map (namesIn . rhs . dget) <$> (getDisplayForms q `catchError_` \ _ -> pure []) -- might be a pattern synonym go (Set.union ds used) (Set.toList ds ++ qs) -- | Check if a display form is looping. hasLoopingDisplayForm :: QName -> TCM Bool hasLoopingDisplayForm q = Set.member q <$> chaseDisplayForms q canonicalName :: QName -> TCM QName canonicalName x = do def <- theDef <$> getConstInfo x case def of Constructor{conSrcCon = c} -> return $ conName c Record{recClause = Just (Clause{ clauseBody = body })} -> can body Datatype{dataClause = Just (Clause{ clauseBody = body })} -> can body _ -> return x where can body = canonicalName $ extract $ fromMaybe __IMPOSSIBLE__ body extract (Def x _) = x extract (Shared p) = extract $ derefPtr p extract _ = __IMPOSSIBLE__ sameDef :: QName -> QName -> TCM (Maybe QName) sameDef d1 d2 = do c1 <- canonicalName d1 c2 <- canonicalName d2 if (c1 == c2) then return $ Just c1 else return Nothing -- | Can be called on either a (co)datatype, a record type or a -- (co)constructor. whatInduction :: MonadTCM tcm => QName -> tcm Induction whatInduction c = liftTCM $ do def <- theDef <$> getConstInfo c case def of Datatype{ dataInduction = i } -> return i Record{} | not (recRecursive def) -> return Inductive Record{ recInduction = i } -> return $ fromMaybe Inductive i Constructor{ conInd = i } -> return i _ -> __IMPOSSIBLE__ -- | Does the given constructor come from a single-constructor type? -- -- Precondition: The name has to refer to a constructor. singleConstructorType :: QName -> TCM Bool singleConstructorType q = do d <- theDef <$> getConstInfo q case d of Record {} -> return True Constructor { conData = d } -> do di <- theDef <$> getConstInfo d return $ case di of Record {} -> True Datatype { dataCons = cs } -> length cs == 1 _ -> __IMPOSSIBLE__ _ -> __IMPOSSIBLE__ -- | Signature lookup errors. data SigError = SigUnknown String -- ^ The name is not in the signature; default error message. | SigAbstract -- ^ The name is not available, since it is abstract. -- | Standard eliminator for 'SigError'. sigError :: (String -> a) -> a -> SigError -> a sigError f a = \case SigUnknown s -> f s SigAbstract -> a class (Functor m, Applicative m, Monad m, HasOptions m, MonadDebug m) => HasConstInfo m where -- | Lookup the definition of a name. The result is a closed thing, all free -- variables have been abstracted over. getConstInfo :: QName -> m Definition getConstInfo q = getConstInfo' q >>= \case Right d -> return d Left (SigUnknown err) -> __IMPOSSIBLE_VERBOSE__ err Left SigAbstract -> __IMPOSSIBLE_VERBOSE__ $ "Abstract, thus, not in scope: " ++ prettyShow q -- | Version that reports exceptions: getConstInfo' :: QName -> m (Either SigError Definition) getConstInfo' q = Right <$> getConstInfo q -- | Lookup the rewrite rules with the given head symbol. getRewriteRulesFor :: QName -> m RewriteRules {-# SPECIALIZE getConstInfo :: QName -> TCM Definition #-} defaultGetRewriteRulesFor :: (Monad m) => m TCState -> QName -> m RewriteRules defaultGetRewriteRulesFor getTCState q = do st <- getTCState let sig = st^.stSignature imp = st^.stImports look s = HMap.lookup q $ s ^. sigRewriteRules return $ mconcat $ catMaybes [look sig, look imp] -- | Get the original name of the projection -- (the current one could be from a module application). getOriginalProjection :: HasConstInfo m => QName -> m QName getOriginalProjection q = projOrig . fromMaybe __IMPOSSIBLE__ <$> isProjection q instance HasConstInfo (TCMT IO) where getRewriteRulesFor = defaultGetRewriteRulesFor get getConstInfo' q = do st <- get env <- ask defaultGetConstInfo st env q getConstInfo q = getConstInfo' q >>= \case Right d -> return d Left (SigUnknown err) -> fail err Left SigAbstract -> notInScope $ qnameToConcrete q defaultGetConstInfo :: (HasOptions m, MonadDebug m) => TCState -> TCEnv -> QName -> m (Either SigError Definition) defaultGetConstInfo st env q = do let defs = st^.(stSignature . sigDefinitions) idefs = st^.(stImports . sigDefinitions) case catMaybes [HMap.lookup q defs, HMap.lookup q idefs] of [] -> return $ Left $ SigUnknown $ "Unbound name: " ++ prettyShow q ++ " " ++ showQNameId q [d] -> mkAbs env d ds -> __IMPOSSIBLE_VERBOSE__ $ "Ambiguous name: " ++ prettyShow q where mkAbs env d | treatAbstractly' q' env = case makeAbstract d of Just d -> return $ Right d Nothing -> return $ Left SigAbstract -- the above can happen since the scope checker is a bit sloppy with 'abstract' | otherwise = return $ Right d where q' = case theDef d of -- Hack to make abstract constructors work properly. The constructors -- live in a module with the same name as the datatype, but for 'abstract' -- purposes they're considered to be in the same module as the datatype. Constructor{} -> dropLastModule q _ -> q dropLastModule q@QName{ qnameModule = m } = q{ qnameModule = mnameFromList $ ifNull (mnameToList m) __IMPOSSIBLE__ init } instance HasConstInfo m => HasConstInfo (MaybeT m) where getConstInfo' = lift . getConstInfo' getRewriteRulesFor = lift . getRewriteRulesFor instance HasConstInfo m => HasConstInfo (ExceptT err m) where getConstInfo' = lift . getConstInfo' getRewriteRulesFor = lift . getRewriteRulesFor {-# INLINE getConInfo #-} getConInfo :: MonadTCM tcm => ConHead -> tcm Definition getConInfo = liftTCM . getConstInfo . conName -- | Look up the polarity of a definition. getPolarity :: QName -> TCM [Polarity] getPolarity q = defPolarity <$> getConstInfo q -- | Look up polarity of a definition and compose with polarity -- represented by 'Comparison'. getPolarity' :: Comparison -> QName -> TCM [Polarity] getPolarity' CmpEq q = map (composePol Invariant) <$> getPolarity q -- return [] getPolarity' CmpLeq q = getPolarity q -- composition with Covariant is identity -- | Set the polarity of a definition. setPolarity :: QName -> [Polarity] -> TCM () setPolarity q pol = do reportSLn "tc.polarity.set" 20 $ "Setting polarity of " ++ prettyShow q ++ " to " ++ prettyShow pol ++ "." modifySignature $ updateDefinition q $ updateDefPolarity $ const pol -- | Get argument occurrence info for argument @i@ of definition @d@ (never fails). getArgOccurrence :: QName -> Nat -> TCM Occurrence getArgOccurrence d i = do def <- getConstInfo d return $! case theDef def of Constructor{} -> StrictPos _ -> fromMaybe Mixed $ defArgOccurrences def !!! i -- | Sets the 'defArgOccurrences' for the given identifier (which -- should already exist in the signature). setArgOccurrences :: QName -> [Occurrence] -> TCM () setArgOccurrences d os = modifyArgOccurrences d $ const os modifyArgOccurrences :: QName -> ([Occurrence] -> [Occurrence]) -> TCM () modifyArgOccurrences d f = modifySignature $ updateDefinition d $ updateDefArgOccurrences f setTreeless :: QName -> TTerm -> TCM () setTreeless q t = modifyGlobalDefinition q $ updateTheDef $ \case fun@Function{} -> fun{ funTreeless = Just $ Compiled t [] } _ -> __IMPOSSIBLE__ setCompiledArgUse :: QName -> [Bool] -> TCM () setCompiledArgUse q use = modifyGlobalDefinition q $ updateTheDef $ \case fun@Function{} -> fun{ funTreeless = for (funTreeless fun) $ \ c -> c { cArgUsage = use } } _ -> __IMPOSSIBLE__ getCompiled :: QName -> TCM (Maybe Compiled) getCompiled q = do (theDef <$> getConstInfo q) <&> \case Function{ funTreeless = t } -> t _ -> Nothing getErasedConArgs :: QName -> TCM [Bool] getErasedConArgs q = do def <- getConstInfo q case theDef def of Constructor{ conData = d, conPars = np, conErased = es } -> return es _ -> __IMPOSSIBLE__ setErasedConArgs :: QName -> [Bool] -> TCM () setErasedConArgs q args = modifyGlobalDefinition q $ updateTheDef $ \case def@Constructor{} -> def{ conErased = args } def -> def -- no-op for non-constructors getTreeless :: QName -> TCM (Maybe TTerm) getTreeless q = fmap cTreeless <$> getCompiled q getCompiledArgUse :: QName -> TCM [Bool] getCompiledArgUse q = maybe [] cArgUsage <$> getCompiled q -- | Get the mutually recursive identifiers of a symbol from the signature. getMutual :: QName -> TCM (Maybe [QName]) getMutual d = getMutual_ . theDef <$> getConstInfo d -- | Get the mutually recursive identifiers from a `Definition`. getMutual_ :: Defn -> Maybe [QName] getMutual_ = \case Function { funMutual = m } -> m Datatype { dataMutual = m } -> m Record { recMutual = m } -> m _ -> Nothing -- | Set the mutually recursive identifiers. setMutual :: QName -> [QName] -> TCM () setMutual d m = modifySignature $ updateDefinition d $ updateTheDef $ \ def -> case def of Function{} -> def { funMutual = Just m } Datatype{} -> def {dataMutual = Just m } Record{} -> def { recMutual = Just m } _ -> if null m then def else __IMPOSSIBLE__ -- nothing to do -- | Check whether two definitions are mutually recursive. mutuallyRecursive :: QName -> QName -> TCM Bool mutuallyRecursive d d1 = (d `elem`) . fromMaybe __IMPOSSIBLE__ <$> getMutual d1 -- | A function/data/record definition is nonRecursive if it is not even mutually -- recursive with itself. definitelyNonRecursive_ :: Defn -> Bool definitelyNonRecursive_ = maybe False null . getMutual_ -- | Get the number of parameters to the current module. getCurrentModuleFreeVars :: TCM Nat getCurrentModuleFreeVars = size <$> (lookupSection =<< currentModule) -- | Compute the number of free variables of a defined name. This is the sum of -- number of parameters shared with the current module and the number of -- anonymous variables (if the name comes from a let-bound module). getDefFreeVars :: (Functor m, Applicative m, ReadTCState m, MonadReader TCEnv m) => QName -> m Nat getDefFreeVars = getModuleFreeVars . qnameModule freeVarsToApply :: (Functor m, HasConstInfo m, HasOptions m, ReadTCState m, MonadReader TCEnv m, MonadDebug m) => QName -> m Args freeVarsToApply q = do vs <- moduleParamsToApply $ qnameModule q t <- defType <$> getConstInfo q let TelV tel _ = telView'UpTo (size vs) t unless (size tel == size vs) __IMPOSSIBLE__ return $ zipWith (\ (Arg _ v) (Dom ai _) -> Arg ai v) vs $ telToList tel {-# SPECIALIZE getModuleFreeVars :: ModuleName -> TCM Nat #-} {-# SPECIALIZE getModuleFreeVars :: ModuleName -> ReduceM Nat #-} getModuleFreeVars :: (Functor m, Applicative m, MonadReader TCEnv m, ReadTCState m) => ModuleName -> m Nat getModuleFreeVars m = do m0 <- commonParentModule m <$> currentModule (+) <$> getAnonymousVariables m <*> (size <$> lookupSection m0) -- | Compute the context variables to apply a definition to. -- -- We have to insert the module telescope of the common prefix -- of the current module and the module where the definition comes from. -- (Properly raised to the current context.) --y -- Example: -- @ -- module M₁ Γ where -- module M₁ Δ where -- f = ... -- module M₃ Θ where -- ... M₁.M₂.f [insert Γ raised by Θ] -- @ moduleParamsToApply :: (Functor m, Applicative m, HasOptions m, MonadReader TCEnv m, ReadTCState m, MonadDebug m) => ModuleName -> m Args moduleParamsToApply m = do -- Get the correct number of free variables (correctly raised) of @m@. reportSLn "tc.sig.param" 90 $ "computing module parameters of " ++ prettyShow m n <- getModuleFreeVars m tel <- take n . telToList <$> lookupSection m sub <- getModuleParameterSub m verboseS "tc.sig.param" 60 $ do cxt <- getContext reportSLn "tc.sig.param" 60 $ unlines $ [ " n = " ++ show n , " cxt = " ++ show (map (fmap fst) cxt) , " sub = " ++ show sub ] unless (size tel == n) __IMPOSSIBLE__ let args = applySubst sub $ zipWith (\ i a -> var i <$ argFromDom a) (downFrom n) tel reportSLn "tc.sig.param" 60 $ " args = " ++ show args -- Apply the original ArgInfo, as the hiding information in the current -- context might be different from the hiding information expected by @m@. getSection m >>= \case Nothing -> do -- We have no section for @m@. -- This should only happen for toplevel definitions, and then there -- are no free vars to apply, or? -- unless (null args) __IMPOSSIBLE__ -- No, this invariant is violated by private modules, see Issue1701a. return args Just (Section stel) -> do -- The section telescope of @m@ should be as least -- as long as the number of free vars @m@ is applied to. -- We still check here as in no case, we want @zipWith@ to silently -- drop some @args@. -- And there are also anonymous modules, thus, the invariant is not trivial. when (size stel < size args) __IMPOSSIBLE__ return $ zipWith (\ (Dom ai _) (Arg _ v) -> Arg ai v) (telToList stel) args -- | Unless all variables in the context are module parameters, create a fresh -- module to capture the non-module parameters. Used when unquoting to make -- sure generated definitions work properly. inFreshModuleIfFreeParams :: TCM a -> TCM a inFreshModuleIfFreeParams k = do sub <- getModuleParameterSub =<< currentModule if sub == IdS then k else do m <- currentModule m' <- qualifyM m . mnameFromList . (:[]) <$> freshName_ "_" addSection m' withCurrentModule m' k -- | Instantiate a closed definition with the correct part of the current -- context. instantiateDef :: Definition -> TCM Definition instantiateDef d = do vs <- freeVarsToApply $ defName d verboseS "tc.sig.inst" 30 $ do ctx <- getContext m <- currentModule reportSLn "tc.sig.inst" 30 $ "instDef in " ++ prettyShow m ++ ": " ++ prettyShow (defName d) ++ " " ++ unwords (map show $ zipWith (<$) (reverse $ map (fst . unDom) ctx) vs) return $ d `apply` vs instantiateRewriteRule :: (Functor m, HasConstInfo m, HasOptions m, ReadTCState m, MonadReader TCEnv m, MonadDebug m) => RewriteRule -> m RewriteRule instantiateRewriteRule rew = do traceSLn "rewriting" 60 ("instantiating rewrite rule " ++ show (rewName rew) ++ " to the local context.") $ do vs <- freeVarsToApply $ rewName rew let rew' = rew `apply` vs traceSLn "rewriting" 60 ("instantiated rewrite rule: ") $ do traceSLn "rewriting" 60 (show rew') $ do return rew' instantiateRewriteRules :: (Functor m, HasConstInfo m, HasOptions m, ReadTCState m, MonadReader TCEnv m, MonadDebug m) => RewriteRules -> m RewriteRules instantiateRewriteRules = mapM instantiateRewriteRule -- | Give the abstract view of a definition. makeAbstract :: Definition -> Maybe Definition makeAbstract d = case defAbstract d of ConcreteDef -> return d AbstractDef -> do def <- makeAbs $ theDef d return d { defArgOccurrences = [] -- no positivity info for abstract things! , defPolarity = [] -- no polarity info for abstract things! , theDef = def } where makeAbs Axiom = Just Axiom makeAbs d@Datatype {} = Just $ AbstractDefn d makeAbs d@Function {} = Just $ AbstractDefn d makeAbs Constructor{} = Nothing -- Andreas, 2012-11-18: Make record constructor and projections abstract. -- Andreas, 2017-08-14: Projections are actually not abstract (issue #2682). -- Return the Defn under a wrapper to allow e.g. eligibleForProjectionLike -- to see whether the abstract thing is a record type or not. makeAbs d@Record{} = Just $ AbstractDefn d makeAbs Primitive{} = __IMPOSSIBLE__ makeAbs AbstractDefn{}= __IMPOSSIBLE__ -- | Enter abstract mode. Abstract definition in the current module are transparent. {-# SPECIALIZE inAbstractMode :: TCM a -> TCM a #-} inAbstractMode :: MonadReader TCEnv m => m a -> m a inAbstractMode = local $ \e -> e { envAbstractMode = AbstractMode, envAllowDestructiveUpdate = False } -- Allowing destructive updates when seeing through -- abstract may break the abstraction. -- | Not in abstract mode. All abstract definitions are opaque. {-# SPECIALIZE inConcreteMode :: TCM a -> TCM a #-} inConcreteMode :: MonadReader TCEnv m => m a -> m a inConcreteMode = local $ \e -> e { envAbstractMode = ConcreteMode } -- | Ignore abstract mode. All abstract definitions are transparent. ignoreAbstractMode :: MonadReader TCEnv m => m a -> m a ignoreAbstractMode = local $ \e -> e { envAbstractMode = IgnoreAbstractMode, envAllowDestructiveUpdate = False } -- Allowing destructive updates when ignoring -- abstract may break the abstraction. -- | Enter concrete or abstract mode depending on whether the given identifier -- is concrete or abstract. {-# SPECIALIZE inConcreteOrAbstractMode :: QName -> (Definition -> TCM a) -> TCM a #-} inConcreteOrAbstractMode :: (MonadReader TCEnv m, HasConstInfo m) => QName -> (Definition -> m a) -> m a inConcreteOrAbstractMode q cont = do -- Andreas, 2015-07-01: If we do not ignoreAbstractMode here, -- we will get ConcreteDef for abstract things, as they are turned into axioms. def <- ignoreAbstractMode $ getConstInfo q case defAbstract def of AbstractDef -> inAbstractMode $ cont def ConcreteDef -> inConcreteMode $ cont def -- | Check whether a name might have to be treated abstractly (either if we're -- 'inAbstractMode' or it's not a local name). Returns true for things not -- declared abstract as well, but for those 'makeAbstract' will have no effect. treatAbstractly :: MonadReader TCEnv m => QName -> m Bool treatAbstractly q = asks $ treatAbstractly' q -- | Andreas, 2015-07-01: -- If the @current@ module is a weak suffix of the identifier module, -- we can see through its abstract definition if we are abstract. -- (Then @treatAbstractly'@ returns @False@). -- -- If I am not mistaken, then we cannot see definitions in the @where@ -- block of an abstract function from the perspective of the function, -- because then the current module is a strict prefix of the module -- of the local identifier. -- This problem is fixed by removing trailing anonymous module name parts -- (underscores) from both names. treatAbstractly' :: QName -> TCEnv -> Bool treatAbstractly' q env = case envAbstractMode env of ConcreteMode -> True IgnoreAbstractMode -> False AbstractMode -> not $ current == m || current `isSubModuleOf` m where current = dropAnon $ envCurrentModule env m = dropAnon $ qnameModule q dropAnon (MName ms) = MName $ reverse $ dropWhile isNoName $ reverse ms -- | Get type of a constant, instantiated to the current context. typeOfConst :: QName -> TCM Type typeOfConst q = defType <$> (instantiateDef =<< getConstInfo q) -- | Get relevance of a constant. relOfConst :: QName -> TCM Relevance relOfConst q = defRelevance <$> getConstInfo q -- | The number of dropped parameters for a definition. -- 0 except for projection(-like) functions and constructors. droppedPars :: Definition -> Int droppedPars d = case theDef d of Axiom{} -> 0 def@Function{} -> projectionArgs def Datatype {dataPars = _} -> 0 -- not dropped Record {recPars = _} -> 0 -- not dropped Constructor{conPars = n} -> n Primitive{} -> 0 AbstractDefn{} -> __IMPOSSIBLE__ -- | Is it the name of a record projection? {-# SPECIALIZE isProjection :: QName -> TCM (Maybe Projection) #-} isProjection :: HasConstInfo m => QName -> m (Maybe Projection) isProjection qn = isProjection_ . theDef <$> getConstInfo qn isProjection_ :: Defn -> Maybe Projection isProjection_ def = case def of Function { funProjection = result } -> result _ -> Nothing -- | Is it a function marked STATIC? isStaticFun :: Defn -> Bool isStaticFun = (^. funStatic) -- | Is it a function marked INLINE? isInlineFun :: Defn -> Bool isInlineFun = (^. funInline) -- | Returns @True@ if we are dealing with a proper projection, -- i.e., not a projection-like function nor a record field value -- (projection applied to argument). isProperProjection :: Defn -> Bool isProperProjection d = caseMaybe (isProjection_ d) False $ \ isP -> if projIndex isP <= 0 then False else isJust $ projProper isP -- | Number of dropped initial arguments of a projection(-like) function. projectionArgs :: Defn -> Int projectionArgs = maybe 0 (max 0 . pred . projIndex) . isProjection_ -- | Check whether a definition uses copatterns. usesCopatterns :: QName -> TCM Bool usesCopatterns q = do d <- theDef <$> getConstInfo q return $ case d of Function{ funCopatternLHS = b } -> b _ -> False -- | Apply a function @f@ to its first argument, producing the proper -- postfix projection if @f@ is a projection. applyDef :: ProjOrigin -> QName -> Arg Term -> TCM Term applyDef o f a = do let fallback = return $ Def f [Apply a] caseMaybeM (isProjection f) fallback $ \ isP -> do if projIndex isP <= 0 then fallback else do -- Get the original projection, if existing. if isNothing (projProper isP) then fallback else do return $ unArg a `applyE` [Proj o $ projOrig isP] Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Mutual.hs0000644000000000000000000000535213154613124020353 0ustar0000000000000000-- {-# LANGUAGE CPP #-} module Agda.TypeChecking.Monad.Mutual where import Prelude hiding (null) import Control.Monad.Reader import Data.Functor ((<$>)) import Data.Set (Set) import qualified Data.Set as Set import qualified Data.Map as Map import Agda.Syntax.Info as Info import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.State import Agda.Utils.Lens import Agda.Utils.Null import Agda.Utils.Pretty ( prettyShow ) noMutualBlock :: TCM a -> TCM a noMutualBlock = local $ \e -> e { envMutualBlock = Nothing } -- | Pass the current mutual block id -- or create a new mutual block if we are not already inside on. inMutualBlock :: (MutualId -> TCM a) -> TCM a inMutualBlock m = do mi <- asks envMutualBlock case mi of Nothing -> do i <- fresh local (\ e -> e { envMutualBlock = Just i }) $ m i -- Don't create a new mutual block if we're already inside one. Just i -> m i -- | Set the mutual block info for a block, -- possibly overwriting the existing one. setMutualBlockInfo :: MutualId -> Info.MutualInfo -> TCM () setMutualBlockInfo mi info = stMutualBlocks %= Map.alter f mi where f Nothing = Just $ MutualBlock info empty f (Just (MutualBlock _ xs)) = Just $ MutualBlock info xs -- | Set the mutual block info for a block if non-existing. insertMutualBlockInfo :: MutualId -> Info.MutualInfo -> TCM () insertMutualBlockInfo mi info = stMutualBlocks %= Map.alter f mi where f Nothing = Just $ MutualBlock info empty f (Just mb@(MutualBlock info0 xs)) | null info0 = Just $ MutualBlock info xs | otherwise = Just mb -- | Set the mutual block for a definition. setMutualBlock :: MutualId -> QName -> TCM () setMutualBlock i x = do stMutualBlocks %= Map.alter f i stSignature %= updateDefinition x (\ defn -> defn { defMutual = i }) where f Nothing = Just $ MutualBlock empty $ Set.singleton x f (Just (MutualBlock mi xs)) = Just $ MutualBlock mi $ Set.insert x xs -- | Get the current mutual block, if any, otherwise a fresh mutual -- block is returned. currentOrFreshMutualBlock :: TCM MutualId currentOrFreshMutualBlock = maybe fresh return =<< asks envMutualBlock lookupMutualBlock :: MutualId -> TCM MutualBlock lookupMutualBlock mi = do mbs <- use stMutualBlocks case Map.lookup mi mbs of Just mb -> return mb Nothing -> return empty -- can end up here if we ask for the current mutual block and there is none -- | Reverse lookup of a mutual block id for a names. mutualBlockOf :: QName -> TCM MutualId mutualBlockOf x = do mb <- Map.toList <$> use stMutualBlocks case filter (Set.member x . mutualNames . snd) mb of (i, _) : _ -> return i _ -> fail $ "No mutual block for " ++ prettyShow x Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Options.hs0000644000000000000000000003214013154613124020532 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Monad.Options where import Prelude hiding (mapM) import Control.Applicative import Control.Monad.Reader hiding (mapM) import Control.Monad.State hiding (mapM) import Data.Maybe import Data.Traversable import System.Directory import System.FilePath import Agda.Syntax.Internal import Agda.Syntax.Common import Agda.Syntax.Concrete import {-# SOURCE #-} Agda.TypeChecking.Monad.Debug import {-# SOURCE #-} Agda.TypeChecking.Errors import Agda.TypeChecking.Warnings import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.State import Agda.TypeChecking.Monad.Benchmark import {-# SOURCE #-} Agda.Interaction.FindFile import Agda.Interaction.Options import qualified Agda.Interaction.Options.Lenses as Lens import Agda.Interaction.Response import Agda.Interaction.Library import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.FileName import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Pretty import Agda.Utils.Trie (Trie) import qualified Agda.Utils.Trie as Trie import Agda.Utils.Except import Agda.Utils.Either #include "undefined.h" import Agda.Utils.Impossible -- | Sets the pragma options. setPragmaOptions :: PragmaOptions -> TCM () setPragmaOptions opts = do stPragmaOptions %= Lens.mapSafeMode (Lens.getSafeMode opts ||) clo <- commandLineOptions let unsafe = unsafePragmaOptions opts when (Lens.getSafeMode clo && not (null unsafe)) $ warning $ SafeFlagPragma unsafe ok <- liftIO $ runOptM $ checkOpts (clo { optPragmaOptions = opts }) case ok of Left err -> __IMPOSSIBLE__ Right opts -> do stPragmaOptions .= optPragmaOptions opts updateBenchmarkingStatus -- | Sets the command line options (both persistent and pragma options -- are updated). -- -- Relative include directories are made absolute with respect to the -- current working directory. If the include directories have changed -- (thus, they are 'Left' now, and were previously @'Right' something@), -- then the state is reset (completely, see setIncludeDirs) . -- -- An empty list of relative include directories (@'Left' []@) is -- interpreted as @["."]@. setCommandLineOptions :: CommandLineOptions -> TCM () setCommandLineOptions = setCommandLineOptions' CurrentDir setCommandLineOptions' :: RelativeTo -> CommandLineOptions -> TCM () setCommandLineOptions' relativeTo opts = do z <- liftIO $ runOptM $ checkOpts opts case z of Left err -> __IMPOSSIBLE__ Right opts -> do incs <- case optAbsoluteIncludePaths opts of [] -> do opts' <- setLibraryPaths relativeTo opts let incs = optIncludePaths opts' setIncludeDirs incs relativeTo getIncludeDirs incs -> return incs modify $ Lens.setCommandLineOptions opts{ optAbsoluteIncludePaths = incs } setPragmaOptions (optPragmaOptions opts) updateBenchmarkingStatus libToTCM :: LibM a -> TCM a libToTCM m = do z <- liftIO $ runExceptT m case z of Left s -> typeError $ GenericDocError s Right x -> return x setLibraryPaths :: RelativeTo -> CommandLineOptions -> TCM CommandLineOptions setLibraryPaths rel o = setLibraryIncludes =<< addDefaultLibraries rel o setLibraryIncludes :: CommandLineOptions -> TCM CommandLineOptions setLibraryIncludes o = do let libs = optLibraries o installed <- libToTCM $ getInstalledLibraries (optOverrideLibrariesFile o) paths <- libToTCM $ libraryIncludePaths (optOverrideLibrariesFile o) installed libs return o{ optIncludePaths = paths ++ optIncludePaths o } addDefaultLibraries :: RelativeTo -> CommandLineOptions -> TCM CommandLineOptions addDefaultLibraries rel o | or [ not $ null $ optLibraries o , not $ optUseLibs o , optShowVersion o ] = pure o | otherwise = do root <- getProjectRoot rel (libs, incs) <- libToTCM $ getDefaultLibraries (filePath root) (optDefaultLibs o) return o{ optIncludePaths = incs ++ optIncludePaths o, optLibraries = libs } setOptionsFromPragma :: OptionsPragma -> TCM () setOptionsFromPragma ps = do opts <- commandLineOptions z <- liftIO $ runOptM (parsePragmaOptions ps opts) case z of Left err -> typeError $ GenericError err Right opts' -> setPragmaOptions opts' -- | Disable display forms. enableDisplayForms :: TCM a -> TCM a enableDisplayForms = local $ \e -> e { envDisplayFormsEnabled = True } -- | Disable display forms. disableDisplayForms :: TCM a -> TCM a disableDisplayForms = local $ \e -> e { envDisplayFormsEnabled = False } -- | Check if display forms are enabled. displayFormsEnabled :: TCM Bool displayFormsEnabled = asks envDisplayFormsEnabled -- | Gets the include directories. -- -- Precondition: 'optAbsoluteIncludePaths' must be nonempty (i.e. -- 'setCommandLineOptions' must have run). getIncludeDirs :: TCM [AbsolutePath] getIncludeDirs = do incs <- optAbsoluteIncludePaths <$> commandLineOptions case incs of [] -> __IMPOSSIBLE__ _ -> return incs -- | Which directory should form the base of relative include paths? data RelativeTo = ProjectRoot AbsolutePath -- ^ The root directory of the \"project\" containing the given -- file. The file needs to be syntactically correct, with a module -- name matching the file name. | CurrentDir -- ^ The current working directory. getProjectRoot :: RelativeTo -> TCM AbsolutePath getProjectRoot CurrentDir = liftIO (absolute =<< getCurrentDirectory) getProjectRoot (ProjectRoot f) = do m <- moduleName' f return (projectRoot f m) -- | Makes the given directories absolute and stores them as include -- directories. -- -- If the include directories change, then the state is reset (completely, -- except for the include directories and 'stInteractionOutputCallback'). -- -- An empty list is interpreted as @["."]@. setIncludeDirs :: [FilePath] -- ^ New include directories. -> RelativeTo -- ^ How should relative paths be interpreted? -> TCM () setIncludeDirs incs relativeTo = do -- save the previous include dirs oldIncs <- gets Lens.getAbsoluteIncludePaths root <- getProjectRoot relativeTo -- Add the current dir if no include path is given incs <- return $ if null incs then ["."] else incs -- Make paths absolute incs <- return $ map (mkAbsolute . (filePath root )) incs -- Andreas, 2013-10-30 Add default include dir libdir <- liftIO $ defaultLibDir -- NB: This is an absolute file name, but -- Agda.Utils.FilePath wants to check absoluteness anyway. let primdir = mkAbsolute $ libdir "prim" -- We add the default dir at the end, since it is then -- printed last in error messages. -- Might also be useful to overwrite default imports... incs <- return $ incs ++ [primdir] reportSDoc "setIncludeDirs" 10 $ return $ vcat [ text "Old include directories:" , nest 2 $ vcat $ map pretty oldIncs , text "New include directories:" , nest 2 $ vcat $ map pretty incs ] -- Check whether the include dirs have changed. If yes, reset state. -- Andreas, 2013-10-30 comments: -- The logic, namely using the include-dirs variable as a driver -- for the interaction, qualifies for a code-obfuscation contest. -- I guess one Boolean more in the state cost 10.000 EUR at the time -- of this implementation... -- -- Andreas, perhaps you have misunderstood something: If the include -- directories have changed and we do not reset the decoded modules, -- then we might (depending on how the rest of the code works) end -- up in a situation in which we use the contents of the file -- "old-path/M.agda", when the user actually meant -- "new-path/M.agda". when (oldIncs /= incs) $ do ho <- getInteractionOutputCallback resetAllState setInteractionOutputCallback ho Lens.putAbsoluteIncludePaths incs -- Andreas, 2016-07-11 (reconstructing semantics): -- -- Check that the module name of the project root -- is still correct wrt. to the changed include path. -- -- E.g. if the include path was "/" and file "/A/B" was named "module A.B", -- and then the include path changes to "/A/", the module name -- becomes invalid; correct would then be "module B". case relativeTo of CurrentDir -> return () ProjectRoot f -> void $ moduleName f -- Andreas, 2016-07-12 WAS: -- do -- m <- moduleName' f -- checkModuleName m f Nothing setInputFile :: FilePath -> TCM () setInputFile file = do opts <- commandLineOptions setCommandLineOptions $ opts { optInputFile = Just file } -- | Should only be run if 'hasInputFile'. getInputFile :: TCM AbsolutePath getInputFile = fromMaybeM __IMPOSSIBLE__ $ getInputFile' -- | Return the 'optInputFile' as 'AbsolutePath', if any. getInputFile' :: TCM (Maybe AbsolutePath) getInputFile' = mapM (liftIO . absolute) =<< do optInputFile <$> commandLineOptions hasInputFile :: TCM Bool hasInputFile = isJust <$> optInputFile <$> commandLineOptions proofIrrelevance :: TCM Bool proofIrrelevance = optProofIrrelevance <$> pragmaOptions {-# SPECIALIZE hasUniversePolymorphism :: TCM Bool #-} hasUniversePolymorphism :: HasOptions m => m Bool hasUniversePolymorphism = optUniversePolymorphism <$> pragmaOptions {-# SPECIALIZE sharedFun :: TCM (Term -> Term) #-} sharedFun :: HasOptions m => m (Term -> Term) sharedFun = do sharing <- optSharing <$> commandLineOptions return $ if sharing then shared_ else id {-# SPECIALIZE shared :: Term -> TCM Term #-} shared :: HasOptions m => Term -> m Term shared v = ($ v) <$> sharedFun {-# SPECIALIZE sharedType :: Type -> TCM Type #-} sharedType :: HasOptions m => Type -> m Type sharedType (El s v) = El s <$> shared v enableCaching :: TCM Bool enableCaching = optCaching <$> commandLineOptions showImplicitArguments :: TCM Bool showImplicitArguments = optShowImplicit <$> pragmaOptions showIrrelevantArguments :: TCM Bool showIrrelevantArguments = optShowIrrelevant <$> pragmaOptions -- | Switch on printing of implicit and irrelevant arguments. -- E.g. for reification in with-function generation. -- -- Restores all 'PragmaOptions' after completion. -- Thus, do not attempt to make persistent 'PragmaOptions' -- changes in a `withShowAllArguments` bracket. withShowAllArguments :: TCM a -> TCM a withShowAllArguments = withShowAllArguments' True withShowAllArguments' :: Bool -> TCM a -> TCM a withShowAllArguments' yes = withPragmaOptions $ \ opts -> opts { optShowImplicit = yes, optShowIrrelevant = yes } -- | Change 'PragmaOptions' for a computation and restore afterwards. withPragmaOptions :: (PragmaOptions -> PragmaOptions) -> TCM a -> TCM a withPragmaOptions f cont = do opts <- pragmaOptions setPragmaOptions $ f opts x <- cont setPragmaOptions opts return x ignoreInterfaces :: TCM Bool ignoreInterfaces = optIgnoreInterfaces <$> commandLineOptions positivityCheckEnabled :: TCM Bool positivityCheckEnabled = not . optDisablePositivity <$> pragmaOptions {-# SPECIALIZE typeInType :: TCM Bool #-} typeInType :: HasOptions m => m Bool typeInType = not . optUniverseCheck <$> pragmaOptions etaEnabled :: TCM Bool etaEnabled = optEta <$> pragmaOptions maxInstanceSearchDepth :: TCM Int maxInstanceSearchDepth = optInstanceSearchDepth <$> pragmaOptions ------------------------------------------------------------------------ -- Verbosity -- Invariant (which we may or may not currently break): Debug -- printouts use one of the following functions: -- -- reportS -- reportSLn -- reportSDoc -- | Retrieve the current verbosity level. {-# SPECIALIZE getVerbosity :: TCM (Trie String Int) #-} getVerbosity :: HasOptions m => m (Trie String Int) getVerbosity = optVerbose <$> pragmaOptions type VerboseKey = String -- | Check whether a certain verbosity level is activated. -- -- Precondition: The level must be non-negative. {-# SPECIALIZE hasVerbosity :: VerboseKey -> Int -> TCM Bool #-} hasVerbosity :: HasOptions m => VerboseKey -> Int -> m Bool hasVerbosity k n | n < 0 = __IMPOSSIBLE__ | otherwise = do t <- getVerbosity let ks = wordsBy (`elem` ".:") k m = last $ 0 : Trie.lookupPath ks t return (n <= m) -- | Check whether a certain verbosity level is activated (exact match). {-# SPECIALIZE hasExactVerbosity :: VerboseKey -> Int -> TCM Bool #-} hasExactVerbosity :: HasOptions m => VerboseKey -> Int -> m Bool hasExactVerbosity k n = (Just n ==) . Trie.lookup (wordsBy (`elem` ".:") k) <$> getVerbosity -- | Run a computation if a certain verbosity level is activated (exact match). {-# SPECIALIZE whenExactVerbosity :: VerboseKey -> Int -> TCM () -> TCM () #-} whenExactVerbosity :: MonadTCM tcm => VerboseKey -> Int -> tcm () -> tcm () whenExactVerbosity k n = whenM $ liftTCM $ hasExactVerbosity k n -- | Run a computation if a certain verbosity level is activated. -- -- Precondition: The level must be non-negative. {-# SPECIALIZE verboseS :: VerboseKey -> Int -> TCM () -> TCM () #-} -- {-# SPECIALIZE verboseS :: MonadIO m => VerboseKey -> Int -> TCMT m () -> TCMT m () #-} -- RULE left-hand side too complicated to desugar {-# SPECIALIZE verboseS :: MonadTCM tcm => VerboseKey -> Int -> tcm () -> tcm () #-} verboseS :: HasOptions m => VerboseKey -> Int -> m () -> m () verboseS k n action = whenM (hasVerbosity k n) action Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Benchmark.hs0000644000000000000000000000640013154613124020771 0ustar0000000000000000-- | Measure CPU time for individual phases of the Agda pipeline. module Agda.TypeChecking.Monad.Benchmark ( module Agda.Benchmarking , B.MonadBench , B.getBenchmark , updateBenchmarkingStatus , B.billTo, B.billPureTo, B.billToCPS , B.reset , print ) where import Prelude hiding (print) import Agda.Benchmarking import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Debug import{-# SOURCE #-} Agda.TypeChecking.Monad.Options import qualified Agda.Utils.Benchmark as B import Agda.Utils.Monad import Agda.Utils.Pretty (prettyShow) benchmarkKey :: String benchmarkKey = "profile" benchmarkLevel :: Int benchmarkLevel = 7 benchmarkModulesKey :: String benchmarkModulesKey = "profile.modules" benchmarkModulesLevel :: Int benchmarkModulesLevel = 10 benchmarkDefsKey :: String benchmarkDefsKey = "profile.definitions" benchmarkDefsLevel :: Int benchmarkDefsLevel = 10 -- | When verbosity is set or changes, we need to turn benchmarking on or off. updateBenchmarkingStatus :: TCM () -- {-# SPECIALIZE updateBenchmarkingStatus :: TCM () #-} -- updateBenchmarkingStatus :: (HasOptions m, MonadBench a m) => m () updateBenchmarkingStatus = B.setBenchmarking =<< benchmarking -- | Check whether benchmarking is activated. {-# SPECIALIZE benchmarking :: TCM (B.BenchmarkOn Phase) #-} benchmarking :: MonadTCM tcm => tcm (B.BenchmarkOn Phase) benchmarking = liftTCM $ do -- Ulf, 2016-12-13: Using verbosity levels to control the type of -- benchmarking isn't ideal, but let's stick with it for now. internal <- hasVerbosity benchmarkKey benchmarkLevel defs <- hasVerbosity benchmarkDefsKey benchmarkDefsLevel modules <- hasVerbosity benchmarkModulesKey benchmarkModulesLevel return $ case (internal, defs, modules) of (True, _, _) -> B.BenchmarkSome isInternalAccount (_, True, _) -> B.BenchmarkSome isDefAccount (_, _, True) -> B.BenchmarkSome isModuleAccount _ -> B.BenchmarkOff -- | Prints the accumulated benchmark results. Does nothing if -- profiling is not activated at level 7. print :: MonadTCM tcm => tcm () print = liftTCM $ whenM (B.isBenchmarkOn [] <$> benchmarking) $ do b <- B.getBenchmark -- Andreas, 2017-07-29, issue #2602: -- The following line messes up the AgdaInfo buffer, -- thus, as Fredrik Forsberg suggest, I restore the original -- line for release 2.5.3 until a fix is found. -- reportSLn "" 0 $ prettyShow b reportSLn benchmarkKey benchmarkLevel $ prettyShow b -- -- | Bill a computation to a specific account. -- {-# SPECIALIZE billTo :: Account -> TCM a -> TCM a #-} -- billTo :: MonadTCM tcm => Account -> tcm a -> tcm a -- billTo account = lift1TCM $ B.billTo account -- Andreas, 2015-05-23 -- FAILS as lift1TCM :: (TCM a -> TCM b) -> tcm a -> tcm b -- cannot be implemented lazily in general. -- With `lazily` I mean that embedded IO computations in @tcm a@ are -- not executed, but passed on to @TCM a -> TCM b@ unevaluated. -- If they are treated strictly, then the whole benchmarking is inaccurate -- of course, as the computation is done before the clock is started. -- -- | Bill a pure computation to a specific account. -- {-# SPECIALIZE billPureTo :: Account -> a -> TCM a #-} -- billPureTo :: MonadTCM tcm => Account -> a -> tcm a -- billPureTo k a = billTo k $ return a Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Closure.hs0000644000000000000000000000116413154613124020515 0ustar0000000000000000module Agda.TypeChecking.Monad.Closure where import Control.Monad import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Env import Agda.TypeChecking.Monad.State import Agda.TypeChecking.Monad.Context enterClosure :: Closure a -> (a -> TCM b) -> TCM b enterClosure (Closure sig env scope pars x) k = withScope_ scope $ withEnv env $ withModuleParameters pars $ k x withClosure :: Closure a -> (a -> TCM b) -> TCM (Closure b) withClosure cl k = enterClosure cl $ k >=> buildClosure mapClosure :: (a -> TCM b) -> Closure a -> TCM (Closure b) mapClosure k cl = enterClosure cl $ k >=> buildClosure Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Env.hs0000644000000000000000000001021113154613124017622 0ustar0000000000000000 module Agda.TypeChecking.Monad.Env where import Control.Monad.Reader import qualified Data.List as List import Data.Monoid import Agda.Syntax.Common import Agda.Syntax.Abstract.Name import Agda.TypeChecking.Monad.Base -- | Get the name of the current module, if any. {-# SPECIALIZE currentModule :: TCM ModuleName #-} {-# SPECIALIZE currentModule :: ReduceM ModuleName #-} currentModule :: MonadReader TCEnv m => m ModuleName currentModule = asks envCurrentModule -- | Set the name of the current module. withCurrentModule :: ModuleName -> TCM a -> TCM a withCurrentModule m = local $ \e -> e { envCurrentModule = m } -- | Get the number of variables bound by anonymous modules. {-# SPECIALIZE getAnonymousVariables :: ModuleName -> TCM Nat #-} {-# SPECIALIZE getAnonymousVariables :: ModuleName -> ReduceM Nat #-} getAnonymousVariables :: MonadReader TCEnv m => ModuleName -> m Nat getAnonymousVariables m = do ms <- asks envAnonymousModules return $ sum [ n | (m', n) <- ms, mnameToList m' `List.isPrefixOf` mnameToList m ] -- | Add variables bound by an anonymous module. withAnonymousModule :: ModuleName -> Nat -> TCM a -> TCM a withAnonymousModule m n = local $ \e -> e { envAnonymousModules = (m, n) : envAnonymousModules e } -- | Set the current environment to the given withEnv :: TCEnv -> TCM a -> TCM a withEnv env = local $ \ env0 -> env -- Keep persistent settings { envAllowDestructiveUpdate = envAllowDestructiveUpdate env0 , envPrintMetasBare = envPrintMetasBare env0 } -- | Get the current environment getEnv :: TCM TCEnv getEnv = ask -- | Increases the module nesting level by one in the given -- computation. withIncreasedModuleNestingLevel :: TCM a -> TCM a withIncreasedModuleNestingLevel = local (\e -> e { envModuleNestingLevel = envModuleNestingLevel e + 1 }) -- | Set highlighting level withHighlightingLevel :: HighlightingLevel -> TCM a -> TCM a withHighlightingLevel h = local $ \e -> e { envHighlightingLevel = h } -- | Restore setting for 'ExpandLast' to default. doExpandLast :: TCM a -> TCM a doExpandLast = local $ \ e -> e { envExpandLast = ExpandLast } dontExpandLast :: TCM a -> TCM a dontExpandLast = local $ \ e -> e { envExpandLast = DontExpandLast } -- | If the reduced did a proper match (constructor or literal pattern), -- then record this as simplification step. {-# SPECIALIZE performedSimplification :: TCM a -> TCM a #-} performedSimplification :: MonadReader TCEnv m => m a -> m a performedSimplification = local $ \ e -> e { envSimplification = YesSimplification } {-# SPECIALIZE performedSimplification' :: Simplification -> TCM a -> TCM a #-} performedSimplification' :: MonadReader TCEnv m => Simplification -> m a -> m a performedSimplification' simpl = local $ \ e -> e { envSimplification = simpl `mappend` envSimplification e } getSimplification :: MonadReader TCEnv m => m Simplification getSimplification = asks envSimplification -- * Controlling reduction. -- | Lens for 'AllowedReductions'. updateAllowedReductions :: (AllowedReductions -> AllowedReductions) -> TCEnv -> TCEnv updateAllowedReductions f e = e { envAllowedReductions = f (envAllowedReductions e) } modifyAllowedReductions :: (AllowedReductions -> AllowedReductions) -> TCM a -> TCM a modifyAllowedReductions = local . updateAllowedReductions putAllowedReductions :: AllowedReductions -> TCM a -> TCM a putAllowedReductions = modifyAllowedReductions . const -- | Reduce @Def f vs@ only if @f@ is a projection. onlyReduceProjections :: TCM a -> TCM a onlyReduceProjections = putAllowedReductions [ProjectionReductions] -- | Allow all reductions except for non-terminating functions (default). allowAllReductions :: TCM a -> TCM a allowAllReductions = putAllowedReductions allReductions -- | Allow all reductions including non-terminating functions. allowNonTerminatingReductions :: TCM a -> TCM a allowNonTerminatingReductions = putAllowedReductions $ [NonTerminatingReductions] ++ allReductions -- * Concerning 'envInsideDotPattern' insideDotPattern :: TCM a -> TCM a insideDotPattern = local $ \e -> e { envInsideDotPattern = True } isInsideDotPattern :: TCM Bool isInsideDotPattern = asks envInsideDotPattern Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Caching.hs0000644000000000000000000000755713154613124020451 0ustar0000000000000000{-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} module Agda.TypeChecking.Monad.Caching ( -- * Log reading/writing operations writeToCurrentLog , readFromCachedLog , cleanCachedLog , cacheCurrentLog -- * Activating/deactivating , activateLoadedFileCache , cachingStarts , noCacheForImportedModule -- * Restoring the 'PostScopeState' , restorePostScopeState ) where import Control.Monad.State import qualified Data.Map as Map import Agda.Syntax.Common import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Options import Agda.Utils.Lens import Agda.Utils.Monad #include "undefined.h" import Agda.Utils.Impossible -- | To be called before any write or restore calls. cachingStarts :: TCM () cachingStarts = do NameId _ m <- use stFreshNameId stFreshNameId .= NameId 1 m -- | Writes a 'TypeCheckAction' to the current log, using the current -- 'PostScopeState' writeToCurrentLog :: TypeCheckAction -> TCM () writeToCurrentLog !d = do reportSLn "cache" 10 $ "cachePostScopeState" !l <- gets stPostScopeState modifyCache $ fmap $ \lfc -> lfc{ lfcCurrent = (d, l) : lfcCurrent lfc} restorePostScopeState :: PostScopeState -> TCM () restorePostScopeState pss = do reportSLn "cache" 10 $ "restorePostScopeState" modify $ \s -> let ipoints = s^.stInteractionPoints pss' = pss{stPostInteractionPoints = stPostInteractionPoints pss `mergeIPMap` ipoints} in s{stPostScopeState = pss'} where mergeIPMap lm sm = Map.mapWithKey (\k v -> maybe v (`mergeIP` v) (Map.lookup k lm)) sm mergeIP li si = si {ipMeta = ipMeta li} modifyCache :: (Maybe LoadedFileCache -> Maybe LoadedFileCache) -> TCM () modifyCache f = do modify $ \s -> let !p = stPersistentState s in s { stPersistentState = p { stLoadedFileCache = f (stLoadedFileCache p)} } getCache :: TCM (Maybe LoadedFileCache) getCache = do gets (stLoadedFileCache . stPersistentState) putCache :: Maybe LoadedFileCache -> TCM () putCache cs = modifyCache $ const cs -- | The cache should not be used for an imported module, and it -- should be restored after the module has been type-checked. This -- combinator takes care of that. noCacheForImportedModule :: TCM a -> TCM a noCacheForImportedModule m = bracket_ getCache putCache $ do modifyCache (const Nothing) m -- | Reads the next entry in the cached type check log, if present. readFromCachedLog :: TCM (Maybe (TypeCheckAction, PostScopeState)) readFromCachedLog = do reportSLn "cache" 10 $ "getCachedTypeCheckAction" mbCache <- getCache case mbCache of Just lfc | (entry : entries) <- lfcCached lfc -> do putCache $ Just lfc{lfcCached = entries} return (Just entry) _ -> do return Nothing -- | Empties the "to read" CachedState. To be used when it gets invalid. cleanCachedLog :: TCM () cleanCachedLog = do reportSLn "cache" 10 $ "cleanCachedLog" modifyCache $ fmap $ \lfc -> lfc{lfcCached = []} -- | Makes sure that the 'stLoadedFileCache' is 'Just', with a clean -- current log. Crashes is 'stLoadedFileCache' is already active with a -- dirty log. Should be called when we start typechecking the current -- file. activateLoadedFileCache :: TCM () activateLoadedFileCache = do reportSLn "cache" 10 $ "activateLoadedFileCache" b <- enableCaching if not b then return () else do modifyCache $ \mbLfc -> case mbLfc of Nothing -> Just $ LoadedFileCache [] [] Just lfc | null (lfcCurrent lfc) -> Just lfc _ -> __IMPOSSIBLE__ -- | Caches the current type check log. Discardes the old cache. Does -- nothing if caching is inactive. cacheCurrentLog :: TCM () cacheCurrentLog = do reportSLn "cache" 10 $ "cacheCurrentTypeCheckLog" modifyCache $ fmap $ \lfc -> lfc{lfcCached = reverse (lfcCurrent lfc), lfcCurrent = []} Agda-2.5.3/src/full/Agda/TypeChecking/Monad/Imports.hs0000644000000000000000000000537513154613124020546 0ustar0000000000000000module Agda.TypeChecking.Monad.Imports where import Control.Monad.State import Control.Monad.Reader import Data.Set (Set) import qualified Data.Map as Map import qualified Data.Set as Set import Agda.Syntax.Abstract.Name import qualified Agda.Syntax.Concrete.Name as C import Agda.TypeChecking.Monad.Base import Agda.Utils.Lens import Agda.Utils.Monad addImport :: ModuleName -> TCM () addImport m = stImportedModules %= Set.insert m addImportCycleCheck :: C.TopLevelModuleName -> TCM a -> TCM a addImportCycleCheck m = local $ \e -> e { envImportPath = m : envImportPath e } getImports :: TCM (Set ModuleName) getImports = use stImportedModules isImported :: ModuleName -> TCM Bool isImported m = Set.member m <$> getImports getImportPath :: TCM [C.TopLevelModuleName] getImportPath = asks envImportPath visitModule :: ModuleInfo -> TCM () visitModule mi = stVisitedModules %= Map.insert (toTopLevelModuleName $ iModuleName $ miInterface mi) mi setVisitedModules :: VisitedModules -> TCM () setVisitedModules ms = stVisitedModules .= ms getVisitedModules :: TCM VisitedModules getVisitedModules = use stVisitedModules isVisited :: C.TopLevelModuleName -> TCM Bool isVisited x = Map.member x <$> use stVisitedModules getVisitedModule :: C.TopLevelModuleName -> TCM (Maybe ModuleInfo) getVisitedModule x = Map.lookup x <$> use stVisitedModules getDecodedModules :: TCM DecodedModules getDecodedModules = stDecodedModules . stPersistentState <$> get setDecodedModules :: DecodedModules -> TCM () setDecodedModules ms = modify $ \s -> s { stPersistentState = (stPersistentState s) { stDecodedModules = ms } } getDecodedModule :: C.TopLevelModuleName -> TCM (Maybe Interface) getDecodedModule x = Map.lookup x . stDecodedModules . stPersistentState <$> get storeDecodedModule :: Interface -> TCM () storeDecodedModule i = modify $ \s -> s { stPersistentState = (stPersistentState s) { stDecodedModules = Map.insert (toTopLevelModuleName $ iModuleName i) i $ (stDecodedModules $ stPersistentState s) } } dropDecodedModule :: C.TopLevelModuleName -> TCM () dropDecodedModule x = modify $ \s -> s { stPersistentState = (stPersistentState s) { stDecodedModules = Map.delete x $ stDecodedModules $ stPersistentState s } } withImportPath :: [C.TopLevelModuleName] -> TCM a -> TCM a withImportPath path = local $ \e -> e { envImportPath = path } -- | Assumes that the first module in the import path is the module we are -- worried about. checkForImportCycle :: TCM () checkForImportCycle = do m:ms <- getImportPath when (m `elem` ms) $ typeError $ CyclicModuleDependency $ dropWhile (/= m) $ reverse (m:ms) Agda-2.5.3/src/full/Agda/TypeChecking/Coverage/0000755000000000000000000000000013154613124017240 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Coverage/Match.hs0000644000000000000000000002743313154613124020641 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Coverage.Match where import Control.Applicative import Control.Monad.State import qualified Data.List as List import Data.Maybe (mapMaybe, isJust) import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend, mconcat, Any(..)) import Data.Traversable (traverse) import Agda.Syntax.Abstract (IsProjP(..)) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern () import Agda.Syntax.Literal import Agda.TypeChecking.Monad import Agda.TypeChecking.Records import Agda.TypeChecking.Substitute import Agda.Utils.Permutation import Agda.Utils.Pretty ( Pretty(pretty), text, (<+>) ) import Agda.Utils.Size import Agda.Utils.List import Agda.Utils.Monad #include "undefined.h" import Agda.Utils.Impossible {-| Given 1. the function clauses @cs@ 2. the patterns @ps@ we want to compute a variable index of the split clause to split on next. First, we find the set @cs'@ of all the clauses that are instances (via substitutions @rhos@) of the split clause. In these substitutions, we look for a column that has only constructor patterns. We try to split on this column first. -} -- | Match the given patterns against a list of clauses match :: [Clause] -> [NamedArg DeBruijnPattern] -> Match (Nat,([DeBruijnPattern],[Literal])) match cs ps = foldr choice No $ zipWith matchIt [0..] cs where -- If liberal matching on literals fails or blocks we go with that. -- If it succeeds we use the result from conservative literal matching. -- This is to make sure that we split enough when literals are involved. -- For instance, -- f ('x' :: 'y' :: _) = ... -- f (c :: s) = ... -- would never split the tail of the list if we only used conservative -- literal matching. matchIt i c = (i,) <$> matchClause yesMatchLit ps i c +++ matchClause noMatchLit ps i c Yes _ +++ m = m No +++ _ = No m@Block{} +++ _ = m -- | Convert the root of a term into a pattern constructor, if possible. buildPattern :: Term -> Maybe DeBruijnPattern buildPattern (Con c ci args) = Just $ ConP c (toConPatternInfo ci) $ map (fmap $ unnamed . DotP) args buildPattern (Var i []) = Just $ deBruijnVar i buildPattern (Shared p) = buildPattern (derefPtr p) buildPattern _ = Nothing -- | A pattern that matches anything (modulo eta). isTrivialPattern :: (HasConstInfo m) => Pattern' a -> m Bool isTrivialPattern p = case p of VarP{} -> return True DotP{} -> return True AbsurdP{} -> return True ConP c i ps -> andM $ (isEtaCon $ conName c) : (map (isTrivialPattern . namedArg) ps) LitP{} -> return False ProjP{} -> return False -- | If matching succeeds, we return the instantiation of the clause pattern vector -- to obtain the split clause pattern vector, plus the literals of the clause patterns -- matched against split clause variables. type MatchResult = Match ([DeBruijnPattern],[Literal]) -- | If matching is inconclusive (@Block@) we want to know which -- variables are blocking the match. data Match a = Yes a -- ^ Matches unconditionally. | No -- ^ Definitely does not match. | Block Any BlockingVars -- ^ Could match if non-empty list of blocking variables -- is instantiated properly. -- Also 'Any' is 'True' if all clauses have a result split. -- (Only then can we do result splitting.) deriving (Functor) -- | Variable blocking a match. data BlockingVar = BlockingVar { blockingVarNo :: Nat -- ^ De Bruijn index of variable blocking the match. , blockingVarCons :: Maybe [ConHead] -- ^ @Nothing@ means there is an overlapping match for this variable. -- This happens if one clause has a constructor pattern at this position, -- and another a variable. It is also used for "just variable". -- -- @Just cons@ means that it is an non-overlapping match and -- @cons@ are the encountered constructors. } deriving (Show) instance Pretty BlockingVar where pretty (BlockingVar i mcs) = case mcs of Nothing -> text $ "blocking var " ++ show i ++ " (overlapping)" Just cs -> text ("blocking var " ++ show i ++ " (non-overlapping); constructors:") <+> pretty cs type BlockingVars = [BlockingVar] -- | Lens for 'blockingVarCons'. mapBlockingVarCons :: (Maybe [ConHead] -> Maybe [ConHead]) -> BlockingVar -> BlockingVar mapBlockingVarCons f b = b { blockingVarCons = f (blockingVarCons b) } clearBlockingVarCons :: BlockingVar -> BlockingVar clearBlockingVarCons = mapBlockingVarCons $ const Nothing overlapping :: BlockingVars -> BlockingVars overlapping = map clearBlockingVarCons -- | Left dominant merge of blocking vars. zipBlockingVars :: BlockingVars -> BlockingVars -> BlockingVars zipBlockingVars xs ys = map upd xs where upd (BlockingVar x (Just cons)) | Just (BlockingVar _ (Just cons')) <- List.find ((x ==) . blockingVarNo) ys = BlockingVar x (Just $ cons ++ cons') upd (BlockingVar x _) = BlockingVar x Nothing -- | @choice m m'@ combines the match results @m@ of a function clause -- with the (already combined) match results $m'$ of the later clauses. -- It is for skipping clauses that definitely do not match ('No'). -- It is left-strict, to be used with @foldr@. -- If one clause unconditionally matches ('Yes') we do not look further. choice :: Match a -> Match a -> Match a choice (Yes a) _ = Yes a choice (Block r xs) (Block s ys) = Block (Any $ getAny r && getAny s) $ zipBlockingVars xs ys choice (Block r xs) (Yes _) = Block r $ overlapping xs choice m@Block{} No = m choice No m = m -- | Could the literal cover (an instantiation of) the split clause pattern? -- Basically, the split clause pattern needs to be a variable. -- -- Note: literal patterns do not occur in the split clause -- since we cannot split into all possible literals (that would be infeasible). type MatchLit = Literal -> DeBruijnPattern -> MatchResult -- | Use this function if literal patterns should not cover a split clause pattern. noMatchLit :: MatchLit noMatchLit _ _ = No -- | Use this function if a literal pattern should cover a split clause variable pattern. yesMatchLit :: MatchLit yesMatchLit l q@VarP{} = Yes ([q], [l]) yesMatchLit l (DotP t) = maybe No (yesMatchLit l) $ buildPattern t yesMatchLit _ ConP{} = No yesMatchLit _ ProjP{} = No yesMatchLit _ AbsurdP{} = __IMPOSSIBLE__ yesMatchLit _ LitP{} = __IMPOSSIBLE__ -- | Check if a clause could match given generously chosen literals matchLits :: Clause -> [NamedArg DeBruijnPattern] -> Maybe [Literal] matchLits c ps = case matchClause yesMatchLit ps 0 c of Yes (qs,ls) -> Just ls _ -> Nothing -- | @matchClause mlit qs i c@ checks whether clause @c@ number @i@ -- covers a split clause with patterns @qs@. matchClause :: MatchLit -- ^ Consider literals? -> [NamedArg DeBruijnPattern] -- ^ Split clause patterns @qs@. -> Nat -- ^ Clause number @i@. -> Clause -- ^ Clause @c@ to cover split clause. -> MatchResult -- ^ Result. -- If 'Yes' the instantiation @rs@ such that @(namedClausePats c)[rs] == qs@. matchClause mlit qs i c = matchPats mlit (namedClausePats c) qs -- | @matchPats mlit ps qs@ checks whether a function clause with patterns -- @ps@ covers a split clause with patterns @qs@. -- -- Issue #842 / #1986: This is accepted: -- @ -- F : Bool -> Set1 -- F true = Set -- F = \ x -> Set -- @ -- For the second clause, the split clause is @F false@, -- so there are more patterns in the split clause than -- in the considered clause. These additional patterns -- are simply dropped by @zipWith@. This will result -- in @mconcat []@ which is @Yes []@. matchPats :: MatchLit -- ^ Matcher for literals. -> [NamedArg (Pattern' a)] -- ^ Clause pattern vector @ps@ (to cover split clause pattern vector). -> [NamedArg DeBruijnPattern] -- ^ Split clause pattern vector @qs@ (to be covered by clause pattern vector). -> MatchResult -- ^ Result. -- If 'Yes' the instantiation @rs@ such that @ps[rs] == qs@. matchPats mlit ps qs = mconcat $ [ projPatternsLeftInSplitClause ] ++ zipWith (matchPat mlit) (map namedArg ps) (map namedArg qs) ++ [ projPatternsLeftInMatchedClause ] where -- Patterns left in split clause: qsrest = drop (length ps) qs -- Andreas, 2016-06-03, issue #1986: -- catch-all for copatterns is inconsistent as found by Ulf. -- Thus, if the split clause has copatterns left, -- the current (shorter) clause is not considered covering. projPatternsLeftInSplitClause = case mapMaybe isProjP qsrest of [] -> mempty -- no proj. patterns left _ -> No -- proj. patterns left -- Patterns left in candidate clause: psrest = drop (length qs) ps -- If the current clause has additional copatterns in -- comparison to the split clause, we should split on them. projPatternsLeftInMatchedClause = case mapMaybe isProjP psrest of [] -> mempty -- no proj. patterns left ds -> Block (Any True) [] -- proj. patterns left -- | Combine results of checking whether function clause patterns -- covers split clause patterns. -- -- 'No' is dominant: if one function clause pattern is disjoint to -- the corresponding split clause pattern, then -- the whole clauses are disjoint. -- -- 'Yes' is neutral: for a match, all patterns have to match. -- -- 'Block' accumulates variables of the split clause -- that have to be instantiated (an projection names of copattern matches) -- to make the split clause an instance of the function clause. instance Semigroup a => Semigroup (Match a) where Yes a <> Yes b = Yes (a <> b) Yes _ <> m = m No <> _ = No Block{} <> No = No Block r xs <> Block s ys = Block (r <> s) (xs <> ys) m@Block{} <> Yes{} = m instance (Semigroup a, Monoid a) => Monoid (Match a) where mempty = Yes mempty mappend = (<>) -- | @matchPat mlit p q@ checks whether a function clause pattern @p@ -- covers a split clause pattern @q@. There are three results: -- @Yes rs@ means it covers, because @p@ is a variable pattern. @rs@ collects -- the instantiations of the variables in @p@ s.t. @p[rs] = q@. -- @No@ means it does not cover. -- @Block [x]@ means @p@ is a proper instance of @q@ and could become -- a cover if @q@ was split on variable @x@. matchPat :: MatchLit -- ^ Matcher for literals. -> Pattern' a -- ^ Clause pattern @p@ (to cover split clause pattern). -> DeBruijnPattern -- ^ Split clause pattern @q@ (to be covered by clause pattern). -> MatchResult -- ^ Result. -- If 'Yes', also the instantiation @rs@ of the clause pattern variables -- to produce the split clause pattern, @p[rs] = q@. matchPat _ VarP{} q = Yes ([q],[]) matchPat _ DotP{} q = mempty matchPat _ AbsurdP{} q = mempty -- Jesper, 2014-11-04: putting 'Yes [q]' here triggers issue 1333. -- Not checking for trivial patterns should be safe here, as dot patterns are -- guaranteed to match if the rest of the pattern does, so some extra splitting -- on them doesn't change the reduction behaviour. matchPat mlit (LitP l) q = mlit l q matchPat _ (ProjP _ d) (ProjP _ d') = if d == d' then mempty else No matchPat _ ProjP{} _ = __IMPOSSIBLE__ matchPat mlit p@(ConP c _ ps) q = case q of VarP x -> Block (Any False) [BlockingVar (dbPatVarIndex x) (Just [c])] ConP c' i qs | c == c' -> matchPats mlit ps qs | otherwise -> No DotP t -> maybe No (matchPat mlit p) $ buildPattern t AbsurdP{} -> __IMPOSSIBLE__ -- excluded by typing LitP _ -> __IMPOSSIBLE__ -- split clause has no literal patterns ProjP{} -> __IMPOSSIBLE__ -- excluded by typing Agda-2.5.3/src/full/Agda/TypeChecking/Coverage/SplitTree.hs0000644000000000000000000000535213154613124021514 0ustar0000000000000000{-# LANGUAGE CPP #-} {-| Split tree for transforming pattern clauses into case trees. The coverage checker generates a split tree from the clauses. The clause compiler uses it to transform clauses to case trees. The initial problem is a set of clauses. The root node designates on which argument to split and has subtrees for all the constructors. Splitting continues until there is only a single clause left at each leaf of the split tree. -} module Agda.TypeChecking.Coverage.SplitTree where import Data.Tree import Agda.Syntax.Abstract.Name import Agda.Syntax.Common import Agda.Syntax.Internal as I import Agda.Utils.Monad import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible type SplitTree = SplitTree' QName type SplitTrees = SplitTrees' QName -- | Abstract case tree shape. data SplitTree' a = -- | No more splits coming. We are at a single, all-variable -- clause. SplittingDone { splitBindings :: Int -- ^ The number of variables bound in the clause } | -- | A split is necessary. SplitAt { splitArg :: Arg Int -- ^ Arg. no to split at. , splitTrees :: SplitTrees' a -- ^ Sub split trees. } -- | Split tree branching. A finite map from constructor names to splittrees -- A list representation seems appropriate, since we are expecting not -- so many constructors per data type, and there is no need for -- random access. type SplitTrees' a = [(a, SplitTree' a)] -- * Printing a split tree data SplitTreeLabel a = SplitTreeLabel { lblConstructorName :: Maybe a -- ^ 'Nothing' for root of split tree , lblSplitArg :: Maybe (Arg Int) , lblBindings :: Maybe Int } instance Pretty a => Pretty (SplitTreeLabel a) where pretty = \case SplitTreeLabel Nothing Nothing (Just n) -> text $ "done, " ++ show n ++ " bindings" SplitTreeLabel Nothing (Just n) Nothing -> text $ "split at " ++ show n SplitTreeLabel (Just q) Nothing (Just n) -> pretty q <+> text (" -> done, " ++ show n ++ " bindings") SplitTreeLabel (Just q) (Just n) Nothing -> pretty q <+> text (" -> split at " ++ show n) _ -> __IMPOSSIBLE__ -- | Convert a split tree into a 'Data.Tree' (for printing). toTree :: SplitTree' a -> Tree (SplitTreeLabel a) toTree t = case t of SplittingDone n -> Node (SplitTreeLabel Nothing Nothing (Just n)) [] SplitAt n ts -> Node (SplitTreeLabel Nothing (Just n) Nothing) $ toTrees ts toTrees :: SplitTrees' a -> Forest (SplitTreeLabel a) toTrees = map (\ (c,t) -> setCons c $ toTree t) where setCons :: a -> Tree (SplitTreeLabel a) -> Tree (SplitTreeLabel a) setCons c (Node l ts) = Node (l { lblConstructorName = Just c }) ts instance Pretty a => Pretty (SplitTree' a) where pretty = text . drawTree . fmap prettyShow . toTree Agda-2.5.3/src/full/Agda/TypeChecking/Rewriting/0000755000000000000000000000000013154613124017457 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Rewriting/NonLinMatch.hs0000644000000000000000000004301613154613124022171 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} {-# LANGUAGE UndecidableInstances #-} {- | Non-linear matching of the lhs of a rewrite rule against a neutral term. Given a lhs Δ ⊢ lhs : B and a candidate term Γ ⊢ t : A we seek a substitution Γ ⊢ σ : Δ such that Γ ⊢ B[σ] = A and Γ ⊢ lhs[σ] = t : A -} module Agda.TypeChecking.Rewriting.NonLinMatch where import Prelude hiding (null, sequence) import Control.Arrow (first, second) import Control.Monad.State import Debug.Trace import System.IO.Unsafe #if __GLASGOW_HASKELL__ <= 708 import Data.Foldable ( foldMap ) #endif import Data.Maybe import Data.Traversable (Traversable,traverse) import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import Data.IntSet (IntSet) import qualified Data.IntSet as IntSet import Data.List (elemIndex) import Data.Monoid import Agda.Syntax.Common import qualified Agda.Syntax.Common as C import Agda.Syntax.Internal import Agda.TypeChecking.EtaContract import Agda.TypeChecking.Free import Agda.TypeChecking.Level (levelView', unLevel, reallyUnLevelView, subLevel) import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin (primLevelSuc, primLevelMax) import Agda.TypeChecking.Pretty import Agda.TypeChecking.Records (isRecordConstructor) import Agda.TypeChecking.Reduce import Agda.TypeChecking.Reduce.Monad import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope (permuteTel) import Agda.Utils.Either import Agda.Utils.Except import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Singleton import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | Turn a term into a non-linear pattern, treating the -- free variables as pattern variables. -- The first argument indicates the relevance we are working under: if this -- is Irrelevant, then we construct a pattern that never fails to match. -- The second argument is the number of bound variables (from pattern lambdas). class PatternFrom a b where patternFrom :: Relevance -> Int -> a -> TCM b instance (PatternFrom a b) => PatternFrom [a] [b] where patternFrom r k = traverse $ patternFrom r k instance (PatternFrom a b) => PatternFrom (Arg a) (Arg b) where patternFrom r k u = let r' = r `composeRelevance` getRelevance u in traverse (patternFrom r' k) u instance (PatternFrom a NLPat) => PatternFrom (Elim' a) (Elim' NLPat) where patternFrom r k (Apply u) = let r' = r `composeRelevance` getRelevance u in Apply <$> traverse (patternFrom r' k) u patternFrom r k (Proj o f) = return $ Proj o f instance (PatternFrom a b) => PatternFrom (Dom a) (Dom b) where patternFrom r k = traverse $ patternFrom r k instance PatternFrom Type NLPType where patternFrom r k a = NLPType <$> patternFrom r k (getSort a) <*> patternFrom r k (unEl a) instance PatternFrom Sort NLPat where patternFrom r k s = do s <- reduce s let done = return PWild case s of Type l -> patternFrom Irrelevant k (Level l) Prop -> done Inf -> done SizeUniv -> done DLub _ _ -> done instance PatternFrom Term NLPat where patternFrom r k v = do v <- unLevel =<< reduce v let done = if isIrrelevant r then return PWild else return $ PTerm v case ignoreSharing v of Var i es | i < k -> PBoundVar i <$> patternFrom r k es | otherwise -> do -- The arguments of `var i` should be distinct bound variables -- in order to build a Miller pattern let mbvs = mfilter fastDistinct $ forM es $ \e -> do e <- isApplyElim e case ignoreSharing $ unArg e of Var j [] | j < k -> Just $ e $> j _ -> Nothing case mbvs of Just bvs -> do let i' = i-k allBoundVars = IntSet.fromList (downFrom k) ok = not (isIrrelevant r) || IntSet.fromList (map unArg bvs) == allBoundVars if ok then return (PVar i bvs) else done Nothing -> done Lam i t -> PLam i <$> patternFrom r k t Lit{} -> done Def f es | isIrrelevant r -> done Def f es -> do Def lsuc [] <- ignoreSharing <$> primLevelSuc Def lmax [] <- ignoreSharing <$> primLevelMax case es of [x] | f == lsuc -> done [x , y] | f == lmax -> done _ -> PDef f <$> patternFrom r k es Con c ci vs | isIrrelevant r -> do mr <- isRecordConstructor (conName c) case mr of Just (_, def) | recEtaEquality def -> PDef (conName c) <$> patternFrom r k (Apply <$> vs) _ -> done Con c ci vs -> PDef (conName c) <$> patternFrom r k (Apply <$> vs) Pi a b | isIrrelevant r -> done Pi a b -> PPi <$> patternFrom r k a <*> patternFrom r k b Sort s -> done Level l -> __IMPOSSIBLE__ DontCare{} -> return PWild MetaV{} -> __IMPOSSIBLE__ Shared{} -> __IMPOSSIBLE__ instance (PatternFrom a b) => PatternFrom (Abs a) (Abs b) where patternFrom r k (Abs name x) = Abs name <$> patternFrom r (k+1) x patternFrom r k (NoAbs name x) = NoAbs name <$> patternFrom r k x -- | Monad for non-linear matching. type NLM = ExceptT Blocked_ (StateT NLMState ReduceM) data NLMState = NLMState { _nlmSub :: Sub , _nlmEqs :: PostponedEquations } instance Null NLMState where empty = NLMState { _nlmSub = empty , _nlmEqs = empty } null s = null (s^.nlmSub) && null (s^.nlmEqs) nlmSub :: Lens' Sub NLMState nlmSub f s = f (_nlmSub s) <&> \x -> s {_nlmSub = x} nlmEqs :: Lens' PostponedEquations NLMState nlmEqs f s = f (_nlmEqs s) <&> \x -> s {_nlmEqs = x} liftRed :: ReduceM a -> NLM a liftRed = lift . lift runNLM :: NLM () -> ReduceM (Either Blocked_ NLMState) runNLM nlm = do (ok,out) <- runStateT (runExceptT nlm) empty case ok of Left block -> return $ Left block Right _ -> return $ Right out matchingBlocked :: Blocked_ -> NLM () matchingBlocked = throwError -- | Add substitution @i |-> v@ to result of matching. tellSub :: Relevance -> Int -> Term -> NLM () tellSub r i v = do old <- IntMap.lookup i <$> use nlmSub case old of Nothing -> nlmSub %= IntMap.insert i (r,v) Just (r',v') | isIrrelevant r -> return () | isIrrelevant r' -> nlmSub %= IntMap.insert i (r,v) | otherwise -> whenJustM (liftRed $ equal v v') matchingBlocked tellEq :: Telescope -> Telescope -> Term -> Term -> NLM () tellEq gamma k u v = do traceSDoc "rewriting" 60 (sep [ text "adding equality between" <+> addContext (gamma `abstract` k) (prettyTCM u) , text " and " <+> addContext k (prettyTCM v) ]) $ do nlmEqs %= (PostponedEquation k u v:) type Sub = IntMap (Relevance, Term) -- | Matching against a term produces a constraint -- which we have to verify after applying -- the substitution computed by matching. data PostponedEquation = PostponedEquation { eqFreeVars :: Telescope -- ^ Telescope of free variables in the equation , eqLhs :: Term -- ^ Term from pattern, living in pattern context. , eqRhs :: Term -- ^ Term from scrutinee, living in context where matching was invoked. } type PostponedEquations = [PostponedEquation] -- | Match a non-linear pattern against a neutral term, -- returning a substitution. class Match a b where match :: Relevance -- ^ Are we currently matching in an irrelevant context? -> Telescope -- ^ The telescope of pattern variables -> Telescope -- ^ The telescope of lambda-bound variables -> a -- ^ The pattern to match -> b -- ^ The term to be matched against the pattern -> NLM () instance Match a b => Match [a] [b] where match r gamma k ps vs | length ps == length vs = zipWithM_ (match r gamma k) ps vs | otherwise = matchingBlocked $ NotBlocked ReallyNotBlocked () instance Match a b => Match (Arg a) (Arg b) where match r gamma k p v = let r' = r `composeRelevance` getRelevance p in match r' gamma k (unArg p) (unArg v) instance Match a b => Match (Elim' a) (Elim' b) where match r gamma k p v = case (p, v) of (Apply p, Apply v) -> let r' = r `composeRelevance` getRelevance p in match r' gamma k p v (Proj _ x, Proj _ y) -> if x == y then return () else traceSDoc "rewriting" 80 (sep [ text "mismatch between projections " <+> prettyTCM x , text " and " <+> prettyTCM y ]) mzero (Apply{}, Proj{} ) -> __IMPOSSIBLE__ (Proj{} , Apply{}) -> __IMPOSSIBLE__ instance Match a b => Match (Dom a) (Dom b) where match r gamma k p v = match r gamma k (C.unDom p) (C.unDom v) instance Match NLPType Type where match r gamma k (NLPType lp p) (El s a) = match r gamma k lp s >> match r gamma k p a instance Match NLPat Sort where match r gamma k p s = case (p , s) of (PWild , _ ) -> return () (p , Type l) -> match Irrelevant gamma k p l _ -> matchingBlocked $ NotBlocked ReallyNotBlocked () instance (Match a b, Subst t1 a, Subst t2 b) => Match (Abs a) (Abs b) where match r gamma k (Abs n p) (Abs _ v) = match r gamma (ExtendTel dummyDom (Abs n k)) p v match r gamma k (Abs n p) (NoAbs _ v) = match r gamma (ExtendTel dummyDom (Abs n k)) p (raise 1 v) match r gamma k (NoAbs n p) (Abs _ v) = match r gamma (ExtendTel dummyDom (Abs n k)) (raise 1 p) v match r gamma k (NoAbs _ p) (NoAbs _ v) = match r gamma k p v instance Match NLPat Level where match r gamma k p l = match r gamma k p =<< liftRed (reallyUnLevelView l) instance Match NLPat Term where match r gamma k p v = do vb <- liftRed $ reduceB' v let n = size k b = void vb v = ignoreBlocking vb prettyPat = addContext (gamma `abstract` k) (prettyTCM p) prettyTerm = addContext k (prettyTCM v) traceSDoc "rewriting" 100 (sep [ text "matching" <+> prettyPat , text "with" <+> prettyTerm]) $ do let yes = return () no msg = do traceSDoc "rewriting" 80 (sep [ text "mismatch between" <+> prettyPat , text " and " <+> prettyTerm , msg ]) $ do matchingBlocked b block b' = do traceSDoc "rewriting" 80 (sep [ text "matching blocked on meta" , text (show b) ]) $ do matchingBlocked (b `mappend` b') case p of PWild -> yes PVar i bvs -> do let allowedVars :: IntSet allowedVars = IntSet.fromList (map unArg bvs) isBadVar :: Int -> Bool isBadVar i = i < n && not (i `IntSet.member` allowedVars) perm :: Permutation perm = Perm n $ reverse $ map unArg $ bvs tel :: Telescope tel = permuteTel perm k ok <- liftRed $ reallyFree isBadVar v case ok of Left b -> block b Right Nothing -> no (text "") Right (Just v) -> tellSub r (i-n) $ teleLam tel $ renameP __IMPOSSIBLE__ perm v PDef f ps -> do v <- liftRed $ constructorForm =<< unLevel v case ignoreSharing v of Def f' es | f == f' -> match r gamma k ps es Con c _ vs | f == conName c -> match r gamma k ps (Apply <$> vs) | otherwise -> do -- @c@ may be a record constructor mr <- liftRed $ isRecordConstructor (conName c) case mr of Just (_, def) | recEtaEquality def -> do let fs = recFields def qs = map (fmap $ \f -> PDef f (ps ++ [Proj ProjSystem f])) fs match r gamma k qs vs _ -> no (text "") Lam i u -> do let pbody = PDef f (raise 1 ps ++ [Apply $ Arg i $ PTerm (var 0)]) body = absBody u match r gamma (ExtendTel dummyDom (Abs (absName u) k)) pbody body MetaV m es -> do matchingBlocked $ Blocked m () v' -> do -- @f@ may be a record constructor as well mr <- liftRed $ isRecordConstructor f case mr of Just (_, def) | recEtaEquality def -> do let fs = recFields def ws = map (fmap $ \f -> v `applyE` [Proj ProjSystem f]) fs qs = fromMaybe __IMPOSSIBLE__ $ allApplyElims ps match r gamma k qs ws _ -> no (text "") PLam i p' -> do let body = Abs (absName p') $ raise 1 v `apply` [Arg i (var 0)] match r gamma k p' body PPi pa pb -> case ignoreSharing v of Pi a b -> match r gamma k pa a >> match r gamma k pb b MetaV m es -> matchingBlocked $ Blocked m () _ -> no (text "") PBoundVar i ps -> case ignoreSharing v of Var i' es | i == i' -> match r gamma k ps es Con c _ vs -> do -- @c@ may be a record constructor mr <- liftRed $ isRecordConstructor (conName c) case mr of Just (_, def) | recEtaEquality def -> do let fs = recFields def qs = map (fmap $ \f -> PBoundVar i (ps ++ [Proj ProjSystem f])) fs match r gamma k qs vs _ -> no (text "") Lam info u -> do let pbody = PBoundVar i (raise 1 ps ++ [Apply $ Arg info $ PTerm (var 0)]) body = absBody u match r gamma (ExtendTel dummyDom (Abs (absName u) k)) pbody body MetaV m es -> matchingBlocked $ Blocked m () _ -> no (text "") PTerm u -> tellEq gamma k u v -- Checks if the given term contains any free variables that satisfy the -- given condition on their DBI, possibly normalizing the term in the process. -- Returns `Right Nothing` if there are such variables, `Right (Just v')` -- if there are none (where v' is the possibly normalized version of the given -- term) or `Left b` if the problem is blocked on a meta. reallyFree :: (Reduce a, Normalise a, Free a) => (Int -> Bool) -> a -> ReduceM (Either Blocked_ (Maybe a)) reallyFree f v = do let xs = getVars v if null (stronglyRigidVars xs) && null (unguardedVars xs) then do if null (weaklyRigidVars xs) && null (flexibleVars xs) && null (irrelevantVars xs) then return $ Right $ Just v else do bv <- normaliseB' v let b = void bv v = ignoreBlocking bv xs = getVars v b' = foldMap (foldMap $ \m -> Blocked m ()) $ flexibleVars xs if null (stronglyRigidVars xs) && null (unguardedVars xs) && null (weaklyRigidVars xs) && null (irrelevantVars xs) then if null (flexibleVars xs) then return $ Right $ Just v else return $ Left $ b `mappend` b' else return $ Right Nothing else return $ Right Nothing where getVars v = runFree (\ i -> if f i then singleton i else empty) IgnoreNot v makeSubstitution :: Telescope -> Sub -> Substitution makeSubstitution gamma sub = prependS __IMPOSSIBLE__ (map val [0 .. size gamma-1]) IdS where val i = case IntMap.lookup i sub of Just (Irrelevant, v) -> Just $ dontCare v Just (_ , v) -> Just v Nothing -> Nothing checkPostponedEquations :: Substitution -> PostponedEquations -> ReduceM (Maybe Blocked_) checkPostponedEquations sub eqs = forM' eqs $ \ (PostponedEquation k lhs rhs) -> do let lhs' = applySubst (liftS (size k) sub) lhs traceSDoc "rewriting" 60 (sep [ text "checking postponed equality between" , addContext k (prettyTCM lhs') , text " and " , addContext k (prettyTCM rhs) ]) $ do equal lhs' rhs -- main function nonLinMatch :: (Match a b) => Telescope -> a -> b -> ReduceM (Either Blocked_ Substitution) nonLinMatch gamma p v = do let no msg b = traceSDoc "rewriting" 80 (sep [ text "matching failed during" <+> text msg , text "blocking: " <+> text (show b) ]) $ return (Left b) caseEitherM (runNLM $ match Relevant gamma EmptyTel p v) (no "matching") $ \ s -> do let sub = makeSubstitution gamma $ s^.nlmSub eqs = s^.nlmEqs traceSDoc "rewriting" 90 (text $ "sub = " ++ show sub) $ do ok <- checkPostponedEquations sub eqs case ok of Nothing -> return $ Right sub Just b -> no "checking of postponed equations" b -- | Untyped βη-equality, does not handle things like empty record types. -- Returns `Nothing` if the terms are equal, or `Just b` if the terms are not -- (where b contains information about possible metas blocking the comparison) -- TODO: implement a type-directed, lazy version of this function. equal :: Term -> Term -> ReduceM (Maybe Blocked_) equal u v = do (u, v) <- etaContract =<< normalise' (u, v) let ok = u == v metas = allMetas (u, v) block = caseMaybe (headMaybe metas) (NotBlocked ReallyNotBlocked ()) (\m -> Blocked m ()) if ok then return Nothing else do traceSDoc "rewriting" 80 (sep [ text "mismatch between " <+> prettyTCM u , text " and " <+> prettyTCM v ]) $ do return $ Just block -- | Normalise the given term but also preserve blocking tags -- TODO: implement a more efficient version of this. normaliseB' :: (Reduce t, Normalise t) => t -> ReduceM (Blocked t) normaliseB' = normalise' >=> reduceB' Agda-2.5.3/src/full/Agda/TypeChecking/Substitute/0000755000000000000000000000000013154613124017660 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Substitute/Class.hs0000644000000000000000000002300613154613124021262 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.TypeChecking.Substitute.Class where import Control.Arrow ((***), second) import Agda.Syntax.Common import Agda.Syntax.Internal import Agda.TypeChecking.Free import Agda.TypeChecking.Substitute.DeBruijn import Agda.Utils.Empty #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Application --------------------------------------------------------------------------- -- | Apply something to a bunch of arguments. -- Preserves blocking tags (application can never resolve blocking). class Apply t where apply :: t -> Args -> t applyE :: t -> Elims -> t apply t args = applyE t $ map Apply args applyE t es = apply t $ map argFromElim es -- precondition: all @es@ are @Apply@s -- | Apply to some default arguments. applys :: Apply t => t -> [Term] -> t applys t vs = apply t $ map defaultArg vs -- | Apply to a single default argument. apply1 :: Apply t => t -> Term -> t apply1 t u = applys t [ u ] --------------------------------------------------------------------------- -- * Abstraction --------------------------------------------------------------------------- -- | @(abstract args v) `apply` args --> v[args]@. class Abstract t where abstract :: Telescope -> t -> t --------------------------------------------------------------------------- -- * Substitution and raising/shifting/weakening --------------------------------------------------------------------------- -- | Apply a substitution. -- For terms: -- -- Γ ⊢ ρ : Δ -- Δ ⊢ t : A -- ----------- -- Γ ⊢ tρ : Aρ class DeBruijn t => Subst t a | a -> t where applySubst :: Substitution' t -> a -> a raise :: Subst t a => Nat -> a -> a raise = raiseFrom 0 raiseFrom :: Subst t a => Nat -> Nat -> a -> a raiseFrom n k = applySubst (liftS n $ raiseS k) -- | Replace de Bruijn index i by a 'Term' in something. subst :: Subst t a => Int -> t -> a -> a subst i u = applySubst $ singletonS i u strengthen :: Subst t a => Empty -> a -> a strengthen err = applySubst (compactS err [Nothing]) -- | Replace what is now de Bruijn index 0, but go under n binders. -- @substUnder n u == subst n (raise n u)@. substUnder :: Subst t a => Nat -> t -> a -> a substUnder n u = applySubst (liftS n (singletonS 0 u)) -- ** Identity instances instance Subst Term QName where applySubst _ q = q --------------------------------------------------------------------------- -- * Explicit substitutions --------------------------------------------------------------------------- -- See Syntax.Internal for the definition. idS :: Substitution' a idS = IdS wkS :: Int -> Substitution' a -> Substitution' a wkS 0 rho = rho wkS n (Wk m rho) = Wk (n + m) rho wkS n (EmptyS err) = EmptyS err wkS n rho = Wk n rho raiseS :: Int -> Substitution' a raiseS n = wkS n idS consS :: DeBruijn a => a -> Substitution' a -> Substitution' a consS t (Wk m rho) | Just n <- deBruijnView t, n + 1 == m = wkS (m - 1) (liftS 1 rho) consS u rho = seq u (u :# rho) -- | To replace index @n@ by term @u@, do @applySubst (singletonS n u)@. -- @ -- Γ, Δ ⊢ u : A -- --------------------------------- -- Γ, Δ ⊢ singletonS |Δ| u : Γ, A, Δ -- @ singletonS :: DeBruijn a => Int -> a -> Substitution' a singletonS n u = map deBruijnVar [0..n-1] ++# consS u (raiseS n) -- ALT: foldl (\ s i -> deBruijnVar i `consS` s) (consS u $ raiseS n) $ downFrom n -- | Single substitution without disturbing any deBruijn indices. -- @ -- Γ, A, Δ ⊢ u : A -- --------------------------------- -- Γ, A, Δ ⊢ inplace |Δ| u : Γ, A, Δ -- @ inplaceS :: Subst a a => Int -> a -> Substitution' a inplaceS k u = singletonS k u `composeS` liftS (k + 1) (raiseS 1) -- | Lift a substitution under k binders. liftS :: Int -> Substitution' a -> Substitution' a liftS 0 rho = rho liftS k IdS = IdS liftS k (Lift n rho) = Lift (n + k) rho liftS k rho = Lift k rho -- | @ -- Γ ⊢ ρ : Δ, Ψ -- ------------------- -- Γ ⊢ dropS |Ψ| ρ : Δ -- @ dropS :: Int -> Substitution' a -> Substitution' a dropS 0 rho = rho dropS n IdS = raiseS n dropS n (Wk m rho) = wkS m (dropS n rho) dropS n (u :# rho) = dropS (n - 1) rho dropS n (Strengthen _ rho) = dropS (n - 1) rho dropS n (Lift 0 rho) = __IMPOSSIBLE__ dropS n (Lift m rho) = wkS 1 $ dropS (n - 1) $ liftS (m - 1) rho dropS n (EmptyS err) = __IMPOSSIBLE__ -- | @applySubst (ρ `composeS` σ) v == applySubst ρ (applySubst σ v)@ composeS :: Subst a a => Substitution' a -> Substitution' a -> Substitution' a composeS rho IdS = rho composeS IdS sgm = sgm composeS rho (EmptyS err) = EmptyS err composeS rho (Wk n sgm) = composeS (dropS n rho) sgm composeS rho (u :# sgm) = applySubst rho u :# composeS rho sgm composeS rho (Strengthen err sgm) = Strengthen err (composeS rho sgm) composeS rho (Lift 0 sgm) = __IMPOSSIBLE__ composeS (u :# rho) (Lift n sgm) = u :# composeS rho (liftS (n - 1) sgm) composeS rho (Lift n sgm) = lookupS rho 0 :# composeS rho (wkS 1 (liftS (n - 1) sgm)) -- If Γ ⊢ ρ : Δ, Θ then splitS |Θ| ρ = (σ, δ), with -- Γ ⊢ σ : Δ -- Γ ⊢ δ : Θσ splitS :: Int -> Substitution' a -> (Substitution' a, Substitution' a) splitS 0 rho = (rho, EmptyS __IMPOSSIBLE__) splitS n (u :# rho) = second (u :#) $ splitS (n - 1) rho splitS n (Strengthen err rho) = second (Strengthen err) $ splitS (n - 1) rho splitS n (Lift 0 _) = __IMPOSSIBLE__ splitS n (Wk m rho) = wkS m *** wkS m $ splitS n rho splitS n IdS = (raiseS n, liftS n $ EmptyS __IMPOSSIBLE__) splitS n (Lift m rho) = wkS 1 *** liftS 1 $ splitS (n - 1) (liftS (m - 1) rho) splitS n (EmptyS err) = __IMPOSSIBLE__ infixr 4 ++# (++#) :: DeBruijn a => [a] -> Substitution' a -> Substitution' a us ++# rho = foldr consS rho us -- | @ -- Γ ⊢ ρ : Δ Γ ⊢ reverse vs : Θ -- ----------------------------- (treating Nothing as having any type) -- Γ ⊢ prependS vs ρ : Δ, Θ -- @ prependS :: DeBruijn a => Empty -> [Maybe a] -> Substitution' a -> Substitution' a prependS err us rho = foldr f rho us where f Nothing rho = Strengthen err rho f (Just u) rho = consS u rho parallelS :: DeBruijn a => [a] -> Substitution' a parallelS us = us ++# idS compactS :: DeBruijn a => Empty -> [Maybe a] -> Substitution' a compactS err us = prependS err us idS -- | Γ ⊢ (strengthenS ⊥ |Δ|) : Γ,Δ strengthenS :: Empty -> Int -> Substitution' a strengthenS err n | n < 0 = __IMPOSSIBLE__ | otherwise = iterate (Strengthen err) idS !! n lookupS :: Subst a a => Substitution' a -> Nat -> a lookupS rho i = case rho of IdS -> deBruijnVar i Wk n IdS -> let j = i + n in if j < 0 then __IMPOSSIBLE__ else deBruijnVar j Wk n rho -> applySubst (raiseS n) (lookupS rho i) u :# rho | i == 0 -> u | i < 0 -> __IMPOSSIBLE__ | otherwise -> lookupS rho (i - 1) Strengthen err rho | i == 0 -> absurd err | i < 0 -> __IMPOSSIBLE__ | otherwise -> lookupS rho (i - 1) Lift n rho | i < n -> deBruijnVar i | otherwise -> raise n $ lookupS rho (i - n) EmptyS err -> absurd err --------------------------------------------------------------------------- -- * Functions on abstractions -- and things we couldn't do before we could define 'absBody' --------------------------------------------------------------------------- -- | Instantiate an abstraction. Strict in the term. absApp :: Subst t a => Abs a -> t -> a absApp (Abs _ v) u = subst 0 u v absApp (NoAbs _ v) _ = v -- | Instantiate an abstraction. Lazy in the term, which allow it to be -- __IMPOSSIBLE__ in the case where the variable shouldn't be used but we -- cannot use 'noabsApp'. Used in Apply. lazyAbsApp :: Subst t a => Abs a -> t -> a lazyAbsApp (Abs _ v) u = applySubst (u :# IdS) v -- Note: do not use consS here! lazyAbsApp (NoAbs _ v) _ = v -- | Instantiate an abstraction that doesn't use its argument. noabsApp :: Subst t a => Empty -> Abs a -> a noabsApp err (Abs _ v) = strengthen err v noabsApp _ (NoAbs _ v) = v absBody :: Subst t a => Abs a -> a absBody (Abs _ v) = v absBody (NoAbs _ v) = raise 1 v mkAbs :: (Subst t a, Free a) => ArgName -> a -> Abs a mkAbs x v | 0 `freeIn` v = Abs x v | otherwise = NoAbs x (raise (-1) v) reAbs :: (Subst t a, Free a) => Abs a -> Abs a reAbs (NoAbs x v) = NoAbs x v reAbs (Abs x v) = mkAbs x v -- | @underAbs k a b@ applies @k@ to @a@ and the content of -- abstraction @b@ and puts the abstraction back. -- @a@ is raised if abstraction was proper such that -- at point of application of @k@ and the content of @b@ -- are at the same context. -- Precondition: @a@ and @b@ are at the same context at call time. underAbs :: Subst t a => (a -> b -> b) -> a -> Abs b -> Abs b underAbs cont a b = case b of Abs x t -> Abs x $ cont (raise 1 a) t NoAbs x t -> NoAbs x $ cont a t -- | @underLambdas n k a b@ drops @n@ initial 'Lam's from @b@, -- performs operation @k@ on @a@ and the body of @b@, -- and puts the 'Lam's back. @a@ is raised correctly -- according to the number of abstractions. underLambdas :: Subst Term a => Int -> (a -> Term -> Term) -> a -> Term -> Term underLambdas n cont a v = loop n a v where loop 0 a v = cont a v loop n a v = case ignoreSharing v of Lam h b -> Lam h $ underAbs (loop $ n-1) a b _ -> __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/Substitute/DeBruijn.hs0000644000000000000000000000261313154613124021720 0ustar0000000000000000 module Agda.TypeChecking.Substitute.DeBruijn where import Agda.Syntax.Common import Agda.Syntax.Internal -- | Things we can substitute for a variable. -- Needs to be able to represent variables, e.g. for substituting under binders. class DeBruijn a where -- | Produce a variable without name suggestion. deBruijnVar :: Int -> a deBruijnVar = debruijnNamedVar underscore -- | Produce a variable with name suggestion. debruijnNamedVar :: String -> Int -> a debruijnNamedVar _ = deBruijnVar -- | Are we dealing with a variable? -- If yes, what is its index? deBruijnView :: a -> Maybe Int -- | We can substitute @Term@s for variables. instance DeBruijn Term where deBruijnVar = var deBruijnView u = case ignoreSharing u of Var i [] -> Just i Level l -> deBruijnView l _ -> Nothing instance DeBruijn LevelAtom where deBruijnVar = NeutralLevel ReallyNotBlocked . deBruijnVar deBruijnView l = case l of NeutralLevel _ u -> deBruijnView u UnreducedLevel u -> deBruijnView u MetaLevel{} -> Nothing BlockedLevel{} -> Nothing instance DeBruijn PlusLevel where deBruijnVar = Plus 0 . deBruijnVar deBruijnView l = case l of Plus 0 a -> deBruijnView a _ -> Nothing instance DeBruijn Level where deBruijnVar i = Max [deBruijnVar i] deBruijnView l = case l of Max [p] -> deBruijnView p _ -> Nothing Agda-2.5.3/src/full/Agda/TypeChecking/Reduce/0000755000000000000000000000000013154613124016714 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Reduce/Fast.hs-boot0000644000000000000000000000024413154613124021106 0ustar0000000000000000 module Agda.TypeChecking.Reduce.Fast where import Agda.Syntax.Internal import Agda.TypeChecking.Monad.Base fastReduce :: Bool -> Term -> ReduceM (Blocked Term) Agda-2.5.3/src/full/Agda/TypeChecking/Reduce/Monad.hs0000644000000000000000000001151013154613124020304 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.TypeChecking.Reduce.Monad ( constructorForm , enterClosure , underAbstraction_ , getConstInfo , isInstantiatedMeta , lookupMeta , askR, applyWhenVerboseS ) where import Prelude hiding (null) import Control.Arrow ((***), first, second) import Control.Applicative hiding (empty) import Control.Monad.Reader import qualified Data.Map as Map import Data.Maybe import Data.Monoid import Debug.Trace import System.IO.Unsafe import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Syntax.Internal import Agda.TypeChecking.Monad hiding ( enterClosure, underAbstraction_, underAbstraction, addCtx, mkContextEntry, isInstantiatedMeta, verboseS, typeOfConst, lookupMeta ) import Agda.TypeChecking.Monad.Builtin hiding ( constructorForm ) import Agda.TypeChecking.Substitute import Agda.Interaction.Options import qualified Agda.Utils.HashMap as HMap import Agda.Utils.Lens import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible gets :: (TCState -> a) -> ReduceM a gets f = f . redSt <$> ReduceM ask useR :: Lens' a TCState -> ReduceM a useR l = gets (^.l) askR :: ReduceM ReduceEnv askR = ReduceM ask localR :: (ReduceEnv -> ReduceEnv) -> ReduceM a -> ReduceM a localR f = ReduceM . local f . unReduceM instance HasOptions ReduceM where pragmaOptions = useR stPragmaOptions commandLineOptions = do p <- useR stPragmaOptions cl <- gets $ stPersistentOptions . stPersistentState return $ cl{ optPragmaOptions = p } instance HasBuiltins ReduceM where getBuiltinThing b = liftM2 mplus (Map.lookup b <$> useR stLocalBuiltins) (Map.lookup b <$> useR stImportedBuiltins) constructorForm :: Term -> ReduceM Term constructorForm v = do mz <- getBuiltin' builtinZero ms <- getBuiltin' builtinSuc return $ fromMaybe v $ constructorForm' mz ms v enterClosure :: Closure a -> (a -> ReduceM b) -> ReduceM b enterClosure (Closure sig env scope pars x) f = localR (mapRedEnvSt inEnv inState) (f x) where inEnv e = env { envAllowDestructiveUpdate = envAllowDestructiveUpdate e } inState s = -- TODO: use the signature here? would that fix parts of issue 118? set stScope scope $ set stModuleParameters pars s withFreshR :: HasFresh i => (i -> ReduceM a) -> ReduceM a withFreshR f = do s <- gets id let (i, s') = nextFresh s localR (mapRedSt $ const s') (f i) withFreshName :: Range -> ArgName -> (Name -> ReduceM a) -> ReduceM a withFreshName r s k = withFreshR $ \i -> k (mkName r i s) withFreshName_ :: ArgName -> (Name -> ReduceM a) -> ReduceM a withFreshName_ = withFreshName noRange mkContextEntry :: Dom (Name, Type) -> (ContextEntry -> ReduceM a) -> ReduceM a mkContextEntry x k = withFreshR $ \i -> k (Ctx i x) addCtx :: Name -> Dom Type -> ReduceM a -> ReduceM a addCtx x a ret = do ctx <- asks $ map (nameConcrete . fst . unDom . ctxEntry) . envContext let x' = head $ filter (notTaken ctx) $ iterate nextName x mkContextEntry ((x',) <$> a) $ \ce -> local (\e -> e { envContext = ce : envContext e }) ret -- let-bindings keep track of own their context where notTaken xs x = isNoName x || nameConcrete x `notElem` xs underAbstraction :: Subst t a => Dom Type -> Abs a -> (a -> ReduceM b) -> ReduceM b underAbstraction _ (NoAbs _ v) f = f v underAbstraction t a f = withFreshName_ (realName $ absName a) $ \x -> addCtx x t $ f (absBody a) where realName s = if isNoName s then "x" else s underAbstraction_ :: Subst t a => Abs a -> (a -> ReduceM b) -> ReduceM b underAbstraction_ = underAbstraction dummyDom lookupMeta :: MetaId -> ReduceM MetaVariable lookupMeta i = fromMaybe __IMPOSSIBLE__ . Map.lookup i <$> useR stMetaStore isInstantiatedMeta :: MetaId -> ReduceM Bool isInstantiatedMeta i = do mv <- lookupMeta i return $ case mvInstantiation mv of InstV{} -> True _ -> False -- | Apply a function if a certain verbosity level is activated. -- -- Precondition: The level must be non-negative. {-# SPECIALIZE applyWhenVerboseS :: VerboseKey -> Int -> (ReduceM a -> ReduceM a) -> ReduceM a-> ReduceM a #-} applyWhenVerboseS :: HasOptions m => VerboseKey -> Int -> (m a -> m a) -> m a -> m a applyWhenVerboseS k n f a = ifM (hasVerbosity k n) (f a) a instance MonadDebug ReduceM where traceDebugMessage n s cont = do ReduceEnv env st <- askR unsafePerformIO $ do _ <- runTCM env st $ displayDebugMessage n s return $ cont formatDebugMessage k n d = do ReduceEnv env st <- askR unsafePerformIO $ do (s , _) <- runTCM env st $ formatDebugMessage k n d return $ return s instance HasConstInfo ReduceM where getRewriteRulesFor = defaultGetRewriteRulesFor (gets id) getConstInfo' q = do ReduceEnv env st <- askR defaultGetConstInfo st env q Agda-2.5.3/src/full/Agda/TypeChecking/Reduce/Fast.hs0000644000000000000000000005377713154613124020170 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE BangPatterns #-} {-# LANGUAGE PatternGuards #-} {-| This module contains an optimised implementation of the reduction algorithm from 'Agda.TypeChecking.Reduce' and 'Agda.TypeChecking.CompiledClause.Match'. It runs roughly an order of magnitude faster than the original implementation. The differences are the following: - Only applies when we don't have --sharing and when all reductions are allowed. This means we can skip a number of checks that would otherwise be performed at each reduction step. - Does not track whether simplifications were made. This information is only used when trying to simplify terms, so the simplifier runs the slow implementation. - Precomputes primZero and primSuc. Since all the functions involved in reduction are implemented in this module in a single where block, we can look up zero and suc once instead of once for each reduction step. - Run outside ReduceM ReduceM is already just a plain reader monad, but pulling out the environment and doing all reduction non-monadically saves a significant amount of time. - Memoise getConstInfo. A big chunk of the time during reduction is spent looking up definitions in the signature. Any long-running reduction will use only a handful definitions though, so memoising getConstInfo is a big win. - Optimised case trees. Since we memoise getConstInfo we can do some preprocessing of the definitions, returning a 'CompactDef' instead of a 'Definition'. In particular we streamline the case trees used for matching in a few ways: - Drop constructor arity information. - Use NameId instead of QName as map keys. - Special branch for natural number successor. None of these changes would make sense to incorporate into the actual case trees. The first two loses information that we need in other places and the third would complicate a lot of code working with case trees. - Optimised parallel substitution. When substituting arguments into function bodies we always have a complete (a term for every free variable) parallel substitution. We run an specialised substitution for this case that falls back to normal substitution when it hits a binder. -} module Agda.TypeChecking.Reduce.Fast ( fastReduce ) where import Control.Applicative import Control.Monad.Reader import Data.Map (Map) import qualified Data.Map as Map import Data.Traversable (traverse) import System.IO.Unsafe import Data.IORef import Agda.Syntax.Internal import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Syntax.Literal import Agda.TypeChecking.CompiledClause import Agda.TypeChecking.Monad import Agda.TypeChecking.Pretty import Agda.TypeChecking.Reduce as R import Agda.TypeChecking.Rewriting (rewrite) import Agda.TypeChecking.Reduce.Monad as RedM import Agda.TypeChecking.Substitute import Agda.TypeChecking.Monad.Builtin hiding (constructorForm) import Agda.TypeChecking.CompiledClause.Match import Agda.Interaction.Options import Agda.Utils.Maybe import Agda.Utils.Memo import Agda.Utils.Function import Agda.Utils.Functor #include "undefined.h" import Agda.Utils.Impossible -- Compact definitions ---------------------------------------------------- -- This is what the memoised getConstInfo returns. We essentially pick out only the -- information needed for fast reduction from the definition. data CompactDef = CompactDef { cdefDelayed :: Bool , cdefNonterminating :: Bool , cdefDef :: CompactDefn , cdefRewriteRules :: RewriteRules } data CompactDefn = CFun { cfunCompiled :: FastCompiledClauses, cfunProjection :: Maybe QName } | CCon { cconSrcCon :: ConHead } | CForce -- ^ primForce | CTyCon -- ^ Datatype or record type. Need to know this for primForce. | COther -- ^ In this case we fall back to slow reduction compactDef :: Maybe ConHead -> Maybe ConHead -> Maybe QName -> Definition -> RewriteRules -> ReduceM CompactDef compactDef z s pf def rewr = do cdefn <- case theDef def of _ | Just (defName def) == pf -> pure CForce Constructor{conSrcCon = c} -> pure CCon{cconSrcCon = c} Function{funCompiled = Just cc, funClauses = _:_, funProjection = proj} -> pure CFun{ cfunCompiled = fastCompiledClauses z s cc , cfunProjection = projOrig <$> proj } Datatype{dataClause = Nothing} -> pure CTyCon Record{recClause = Nothing} -> pure CTyCon _ -> pure COther return $ CompactDef { cdefDelayed = defDelayed def == Delayed , cdefNonterminating = defNonterminating def , cdefDef = cdefn , cdefRewriteRules = rewr } -- Faster case trees ------------------------------------------------------ data FastCase c = FBranches { fprojPatterns :: Bool -- ^ We are constructing a record here (copatterns). -- 'conBranches' lists projections. , fconBranches :: Map NameId c -- ^ Map from constructor (or projection) names to their arity -- and the case subtree. (Projections have arity 0.) , fsucBranch :: Maybe c , flitBranches :: Map Literal c -- ^ Map from literal to case subtree. , fcatchAllBranch :: Maybe c -- ^ (Possibly additional) catch-all clause. } -- | Case tree with bodies. data FastCompiledClauses = FCase Int (FastCase FastCompiledClauses) -- ^ @Case n bs@ stands for a match on the @n@-th argument -- (counting from zero) with @bs@ as the case branches. -- If the @n@-th argument is a projection, we have only 'conBranches' -- with arity 0. | FDone [Arg ArgName] Term -- ^ @Done xs b@ stands for the body @b@ where the @xs@ contains hiding -- and name suggestions for the free variables. This is needed to build -- lambdas on the right hand side for partial applications which can -- still reduce. | FFail -- ^ Absurd case. type FastStack = [(FastCompiledClauses, MaybeReducedElims, Elims -> Elims)] fastCompiledClauses :: Maybe ConHead -> Maybe ConHead -> CompiledClauses -> FastCompiledClauses fastCompiledClauses z s cc = case cc of Fail -> FFail Done xs b -> FDone xs b Case (Arg _ n) bs -> FCase n (fastCase z s bs) fastCase :: Maybe ConHead -> Maybe ConHead -> Case CompiledClauses -> FastCase FastCompiledClauses fastCase z s (Branches proj con lit wild) = FBranches { fprojPatterns = proj , fconBranches = Map.mapKeysMonotonic (nameId . qnameName) $ fmap (fastCompiledClauses z s . content) con , fsucBranch = fmap (fastCompiledClauses z s . content) $ flip Map.lookup con . conName =<< s , flitBranches = fmap (fastCompiledClauses z s) lit , fcatchAllBranch = fmap (fastCompiledClauses z s) wild } {-# INLINE lookupCon #-} lookupCon :: QName -> FastCase c -> Maybe c lookupCon c (FBranches _ cons _ _ _) = Map.lookup (nameId $ qnameName c) cons -- QName memo ------------------------------------------------------------- {-# NOINLINE memoQName #-} memoQName :: (QName -> a) -> (QName -> a) memoQName f = unsafePerformIO $ do tbl <- newIORef Map.empty return (unsafePerformIO . f' tbl) where f' tbl x = do let i = nameId (qnameName x) m <- readIORef tbl case Map.lookup i m of Just y -> return y Nothing -> do let y = f x writeIORef tbl (Map.insert i y m) return y -- Faster substitution ---------------------------------------------------- -- Precondition: All free variables of the term are assigned values in the -- list. -- Reverts to normal substitution if it hits a binder or other icky stuff (like -- levels). It's strict in the shape of the result to avoid creating huge -- thunks for accumulator arguments. strictSubst :: Bool -> [Term] -> Term -> Term strictSubst strict us | not strict = applySubst rho | otherwise = go 0 where rho = parallelS us go k v = case v of Var x es | x < k -> Var x $! map' (goE k) es | otherwise -> applyE (raise k $ us !! (x - k)) $! map' (goE k) es Def f es -> defApp f [] $! map' (goE k) es Con c ci vs -> Con c ci $! map' (mapArg' $ go k) vs Lam i b -> Lam i $! goAbs k b Lit{} -> v _ -> applySubst (liftS k rho) v goE k (Apply v) = Apply $! mapArg' (go k) v goE _ p = p goAbs k (Abs x v) = Abs x $! go (k + 1) v goAbs k (NoAbs x v) = NoAbs x $! go k v map' :: (a -> b) -> [a] -> [b] map' f [] = [] map' f (x : xs) = ((:) $! f x) $! map' f xs mapArg' :: (a -> b) -> Arg a -> Arg b mapArg' f (Arg i x) = Arg i $! f x -- Fast reduction --------------------------------------------------------- -- | First argument: allow non-terminating reductions. fastReduce :: Bool -> Term -> ReduceM (Blocked Term) fastReduce allowNonTerminating v = do let name (Con c _ _) = c name _ = __IMPOSSIBLE__ z <- fmap name <$> getBuiltin' builtinZero s <- fmap name <$> getBuiltin' builtinSuc pf <- fmap primFunName <$> getPrimitive' "primForce" rwr <- optRewriting <$> pragmaOptions constInfo <- unKleisli $ \f -> do info <- getConstInfo f rewr <- instantiateRewriteRules =<< getRewriteRulesFor f compactDef z s pf info rewr ReduceM $ \ env -> reduceTm env (memoQName constInfo) allowNonTerminating rwr z s v unKleisli :: (a -> ReduceM b) -> ReduceM (a -> b) unKleisli f = ReduceM $ \ env x -> unReduceM (f x) env reduceTm :: ReduceEnv -> (QName -> CompactDef) -> Bool -> Bool -> Maybe ConHead -> Maybe ConHead -> Term -> Blocked Term reduceTm env !constInfo allowNonTerminating hasRewriting zero suc = reduceB' 0 where -- Force substitutions every nth step to avoid memory leaks. Doing it in -- every is too expensive (issue 2215). strictEveryNth = 1000 runReduce m = unReduceM m env conNameId = nameId . qnameName . conName isZero = case zero of Nothing -> const False Just z -> (conNameId z ==) . conNameId isSuc = case suc of Nothing -> const False Just s -> (conNameId s ==) . conNameId reduceB' steps v = case v of Def f es -> unfoldDefinitionE steps False reduceB' (Def f []) f es Con c ci vs -> -- Constructors can reduce' when they come from an -- instantiated module. case unfoldDefinition steps False reduceB' (Con c ci []) (conName c) vs of NotBlocked r v -> NotBlocked r $ reduceNat v b -> b Lit{} -> done Var{} -> done _ -> runReduce (slowReduceTerm v) where done = notBlocked v reduceNat v@(Con c ci []) | isZero c = Lit $ LitNat (getRange c) 0 reduceNat v@(Con c ci [a]) | isSuc c = inc . ignoreBlocking $ reduceB' 0 (unArg a) where inc (Lit (LitNat r n)) = Lit (LitNat noRange $ n + 1) inc w = Con c ci [defaultArg w] reduceNat v = v originalProjection :: QName -> QName originalProjection q = case cdefDef $ constInfo q of CFun{ cfunProjection = Just p } -> p _ -> __IMPOSSIBLE__ -- Andreas, 2013-03-20 recursive invokations of unfoldCorecursion -- need also to instantiate metas, see Issue 826. unfoldCorecursionE :: Elim -> Blocked Elim unfoldCorecursionE (Proj o p) = notBlocked $ Proj o $ originalProjection p unfoldCorecursionE (Apply (Arg info v)) = fmap (Apply . Arg info) $ unfoldCorecursion 0 v unfoldCorecursion :: Int -> Term -> Blocked Term unfoldCorecursion steps (Def f es) = unfoldDefinitionE steps True unfoldCorecursion (Def f []) f es unfoldCorecursion steps v = reduceB' steps v -- | If the first argument is 'True', then a single delayed clause may -- be unfolded. unfoldDefinition :: Int -> Bool -> (Int -> Term -> Blocked Term) -> Term -> QName -> Args -> Blocked Term unfoldDefinition steps unfoldDelayed keepGoing v f args = unfoldDefinitionE steps unfoldDelayed keepGoing v f (map Apply args) unfoldDefinitionE :: Int -> Bool -> (Int -> Term -> Blocked Term) -> Term -> QName -> Elims -> Blocked Term unfoldDefinitionE steps unfoldDelayed keepGoing v f es = case unfoldDefinitionStep steps unfoldDelayed (constInfo f) v f es of NoReduction v -> v YesReduction _ v -> (keepGoing $! steps + 1) v unfoldDefinitionStep :: Int -> Bool -> CompactDef -> Term -> QName -> Elims -> Reduced (Blocked Term) Term unfoldDefinitionStep steps unfoldDelayed CompactDef{cdefDelayed = delayed, cdefNonterminating = nonterm, cdefDef = def, cdefRewriteRules = rewr} v0 f es = let v = v0 `applyE` es -- Non-terminating functions -- (i.e., those that failed the termination check) -- and delayed definitions -- are not unfolded unless explicitely permitted. dontUnfold = (not allowNonTerminating && nonterm) || (not unfoldDelayed && delayed) in case def of CCon{cconSrcCon = c} -> if hasRewriting then runReduce $ rewrite (notBlocked ()) (Con c ConOSystem []) rewr es else NoReduction $ notBlocked $ Con c ConOSystem [] `applyE` es CFun{cfunCompiled = cc} -> reduceNormalE steps v0 f (map notReduced es) dontUnfold cc CForce -> reduceForce unfoldDelayed v0 f es CTyCon -> if hasRewriting then runReduce $ rewrite (notBlocked ()) v0 rewr es else NoReduction $ notBlocked v COther -> runReduce $ R.unfoldDefinitionStep unfoldDelayed v0 f es where yesReduction = YesReduction NoSimplification reduceForce :: Bool -> Term -> QName -> Elims -> Reduced (Blocked Term) Term reduceForce unfoldDelayed v0 pf (Apply a : Apply b : Apply s : Apply t : Apply u : Apply f : es) = case reduceB' 0 (unArg u) of ub@Blocked{} -> noGo ub ub@(NotBlocked _ u) | isWHNF u -> yesReduction $ unArg f `applyE` (Apply (defaultArg u) : es) | otherwise -> noGo ub where noGo ub = NoReduction $ ub <&> \ u -> Def pf (Apply a : Apply b : Apply s : Apply t : Apply (defaultArg u) : Apply f : es) isWHNF u = case u of Lit{} -> True Con{} -> True Lam{} -> True Pi{} -> True Sort{} -> True Level{} -> True DontCare{} -> True MetaV{} -> False Var{} -> False Def q _ -> isTyCon q Shared{} -> __IMPOSSIBLE__ isTyCon q = case cdefDef $ constInfo q of CTyCon -> True _ -> False -- TODO: partially applied to u reduceForce unfoldDelayed v0 pf es = runReduce $ R.unfoldDefinitionStep unfoldDelayed v0 f es reduceNormalE :: Int -> Term -> QName -> [MaybeReduced Elim] -> Bool -> FastCompiledClauses -> Reduced (Blocked Term) Term reduceNormalE steps v0 f es dontUnfold cc | dontUnfold = defaultResult -- non-terminating or delayed | otherwise = case match' steps f [(cc, es, id)] of YesReduction s u -> YesReduction s u NoReduction es' -> if hasRewriting then runReduce $ rewrite (void es') v0 rewr (ignoreBlocking es') else NoReduction $ applyE v0 <$> es' where defaultResult = if hasRewriting then runReduce $ rewrite (NotBlocked AbsurdMatch ()) v0 rewr (map ignoreReduced es) else NoReduction $ NotBlocked AbsurdMatch vfull vfull = v0 `applyE` map ignoreReduced es match' :: Int -> QName -> FastStack -> Reduced (Blocked Elims) Term match' steps f ((c, es, patch) : stack) = let no blocking es = NoReduction $ blocking $ patch $ map ignoreReduced es yes t = yesReduction t in case c of -- impossible case FFail -> no (NotBlocked AbsurdMatch) es -- done matching FDone xs t -- common case: exact number of arguments | m == n -> {-# SCC match'Done #-} yes $ doSubst es t -- if the function was partially applied, return a lambda | m < n -> yes $ doSubst es $ foldr lam t (drop m xs) -- otherwise, just apply instantiation to body -- apply the result to any extra arguments | otherwise -> yes $ doSubst es0 t `applyE` map ignoreReduced es1 where n = length xs m = length es useStrictSubst = rem steps strictEveryNth == 0 doSubst es t = strictSubst useStrictSubst (reverse $ map (unArg . argFromElim . ignoreReduced) es) t (es0, es1) = splitAt n es lam x t = Lam (argInfo x) (Abs (unArg x) t) -- splitting on the @n@th elimination FCase n bs -> {-# SCC "match'Case" #-} case splitAt n es of -- if the @n@th elimination is not supplied, no match (_, []) -> no (NotBlocked Underapplied) es -- if the @n@th elimination is @e0@ (es0, MaybeRed red e0 : es1) -> -- get the reduced form of @e0@ let eb = case red of Reduced b -> e0 <$ b NotReduced -> unfoldCorecursionE e0 e = ignoreBlocking eb -- replace the @n@th argument by its reduced form es' = es0 ++ [MaybeRed (Reduced $ () <$ eb) e] ++ es1 -- if a catch-all clause exists, put it on the stack catchAllFrame stack = maybe stack (\c -> (c, es', patch) : stack) (fcatchAllBranch bs) -- If our argument is @Lit l@, we push @litFrame l@ onto the stack. litFrame l stack = case Map.lookup l (flitBranches bs) of Nothing -> stack Just cc -> (cc, es0 ++ es1, patchLit) : stack -- If our argument (or its constructor form) is @Con c ci vs@ -- we push @conFrame c ci vs@ onto the stack. conFrame c ci vs stack = case lookupCon (conName c) bs of Nothing -> stack Just cc -> ( cc , es0 ++ map (MaybeRed NotReduced . Apply) vs ++ es1 , patchCon c ci (length vs) ) : stack sucFrame n stack = case fsucBranch bs of Nothing -> stack Just cc -> (cc, es0 ++ [v] ++ es1, patchCon (fromJust suc) ConOSystem 1) : stack where v = MaybeRed (Reduced $ notBlocked ()) $ Apply $ defaultArg $ Lit $ LitNat noRange n -- If our argument is @Proj p@, we push @projFrame p@ onto the stack. projFrame p stack = case lookupCon p bs of Nothing -> stack Just cc -> (cc, es0 ++ es1, patchLit) : stack -- The new patch function restores the @n@th argument to @v@: -- In case we matched a literal, just put @v@ back. patchLit es = patch (es0 ++ [e] ++ es1) where (es0, es1) = splitAt n es -- In case we matched constructor @c@ with @m@ arguments, -- contract these @m@ arguments @vs@ to @Con c ci vs@. patchCon c ci m es = patch (es0 ++ [Con c ci vs <$ e] ++ es2) where (es0, rest) = splitAt n es (es1, es2) = splitAt m rest vs = map argFromElim es1 -- Now do the matching on the @n@ths argument: in case eb of Blocked x _ -> no (Blocked x) es' NotBlocked blk elim -> case elim of Apply (Arg info v) -> case v of MetaV x _ -> no (Blocked x) es' -- In case of a natural number literal, try also its constructor form Lit l@(LitNat r n) -> let cFrame stack | n > 0 = sucFrame (n - 1) stack | n == 0, Just z <- zero = conFrame z ConOSystem [] stack | otherwise = stack in match' steps f $ litFrame l $ cFrame $ catchAllFrame stack Lit l -> match' steps f $ litFrame l $ catchAllFrame stack Con c ci vs -> match' steps f $ conFrame c ci vs $ catchAllFrame $ stack -- Otherwise, we are stuck. If we were stuck before, -- we keep the old reason, otherwise we give reason StuckOn here. _ -> no (NotBlocked $ stuckOn elim blk) es' -- In case of a projection, push the projFrame Proj _ p -> match' steps f $ projFrame p stack -- If we reach the empty stack, then pattern matching was incomplete match' _ f [] = runReduce $ do pds <- getPartialDefs if f `elem` pds then return (NoReduction $ NotBlocked MissingClauses es) else do traceSLn "impossible" 10 ("Incomplete pattern matching when applying " ++ show f) __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/TypeChecking/Positivity/0000755000000000000000000000000013154613124017670 5ustar0000000000000000Agda-2.5.3/src/full/Agda/TypeChecking/Positivity/Occurrence.hs0000644000000000000000000001616313154613124022323 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} -- | Occurrences. module Agda.TypeChecking.Positivity.Occurrence ( Occurrence(..) , OccursWhere(..) , Where(..) , boundToEverySome , productOfEdgesInBoundedWalk ) where import Control.Applicative import Control.DeepSeq import Control.Monad import Data.Data (Data) import Data.Either import Data.Foldable (toList) import Data.Maybe import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map import Data.Sequence (Seq) import qualified Data.Sequence as Seq import Data.Typeable (Typeable) import Agda.Syntax.Common import Agda.Syntax.Abstract.Name import Agda.Syntax.Position import Agda.Utils.Graph.AdjacencyMap.Unidirectional (Graph) import qualified Agda.Utils.Graph.AdjacencyMap.Unidirectional as Graph import Agda.Utils.List import Agda.Utils.Null import Agda.Utils.Pretty import Agda.Utils.SemiRing #include "undefined.h" import Agda.Utils.Impossible -- Specification of occurrences ------------------------------------------- -- Operations and instances in Agda.TypeChecking.Positivity. -- | Description of an occurrence. data OccursWhere = Unknown -- ^ an unknown position (treated as negative) | Known Range (Seq Where) -- ^ The elements of the sequence, from left to right, explain how -- to get to the occurrence. deriving (Show, Eq, Ord, Typeable, Data) -- | One part of the description of an occurrence. data Where = LeftOfArrow | DefArg QName Nat -- ^ in the nth argument of a define constant | UnderInf -- ^ in the principal argument of built-in ∞ | VarArg -- ^ as an argument to a bound variable | MetaArg -- ^ as an argument of a metavariable | ConArgType QName -- ^ in the type of a constructor | IndArgType QName -- ^ in a datatype index of a constructor | InClause Nat -- ^ in the nth clause of a defined function | Matched -- ^ matched against in a clause of a defined function | InDefOf QName -- ^ in the definition of a constant deriving (Show, Eq, Ord, Typeable, Data) -- | Subterm occurrences for positivity checking. -- The constructors are listed in increasing information they provide: -- @Mixed <= JustPos <= StrictPos <= GuardPos <= Unused@ -- @Mixed <= JustNeg <= Unused@. data Occurrence = Mixed -- ^ Arbitrary occurrence (positive and negative). | JustNeg -- ^ Negative occurrence. | JustPos -- ^ Positive occurrence, but not strictly positive. | StrictPos -- ^ Strictly positive occurrence. | GuardPos -- ^ Guarded strictly positive occurrence (i.e., under ∞). For checking recursive records. | Unused -- ^ No occurrence. deriving (Typeable, Data, Show, Eq, Ord, Enum, Bounded) -- * Pretty instances instance Pretty Occurrence where pretty = text . \case Unused -> "_" Mixed -> "*" JustNeg -> "-" JustPos -> "+" StrictPos -> "++" GuardPos -> "g+" instance Pretty Where where pretty = \case LeftOfArrow -> text "LeftOfArrow" DefArg q i -> text "DefArg" <+> pretty q <+> pretty i UnderInf -> text "UnderInf" VarArg -> text "VarArg" MetaArg -> text "MetaArg" ConArgType q -> text "ConArgType" <+> pretty q IndArgType q -> text "IndArgType" <+> pretty q InClause i -> text "InClause" <+> pretty i Matched -> text "Matched" InDefOf q -> text "InDefOf" <+> pretty q instance Pretty OccursWhere where pretty = \case Unknown -> text "Unknown" Known _r ws -> text "Known _" <+> pretty (toList ws) -- * Instances for 'Occurrence' instance NFData Occurrence where rnf x = seq x () instance KillRange Occurrence where killRange = id -- | 'Occurrence' is a complete lattice with least element 'Mixed' -- and greatest element 'Unused'. -- -- It forms a commutative semiring where 'oplus' is meet (glb) -- and 'otimes' is composition. Both operations are idempotent. -- -- For 'oplus', 'Unused' is neutral (zero) and 'Mixed' is dominant. -- For 'otimes', 'StrictPos' is neutral (one) and 'Unused' is dominant. instance SemiRing Occurrence where ozero = Unused oone = StrictPos oplus Mixed _ = Mixed -- dominant oplus _ Mixed = Mixed oplus Unused o = o -- neutral oplus o Unused = o oplus JustNeg JustNeg = JustNeg oplus JustNeg o = Mixed -- negative and any form of positve oplus o JustNeg = Mixed oplus GuardPos o = o -- second-rank neutral oplus o GuardPos = o oplus StrictPos o = o -- third-rank neutral oplus o StrictPos = o oplus JustPos JustPos = JustPos otimes Unused _ = Unused -- dominant otimes _ Unused = Unused otimes Mixed _ = Mixed -- second-rank dominance otimes _ Mixed = Mixed otimes JustNeg JustNeg = JustPos otimes JustNeg _ = JustNeg -- third-rank dominance otimes _ JustNeg = JustNeg otimes JustPos _ = JustPos -- fourth-rank dominance otimes _ JustPos = JustPos otimes GuardPos _ = GuardPos -- _ `elem` [StrictPos, GuardPos] otimes _ GuardPos = GuardPos otimes StrictPos StrictPos = StrictPos -- neutral instance StarSemiRing Occurrence where ostar Mixed = Mixed ostar JustNeg = Mixed ostar JustPos = JustPos ostar StrictPos = StrictPos ostar GuardPos = StrictPos ostar Unused = StrictPos instance Null Occurrence where empty = Unused -- | The map contains bindings of the form @bound |-> ess@, satisfying -- the following property: for every non-empty list @w@, -- @'foldr1' 'otimes' w '<=' bound@ iff -- @'or' [ 'all' every w '&&' 'any' some w | (every, some) <- ess ]@. boundToEverySome :: Map Occurrence [(Occurrence -> Bool, Occurrence -> Bool)] boundToEverySome = Map.fromList [ ( JustPos , [((/= Unused), (`elem` [Mixed, JustNeg, JustPos]))] ) , ( StrictPos , [ ((/= Unused), (`elem` [Mixed, JustNeg, JustPos])) , ((not . (`elem` [Unused, GuardPos])), const True) ] ) , ( GuardPos , [((/= Unused), const True)] ) ] -- | @productOfEdgesInBoundedWalk occ g u v bound@ returns @'Just' e@ -- iff there is a walk @c@ (a list of edges) in @g@, from @u@ to @v@, -- for which the product @'foldr1' 'otimes' ('map' occ c) '<=' bound@. -- In this case the returned value @e@ is the product @'foldr1' -- 'otimes' c@ for one such walk. -- -- Preconditions: @u@ and @v@ must belong to @g@, and @bound@ must -- belong to the domain of @boundToEverySome@. -- There is a property for this function in -- Agda.Utils.Graph.AdjacencyMap.Unidirectional.Tests. productOfEdgesInBoundedWalk :: (SemiRing e, Ord n) => (e -> Occurrence) -> Graph n n e -> n -> n -> Occurrence -> Maybe e productOfEdgesInBoundedWalk occ g u v bound = case Map.lookup bound boundToEverySome of Nothing -> __IMPOSSIBLE__ Just ess -> case msum [ Graph.walkSatisfying (every . occ) (some . occ) g u v | (every, some) <- ess ] of Just es@(_ : _) -> Just (foldr1 otimes (map Graph.label es)) Just [] -> __IMPOSSIBLE__ Nothing -> Nothing Agda-2.5.3/src/full/Agda/Interaction/0000755000000000000000000000000013154613124015407 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Interaction/Imports.hs-boot0000644000000000000000000000045313154613124020343 0ustar0000000000000000 module Agda.Interaction.Imports where import Agda.Syntax.Abstract.Name ( ModuleName ) import Agda.Syntax.Scope.Base ( Scope ) import Agda.TypeChecking.Monad.Base ( TCM ) import Data.Map ( Map ) scopeCheckImport :: ModuleName -> TCM (ModuleName, Map ModuleName Scope) Agda-2.5.3/src/full/Agda/Interaction/CommandLine.hs0000644000000000000000000002426013154613124020135 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Interaction.CommandLine where import Control.Monad.Reader import Control.Applicative import qualified Data.List as List import Data.Maybe import Agda.Interaction.BasicOps as BasicOps hiding (parseExpr) import Agda.Interaction.Monad import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Common import Agda.Syntax.Internal (telToList) import qualified Agda.Syntax.Internal as I import Agda.Syntax.Parser import Agda.Syntax.Position import Agda.Syntax.Scope.Base import Agda.Syntax.Translation.ConcreteToAbstract import Agda.Syntax.Abstract.Pretty import Text.PrettyPrint import Agda.TheTypeChecker import Agda.TypeChecking.Constraints import Agda.TypeChecking.Monad import Agda.TypeChecking.Reduce import Agda.TypeChecking.Errors import Agda.TypeChecking.Pretty ( PrettyTCM(prettyTCM) ) import Agda.TypeChecking.Substitute import Agda.TypeChecking.Warnings (runPM) import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.Monad #include "undefined.h" import Agda.Utils.Impossible data ExitCode a = Continue | ContinueIn TCEnv | Return a type Command a = (String, [String] -> TCM (ExitCode a)) matchCommand :: String -> [Command a] -> Either [String] ([String] -> TCM (ExitCode a)) matchCommand x cmds = case List.filter (List.isPrefixOf x . fst) cmds of [(_,m)] -> Right m xs -> Left $ List.map fst xs interaction :: String -> [Command a] -> (String -> TCM (ExitCode a)) -> IM a interaction prompt cmds eval = loop where go (Return x) = return x go Continue = loop go (ContinueIn env) = local (const env) loop loop = do ms <- readline prompt case fmap words ms of Nothing -> return $ error "** EOF **" Just [] -> loop Just ((':':cmd):args) -> do case matchCommand cmd cmds of Right c -> go =<< liftTCM (c args) Left [] -> do liftIO $ putStrLn $ "Unknown command '" ++ cmd ++ "'" loop Left xs -> do liftIO $ putStrLn $ "More than one command match: " ++ concat (List.intersperse ", " xs) loop Just _ -> do go =<< liftTCM (eval $ fromJust ms) `catchError` \e -> do s <- liftTCM $ prettyError e liftIO $ putStrLn s loop -- | The interaction loop. interactionLoop :: TCM (Maybe Interface) -> IM () interactionLoop doTypeCheck = do liftTCM reload interaction "Main> " commands evalTerm where reload = do mi <- doTypeCheck -- Note that mi is Nothing if (1) there is no input file or -- (2) the file type checked with unsolved metas and -- --allow-unsolved-metas was used. In the latter case the -- behaviour of agda -I may be surprising. If agda -I ever -- becomes properly supported again, then this behaviour -- should perhaps be fixed. setScope $ case mi of Just i -> iInsideScope i Nothing -> emptyScopeInfo `catchError` \e -> do s <- prettyError e liftIO $ putStrLn s liftIO $ putStrLn "Failed." commands = [ "quit" |> \_ -> return $ Return () , "?" |> \_ -> continueAfter $ liftIO $ help commands , "reload" |> \_ -> do reload ContinueIn <$> ask , "constraints" |> \args -> continueAfter $ showConstraints args , "Context" |> \args -> continueAfter $ showContext args , "give" |> \args -> continueAfter $ giveMeta args , "Refine" |> \args -> continueAfter $ refineMeta args , "metas" |> \args -> continueAfter $ showMetas args , "load" |> \args -> continueAfter $ loadFile reload args , "eval" |> \args -> continueAfter $ evalIn args , "typeOf" |> \args -> continueAfter $ typeOf args , "typeIn" |> \args -> continueAfter $ typeIn args , "wakeup" |> \_ -> continueAfter $ retryConstraints , "scope" |> \_ -> continueAfter $ showScope ] where (|>) = (,) continueAfter :: TCM a -> TCM (ExitCode b) continueAfter m = withCurrentFile $ do m >> return Continue -- | Set 'envCurrentPath' to 'optInputFile'. withCurrentFile :: TCM a -> TCM a withCurrentFile cont = do mpath <- getInputFile' local (\ e -> e { envCurrentPath = mpath }) cont loadFile :: TCM () -> [String] -> TCM () loadFile reload [file] = do setInputFile file withCurrentFile reload loadFile _ _ = liftIO $ putStrLn ":load file" showConstraints :: [String] -> TCM () showConstraints [] = do cs <- BasicOps.getConstraints liftIO $ putStrLn $ unlines (List.map show cs) showConstraints _ = liftIO $ putStrLn ":constraints [cid]" showMetas :: [String] -> TCM () showMetas [m] = do i <- InteractionId <$> readM m withInteractionId i $ do s <- typeOfMeta AsIs i r <- getInteractionRange i d <- showA s liftIO $ putStrLn $ d ++ " " ++ show r showMetas [m,"normal"] = do i <- InteractionId <$> readM m withInteractionId i $ do s <- showA =<< typeOfMeta Normalised i r <- getInteractionRange i liftIO $ putStrLn $ s ++ " " ++ show r showMetas [] = do interactionMetas <- typesOfVisibleMetas AsIs hiddenMetas <- typesOfHiddenMetas AsIs mapM_ (liftIO . putStrLn) =<< mapM showII interactionMetas mapM_ print' hiddenMetas where showII o = withInteractionId (outputFormId $ OutputForm noRange [] o) $ showA o showM o = withMetaId (nmid $ outputFormId $ OutputForm noRange [] o) $ showA o metaId (OfType i _) = i metaId (JustType i) = i metaId (JustSort i) = i metaId (Assign i e) = i metaId _ = __IMPOSSIBLE__ print' x = do r <- getMetaRange $ nmid $ metaId x d <- showM x liftIO $ putStrLn $ d ++ " [ at " ++ show r ++ " ]" showMetas _ = liftIO $ putStrLn $ ":meta [metaid]" showScope :: TCM () showScope = do scope <- getScope liftIO $ print scope metaParseExpr :: InteractionId -> String -> TCM A.Expr metaParseExpr ii s = do m <- lookupInteractionId ii scope <- getMetaScope <$> lookupMeta m r <- getRange <$> lookupMeta m --liftIO $ putStrLn $ show scope let pos = case rStart r of Nothing -> __IMPOSSIBLE__ Just pos -> pos e <- runPM $ parsePosString exprParser pos s concreteToAbstract scope e actOnMeta :: [String] -> (InteractionId -> A.Expr -> TCM a) -> TCM a actOnMeta (is:es) f = do i <- readM is let ii = InteractionId i e <- metaParseExpr ii (unwords es) withInteractionId ii $ f ii e actOnMeta _ _ = __IMPOSSIBLE__ giveMeta :: [String] -> TCM () giveMeta s | length s >= 2 = do _ <- actOnMeta s $ \ ii e -> give WithoutForce ii Nothing e return () giveMeta _ = liftIO $ putStrLn $ ": give" ++ " metaid expr" refineMeta :: [String] -> TCM () refineMeta s | length s >= 2 = do _ <- actOnMeta s $ \ ii e -> refine WithoutForce ii Nothing e return () refineMeta _ = liftIO $ putStrLn $ ": refine" ++ " metaid expr" retryConstraints :: TCM () retryConstraints = liftTCM wakeupConstraints_ evalIn :: [String] -> TCM () evalIn s | length s >= 2 = do d <- actOnMeta s $ \_ e -> prettyA =<< evalInCurrent e liftIO $ print d evalIn _ = liftIO $ putStrLn ":eval metaid expr" parseExpr :: String -> TCM A.Expr parseExpr s = do e <- runPM $ parse exprParser s localToAbstract e return evalTerm :: String -> TCM (ExitCode a) evalTerm s = do e <- parseExpr s v <- evalInCurrent e e <- prettyTCM v liftIO $ putStrLn $ show e return Continue where evalInCurrent e = do (v,t) <- inferExpr e v' <- normalise v return v' typeOf :: [String] -> TCM () typeOf s = do e <- parseExpr (unwords s) e0 <- typeInCurrent Normalised e e1 <- typeInCurrent AsIs e liftIO . putStrLn =<< showA e1 typeIn :: [String] -> TCM () typeIn s@(_:_:_) = actOnMeta s $ \i e -> do e1 <- typeInMeta i Normalised e e2 <- typeInMeta i AsIs e liftIO . putStrLn =<< showA e1 typeIn _ = liftIO $ putStrLn ":typeIn meta expr" showContext :: [String] -> TCM () showContext (meta:args) = do i <- InteractionId <$> readM meta mi <- lookupMeta =<< lookupInteractionId i withMetaInfo (getMetaInfo mi) $ do ctx <- List.map unDom . telToList <$> getContextTelescope zipWithM_ display ctx $ reverse $ zipWith const [1..] ctx where display (x, t) n = do t <- case args of ["normal"] -> normalise $ raise n t _ -> return $ raise n t d <- prettyTCM t liftIO $ print $ text (I.argNameToString x) <+> text ":" <+> d showContext _ = liftIO $ putStrLn ":Context meta" -- | The logo that prints when Agda is started in interactive mode. splashScreen :: String splashScreen = unlines [ " _ " , " ____ | |" , " / __ \\ | |" , " | |__| |___ __| | ___" , " | __ / _ \\/ _ |/ __\\ Agda Interactive" , " | | |/ /_\\ \\/_| / /_| \\" , " |_| |\\___ /____\\_____/ Type :? for help." , " __/ /" , " \\__/" , "" -- , "The interactive mode is no longer supported. Don't complain if it doesn't work." , "The interactive mode is no longer under active development. Use at your own risk." ] -- | The help message help :: [Command a] -> IO () help cs = putStr $ unlines $ [ "Command overview" ] ++ List.map explain cs ++ [ " Infer type of expression and evaluate it." ] where explain (x,_) = ":" ++ x Agda-2.5.3/src/full/Agda/Interaction/InteractionTop.hs-boot0000644000000000000000000000016713154613124021652 0ustar0000000000000000module Agda.Interaction.InteractionTop where import Agda.TypeChecking.Monad.Base (TCM) showOpenMetas :: TCM [String] Agda-2.5.3/src/full/Agda/Interaction/InteractionTop.hs0000644000000000000000000015431413154613124020715 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# OPTIONS_GHC -fno-cse #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.Interaction.InteractionTop ( module Agda.Interaction.InteractionTop ) where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Concurrent import Control.Concurrent.Async import Control.Concurrent.STM.TChan import qualified Control.Exception as E import Control.Monad.Identity import Control.Monad.Reader import Control.Monad.State import Control.Monad.STM import qualified Data.Char as Char import Data.Foldable (Foldable) import Data.Function import qualified Data.List as List import Data.Maybe import Data.Map (Map) import qualified Data.Map as Map import Data.Monoid import Data.Traversable (Traversable) import qualified Data.Traversable as Trav import System.Directory import System.FilePath import Agda.TypeChecking.Monad as TM hiding (initState, setCommandLineOptions) import qualified Agda.TypeChecking.Monad as TM import qualified Agda.TypeChecking.Pretty as TCP import Agda.TypeChecking.Rules.Term (checkExpr, isType_) import Agda.TypeChecking.Errors import Agda.Syntax.Fixity import Agda.Syntax.Position import Agda.Syntax.Parser import Agda.Syntax.Common import Agda.Syntax.Literal import Agda.Syntax.Concrete as C import Agda.Syntax.Concrete.Generic as C import Agda.Syntax.Concrete.Pretty () import Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Pretty import Agda.Syntax.Info (mkDefInfo) import Agda.Syntax.Translation.ConcreteToAbstract import Agda.Syntax.Translation.AbstractToConcrete hiding (withScope) import Agda.Syntax.Scope.Base import Agda.Interaction.FindFile import Agda.Interaction.Options import Agda.Interaction.Options.Lenses as Lenses import Agda.Interaction.MakeCase import Agda.Interaction.SearchAbout import Agda.Interaction.Response hiding (Function, ExtendedLambda) import qualified Agda.Interaction.Response as R import qualified Agda.Interaction.BasicOps as B import Agda.Interaction.BasicOps hiding (whyInScope) import Agda.Interaction.Highlighting.Precise hiding (Postulate) import qualified Agda.Interaction.Imports as Imp import Agda.TypeChecking.Warnings import Agda.Interaction.Highlighting.Generate import qualified Agda.Interaction.Highlighting.LaTeX as LaTeX import qualified Agda.Interaction.Highlighting.Range as H import Agda.Compiler.Common (IsMain (..)) import Agda.Compiler.Backend import Agda.Auto.Auto as Auto import Agda.Utils.Except ( ExceptT , mkExceptT , MonadError(catchError, throwError) , runExceptT ) import Agda.Utils.FileName import Agda.Utils.Function import Agda.Utils.Hash import qualified Agda.Utils.HashMap as HMap import Agda.Utils.Lens import Agda.Utils.Maybe import qualified Agda.Utils.Maybe.Strict as Strict import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Pretty import Agda.Utils.String import Agda.Utils.Time #include "undefined.h" import Agda.Utils.Impossible ------------------------------------------------------------------------ -- The CommandM monad -- | Auxiliary state of an interactive computation. data CommandState = CommandState { theInteractionPoints :: [InteractionId] -- ^ The interaction points of the buffer, in the order in which -- they appear in the buffer. The interaction points are -- recorded in 'theTCState', but when new interaction points are -- added by give or refine Agda does not ensure that the ranges -- of later interaction points are updated. , theCurrentFile :: Maybe (AbsolutePath, ClockTime) -- ^ The file which the state applies to. Only stored if the -- module was successfully type checked (potentially with -- warnings). The 'ClockTime' is the modification time stamp of -- the file when it was last loaded. , optionsOnReload :: CommandLineOptions -- ^ Reset the options on each reload to these. , oldInteractionScopes :: !OldInteractionScopes -- ^ We remember (the scope of) old interaction points to make it -- possible to parse and compute highlighting information for the -- expression that it got replaced by. , commandQueue :: CommandQueue -- ^ Command queue. -- -- The commands in the queue are processed in the order in which -- they are received. Abort commands do not have precedence over -- other commands, they only abort the immediately preceding -- command. (The Emacs mode is expected not to send a new command, -- other than the abort command, before the previous command has -- completed.) } type OldInteractionScopes = Map InteractionId ScopeInfo -- | Initial auxiliary interaction state initCommandState :: CommandQueue -> CommandState initCommandState commandQueue = CommandState { theInteractionPoints = [] , theCurrentFile = Nothing , optionsOnReload = defaultOptions , oldInteractionScopes = Map.empty , commandQueue = commandQueue } -- | Monad for computing answers to interactive commands. -- -- 'CommandM' is 'TCM' extended with state 'CommandState'. type CommandM = StateT CommandState TCM -- | Restore both 'TCState' and 'CommandState'. localStateCommandM :: CommandM a -> CommandM a localStateCommandM m = do cSt <- get tcSt <- lift $ get x <- m lift $ put tcSt put cSt return x -- | Restore 'TCState', do not touch 'CommandState'. liftLocalState :: TCM a -> CommandM a liftLocalState = lift . localState -- | Build an opposite action to 'lift' for state monads. revLift :: MonadState st m => (forall c . m c -> st -> k (c, st)) -- ^ run -> (forall b . k b -> m b) -- ^ lift -> (forall x . (m a -> k x) -> k x) -> m a -- ^ reverse lift in double negative position revLift run lift f = do st <- get (a, st) <- lift $ f (`run` st) put st return a -- | Opposite of 'liftIO' for 'CommandM'. -- Use only if main errors are already catched. commandMToIO :: (forall x . (CommandM a -> IO x) -> IO x) -> CommandM a commandMToIO ci_i = revLift runStateT lift $ \ct -> revLift runSafeTCM liftIO $ ci_i . (. ct) -- | Lift a TCM action transformer to a CommandM action transformer. liftCommandMT :: (forall a . TCM a -> TCM a) -> CommandM a -> CommandM a liftCommandMT f m = revLift runStateT lift $ f . ($ m) -- | Ditto, but restore state. liftCommandMTLocalState :: (forall a . TCM a -> TCM a) -> CommandM a -> CommandM a liftCommandMTLocalState f = liftCommandMT f . localStateCommandM -- | Put a response by the callback function given by 'stInteractionOutputCallback'. putResponse :: Response -> CommandM () putResponse = lift . appInteractionOutputCallback -- | A Lens for 'theInteractionPoints'. modifyTheInteractionPoints :: ([InteractionId] -> [InteractionId]) -> CommandM () modifyTheInteractionPoints f = modify $ \ s -> s { theInteractionPoints = f (theInteractionPoints s) } -- * Operations for manipulating 'oldInteractionScopes'. -- | A Lens for 'oldInteractionScopes'. modifyOldInteractionScopes :: (OldInteractionScopes -> OldInteractionScopes) -> CommandM () modifyOldInteractionScopes f = modify $ \ s -> s { oldInteractionScopes = f $ oldInteractionScopes s } insertOldInteractionScope :: InteractionId -> ScopeInfo -> CommandM () insertOldInteractionScope ii scope = do lift $ reportSLn "interaction.scope" 20 $ "inserting old interaction scope " ++ show ii modifyOldInteractionScopes $ Map.insert ii scope removeOldInteractionScope :: InteractionId -> CommandM () removeOldInteractionScope ii = do lift $ reportSLn "interaction.scope" 20 $ "removing old interaction scope " ++ show ii modifyOldInteractionScopes $ Map.delete ii getOldInteractionScope :: InteractionId -> CommandM ScopeInfo getOldInteractionScope ii = do ms <- gets $ Map.lookup ii . oldInteractionScopes case ms of Nothing -> fail $ "not an old interaction point: " ++ show ii Just scope -> return scope -- | Do setup and error handling for a command. handleCommand_ :: CommandM () -> CommandM () handleCommand_ = handleCommand id (return ()) handleCommand :: (forall a. CommandM a -> CommandM a) -> CommandM () -> CommandM () -> CommandM () handleCommand wrap onFail cmd = handleNastyErrors $ wrap $ do oldState <- lift get -- -- Andreas, 2016-11-18 OLD CODE: -- -- onFail and handleErr are executed in "new" command state (not TCState). -- -- But it seems that if an exception is raised, it is identical to the old state, -- -- see code for catchErr. -- res <- (`catchErr` (return . Just)) $ Nothing <$ cmd -- maybe (return ()) (\ e -> onFail >> handleErr e) res -- Andreas, 2016-11-18 NEW CODE: execute onFail and handleErr in handler -- which means (looking at catchErr) they run in state s rathern than s'. -- Yet, it looks like s == s' in case the command failed. cmd `catchErr` \ e -> do onFail handleErr e -- Andreas, 2016-11-18, issue #2174 -- Reset TCState after error is handled, to get rid of metas created during failed command lift $ do newPersistentState <- use lensPersistentState put oldState lensPersistentState .= newPersistentState where -- Preserves state so we can do unsolved meta highlighting catchErr :: CommandM a -> (TCErr -> CommandM a) -> CommandM a catchErr m h = do s <- get (x, s') <- lift $ do disableDestructiveUpdate (runStateT m s) `catchError_` \ e -> runStateT (h e) s put s' return x -- | Handle every possible kind of error (#637), except for -- ThreadKilled, which is used to abort Agda. handleNastyErrors :: CommandM () -> CommandM () handleNastyErrors m = commandMToIO $ \ toIO -> do let handle e = Right <$> (toIO $ handleErr $ Exception noRange $ text $ show e) asyncHandler e@E.ThreadKilled = return (Left e) asyncHandler e = handle e generalHandler (e :: E.SomeException) = handle e r <- ((Right <$> toIO m) `E.catch` asyncHandler) `E.catch` generalHandler case r of Right x -> return x Left e -> E.throwIO e -- | Displays an error and instructs Emacs to jump to the site of the -- error. Because this function may switch the focus to another file -- the status information is also updated. handleErr e = do unsolvedNotOK <- lift $ not . optAllowUnsolved <$> pragmaOptions meta <- lift $ computeUnsolvedMetaWarnings constr <- lift $ computeUnsolvedConstraints err <- lift $ errorHighlighting e modFile <- lift $ use stModuleToSource method <- lift $ view eHighlightingMethod let info = compress $ mconcat $ -- Errors take precedence over unsolved things. err : if unsolvedNotOK then [meta, constr] else [] s1 <- lift $ prettyError e s2 <- lift $ prettyTCWarnings' =<< Imp.errorWarningsOfTCErr e let s = List.intercalate "\n" $ filter (not . null) $ s1 : s2 x <- lift $ optShowImplicit <$> use stPragmaOptions unless (null s1) $ mapM_ putResponse $ [ Resp_DisplayInfo $ Info_Error s ] ++ tellEmacsToJumpToError (getRange e) ++ [ Resp_HighlightingInfo info method modFile ] ++ [ Resp_Status $ Status { sChecked = False , sShowImplicitArguments = x } ] -- | Run an 'IOTCM' value, catch the exceptions, emit output -- -- If an error happens the state of 'CommandM' does not change, -- but stPersistent may change (which contains successfully -- loaded interfaces for example). runInteraction :: IOTCM -> CommandM () runInteraction (IOTCM current highlighting highlightingMethod cmd) = handleCommand inEmacs onFail $ do current <- liftIO $ absolute current -- Raises an error if the given file is not the one currently -- loaded. cf <- gets theCurrentFile when (not (independent cmd) && Just current /= (fst <$> cf)) $ lift $ typeError $ GenericError "Error: First load the file." withCurrentFile $ interpret cmd cf <- gets theCurrentFile when (Just current == (fst <$> cf)) $ putResponse . Resp_InteractionPoints =<< gets theInteractionPoints where inEmacs = liftCommandMT $ withEnv $ initEnv { envHighlightingLevel = highlighting , envHighlightingMethod = highlightingMethod } -- If an independent command fails we should reset theCurrentFile (Issue853). onFail | independent cmd = modify $ \ s -> s { theCurrentFile = Nothing } | otherwise = return () ------------------------------------------------------------------------ -- Command queues -- | Commands. data Command = Command IOTCM -- ^ An 'IOTCM' command. | Done -- ^ Stop processing commands. | Error String -- ^ An error message for a command that could not be parsed. deriving Show -- | Command queues. type CommandQueue = TChan Command -- | The next command. nextCommand :: CommandM Command nextCommand = liftIO . atomically . readTChan =<< gets commandQueue -- | Runs the given computation, but if an abort command is -- encountered (and acted upon), then the computation is interrupted, -- the persistent state and all options are restored, and some -- commands are sent to the frontend. -- TODO: It might be nice if some of the changes to the persistent -- state inflicted by the interrupted computation were preserved. maybeAbort :: CommandM () -> CommandM () maybeAbort c = do commandState <- get tcState <- lift get tcEnv <- lift ask result <- liftIO $ race (runTCM tcEnv tcState $ runStateT c commandState) (waitForAbort $ commandQueue commandState) case result of Left (((), commandState), tcState) -> do lift $ put tcState put commandState Right () -> do lift $ put $ initState { stPersistentState = stPersistentState tcState , stPreScopeState = (stPreScopeState initState) { stPrePragmaOptions = stPrePragmaOptions (stPreScopeState tcState) } } put $ (initCommandState (commandQueue commandState)) { optionsOnReload = optionsOnReload commandState } putResponse Resp_DoneAborting displayStatus where -- | Returns if the first element in the queue is an abort command. -- The abort command is removed from the queue. waitForAbort :: CommandQueue -> IO () waitForAbort q = atomically $ do c <- peekTChan q case c of Command (IOTCM _ _ _ Cmd_abort) -> void $ readTChan q _ -> retry -- | Creates a command queue, and forks a thread that writes commands -- to the queue. The queue is returned. initialiseCommandQueue :: IO Command -- ^ Returns the next command. -> IO CommandQueue initialiseCommandQueue next = do q <- newTChanIO let readCommands = do c <- next atomically $ writeTChan q c case c of Done -> return () _ -> readCommands _ <- forkIO readCommands return q ---------------------------------------------------------------------------- -- | An interactive computation. type Interaction = Interaction' Range data Interaction' range -- | @cmd_load m argv@ loads the module in file @m@, using -- @argv@ as the command-line options. = Cmd_load FilePath [String] -- | @cmd_compile b m argv@ compiles the module in file @m@ using -- the backend @b@, using @argv@ as the command-line options. | Cmd_compile CompilerBackend FilePath [String] | Cmd_constraints -- | Show unsolved metas. If there are no unsolved metas but unsolved constraints -- show those instead. | Cmd_metas -- | Display all warnings. | Cmd_warnings -- | Shows all the top-level names in the given module, along with -- their types. Uses the top-level scope. | Cmd_show_module_contents_toplevel B.Rewrite String -- | Shows all the top-level names in scope which mention all the given -- identifiers in their type. | Cmd_search_about_toplevel B.Rewrite String -- | Solve (all goals / the goal at point) whose values are determined by -- the constraints. | Cmd_solveAll B.Rewrite | Cmd_solveOne B.Rewrite InteractionId range String -- | Parse the given expression (as if it were defined at the -- top-level of the current module) and infer its type. | Cmd_infer_toplevel B.Rewrite -- Normalise the type? String -- | Parse and type check the given expression (as if it were defined -- at the top-level of the current module) and normalise it. | Cmd_compute_toplevel B.ComputeMode String ------------------------------------------------------------------------ -- Syntax highlighting -- | @cmd_load_highlighting_info source@ loads syntax highlighting -- information for the module in @source@, and asks Emacs to apply -- highlighting info from this file. -- -- If the module does not exist, or its module name is malformed or -- cannot be determined, or the module has not already been visited, -- or the cached info is out of date, then no highlighting information -- is printed. -- -- This command is used to load syntax highlighting information when a -- new file is opened, and it would probably be annoying if jumping to -- the definition of an identifier reset the proof state, so this -- command tries not to do that. One result of this is that the -- command uses the current include directories, whatever they happen -- to be. | Cmd_load_highlighting_info FilePath -- | Tells Agda to compute highlighting information for the expression just -- spliced into an interaction point. | Cmd_highlight InteractionId range String ------------------------------------------------------------------------ -- Implicit arguments -- | Tells Agda whether or not to show implicit arguments. | ShowImplicitArgs Bool -- Show them? -- | Toggle display of implicit arguments. | ToggleImplicitArgs ------------------------------------------------------------------------ -- | Goal commands -- -- If the range is 'noRange', then the string comes from the -- minibuffer rather than the goal. | Cmd_give UseForce InteractionId range String | Cmd_refine InteractionId range String | Cmd_intro Bool InteractionId range String | Cmd_refine_or_intro Bool InteractionId range String | Cmd_auto InteractionId range String | Cmd_context B.Rewrite InteractionId range String | Cmd_helper_function B.Rewrite InteractionId range String | Cmd_infer B.Rewrite InteractionId range String | Cmd_goal_type B.Rewrite InteractionId range String -- | Displays the current goal and context. | Cmd_goal_type_context B.Rewrite InteractionId range String -- | Displays the current goal and context /and/ infers the type of an -- expression. | Cmd_goal_type_context_infer B.Rewrite InteractionId range String -- | Grabs the current goal's type and checks the expression in the hole -- against it. | Cmd_goal_type_context_check B.Rewrite InteractionId range String -- | Shows all the top-level names in the given module, along with -- their types. Uses the scope of the given goal. | Cmd_show_module_contents B.Rewrite InteractionId range String | Cmd_make_case InteractionId range String | Cmd_compute B.ComputeMode InteractionId range String | Cmd_why_in_scope InteractionId range String | Cmd_why_in_scope_toplevel String -- | Displays version of the running Agda | Cmd_show_version | Cmd_abort -- ^ Abort the current computation. -- -- Does nothing if no computation is in progress. deriving (Show, Read, Functor, Foldable, Traversable) type IOTCM = IOTCM' Range data IOTCM' range = IOTCM FilePath -- -^ The current file. If this file does not match -- 'theCurrentFile, and the 'Interaction' is not -- \"independent\", then an error is raised. HighlightingLevel HighlightingMethod (Interaction' range) -- -^ What to do deriving (Show, Read, Functor, Foldable, Traversable) --------------------------------------------------------- -- Read instances -- | The 'Parse' monad. -- 'StateT' state holds the remaining input. type Parse a = ExceptT String (StateT String Identity) a -- | Converter from the type of 'reads' to 'Parse' -- The first paramter is part of the error message -- in case the parse fails. readsToParse :: String -> (String -> Maybe (a, String)) -> Parse a readsToParse s f = do st <- lift get case f st of Nothing -> throwError s Just (a, st) -> do lift $ put st return a parseToReadsPrec :: Parse a -> Int -> String -> [(a, String)] parseToReadsPrec p i s = case runIdentity . flip runStateT s . runExceptT $ parens' p of (Right a, s) -> [(a,s)] _ -> [] -- | Demand an exact string. exact :: String -> Parse () exact s = readsToParse (show s) $ fmap (\x -> ((),x)) . List.stripPrefix s . dropWhile (==' ') readParse :: Read a => Parse a readParse = readsToParse "read failed" $ listToMaybe . reads parens' :: Parse a -> Parse a parens' p = do exact "(" x <- p exact ")" return x `mplus` p instance Read InteractionId where readsPrec = parseToReadsPrec $ fmap InteractionId readParse -- | Note that the grammar implemented by this instance does not -- necessarily match the current representation of ranges. instance Read a => Read (Range' a) where readsPrec = parseToReadsPrec $ (exact "intervalsToRange" >> liftM2 intervalsToRange readParse readParse) `mplus` (exact "noRange" >> return noRange) instance Read a => Read (Interval' a) where readsPrec = parseToReadsPrec $ do exact "Interval" liftM2 Interval readParse readParse instance Read AbsolutePath where readsPrec = parseToReadsPrec $ do exact "mkAbsolute" fmap mkAbsolute readParse instance Read a => Read (Position' a) where readsPrec = parseToReadsPrec $ do exact "Pn" liftM4 Pn readParse readParse readParse readParse --------------------------------------------------------- -- | Can the command run even if the relevant file has not been loaded -- into the state? independent :: Interaction -> Bool independent (Cmd_load {}) = True independent (Cmd_compile {}) = True independent (Cmd_load_highlighting_info {}) = True independent Cmd_show_version = True independent _ = False -- | Interpret an interaction interpret :: Interaction -> CommandM () interpret (Cmd_load m argv) = cmd_load' m argv True Imp.TypeCheck $ \_ -> interpret Cmd_metas interpret (Cmd_compile b file argv) = cmd_load' file argv (b `elem` [LaTeX, QuickLaTeX]) (if b == QuickLaTeX then Imp.ScopeCheck else Imp.TypeCheck) $ \(i, mw) -> do mw <- lift $ Imp.applyFlagsToMaybeWarnings RespectFlags mw case mw of Imp.NoWarnings -> do lift $ case b of LaTeX -> LaTeX.generateLaTeX i QuickLaTeX -> LaTeX.generateLaTeX i OtherBackend "GHCNoMain" -> callBackend "GHC" NotMain i -- for backwards compatibility OtherBackend b -> callBackend b IsMain i (pwe, pwa) <- interpretWarnings display_info $ Info_CompilationOk pwa pwe Imp.SomeWarnings w -> do pw <- lift $ prettyTCWarnings w display_info $ Info_Error $ unlines [ "You need to fix the following errors before you can compile" , "the module:" , "" , pw ] interpret Cmd_constraints = display_info . Info_Constraints . unlines . map show =<< lift B.getConstraints interpret Cmd_metas = do -- CL.showMetas [] unsolvedNotOK <- lift $ not . optAllowUnsolved <$> pragmaOptions ms <- lift showOpenMetas (pwe, pwa) <- interpretWarnings display_info $ Info_AllGoalsWarnings (unlines ms) pwa pwe interpret Cmd_warnings = do -- Ulf, 2016-08-09: Warnings are now printed in the info buffer by Cmd_metas. -- pws <- interpretWarnings -- unless (null pwd) $ display_info $ Info_Warning pws return () interpret (Cmd_show_module_contents_toplevel norm s) = liftCommandMT B.atTopLevel $ showModuleContents norm noRange s interpret (Cmd_search_about_toplevel norm s) = liftCommandMT B.atTopLevel $ searchAbout norm noRange s interpret (Cmd_solveAll norm) = solveInstantiatedGoals norm Nothing interpret (Cmd_solveOne norm ii _ _) = solveInstantiatedGoals norm' (Just ii) -- `solveOne` is called via `agda2-maybe-normalised` which does not use -- AsIs < Simplified < Normalised but rather Simplified < Instantiated < Normalised -- So we remap the Rewrite modifiers to match solveAll's behaviour. -- NB: instantiate is called in getSolvedInteractionPoints no matter what. where norm' = case norm of Simplified -> AsIs Instantiated -> Simplified _ -> norm interpret (Cmd_infer_toplevel norm s) = parseAndDoAtToplevel (B.typeInCurrent norm) Info_InferredType s interpret (Cmd_compute_toplevel cmode s) = parseAndDoAtToplevel' action Info_NormalForm $ computeWrapInput cmode s where action = allowNonTerminatingReductions . (if computeIgnoreAbstract cmode then ignoreAbstractMode else inConcreteMode) . (B.showComputed cmode <=< B.evalInCurrent) interpret (ShowImplicitArgs showImpl) = do opts <- lift commandLineOptions setCommandLineOpts $ opts { optPragmaOptions = (optPragmaOptions opts) { optShowImplicit = showImpl } } interpret ToggleImplicitArgs = do opts <- lift commandLineOptions let ps = optPragmaOptions opts setCommandLineOpts $ opts { optPragmaOptions = ps { optShowImplicit = not $ optShowImplicit ps } } interpret (Cmd_load_highlighting_info source) = do -- Make sure that the include directories have -- been set. setCommandLineOpts =<< lift commandLineOptions resp <- lift $ liftIO . tellToUpdateHighlighting =<< do ex <- liftIO $ doesFileExist source absSource <- liftIO $ absolute source case ex of False -> return Nothing True -> do mmi <- (getVisitedModule =<< moduleName absSource) `catchError` \_ -> return Nothing case mmi of Nothing -> return Nothing Just mi -> do sourceH <- liftIO $ hashFile absSource if sourceH == iSourceHash (miInterface mi) then do modFile <- use stModuleToSource method <- view eHighlightingMethod return $ Just (iHighlighting $ miInterface mi, method, modFile) else return Nothing mapM_ putResponse resp interpret (Cmd_highlight ii rng s) = do scope <- getOldInteractionScope ii removeOldInteractionScope ii handle $ do e <- try ("Highlighting failed to parse expression in " ++ show ii) $ B.parseExpr rng s e <- try ("Highlighting failed to scope check expression in " ++ show ii) $ concreteToAbstract scope e lift $ printHighlightingInfo =<< generateTokenInfoFromString rng s lift $ highlightExpr e where handle :: ExceptT String TCM () -> CommandM () handle m = do res <- lift $ runExceptT m case res of Left s -> display_info $ Info_Error s Right _ -> return () try :: String -> TCM a -> ExceptT String TCM a try err m = mkExceptT $ do (Right <$> m) `catchError` \ _ -> return (Left err) interpret (Cmd_give force ii rng s) = give_gen force ii rng s Give interpret (Cmd_refine ii rng s) = give_gen WithoutForce ii rng s Refine interpret (Cmd_intro pmLambda ii rng _) = do ss <- lift $ B.introTactic pmLambda ii liftCommandMT (B.withInteractionId ii) $ case ss of [] -> do display_info $ Info_Intro $ text "No introduction forms found." [s] -> give_gen WithoutForce ii rng s Intro _:_:_ -> do display_info $ Info_Intro $ sep [ text "Don't know which constructor to introduce of" , let mkOr [] = [] mkOr [x, y] = [text x <+> text "or" <+> text y] mkOr (x:xs) = text x : mkOr xs in nest 2 $ fsep $ punctuate comma (mkOr ss) ] interpret (Cmd_refine_or_intro pmLambda ii r s) = interpret $ let s' = trim s in (if null s' then Cmd_intro pmLambda else Cmd_refine) ii r s' interpret (Cmd_auto ii rng s) = do -- Andreas, 2014-07-05 Issue 1226: -- Save the state to have access to even those interaction ids -- that Auto solves (since Auto gives the solution right away). st <- lift $ get (time , res) <- maybeTimed $ lift $ Auto.auto ii rng s case autoProgress res of Solutions sols -> do lift $ reportSLn "auto" 10 $ "Auto produced the following solutions " ++ show sols forM_ sols $ \(ii, s) -> do -- Andreas, 2014-07-05 Issue 1226: -- For highlighting, Resp_GiveAction needs to access -- the @oldInteractionScope@s of the interaction points solved by Auto. -- We dig them out from the state before Auto was invoked. insertOldInteractionScope ii =<< liftLocalState (put st >> getInteractionScope ii) -- Andreas, 2014-07-07: NOT TRUE: -- -- Andreas, 2014-07-05: The following should be obsolete, -- -- as Auto has removed the interaction points already: -- modifyTheInteractionPoints $ filter (/= ii) putResponse $ Resp_GiveAction ii $ Give_String s -- Andreas, 2014-07-07: Remove the interaction points in one go. modifyTheInteractionPoints (List.\\ (map fst sols)) case autoMessage res of Nothing -> interpret Cmd_metas Just msg -> display_info $ Info_Auto msg FunClauses cs -> do case autoMessage res of Nothing -> return () Just msg -> display_info $ Info_Auto msg putResponse $ Resp_MakeCase R.Function cs Refinement s -> give_gen WithoutForce ii rng s Refine maybe (return ()) (display_info . Info_Time) time interpret (Cmd_context norm ii _ _) = display_info . Info_Context =<< liftLocalState (prettyContext norm False ii) interpret (Cmd_helper_function norm ii rng s) = display_info . Info_HelperFunction =<< liftLocalState (cmd_helper_function norm ii rng s) interpret (Cmd_infer norm ii rng s) = display_info . Info_InferredType =<< liftLocalState (B.withInteractionId ii (prettyATop =<< B.typeInMeta ii norm =<< B.parseExprIn ii rng s)) interpret (Cmd_goal_type norm ii _ _) = display_info . Info_CurrentGoal =<< liftLocalState (B.withInteractionId ii $ prettyTypeOfMeta norm ii) interpret (Cmd_goal_type_context norm ii rng s) = cmd_goal_type_context_and empty norm ii rng s interpret (Cmd_goal_type_context_infer norm ii rng s) = do -- In case of the empty expression to type, don't fail with -- a stupid parse error, but just fall back to -- Cmd_goal_type_context. have <- if all Char.isSpace s then return empty else liftLocalState $ do typ <- B.withInteractionId ii $ prettyATop =<< B.typeInMeta ii norm =<< B.parseExprIn ii rng s return $ text "Have:" <+> typ cmd_goal_type_context_and have norm ii rng s interpret (Cmd_goal_type_context_check norm ii rng s) = do have <- liftLocalState $ B.withInteractionId ii $ do expr <- B.parseExprIn ii rng s goal <- B.typeOfMeta AsIs ii term <- case goal of OfType _ ty -> checkExpr expr =<< isType_ ty _ -> __IMPOSSIBLE__ txt <- TCP.prettyTCM =<< normalForm norm term return $ text "Elaborates to:" <+> txt cmd_goal_type_context_and have norm ii rng s interpret (Cmd_show_module_contents norm ii rng s) = liftCommandMT (B.withInteractionId ii) $ showModuleContents norm rng s interpret (Cmd_why_in_scope_toplevel s) = liftCommandMT B.atTopLevel $ whyInScope s interpret (Cmd_why_in_scope ii rng s) = liftCommandMT (B.withInteractionId ii) $ whyInScope s interpret (Cmd_make_case ii rng s) = do (f, casectxt, cs) <- lift $ makeCase ii rng s liftCommandMT (B.withInteractionId ii) $ do hidden <- lift $ showImplicitArguments tel <- lift $ lookupSection (qnameModule f) -- don't shadow the names in this telescope let cs' :: [A.Clause] = List.map (extlam_dropLLifted casectxt hidden) cs pcs :: [Doc] <- lift $ inTopContext $ addContext tel $ mapM prettyA cs' let pcs' :: [String] = List.map (extlam_dropName casectxt . render) pcs lift $ reportSDoc "interaction.case" 60 $ TCP.vcat [ TCP.text "InteractionTop.Cmd_make_case" , TCP.nest 2 $ TCP.vcat [ TCP.text "cs = " TCP.<+> TCP.vcat (map prettyA cs) , TCP.text "cs' = " TCP.<+> TCP.vcat (map prettyA cs') , TCP.text "pcs = " TCP.<+> TCP.vcat (map return pcs) , TCP.text "pcs' = " TCP.<+> TCP.vcat (map TCP.text pcs') ] ] lift $ reportSDoc "interaction.case" 90 $ TCP.vcat [ TCP.text "InteractionTop.Cmd_make_case" , TCP.nest 2 $ TCP.vcat [ TCP.text "cs = " TCP.<+> TCP.text (show cs) , TCP.text "cs' = " TCP.<+> TCP.text (show cs') ] ] putResponse $ Resp_MakeCase (makeCaseVariant casectxt) pcs' where render = renderStyle (style { mode = OneLineMode }) makeCaseVariant :: CaseContext -> MakeCaseVariant makeCaseVariant Nothing = R.Function makeCaseVariant Just{} = R.ExtendedLambda -- very dirty hack, string manipulation by dropping the function name -- and replacing the last " = " with " -> ". It's important not to replace -- the equal sign in named implicit with an arrow! extlam_dropName :: CaseContext -> String -> String extlam_dropName Nothing x = x extlam_dropName Just{} x = unwords $ reverse $ replEquals $ reverse $ drop 1 $ words x where replEquals ("=" : ws) = "→" : ws replEquals (w : ws) = w : replEquals ws replEquals [] = [] -- Drops pattern added to extended lambda functions when lambda lifting them extlam_dropLLifted :: CaseContext -> Bool -> A.Clause -> A.Clause extlam_dropLLifted Nothing _ x = x extlam_dropLLifted _ _ (A.Clause (A.LHS _ A.LHSProj{} _) _ _ _ _ _) = __IMPOSSIBLE__ extlam_dropLLifted (Just (ExtLamInfo h nh)) hidden cl@A.Clause{ A.clauseLHS = A.LHS info (A.LHSHead name nps) ps } = let n = if hidden then h + nh else nh in cl{ A.clauseLHS = A.LHS info (A.LHSHead name (drop n nps)) ps } interpret (Cmd_compute cmode ii rng s) = display_info . Info_NormalForm =<< do liftLocalState $ do e <- B.parseExprIn ii rng $ computeWrapInput cmode s B.withInteractionId ii $ do showComputed cmode =<< do applyWhen (computeIgnoreAbstract cmode) ignoreAbstractMode $ B.evalInCurrent e interpret Cmd_show_version = display_info Info_Version interpret Cmd_abort = return () -- | Show warnings interpretWarnings :: CommandM (String, String) interpretWarnings = do mws <- lift $ Imp.getAllWarnings AllWarnings RespectFlags case filter isNotMeta <$> mws of Imp.SomeWarnings ws@(_:_) -> do let (we, wa) = classifyWarnings ws pwe <- lift $ prettyTCWarnings we pwa <- lift $ prettyTCWarnings wa return (pwe, pwa) _ -> return ("", "") where isNotMeta w = case tcWarning w of UnsolvedInteractionMetas{} -> False UnsolvedMetaVariables{} -> False _ -> True -- | Solved goals already instantiated internally -- The second argument potentially limits it to one specific goal. solveInstantiatedGoals :: B.Rewrite -> Maybe InteractionId -> CommandM () solveInstantiatedGoals norm mii = do -- Andreas, 2016-10-23 issue #2280: throw away meta elims. out <- lift $ local (\ e -> e { envPrintMetasBare = True }) $ do sip <- B.getSolvedInteractionPoints False norm -- only solve metas which have a proper instantiation, i.e., not another meta maybe id (\ ii -> filter ((ii ==) . fst)) mii <$> mapM prt sip putResponse $ Resp_SolveAll out where prt (i, m, e) = do mi <- getMetaInfo <$> lookupMeta m e <- withMetaInfo mi $ abstractToConcreteCtx TopCtx e return (i, e) -- | Print open metas nicely. showOpenMetas :: TCM [String] showOpenMetas = do ims <- B.typesOfVisibleMetas B.AsIs di <- forM ims $ \ i -> B.withInteractionId (B.outputFormId $ B.OutputForm noRange [] i) $ showATop i -- Show unsolved implicit arguments simplified. unsolvedNotOK <- not . optAllowUnsolved <$> pragmaOptions hms <- (guard unsolvedNotOK >>) <$> B.typesOfHiddenMetas B.Simplified dh <- mapM showA' hms return $ di ++ dh where metaId (B.OfType i _) = i metaId (B.JustType i) = i metaId (B.JustSort i) = i metaId (B.Assign i e) = i metaId _ = __IMPOSSIBLE__ showA' :: B.OutputConstraint A.Expr NamedMeta -> TCM String showA' m = do let i = nmid $ metaId m r <- getMetaRange i d <- B.withMetaId i (showATop m) return $ d ++ " [ at " ++ show r ++ " ]" -- | @cmd_load' file argv unsolvedOk cmd@ -- loads the module in file @file@, -- using @argv@ as the command-line options. -- -- If type checking completes without any exceptions having been -- encountered then the command @cmd r@ is executed, where @r@ is the -- result of 'Imp.typeCheckMain'. cmd_load' :: FilePath -> [String] -> Bool -- ^ Allow unsolved meta-variables? -> Imp.Mode -- ^ Full type-checking, or only -- scope-checking? -> ((Interface, Imp.MaybeWarnings) -> CommandM ()) -> CommandM () cmd_load' file argv unsolvedOK mode cmd = do f <- liftIO $ absolute file ex <- liftIO $ doesFileExist $ filePath f let relativeTo | ex = ProjectRoot f | otherwise = CurrentDir -- Forget the previous "current file" and interaction points. modify $ \st -> st { theInteractionPoints = [] , theCurrentFile = Nothing } t <- liftIO $ getModificationTime file -- All options are reset when a file is reloaded, including the -- choice of whether or not to display implicit arguments. opts0 <- gets optionsOnReload z <- liftIO $ runOptM $ parseStandardOptions' argv opts0 case z of Left err -> lift $ typeError $ GenericError err Right opts -> do let update o = o { optAllowUnsolved = unsolvedOK && optAllowUnsolved o} lift $ TM.setCommandLineOptions' relativeTo $ mapPragmaOptions update opts displayStatus -- Reset the state, preserving options and decoded modules. Note -- that if the include directories have changed, then the decoded -- modules are reset when cmd_load' is run by ioTCM. lift resetState -- Clear the info buffer to make room for information about which -- module is currently being type-checked. putResponse Resp_ClearRunningInfo -- Remove any prior syntax highlighting. putResponse Resp_ClearHighlighting -- We activate the cache only when agda is used interactively lift activateLoadedFileCache ok <- lift $ Imp.typeCheckMain f mode -- The module type checked. If the file was not changed while the -- type checker was running then the interaction points and the -- "current file" are stored. t' <- liftIO $ getModificationTime file when (t == t') $ do is <- lift $ sortInteractionPoints =<< getInteractionPoints modify $ \st -> st { theInteractionPoints = is , theCurrentFile = Just (f, t) } cmd ok -- | Set 'envCurrentPath' to 'theCurrentFile', if any. withCurrentFile :: CommandM a -> CommandM a withCurrentFile m = do mfile <- fmap fst <$> gets theCurrentFile local (\ e -> e { envCurrentPath = mfile }) m -- | Available backends. data CompilerBackend = LaTeX | QuickLaTeX | OtherBackend String deriving (Eq) instance Show CompilerBackend where show LaTeX = "LaTeX" show QuickLaTeX = "QuickLaTeX" show (OtherBackend s) = s instance Read CompilerBackend where readsPrec _ s = do (t, s) <- lex s let b = case t of "LaTeX" -> LaTeX "QuickLaTeX" -> QuickLaTeX _ -> OtherBackend t return (b, s) data GiveRefine = Give | Refine | Intro deriving (Eq, Show) -- | A "give"-like action (give, refine, etc). -- -- @give_gen force ii rng s give_ref mk_newtxt@ -- acts on interaction point @ii@ -- occupying range @rng@, -- placing the new content given by string @s@, -- and replacing @ii@ by the newly created interaction points -- in the state if safety checks pass (unless @force@ is applied). give_gen :: UseForce -- ^ Should safety checks be skipped? -> InteractionId -> Range -> String -> GiveRefine -> CommandM () give_gen force ii rng s0 giveRefine = do let s = trim s0 lift $ reportSLn "interaction.give" 20 $ "give_gen " ++ s -- Andreas, 2015-02-26 if string is empty do nothing rather -- than giving a parse error. unless (null s) $ do let give_ref = case giveRefine of Give -> B.give Refine -> B.refine Intro -> B.refine -- save scope of the interaction point (for printing the given expr. later) scope <- lift $ getInteractionScope ii -- parse string and "give", obtaining an abstract expression -- and newly created interaction points (time, (ae, ae0, iis)) <- maybeTimed $ lift $ do mis <- getInteractionPoints reportSLn "interaction.give" 30 $ "interaction points before = " ++ show mis given <- B.parseExprIn ii rng s ae <- give_ref force ii Nothing given mis' <- getInteractionPoints reportSLn "interaction.give" 30 $ "interaction points after = " ++ show mis' return (ae, given, mis' List.\\ mis) -- favonia: backup the old scope for highlighting insertOldInteractionScope ii scope -- sort the new interaction points and put them into the state -- in replacement of the old interaction point iis <- lift $ sortInteractionPoints iis modifyTheInteractionPoints $ replace ii iis -- print abstract expr ce <- lift $ abstractToConcreteEnv (makeEnv scope) ae lift $ reportSLn "interaction.give" 30 $ unlines [ "ce = " ++ show ce , "scopePrecedence = " ++ show (scopePrecedence scope) ] -- if the command was @Give@, use the literal user input; -- Andreas, 2014-01-15, see issue 1020: -- Refine could solve a goal by introducing the sole constructor -- without arguments. Then there are no interaction metas, but -- we still cannot just `give' the user string (which may be empty). -- WRONG: also, if no interaction metas were created by @Refine@ -- WRONG: let literally = (giveRefine == Give || null iis) && rng /= noRange -- Ulf, 2015-03-30, if we're doing intro we can't do literal give since -- there is nothing in the hole (issue 1892). let literally = giveRefine /= Intro && ae == ae0 && rng /= noRange -- Ulf, 2014-01-24: This works for give since we're highlighting the string -- that's already in the buffer. Doing it before the give action means that -- the highlighting is moved together with the text when the hole goes away. -- To make it work for refine we'd have to adjust the ranges. when literally $ lift $ do printHighlightingInfo =<< generateTokenInfoFromString rng s highlightExpr ae putResponse $ Resp_GiveAction ii $ mkNewTxt literally ce lift $ reportSLn "interaction.give" 30 $ "putResponse GiveAction passed" -- display new goal set (if not measuring time) maybe (interpret Cmd_metas) (display_info . Info_Time) time lift $ reportSLn "interaction.give" 30 $ "interpret Cmd_metas passed" where -- Substitutes xs for x in ys. replace x xs ys = concatMap (\ y -> if y == x then xs else [y]) ys -- For @Give@ we can replace the ii by the user given input. mkNewTxt True C.Paren{} = Give_Paren mkNewTxt True _ = Give_NoParen -- Otherwise, we replace it by the reified value Agda computed. mkNewTxt False ce = Give_String $ show ce highlightExpr :: A.Expr -> TCM () highlightExpr e = local (\e -> e { envModuleNestingLevel = 0 , envHighlightingLevel = NonInteractive , envHighlightingMethod = Direct }) $ generateAndPrintSyntaxInfo decl Full True where dummy = mkName_ (NameId 0 0) "dummy" info = mkDefInfo (nameConcrete dummy) noFixity' PublicAccess ConcreteDef (getRange e) decl = A.Axiom NoFunSig info defaultArgInfo Nothing (qnameFromList [dummy]) e -- | Sorts interaction points based on their ranges. sortInteractionPoints :: [InteractionId] -> TCM [InteractionId] sortInteractionPoints is = map fst . List.sortBy (compare `on` snd) <$> do forM is $ \ i -> do (i,) <$> getInteractionRange i -- | Pretty-prints the type of the meta-variable. prettyTypeOfMeta :: B.Rewrite -> InteractionId -> TCM Doc prettyTypeOfMeta norm ii = do form <- B.typeOfMeta norm ii case form of B.OfType _ e -> prettyATop e _ -> text <$> showATop form -- | Pretty-prints the context of the given meta-variable. prettyContext :: B.Rewrite -- ^ Normalise? -> Bool -- ^ Print the elements in reverse order? -> InteractionId -> TCM Doc prettyContext norm rev ii = B.withInteractionId ii $ do ctx <- B.contextOfMeta ii norm es <- mapM (prettyATop . B.ofExpr) ctx ns <- mapM (showATop . B.ofName) ctx return $ align 10 $ applyWhen rev reverse $ filter (not . null . fst) $ zip ns $ map (text ":" <+>) es -- | Create type of application of new helper function that would solve the goal. cmd_helper_function :: B.Rewrite -> InteractionId -> Range -> String -> TCM Doc cmd_helper_function norm ii r s = B.withInteractionId ii $ inTopContext $ prettyATop =<< B.metaHelperType norm ii r s -- | Displays the current goal, the given document, and the current -- context. -- -- Should not modify the state. cmd_goal_type_context_and :: Doc -> B.Rewrite -> InteractionId -> Range -> String -> StateT CommandState (TCMT IO) () cmd_goal_type_context_and doc norm ii _ _ = display_info . Info_GoalType =<< do lift $ do goal <- B.withInteractionId ii $ prettyTypeOfMeta norm ii ctx <- prettyContext norm True ii return $ vcat [ text "Goal:" <+> goal , doc , text (replicate 60 '\x2014') , ctx ] -- | Shows all the top-level names in the given module, along with -- their types. showModuleContents :: B.Rewrite -> Range -> String -> CommandM () showModuleContents norm rng s = display_info . Info_ModuleContents =<< do liftLocalState $ do (modules, types) <- B.moduleContents norm rng s types' <- forM types $ \ (x, t) -> do t <- TCP.prettyTCM t return (prettyShow x, text ":" <+> t) return $ vcat [ text "Modules" , nest 2 $ vcat $ map (text . show) modules , text "Names" , nest 2 $ align 10 types' ] -- | Shows all the top-level names in scope which mention all the given -- identifiers in their type. searchAbout :: B.Rewrite -> Range -> String -> CommandM () searchAbout norm rg nm = do let tnm = trim nm unless (null tnm) $ do fancy <- lift $ B.atTopLevel $ do hits <- findMentions norm rg tnm forM hits $ \ (x, t) -> do t <- TCP.prettyTCM t return (prettyShow x, text ":" <+> t) display_info $ Info_SearchAbout $ text "Definitions about" <+> text (List.intercalate ", " $ words nm) $$ nest 2 (align 10 fancy) -- | Explain why something is in scope. whyInScope :: String -> CommandM () whyInScope s = display_info . Info_WhyInScope =<< do Just (file, _) <- gets theCurrentFile let cwd = takeDirectory $ filePath file liftLocalState $ do (v, xs, ms) <- B.whyInScope s explanation cwd v xs ms where explanation _ Nothing [] [] = TCP.text (s ++ " is not in scope.") explanation cwd v xs ms = TCP.vcat [ TCP.text (s ++ " is in scope as") , TCP.nest 2 $ TCP.vcat [variable v xs, modules ms] ] where prettyRange :: Range -> TCM Doc prettyRange r = text . show . (fmap . fmap) mkRel <$> do return r mkRel = Str . makeRelative cwd . filePath -- variable :: Maybe _ -> [_] -> TCM Doc variable Nothing xs = names xs variable (Just x) xs | null xs = asVar | otherwise = TCP.vcat [ TCP.sep [ asVar, TCP.nest 2 $ shadowing x] , TCP.nest 2 $ names xs ] where asVar :: TCM Doc asVar = do TCP.text "* a variable bound at" TCP.<+> TCP.prettyTCM (nameBindingSite $ localVar x) shadowing :: LocalVar -> TCM Doc shadowing (LocalVar _ _ []) = TCP.text "shadowing" shadowing _ = TCP.text "in conflict with" names xs = TCP.vcat $ map pName xs modules ms = TCP.vcat $ map pMod ms pKind DefName = TCP.text "defined name" pKind ConName = TCP.text "constructor" pKind FldName = TCP.text "record field" pKind PatternSynName = TCP.text "pattern synonym" pKind MacroName = TCP.text "macro name" pKind QuotableName = TCP.text "quotable name" pName :: AbstractName -> TCM Doc pName a = TCP.sep [ TCP.text "* a" TCP.<+> pKind (anameKind a) TCP.<+> TCP.text (prettyShow $ anameName a) , TCP.nest 2 $ TCP.text "brought into scope by" ] TCP.$$ TCP.nest 2 (pWhy (nameBindingSite $ qnameName $ anameName a) (anameLineage a)) pMod :: AbstractModule -> TCM Doc pMod a = TCP.sep [ TCP.text "* a module" TCP.<+> TCP.text (prettyShow $ amodName a) , TCP.nest 2 $ TCP.text "brought into scope by" ] TCP.$$ TCP.nest 2 (pWhy (nameBindingSite $ qnameName $ mnameToQName $ amodName a) (amodLineage a)) pWhy :: Range -> WhyInScope -> TCM Doc pWhy r Defined = TCP.text "- its definition at" TCP.<+> TCP.prettyTCM r pWhy r (Opened (C.QName x) w) | isNoName x = pWhy r w pWhy r (Opened m w) = TCP.text "- the opening of" TCP.<+> TCP.text (show m) TCP.<+> TCP.text "at" TCP.<+> TCP.prettyTCM (getRange m) TCP.$$ pWhy r w pWhy r (Applied m w) = TCP.text "- the application of" TCP.<+> TCP.text (show m) TCP.<+> TCP.text "at" TCP.<+> TCP.prettyTCM (getRange m) TCP.$$ pWhy r w -- | Sets the command line options and updates the status information. setCommandLineOpts :: CommandLineOptions -> CommandM () setCommandLineOpts opts = do lift $ TM.setCommandLineOptions opts displayStatus -- | Computes some status information. -- -- Does not change the state. status :: CommandM Status status = do cf <- gets theCurrentFile showImpl <- lift showImplicitArguments -- Check if the file was successfully type checked, and has not -- changed since. Note: This code does not check if any dependencies -- have changed, and uses a time stamp to check for changes. checked <- lift $ case cf of Nothing -> return False Just (f, t) -> do t' <- liftIO $ getModificationTime $ filePath f case t == t' of False -> return False True -> do mm <- lookupModuleFromSource f case mm of Nothing -> return False -- work-around for Issue1007 Just m -> maybe False (not . miWarnings) <$> getVisitedModule m return $ Status { sShowImplicitArguments = showImpl , sChecked = checked } -- | Displays or updates status information. -- -- Does not change the state. displayStatus :: CommandM () displayStatus = putResponse . Resp_Status =<< status -- | @display_info@ does what @'display_info'' False@ does, but -- additionally displays some status information (see 'status' and -- 'displayStatus'). display_info :: DisplayInfo -> CommandM () display_info info = do displayStatus putResponse $ Resp_DisplayInfo info refreshStr :: [String] -> String -> ([String], String) refreshStr taken s = go nameModifiers where go (m:mods) = let s' = s ++ m in if s' `elem` taken then go mods else (s':taken, s') go _ = __IMPOSSIBLE__ nameModifiers :: [String] nameModifiers = "" : "'" : "''" : [show i | i <-[3..]] -- | Parses and scope checks an expression (using the \"inside scope\" -- as the scope), performs the given command with the expression as -- input, and displays the result. parseAndDoAtToplevel' :: (A.Expr -> TCM Doc) -- ^ The command to perform. -> (Doc -> DisplayInfo) -- ^ The name to use for the buffer displaying the output. -> String -- ^ The expression to parse. -> CommandM () parseAndDoAtToplevel' cmd title s = do (time, res) <- localStateCommandM $ do e <- lift $ runPM $ parse exprParser s maybeTimed (lift $ B.atTopLevel $ cmd =<< concreteToAbstract_ e) display_info (title $ fromMaybe empty time $$ res) parseAndDoAtToplevel :: (A.Expr -> TCM A.Expr) -> (Doc -> DisplayInfo) -> String -> CommandM () parseAndDoAtToplevel cmd = parseAndDoAtToplevel' (prettyA <=< cmd) maybeTimed :: CommandM a -> CommandM (Maybe Doc, a) maybeTimed work = do doTime <- lift $ hasVerbosity "profile.interactive" 10 if not doTime then (Nothing,) <$> work else do (r, time) <- measureTime work return (Just $ text "Time:" <+> pretty time, r) -- | Tell to highlight the code using the given highlighting -- info (unless it is @Nothing@). tellToUpdateHighlighting :: Maybe (HighlightingInfo, HighlightingMethod, ModuleToSource) -> IO [Response] tellToUpdateHighlighting Nothing = return [] tellToUpdateHighlighting (Just (info, method, modFile)) = return [Resp_HighlightingInfo info method modFile] -- | Tells the Emacs mode to go to the first error position (if any). tellEmacsToJumpToError :: Range -> [Response] tellEmacsToJumpToError r = case rStart r of Nothing -> [] Just (Pn { srcFile = Strict.Nothing }) -> [] Just (Pn { srcFile = Strict.Just f, posPos = p }) -> [ Resp_JumpToError (filePath f) p ] Agda-2.5.3/src/full/Agda/Interaction/BasicOps.hs0000644000000000000000000012273513154613124017460 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE UndecidableInstances #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Agda.Interaction.BasicOps where import Prelude hiding (null) import Control.Arrow ((***), first, second) import Control.Applicative hiding (empty) import Control.Monad.Reader import Control.Monad.State import Control.Monad.Identity import qualified Data.Map as Map import qualified Data.Set as Set import qualified Data.List as List import Data.Maybe import Data.Traversable hiding (mapM, forM, for) import Data.Monoid import qualified Agda.Syntax.Concrete as C -- ToDo: Remove with instance of ToConcrete import Agda.Syntax.Position import Agda.Syntax.Abstract as A hiding (Open, Apply, Assign) import Agda.Syntax.Abstract.Views as A import Agda.Syntax.Abstract.Pretty import Agda.Syntax.Common import Agda.Syntax.Info (ExprInfo(..),MetaInfo(..),emptyMetaInfo,exprNoRange) import qualified Agda.Syntax.Info as Info import Agda.Syntax.Internal as I import Agda.Syntax.Literal import Agda.Syntax.Translation.InternalToAbstract import Agda.Syntax.Translation.AbstractToConcrete import Agda.Syntax.Translation.ConcreteToAbstract import Agda.Syntax.Scope.Base import Agda.Syntax.Scope.Monad import Agda.Syntax.Fixity(Precedence(..)) import Agda.Syntax.Parser import Agda.TheTypeChecker import Agda.TypeChecking.Constraints import Agda.TypeChecking.Conversion import Agda.TypeChecking.Errors ( stringTCErr ) import Agda.TypeChecking.Monad as M hiding (MetaInfo) import Agda.TypeChecking.MetaVars import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.With import Agda.TypeChecking.Coverage import Agda.TypeChecking.Records import Agda.TypeChecking.Irrelevance (wakeIrrelevantVars) import Agda.TypeChecking.Pretty (prettyTCM) import Agda.TypeChecking.Free import Agda.TypeChecking.CheckInternal import Agda.TypeChecking.SizedTypes.Solve import qualified Agda.TypeChecking.Pretty as TP import Agda.TypeChecking.Warnings (runPM) import Agda.Termination.TermCheck (termMutual) import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Pretty import Agda.Utils.Permutation import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible -- | Parses an expression. parseExpr :: Range -> String -> TCM C.Expr parseExpr rng s = do C.ExprWhere e wh <- runPM $ parsePosString exprWhereParser pos s unless (null wh) $ typeError $ GenericError $ "where clauses are not supported in holes" return e where pos = fromMaybe (startPos Nothing) $ rStart rng parseExprIn :: InteractionId -> Range -> String -> TCM Expr parseExprIn ii rng s = do mId <- lookupInteractionId ii updateMetaVarRange mId rng mi <- getMetaInfo <$> lookupMeta mId e <- parseExpr rng s concreteToAbstract (clScope mi) e giveExpr :: UseForce -> Maybe InteractionId -> MetaId -> Expr -> TCM () -- When translator from internal to abstract is given, this function might return -- the expression returned by the type checker. giveExpr force mii mi e = do mv <- lookupMeta mi -- In the context (incl. signature) of the meta variable, -- type check expression and assign meta withMetaInfo (getMetaInfo mv) $ do metaTypeCheck mv (mvJudgement mv) where metaTypeCheck mv IsSort{} = __IMPOSSIBLE__ metaTypeCheck mv (HasType _ t) = disableDestructiveUpdate $ do reportSDoc "interaction.give" 20 $ TP.text "give: meta type =" TP.<+> prettyTCM t -- Here, we must be in the same context where the meta was created. -- Thus, we can safely apply its type to the context variables. ctx <- getContextArgs t' <- t `piApplyM` permute (takeP (length ctx) $ mvPermutation mv) ctx traceCall (CheckExprCall e t') $ do reportSDoc "interaction.give" 20 $ TP.text "give: instantiated meta type =" TP.<+> prettyTCM t' v <- checkExpr e t' case mvInstantiation mv of InstV xs v' -> unlessM ((Irrelevant ==) <$> asks envRelevance) $ do reportSDoc "interaction.give" 20 $ TP.sep [ TP.text "meta was already set to value v' = " TP.<+> prettyTCM v' TP.<+> TP.text " with free variables " TP.<+> return (fsep $ map pretty xs) , TP.text "now comparing it to given value v = " TP.<+> prettyTCM v , TP.text "in context " TP.<+> inTopContext (prettyTCM ctx) ] -- The number of free variables should be at least the size of the context -- (Ideally, if we implemented contextual type theory, it should be the same.) when (length xs < size ctx) __IMPOSSIBLE__ -- if there are more free variables than the context has -- we need to abstract over the additional ones (xs2) let (_xs1, xs2) = splitAt (size ctx) xs v' <- return $ foldr mkLam v' xs2 reportSDoc "interaction.give" 20 $ TP.sep [ TP.text "in meta context, v' = " TP.<+> prettyTCM v' ] equalTerm t' v v' -- Note: v' now lives in context of meta _ -> do -- updateMeta mi v reportSLn "interaction.give" 20 "give: meta unassigned, assigning..." args <- getContextArgs nowSolvingConstraints $ assign DirEq mi args v reportSDoc "interaction.give" 20 $ TP.text "give: meta variable updated!" unless (force == WithForce) $ redoChecks mii wakeupConstraints mi solveSizeConstraints DontDefaultToInfty unless (force == WithForce) $ do -- Double check. reportSDoc "interaction.give" 20 $ TP.text "give: double checking" vfull <- instantiateFull v checkInternal vfull t' -- | After a give, redo termination etc. checks for function which was complemented. redoChecks :: Maybe InteractionId -> TCM () redoChecks Nothing = return () redoChecks (Just ii) = do reportSLn "interaction.give" 20 $ "give: redoing termination check for function surrounding " ++ show ii ip <- lookupInteractionPoint ii case ipClause ip of IPNoClause -> return () IPClause f _ _ -> do mb <- mutualBlockOf f terErrs <- local (\ e -> e { envMutualBlock = Just mb }) $ termMutual [] unless (null terErrs) $ typeError $ TerminationCheckFailed terErrs -- TODO redo positivity check! -- | Try to fill hole by expression. -- -- Returns the given expression unchanged -- (for convenient generalization to @'refine'@). give :: UseForce -- ^ Skip safety checks? -> InteractionId -- ^ Hole. -> Maybe Range -> Expr -- ^ The expression to give. -> TCM Expr -- ^ If successful, the very expression is returned unchanged. give force ii mr e = liftTCM $ do -- if Range is given, update the range of the interaction meta mi <- lookupInteractionId ii whenJust mr $ updateMetaVarRange mi reportSDoc "interaction.give" 10 $ TP.text "giving expression" TP.<+> prettyTCM e reportSDoc "interaction.give" 50 $ TP.text $ show $ deepUnscope e -- Try to give mi := e do setMetaOccursCheck mi DontRunMetaOccursCheck -- #589, #2710: Allow giving recursive solutions. giveExpr force (Just ii) mi e `catchError` \ case -- Turn PatternErr into proper error: PatternErr{} -> typeError . GenericDocError =<< do withInteractionId ii $ TP.text "Failed to give" TP.<+> prettyTCM e err -> throwError err removeInteractionPoint ii return e -- | Try to refine hole by expression @e@. -- -- This amounts to successively try to give @e@, @e ?@, @e ? ?@, ... -- Returns the successfully given expression. refine :: UseForce -- ^ Skip safety checks when giving? -> InteractionId -- ^ Hole. -> Maybe Range -> Expr -- ^ The expression to refine the hole with. -> TCM Expr -- ^ The successfully given expression. refine force ii mr e = do mi <- lookupInteractionId ii mv <- lookupMeta mi let range = fromMaybe (getRange mv) mr scope = M.getMetaScope mv reportSDoc "interaction.refine" 10 $ TP.text "refining with expression" TP.<+> prettyTCM e reportSDoc "interaction.refine" 50 $ TP.text $ show $ deepUnscope e -- We try to append up to 10 meta variables tryRefine 10 range scope e where tryRefine :: Int -> Range -> ScopeInfo -> Expr -> TCM Expr tryRefine nrOfMetas r scope e = try nrOfMetas e where try :: Int -> Expr -> TCM Expr try 0 e = throwError $ stringTCErr "Cannot refine" try n e = give force ii (Just r) e `catchError` (\_ -> try (n - 1) =<< appMeta e) -- Apply A.Expr to a new meta appMeta :: Expr -> TCM Expr appMeta e = do let rng = rightMargin r -- Andreas, 2013-05-01 conflate range to its right margin to ensure that appended metas are last in numbering. This fixes issue 841. -- Make new interaction point ii <- registerInteractionPoint False rng Nothing let info = Info.MetaInfo { Info.metaRange = rng , Info.metaScope = scope { scopePrecedence = [ArgumentCtx] } -- Ulf, 2017-09-07: The `ArgumentCtx` above is causing #737. -- If we're building an operator application the precedence -- should be something else. , metaNumber = Nothing -- in order to print just as ?, not ?n , metaNameSuggestion = "" } metaVar = QuestionMark info ii count x e = getSum $ foldExpr isX e where isX (A.Var y) | x == y = Sum 1 isX _ = mempty lamView (A.Lam _ (DomainFree _ x) e) = Just (x, e) lamView (A.Lam i (DomainFull (TypedBindings r (Arg ai (TBind br (x : xs) a)))) e) | null xs = Just (dget x, e) | otherwise = Just (dget x, A.Lam i (DomainFull $ TypedBindings r $ Arg ai $ TBind br xs a) e) lamView _ = Nothing -- reduce beta-redexes where the argument is used at most once smartApp i e arg = case lamView $ unScope e of Just (x, e) | count x e < 2 -> mapExpr subX e where subX (A.Var y) | x == y = namedArg arg subX e = e _ -> App i e arg return $ smartApp (ExprRange r) e $ defaultNamedArg metaVar --ToDo: The position of metaVar is not correct --ToDo: The fixity of metavars is not correct -- fixed? MT {-| Evaluate the given expression in the current environment -} evalInCurrent :: Expr -> TCM Expr evalInCurrent e = do (v, t) <- inferExpr e v' <- {- etaContract =<< -} normalise v reify v' evalInMeta :: InteractionId -> Expr -> TCM Expr evalInMeta ii e = do m <- lookupInteractionId ii mi <- getMetaInfo <$> lookupMeta m withMetaInfo mi $ evalInCurrent e -- | Modifier for interactive commands, -- specifying the amount of normalization in the output. -- data Rewrite = AsIs | Instantiated | HeadNormal | Simplified | Normalised deriving (Show, Read) normalForm :: (Reduce t, Simplify t, Normalise t) => Rewrite -> t -> TCM t normalForm AsIs t = return t normalForm Instantiated t = return t -- reify does instantiation normalForm HeadNormal t = {- etaContract =<< -} reduce t normalForm Simplified t = {- etaContract =<< -} simplify t normalForm Normalised t = {- etaContract =<< -} normalise t -- | Modifier for the interactive computation command, -- specifying the mode of computation and result display. -- data ComputeMode = DefaultCompute | IgnoreAbstract | UseShowInstance deriving (Show, Read, Eq) computeIgnoreAbstract :: ComputeMode -> Bool computeIgnoreAbstract DefaultCompute = False computeIgnoreAbstract IgnoreAbstract = True computeIgnoreAbstract UseShowInstance = True -- UseShowInstance requires the result to be a string literal so respecting -- abstract can only ever break things. computeWrapInput :: ComputeMode -> String -> String computeWrapInput UseShowInstance s = "show (" ++ s ++ ")" computeWrapInput _ s = s showComputed :: ComputeMode -> Expr -> TCM Doc showComputed UseShowInstance e = case e of A.Lit (LitString _ s) -> pure (text s) _ -> (text "Not a string:" $$) <$> prettyATop e showComputed _ e = prettyATop e -- | Modifier for interactive commands, -- specifying whether safety checks should be ignored. data UseForce = WithForce -- ^ Ignore additional checks, like termination/positivity... | WithoutForce -- ^ Don't ignore any checks. deriving (Eq, Read, Show) data OutputForm a b = OutputForm Range [ProblemId] (OutputConstraint a b) deriving (Functor) data OutputConstraint a b = OfType b a | CmpInType Comparison a b b | CmpElim [Polarity] a [b] [b] | JustType b | CmpTypes Comparison b b | CmpLevels Comparison b b | CmpTeles Comparison b b | JustSort b | CmpSorts Comparison b b | Guard (OutputConstraint a b) ProblemId | Assign b a | TypedAssign b a a | PostponedCheckArgs b [a] a a | IsEmptyType a | SizeLtSat a | FindInScopeOF b a [(a,a)] deriving (Functor) -- | A subset of 'OutputConstraint'. data OutputConstraint' a b = OfType' { ofName :: b , ofExpr :: a } outputFormId :: OutputForm a b -> b outputFormId (OutputForm _ _ o) = out o where out o = case o of OfType i _ -> i CmpInType _ _ i _ -> i CmpElim _ _ (i:_) _ -> i CmpElim _ _ [] _ -> __IMPOSSIBLE__ JustType i -> i CmpLevels _ i _ -> i CmpTypes _ i _ -> i CmpTeles _ i _ -> i JustSort i -> i CmpSorts _ i _ -> i Guard o _ -> out o Assign i _ -> i TypedAssign i _ _ -> i PostponedCheckArgs i _ _ _ -> i IsEmptyType _ -> __IMPOSSIBLE__ -- Should never be used on IsEmpty constraints SizeLtSat{} -> __IMPOSSIBLE__ FindInScopeOF _ _ _ -> __IMPOSSIBLE__ instance Reify ProblemConstraint (Closure (OutputForm Expr Expr)) where reify (PConstr pids cl) = enterClosure cl $ \c -> buildClosure =<< (OutputForm (getRange c) (Set.toList pids) <$> reify c) reifyElimToExpr :: I.Elim -> TCM Expr reifyElimToExpr e = case e of I.Apply v -> appl "apply" <$> reify v I.Proj _o f -> appl "proj" <$> reify ((defaultArg $ I.Def f []) :: Arg Term) where appl :: String -> Arg Expr -> Expr appl s v = A.App exprNoRange (A.Lit (LitString noRange s)) $ fmap unnamed v instance Reify Constraint (OutputConstraint Expr Expr) where reify (ValueCmp cmp t u v) = CmpInType cmp <$> reify t <*> reify u <*> reify v reify (ElimCmp cmp t v es1 es2) = CmpElim cmp <$> reify t <*> mapM reifyElimToExpr es1 <*> mapM reifyElimToExpr es2 reify (LevelCmp cmp t t') = CmpLevels cmp <$> reify t <*> reify t' reify (TypeCmp cmp t t') = CmpTypes cmp <$> reify t <*> reify t' reify (TelCmp a b cmp t t') = CmpTeles cmp <$> (ETel <$> reify t) <*> (ETel <$> reify t') reify (SortCmp cmp s s') = CmpSorts cmp <$> reify s <*> reify s' reify (Guarded c pid) = do o <- reify c return $ Guard o pid reify (UnBlock m) = do mi <- mvInstantiation <$> lookupMeta m m' <- reify (MetaV m []) case mi of BlockedConst t -> do e <- reify t return $ Assign m' e PostponedTypeCheckingProblem cl _ -> enterClosure cl $ \p -> case p of CheckExpr e a -> do a <- reify a return $ TypedAssign m' e a CheckLambda (Arg ai (xs, mt)) body target -> do domType <- maybe (return underscore) reify mt target <- reify target let bs = TypedBindings noRange $ Arg ai $ TBind noRange xs domType e = A.Lam Info.defaultLamInfo_ (DomainFull bs) body return $ TypedAssign m' e target CheckArgs _ _ args t0 t1 _ -> do t0 <- reify t0 t1 <- reify t1 return $ PostponedCheckArgs m' (map (namedThing . unArg) args) t0 t1 UnquoteTactic tac _ goal -> do tac <- A.App exprNoRange (A.Unquote exprNoRange) . defaultNamedArg <$> reify tac OfType tac <$> reify goal Open{} -> __IMPOSSIBLE__ OpenIFS{} -> __IMPOSSIBLE__ InstV{} -> __IMPOSSIBLE__ reify (FindInScope m _b mcands) = FindInScopeOF <$> (reify $ MetaV m []) <*> (reify =<< getMetaType m) <*> (forM (fromMaybe [] mcands) $ \ (Candidate tm ty eti _) -> do (,) <$> reify tm <*> reify ty) reify (IsEmpty r a) = IsEmptyType <$> reify a reify (CheckSizeLtSat a) = SizeLtSat <$> reify a -- ASR TODO (28 December 2014): This function will be unnecessary when -- using a Pretty instance for OutputConstraint instead of the Show -- instance. showComparison :: Comparison -> String showComparison cmp = " " ++ prettyShow cmp ++ " " instance (Show a,Show b) => Show (OutputForm a b) where show o = case o of OutputForm r [] c -> show c ++ range r OutputForm r pids c -> show pids ++ " " ++ show c ++ range r where range r | null s = "" | otherwise = " [ at " ++ s ++ " ]" where s = show r instance (Show a,Show b) => Show (OutputConstraint a b) where show (OfType e t) = show e ++ " : " ++ show t show (JustType e) = "Type " ++ show e show (JustSort e) = "Sort " ++ show e show (CmpInType cmp t e e') = show e ++ showComparison cmp ++ show e' ++ " : " ++ show t show (CmpElim cmp t e e') = show e ++ " == " ++ show e' ++ " : " ++ show t show (CmpTypes cmp t t') = show t ++ showComparison cmp ++ show t' show (CmpLevels cmp t t') = show t ++ showComparison cmp ++ show t' show (CmpTeles cmp t t') = show t ++ showComparison cmp ++ show t' show (CmpSorts cmp s s') = show s ++ showComparison cmp ++ show s' show (Guard o pid) = show o ++ " [blocked by problem " ++ prettyShow pid ++ "]" show (Assign m e) = show m ++ " := " ++ show e show (TypedAssign m e a) = show m ++ " := " ++ show e ++ " :? " ++ show a show (PostponedCheckArgs m es t0 t1) = show m ++ " := (_ : " ++ show t0 ++ ") " ++ unwords (map (paren . show) es) ++ " : " ++ show t1 where paren s | elem ' ' s = "(" ++ s ++ ")" | otherwise = s show (IsEmptyType a) = "Is empty: " ++ show a show (SizeLtSat a) = "Not empty type of sizes: " ++ show a show (FindInScopeOF s t cs) = "Resolve instance argument " ++ showCand (s,t) ++ ".\n Candidates:\n [ " ++ List.intercalate "\n , " (map showCand cs) ++ " ]" where showCand (tm,ty) = indent 6 $ show tm ++ " : " ++ show ty indent n s = List.intercalate ("\n" ++ replicate n ' ') $ lines s instance (ToConcrete a c, ToConcrete b d) => ToConcrete (OutputForm a b) (OutputForm c d) where toConcrete (OutputForm r pid c) = OutputForm r pid <$> toConcrete c instance (ToConcrete a c, ToConcrete b d) => ToConcrete (OutputConstraint a b) (OutputConstraint c d) where toConcrete (OfType e t) = OfType <$> toConcrete e <*> toConcreteCtx TopCtx t toConcrete (JustType e) = JustType <$> toConcrete e toConcrete (JustSort e) = JustSort <$> toConcrete e toConcrete (CmpInType cmp t e e') = CmpInType cmp <$> toConcreteCtx TopCtx t <*> toConcreteCtx ArgumentCtx e <*> toConcreteCtx ArgumentCtx e' toConcrete (CmpElim cmp t e e') = CmpElim cmp <$> toConcreteCtx TopCtx t <*> toConcreteCtx TopCtx e <*> toConcreteCtx TopCtx e' toConcrete (CmpTypes cmp e e') = CmpTypes cmp <$> toConcreteCtx ArgumentCtx e <*> toConcreteCtx ArgumentCtx e' toConcrete (CmpLevels cmp e e') = CmpLevels cmp <$> toConcreteCtx ArgumentCtx e <*> toConcreteCtx ArgumentCtx e' toConcrete (CmpTeles cmp e e') = CmpTeles cmp <$> toConcrete e <*> toConcrete e' toConcrete (CmpSorts cmp e e') = CmpSorts cmp <$> toConcreteCtx ArgumentCtx e <*> toConcreteCtx ArgumentCtx e' toConcrete (Guard o pid) = Guard <$> toConcrete o <*> pure pid toConcrete (Assign m e) = noTakenNames $ Assign <$> toConcrete m <*> toConcreteCtx TopCtx e toConcrete (TypedAssign m e a) = TypedAssign <$> toConcrete m <*> toConcreteCtx TopCtx e <*> toConcreteCtx TopCtx a toConcrete (PostponedCheckArgs m args t0 t1) = PostponedCheckArgs <$> toConcrete m <*> toConcrete args <*> toConcrete t0 <*> toConcrete t1 toConcrete (IsEmptyType a) = IsEmptyType <$> toConcreteCtx TopCtx a toConcrete (SizeLtSat a) = SizeLtSat <$> toConcreteCtx TopCtx a toConcrete (FindInScopeOF s t cs) = FindInScopeOF <$> toConcrete s <*> toConcrete t <*> mapM (\(tm,ty) -> (,) <$> toConcrete tm <*> toConcrete ty) cs instance (Pretty a, Pretty b) => Pretty (OutputConstraint' a b) where pretty (OfType' e t) = pretty e <+> text ":" <+> pretty t instance (ToConcrete a c, ToConcrete b d) => ToConcrete (OutputConstraint' a b) (OutputConstraint' c d) where toConcrete (OfType' e t) = OfType' <$> toConcrete e <*> toConcreteCtx TopCtx t getConstraints :: TCM [OutputForm C.Expr C.Expr] getConstraints = liftTCM $ do cs <- M.getAllConstraints cs <- forM cs $ \c -> do cl <- reify c enterClosure cl abstractToConcrete_ ss <- mapM toOutputForm =<< getSolvedInteractionPoints True AsIs -- get all return $ ss ++ cs where toOutputForm (ii, mi, e) = do mv <- getMetaInfo <$> lookupMeta mi withMetaInfo mv $ do let m = QuestionMark emptyMetaInfo{ metaNumber = Just $ fromIntegral ii } ii abstractToConcrete_ $ OutputForm noRange [] $ Assign m e -- | @getSolvedInteractionPoints True@ returns all solutions, -- even if just solved by another, non-interaction meta. -- -- @getSolvedInteractionPoints False@ only returns metas that -- are solved by a non-meta. getSolvedInteractionPoints :: Bool -> Rewrite -> TCM [(InteractionId, MetaId, Expr)] getSolvedInteractionPoints all norm = concat <$> do mapM solution =<< getInteractionIdsAndMetas where solution (i, m) = do mv <- lookupMeta m withMetaInfo (getMetaInfo mv) $ do args <- getContextArgs scope <- getScope let sol v = do -- Andreas, 2014-02-17 exclude metas solved by metas v <- ignoreSharing <$> instantiate v let isMeta = case v of MetaV{} -> True; _ -> False if isMeta && not all then return [] else do e <- reify =<< normalForm norm v return [(i, m, ScopedExpr scope e)] unsol = return [] case mvInstantiation mv of InstV{} -> sol (MetaV m $ map Apply args) Open{} -> unsol OpenIFS{} -> unsol BlockedConst{} -> unsol PostponedTypeCheckingProblem{} -> unsol typeOfMetaMI :: Rewrite -> MetaId -> TCM (OutputConstraint Expr NamedMeta) typeOfMetaMI norm mi = do mv <- lookupMeta mi withMetaInfo (getMetaInfo mv) $ rewriteJudg mv (mvJudgement mv) where rewriteJudg :: MetaVariable -> Judgement MetaId -> TCM (OutputConstraint Expr NamedMeta) rewriteJudg mv (HasType i t) = do ms <- getMetaNameSuggestion i t <- normalForm norm t vs <- getContextArgs let x = NamedMeta ms i reportSDoc "interactive.meta" 10 $ TP.vcat [ TP.text $ unwords ["permuting", show i, "with", show $ mvPermutation mv] , TP.nest 2 $ TP.vcat [ TP.text "len =" TP.<+> TP.text (show $ length vs) , TP.text "args =" TP.<+> prettyTCM vs , TP.text "t =" TP.<+> prettyTCM t , TP.text "x =" TP.<+> TP.pretty x ] ] reportSDoc "interactive.meta.scope" 20 $ TP.text $ show $ getMetaScope mv -- Andreas, 2016-01-19, issue #1783: need piApplyM instead of just piApply OfType x <$> do reify =<< t `piApplyM` permute (takeP (size vs) $ mvPermutation mv) vs rewriteJudg mv (IsSort i t) = do ms <- getMetaNameSuggestion i return $ JustSort $ NamedMeta ms i typeOfMeta :: Rewrite -> InteractionId -> TCM (OutputConstraint Expr InteractionId) typeOfMeta norm ii = typeOfMeta' norm . (ii,) =<< lookupInteractionId ii typeOfMeta' :: Rewrite -> (InteractionId, MetaId) -> TCM (OutputConstraint Expr InteractionId) typeOfMeta' norm (ii, mi) = fmap (\_ -> ii) <$> typeOfMetaMI norm mi typesOfVisibleMetas :: Rewrite -> TCM [OutputConstraint Expr InteractionId] typesOfVisibleMetas norm = liftTCM $ mapM (typeOfMeta' norm) =<< getInteractionIdsAndMetas typesOfHiddenMetas :: Rewrite -> TCM [OutputConstraint Expr NamedMeta] typesOfHiddenMetas norm = liftTCM $ do is <- getInteractionMetas store <- Map.filterWithKey (openAndImplicit is) <$> getMetaStore mapM (typeOfMetaMI norm) $ Map.keys store where openAndImplicit is x m = case mvInstantiation m of M.InstV{} -> False M.Open -> x `notElem` is M.OpenIFS -> x `notElem` is -- OR: True !? M.BlockedConst{} -> True M.PostponedTypeCheckingProblem{} -> False metaHelperType :: Rewrite -> InteractionId -> Range -> String -> TCM (OutputConstraint' Expr Expr) metaHelperType norm ii rng s = case words s of [] -> failure f : _ -> do ensureName f A.Application h args <- A.appView . getBody . deepUnscope <$> parseExprIn ii rng ("let " ++ f ++ " = _ in " ++ s) withInteractionId ii $ do cxtArgs <- getContextArgs -- cleanupType relies on with arguments being named 'w', -- so we'd better rename any actual 'w's to avoid confusion. tel <- runIdentity . onNamesTel unW <$> getContextTelescope a <- runIdentity . onNames unW . (`piApply` cxtArgs) <$> (getMetaType =<< lookupInteractionId ii) (vs, as) <- unzip <$> mapM (inferExpr . namedThing . unArg) args -- Remember the arity of a TelV atel _ <- telView a let arity = size atel (delta1, delta2, _, a', as', vs') = splitTelForWith tel a (map OtherType as) vs a <- local (\e -> e { envPrintDomainFreePi = True }) $ do reify =<< cleanupType arity args =<< normalForm norm =<< fst <$> withFunctionType delta1 vs' as' delta2 a' reportSDoc "interaction.helper" 10 $ TP.vcat [ TP.text "generating helper function" , TP.nest 2 $ TP.text "tel = " TP.<+> inTopContext (prettyTCM tel) , TP.nest 2 $ TP.text "a = " TP.<+> prettyTCM a , TP.nest 2 $ TP.text "vs = " TP.<+> prettyTCM vs , TP.nest 2 $ TP.text "as = " TP.<+> prettyTCM as , TP.nest 2 $ TP.text "delta1 = " TP.<+> inTopContext (prettyTCM delta1) , TP.nest 2 $ TP.text "delta2 = " TP.<+> inTopContext (addContext delta1 $ prettyTCM delta2) , TP.nest 2 $ TP.text "a' = " TP.<+> inTopContext (addContext delta1 $ addContext delta2 $ prettyTCM a') , TP.nest 2 $ TP.text "as' = " TP.<+> inTopContext (addContext delta1 $ prettyTCM as') , TP.nest 2 $ TP.text "vs' = " TP.<+> inTopContext (addContext delta1 $ prettyTCM vs') ] return (OfType' h a) where failure = typeError $ GenericError $ "Expected an argument of the form f e1 e2 .. en" ensureName f = do ce <- parseExpr rng f case ce of C.Ident{} -> return () C.RawApp _ [C.Ident{}] -> return () _ -> do reportSLn "interaction.helper" 10 $ "ce = " ++ show ce failure cleanupType arity args t = do -- Get the arity of t TelV ttel _ <- telView t -- Compute the number or pi-types subject to stripping. let n = size ttel - arity -- It cannot be negative, otherwise we would have performed a -- negative number of with-abstractions. unless (n >= 0) __IMPOSSIBLE__ return $ evalState (renameVars $ hiding args $ stripUnused n t) args getBody (A.Let _ _ e) = e getBody _ = __IMPOSSIBLE__ -- Strip the non-dependent abstractions from the first n abstractions. stripUnused n (El s v) = El s $ strip n v strip 0 v = v strip n v = case v of I.Pi a b -> case stripUnused (n-1) <$> b of b | absName b == "w" -> I.Pi a b NoAbs _ b -> unEl b Abs s b | 0 `freeIn` b -> I.Pi (hide a) (Abs s b) | otherwise -> strengthen __IMPOSSIBLE__ (unEl b) _ -> v -- todo: handle if goal type is a Pi -- renameVars = onNames (stringToArgName <.> renameVar . argNameToString) renameVars = onNames renameVar hiding args (El s v) = El s $ hidingTm args v hidingTm (arg:args) (I.Pi a b) | absName b == "w" = I.Pi (setHiding (getHiding arg) a) (hiding args <$> b) hidingTm args (I.Pi a b) = I.Pi a (hiding args <$> b) hidingTm _ a = a -- onNames :: Applicative m => (ArgName -> m ArgName) -> Type -> m Type onNames :: Applicative m => (String -> m String) -> Type -> m Type onNames f (El s v) = El s <$> onNamesTm f v -- onNamesTel :: Applicative f => (ArgName -> f ArgName) -> I.Telescope -> f I.Telescope onNamesTel :: Applicative f => (String -> f String) -> I.Telescope -> f I.Telescope onNamesTel f I.EmptyTel = pure I.EmptyTel onNamesTel f (I.ExtendTel a b) = I.ExtendTel <$> traverse (onNames f) a <*> onNamesAbs f onNamesTel b onNamesTm f v = case v of I.Var x es -> I.Var x <$> onNamesElims f es I.Def q es -> I.Def q <$> onNamesElims f es I.Con c ci args -> I.Con c ci <$> onNamesArgs f args I.Lam i b -> I.Lam i <$> onNamesAbs f onNamesTm b I.Pi a b -> I.Pi <$> traverse (onNames f) a <*> onNamesAbs f onNames b I.DontCare v -> I.DontCare <$> onNamesTm f v I.Lit{} -> pure v I.Sort{} -> pure v I.Level{} -> pure v I.MetaV{} -> pure v I.Shared{} -> pure v onNamesElims f = traverse $ traverse $ onNamesTm f onNamesArgs f = traverse $ traverse $ onNamesTm f onNamesAbs f = onNamesAbs' f (stringToArgName <.> f . argNameToString) onNamesAbs' f f' nd (Abs s x) = Abs <$> f' s <*> nd f x onNamesAbs' f f' nd (NoAbs s x) = NoAbs <$> f' s <*> nd f x unW "w" = return ".w" unW s = return s renameVar ('.':s) = pure s renameVar "w" = betterName renameVar s = pure s betterName = do arg : args <- get put args return $ case arg of Arg _ (Named _ (A.Var x)) -> show $ A.nameConcrete x Arg _ (Named (Just x) _) -> argNameToString $ rangedThing x _ -> "w" -- Gives a list of names and corresponding types. contextOfMeta :: InteractionId -> Rewrite -> TCM [OutputConstraint' Expr Name] contextOfMeta ii norm = do info <- getMetaInfo <$> (lookupMeta =<< lookupInteractionId ii) withMetaInfo info $ do cxt <- getContext let n = length cxt localVars = zipWith raise [1..] cxt mkLet (x, lb) = do (tm, Dom c ty) <- getOpen lb return $ Dom c (x, ty) letVars <- mapM mkLet . Map.toDescList =<< asks envLetBindings gfilter visible . reverse <$> mapM out (letVars ++ localVars) where gfilter p = catMaybes . map p visible (OfType x y) | not (isNoName x) = Just (OfType' x y) | otherwise = Nothing visible _ = __IMPOSSIBLE__ out (Dom _ (x, t)) = do t' <- reify =<< normalForm norm t return $ OfType x t' -- | Returns the type of the expression in the current environment -- We wake up irrelevant variables just in case the user want to -- invoke that command in an irrelevant context. typeInCurrent :: Rewrite -> Expr -> TCM Expr typeInCurrent norm e = do (_,t) <- wakeIrrelevantVars $ inferExpr e v <- normalForm norm t reify v typeInMeta :: InteractionId -> Rewrite -> Expr -> TCM Expr typeInMeta ii norm e = do m <- lookupInteractionId ii mi <- getMetaInfo <$> lookupMeta m withMetaInfo mi $ typeInCurrent norm e withInteractionId :: InteractionId -> TCM a -> TCM a withInteractionId i ret = do m <- lookupInteractionId i withMetaId m ret withMetaId :: MetaId -> TCM a -> TCM a withMetaId m ret = do mv <- lookupMeta m withMetaInfo' mv ret -- The intro tactic -- Returns the terms (as strings) that can be -- used to refine the goal. Uses the coverage checker -- to find out which constructors are possible. introTactic :: Bool -> InteractionId -> TCM [String] introTactic pmLambda ii = do mi <- lookupInteractionId ii mv <- lookupMeta mi withMetaInfo (getMetaInfo mv) $ case mvJudgement mv of HasType _ t -> do t <- reduce =<< piApplyM t =<< getContextArgs -- Andreas, 2013-03-05 Issue 810: skip hidden domains in introduction -- of constructor. TelV tel' t <- telViewUpTo' (-1) notVisible t -- if we cannot introduce a constructor, we try a lambda let fallback = do TelV tel _ <- telView t reportSDoc "interaction.intro" 20 $ TP.sep [ TP.text "introTactic/fallback" , TP.text "tel' = " TP.<+> prettyTCM tel' , TP.text "tel = " TP.<+> prettyTCM tel ] case (tel', tel) of (EmptyTel, EmptyTel) -> return [] _ -> introFun (telToList tel' ++ telToList tel) case ignoreSharing $ unEl t of I.Def d _ -> do def <- getConstInfo d case theDef def of Datatype{} -> addContext tel' $ introData t Record{ recNamedCon = name } | name -> addContext tel' $ introData t | otherwise -> addContext tel' $ introRec d _ -> fallback _ -> fallback `catchError` \_ -> return [] _ -> __IMPOSSIBLE__ where conName [p] = [ c | I.ConP c _ _ <- [namedArg p] ] conName _ = __IMPOSSIBLE__ showTCM v = show <$> prettyTCM v introFun tel = addContext tel' $ do reportSDoc "interaction.intro" 10 $ do TP.text "introFun" TP.<+> prettyTCM (telFromList tel) imp <- showImplicitArguments let okHiding0 h = imp || h == NotHidden -- if none of the vars were displayed, we would get a parse error -- thus, we switch to displaying all allHidden = null (filter okHiding0 hs) okHiding = if allHidden then const True else okHiding0 vars <- -- setShowImplicitArguments (imp || allHidden) $ (if allHidden then withShowAllArguments else id) $ mapM showTCM [ setHiding h $ defaultArg $ var i :: Arg Term | (h, i) <- zip hs $ downFrom n , okHiding h ] if pmLambda then return [ unwords $ ["λ", "{"] ++ vars ++ ["→", "?", "}"] ] else return [ unwords $ ["λ"] ++ vars ++ ["→", "?"] ] where n = size tel hs = map getHiding tel tel' = telFromList [ fmap makeName b | b <- tel ] makeName ("_", t) = ("x", t) makeName (x, t) = (x, t) introData t = do let tel = telFromList [defaultDom ("_", t)] pat = [defaultArg $ unnamed $ debruijnNamedVar "c" 0] r <- splitLast CoInductive tel pat case r of Left err -> return [] Right cov -> mapM showTCM $ concatMap (conName . scPats) $ splitClauses cov introRec :: QName -> TCM [String] introRec d = do hfs <- getRecordFieldNames d fs <- ifM showImplicitArguments (return $ map unArg hfs) (return [ unArg a | a <- hfs, visible a ]) let e = C.Rec noRange $ for fs $ \ f -> Left $ C.FieldAssignment f $ C.QuestionMark noRange Nothing return [ prettyShow e ] -- | Runs the given computation as if in an anonymous goal at the end -- of the top-level module. -- -- Sets up current module, scope, and context. atTopLevel :: TCM a -> TCM a atTopLevel m = inConcreteMode $ do let err = typeError $ GenericError "The file has not been loaded yet." caseMaybeM (use stCurrentModule) err $ \ current -> do caseMaybeM (getVisitedModule $ toTopLevelModuleName current) __IMPOSSIBLE__ $ \ mi -> do let scope = iInsideScope $ miInterface mi tel <- lookupSection current -- Get the names of the local variables from @scope@ -- and put them into the context. -- -- Andreas, 2017-04-24, issue #2552: -- -- Delete the let-bound ones, since they are not represented -- in the module telescope. -- -- This is a temporary fix until a better solution is available, -- e.g., when the module telescope represents let-bound variables. -- -- Unfortunately, referring to let-bound variables -- from the top level module telescope will for now result in a not-in-scope error. let names :: [A.Name] names = map localVar $ filter ((False ==) . localLetBound) $ map snd $ reverse $ scopeLocals scope -- Andreas, 2016-12-31, issue #2371 -- The following is an unnecessary complication, as shadowed locals -- are not in scope anyway (they are ambiguous). -- -- Replace the shadowed names by fresh names (such that they do not shadow imports) -- let mnames :: [Maybe A.Name] -- mnames = map (notShadowedLocal . snd) $ reverse $ scopeLocals scope -- names <- mapM (maybe freshNoName_ return) mnames let types :: [Dom I.Type] types = map (snd <$>) $ telToList tel gamma :: ListTel' A.Name gamma = fromMaybe __IMPOSSIBLE__ $ zipWith' (\ x dom -> (x,) <$> dom) names types reportSDoc "interaction.top" 20 $ TP.vcat [ TP.text "BasicOps.atTopLevel" , TP.text " names = " TP.<+> TP.sep (map prettyA names) , TP.text " types = " TP.<+> TP.sep (map prettyTCM types) ] M.withCurrentModule current $ withScope_ scope $ addContext gamma $ m -- | Parse a name. parseName :: Range -> String -> TCM C.QName parseName r s = do m <- parseExpr r s case m of C.Ident m -> return m C.RawApp _ [C.Ident m] -> return m _ -> typeError $ GenericError $ "Not an identifier: " ++ show m ++ "." -- | Check whether an expression is a (qualified) identifier. isQName :: C.Expr -> Maybe C.QName isQName m = do case m of C.Ident m -> return m C.RawApp _ [C.Ident m] -> return m _ -> Nothing -- | Returns the contents of the given module or record. moduleContents :: Rewrite -- ^ How should the types be presented? -> Range -- ^ The range of the next argument. -> String -- ^ The module name. -> TCM ([C.Name], [(C.Name, Type)]) -- ^ Module names, names paired up with corresponding types. moduleContents norm rng s = traceCall ModuleContents $ do e <- parseExpr rng s case isQName e of -- If the expression is not a single identifier, it is not a module name -- and treated as a record expression. Nothing -> getRecordContents norm e -- Otherwise, if it is not in scope as a module name, it is treated -- as a record name. Just x -> do ms :: [AbstractModule] <- scopeLookup x <$> getScope if null ms then getRecordContents norm e else getModuleContents norm x -- | Returns the contents of the given record identifier. getRecordContents :: Rewrite -- ^ Amount of normalization in types. -> C.Expr -- ^ Expression presumably of record type. -> TCM ([C.Name], [(C.Name, Type)]) -- ^ Module names, names paired up with corresponding types. getRecordContents norm ce = do e <- toAbstract ce (_, t) <- inferExpr e let notRecordType = typeError $ ShouldBeRecordType t (q, vs, defn) <- fromMaybeM notRecordType $ isRecordType t case defn of Record{ recFields = fs, recTel = tel } -> do let xs = map (nameConcrete . qnameName . unArg) fs doms = telToList $ apply tel vs ts <- mapM (normalForm norm) $ map (snd . unDom) doms return ([], zip xs ts) _ -> __IMPOSSIBLE__ -- | Returns the contents of the given module. getModuleContents :: Rewrite -- ^ Amount of normalization in types. -> C.QName -- ^ Module name. -> TCM ([C.Name], [(C.Name, Type)]) -- ^ Module names, names paired up with corresponding types. getModuleContents norm m = do modScope <- getNamedScope . amodName =<< resolveModule m let modules :: ThingsInScope AbstractModule modules = exportedNamesInScope modScope names :: ThingsInScope AbstractName names = exportedNamesInScope modScope xns = [ (x,n) | (x, ns) <- Map.toList names, n <- ns ] types <- forM xns $ \(x, n) -> do d <- getConstInfo $ anameName n t <- normalForm norm =<< (defType <$> instantiateDef d) return (x, t) return (Map.keys modules, types) whyInScope :: String -> TCM (Maybe LocalVar, [AbstractName], [AbstractModule]) whyInScope s = do x <- parseName noRange s scope <- getScope return ( lookup x $ map (first C.QName) $ scopeLocals scope , scopeLookup x scope , scopeLookup x scope ) Agda-2.5.3/src/full/Agda/Interaction/EmacsCommand.hs0000644000000000000000000000616713154613124020304 0ustar0000000000000000 ------------------------------------------------------------------------ -- | Code for instructing Emacs to do things ------------------------------------------------------------------------ module Agda.Interaction.EmacsCommand ( Lisp(..) , response , putResponse , display_info' , display_warning , clearRunningInfo , clearWarning , displayRunningInfo ) where import qualified Data.List as List import Agda.Utils.Pretty import Agda.Utils.String -- | Simple Emacs Lisp expressions. data Lisp a = A a -- ^ Atom. | Cons (Lisp a) (Lisp a) -- Cons cell. | L [Lisp a] -- ^ List. | Q (Lisp a) -- Quoted expression. instance Pretty a => Pretty (Lisp a) where pretty (A a ) = pretty a pretty (Cons a b) = parens (pretty a <+> text "." <+> pretty b) pretty (L xs) = parens (hsep (map pretty xs)) pretty (Q x) = text "'" <> pretty x instance Show (Lisp String) where showsPrec _ (A a) = showString a showsPrec p (Cons a b) = showString "(" . showsPrec p a . showString " . " . showsPrec p b . showString ")" showsPrec p (L xs) = showString "(" . foldr (.) (showString ")") (List.intersperse (showString " ") (map (showsPrec p) xs)) showsPrec p (Q x) = showString "'" . showsPrec p x -- | Formats a response command. -- -- Replaces @'\n'@ with spaces to ensure that each command is a -- single line. response :: Lisp String -> String response = (++ "\n") . map replaceNewLines . show . pretty where replaceNewLines '\n' = ' ' replaceNewLines c = c -- | Writes a response command to standard output. putResponse :: Lisp String -> IO () putResponse = putStr . response -- | @displayInBuffer buffername append header content@ displays @content@ -- (with header @header@) in some suitable way in the buffer @buffername@. -- If @append@ is @True@, then the content is appended to previous content -- (if any), otherwise any previous content is deleted. displayInBuffer :: String -> Bool -> String -> String -> Lisp String displayInBuffer buffername append header content = L [ A buffername , A (quote header) , A (quote content) , A (if append then "t" else "nil") ] display_info' :: Bool -> String -> String -> Lisp String display_info' = displayInBuffer "agda2-info-action" display_warning :: String -> String -> Lisp String display_warning = displayInBuffer "agda2-warning-action" False ------------------------------------------------------------------------ -- Running info -- | The name of the running info buffer. runningInfoBufferName :: String runningInfoBufferName = "*Type-checking*" -- | Clear the running info buffer. clearRunningInfo :: Lisp String clearRunningInfo = display_info' False runningInfoBufferName "" -- | Clear the warning buffer clearWarning :: Lisp String clearWarning = L [ A "agda2-close-warning" ] -- | Display running information about what the type-checker is up to. displayRunningInfo :: String -> Lisp String displayRunningInfo s = display_info' True runningInfoBufferName s Agda-2.5.3/src/full/Agda/Interaction/SearchAbout.hs0000644000000000000000000000547513154613124020156 0ustar0000000000000000module Agda.Interaction.SearchAbout (findMentions) where import Control.Applicative import Control.Monad import qualified Data.Map as Map import qualified Data.Set as Set import Data.List (isInfixOf) import Data.Either (partitionEithers) import Agda.Syntax.Position (Range) import Agda.Syntax.Scope.Base import Agda.Syntax.Scope.Monad import Agda.TypeChecking.Monad.Signature import Agda.TypeChecking.Monad.Env import Agda.Syntax.Internal.Names (namesIn) import Agda.Interaction.BasicOps (normalForm, Rewrite, parseName) import qualified Agda.Syntax.Concrete as C import qualified Agda.Syntax.Abstract as A import qualified Agda.Syntax.Internal as I import Agda.Utils.Pretty ( prettyShow ) findMentions :: Rewrite -> Range -> String -> ScopeM [(C.Name, I.Type)] findMentions norm rg nm = do -- We start by dealing with the user's input -- The users passes in `nm`, a list of identifiers and strings -- to match against definitions in scope. `findMentions` will -- select all of the definitions such that: -- - all of the specified identifiers appear in their type -- (which has been normalised according to `norm`) -- - all of the specified strings are substrings of their name -- We separate the strings from the names by a rough analysis -- and then parse and resolve the names in the current scope let (userSubStrings, nms) = partitionEithers $ isString <$> words nm rnms <- mapM (parseName rg >=> resolveName) nms let userIdentifiers = fmap (fmap anameName . anames) rnms -- We then collect all the things in scope, by name. -- Issue #2381: We explicitly filter out pattern synonyms because they -- don't have a type. Looking it up makes Agda panic! snms <- fmap (nsNames . allThingsInScope) $ currentModule >>= getNamedScope let namesInScope = filter ((PatternSynName /=) . anameKind . snd) $ concatMap (uncurry $ map . (,)) $ Map.toList snms -- Once we have the user-provided names and the names of all the -- thing in scope we can start the search: for each name in scope, -- we grab its type, normalise it according to `norm` and collect -- the identifiers in it. We then check whether it meets the user's -- criteria. ress <- forM namesInScope $ \ (x, n) -> do t <- normalForm norm =<< typeOfConst (anameName n) let namesInT = Set.toList $ namesIn t let defName = prettyShow x return $ do guard $ all (`isInfixOf` defName) userSubStrings guard $ all (any (`elem` namesInT)) userIdentifiers return (x, t) return $ concat ress where isString str | not (null str) && head str == '"' && last str == '"' = Left $ filter (/= '"') str | otherwise = Right str anames (DefinedName _ an) = [an] anames (FieldName ans) = ans anames (ConstructorName ans) = ans anames _ = [] Agda-2.5.3/src/full/Agda/Interaction/FindFile.hs-boot0000644000000000000000000000057213154613124020370 0ustar0000000000000000module Agda.Interaction.FindFile where import Agda.Syntax.Concrete.Name (TopLevelModuleName) import Agda.TypeChecking.Monad.Base (TCM) import Agda.Utils.FileName (AbsolutePath) moduleName :: AbsolutePath -> TCM TopLevelModuleName moduleName' :: AbsolutePath -> TCM TopLevelModuleName checkModuleName :: TopLevelModuleName -> AbsolutePath -> Maybe TopLevelModuleName -> TCM () Agda-2.5.3/src/full/Agda/Interaction/FindFile.hs0000644000000000000000000001742013154613124017427 0ustar0000000000000000{-# LANGUAGE CPP #-} ------------------------------------------------------------------------ -- | Functions which map between module names and file names. -- -- Note that file name lookups are cached in the 'TCState'. The code -- assumes that no Agda source files are added or removed from the -- include directories while the code is being type checked. ------------------------------------------------------------------------ module Agda.Interaction.FindFile ( toIFile , FindError(..), findErrorToTypeError , findFile, findFile', findFile'' , findInterfaceFile , checkModuleName , moduleName', moduleName , rootNameModule , replaceModuleExtension ) where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad import Control.Monad.Trans import qualified Data.List as List import Data.Maybe (catMaybes) import qualified Data.Map as Map import System.FilePath import Agda.Syntax.Common import Agda.Syntax.Concrete import Agda.Syntax.Parser import Agda.Syntax.Parser.Literate (literateExts, literateExtsShortList) import Agda.Syntax.Position import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Trace import Agda.TypeChecking.Monad.Benchmark (billTo) import qualified Agda.TypeChecking.Monad.Benchmark as Bench import {-# SOURCE #-} Agda.TypeChecking.Monad.Options (getIncludeDirs) import Agda.TypeChecking.Warnings (runPM) import Agda.Utils.Except import Agda.Utils.FileName import Agda.Utils.Lens import Agda.Utils.Null #include "undefined.h" import Agda.Utils.Impossible -- | Converts an Agda file name to the corresponding interface file -- name. toIFile :: AbsolutePath -> AbsolutePath toIFile = replaceModuleExtension ".agdai" replaceModuleExtension :: String -> AbsolutePath -> AbsolutePath replaceModuleExtension ext@('.':_) = mkAbsolute . (++ ext) . dropAgdaExtension . filePath replaceModuleExtension ext = replaceModuleExtension ('.':ext) -- | Errors which can arise when trying to find a source file. -- -- Invariant: All paths are absolute. data FindError = NotFound [AbsolutePath] -- ^ The file was not found. It should have had one of the given -- file names. | Ambiguous [AbsolutePath] -- ^ Several matching files were found. -- -- Invariant: The list of matching files has at least two -- elements. -- | Given the module name which the error applies to this function -- converts a 'FindError' to a 'TypeError'. findErrorToTypeError :: TopLevelModuleName -> FindError -> TypeError findErrorToTypeError m (NotFound files) = FileNotFound m files findErrorToTypeError m (Ambiguous files) = AmbiguousTopLevelModuleName m files -- | Finds the source file corresponding to a given top-level module -- name. The returned paths are absolute. -- -- Raises an error if the file cannot be found. findFile :: TopLevelModuleName -> TCM AbsolutePath findFile m = do mf <- findFile' m case mf of Left err -> typeError $ findErrorToTypeError m err Right f -> return f -- | Tries to find the source file corresponding to a given top-level -- module name. The returned paths are absolute. -- -- SIDE EFFECT: Updates 'stModuleToSource'. findFile' :: TopLevelModuleName -> TCM (Either FindError AbsolutePath) findFile' m = do dirs <- getIncludeDirs modFile <- use stModuleToSource (r, modFile) <- liftIO $ findFile'' dirs m modFile stModuleToSource .= modFile return r -- | A variant of 'findFile'' which does not require 'TCM'. findFile'' :: [AbsolutePath] -- ^ Include paths. -> TopLevelModuleName -> ModuleToSource -- ^ Cached invocations of 'findFile'''. An updated copy is returned. -> IO (Either FindError AbsolutePath, ModuleToSource) findFile'' dirs m modFile = case Map.lookup m modFile of Just f -> return (Right f, modFile) Nothing -> do files <- fileList sourceFileExts filesShortList <- fileList sourceFileExtsShortList existingFiles <- liftIO $ filterM (doesFileExistCaseSensitive . filePath) files return $ case List.nub existingFiles of [] -> (Left (NotFound filesShortList), modFile) [file] -> (Right file, Map.insert m file modFile) files -> (Left (Ambiguous existingFiles), modFile) where fileList exts = mapM absolute [ filePath dir file | dir <- dirs , file <- map (moduleNameToFileName m) exts ] -- | Finds the interface file corresponding to a given top-level -- module name. The returned paths are absolute. -- -- Raises an error if the source file cannot be found, and returns -- 'Nothing' if the source file can be found but not the interface -- file. findInterfaceFile :: TopLevelModuleName -> TCM (Maybe AbsolutePath) findInterfaceFile m = do f <- toIFile <$> findFile m ex <- liftIO $ doesFileExistCaseSensitive $ filePath f return $ if ex then Just f else Nothing -- | Ensures that the module name matches the file name. The file -- corresponding to the module name (according to the include path) -- has to be the same as the given file name. checkModuleName :: TopLevelModuleName -- ^ The name of the module. -> AbsolutePath -- ^ The file from which it was loaded. -> Maybe TopLevelModuleName -- ^ The expected name, coming from an import statement. -> TCM () checkModuleName name file mexpected = do findFile' name >>= \case Left (NotFound files) -> typeError $ case mexpected of Nothing -> ModuleNameDoesntMatchFileName name files Just expected -> ModuleNameUnexpected name expected Left (Ambiguous files) -> typeError $ AmbiguousTopLevelModuleName name files Right file' -> do file <- liftIO $ absolute (filePath file) if file === file' then return () else typeError $ ModuleDefinedInOtherFile name file file' -- | Computes the module name of the top-level module in the given file. -- -- Warning! Parses the whole file to get the module name out. -- Use wisely! -- -- No side effects! Only in 'TCM' to raise errors. moduleName' :: AbsolutePath -> TCM TopLevelModuleName moduleName' file = billTo [Bench.ModuleName] $ do q <- runPM (parseFile' moduleParser file) let name = topLevelModuleName q if moduleNameParts name == ["_"] then do q <- runPM (parse moduleNameParser defaultName) `catchError` \_ -> typeError $ GenericError $ "File name " ++ show file ++ " is invalid as it does not correspond to a valid module name." return $ TopLevelModuleName (getRange q) [defaultName] else return name where defaultName = rootNameModule file sourceFileExts :: [String] sourceFileExts = [".agda"] ++ literateExts sourceFileExtsShortList :: [String] sourceFileExtsShortList = [".agda"] ++ literateExtsShortList dropAgdaExtension :: String -> String dropAgdaExtension s = case catMaybes [ stripExtension ext s | ext <- sourceFileExts ] of [name] -> name _ -> __IMPOSSIBLE__ where stripExtension :: String -> String -> Maybe String stripExtension e = fmap reverse . List.stripPrefix (reverse e) . reverse rootNameModule :: AbsolutePath -> String rootNameModule = dropAgdaExtension . snd . splitFileName . filePath -- | A variant of 'moduleName'' which raises an error if the file name -- does not match the module name. -- -- The file name is interpreted relative to the current working -- directory (unless it is absolute). moduleName :: AbsolutePath -> TCM TopLevelModuleName moduleName file = do m <- moduleName' file let r = getRange m -- Andreas, 2016-07-11, issue 2092 -- The error range should be set to the file with the wrong module name -- not the importing one (which would be the default). (if null r then id else traceCall (SetRange r)) $ checkModuleName m file Nothing return m Agda-2.5.3/src/full/Agda/Interaction/Response.hs0000644000000000000000000001155113154613124017544 0ustar0000000000000000{-# LANGUAGE CPP #-} ------------------------------------------------------------------------ -- | Data type for all interactive responses ------------------------------------------------------------------------ module Agda.Interaction.Response ( Response (..) , MakeCaseVariant (..) , DisplayInfo (..) , Status (..) , GiveResult (..) , InteractionOutputCallback , defaultInteractionOutputCallback ) where import Agda.Interaction.Highlighting.Precise import {-# SOURCE #-} Agda.TypeChecking.Monad.Base import Agda.Syntax.Common (InteractionId(..)) import Agda.Syntax.Concrete (Expr) import Agda.Utils.Pretty import Control.Monad.Trans import Data.Int import System.IO #include "undefined.h" import Agda.Utils.Impossible -- | Responses for any interactive interface -- -- Note that the response is given in pieces and incrementally, -- so the user can have timely response even during long computations. data Response = Resp_HighlightingInfo HighlightingInfo HighlightingMethod ModuleToSource | Resp_Status Status | Resp_JumpToError FilePath Int32 | Resp_InteractionPoints [InteractionId] | Resp_GiveAction InteractionId GiveResult | Resp_MakeCase MakeCaseVariant [String] | Resp_SolveAll [(InteractionId, Expr)] -- ^ Solution for one or more meta-variables. | Resp_DisplayInfo DisplayInfo | Resp_RunningInfo Int String -- ^ The integer is the message's debug level. | Resp_ClearRunningInfo | Resp_ClearHighlighting | Resp_DoneAborting -- ^ A command sent when an abort command has completed -- successfully. -- | There are two kinds of \"make case\" commands. data MakeCaseVariant = Function | ExtendedLambda -- | Info to display at the end of an interactive command data DisplayInfo = Info_CompilationOk String String -- ^ Strings are the warnings and the (non-fatal) errors | Info_Constraints String | Info_AllGoalsWarnings String String String -- ^ Strings are the goals, the warnings and the (non-fatal) errors | Info_Time Doc | Info_Error String -- ^ When an error message is displayed this constructor should be -- used, if appropriate. -- | Info_Warning String --FNF: currently unused | Info_Intro Doc -- ^ 'Info_Intro' denotes two different types of errors -- TODO: split these into separate constructors | Info_Auto String -- ^ 'Info_Auto' denotes either an error or a success (when 'Resp_GiveAction' is present) -- TODO: split these into separate constructors | Info_ModuleContents Doc | Info_SearchAbout Doc | Info_WhyInScope Doc | Info_NormalForm Doc | Info_GoalType Doc | Info_CurrentGoal Doc | Info_InferredType Doc | Info_Context Doc | Info_HelperFunction Doc | Info_Version deriving Show -- | Status information. data Status = Status { sShowImplicitArguments :: Bool -- ^ Are implicit arguments displayed? , sChecked :: Bool -- ^ Has the module been successfully type checked? } -- | Give action result -- -- Comment derived from agda2-mode.el -- -- If 'GiveResult' is 'Give_String s', then the goal is replaced by 's', -- and otherwise the text inside the goal is retained (parenthesised -- if 'GiveResult' is 'Give_Paren'). data GiveResult = Give_String String | Give_Paren | Give_NoParen -- | Callback fuction to call when there is a response -- to give to the interactive frontend. -- -- Note that the response is given in pieces and incrementally, -- so the user can have timely response even during long computations. -- -- Typical 'InteractionOutputCallback' functions: -- -- * Convert the response into a 'String' representation and -- print it on standard output -- (suitable for inter-process communication). -- -- * Put the response into a mutable variable stored in the -- closure of the 'InteractionOutputCallback' function. -- (suitable for intra-process communication). type InteractionOutputCallback = Response -> IO () -- | The default 'InteractionOutputCallback' function prints certain -- things to stdout (other things generate internal errors). defaultInteractionOutputCallback :: InteractionOutputCallback defaultInteractionOutputCallback r = case r of Resp_HighlightingInfo {} -> __IMPOSSIBLE__ Resp_Status {} -> __IMPOSSIBLE__ Resp_JumpToError {} -> __IMPOSSIBLE__ Resp_InteractionPoints {} -> __IMPOSSIBLE__ Resp_GiveAction {} -> __IMPOSSIBLE__ Resp_MakeCase {} -> __IMPOSSIBLE__ Resp_SolveAll {} -> __IMPOSSIBLE__ Resp_DisplayInfo {} -> __IMPOSSIBLE__ Resp_RunningInfo _ s -> liftIO $ do putStr s hFlush stdout Resp_ClearRunningInfo {} -> __IMPOSSIBLE__ Resp_ClearHighlighting {} -> __IMPOSSIBLE__ Resp_DoneAborting {} -> __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/Interaction/Monad.hs0000644000000000000000000000046013154613124017001 0ustar0000000000000000module Agda.Interaction.Monad (IM, runIM, readline) where import Control.Monad.Trans import System.Console.Haskeline import Agda.TypeChecking.Monad -- | Line reader. The line reader history is not stored between -- sessions. readline :: String -> IM (Maybe String) readline s = lift (getInputLine s) Agda-2.5.3/src/full/Agda/Interaction/Library.hs0000644000000000000000000003416613154613124017361 0ustar0000000000000000-- | Library management. -- -- Sample use: -- -- @ -- -- Get libraries as listed in @.agda/libraries@ file. -- libs <- getInstalledLibraries Nothing -- -- -- Get the libraries (and immediate paths) relevant for @projectRoot@. -- -- This involves locating and processing the @.agda-lib@ file for the project. -- (libNames, includePaths) <- getDefaultLibraries projectRoot True -- -- -- Get include paths of depended-on libraries. -- resolvedPaths <- libraryIncludePaths Nothing libs libNames -- -- let allPaths = includePaths ++ resolvedPaths -- @ -- module Agda.Interaction.Library ( getDefaultLibraries , getInstalledLibraries , libraryIncludePaths , LibName , LibM -- * Exported for testing , VersionView(..), versionView, unVersionView , findLib' ) where import Control.Arrow (first, second) import Control.Applicative import Control.Exception import Control.Monad.Writer import Data.Char import Data.Either import Data.Function import qualified Data.List as List import Data.Maybe import System.Directory ( getAppUserDataDirectory ) import System.Directory import System.FilePath import System.Environment import Agda.Interaction.Library.Base import Agda.Interaction.Library.Parse import Agda.Utils.Environment import Agda.Utils.Except ( ExceptT, runExceptT, MonadError(throwError) ) import Agda.Utils.IO ( catchIO ) import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Pretty import Agda.Utils.String ( trim, ltrim ) import Agda.Version ------------------------------------------------------------------------ -- Types and Monads ------------------------------------------------------------------------ -- | Library names are structured into the base name and a suffix of version -- numbers, e.g. @mylib-1.2.3@. The version suffix is optional. data VersionView = VersionView { vvBase :: LibName -- ^ Actual library name. , vvNumbers :: [Integer] -- ^ Major version, minor version, subminor version, etc., all non-negative. -- Note: a priori, there is no reason why the version numbers should be @Int@s. } deriving (Eq, Show) -- | Collected errors while processing library files. -- data LibError = LibNotFound FilePath LibName -- ^ Raised when a library name could no successfully be resolved -- to an @.agda-lib@ file. | AmbiguousLib LibName [AgdaLibFile] -- ^ Raised when a library name is defined in several @.agda-lib files@. | OtherError String -- ^ Generic error. deriving (Show) -- | Collects 'LibError's. -- type LibErrorIO = WriterT [LibError] IO -- | Throws 'Doc' exceptions. type LibM = ExceptT Doc IO -- | Raise collected 'LibErrors' as exception. -- mkLibM :: [AgdaLibFile] -> LibErrorIO a -> LibM a mkLibM libs m = do (x, err) <- lift $ runWriterT m case err of [] -> return x _ -> throwError =<< do lift $ vcat <$> mapM (formatLibError libs) err ------------------------------------------------------------------------ -- Resources ------------------------------------------------------------------------ -- | Get the path to @~/.agda@ (system-specific). -- Can be overwritten by the @AGDA_DIR@ environment variable. -- -- (This is not to be confused with the directory for the data files -- that Agda needs (e.g. the primitive modules).) -- getAgdaAppDir :: IO FilePath getAgdaAppDir = do -- System-specific command to build the path to ~/.agda (Unix) or %APPDATA%\agda (Win) let agdaDir = getAppUserDataDirectory "agda" -- The default can be overwritten by setting the AGDA_DIR environment variable caseMaybeM (lookupEnv "AGDA_DIR") agdaDir $ \ dir -> ifM (doesDirectoryExist dir) (canonicalizePath dir) $ do d <- agdaDir putStrLn $ "Warning: Environment variable AGDA_DIR points to non-existing directory " ++ show dir ++ ", using " ++ show d ++ " instead." return d -- | The @~/.agda/libraries@ file lists the libraries Agda should know about. -- The content of @libraries@ is is a list of pathes to @.agda-lib@ files. -- -- Agda honors also version specific @libraries@ files, e.g. @libraries-2.6.0@. -- -- @defaultLibraryFiles@ gives a list of all @libraries@ files Agda should process -- by default. -- defaultLibraryFiles :: [FilePath] defaultLibraryFiles = ["libraries-" ++ version, "libraries"] -- | The @defaultsFile@ contains a list of library names relevant for each Agda project. -- defaultsFile :: FilePath defaultsFile = "defaults" ------------------------------------------------------------------------ -- * Get the libraries for the current project ------------------------------------------------------------------------ -- | Get pathes of @.agda-lib@ files in given project root. -- -- If there are none, look in the parent directories until one is found. -- findAgdaLibFiles :: FilePath -- ^ Project root. -> IO [FilePath] -- ^ Pathes of @.agda-lib@ files for this project (if any). findAgdaLibFiles root = do libs <- map (root ) . filter ((== ".agda-lib") . takeExtension) <$> getDirectoryContents root case libs of [] -> do up <- canonicalizePath $ root ".." if up == root then return [] else findAgdaLibFiles up files -> return files -- | Get dependencies and include paths for given project root: -- -- Look for @.agda-lib@ files according to 'findAgdaLibFiles'. -- If none are found, use default dependencies (according to @defaults@ file) -- and current directory (project root). -- getDefaultLibraries :: FilePath -- ^ Project root. -> Bool -- ^ Use @defaults@ if no @.agda-lib@ file exists for this project? -> LibM ([LibName], [FilePath]) -- ^ The returned @LibName@s are all non-empty strings. getDefaultLibraries root optDefaultLibs = mkLibM [] $ do libs <- lift $ findAgdaLibFiles root if null libs then (,[]) <$> if optDefaultLibs then (libNameForCurrentDir :) <$> readDefaultsFile else return [] else libsAndPaths <$> parseLibFiles Nothing (zip (repeat 0) libs) where libsAndPaths ls = (concatMap libDepends ls, concatMap libIncludes ls) -- | Return list of libraries to be used by default. -- -- None if the @defaults@ file does not exist. -- readDefaultsFile :: LibErrorIO [LibName] readDefaultsFile = do agdaDir <- lift $ getAgdaAppDir let file = agdaDir defaultsFile ifNotM (lift $ doesFileExist file) (return []) $ {-else-} do ls <- lift $ map snd . stripCommentLines <$> readFile file return $ concatMap splitCommas ls `catchIO` \ e -> do tell [ OtherError $ unlines ["Failed to read defaults file.", show e] ] return [] ------------------------------------------------------------------------ -- * Reading the installed libraries ------------------------------------------------------------------------ -- | Returns the path of the @libraries@ file which lists the libraries Agda knows about. -- -- Note: file may not exist. -- getLibrariesFile :: Maybe FilePath -- ^ Override the default @libraries@ file? -> IO FilePath getLibrariesFile (Just overrideLibFile) = return overrideLibFile getLibrariesFile Nothing = do agdaDir <- getAgdaAppDir let defaults = map (agdaDir ) defaultLibraryFiles -- NB: non-empty list files <- filterM doesFileExist defaults case files of file : _ -> return file [] -> return (last defaults) -- doesn't exist, but that's ok -- | Parse the descriptions of the libraries Agda knows about. -- -- Returns none if there is no @libraries@ file. -- getInstalledLibraries :: Maybe FilePath -- ^ Override the default @libraries@ file? -> LibM [AgdaLibFile] -- ^ Content of library files. (Might have empty @LibName@s.) getInstalledLibraries overrideLibFile = mkLibM [] $ do file <- lift $ getLibrariesFile overrideLibFile ifNotM (lift $ doesFileExist file) (return []) $ {-else-} do ls <- lift $ stripCommentLines <$> readFile file files <- lift $ sequence [ (i, ) <$> expandEnvironmentVariables s | (i, s) <- ls ] parseLibFiles (Just file) files `catchIO` \ e -> do tell [ OtherError $ unlines ["Failed to read installed libraries.", show e] ] return [] -- | Parse the given library files. -- parseLibFiles :: Maybe FilePath -- ^ Name of @libraries@ file for error reporting. -> [(LineNumber, FilePath)] -- ^ Library files paired with their line number in @libraries@. -> LibErrorIO [AgdaLibFile] -- ^ Content of library files. (Might have empty @LibName@s.) parseLibFiles libFile files = do rs <- lift $ mapM (parseLibFile . snd) files -- Format and raise the errors. let loc line | Just f <- libFile = f ++ ":" ++ show line ++ ": " | otherwise = "" tell [ OtherError $ pos ++ err | ((line, path), Left err) <- zip files rs , let pos = if List.isPrefixOf "Failed to read" err then loc line else path ++ ":" ++ (if all isDigit (take 1 err) then "" else " ") ] return $ rights rs -- | Remove trailing white space and line comments. -- stripCommentLines :: String -> [(LineNumber, String)] stripCommentLines = concatMap strip . zip [1..] . lines where strip (i, s) = [ (i, s') | not $ null s' ] where s' = trimLineComment s -- | Pretty-print 'LibError'. formatLibError :: [AgdaLibFile] -> LibError -> IO Doc formatLibError installed = \case LibNotFound file lib -> return $ vcat $ [ text $ "Library '" ++ lib ++ "' not found." , sep [ text "Add the path to its .agda-lib file to" , nest 2 $ text $ "'" ++ file ++ "'" , text "to install." ] , text "Installed libraries:" ] ++ map (nest 2) (if null installed then [text "(none)"] else [ sep [ text $ libName l, nest 2 $ parens $ text $ libFile l ] | l <- installed ]) AmbiguousLib lib tgts -> return $ vcat $ [ sep [ text $ "Ambiguous library '" ++ lib ++ "'.", text "Could refer to any one of" ] ] ++ [ nest 2 $ text (libName l) <+> parens (text $ libFile l) | l <- tgts ] OtherError err -> return $ text err ------------------------------------------------------------------------ -- * Resolving library names to include pathes ------------------------------------------------------------------------ -- | Get all include pathes for a list of libraries to use. libraryIncludePaths :: Maybe FilePath -- ^ @libraries@ file (error reporting only). -> [AgdaLibFile] -- ^ Libraries Agda knows about. -> [LibName] -- ^ (Non-empty) library names to be resolved to (lists of) pathes. -> LibM [FilePath] -- ^ Resolved pathes (no duplicates). Contains "." if @[LibName]@ does. libraryIncludePaths overrideLibFile libs xs0 = mkLibM libs $ WriterT $ do file <- getLibrariesFile overrideLibFile return $ runWriter $ (dot ++) . incs <$> find file [] xs where (dots, xs) = List.partition (== libNameForCurrentDir) $ map trim xs0 incs = List.nub . concatMap libIncludes dot = [ "." | not $ null dots ] -- | Due to library dependencies, the work list may grow temporarily. find :: FilePath -- ^ Only for error reporting. -> [LibName] -- ^ Already resolved libraries. -> [LibName] -- ^ Work list: libraries left to be resolved. -> Writer [LibError] [AgdaLibFile] find _ _ [] = pure [] find file visited (x : xs) | elem x visited = find file visited xs | otherwise = case findLib x libs of [l] -> (l :) <$> find file (x : visited) (libDepends l ++ xs) [] -> tell [LibNotFound file x] >> find file (x : visited) xs ls -> tell [AmbiguousLib x ls] >> find file (x : visited) xs -- | @findLib x libs@ retrieves the matches for @x@ from list @libs@. -- -- 1. Case @x@ is unversioned: -- If @x@ is contained in @libs@, then that match is returned. -- Otherwise, the matches with the highest version number are returned. -- -- 2. Case @x@ is versioned: the matches with the highest version number are returned. -- -- Examples, see 'findLib''. -- findLib :: LibName -> [AgdaLibFile] -> [AgdaLibFile] findLib = findLib' libName -- | Generalized version of 'findLib' for testing. -- -- > findLib' id "a" [ "a-1", "a-02", "a-2", "b" ] == [ "a-02", "a-2" ] -- -- > findLib' id "a" [ "a", "a-1", "a-01", "a-2", "b" ] == [ "a" ] -- > findLib' id "a-1" [ "a", "a-1", "a-01", "a-2", "b" ] == [ "a-1", "a-01" ] -- > findLib' id "a-2" [ "a", "a-1", "a-01", "a-2", "b" ] == [ "a-2" ] -- > findLib' id "c" [ "a", "a-1", "a-01", "a-2", "b" ] == [] -- findLib' :: (a -> LibName) -> LibName -> [a] -> [a] findLib' libName x libs = case ls of -- Take the first and all exact matches (modulo leading zeros in version numbers). l : ls' -> l : takeWhile (((==) `on` versionMeasure) l) ls' [] -> [] where -- @LibName@s that match @x@, sorted descendingly. -- The unversioned LibName, if any, will come first. ls = List.sortBy (flip compare `on` versionMeasure) [ l | l <- libs, x `hasMatch` libName l ] -- foo > foo-2.2 > foo-2.0.1 > foo-2 > foo-1.0 versionMeasure l = (rx, null vs, vs) where VersionView rx vs = versionView (libName l) -- | @x `hasMatch` y@ if @x@ and @y@ have the same @vvBase@ and -- either @x@ has no version qualifier or the versions also match. hasMatch :: LibName -> LibName -> Bool hasMatch x y = rx == ry && (vx == vy || null vx) where VersionView rx vx = versionView x VersionView ry vy = versionView y -- | Split a library name into basename and a list of version numbers. -- -- > versionView "foo-1.2.3" == VersionView "foo" [1, 2, 3] -- > versionView "foo-01.002.3" == VersionView "foo" [1, 2, 3] -- -- Note that because of leading zeros, @versionView@ is not injective. -- (@unVersionView . versionView@ would produce a normal form.) versionView :: LibName -> VersionView versionView s = case span (\ c -> isDigit c || c == '.') (reverse s) of (v, '-' : x) | valid vs -> VersionView (reverse x) $ reverse $ map (read . reverse) vs where vs = chopWhen (== '.') v valid [] = False valid vs = not $ any null vs _ -> VersionView s [] -- | Print a @VersionView@, inverse of @versionView@ (modulo leading zeros). unVersionView :: VersionView -> LibName unVersionView = \case VersionView base [] -> base VersionView base vs -> base ++ "-" ++ List.intercalate "." (map show vs) Agda-2.5.3/src/full/Agda/Interaction/EmacsTop.hs0000644000000000000000000001645013154613124017464 0ustar0000000000000000-- {-# LANGUAGE CPP #-} module Agda.Interaction.EmacsTop ( mimicGHCi ) where import Control.Applicative import Control.Monad.State import Data.Char import qualified Data.List as List import Data.Maybe import System.IO import Agda.Utils.Monad import Agda.Utils.Maybe import Agda.Utils.Pretty import Agda.Utils.String import Agda.Syntax.Common import Agda.TypeChecking.Monad import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.Interaction.Response as R import Agda.Interaction.InteractionTop import Agda.Interaction.EmacsCommand hiding (putResponse) import Agda.Interaction.Highlighting.Emacs import Agda.Interaction.Options import Agda.VersionCommit ---------------------------------- -- | 'mimicGHCi' is a fake ghci interpreter for the Emacs frontend -- and for interaction tests. -- -- 'mimicGHCi' reads the Emacs frontend commands from stdin, -- interprets them and print the result into stdout. mimicGHCi :: TCM () -> TCM () mimicGHCi setup = do liftIO $ do hSetBuffering stdout LineBuffering hSetBuffering stdin LineBuffering hSetEncoding stdout utf8 hSetEncoding stdin utf8 setInteractionOutputCallback $ mapM_ print <=< lispifyResponse commands <- liftIO $ initialiseCommandQueue readCommand handleCommand_ (lift setup) `evalStateT` initCommandState commands opts <- commandLineOptions _ <- interact' `runStateT` (initCommandState commands) { optionsOnReload = opts{ optAbsoluteIncludePaths = [] } } return () where interact' :: CommandM () interact' = do Bench.reset done <- Bench.billTo [] $ do liftIO $ do putStr "Agda2> " hFlush stdout c <- nextCommand case c of Done -> return True -- Done. Error s -> liftIO (putStrLn s) >> return False Command c -> do maybeAbort (runInteraction c) return False lift Bench.print unless done interact' -- Reads the next command from stdin. readCommand :: IO Command readCommand = do done <- isEOF if done then return Done else do r <- getLine _ <- return $! length r -- force to read the full input line case dropWhile isSpace r of "" -> readCommand ('-':'-':_) -> readCommand _ -> case listToMaybe $ reads r of Just (x, "") -> return $ Command x Just (_, rem) -> return $ Error $ "not consumed: " ++ rem _ -> return $ Error $ "cannot read: " ++ r -- | Given strings of goals, warnings and errors, return a pair of the -- body and the title for the info buffer formatWarningsAndErrors :: String -> String -> String -> (String, String) formatWarningsAndErrors g w e = (body, title) where isG = not $ null g isW = not $ null w isE = not $ null e title = List.intercalate "," $ catMaybes [ " Goals" <$ guard isG , " Warnings" <$ guard isW , " Errors" <$ guard isE , " Done" <$ guard (not (isG || isW || isE)) ] body = List.intercalate "\n" $ catMaybes [ g <$ guard isG , delimiter "Warnings" <$ guard (isW && (isG || isE)) , w <$ guard isW , delimiter "Errors" <$ guard (isE && (isG || isW)) , e <$ guard isE ] -- | Convert Response to an elisp value for the interactive emacs frontend. lispifyResponse :: Response -> IO [Lisp String] lispifyResponse (Resp_HighlightingInfo info method modFile) = (:[]) <$> lispifyHighlightingInfo info method modFile lispifyResponse (Resp_DisplayInfo info) = return $ case info of Info_CompilationOk w e -> f body "*Compilation result*" where (body, _) = formatWarningsAndErrors "The module was successfully compiled.\n" w e -- abusing the goals field since we ignore the title Info_Constraints s -> f s "*Constraints*" Info_AllGoalsWarnings g w e -> f body ("*All" ++ title ++ "*") where (body, title) = formatWarningsAndErrors g w e Info_Auto s -> f s "*Auto*" Info_Error s -> f s "*Error*" -- FNF: if Info_Warning comes back into use, the above should be -- clearWarning : f s "*Error*" --Info_Warning s -> [ display_warning "*Errors*" s ] -- FNF: currently unused Info_Time s -> f (render s) "*Time*" Info_NormalForm s -> f (render s) "*Normal Form*" -- show? Info_InferredType s -> f (render s) "*Inferred Type*" Info_CurrentGoal s -> f (render s) "*Current Goal*" Info_GoalType s -> f (render s) "*Goal type etc.*" Info_ModuleContents s -> f (render s) "*Module contents*" Info_SearchAbout s -> f (render s) "*Search About*" Info_WhyInScope s -> f (render s) "*Scope Info*" Info_Context s -> f (render s) "*Context*" Info_HelperFunction s -> [ L [ A "agda2-info-action-and-copy" , A $ quote "*Helper function*" , A $ quote (render s ++ "\n") , A "nil" ] ] Info_Intro s -> f (render s) "*Intro*" Info_Version -> f ("Agda version " ++ versionWithCommitInfo) "*Agda Version*" where f content bufname = [ display_info' False bufname content ] lispifyResponse Resp_ClearHighlighting = return [ L [ A "agda2-highlight-clear" ] ] lispifyResponse Resp_DoneAborting = return [ L [ A "agda2-abort-done" ] ] lispifyResponse Resp_ClearRunningInfo = return [ clearRunningInfo ] -- FNF: if Info_Warning comes back into use, the above should be -- return [ clearRunningInfo, clearWarning ] lispifyResponse (Resp_RunningInfo n s) | n <= 1 = return [ displayRunningInfo s ] | otherwise = return [ L [A "agda2-verbose", A (quote s)] ] lispifyResponse (Resp_Status s) = return [ L [ A "agda2-status-action" , A (quote $ List.intercalate "," $ catMaybes [checked, showImpl]) ] ] where checked = boolToMaybe (sChecked s) "Checked" showImpl = boolToMaybe (sShowImplicitArguments s) "ShowImplicit" lispifyResponse (Resp_JumpToError f p) = return [ lastTag 3 $ L [ A "agda2-maybe-goto", Q $ L [A (quote f), A ".", A (show p)] ] ] lispifyResponse (Resp_InteractionPoints is) = return [ lastTag 1 $ L [A "agda2-goals-action", Q $ L $ map showNumIId is] ] lispifyResponse (Resp_GiveAction ii s) = return [ L [ A "agda2-give-action", showNumIId ii, A s' ] ] where s' = case s of Give_String str -> quote str Give_Paren -> "'paren" Give_NoParen -> "'no-paren" lispifyResponse (Resp_MakeCase variant pcs) = return [ lastTag 2 $ L [ A cmd, Q $ L $ map (A . quote) pcs ] ] where cmd = case variant of R.Function -> "agda2-make-case-action" R.ExtendedLambda -> "agda2-make-case-action-extendlam" lispifyResponse (Resp_SolveAll ps) = return [ lastTag 2 $ L [ A "agda2-solveAll-action", Q . L $ concatMap prn ps ] ] where prn (ii,e)= [showNumIId ii, A $ quote $ show e] -- | Adds a \"last\" tag to a response. lastTag :: Integer -> Lisp String -> Lisp String lastTag n r = Cons (Cons (A "last") (A $ show n)) r -- | Show an iteraction point identifier as an elisp expression. showNumIId :: InteractionId -> Lisp String showNumIId = A . tail . show Agda-2.5.3/src/full/Agda/Interaction/Options.hs0000644000000000000000000007635613154613124017417 0ustar0000000000000000{-# LANGUAGE CPP #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE DeriveDataTypeable #-} #endif module Agda.Interaction.Options ( CommandLineOptions(..) , IgnoreFlags(..) , PragmaOptions(..) , OptionsPragma , Flag, OptM, runOptM, OptDescr(..), ArgDescr(..) , Verbosity , WarningMode(..) , checkOpts , parseStandardOptions, parseStandardOptions' , parsePragmaOptions , parsePluginOptions , defaultOptions , defaultInteractionOptions , defaultVerbosity , defaultCutOff , defaultPragmaOptions , standardOptions_ , unsafePragmaOptions , isLiterate , mapFlag , usage , defaultLibDir -- Reused by PandocAgda , inputFlag , standardOptions , getOptSimple ) where import Control.Applicative import Control.Monad ( (>=>), when ) import Control.Monad.Trans import Data.Either import Data.Maybe import Data.List ( isSuffixOf , intercalate ) #if __GLASGOW_HASKELL__ <= 708 import Data.Typeable ( Typeable ) #endif import System.Console.GetOpt ( getOpt', usageInfo, ArgOrder(ReturnInOrder) , OptDescr(..), ArgDescr(..) ) import System.Directory ( doesFileExist, doesDirectoryExist ) import Text.EditDistance import Agda.Termination.CutOff ( CutOff(..) ) import Agda.Interaction.Library import Agda.Utils.Except ( ExceptT , MonadError(catchError, throwError) , runExceptT ) import Agda.Utils.FileName ( absolute, AbsolutePath, filePath ) import Agda.Utils.Monad ( ifM, readM ) import Agda.Utils.List ( groupOn, wordsBy ) import Agda.Utils.String ( indent ) import Agda.Utils.Trie ( Trie ) import Agda.Syntax.Parser.Literate ( literateExts ) import qualified Agda.Utils.Trie as Trie import Agda.Version -- Paths_Agda.hs is in $(BUILD_DIR)/build/autogen/. import Paths_Agda ( getDataFileName ) -- | This should probably go somewhere else. isLiterate :: FilePath -> Bool isLiterate file = any (`isSuffixOf` file) literateExts -- OptDescr is a Functor -------------------------------------------------- type Verbosity = Trie String Int -- ignore or respect the flags --allow-unsolved-metas, -- --no-termination-check, --no-positivity-check? data IgnoreFlags = IgnoreFlags | RespectFlags deriving Eq -- Potentially turn harmless warnings into nothing, or errors -- (does not apply to non-fatal errors) data WarningMode = LeaveAlone | TurnIntoErrors | IgnoreAllWarnings deriving (Show, Eq #if __GLASGOW_HASKELL__ <= 708 , Typeable #endif ) warningModes :: [ (String, WarningMode) ] warningModes = [ (defaultWarningMode, LeaveAlone) , ("ignore" , IgnoreAllWarnings) , ("error" , TurnIntoErrors) ] -- Don't forget to update -- doc/user-manual/tools/command-line-options.rst -- if you make changes to the command-line options! data CommandLineOptions = Options { optProgramName :: String , optInputFile :: Maybe FilePath , optIncludePaths :: [FilePath] , optAbsoluteIncludePaths :: [AbsolutePath] , optLibraries :: [LibName] , optOverrideLibrariesFile :: Maybe FilePath -- ^ Use this (if Just) instead of .agda/libraries , optDefaultLibs :: Bool -- ^ Use ~/.agda/defaults , optUseLibs :: Bool -- ^ look for .agda-lib files , optShowVersion :: Bool , optShowHelp :: Bool , optInteractive :: Bool , optGHCiInteraction :: Bool , optOptimSmashing :: Bool , optCompileDir :: Maybe FilePath -- ^ In the absence of a path the project root is used. , optGenerateVimFile :: Bool , optGenerateLaTeX :: Bool , optGenerateHTML :: Bool , optDependencyGraph :: Maybe FilePath , optLaTeXDir :: FilePath , optCountClusters :: Bool -- ^ Count extended grapheme clusters rather than code points when -- generating LaTeX. , optHTMLDir :: FilePath , optCSSFile :: Maybe FilePath , optIgnoreInterfaces :: Bool , optForcing :: Bool , optPragmaOptions :: PragmaOptions , optSharing :: Bool , optCaching :: Bool , optOnlyScopeChecking :: Bool -- ^ Should the top-level module only be scope-checked, and not -- type-checked? } deriving Show -- | Options which can be set in a pragma. data PragmaOptions = PragmaOptions { optShowImplicit :: Bool , optShowIrrelevant :: Bool , optVerbose :: Verbosity , optProofIrrelevance :: Bool , optAllowUnsolved :: Bool , optDisablePositivity :: Bool , optTerminationCheck :: Bool , optTerminationDepth :: CutOff -- ^ Cut off structural order comparison at some depth in termination checker? , optCompletenessCheck :: Bool , optUniverseCheck :: Bool , optSizedTypes :: Bool , optInjectiveTypeConstructors :: Bool , optGuardingTypeConstructors :: Bool , optUniversePolymorphism :: Bool , optIrrelevantProjections :: Bool , optExperimentalIrrelevance :: Bool -- ^ irrelevant levels, irrelevant data matching , optWithoutK :: Bool , optCopatterns :: Bool -- ^ Allow definitions by copattern matching? , optPatternMatching :: Bool -- ^ Is pattern matching allowed in the current file? , optExactSplit :: Bool , optEta :: Bool , optRewriting :: Bool -- ^ Can rewrite rules be added and used? , optPostfixProjections :: Bool -- ^ Should system generated projections 'ProjSystem' be printed -- postfix (True) or prefix (False). , optInstanceSearchDepth :: Int , optSafe :: Bool , optWarningMode :: WarningMode , optCompileNoMain :: Bool } deriving ( Show , Eq #if __GLASGOW_HASKELL__ <= 708 , Typeable #endif ) -- | The options from an @OPTIONS@ pragma. -- -- In the future it might be nice to switch to a more structured -- representation. Note that, currently, there is not a one-to-one -- correspondence between list elements and options. type OptionsPragma = [String] -- | Map a function over the long options. Also removes the short options. -- Will be used to add the plugin name to the plugin options. mapFlag :: (String -> String) -> OptDescr a -> OptDescr a mapFlag f (Option _ long arg descr) = Option [] (map f long) arg descr defaultVerbosity :: Verbosity defaultVerbosity = Trie.singleton [] 1 defaultInteractionOptions :: PragmaOptions defaultInteractionOptions = defaultPragmaOptions defaultOptions :: CommandLineOptions defaultOptions = Options { optProgramName = "agda" , optInputFile = Nothing , optIncludePaths = [] , optAbsoluteIncludePaths = [] , optLibraries = [] , optOverrideLibrariesFile = Nothing , optDefaultLibs = True , optUseLibs = True , optShowVersion = False , optShowHelp = False , optInteractive = False , optGHCiInteraction = False , optOptimSmashing = True , optCompileDir = Nothing , optGenerateVimFile = False , optGenerateLaTeX = False , optGenerateHTML = False , optDependencyGraph = Nothing , optLaTeXDir = defaultLaTeXDir , optCountClusters = False , optHTMLDir = defaultHTMLDir , optCSSFile = Nothing , optIgnoreInterfaces = False , optForcing = True , optPragmaOptions = defaultPragmaOptions , optSharing = False , optCaching = False , optOnlyScopeChecking = False } defaultPragmaOptions :: PragmaOptions defaultPragmaOptions = PragmaOptions { optShowImplicit = False , optShowIrrelevant = False , optVerbose = defaultVerbosity , optProofIrrelevance = False , optExperimentalIrrelevance = False , optIrrelevantProjections = True , optAllowUnsolved = False , optDisablePositivity = False , optTerminationCheck = True , optTerminationDepth = defaultCutOff , optCompletenessCheck = True , optUniverseCheck = True , optSizedTypes = True , optInjectiveTypeConstructors = False , optGuardingTypeConstructors = False , optUniversePolymorphism = True , optWithoutK = False , optCopatterns = True , optPatternMatching = True , optExactSplit = False , optEta = True , optRewriting = False , optPostfixProjections = False , optInstanceSearchDepth = 500 , optSafe = False , optWarningMode = fromJust $ lookup defaultWarningMode warningModes , optCompileNoMain = False } -- | The default termination depth. defaultCutOff :: CutOff defaultCutOff = CutOff 0 -- minimum value -- | The default output directory for LaTeX. defaultLaTeXDir :: String defaultLaTeXDir = "latex" -- | The default output directory for HTML. defaultHTMLDir :: String defaultHTMLDir = "html" -- | The default warning mode. defaultWarningMode :: String defaultWarningMode = "warn" type OptM = ExceptT String IO runOptM :: OptM a -> IO (Either String a) runOptM = runExceptT {- | @f :: Flag opts@ is an action on the option record that results from parsing an option. @f opts@ produces either an error message or an updated options record -} type Flag opts = opts -> OptM opts -- | Checks that the given options are consistent. checkOpts :: Flag CommandLineOptions checkOpts opts | not (matches [optGHCiInteraction, isJust . optInputFile] <= 1) = throwError "Choose at most one: input file or --interaction.\n" | or [ p opts && matches ps > 1 | (p, ps) <- exclusive ] = throwError exclusiveMessage | otherwise = return opts where matches = length . filter ($ opts) atMostOne = [ optGenerateHTML , isJust . optDependencyGraph ] ++ map fst exclusive exclusive = [ ( optOnlyScopeChecking , optSafe . optPragmaOptions : optGenerateVimFile : atMostOne ) , ( optInteractive , optGenerateLaTeX : atMostOne ) , ( optGHCiInteraction , optGenerateLaTeX : atMostOne ) ] exclusiveMessage = unlines $ [ "The options --interactive, --interaction and" , "--only-scope-checking cannot be combined with each other or" , "with --html or --dependency-graph. Furthermore" , "--interactive and --interaction cannot be combined with" , "--latex, and --only-scope-checking cannot be combined with" , "--safe or --vim." ] -- | Check for unsafe pramas. Gives a list of used unsafe flags. unsafePragmaOptions :: PragmaOptions -> [String] unsafePragmaOptions opts = [ "--allow-unsolved-metas" | optAllowUnsolved opts ] ++ [ "--no-positivity-check" | optDisablePositivity opts ] ++ [ "--no-termination-check" | not (optTerminationCheck opts) ] ++ [ "--type-in-type" | not (optUniverseCheck opts) ] ++ -- [ "--sized-types" | optSizedTypes opts ] ++ [ "--injective-type-constructors" | optInjectiveTypeConstructors opts ] ++ [ "--guardedness-preserving-type-constructors" | optGuardingTypeConstructors opts ] ++ [ "--experimental-irrelevance" | optExperimentalIrrelevance opts ] ++ [ "--rewriting" | optRewriting opts ] ++ [] inputFlag :: FilePath -> Flag CommandLineOptions inputFlag f o = case optInputFile o of Nothing -> return $ o { optInputFile = Just f } Just _ -> throwError "only one input file allowed" versionFlag :: Flag CommandLineOptions versionFlag o = return $ o { optShowVersion = True } helpFlag :: Flag CommandLineOptions helpFlag o = return $ o { optShowHelp = True } safeFlag :: Flag PragmaOptions safeFlag o = return $ o { optSafe = True } sharingFlag :: Bool -> Flag CommandLineOptions sharingFlag b o = return $ o { optSharing = b } cachingFlag :: Bool -> Flag CommandLineOptions cachingFlag b o = return $ o { optCaching = b } proofIrrelevanceFlag :: Flag PragmaOptions proofIrrelevanceFlag o = return $ o { optProofIrrelevance = True } experimentalIrrelevanceFlag :: Flag PragmaOptions experimentalIrrelevanceFlag o = return $ o { optExperimentalIrrelevance = True } noIrrelevantProjectionsFlag :: Flag PragmaOptions noIrrelevantProjectionsFlag o = return $ o { optIrrelevantProjections = False } ignoreInterfacesFlag :: Flag CommandLineOptions ignoreInterfacesFlag o = return $ o { optIgnoreInterfaces = True } allowUnsolvedFlag :: Flag PragmaOptions allowUnsolvedFlag o = return $ o { optAllowUnsolved = True } showImplicitFlag :: Flag PragmaOptions showImplicitFlag o = return $ o { optShowImplicit = True } showIrrelevantFlag :: Flag PragmaOptions showIrrelevantFlag o = return $ o { optShowIrrelevant = True } ghciInteractionFlag :: Flag CommandLineOptions ghciInteractionFlag o = return $ o { optGHCiInteraction = True } vimFlag :: Flag CommandLineOptions vimFlag o = return $ o { optGenerateVimFile = True } latexFlag :: Flag CommandLineOptions latexFlag o = return $ o { optGenerateLaTeX = True } onlyScopeCheckingFlag :: Flag CommandLineOptions onlyScopeCheckingFlag o = return $ o { optOnlyScopeChecking = True } countClustersFlag :: Flag CommandLineOptions countClustersFlag o = #ifdef COUNT_CLUSTERS return $ o { optCountClusters = True } #else throwError "Cluster counting has not been enabled in this build of Agda." #endif latexDirFlag :: FilePath -> Flag CommandLineOptions latexDirFlag d o = return $ o { optLaTeXDir = d } noPositivityFlag :: Flag PragmaOptions noPositivityFlag o = return $ o { optDisablePositivity = True } dontTerminationCheckFlag :: Flag PragmaOptions dontTerminationCheckFlag o = return $ o { optTerminationCheck = False } -- The option was removed. See Issue 1918. dontCompletenessCheckFlag :: Flag PragmaOptions dontCompletenessCheckFlag _ = throwError "the --no-coverage-check option has been removed" dontUniverseCheckFlag :: Flag PragmaOptions dontUniverseCheckFlag o = return $ o { optUniverseCheck = False } etaFlag :: Flag PragmaOptions etaFlag o = return $ o { optEta = True } noEtaFlag :: Flag PragmaOptions noEtaFlag o = return $ o { optEta = False } sizedTypes :: Flag PragmaOptions sizedTypes o = return $ o { optSizedTypes = True } noSizedTypes :: Flag PragmaOptions noSizedTypes o = return $ o { optSizedTypes = False } injectiveTypeConstructorFlag :: Flag PragmaOptions injectiveTypeConstructorFlag o = return $ o { optInjectiveTypeConstructors = True } guardingTypeConstructorFlag :: Flag PragmaOptions guardingTypeConstructorFlag o = return $ o { optGuardingTypeConstructors = True } universePolymorphismFlag :: Flag PragmaOptions universePolymorphismFlag o = return $ o { optUniversePolymorphism = True } noUniversePolymorphismFlag :: Flag PragmaOptions noUniversePolymorphismFlag o = return $ o { optUniversePolymorphism = False } noForcingFlag :: Flag CommandLineOptions noForcingFlag o = return $ o { optForcing = False } withKFlag :: Flag PragmaOptions withKFlag o = return $ o { optWithoutK = False } withoutKFlag :: Flag PragmaOptions withoutKFlag o = return $ o { optWithoutK = True } copatternsFlag :: Flag PragmaOptions copatternsFlag o = return $ o { optCopatterns = True } noCopatternsFlag :: Flag PragmaOptions noCopatternsFlag o = return $ o { optCopatterns = False } noPatternMatchingFlag :: Flag PragmaOptions noPatternMatchingFlag o = return $ o { optPatternMatching = False } exactSplitFlag :: Flag PragmaOptions exactSplitFlag o = return $ o { optExactSplit = True } noExactSplitFlag :: Flag PragmaOptions noExactSplitFlag o = return $ o { optExactSplit = False } rewritingFlag :: Flag PragmaOptions rewritingFlag o = return $ o { optRewriting = True } postfixProjectionsFlag :: Flag PragmaOptions postfixProjectionsFlag o = return $ o { optPostfixProjections = True } instanceDepthFlag :: String -> Flag PragmaOptions instanceDepthFlag s o = do d <- integerArgument "--instance-search-depth" s return $ o { optInstanceSearchDepth = d } interactiveFlag :: Flag CommandLineOptions interactiveFlag o = return $ o { optInteractive = True , optPragmaOptions = (optPragmaOptions o) { optAllowUnsolved = True } } compileFlagNoMain :: Flag PragmaOptions compileFlagNoMain o = return $ o { optCompileNoMain = True } compileDirFlag :: FilePath -> Flag CommandLineOptions compileDirFlag f o = return $ o { optCompileDir = Just f } htmlFlag :: Flag CommandLineOptions htmlFlag o = return $ o { optGenerateHTML = True } dependencyGraphFlag :: FilePath -> Flag CommandLineOptions dependencyGraphFlag f o = return $ o { optDependencyGraph = Just f } htmlDirFlag :: FilePath -> Flag CommandLineOptions htmlDirFlag d o = return $ o { optHTMLDir = d } cssFlag :: FilePath -> Flag CommandLineOptions cssFlag f o = return $ o { optCSSFile = Just f } includeFlag :: FilePath -> Flag CommandLineOptions includeFlag d o = return $ o { optIncludePaths = d : optIncludePaths o } libraryFlag :: String -> Flag CommandLineOptions libraryFlag s o = return $ o { optLibraries = optLibraries o ++ [s] } overrideLibrariesFileFlag :: String -> Flag CommandLineOptions overrideLibrariesFileFlag s o = do ifM (liftIO $ doesFileExist s) {-then-} (return $ o { optOverrideLibrariesFile = Just s }) {-else-} (throwError $ "Libraries file not found: " ++ s) noDefaultLibsFlag :: Flag CommandLineOptions noDefaultLibsFlag o = return $ o { optDefaultLibs = False } noLibsFlag :: Flag CommandLineOptions noLibsFlag o = return $ o { optUseLibs = False } verboseFlag :: String -> Flag PragmaOptions verboseFlag s o = do (k,n) <- parseVerbose s return $ o { optVerbose = Trie.insert k n $ optVerbose o } where parseVerbose s = case wordsBy (`elem` ":.") s of [] -> usage ss -> do n <- readM (last ss) `catchError` \_ -> usage return (init ss, n) usage = throwError "argument to verbose should be on the form x.y.z:N or N" warningModeFlag :: String -> Flag PragmaOptions warningModeFlag s o = case lookup s warningModes of Just m -> return $ o { optWarningMode = m } Nothing -> usage where usage = throwError $ "unknown warning mode (available: " ++ intercalate ", " (map fst warningModes) ++ ")" terminationDepthFlag :: String -> Flag PragmaOptions terminationDepthFlag s o = do k <- readM s `catchError` \_ -> usage when (k < 1) $ usage -- or: turn termination checking off for 0 return $ o { optTerminationDepth = CutOff $ k-1 } where usage = throwError "argument to termination-depth should be >= 1" integerArgument :: String -> String -> OptM Int integerArgument flag s = readM s `catchError` \_ -> throwError $ "option '" ++ flag ++ "' requires an integer argument" standardOptions :: [OptDescr (Flag CommandLineOptions)] standardOptions = [ Option ['V'] ["version"] (NoArg versionFlag) "show version number" , Option ['?'] ["help"] (NoArg helpFlag) "show this help" , Option ['I'] ["interactive"] (NoArg interactiveFlag) "start in interactive mode" , Option [] ["interaction"] (NoArg ghciInteractionFlag) "for use with the Emacs mode" , Option [] ["compile-dir"] (ReqArg compileDirFlag "DIR") ("directory for compiler output (default: the project root)") , Option [] ["vim"] (NoArg vimFlag) "generate Vim highlighting files" , Option [] ["latex"] (NoArg latexFlag) "generate LaTeX with highlighted source code" , Option [] ["latex-dir"] (ReqArg latexDirFlag "DIR") ("directory in which LaTeX files are placed (default: " ++ defaultLaTeXDir ++ ")") , Option [] ["count-clusters"] (NoArg countClustersFlag) ("count extended grapheme clusters when " ++ "generating LaTeX (note that this flag " ++ #ifdef COUNT_CLUSTERS "is not enabled in all builds of Agda)" #else "has not been enabled in this build of Agda)" #endif ) , Option [] ["html"] (NoArg htmlFlag) "generate HTML files with highlighted source code" , Option [] ["html-dir"] (ReqArg htmlDirFlag "DIR") ("directory in which HTML files are placed (default: " ++ defaultHTMLDir ++ ")") , Option [] ["css"] (ReqArg cssFlag "URL") "the CSS file used by the HTML files (can be relative)" , Option [] ["dependency-graph"] (ReqArg dependencyGraphFlag "FILE") "generate a Dot file with a module dependency graph" , Option [] ["ignore-interfaces"] (NoArg ignoreInterfacesFlag) "ignore interface files (re-type check everything)" , Option ['i'] ["include-path"] (ReqArg includeFlag "DIR") "look for imports in DIR" , Option ['l'] ["library"] (ReqArg libraryFlag "LIB") "use library LIB" , Option [] ["library-file"] (ReqArg overrideLibrariesFileFlag "FILE") "use FILE instead of the standard libraries file" , Option [] ["no-libraries"] (NoArg noLibsFlag) "don't use any library files" , Option [] ["no-default-libraries"] (NoArg noDefaultLibsFlag) "don't use default libraries" , Option [] ["no-forcing"] (NoArg noForcingFlag) "disable the forcing optimisation" , Option [] ["sharing"] (NoArg $ sharingFlag True) "enable sharing and call-by-need evaluation (experimental) (default: OFF)" , Option [] ["no-sharing"] (NoArg $ sharingFlag False) "disable sharing and call-by-need evaluation" , Option [] ["caching"] (NoArg $ cachingFlag True) "enable caching of typechecking (experimental) (default: OFF)" , Option [] ["no-caching"] (NoArg $ cachingFlag False) "disable caching of typechecking" , Option [] ["only-scope-checking"] (NoArg onlyScopeCheckingFlag) "only scope-check the top-level module, do not type-check it" ] ++ map (fmap lift) pragmaOptions where lift :: Flag PragmaOptions -> Flag CommandLineOptions lift f = \opts -> do ps <- f (optPragmaOptions opts) return (opts { optPragmaOptions = ps }) pragmaOptions :: [OptDescr (Flag PragmaOptions)] pragmaOptions = [ Option [] ["show-implicit"] (NoArg showImplicitFlag) "show implicit arguments when printing" , Option [] ["show-irrelevant"] (NoArg showIrrelevantFlag) "show irrelevant arguments when printing" , Option ['v'] ["verbose"] (ReqArg verboseFlag "N") "set verbosity level to N" -- , Option [] ["proof-irrelevance"] (NoArg proofIrrelevanceFlag) -- "enable proof irrelevance (experimental feature)" , Option [] ["allow-unsolved-metas"] (NoArg allowUnsolvedFlag) "succeed and create interface file regardless of unsolved meta variables" , Option [] ["no-positivity-check"] (NoArg noPositivityFlag) "do not warn about not strictly positive data types" , Option [] ["no-termination-check"] (NoArg dontTerminationCheckFlag) "do not warn about possibly nonterminating code" , Option [] ["termination-depth"] (ReqArg terminationDepthFlag "N") "allow termination checker to count decrease/increase upto N (default N=1)" , Option [] ["no-coverage-check"] (NoArg dontCompletenessCheckFlag) "the option has been removed" , Option [] ["type-in-type"] (NoArg dontUniverseCheckFlag) "ignore universe levels (this makes Agda inconsistent)" , Option [] ["sized-types"] (NoArg sizedTypes) "use sized types (default, inconsistent with `musical' coinduction)" , Option [] ["no-sized-types"] (NoArg noSizedTypes) "disable sized types" , Option [] ["injective-type-constructors"] (NoArg injectiveTypeConstructorFlag) "enable injective type constructors (makes Agda anti-classical and possibly inconsistent)" , Option [] ["guardedness-preserving-type-constructors"] (NoArg guardingTypeConstructorFlag) "treat type constructors as inductive constructors when checking productivity" , Option [] ["no-universe-polymorphism"] (NoArg noUniversePolymorphismFlag) "disable universe polymorphism" , Option [] ["universe-polymorphism"] (NoArg universePolymorphismFlag) "enable universe polymorphism (default)" , Option [] ["no-irrelevant-projections"] (NoArg noIrrelevantProjectionsFlag) "disable projection of irrelevant record fields" , Option [] ["experimental-irrelevance"] (NoArg experimentalIrrelevanceFlag) "enable potentially unsound irrelevance features (irrelevant levels, irrelevant data matching)" , Option [] ["with-K"] (NoArg withKFlag) "enable the K rule in pattern matching" , Option [] ["without-K"] (NoArg withoutKFlag) "disable the K rule in pattern matching" , Option [] ["copatterns"] (NoArg copatternsFlag) "enable definitions by copattern matching (default)" , Option [] ["no-copatterns"] (NoArg noCopatternsFlag) "disable definitions by copattern matching" , Option [] ["no-pattern-matching"] (NoArg noPatternMatchingFlag) "disable pattern matching completely" , Option [] ["exact-split"] (NoArg exactSplitFlag) "require all clauses in a definition to hold as definitional equalities (unless marked CATCHALL)" , Option [] ["no-exact-split"] (NoArg noExactSplitFlag) "do not require all clauses in a definition to hold as definitional equalities (default)" , Option [] ["no-eta-equality"] (NoArg noEtaFlag) "default records to no-eta-equality" , Option [] ["rewriting"] (NoArg rewritingFlag) "enable declaration and use of REWRITE rules" , Option [] ["postfix-projections"] (NoArg postfixProjectionsFlag) "make postfix projection notation the default" , Option [] ["instance-search-depth"] (ReqArg instanceDepthFlag "N") "set instance search depth to N (default: 500)" , Option [] ["safe"] (NoArg safeFlag) "disable postulates, unsafe OPTION pragmas and primTrustMe" , Option ['W'] ["warning"] (ReqArg warningModeFlag "MODE") ("set warning mode to MODE (available: " ++ intercalate ", " (map fst warningModes) ++ ". Default: " ++ defaultWarningMode ++ ")") , Option [] ["no-main"] (NoArg compileFlagNoMain) "do not treat the requested module as the main module of a program when compiling" ] -- | Used for printing usage info. standardOptions_ :: [OptDescr ()] standardOptions_ = map (fmap $ const ()) standardOptions -- | Simple interface for System.Console.GetOpt -- Could be moved to Agda.Utils.Options (does not exist yet) getOptSimple :: [String] -- ^ command line argument words -> [OptDescr (Flag opts)] -- ^ options handlers -> (String -> Flag opts) -- ^ handler of non-options (only one is allowed) -> Flag opts -- ^ combined opts data structure transformer getOptSimple argv opts fileArg = \ defaults -> case getOpt' (ReturnInOrder fileArg) opts argv of (o, _, [] , [] ) -> foldl (>>=) (return defaults) o (_, _, unrecognized, errs) -> throwError $ umsg ++ emsg where ucap = "Unrecognized " ++ plural unrecognized "option" ++ ":" ecap = plural errs "Option error" ++ ":" umsg = if null unrecognized then "" else unlines $ ucap : map suggest unrecognized emsg = if null errs then "" else unlines $ ecap : errs plural [_] x = x plural _ x = x ++ "s" -- Suggest alternatives that are at most 3 typos away longopts :: [String] longopts = map ("--" ++) $ concat $ map (\ (Option _ long _ _) -> long) opts dist :: String -> String -> Int dist s t = restrictedDamerauLevenshteinDistance defaultEditCosts s t close :: String -> String -> Maybe (Int, String) close s t = let d = dist s t in if d <= 3 then Just (d, t) else Nothing closeopts :: String -> [(Int, String)] closeopts s = mapMaybe (close s) longopts alts :: String -> [[String]] alts s = map (map snd) $ groupOn fst $ closeopts s suggest :: String -> String suggest s = case alts s of [] -> s as : _ -> s ++ " (did you mean " ++ sugs as ++ " ?)" sugs :: [String] -> String sugs [a] = a sugs as = "any of " ++ intercalate " " as -- | Parse the standard options. parseStandardOptions :: [String] -> OptM CommandLineOptions parseStandardOptions argv = parseStandardOptions' argv defaultOptions parseStandardOptions' :: [String] -> Flag CommandLineOptions parseStandardOptions' argv opts = do opts <- getOptSimple (stripRTS argv) standardOptions inputFlag opts checkOpts opts -- | Parse options from an options pragma. parsePragmaOptions :: [String] -- ^ Pragma options. -> CommandLineOptions -- ^ Command-line options which should be updated. -> OptM PragmaOptions parsePragmaOptions argv opts = do ps <- getOptSimple argv pragmaOptions (\s _ -> throwError $ "Bad option in pragma: " ++ s) (optPragmaOptions opts) _ <- checkOpts (opts { optPragmaOptions = ps }) return ps -- | Parse options for a plugin. parsePluginOptions :: [String] -> [OptDescr (Flag opts)] -> Flag opts parsePluginOptions argv opts = getOptSimple argv opts (\s _ -> throwError $ "Internal error: Flag " ++ s ++ " passed to a plugin") -- | The usage info message. The argument is the program name (probably -- agda). usage :: [OptDescr ()] -> String -> String usage options progName = usageInfo (header progName) options where header progName = unlines [ "Agda version " ++ version, "" , "Usage: " ++ progName ++ " [OPTIONS...] [FILE]" ] -- Remove +RTS .. -RTS from arguments stripRTS :: [String] -> [String] stripRTS [] = [] stripRTS ("--RTS" : argv) = argv stripRTS (arg : argv) | is "+RTS" arg = stripRTS $ drop 1 $ dropWhile (not . is "-RTS") argv | otherwise = arg : stripRTS argv where is x arg = [x] == take 1 (words arg) ------------------------------------------------------------------------ -- Some paths -- | Returns the absolute default lib dir. This directory is used to -- store the Primitive.agda file. defaultLibDir :: IO FilePath defaultLibDir = do libdir <- fmap filePath (absolute =<< getDataFileName "lib") ifM (doesDirectoryExist libdir) (return libdir) (error $ "The lib directory " ++ libdir ++ " does not exist") Agda-2.5.3/src/full/Agda/Interaction/MakeCase.hs0000644000000000000000000003317013154613124017420 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Interaction.MakeCase where import Prelude hiding (mapM, mapM_, null) import Control.Applicative hiding (empty) import Control.Monad hiding (mapM, mapM_, forM) import qualified Data.Map as Map import qualified Data.List as List import Data.Maybe import Data.Traversable import Agda.Syntax.Common import Agda.Syntax.Position import qualified Agda.Syntax.Concrete as C import qualified Agda.Syntax.Abstract as A import qualified Agda.Syntax.Info as A import Agda.Syntax.Internal import Agda.Syntax.Internal.Pattern import Agda.Syntax.Scope.Monad (resolveName, ResolvedName(..)) import Agda.Syntax.Translation.ConcreteToAbstract import Agda.Syntax.Translation.InternalToAbstract import Agda.TypeChecking.Monad import Agda.TypeChecking.Coverage import Agda.TypeChecking.Pretty import Agda.TypeChecking.RecordPatterns import Agda.TypeChecking.Reduce import Agda.TypeChecking.Substitute import Agda.TypeChecking.Irrelevance import Agda.TypeChecking.Rules.LHS.Implicit import Agda.TheTypeChecker import Agda.Interaction.Options import Agda.Interaction.BasicOps import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Monad import Agda.Utils.Null import qualified Agda.Utils.Pretty as P import Agda.Utils.Singleton import Agda.Utils.Size import qualified Agda.Utils.HashMap as HMap #include "undefined.h" import Agda.Utils.Impossible type CaseContext = Maybe ExtLamInfo -- | Parse variables (visible or hidden), returning their de Bruijn indices. -- Used in 'makeCase'. parseVariables :: QName -- ^ The function name. -> Telescope -- ^ The telescope of the clause we are splitting. -> InteractionId -- ^ The hole of this function we are working on. -> Range -- ^ The range of this hole. -> [String] -- ^ The words the user entered in this hole (variable names). -> TCM [Int] -- ^ The computed de Bruijn indices of the variables to split on. parseVariables f tel ii rng ss = do -- Get into the context of the meta. mId <- lookupInteractionId ii updateMetaVarRange mId rng mi <- getMetaInfo <$> lookupMeta mId enterClosure mi $ \ r -> do -- Get printed representation of variables in context. n <- getContextSize xs <- forM (downFrom n) $ \ i -> do (,i) . P.render <$> prettyTCM (var i) -- We might be under some lambdas, in which case the context -- is bigger than the number of pattern variables. let nlocals = n - size tel unless (nlocals >= 0) __IMPOSSIBLE__ reportSDoc "interaction.case" 20 $ do m <- currentModule tel <- lookupSection m fv <- getDefFreeVars f vcat [ text "parseVariables:" , text "current module =" <+> prettyTCM m , text "current section =" <+> inTopContext (prettyTCM tel) , text $ "function's fvs = " ++ show fv , text $ "number of locals= " ++ show nlocals ] -- Compute which variables correspond to module parameters. These cannot be split on. -- Note: these are not necessarily the outer-most bound variables, since -- module parameter refinement may have instantiated them, or -- with-abstraction might have reshuffled the variables (#2181). pars <- freeVarsToApply f let nonSplittableVars = [ i | Var i [] <- map unArg pars ] -- Resolve each string to a variable. forM ss $ \ s -> do let failNotVar = typeError $ GenericError $ "Not a variable: " ++ s done i | i < 0 = typeError $ GenericError $ "Cannot split on local variable " ++ s -- See issue #2239 | elem i nonSplittableVars = typeError $ GenericError $ "Cannot split on variable " ++ s ++ ". It is either a module parameter " ++ "or already instantiated by a dot pattern" | otherwise = return i -- Note: the range in the concrete name is only approximate. resName <- resolveName $ C.QName $ C.Name r $ C.stringNameParts s case resName of -- Fail if s is a name, but not of a variable. DefinedName{} -> failNotVar FieldName{} -> failNotVar ConstructorName{} -> failNotVar PatternSynResName{} -> failNotVar -- If s is a variable name in scope, get its de Bruijn index -- via the type checker. VarName x _ -> do (v, _) <- getVarInfo x case ignoreSharing v of Var i [] -> done $ i - nlocals _ -> typeError . GenericDocError =<< sep [ text $ "Cannot split on variable " ++ s ++ ", because it is bound to" , prettyTCM v ] -- If s is not a name, compare it to the printed variable representation. -- This fallback is to enable splitting on hidden variables. UnknownName -> do case filter ((s ==) . fst) xs of [] -> typeError $ GenericError $ "Unbound variable " ++ s [(_,i)] -> done $ i - nlocals -- Issue 1325: Variable names in context can be ambiguous. _ -> typeError $ GenericError $ "Ambiguous variable " ++ s -- | Lookup the clause for an interaction point in the signature. -- Returns the CaseContext, the clause itself, and a list of previous clauses -- Andreas, 2016-06-08, issue #289 and #2006. -- This replace the old findClause hack (shutter with disgust). getClauseForIP :: QName -> Int -> TCM (CaseContext, Clause, [Clause]) getClauseForIP f clauseNo = do (theDef <$> getConstInfo f) >>= \case Function{funClauses = cs, funExtLam = extlam} -> do let (cs1,cs2) = fromMaybe __IMPOSSIBLE__ $ splitExactlyAt clauseNo cs c = fromMaybe __IMPOSSIBLE__ $ headMaybe cs2 return (extlam, c, cs1) d -> do reportSDoc "impossible" 10 $ vcat [ text "getClauseForIP" <+> prettyTCM f <+> text (show clauseNo) <+> text "received" , text (show d) ] __IMPOSSIBLE__ -- | Entry point for case splitting tactic. makeCase :: InteractionId -> Range -> String -> TCM (QName, CaseContext, [A.Clause]) makeCase hole rng s = withInteractionId hole $ do -- Get function clause which contains the interaction point. InteractionPoint { ipMeta = mm, ipClause = ipCl} <- lookupInteractionPoint hole let meta = fromMaybe __IMPOSSIBLE__ mm (f, clauseNo, rhs) <- case ipCl of IPClause f clauseNo rhs-> return (f, clauseNo, rhs) IPNoClause -> typeError $ GenericError $ "Cannot split here, as we are not in a function definition" (casectxt, clause, prevClauses) <- getClauseForIP f clauseNo let perm = fromMaybe __IMPOSSIBLE__ $ clausePerm clause tel = clauseTel clause ps = namedClausePats clause reportSDoc "interaction.case" 10 $ vcat [ text "splitting clause:" , nest 2 $ vcat [ text "f =" <+> prettyTCM f , text "context =" <+> ((inTopContext . prettyTCM) =<< getContextTelescope) , text "tel =" <+> (inTopContext . prettyTCM) tel , text "perm =" <+> text (show perm) , text "ps =" <+> text (show ps) ] ] -- Check split variables. let vars = words s -- If the user just entered ".", do nothing. -- This will expand an ellipsis, if present. if concat vars == "." then do cl <- makeAbstractClause f rhs $ clauseToSplitClause clause return (f, casectxt, [cl]) -- If we have no split variables, split on result. else if null vars then do -- Andreas, 2017-07-24, issue #2654: -- When we introduce projection patterns in an extended lambda, -- we need to print them postfix. let postProjInExtLam = applyWhen (isJust casectxt) $ withPragmaOptions $ \ opt -> opt { optPostfixProjections = True } (piTel, sc) <- fixTarget $ clauseToSplitClause clause -- Andreas, 2015-05-05 If we introduced new function arguments -- do not split on result. This might be more what the user wants. -- To split on result, he can then C-c C-c again. -- Andreas, 2015-05-21 Issue 1516: However, if only hidden -- arguments are introduced, C-c C-c virtually does nothing -- (as they are not shown and get lost on the way to emacs and back). newPats <- if null piTel then return False else do -- If there were any pattern introduce, they will only have effect -- if any of them is shown by the printer imp <- optShowImplicit <$> pragmaOptions return $ imp || any visible (telToList piTel) scs <- if newPats then return [sc] else postProjInExtLam $ do res <- splitResult f sc case res of Nothing -> typeError $ GenericError $ "Cannot split on result here" Just cov -> ifNotM (optCopatterns <$> pragmaOptions) failNoCop $ {-else-} do -- Andreas, 2016-05-03: do not introduce function arguments after projection. -- This is sometimes annoying and can anyway be done by another C-c C-c. -- mapM (snd <.> fixTarget) $ splitClauses cov return $ splitClauses cov checkClauseIsClean ipCl (f, casectxt,) <$> mapM (makeAbstractClause f rhs) scs else do -- split on variables xs <- parseVariables f tel hole rng vars -- Variables that are not in scope yet are brought into scope (@toShow@) -- The other variables are split on (@toSplit@). let (toShow, toSplit) = flip mapEither (zip xs vars) $ \ (x, s) -> if take 1 s == "." then Left x else Right x let sc = makePatternVarsVisible toShow $ clauseToSplitClause clause scs <- split f toSplit sc -- filter out clauses that are already covered scs <- filterM (not <.> isCovered f prevClauses . fst) scs cs <- forM scs $ \(sc, isAbsurd) -> do if isAbsurd then makeAbsurdClause f sc else makeAbstractClause f rhs sc reportSDoc "interaction.case" 65 $ vcat [ text "split result:" , nest 2 $ vcat $ map (text . show) cs ] checkClauseIsClean ipCl return (f, casectxt, cs) where failNoCop = typeError $ GenericError $ "OPTION --copatterns needed to split on result here" -- Split clause on given variables, return the resulting clauses together -- with a bool indicating whether each clause is absurd split :: QName -> [Nat] -> SplitClause -> TCM [(SplitClause, Bool)] split f [] clause = return [(clause,False)] split f (var : vars) clause = do z <- dontAssignMetas $ splitClauseWithAbsurd clause var case z of Left err -> typeError $ SplitError err Right (Left cl) -> return [(cl,True)] Right (Right cov) -> concat <$> do forM (splitClauses cov) $ \ cl -> split f (mapMaybe (newVar cl) vars) cl -- Finds the new variable corresponding to an old one, if any. newVar :: SplitClause -> Nat -> Maybe Nat newVar c x = case ignoreSharing $ applyPatSubst (scSubst c) (var x) of Var y [] -> Just y _ -> Nothing -- Check whether clause has been refined after last load. -- In this case, we refuse to split, as this might lose the refinements. checkClauseIsClean :: IPClause -> TCM () checkClauseIsClean ipCl = do sips <- filter ipSolved . Map.elems <$> use stInteractionPoints when (List.any ((== ipCl) . ipClause) sips) $ typeError $ GenericError $ "Cannot split as clause rhs has been refined. Please reload" -- | Mark the variables given by the list of deBruijn indices as 'UserWritten' -- in the 'SplitClause'. makePatternVarsVisible :: [Nat] -> SplitClause -> SplitClause makePatternVarsVisible [] sc = sc makePatternVarsVisible is sc@SClause{ scPats = ps } = sc{ scPats = map (mapNamedArg mkVis) ps } where mkVis :: NamedArg DBPatVar -> NamedArg DBPatVar mkVis nx@(Arg ai (Named n (DBPatVar x i))) | i `elem` is = -- We could introduce extra consistency checks, like -- if visible ai then __IMPOSSIBLE__ else -- or passing the parsed name along and comparing it with @x@ setOrigin CaseSplit nx | otherwise = nx -- | Make clause with no rhs (because of absurd match). makeAbsurdClause :: QName -> SplitClause -> TCM A.Clause makeAbsurdClause f (SClause tel ps _ _ t) = do reportSDoc "interaction.case" 10 $ vcat [ text "Interaction.MakeCase.makeAbsurdClause: split clause:" , nest 2 $ vcat [ text "context =" <+> do (inTopContext . prettyTCM) =<< getContextTelescope , text "tel =" <+> do inTopContext $ prettyTCM tel , text "ps =" <+> do inTopContext $ addContext tel $ prettyTCMPatternList ps -- P.sep <$> prettyTCMPatterns ps ] ] withCurrentModule (qnameModule f) $ do -- Andreas, 2015-05-29 Issue 635 -- Contract implicit record patterns before printing. -- c <- translateRecordPatterns $ Clause noRange tel perm ps NoBody t False -- Jesper, 2015-09-19 Don't contract, since we do on-demand splitting let c = Clause noRange noRange tel ps Nothing t False Nothing -- Normalise the dot patterns ps <- addContext tel $ normalise $ namedClausePats c reportSDoc "interaction.case" 60 $ text "normalized patterns: " <+> text (show ps) inTopContext $ reify $ QNamed f $ c { namedClausePats = ps } -- | Make a clause with a question mark as rhs. makeAbstractClause :: QName -> A.RHS -> SplitClause -> TCM A.Clause makeAbstractClause f rhs cl = do lhs <- A.clauseLHS <$> makeAbsurdClause f cl reportSDoc "interaction.case" 60 $ text "reified lhs: " <+> text (show lhs) return $ A.Clause lhs [] [] rhs [] False -- let ii = InteractionId (-1) -- Dummy interaction point since we never type check this. -- -- Can end up in verbose output though (#1842), hence not __IMPOSSIBLE__. -- let info = A.emptyMetaInfo -- metaNumber = Nothing in order to print as ?, not ?n -- return $ A.Clause lhs [] (A.RHS $ A.QuestionMark info ii) [] False Agda-2.5.3/src/full/Agda/Interaction/Imports.hs0000644000000000000000000011213313154613124017401 0ustar0000000000000000{-# LANGUAGE CPP #-} {-| This module deals with finding imported modules and loading their interface files. -} module Agda.Interaction.Imports where import Prelude hiding (null) import Control.Arrow import Control.DeepSeq import Control.Monad.Reader import Control.Monad.State import Control.Monad.Trans.Maybe import qualified Control.Exception as E #if __GLASGOW_HASKELL__ <= 708 import Data.Foldable ( Foldable ) import Data.Traversable ( Traversable, traverse ) #endif import Data.Function (on) import qualified Data.Map as Map import qualified Data.List as List import qualified Data.Set as Set import qualified Data.Foldable as Fold (toList) import qualified Data.List as List import Data.Maybe import Data.Monoid (mempty, mappend) import Data.Map (Map) import Data.Set (Set) import System.Directory (doesFileExist, getModificationTime, removeFile) import System.FilePath (()) import qualified Text.PrettyPrint.Boxes as Boxes import Agda.Benchmarking import qualified Agda.Syntax.Abstract as A import qualified Agda.Syntax.Concrete as C import Agda.Syntax.Abstract.Name import Agda.Syntax.Parser import Agda.Syntax.Position import Agda.Syntax.Scope.Base import Agda.Syntax.Translation.ConcreteToAbstract import Agda.Syntax.Internal import Agda.TypeChecking.Errors import Agda.TypeChecking.Warnings import Agda.TypeChecking.Reduce import Agda.TypeChecking.MetaVars ( openMetasToPostulates ) import Agda.TypeChecking.Monad import Agda.TypeChecking.Serialise import Agda.TypeChecking.Telescope import Agda.TypeChecking.Primitive import Agda.TypeChecking.Pretty as P import Agda.TypeChecking.DeadCode import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TheTypeChecker import Agda.Interaction.FindFile import {-# SOURCE #-} Agda.Interaction.InteractionTop (showOpenMetas) import Agda.Interaction.Options import qualified Agda.Interaction.Options.Lenses as Lens import Agda.Interaction.Highlighting.Precise (HighlightingInfo) import Agda.Interaction.Highlighting.Generate import Agda.Interaction.Highlighting.Vim import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.FileName import Agda.Utils.Lens import Agda.Utils.Maybe import qualified Agda.Utils.Maybe.Strict as Strict import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.IO.Binary import Agda.Utils.Pretty hiding (Mode) import Agda.Utils.Time import Agda.Utils.Hash import qualified Agda.Utils.HashMap as HMap import qualified Agda.Utils.Trie as Trie #include "undefined.h" import Agda.Utils.Impossible -- | Is the aim to type-check the top-level module, or only to -- scope-check it? data Mode = ScopeCheck | TypeCheck deriving (Eq, Show) -- | Are we loading the interface for the user-loaded file -- or for an import? data MainInterface = MainInterface Mode -- ^ For the main file. -- -- In this case state changes inflicted by -- 'createInterface' are preserved. | NotMainInterface -- ^ For an imported file. -- -- In this case state changes inflicted by -- 'createInterface' are not preserved. deriving (Eq, Show) -- | Should state changes inflicted by 'createInterface' be preserved? includeStateChanges :: MainInterface -> Bool includeStateChanges (MainInterface _) = True includeStateChanges NotMainInterface = False -- | Merge an interface into the current proof state. mergeInterface :: Interface -> TCM () mergeInterface i = do let sig = iSignature i builtin = Map.toList $ iBuiltin i prim = [ x | (_,Prim x) <- builtin ] bi = Map.fromList [ (x,Builtin t) | (x,Builtin t) <- builtin ] warns = iWarnings i bs <- gets stBuiltinThings reportSLn "import.iface.merge" 10 $ "Merging interface" reportSLn "import.iface.merge" 20 $ " Current builtins " ++ show (Map.keys bs) ++ "\n" ++ " New builtins " ++ show (Map.keys bi) let check b = case (b1, b2) of (Builtin x, Builtin y) | x == y -> return () | otherwise -> typeError $ DuplicateBuiltinBinding b x y _ -> __IMPOSSIBLE__ where Just b1 = Map.lookup b bs Just b2 = Map.lookup b bi mapM_ check (map fst $ Map.toList $ Map.intersection bs bi) addImportedThings sig bi (iPatternSyns i) (iDisplayForms i) warns reportSLn "import.iface.merge" 20 $ " Rebinding primitives " ++ show prim mapM_ rebind prim where rebind (x, q) = do PrimImpl _ pf <- lookupPrimitiveFunction x stImportedBuiltins %= Map.insert x (Prim pf{ primFunName = q }) addImportedThings :: Signature -> BuiltinThings PrimFun -> A.PatternSynDefns -> DisplayForms -> [TCWarning] -> TCM () addImportedThings isig ibuiltin patsyns display warnings = do stImports %= \ imp -> unionSignatures [imp, isig] stImportedBuiltins %= \ imp -> Map.union imp ibuiltin stPatternSynImports %= \ imp -> Map.union imp patsyns stImportedDisplayForms %= \ imp -> HMap.unionWith (++) imp display stTCWarnings %= \ imp -> List.union imp warnings addImportedInstances isig -- | Scope checks the given module. A proper version of the module -- name (with correct definition sites) is returned. scopeCheckImport :: ModuleName -> TCM (ModuleName, Map ModuleName Scope) scopeCheckImport x = do reportSLn "import.scope" 5 $ "Scope checking " ++ prettyShow x verboseS "import.scope" 10 $ do visited <- Map.keys <$> getVisitedModules reportSLn "import.scope" 10 $ " visited: " ++ List.intercalate ", " (map prettyShow visited) -- Since scopeCheckImport is called from the scope checker, -- we need to reimburse her account. i <- Bench.billTo [] $ getInterface x addImport x -- let s = publicModules $ iInsideScope i let s = iScope i return (iModuleName i `withRangesOfQ` mnameToConcrete x, s) data MaybeWarnings' a = NoWarnings | SomeWarnings a deriving (Functor, Foldable, Traversable) type MaybeWarnings = MaybeWarnings' [TCWarning] applyFlagsToMaybeWarnings :: IgnoreFlags -> MaybeWarnings -> TCM MaybeWarnings applyFlagsToMaybeWarnings r mw = do w' <- traverse (applyFlagsToTCWarnings r) mw return $ if null w' then NoWarnings else w' instance Null a => Null (MaybeWarnings' a) where empty = NoWarnings null mws = case mws of NoWarnings -> True SomeWarnings ws -> null ws hasWarnings :: MaybeWarnings -> Bool hasWarnings = not . null -- | If the module has already been visited (without warnings), then -- its interface is returned directly. Otherwise the computation is -- used to find the interface and the computed interface is stored for -- potential later use (unless the 'MainInterface' is @'MainInterface' -- 'ScopeCheck'@). alreadyVisited :: C.TopLevelModuleName -> MainInterface -> TCM (Interface, MaybeWarnings) -> TCM (Interface, MaybeWarnings) alreadyVisited x isMain getIface = do mm <- getVisitedModule x case mm of -- A module with warnings should never be allowed to be -- imported from another module. Just mi | not (miWarnings mi) -> do reportSLn "import.visit" 10 $ " Already visited " ++ prettyShow x return (miInterface mi, NoWarnings) _ -> do reportSLn "import.visit" 5 $ " Getting interface for " ++ prettyShow x r@(i, wt) <- getIface reportSLn "import.visit" 5 $ " Now we've looked at " ++ prettyShow x unless (isMain == MainInterface ScopeCheck) $ visitModule $ ModuleInfo { miInterface = i , miWarnings = hasWarnings wt } return r -- | Type checks the main file of the interaction. -- This could be the file loaded in the interacting editor (emacs), -- or the file passed on the command line. -- -- First, the primitive modules are imported. -- Then, @getInterface'@ is called to do the main work. -- -- If the 'Mode' is 'ScopeCheck', then type-checking is not -- performed, only scope-checking. (This may include type-checking -- of imported modules.) In this case the generated, partial -- interface is not stored in the state ('stDecodedModules'). Note, -- however, that if the file has already been type-checked, then a -- complete interface is returned. typeCheckMain :: AbsolutePath -> Mode -> TCM (Interface, MaybeWarnings) typeCheckMain f mode = do -- liftIO $ putStrLn $ "This is typeCheckMain " ++ prettyShow f -- liftIO . putStrLn . show =<< getVerbosity reportSLn "import.main" 10 $ "Importing the primitive modules." libdir <- liftIO defaultLibDir reportSLn "import.main" 20 $ "Library dir = " ++ show libdir -- To allow posulating the built-ins, check the primitive module -- in unsafe mode _ <- bracket_ (gets $ Lens.getSafeMode) Lens.putSafeMode $ do Lens.putSafeMode False -- Turn off import-chasing messages. -- We have to modify the persistent verbosity setting, since -- getInterface resets the current verbosity settings to the persistent ones. bracket_ (gets $ Lens.getPersistentVerbosity) Lens.putPersistentVerbosity $ do Lens.modifyPersistentVerbosity (Trie.delete []) -- set root verbosity to 0 -- We don't want to generate highlighting information for Agda.Primitive. withHighlightingLevel None $ getInterface_ =<< do moduleName $ mkAbsolute $ libdir "prim" "Agda" "Primitive.agda" reportSLn "import.main" 10 $ "Done importing the primitive modules." -- Now do the type checking via getInterface. m <- moduleName f getInterface' m (MainInterface mode) -- | Tries to return the interface associated to the given (imported) module. -- The time stamp of the relevant interface file is also returned. -- Calls itself recursively for the imports of the given module. -- May type check the module. -- An error is raised if a warning is encountered. -- -- Do not use this for the main file, use 'typeCheckMain' instead. getInterface :: ModuleName -> TCM Interface getInterface = getInterface_ . toTopLevelModuleName -- | See 'getInterface'. getInterface_ :: C.TopLevelModuleName -> TCM Interface getInterface_ x = do (i, wt) <- getInterface' x NotMainInterface case wt of SomeWarnings w -> tcWarningsToError (filter (notIM . tcWarning) w) NoWarnings -> return i -- filter out unsolved interaction points for imported module so -- that we get the right error message (see test case Fail/Issue1296) where notIM UnsolvedInteractionMetas{} = False notIM _ = True -- | A more precise variant of 'getInterface'. If warnings are -- encountered then they are returned instead of being turned into -- errors. getInterface' :: C.TopLevelModuleName -> MainInterface -> TCM (Interface, MaybeWarnings) getInterface' x isMain = do withIncreasedModuleNestingLevel $ do -- Preserve the pragma options unless we are checking the main -- interface. bracket_ (use stPragmaOptions) (unless (includeStateChanges isMain) . setPragmaOptions) $ do -- Forget the pragma options (locally). setCommandLineOptions . stPersistentOptions . stPersistentState =<< get alreadyVisited x isMain $ addImportCycleCheck x $ do file <- findFile x -- requires source to exist reportSLn "import.iface" 10 $ " Check for cycle" checkForImportCycle uptodate <- Bench.billTo [Bench.Import] $ do ignore <- ignoreInterfaces cached <- runMaybeT $ isCached x file -- If it's cached ignoreInterfaces has no effect; -- to avoid typechecking a file more than once. sourceH <- liftIO $ hashFile file ifaceH <- case cached of Nothing -> fmap fst <$> getInterfaceFileHashes (filePath $ toIFile file) Just i -> return $ Just $ iSourceHash i let unchanged = Just sourceH == ifaceH return $ unchanged && (not ignore || isJust cached) reportSLn "import.iface" 5 $ " " ++ prettyShow x ++ " is " ++ (if uptodate then "" else "not ") ++ "up-to-date." (stateChangesIncluded, (i, wt)) <- do -- -- Andreas, 2014-10-20 AIM XX: -- -- Always retype-check the main file to get the iInsideScope -- -- which is no longer serialized. -- let maySkip = isMain == NotMainInterface -- Andreas, 2015-07-13: Serialize iInsideScope again. let maySkip = True if uptodate && maySkip then getStoredInterface x file isMain else typeCheck x file isMain -- Ensure that the given module name matches the one in the file. let topLevelName = toTopLevelModuleName $ iModuleName i unless (topLevelName == x) $ do -- Andreas, 2014-03-27 This check is now done in the scope checker. -- checkModuleName topLevelName file typeError $ OverlappingProjects file topLevelName x visited <- isVisited x reportSLn "import.iface" 5 $ if visited then " We've been here. Don't merge." else " New module. Let's check it out." unless (visited || stateChangesIncluded) $ do mergeInterface i Bench.billTo [Bench.Highlighting] $ ifTopLevelAndHighlightingLevelIs NonInteractive $ highlightFromInterface i file stCurrentModule .= Just (iModuleName i) -- Interfaces are not stored if we are only scope-checking, or -- if any warnings were encountered. case (isMain, wt) of (MainInterface ScopeCheck, _) -> return () (_, SomeWarnings w) -> return () _ -> storeDecodedModule i return (i, wt) -- | Check whether interface file exists and is in cache -- in the correct version (as testified by the interface file hash). isCached :: C.TopLevelModuleName -- ^ Module name of file we process. -> AbsolutePath -- ^ File we process. -> MaybeT TCM Interface isCached x file = do let ifile = filePath $ toIFile file -- Make sure the file exists in the case sensitive spelling. guardM $ liftIO $ doesFileExistCaseSensitive ifile -- Check that we have cached the module. mi <- MaybeT $ getDecodedModule x -- Check that the interface file exists and return its hash. h <- MaybeT $ fmap snd <$> getInterfaceFileHashes ifile -- Make sure the hashes match. guard $ iFullHash mi == h return mi -- | Try to get the interface from interface file or cache. getStoredInterface :: C.TopLevelModuleName -- ^ Module name of file we process. -> AbsolutePath -- ^ File we process. -> MainInterface -> TCM (Bool, (Interface, MaybeWarnings)) -- ^ @Bool@ is: do we have to merge the interface? getStoredInterface x file isMain = do -- If something goes wrong (interface outdated etc.) -- we revert to fresh type checking. let fallback = typeCheck x file isMain -- Examine the hash of the interface file. If it is different from the -- stored version (in stDecodedModules), or if there is no stored version, -- read and decode it. Otherwise use the stored version. let ifile = filePath $ toIFile file h <- fmap snd <$> getInterfaceFileHashes ifile mm <- getDecodedModule x (cached, mi) <- Bench.billTo [Bench.Deserialization] $ case mm of Just mi -> if Just (iFullHash mi) /= h then do dropDecodedModule x reportSLn "import.iface" 50 $ " cached hash = " ++ show (iFullHash mi) reportSLn "import.iface" 50 $ " stored hash = " ++ show h reportSLn "import.iface" 5 $ " file is newer, re-reading " ++ ifile (False,) <$> readInterface ifile else do reportSLn "import.iface" 5 $ " using stored version of " ++ ifile return (True, Just mi) Nothing -> do reportSLn "import.iface" 5 $ " no stored version, reading " ++ ifile (False,) <$> readInterface ifile -- Check that it's the right version case mi of Nothing -> do reportSLn "import.iface" 5 $ " bad interface, re-type checking" fallback Just i -> do reportSLn "import.iface" 5 $ " imports: " ++ show (iImportedModules i) hs <- map iFullHash <$> mapM getInterface (map fst $ iImportedModules i) -- If any of the imports are newer we need to retype check if hs /= map snd (iImportedModules i) then do -- liftIO close -- Close the interface file. See above. fallback else do unless cached $ do chaseMsg "Loading " x $ Just ifile -- print imported warnings let ws = filter ((Strict.Just file ==) . tcWarningOrigin) (iWarnings i) unless (null ws) $ reportSDoc "warning" 1 $ P.vcat $ P.prettyTCM <$> ws -- We set the pragma options of the skipped file here, -- because if the top-level file is skipped we want the -- pragmas to apply to interactive commands in the UI. mapM_ setOptionsFromPragma (iPragmaOptions i) return (False, (i, NoWarnings)) -- | Run the type checker on a file and create an interface. -- -- Mostly, this function calls 'createInterface'. -- But if it is not the main module we check, -- we do it in a fresh state, suitably initialize, -- in order to forget some state changes after successful type checking. typeCheck :: C.TopLevelModuleName -- ^ Module name of file we process. -> AbsolutePath -- ^ File we process. -> MainInterface -> TCM (Bool, (Interface, MaybeWarnings)) -- ^ @Bool@ is: do we have to merge the interface? typeCheck x file isMain = do unless (includeStateChanges isMain) cleanCachedLog let checkMsg = case isMain of MainInterface ScopeCheck -> "Reading " _ -> "Checking" withMsgs = bracket_ (chaseMsg checkMsg x $ Just $ filePath file) (const $ do ws <- getAllWarnings' AllWarnings RespectFlags let (we, wa) = classifyWarnings ws let wa' = filter ((Strict.Just file ==) . tcWarningOrigin) wa unless (null wa') $ reportSDoc "warning" 1 $ P.vcat $ P.prettyTCM <$> wa' when (null we) $ chaseMsg "Finished" x Nothing) -- Do the type checking. case isMain of MainInterface _ -> do r <- withMsgs $ createInterface file x isMain -- Merge the signature with the signature for imported -- things. reportSLn "import.iface" 40 $ "Merging with state changes included." sig <- getSignature patsyns <- getPatternSyns display <- use stImportsDisplayForms addImportedThings sig Map.empty patsyns display [] setSignature emptySignature setPatternSyns Map.empty return (True, r) NotMainInterface -> do ms <- getImportPath nesting <- asks envModuleNestingLevel range <- asks envRange call <- asks envCall mf <- use stModuleToSource vs <- getVisitedModules ds <- getDecodedModules opts <- stPersistentOptions . stPersistentState <$> get isig <- use stImports ibuiltin <- use stImportedBuiltins display <- use stImportsDisplayForms ipatsyns <- getPatternSynImports ho <- getInteractionOutputCallback -- Every interface is treated in isolation. Note: Some changes to -- the persistent state may not be preserved if an error other -- than a type error or an IO exception is encountered in an -- imported module. r <- noCacheForImportedModule $ freshTCM $ withImportPath ms $ local (\e -> e { envModuleNestingLevel = nesting -- Andreas, 2014-08-18: -- Preserve the range of import statement -- for reporting termination errors in -- imported modules: , envRange = range , envCall = call }) $ do setDecodedModules ds setCommandLineOptions opts setInteractionOutputCallback ho stModuleToSource .= mf setVisitedModules vs addImportedThings isig ibuiltin ipatsyns display [] r <- withMsgs $ createInterface file x isMain mf <- use stModuleToSource ds <- getDecodedModules return (r, do stModuleToSource .= mf setDecodedModules ds case r of (i, NoWarnings) -> storeDecodedModule i _ -> return () ) case r of Left err -> throwError err Right (r, update) -> do update case r of (_, NoWarnings) -> -- We skip the file which has just been type-checked to -- be able to forget some of the local state from -- checking the module. -- Note that this doesn't actually read the interface -- file, only the cached interface. (This comment is not -- correct, see -- test/Fail/customised/NestedProjectRoots.err.) getStoredInterface x file isMain _ -> return (False, r) -- | Formats and outputs the "Checking", "Finished" and "Loading " messages. chaseMsg :: String -- ^ The prefix, like @Checking@, @Finished@, @Loading @. -> C.TopLevelModuleName -- ^ The module name. -> Maybe String -- ^ Optionally: the file name. -> TCM () chaseMsg kind x file = do indentation <- (`replicate` ' ') <$> asks envModuleNestingLevel let maybeFile = caseMaybe file "." $ \ f -> " (" ++ f ++ ")." reportSLn "import.chase" 1 $ concat $ [ indentation, kind, " ", prettyShow x, maybeFile ] -- | Print the highlighting information contained in the given interface. highlightFromInterface :: Interface -> AbsolutePath -- ^ The corresponding file. -> TCM () highlightFromInterface i file = do reportSLn "import.iface" 5 $ "Generating syntax info for " ++ filePath file ++ " (read from interface)." printHighlightingInfo (iHighlighting i) readInterface :: FilePath -> TCM (Maybe Interface) readInterface file = do -- Decode the interface file (s, close) <- liftIO $ readBinaryFile' file do mi <- liftIO . E.evaluate =<< decodeInterface s -- Close the file. Note -- ⑴ that evaluate ensures that i is evaluated to WHNF (before -- the next IO operation is executed), and -- ⑵ that decode returns Nothing if an error is encountered, -- so it is safe to close the file here. liftIO close return $ constructIScope <$> mi -- Catch exceptions and close `catchError` \e -> liftIO close >> handler e -- Catch exceptions `catchError` handler where handler e = case e of IOException _ _ e -> do reportSLn "" 0 $ "IO exception: " ++ show e return Nothing -- Work-around for file locking bug. -- TODO: What does this refer to? Please -- document. _ -> throwError e -- | Writes the given interface to the given file. writeInterface :: FilePath -> Interface -> TCM () writeInterface file i = do reportSLn "import.iface.write" 5 $ "Writing interface file " ++ file ++ "." -- Andreas, 2015-07-13 -- After QName memoization (AIM XXI), scope serialization might be cheap enough. -- -- Andreas, Makoto, 2014-10-18 AIM XX: -- -- iInsideScope is bloating the interface files, so we do not serialize it? -- i <- return $ -- i { iInsideScope = emptyScopeInfo -- } -- Andreas, 2016-02-02 this causes issue #1804, so don't do it: -- i <- return $ -- i { iInsideScope = removePrivates $ iInsideScope i -- } encodeFile file i reportSLn "import.iface.write" 5 $ "Wrote interface file." reportSLn "import.iface.write" 50 $ " hash = " ++ show (iFullHash i) ++ "" `catchError` \e -> do reportSLn "" 1 $ "Failed to write interface " ++ file ++ "." liftIO $ whenM (doesFileExist file) $ removeFile file throwError e removePrivates :: ScopeInfo -> ScopeInfo removePrivates si = si { scopeModules = restrictPrivate <$> scopeModules si } -- | Tries to type check a module and write out its interface. The -- function only writes out an interface file if it does not encounter -- any warnings. -- -- If appropriate this function writes out syntax highlighting -- information. createInterface :: AbsolutePath -- ^ The file to type check. -> C.TopLevelModuleName -- ^ The expected module name. -> MainInterface -> TCM (Interface, MaybeWarnings) createInterface file mname isMain = Bench.billTo [Bench.TopModule mname] $ local (\e -> e { envCurrentPath = Just file }) $ do modFile <- use stModuleToSource fileTokenInfo <- Bench.billTo [Bench.Highlighting] $ generateTokenInfo file stTokens .= fileTokenInfo reportSLn "import.iface.create" 5 $ "Creating interface for " ++ prettyShow mname ++ "." verboseS "import.iface.create" 10 $ do visited <- Map.keys <$> getVisitedModules reportSLn "import.iface.create" 10 $ " visited: " ++ List.intercalate ", " (map prettyShow visited) -- Parsing. (pragmas, top) <- Bench.billTo [Bench.Parsing] $ runPM $ parseFile' moduleParser file pragmas <- concat <$> concreteToAbstract_ pragmas -- identity for top-level pragmas at the moment let getOptions (A.OptionsPragma opts) = Just opts getOptions _ = Nothing options = catMaybes $ map getOptions pragmas mapM_ setOptionsFromPragma options -- Scope checking. reportSLn "import.iface.create" 7 $ "Starting scope checking." topLevel <- Bench.billTo [Bench.Scoping] $ concreteToAbstract_ (TopLevel file mname top) reportSLn "import.iface.create" 7 $ "Finished scope checking." let ds = topLevelDecls topLevel scope = topLevelScope topLevel -- Highlighting from scope checker. reportSLn "import.iface.create" 7 $ "Starting highlighting from scope." Bench.billTo [Bench.Highlighting] $ do -- Generate and print approximate syntax highlighting info. ifTopLevelAndHighlightingLevelIs NonInteractive $ printHighlightingInfo fileTokenInfo let onlyScope = isMain == MainInterface ScopeCheck ifTopLevelAndHighlightingLevelIsOr NonInteractive onlyScope $ mapM_ (\ d -> generateAndPrintSyntaxInfo d Partial onlyScope) ds reportSLn "import.iface.create" 7 $ "Finished highlighting from scope." -- Type checking. -- invalidate cache if pragmas change, TODO move cachingStarts opts <- use stPragmaOptions me <- readFromCachedLog case me of Just (Pragmas opts', _) | opts == opts' -> return () _ -> do reportSLn "cache" 10 $ "pragma changed: " ++ show (isJust me) cleanCachedLog writeToCurrentLog $ Pragmas opts case isMain of MainInterface ScopeCheck -> do reportSLn "import.iface.create" 7 $ "Skipping type checking." cacheCurrentLog _ -> do reportSLn "import.iface.create" 7 $ "Starting type checking." Bench.billTo [Bench.Typing] $ mapM_ checkDeclCached ds `finally_` cacheCurrentLog reportSLn "import.iface.create" 7 $ "Finished type checking." -- Ulf, 2013-11-09: Since we're rethrowing the error, leave it up to the -- code that handles that error to reset the state. -- Ulf, 2013-11-13: Errors are now caught and highlighted in InteractionTop. -- catchError_ (checkDecls ds) $ \e -> do -- ifTopLevelAndHighlightingLevelIs NonInteractive $ -- printErrorInfo e -- throwError e unfreezeMetas -- Profiling: Count number of metas. verboseS "profile.metas" 10 $ do MetaId n <- fresh tickN "metas" (fromIntegral n) -- Highlighting from type checker. reportSLn "import.iface.create" 7 $ "Starting highlighting from type info." Bench.billTo [Bench.Highlighting] $ do -- Move any remaining token highlighting to stSyntaxInfo. toks <- use stTokens ifTopLevelAndHighlightingLevelIs NonInteractive $ printHighlightingInfo toks stTokens .= mempty stSyntaxInfo %= \inf -> inf `mappend` toks whenM (optGenerateVimFile <$> commandLineOptions) $ -- Generate Vim file. withScope_ scope $ generateVimFile $ filePath file reportSLn "import.iface.create" 7 $ "Finished highlighting from type info." setScope scope reportSLn "scope.top" 50 $ "SCOPE " ++ show scope -- TODO: It would be nice if unsolved things were highlighted -- after every mutual block. openMetas <- getOpenMetas unless (null openMetas) $ do reportSLn "import.metas" 10 "We have unsolved metas." reportSLn "import.metas" 10 . unlines =<< showOpenMetas ifTopLevelAndHighlightingLevelIs NonInteractive $ printUnsolvedInfo -- Andreas, 2016-08-03, issue #964 -- When open metas are allowed, -- permanently freeze them now by turning them into postulates. -- This will enable serialization. -- savedMetaStore <- use stMetaStore unless (includeStateChanges isMain) $ whenM (optAllowUnsolved <$> pragmaOptions) $ do withCurrentModule (scopeCurrent scope) $ openMetasToPostulates -- Clear constraints as they might refer to what -- they think are open metas. stAwakeConstraints .= [] stSleepingConstraints .= [] -- Serialization. reportSLn "import.iface.create" 7 $ "Starting serialization." syntaxInfo <- use stSyntaxInfo i <- Bench.billTo [Bench.Serialization, Bench.BuildInterface] $ do buildInterface file topLevel syntaxInfo options reportSLn "tc.top" 101 $ unlines $ "Signature:" : [ unlines [ prettyShow x , " type: " ++ show (defType def) , " def: " ++ show cc ] | (x, def) <- HMap.toList $ iSignature i ^. sigDefinitions, Function{ funCompiled = cc } <- [theDef def] ] reportSLn "import.iface.create" 7 $ "Finished serialization." mallWarnings <- getAllWarnings ErrorWarnings $ case isMain of MainInterface _ -> IgnoreFlags NotMainInterface -> RespectFlags reportSLn "import.iface.create" 7 $ "Considering writing to interface file." case (mallWarnings, isMain) of (SomeWarnings allWarnings, _) -> return () (_, MainInterface ScopeCheck) -> return () _ -> Bench.billTo [Bench.Serialization] $ do -- The file was successfully type-checked (and no warnings were -- encountered), so the interface should be written out. let ifile = filePath $ toIFile file writeInterface ifile i reportSLn "import.iface.create" 7 $ "Finished (or skipped) writing to interface file." -- -- Restore the open metas, as we might continue in interaction mode. -- Actually, we do not serialize the metas if checking the MainInterface -- stMetaStore .= savedMetaStore -- Profiling: Print statistics. printStatistics 30 (Just mname) =<< getStatistics -- Get the statistics of the current module -- and add it to the accumulated statistics. localStatistics <- getStatistics lensAccumStatistics %= Map.unionWith (+) localStatistics verboseS "profile" 1 $ do reportSLn "import.iface" 5 $ "Accumulated statistics." return $ first constructIScope (i, mallWarnings) -- | Collect all warnings that have accumulated in the state. -- Depending on the argument, we either respect the flags passed -- in by the user, or not (for instance when deciding if we are -- writing an interface file or not) getAllWarnings' :: WhichWarnings -> IgnoreFlags -> TCM [TCWarning] getAllWarnings' ww ifs = do openMetas <- getOpenMetas interactionMetas <- getInteractionMetas let getUniqueMetas = fmap List.nub . mapM getMetaRange unsolvedInteractions <- getUniqueMetas interactionMetas unsolvedMetas <- getUniqueMetas (openMetas List.\\ interactionMetas) unsolvedConstraints <- getAllConstraints collectedTCWarnings <- use stTCWarnings unsolved <- mapM warning_ [ UnsolvedInteractionMetas unsolvedInteractions , UnsolvedMetaVariables unsolvedMetas , UnsolvedConstraints unsolvedConstraints ] fmap (filter ((<= ww) . classifyWarning . tcWarning)) $ applyFlagsToTCWarnings ifs $ reverse $ unsolved ++ collectedTCWarnings getAllWarnings :: WhichWarnings -> IgnoreFlags -> TCM MaybeWarnings getAllWarnings ww ifs = do allWarnings <- getAllWarnings' ww ifs return $ if null allWarnings -- Andreas, issue 964: not checking null interactionPoints -- anymore; we want to serialize with open interaction points now! then NoWarnings else SomeWarnings allWarnings errorWarningsOfTCErr :: TCErr -> TCM [TCWarning] errorWarningsOfTCErr err = case err of TypeError tcst cls -> case clValue cls of NonFatalErrors{} -> return [] _ -> localState $ do put tcst ws <- getAllWarnings' ErrorWarnings RespectFlags -- We filter out the unsolved(Metas/Constraints) to stay -- true to the previous error messages. return $ filter (not . isUnsolvedWarning . tcWarning) ws _ -> return [] -- | Reconstruct the 'iScope' (not serialized) -- from the 'iInsideScope' (serialized). constructIScope :: Interface -> Interface constructIScope i = billToPure [ Deserialization ] $ i{ iScope = publicModules $ iInsideScope i } -- | Builds an interface for the current module, which should already -- have been successfully type checked. buildInterface :: AbsolutePath -> TopLevelInfo -- ^ 'TopLevelInfo' for the current module. -> HighlightingInfo -- ^ Syntax highlighting info for the module. -> [OptionsPragma] -- ^ Options set in @OPTIONS@ pragmas. -> TCM Interface buildInterface file topLevel syntaxInfo pragmas = do reportSLn "import.iface" 5 "Building interface..." let m = topLevelModuleName topLevel scope' <- getScope let scope = scope' { scopeCurrent = m } -- Andreas, 2014-05-03: killRange did not result in significant reduction -- of .agdai file size, and lost a few seconds performance on library-test. -- Andreas, Makoto, 2014-10-18 AIM XX: repeating the experiment -- with discarding also the nameBindingSite in QName: -- Saves 10% on serialization time (and file size)! -- -- NOTE: We no longer discard all nameBindingSites (but the commit -- that introduced this change seems to have made Agda a bit -- faster and interface file sizes a bit smaller, at least for the -- standard library). builtin <- use stLocalBuiltins ms <- getImports mhs <- mapM (\ m -> (m,) <$> moduleHash m) $ Set.toList ms foreignCode <- use stForeignCode -- Ulf, 2016-04-12: -- Non-closed display forms are not applicable outside the module anyway, -- and should be dead-code eliminated (#1928). display <- HMap.filter (not . null) . HMap.map (filter isGlobal) <$> use stImportsDisplayForms -- TODO: Kill some ranges? (display, sig) <- eliminateDeadCode display =<< getSignature -- Andreas, 2015-02-09 kill ranges in pattern synonyms before -- serialization to avoid error locations pointing to external files -- when expanding a pattern synoym. patsyns <- killRange <$> getPatternSyns h <- liftIO $ hashFile file let builtin' = Map.mapWithKey (\ x b -> (x,) . primFunName <$> b) builtin warnings <- getAllWarnings' AllWarnings RespectFlags reportSLn "import.iface" 7 " instantiating all meta variables" i <- instantiateFull $ Interface { iSourceHash = h , iImportedModules = mhs , iModuleName = m , iScope = empty -- publicModules scope , iInsideScope = topLevelScope topLevel , iSignature = sig , iDisplayForms = display , iBuiltin = builtin' , iForeignCode = foreignCode , iHighlighting = syntaxInfo , iPragmaOptions = pragmas , iPatternSyns = patsyns , iWarnings = warnings } reportSLn "import.iface" 7 " interface complete" return i -- | Returns (iSourceHash, iFullHash) getInterfaceFileHashes :: FilePath -> TCM (Maybe (Hash, Hash)) getInterfaceFileHashes ifile = do exist <- liftIO $ doesFileExist ifile if not exist then return Nothing else do (s, close) <- liftIO $ readBinaryFile' ifile let hs = decodeHashes s liftIO $ maybe 0 (uncurry (+)) hs `seq` close return hs moduleHash :: ModuleName -> TCM Hash moduleHash m = iFullHash <$> getInterface m -- | True if the first file is newer than the second file. If a file doesn't -- exist it is considered to be infinitely old. isNewerThan :: FilePath -> FilePath -> IO Bool isNewerThan new old = do newExist <- doesFileExist new oldExist <- doesFileExist old if not (newExist && oldExist) then return newExist else do newT <- getModificationTime new oldT <- getModificationTime old return $ newT >= oldT Agda-2.5.3/src/full/Agda/Interaction/Library/0000755000000000000000000000000013154613124017013 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Interaction/Library/Base.hs0000644000000000000000000000150013154613124020215 0ustar0000000000000000-- | Basic data types for library management. module Agda.Interaction.Library.Base where -- | A symbolic library name. -- type LibName = String -- | The special name @\".\"@ is used to indicated that the current directory -- should count as a project root. -- libNameForCurrentDir :: LibName libNameForCurrentDir = "." -- | Content of a @.agda-lib@ file. -- data AgdaLibFile = AgdaLib { libName :: LibName -- ^ The symbolic name of the library. , libFile :: FilePath -- ^ Path to this @.agda-lib@ file (not content of the file). , libIncludes :: [FilePath] -- ^ Roots where to look for the modules of the library. , libDepends :: [LibName] -- ^ Dependencies. } deriving (Show) emptyLibFile :: AgdaLibFile emptyLibFile = AgdaLib { libName = "", libFile = "", libIncludes = [], libDepends = [] } Agda-2.5.3/src/full/Agda/Interaction/Library/Parse.hs0000644000000000000000000001664613154613124020436 0ustar0000000000000000-- | Parser for @.agda-lib@ files. -- -- Example file: -- -- @ -- name: Main -- depend: -- standard-library -- include: . -- src more-src -- @ -- -- Should parse as: -- -- @ -- AgdaLib -- { libName = "Main" -- , libFile = path_to_this_file -- , libIncludes = [ "." , "src" , "more-src" ] -- , libDepends = [ "standard-library" ] -- } -- @ -- module Agda.Interaction.Library.Parse ( parseLibFile, splitCommas, trimLineComment, LineNumber ) where import Control.Applicative import Control.Exception import Control.Monad import Data.Char import qualified Data.List as List import System.FilePath import Agda.Interaction.Library.Base import Agda.Utils.Except ( MonadError(throwError) ) import Agda.Utils.IO ( catchIO ) import Agda.Utils.String ( ltrim ) -- | Parser monad: Can throw @String@ error messages. -- type P = Either String -- | The config files we parse have the generic structure of a sequence -- of @field : content@ entries. type GenericFile = [GenericEntry] data GenericEntry = GenericEntry { geHeader :: String -- ^ E.g. field name. @trim@med. , geContent :: [String] -- ^ E.g. field content. @trim@med. } -- | Library file field format format [sic!]. data Field = forall a. Field { fName :: String -- ^ Name of the field. , fOptional :: Bool -- ^ Is it optional? , fParse :: [String] -> P a -- ^ Content parser for this field. , fSet :: a -> AgdaLibFile -> AgdaLibFile -- ^ Sets parsed content in 'AgdaLibFile' structure. } -- | @.agda-lib@ file format with parsers and setters. agdaLibFields :: [Field] agdaLibFields = -- Andreas, 2017-08-23, issue #2708, field "name" is optional. [ Field "name" True parseName $ \ name l -> l { libName = name } , Field "include" True (pure . concatMap words) $ \ inc l -> l { libIncludes = inc } , Field "depend" True (pure . concatMap splitCommas) $ \ ds l -> l { libDepends = ds } ] where parseName [s] | [name] <- words s = pure name parseName ls = throwError $ "Bad library name: '" ++ unwords ls ++ "'" -- | Parse @.agda-lib@ file. -- -- Sets 'libFile' name and turn mentioned include directories into absolute pathes -- (provided the given 'FilePath' is absolute). -- parseLibFile :: FilePath -> IO (P AgdaLibFile) parseLibFile file = (fmap setPath . parseLib <$> readFile file) `catchIO` \e -> return (Left $ "Failed to read library file " ++ file ++ ".\nReason: " ++ show e) where setPath lib = unrelativise (takeDirectory file) lib{ libFile = file } unrelativise dir lib = lib { libIncludes = map (dir ) (libIncludes lib) } -- | Parse file contents. parseLib :: String -> P AgdaLibFile parseLib s = fromGeneric =<< parseGeneric s -- | Parse 'GenericFile' with 'agdaLibFields' descriptors. fromGeneric :: GenericFile -> P AgdaLibFile fromGeneric = fromGeneric' agdaLibFields -- | Given a list of 'Field' descriptors (with their custom parsers), -- parse a 'GenericFile' into the 'AgdaLibFile' structure. -- -- Checks mandatory fields are present; no duplicate fields, no unknown fields. fromGeneric' :: [Field] -> GenericFile -> P AgdaLibFile fromGeneric' fields fs = do checkFields fields (map geHeader fs) foldM upd emptyLibFile fs where upd :: AgdaLibFile -> GenericEntry -> P AgdaLibFile upd l (GenericEntry h cs) = do Field{..} <- findField h fields x <- fParse cs return $ fSet x l -- | Ensure that there are no duplicate fields and no mandatory fields are missing. checkFields :: [Field] -> [String] -> P () checkFields fields fs = do let mandatory = [ fName f | f <- fields, not $ fOptional f ] -- Missing fields. missing = mandatory List.\\ fs -- Duplicate fields. dup = fs List.\\ List.nub fs -- Plural s for error message. s xs = if length xs > 1 then "s" else "" list xs = List.intercalate ", " [ "'" ++ f ++ "'" | f <- xs ] when (not $ null missing) $ throwError $ "Missing field" ++ s missing ++ " " ++ list missing when (not $ null dup) $ throwError $ "Duplicate field" ++ s dup ++ " " ++ list dup -- | Find 'Field' with given 'fName', throw error if unknown. findField :: String -> [Field] -> P Field findField s fs = maybe err return $ List.find ((s ==) . fName) fs where err = throwError $ "Unknown field '" ++ s ++ "'" -- Generic file parser ---------------------------------------------------- -- | Example: -- -- @ -- parseGeneric "name:Main--BLA\ndepend:--BLA\n standard-library--BLA\ninclude : . --BLA\n src more-src \n" -- == Right [("name",["Main"]),("depend",["standard-library"]),("include",[".","src more-src"])] -- @ parseGeneric :: String -> P GenericFile parseGeneric s = groupLines =<< concat <$> mapM (uncurry parseLine) (zip [1..] $ map stripComments $ lines s) type LineNumber = Int -- | Lines with line numbers. data GenericLine = Header LineNumber String -- ^ Header line, like a field name, e.g. "include :". Cannot be indented. -- @String@ is 'trim'med. | Content LineNumber String -- ^ Other line. Must be indented. -- @String@ is 'trim'med. deriving (Show) -- | Parse line into 'Header' and 'Content' components. -- -- Precondition: line comments and trailing whitespace have been stripped away. -- -- Example file: -- -- @ -- name: Main -- depend: -- standard-library -- include: . -- src more-src -- @ -- -- This should give -- -- @ -- [ Header 1 "name" -- , Content 1 "Main" -- , Header 2 "depend" -- , Content 3 "standard-library" -- , Header 4 "include" -- , Content 4 "." -- , Content 5 "src more-src" -- ] -- @ parseLine :: LineNumber -> String -> P [GenericLine] parseLine _ "" = pure [] parseLine l s@(c:_) -- Indented lines are 'Content'. | isSpace c = pure [Content l $ ltrim s] -- Non-indented lines are 'Header'. | otherwise = case break (==':') s of -- Headers are single words followed by a colon. -- Anything after the colon that is not whitespace is 'Content'. (h, ':' : r) -> case words h of [h] -> pure $ [Header l h] ++ [Content l r' | let r' = ltrim r, not (null r')] [] -> throwError $ show l ++ ": Missing field name" hs -> throwError $ show l ++ ": Bad field name " ++ show h _ -> throwError $ show l ++ ": Missing ':' for field " ++ show (ltrim s) -- | Collect 'Header' and subsequent 'Content's into 'GenericEntry'. -- -- Tailing 'Content's? That's an error. -- groupLines :: [GenericLine] -> P GenericFile groupLines [] = pure [] groupLines (Content l c : _) = throwError $ show l ++ ": Missing field" groupLines (Header _ h : ls) = (GenericEntry h [ c | Content _ c <- cs ] :) <$> groupLines ls1 where (cs, ls1) = span isContent ls isContent Content{} = True isContent Header{} = False -- | Remove leading whitespace and line comment. trimLineComment :: String -> String trimLineComment = stripComments . ltrim -- | Break a comma-separated string. Result strings are @trim@med. splitCommas :: String -> [String] splitCommas s = words $ map (\c -> if c == ',' then ' ' else c) s -- | ...and trailing, but not leading, whitespace. stripComments :: String -> String stripComments "" = "" stripComments ('-':'-':_) = "" stripComments (c : s) = cons c (stripComments s) where cons c "" | isSpace c = "" cons c s = c : s Agda-2.5.3/src/full/Agda/Interaction/Highlighting/0000755000000000000000000000000013154613124020014 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Interaction/Highlighting/Generate.hs-boot0000644000000000000000000000032213154613124023040 0ustar0000000000000000module Agda.Interaction.Highlighting.Generate where import Agda.TypeChecking.Monad.Base import Agda.Syntax.Position (Range) highlightAsTypeChecked :: MonadTCM tcm => Range -> Range -> tcm a -> tcm a Agda-2.5.3/src/full/Agda/Interaction/Highlighting/Precise.hs0000644000000000000000000002600613154613124021746 0ustar0000000000000000{-# LANGUAGE DeriveDataTypeable #-} -- | Types used for precise syntax highlighting. module Agda.Interaction.Highlighting.Precise ( -- * Files Aspect(..) , NameKind(..) , OtherAspect(..) , Aspects(..) , DefinitionSite(..) , File(..) , HighlightingInfo -- ** Creation , singleton , several -- ** Merging , merge -- ** Inspection , smallestPos , toMap -- * Compressed files , CompressedFile(..) , compressedFileInvariant , compress , decompress , noHighlightingInRange -- ** Creation , singletonC , severalC , splitAtC , selectC -- ** Inspection , smallestPosC -- ** Merge , mergeC ) where import Agda.Utils.String import Agda.Utils.List import Data.Maybe import qualified Data.List as List import Data.Function import Data.Semigroup import Control.Applicative ((<$>), (<*>)) import Control.Arrow (second) import Control.Monad import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import Data.Typeable (Typeable) import qualified Agda.Syntax.Position as P import qualified Agda.Syntax.Common as Common import qualified Agda.Syntax.Concrete as SC import Agda.Interaction.Highlighting.Range ------------------------------------------------------------------------ -- Files -- | Syntactic aspects of the code. (These cannot overlap.) -- They can be obtained from the lexed tokens already, -- except for the 'NameKind'. data Aspect = Comment | Option | Keyword | String | Number | Symbol -- ^ Symbols like forall, =, ->, etc. | PrimitiveType -- ^ Things like Set and Prop. | Name (Maybe NameKind) Bool -- ^ Is the name an operator part? deriving (Eq, Show, Typeable) -- | @NameKind@s are figured out during scope checking. data NameKind = Bound -- ^ Bound variable. | Constructor Common.Induction -- ^ Inductive or coinductive constructor. | Datatype | Field -- ^ Record field. | Function | Module -- ^ Module name. | Postulate | Primitive -- ^ Primitive. | Record -- ^ Record type. | Argument -- ^ Named argument, like x in {x = v} | Macro -- ^ Macro. deriving (Eq, Show, Typeable) -- | Other aspects, generated by type checking. -- (These can overlap with each other and with 'Aspect's.) data OtherAspect = Error | DottedPattern | UnsolvedMeta | UnsolvedConstraint -- ^ Unsolved constraint not connected to meta-variable. This -- could for instance be an emptyness constraint. | TerminationProblem | PositivityProblem | ReachabilityProblem | CoverageProblem | IncompletePattern -- ^ When this constructor is used it is probably a good idea to -- include a 'note' explaining why the pattern is incomplete. | CatchallClause | TypeChecks -- ^ Code which is being type-checked. deriving (Eq, Show, Enum, Bounded, Typeable) -- | Meta information which can be associated with a -- character\/character range. data Aspects = Aspects { aspect :: Maybe Aspect , otherAspects :: [OtherAspect] , note :: Maybe String -- ^ This note, if present, can be displayed as a tool-tip or -- something like that. It should contain useful information about -- the range (like the module containing a certain identifier, or -- the fixity of an operator). , definitionSite :: Maybe DefinitionSite -- ^ The definition site of the annotated thing, if applicable and -- known. File positions are counted from 1. } deriving (Show, Typeable) data DefinitionSite = DefinitionSite { defSiteModule :: SC.TopLevelModuleName -- ^ The defining module. , defSitePos :: Int -- ^ The file position in that module. , defSiteHere :: Bool -- ^ Has this @DefinitionSite@ been created at the defining site of the name? , defSiteAnchor :: Maybe String -- ^ A pretty name for the HTML linking. } deriving (Show, Typeable) instance Eq DefinitionSite where DefinitionSite m p _ _ == DefinitionSite m' p' _ _ = m == m' && p == p' instance Eq Aspects where Aspects a o _ d == Aspects a' o' _ d' = (a, List.nub o, d) == (a', List.nub o', d') -- | A 'File' is a mapping from file positions to meta information. -- -- The first position in the file has number 1. newtype File = File { mapping :: IntMap Aspects } deriving (Eq, Show, Typeable) -- | Syntax highlighting information. type HighlightingInfo = CompressedFile ------------------------------------------------------------------------ -- Creation -- | @'singleton' rs m@ is a file whose positions are those in @rs@, -- and in which every position is associated with @m@. singleton :: Ranges -> Aspects -> File singleton rs m = File { mapping = IntMap.fromAscList [ (p, m) | p <- rangesToPositions rs ] } -- | Like 'singleton', but with several 'Ranges' instead of only one. several :: [Ranges] -> Aspects -> File several rs m = mconcat $ map (\r -> singleton r m) rs ------------------------------------------------------------------------ -- Merging -- | Merges meta information. mergeAspects :: Aspects -> Aspects -> Aspects mergeAspects m1 m2 = Aspects { aspect = (mplus `on` aspect) m1 m2 , otherAspects = List.nub $ ((++) `on` otherAspects) m1 m2 , note = case (note m1, note m2) of (Just n1, Just n2) -> Just $ if n1 == n2 then n1 else addFinalNewLine n1 ++ "----\n" ++ n2 (Just n1, Nothing) -> Just n1 (Nothing, Just n2) -> Just n2 (Nothing, Nothing) -> Nothing , definitionSite = (mplus `on` definitionSite) m1 m2 } instance Semigroup Aspects where (<>) = mergeAspects instance Monoid Aspects where mempty = Aspects { aspect = Nothing , otherAspects = [] , note = Nothing , definitionSite = Nothing } mappend = (<>) -- | Merges files. merge :: File -> File -> File merge f1 f2 = File { mapping = (IntMap.unionWith mappend `on` mapping) f1 f2 } instance Semigroup File where (<>) = merge instance Monoid File where mempty = File { mapping = IntMap.empty } mappend = (<>) ------------------------------------------------------------------------ -- Inspection -- | Returns the smallest position, if any, in the 'File'. smallestPos :: File -> Maybe Int smallestPos = fmap (fst . fst) . IntMap.minViewWithKey . mapping -- | Convert the 'File' to a map from file positions (counting from 1) -- to meta information. toMap :: File -> IntMap Aspects toMap = mapping ------------------------------------------------------------------------ -- Compressed files -- | A compressed 'File', in which consecutive positions with the same -- 'Aspects' are stored together. newtype CompressedFile = CompressedFile { ranges :: [(Range, Aspects)] } deriving (Eq, Show, Typeable) -- | Invariant for compressed files. -- -- Note that these files are not required to be /maximally/ -- compressed, because ranges are allowed to be empty, and the -- 'Aspects's in adjacent ranges are allowed to be equal. compressedFileInvariant :: CompressedFile -> Bool compressedFileInvariant (CompressedFile []) = True compressedFileInvariant (CompressedFile f) = all rangeInvariant rs && all (not . empty) rs && and (zipWith (<=) (map to $ init rs) (map from $ tail rs)) where rs = map fst f -- | Compresses a file by merging consecutive positions with equal -- meta information into longer ranges. compress :: File -> CompressedFile compress f = CompressedFile $ map join $ groupBy' p (IntMap.toAscList $ mapping f) where p (pos1, m1) (pos2, m2) = pos2 == pos1 + 1 && m1 == m2 join pms = ( Range { from = head ps, to = last ps + 1 } , head ms ) where (ps, ms) = unzip pms -- | Decompresses a compressed file. decompress :: CompressedFile -> File decompress = File . IntMap.fromList . concat . map (\(r, m) -> [ (p, m) | p <- rangeToPositions r ]) . ranges -- | Clear any highlighting info for the given ranges. Used to make sure -- unsolved meta highlighting overrides error highlighting. noHighlightingInRange :: Ranges -> CompressedFile -> CompressedFile noHighlightingInRange rs (CompressedFile hs) = CompressedFile $ concatMap clear hs where clear (r, i) = case minus (Ranges [r]) rs of Ranges [] -> [] Ranges rs -> [ (r, i) | r <- rs ] ------------------------------------------------------------------------ -- Operations that work directly with compressed files -- | @'singletonC' rs m@ is a file whose positions are those in @rs@, -- and in which every position is associated with @m@. singletonC :: Ranges -> Aspects -> CompressedFile singletonC (Ranges rs) m = CompressedFile [(r, m) | r <- rs, not (empty r)] -- | Like 'singletonR', but with a list of 'Ranges' instead of a -- single one. severalC :: [Ranges] -> Aspects -> CompressedFile severalC rss m = mconcat $ map (\rs -> singletonC rs m) rss -- | Merges compressed files. mergeC :: CompressedFile -> CompressedFile -> CompressedFile mergeC (CompressedFile f1) (CompressedFile f2) = CompressedFile (mrg f1 f2) where mrg [] f2 = f2 mrg f1 [] = f1 mrg (p1@(i1,_):f1) (p2@(i2,_):f2) | to i1 <= from i2 = p1 : mrg f1 (p2:f2) | to i2 <= from i1 = p2 : mrg (p1:f1) f2 | to i1 <= to i2 = ps1 ++ mrg f1 (ps2 ++ f2) | otherwise = ps1 ++ mrg (ps2 ++ f1) f2 where (ps1, ps2) = fuse p1 p2 -- Precondition: The ranges are overlapping. fuse (i1, m1) (i2, m2) = ( fix [ (Range { from = a, to = b }, ma) , (Range { from = b, to = c }, mergeAspects m1 m2) ] , fix [ (Range { from = c, to = d }, md) ] ) where [(a, ma), (b, _), (c, _), (d, md)] = List.sortBy (compare `on` fst) [(from i1, m1), (to i1, m1), (from i2, m2), (to i2, m2)] fix = filter (not . empty . fst) instance Semigroup CompressedFile where (<>) = mergeC instance Monoid CompressedFile where mempty = CompressedFile [] mappend = (<>) -- | @splitAtC p f@ splits the compressed file @f@ into @(f1, f2)@, -- where all the positions in @f1@ are @< p@, and all the positions -- in @f2@ are @>= p@. splitAtC :: Int -> CompressedFile -> (CompressedFile, CompressedFile) splitAtC p f = (CompressedFile f1, CompressedFile f2) where (f1, f2) = split $ ranges f split [] = ([], []) split (rx@(r,x) : f) | p <= from r = ([], rx:f) | to r <= p = (rx:f1, f2) | otherwise = ([ (toP, x) ], (fromP, x) : f) where (f1, f2) = split f toP = Range { from = from r, to = p } fromP = Range { from = p, to = to r } selectC :: P.Range -> CompressedFile -> CompressedFile selectC r cf = cf' where empty = (0,0) (from, to) = fromMaybe empty (rangeToEndPoints r) (_, (cf', _)) = (second (splitAtC to)) . splitAtC from $ cf -- | Returns the smallest position, if any, in the 'CompressedFile'. smallestPosC :: CompressedFile -> Maybe Int smallestPosC (CompressedFile []) = Nothing smallestPosC (CompressedFile ((r, _) : _)) = Just (from r) Agda-2.5.3/src/full/Agda/Interaction/Highlighting/Dot.hs0000644000000000000000000000643513154613124021106 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Generate an import dependency graph for a given module. module Agda.Interaction.Highlighting.Dot where import Control.Applicative import Control.Monad.State import qualified Data.Map as M import Data.Map(Map) import Data.Maybe import Data.Monoid import qualified Data.Set as S import Data.Set (Set) import Agda.Interaction.Options import Agda.Syntax.Abstract import Agda.TypeChecking.Monad #include "undefined.h" import Agda.Utils.Impossible -- | Internal module identifiers for construction of dependency graph. type ModuleId = String data DotState = DotState { dsModules :: Map ModuleName ModuleId -- ^ Records already processed modules -- and maps them to an internal identifier. , dsNameSupply :: [ModuleId] -- ^ Supply of internal identifiers. , dsConnection :: Set (ModuleId, ModuleId) -- ^ Edges of dependency graph. } initialDotState :: DotState initialDotState = DotState { dsModules = mempty , dsNameSupply = map (('m':) . show) [0..] , dsConnection = mempty } type DotM = StateT DotState TCM -- | Translate a 'ModuleName' to an internal 'ModuleId'. -- Returns @True@ if the 'ModuleName' is new, i.e., has not been -- encountered before and is thus added to the map of processed modules. addModule :: ModuleName -> DotM (ModuleId, Bool) addModule m = do s <- get case M.lookup m (dsModules s) of Just r -> return (r, False) Nothing -> do let newName:nameSupply = dsNameSupply s put s { dsModules = M.insert m newName (dsModules s) , dsNameSupply = nameSupply } return (newName, True) -- | Add an arc from importer to imported. addConnection :: ModuleId -> ModuleId -> DotM () addConnection m1 m2 = modify $ \s -> s {dsConnection = S.insert (m1,m2) (dsConnection s)} -- | Recursively build import graph, starting from given 'Interface'. -- Modifies the state in 'DotM' and returns the 'ModuleId' of the 'Interface'. dottify :: Interface -> DotM ModuleId dottify inter = do let curModule = iModuleName inter (name, continue) <- addModule curModule -- If we have not visited this interface yet, -- process its imports recursively and -- add them as connections to the graph. when continue $ do importsifs <- lift $ map miInterface . catMaybes <$> mapM (getVisitedModule . toTopLevelModuleName . fst) (iImportedModules inter) imports <- mapM dottify importsifs mapM_ (addConnection name) imports return name -- | Generate a .dot file for the import graph starting with the -- given 'Interface' and write it to the file specified by the -- command line option. generateDot :: Interface -> TCM () generateDot inter = do (top, state) <- flip runStateT initialDotState $ do dottify inter fp <- fromMaybe __IMPOSSIBLE__ . optDependencyGraph <$> commandLineOptions liftIO $ writeFile fp $ mkDot state where mkDot :: DotState -> String mkDot st = unlines $ [ "digraph dependencies {" ] ++ [" " ++ repr ++ "[label=\"" ++ show (mnameToConcrete modulename) ++ "\"];" | (modulename, repr) <- M.toList (dsModules st)] ++ [" " ++ r1 ++ " -> " ++ r2 ++ ";" | (r1 , r2) <- S.toList (dsConnection st) ] ++ ["}"] Agda-2.5.3/src/full/Agda/Interaction/Highlighting/HTML.hs0000644000000000000000000001667113154613124021127 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} -- | Function for generating highlighted, hyperlinked HTML from Agda -- sources. module Agda.Interaction.Highlighting.HTML ( generateHTML -- Reused by PandocAgda , defaultCSSFile , generateHTMLWithPageGen , generatePage , page , tokenStream , code ) where import Prelude hiding ((!!), concatMap) import Control.Applicative import Control.Monad import Control.Monad.Trans import Data.Function import Data.Monoid import Data.Foldable (toList, concatMap) import Data.Maybe import qualified Data.IntMap as IntMap import qualified Data.Map as Map import qualified Data.List as List import Data.Text.Lazy (Text) import qualified Network.URI.Encode import System.FilePath import System.Directory import Text.Blaze.Html5 hiding (code, map, title) import qualified Text.Blaze.Html5 as Html5 import Text.Blaze.Html5.Attributes as Attr import Text.Blaze.Html.Renderer.Text -- The imported operator (!) attaches an Attribute to an Html value -- The defined operator (!!) attaches a list of such Attributes import Paths_Agda import Agda.Interaction.Highlighting.Precise import Agda.Interaction.Options import qualified Agda.Syntax.Concrete as C import Agda.Syntax.Common import Agda.TypeChecking.Monad (TCM) import qualified Agda.TypeChecking.Monad as TCM import Agda.Utils.FileName (filePath) import Agda.Utils.Function import Agda.Utils.Lens import qualified Agda.Utils.IO.UTF8 as UTF8 import Agda.Utils.Pretty hiding ((<>)) import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- | The name of the default CSS file. defaultCSSFile :: FilePath defaultCSSFile = "Agda.css" -- | Generates HTML files from all the sources which have been -- visited during the type checking phase. -- -- This function should only be called after type checking has -- completed successfully. generateHTML :: TCM () generateHTML = generateHTMLWithPageGen pageGen where pageGen :: FilePath -> C.TopLevelModuleName -> CompressedFile -> TCM () pageGen dir mod hinfo = generatePage renderer dir mod where renderer :: FilePath -> FilePath -> String -> Text renderer css _ contents = page css mod $ code $ tokenStream contents hinfo -- | Prepare information for HTML page generation. -- -- The page generator receives the file path of the module, -- the top level module name of the module -- and the highlighting information of the module. generateHTMLWithPageGen :: (FilePath -> C.TopLevelModuleName -> CompressedFile -> TCM ()) -- ^ Page generator -> TCM () generateHTMLWithPageGen generatePage = do options <- TCM.commandLineOptions -- There is a default directory given by 'defaultHTMLDir' let dir = optHTMLDir options liftIO $ createDirectoryIfMissing True dir -- If the default CSS file should be used, then it is copied to -- the output directory. liftIO $ when (isNothing $ optCSSFile options) $ do cssFile <- getDataFileName defaultCSSFile copyFile cssFile (dir defaultCSSFile) TCM.reportSLn "html" 1 $ unlines [ "" , "Warning: HTML is currently generated for ALL files which can be" , "reached from the given module, including library files." ] -- Pull highlighting info from the state and generate all the -- web pages. mapM_ (\(m, h) -> generatePage dir m h) =<< map (mapSnd $ TCM.iHighlighting . TCM.miInterface) . Map.toList <$> TCM.getVisitedModules -- | Converts module names to the corresponding HTML file names. modToFile :: C.TopLevelModuleName -> FilePath modToFile m = Network.URI.Encode.encode $ render (pretty m) <.> "html" -- | Generates a highlighted, hyperlinked version of the given module. generatePage :: (FilePath -> FilePath -> String -> Text) -- ^ Page renderer -> FilePath -- ^ Directory in which to create files. -> C.TopLevelModuleName -- ^ Module to be highlighted. -> TCM () generatePage renderpage dir mod = do f <- fromMaybe __IMPOSSIBLE__ . Map.lookup mod <$> use TCM.stModuleToSource contents <- liftIO $ UTF8.readTextFile $ filePath f css <- fromMaybe defaultCSSFile . optCSSFile <$> TCM.commandLineOptions let html = renderpage css (filePath f) contents TCM.reportSLn "html" 1 $ "Generating HTML for " ++ render (pretty mod) ++ " (" ++ target ++ ")." liftIO $ UTF8.writeTextToFile target html where target = dir modToFile mod -- | Attach multiple Attributes (!!) :: Html -> [Attribute] -> Html h !! as = h ! mconcat as -- | Constructs the web page, including headers. page :: FilePath -- ^ URL to the CSS file. -> C.TopLevelModuleName -- ^ Module to be highlighted. -> Html -> Text page css modName pagecontent = renderHtml $ docTypeHtml $ hdr <> rest where hdr = Html5.head $ mconcat [ meta !! [ charset "utf-8" ] , Html5.title (toHtml $ render $ pretty modName) , link !! [ rel "stylesheet" , href (stringValue css) ] ] rest = body (pre pagecontent) -- | Constructs token stream ready to print. tokenStream :: String -- ^ The contents of the module. -> CompressedFile -- ^ Highlighting information. -> [(Int, String, Aspects)] -- ^ (position, contents, info) tokenStream contents info = map (\cs -> case cs of (mi, (pos, _)) : _ -> (pos, map (snd . snd) cs, fromMaybe mempty mi) [] -> __IMPOSSIBLE__) $ List.groupBy ((==) `on` fst) $ map (\(pos, c) -> (IntMap.lookup pos infoMap, (pos, c))) $ zip [1..] contents where infoMap = toMap (decompress info) -- | Constructs the HTML displaying the code. code :: [(Int, String, Aspects)] -> Html code = mconcat . map mkHtml where mkHtml :: (Int, String, Aspects) -> Html mkHtml (pos, s, mi) = -- Andreas, 2017-06-16, issue #2605: -- Do not create anchors for whitespace. applyUnless (mi == mempty) (annotate pos mi) $ toHtml s annotate :: Int -> Aspects -> Html -> Html annotate pos mi content = a content !! attributes where attributes = concat [ [Attr.id $ stringValue $ applyWhen here anchorName $ show pos ] , toList $ fmap link mDefinitionSite , class_ (stringValue $ unwords classes) <$ guard (not $ null classes) ] classes = concat [ concatMap noteClasses (note mi) , otherAspectClasses (otherAspects mi) , concatMap aspectClasses (aspect mi) ] aspectClasses (Name mKind op) = kindClass ++ opClass where kindClass = toList $ fmap showKind mKind showKind (Constructor Inductive) = "InductiveConstructor" showKind (Constructor CoInductive) = "CoinductiveConstructor" showKind k = show k opClass = if op then ["Operator"] else [] aspectClasses a = [show a] otherAspectClasses = map show -- Notes are not included. noteClasses s = [] mDefinitionSite = definitionSite mi here = maybe False defSiteHere mDefinitionSite anchorName = (`fromMaybe` maybe __IMPOSSIBLE__ defSiteAnchor mDefinitionSite) link (DefinitionSite m pos _here aName) = href $ stringValue $ -- If the definition site points to the top of a file, -- we drop the anchor part and just link to the file. applyUnless (pos <= 1) (++ "#" ++ Network.URI.Encode.encode (fromMaybe (show pos) aName)) (Network.URI.Encode.encode $ modToFile m) Agda-2.5.3/src/full/Agda/Interaction/Highlighting/Vim.hs0000644000000000000000000000544713154613124021115 0ustar0000000000000000-- {-# LANGUAGE CPP #-} module Agda.Interaction.Highlighting.Vim where import Control.Monad.Trans import Data.Function ( on ) import qualified Data.List as List import qualified Data.Map as Map import System.FilePath import Agda.Syntax.Scope.Base import Agda.Syntax.Common import Agda.Syntax.Concrete.Name as CName import Agda.TypeChecking.Monad import qualified Agda.Utils.IO.UTF8 as UTF8 import Agda.Utils.Tuple vimFile :: FilePath -> FilePath vimFile file = case splitFileName file of (path, name) -> path "" <.> name <.> "vim" escape :: String -> String escape = concatMap esc where escchars = "$\\^.*~[]" esc c | c `elem` escchars = ['\\',c] | otherwise = [c] wordBounded :: String -> String wordBounded s0 = concat ["\\<", s0, "\\>"] keyword :: String -> [String] -> String keyword _ [] = "" keyword cat ws = "syn keyword " ++ unwords (cat : ws) match :: String -> [String] -> String match _ [] = "" match cat ws = "syn match " ++ cat ++ " \"" ++ concat (List.intersperse "\\|" $ map (wordBounded . escape) ws) ++ "\"" matches :: [String] -> [String] -> [String] -> [String] -> [String] -> [String] -> [String] matches cons icons defs idefs flds iflds = map snd $ List.sortBy (compare `on` fst) $ cons' ++ defs' ++ icons' ++ idefs' where cons' = foo "agdaConstructor" $ classify length cons icons' = foo "agdaInfixConstructor" $ classify length icons defs' = foo "agdaFunction" $ classify length defs idefs' = foo "agdaInfixFunction" $ classify length idefs flds' = foo "agdaProjection" $ classify length flds iflds' = foo "agdaInfixProjection" $ classify length iflds classify f = List.groupBy ((==) `on` f) . List.sortBy (compare `on` f) foo :: String -> [[String]] -> [(Int, String)] foo cat = map (length . head /\ match cat) toVim :: NamesInScope -> String toVim ns = unlines $ matches mcons micons mdefs midefs mflds miflds where cons = [ x | (x, def:_) <- Map.toList ns, anameKind def == ConName ] defs = [ x | (x, def:_) <- Map.toList ns, anameKind def == DefName ] flds = [ x | (x, fld:_) <- Map.toList ns, anameKind fld == FldName ] mcons = map show cons mdefs = map show defs mflds = map show flds micons = concatMap parts cons midefs = concatMap parts defs miflds = concatMap parts flds parts (NoName _ _) = [] parts (Name _ [_]) = [] parts (Name _ ps) = [ rawNameToString x | Id x <- ps ] generateVimFile :: FilePath -> TCM () generateVimFile file = do scope <- getScope liftIO $ UTF8.writeFile (vimFile file) $ toVim $ names scope where names = nsNames . everythingInScope Agda-2.5.3/src/full/Agda/Interaction/Highlighting/LaTeX.hs0000644000000000000000000005606613154613124021342 0ustar0000000000000000{-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} {-# LANGUAGE ViewPatterns #-} -- | Function for generating highlighted and aligned LaTeX from literate -- Agda source. module Agda.Interaction.Highlighting.LaTeX ( generateLaTeX ) where import Prelude hiding (log) import Data.Char import Data.Maybe import Data.Function import Control.Monad.RWS.Strict import Control.Applicative import Control.Arrow (second) import System.Directory import System.Exit import System.FilePath import System.Process import Data.Text (Text) import qualified Data.Text as T #ifdef COUNT_CLUSTERS import qualified Data.Text.ICU as ICU #endif import qualified Data.Text.IO as T import qualified Data.Text.Lazy as L import qualified Data.Text.Lazy.Encoding as E import qualified Data.ByteString.Lazy as BS import Data.HashSet (HashSet) import qualified Data.HashSet as Set import qualified Data.IntMap as IntMap import qualified Data.List as List import Paths_Agda import Agda.Syntax.Abstract (toTopLevelModuleName) import Agda.Syntax.Common import Agda.Syntax.Concrete (TopLevelModuleName, moduleNameParts, projectRoot) import Agda.Syntax.Parser.Literate (literateTeX, LayerRole, atomizeLayers) import qualified Agda.Syntax.Parser.Literate as L import Agda.Syntax.Position (startPos) import qualified Agda.Interaction.FindFile as Find import Agda.Interaction.Highlighting.Precise import Agda.TypeChecking.Monad (TCM, Interface(..)) import qualified Agda.TypeChecking.Monad as TCM import Agda.Interaction.Options (optGHCiInteraction, optLaTeXDir, optCountClusters) import Agda.Compiler.CallCompiler import qualified Agda.Utils.IO.UTF8 as UTF8 import Agda.Utils.FileName (filePath, AbsolutePath, mkAbsolute) #include "undefined.h" import Agda.Utils.Impossible ------------------------------------------------------------------------ -- * Datatypes. -- | The @LaTeX@ monad is a combination of @ExceptT@, @RWST@ and -- @IO@. The error part is just used to keep track whether we finished -- or not, the reader part isn't used, the writer is where the output -- goes and the state is for keeping track of the tokens and some other -- useful info, and the I/O part is used for printing debugging info. type LaTeX = RWST () [Output] State IO -- | Output items. data Output = Text !Text -- ^ A piece of text. | MaybeColumn !AlignmentColumn -- ^ A column. If it turns out to be an indentation column that is -- not used to indent or align something, then no column will be -- generated, only whitespace ('agdaSpace'). deriving Show -- | Column kinds. data Kind = Indentation -- ^ Used only for indentation (the placement of the first token -- on a line, relative to tokens on previous lines). | Alignment -- ^ Used both for indentation and for alignment. deriving (Eq, Show) -- | Unique identifiers for indentation columns. type IndentationColumnId = Int -- | Alignment and indentation columns. data AlignmentColumn = AlignmentColumn { columnCodeBlock :: !Int -- ^ The column's code block. , columnColumn :: !Int -- ^ The column number. , columnKind :: Maybe IndentationColumnId -- ^ The column kind. 'Nothing' for alignment columns and @'Just' -- i@ for indentation columns, where @i@ is the column's unique -- identifier. } deriving Show data State = State { codeBlock :: !Int -- ^ The number of the current code block. , column :: !Int -- ^ The current column number. , columns :: [AlignmentColumn] -- ^ All alignment columns found on the -- current line (so far), in reverse -- order. , columnsPrev :: [AlignmentColumn] -- ^ All alignment columns found in -- previous lines (in any code block), -- with larger columns coming first. , nextId :: !IndentationColumnId -- ^ The next indentation column -- identifier. , usedColumns :: HashSet IndentationColumnId -- ^ Indentation columns that have -- actually -- been used. , countClusters :: !Bool -- ^ Count extended grapheme clusters? } type Tokens = [Token] data Token = Token { text :: !Text , info :: Aspects } deriving Show withTokenText :: (Text -> Text) -> Token -> Token withTokenText f tok@Token{text = t} = tok{text = f t} data Debug = MoveColumn | NonCode | Code | Spaces | Output deriving (Eq, Show) -- | Says what debug information should printed. debugs :: [Debug] debugs = [] -- | Run function for the @LaTeX@ monad. runLaTeX :: LaTeX a -> () -> State -> IO (a, State, [Output]) runLaTeX = runRWST emptyState :: Bool -- ^ Count extended grapheme clusters? -> State emptyState cc = State { codeBlock = 0 , column = 0 , columns = [] , columnsPrev = [] , nextId = 0 , usedColumns = Set.empty , countClusters = cc } ------------------------------------------------------------------------ -- * Some helpers. -- | Gives the size of the string. If cluster counting is enabled, -- then the number of extended grapheme clusters is computed (the root -- locale is used), and otherwise the number of code points. size :: Text -> LaTeX Int size t = do cc <- countClusters <$> get if cc then #ifdef COUNT_CLUSTERS return $ length $ ICU.breaks (ICU.breakCharacter ICU.Root) t #else __IMPOSSIBLE__ #endif else return $ T.length t (<+>) :: Text -> Text -> Text (<+>) = T.append isInfixOf' :: Text -> Text -> Maybe (Text, Text) isInfixOf' needle haystack = go (T.tails haystack) 0 where go [] !n = Nothing go ((T.stripPrefix needle -> Just suf) : xss) n = Just (T.take n haystack, suf) go (_ : xss) n = go xss (n + 1) -- Same as above, but starts searching from the back rather than the -- front. isInfixOfRev :: Text -> Text -> Maybe (Text, Text) isInfixOfRev needle haystack = case T.reverse needle `isInfixOf'` T.reverse haystack of Nothing -> Nothing Just (pre, suf) -> Just (T.reverse suf, T.reverse pre) -- | Does the string consist solely of whitespace? isSpaces :: Text -> Bool isSpaces = T.all isSpace -- | Is the character a whitespace character distinct from '\n'? isSpaceNotNewline :: Char -> Bool isSpaceNotNewline c = isSpace c && c /= '\n' -- | Replaces all forms of whitespace, except for new-line characters, -- with spaces. replaceSpaces :: Text -> Text replaceSpaces = T.map (\c -> if isSpaceNotNewline c then ' ' else c) -- | Yields the next token, taking special care to begin/end code -- blocks. Junk occuring before and after the code blocks is separated -- into separate tokens, this makes it easier to keep track of whether -- we are in a code block or not. moveColumnForToken :: Token -> LaTeX () moveColumnForToken t = do unless (isSpaces (text t)) $ do log MoveColumn $ text t moveColumn =<< size (text t) -- | Merges 'columns' into 'columnsPrev', resets 'column' and -- 'columns' resetColumn :: LaTeX () resetColumn = modify $ \s -> s { column = 0 , columnsPrev = merge (columns s) (columnsPrev s) , columns = [] } where -- Remove shadowed columns from old. merge [] old = old merge new old = new ++ filter ((< leastNew) . columnColumn) old where leastNew = columnColumn (last new) moveColumn :: Int -> LaTeX () moveColumn i = modify $ \s -> s { column = i + column s } -- | Registers a column of the given kind. The column is returned. registerColumn :: Kind -> LaTeX AlignmentColumn registerColumn kind = do column <- gets column codeBlock <- gets codeBlock kind <- case kind of Alignment -> return Nothing Indentation -> do nextId <- gets nextId modify $ \s -> s { nextId = succ nextId } return (Just nextId) let c = AlignmentColumn { columnColumn = column , columnCodeBlock = codeBlock , columnKind = kind } modify $ \s -> s { columns = c : columns s } return c -- | Registers the given column as used (if it is an indentation -- column). useColumn :: AlignmentColumn -> LaTeX () useColumn c = case columnKind c of Nothing -> return () Just i -> modify $ \s -> s { usedColumns = Set.insert i (usedColumns s) } -- | Alignment column zero in the current code block. columnZero :: LaTeX AlignmentColumn columnZero = do codeBlock <- gets codeBlock return $ AlignmentColumn { columnColumn = 0 , columnCodeBlock = codeBlock , columnKind = Nothing } -- | Registers column zero as an alignment column. registerColumnZero :: LaTeX () registerColumnZero = do c <- columnZero modify $ \s -> s { columns = [c] } -- | Changes to the state that are performed at the start of a code -- block. -- This code is based on the assumption that there are no -- non-whitespace characters following \begin{code}. For occurrences -- of \begin{code} which start a code block this is true. However, the -- LaTeX backend does not identify code blocks correctly, see Issue -- #2400. enterCode :: LaTeX () enterCode = do resetColumn modify $ \s -> s { codeBlock = codeBlock s + 1 } -- | Changes to the state that are performed at the end of a code -- block. leaveCode :: LaTeX () leaveCode = return () logHelper :: Debug -> Text -> [String] -> LaTeX () logHelper debug text extra = when (debug `elem` debugs) $ do lift $ T.putStrLn $ T.pack (show debug ++ ": ") <+> T.pack "'" <+> text <+> T.pack "' " <+> if null extra then T.empty else T.pack "(" <+> T.pack (unwords extra) <+> T.pack ")" log :: Debug -> Text -> LaTeX () log MoveColumn text = do cols <- gets columns logHelper MoveColumn text ["columns=", show cols] log Code text = do cols <- gets columns col <- gets column logHelper Code text ["columns=", show cols, "col=", show col] log debug text = logHelper debug text [] log' :: Debug -> String -> LaTeX () log' d = log d . T.pack output :: Output -> LaTeX () output item = do log' Output (show item) tell [item] ------------------------------------------------------------------------ -- * LaTeX and polytable strings. -- Polytable, http://www.ctan.org/pkg/polytable, is used for code -- alignment, similar to lhs2TeX's approach. nl, beginCode, endCode :: Text nl = T.pack "%\n" beginCode = T.pack "\\begin{code}" endCode = T.pack "\\end{code}" -- | A command that is used when two tokens are put next to each other -- in the same column. agdaSpace :: Text agdaSpace = cmdPrefix <+> T.pack "Space" <+> cmdArg T.empty <+> nl -- | The column's name. -- -- Indentation columns have unique names, distinct from all alignment -- column names. columnName :: AlignmentColumn -> Text columnName c = T.pack $ case columnKind c of Nothing -> show (columnColumn c) Just i -> show i ++ "I" -- | Opens a column with the given name. ptOpen' :: Text -> Text ptOpen' name = T.pack "\\>[" <+> name <+> T.singleton ']' -- | Opens the given column. ptOpen :: AlignmentColumn -> Text ptOpen c = ptOpen' (columnName c) -- | Opens a special column that is only used at the beginning of -- lines. ptOpenBeginningOfLine :: Text ptOpenBeginningOfLine = ptOpen' (T.pack ".") -- | Opens the given column, and inserts an indentation instruction -- with the given argument at the end of it. ptOpenIndent :: AlignmentColumn -> Int -- ^ Indentation instruction argument. -> Text ptOpenIndent c delta = ptOpen c <+> T.pack "[@{}l@{" <+> cmdPrefix <+> T.pack "Indent" <+> cmdArg (T.pack $ show delta) <+> T.pack "}]" ptClose :: Text ptClose = T.pack "\\<" ptClose' :: AlignmentColumn -> Text ptClose' c = ptClose <+> T.singleton '[' <+> columnName c <+> T.singleton ']' ptNL :: Text ptNL = nl <+> T.pack "\\\\\n" ptEmptyLine :: Text ptEmptyLine = nl <+> T.pack "\\\\[" <+> cmdPrefix <+> T.pack "EmptyExtraSkip" <+> T.pack "]%\n" cmdPrefix :: Text cmdPrefix = T.pack "\\Agda" cmdArg :: Text -> Text cmdArg x = T.singleton '{' <+> x <+> T.singleton '}' ------------------------------------------------------------------------ -- * Automaton. -- | The start state, @nonCode@, prints non-code (the LaTeX part of -- literate Agda) until it sees a @beginBlock@. processLayers :: [(LayerRole, Tokens)] -> LaTeX () processLayers = mapM_ $ \(layerRole,toks) -> do case layerRole of L.Markup -> processMarkup toks L.Comment -> processComment toks L.Code -> processCode toks processMarkup, processComment, processCode :: Tokens -> LaTeX () -- | Deals with markup processMarkup = mapM_ moveColumnForToken -- | Deals with literate text, which is output verbatim processComment = mapM_ $ \t -> do unless (T.singleton '%' == T.take 1 (T.stripStart (text t))) $ do moveColumnForToken t output (Text (text t)) -- | Deals with code blocks. Every token, except spaces, is pretty -- printed as a LaTeX command. processCode toks' = do output $ Text $ beginCode <+> nl enterCode mapM_ go toks' ptOpenWhenColumnZero =<< gets column output $ Text $ ptClose <+> nl <+> endCode leaveCode where go tok' = do -- Get the column information before grabbing the token, since -- grabbing (possibly) moves the column. col <- gets column moveColumnForToken tok' let tok = text tok' log Code tok if (tok == T.empty) then return () else do if (isSpaces tok) then do spaces $ T.group $ replaceSpaces tok else do ptOpenWhenColumnZero col case aspect (info tok') of Nothing -> output $ Text $ escape tok Just a -> output $ Text $ cmdPrefix <+> T.pack (cmd a) <+> cmdArg (escape tok) -- Non-whitespace tokens at the start of a line trigger an -- alignment column. ptOpenWhenColumnZero col = when (col == 0) $ do registerColumnZero output . Text . ptOpen =<< columnZero cmd :: Aspect -> String cmd a = let s = show a in case a of Comment -> s Option -> s Keyword -> s String -> s Number -> s Symbol -> s PrimitiveType -> s Name Nothing isOp -> cmd (Name (Just Postulate) isOp) -- At the time of writing the case above can be encountered in -- --only-scope-checking mode, for instance for the token "Size" -- in the following code: -- -- {-# BUILTIN SIZE Size #-} -- -- The choice of "Postulate" works for this example, but might -- be less appropriate for others. Name (Just kind) _ -> case kind of Bound -> s Constructor Inductive -> "InductiveConstructor" Constructor CoInductive -> "CoinductiveConstructor" Datatype -> s Field -> s Function -> s Module -> s Postulate -> s Primitive -> s Record -> s Argument -> s Macro -> s where s = show kind -- Escapes special characters. escape :: Text -> Text escape (T.uncons -> Nothing) = T.empty escape (T.uncons -> Just (c, s)) = T.pack (replace c) <+> escape s where replace :: Char -> String replace c = case c of '_' -> "\\_" '{' -> "\\{" '}' -> "\\}" '#' -> "\\#" '$' -> "\\$" '&' -> "\\&" '%' -> "\\%" '~' -> "\\textasciitilde{}" '^' -> "\\textasciicircum{}" '\\' -> "\\textbackslash{}" '-' -> "{-}" _ -> [ c ] escape _ = __IMPOSSIBLE__ -- | Every element in the list should consist of either one or more -- newline characters, or one or more space characters. Two adjacent -- list elements must not contain the same character. -- -- If the final element of the list consists of spaces, then these -- spaces are assumed to not be trailing whitespace. spaces :: [Text] -> LaTeX () spaces [] = return () -- Newlines. spaces (s@(T.uncons -> Just ('\n', _)) : ss) = do col <- gets column when (col == 0) $ output . Text . ptOpen =<< columnZero output $ Text $ ptClose <+> ptNL <+> T.replicate (T.length s - 1) ptEmptyLine resetColumn spaces ss -- Spaces followed by a newline character. spaces (_ : ss@(_ : _)) = spaces ss -- Spaces that are not followed by a newline character. spaces [ s ] = do col <- gets column let len = T.length s kind = if col /= 0 && len == 1 then Indentation else Alignment moveColumn len column <- registerColumn kind if col /= 0 then log' Spaces "col /= 0" else do columns <- gets columnsPrev codeBlock <- gets codeBlock log' Spaces $ "col == 0: " ++ show (len, columns) case filter ((<= len) . columnColumn) columns of c : _ | columnColumn c == len, isJust (columnKind c) -> do -- Align. (This happens automatically if the column is an -- alignment column, but c is an indentation column.) useColumn c output $ Text $ ptOpenBeginningOfLine output $ Text $ ptClose' c c : _ | columnColumn c < len -> do -- Indent. useColumn c output $ Text $ ptOpenIndent c (codeBlock - columnCodeBlock c) _ -> return () output $ MaybeColumn column -- Split multi-lines string literals into multiple string literals -- Isolating leading spaces for the alignment machinery to work -- properly stringLiteral :: Token -> Tokens stringLiteral t | aspect (info t) == Just String = reverse $ foldl (\xs x -> t { text = x } : xs) [] $ concatMap leadingSpaces $ List.intersperse (T.pack "\n") $ T.lines (text t) where leadingSpaces :: Text -> [Text] leadingSpaces t = [pre, suf] where (pre , suf) = T.span isSpaceNotNewline t stringLiteral t = [t] ------------------------------------------------------------------------ -- * Main. defaultStyFile :: String defaultStyFile = "agda.sty" -- | Generates a LaTeX file for the given interface. -- -- The underlying source file is assumed to match the interface, but -- this is not checked. TODO: Fix this problem, perhaps by storing the -- source code in the interface. generateLaTeX :: Interface -> TCM () generateLaTeX i = do let mod = toTopLevelModuleName $ iModuleName i hi = iHighlighting i options <- TCM.commandLineOptions dir <- case optGHCiInteraction options of False -> return $ optLaTeXDir options True -> do sourceFile <- Find.findFile mod return $ filePath (projectRoot sourceFile mod) optLaTeXDir options liftIO $ createDirectoryIfMissing True dir (code, _, _) <- liftIO $ readProcessWithExitCode "kpsewhich" [ "--path=" ++ dir, defaultStyFile ] "" when (code /= ExitSuccess) $ do TCM.reportSLn "compile.latex" 1 $ unlines [ defaultStyFile ++ " was not found. Copying a default version of " ++ defaultStyFile , "into " ++ dir ++ "." ] liftIO $ do styFile <- getDataFileName defaultStyFile liftIO $ copyFile styFile (dir defaultStyFile) let outPath = modToFile mod inAbsPath <- liftM filePath (Find.findFile mod) liftIO $ do source <- UTF8.readTextFile inAbsPath latex <- E.encodeUtf8 `fmap` toLaTeX (optCountClusters options) (mkAbsolute inAbsPath) source hi createDirectoryIfMissing True $ dir takeDirectory outPath BS.writeFile (dir outPath) latex where modToFile :: TopLevelModuleName -> FilePath modToFile m = List.intercalate [pathSeparator] (moduleNameParts m) <.> "tex" groupByFst :: forall a b. Eq a => [(a,b)] -> [(a,[b])] groupByFst = map (\xs -> case xs of -- Float the grouping to the top level [] -> __IMPOSSIBLE__ (tag, _): _ -> (tag, map snd xs)) . List.groupBy ((==) `on` fst) -- Group together characters in the same -- role. -- | Transforms the source code into LaTeX. toLaTeX :: Bool -- ^ Count extended grapheme clusters? -> AbsolutePath -> String -> HighlightingInfo -> IO L.Text toLaTeX cc path source hi = processTokens cc . map (\(role, tokens) -> (role,) $ -- This bit fixes issue 954 (if L.isCode role then -- Remove trailing whitespace from the -- final line; the function spaces -- expects trailing whitespace to be -- followed by a newline character. whenMoreThanOne (withLast $ withTokenText $ \suf -> fromMaybe suf $ fmap (T.dropWhileEnd isSpaceNotNewline) $ T.stripSuffix (T.singleton '\n') suf) . (withLast $ withTokenText $ T.dropWhileEnd isSpaceNotNewline) . (withFirst $ withTokenText $ \pre -> fromMaybe pre $ T.stripPrefix (T.singleton '\n') $ T.dropWhile isSpaceNotNewline pre) else -- do nothing id) tokens) . map (second ( -- Split tokens at newlines concatMap stringLiteral -- Head the list (the grouped chars contain the same meta info) and -- collect the characters into a string. . map (\(mi, cs) -> Token { text = T.pack cs , info = fromMaybe mempty mi }) -- Characters which share the same meta info are the same token, so -- group them together. . groupByFst )) -- Characters in different layers belong to different tokens . groupByFst -- Look up the meta info at each position in the highlighting info. . map (\(pos, (role, char)) -> (role, (IntMap.lookup pos infoMap, char))) -- Add position in file to each character. . zip [1..] -- Map each character to its role . atomizeLayers . literateTeX (startPos (Just path)) $ source where infoMap = toMap (decompress hi) -- | This function preserves laziness of the list withLast :: (a -> a) -> [a] -> [a] withLast f [] = [] withLast f [a] = [f a] withLast f (a:as) = a:withLast f as -- | This function preserves laziness of the list withFirst :: (a -> a) -> [a] -> [a] withFirst _ [] = [] withFirst f (a:as) = f a:as whenMoreThanOne :: ([a] -> [a]) -> [a] -> [a] whenMoreThanOne f xs@(_:_:_) = f xs whenMoreThanOne _ xs = xs processTokens :: Bool -- ^ Count extended grapheme clusters? -> [(LayerRole, Tokens)] -> IO L.Text processTokens cc ts = do ((), s, os) <- runLaTeX (processLayers ts) () (emptyState cc) return $ L.fromChunks $ map (render s) os where render _ (Text s) = s render s (MaybeColumn c) | Just i <- columnKind c, not (Set.member i (usedColumns s)) = agdaSpace | otherwise = nl <+> ptOpen c Agda-2.5.3/src/full/Agda/Interaction/Highlighting/Emacs.hs0000644000000000000000000000670213154613124021405 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Functions which give precise syntax highlighting info to Emacs. module Agda.Interaction.Highlighting.Emacs ( lispifyHighlightingInfo ) where import Agda.Interaction.Highlighting.Precise import Agda.Interaction.Highlighting.Range import Agda.Interaction.EmacsCommand import Agda.Syntax.Common import Agda.TypeChecking.Monad (TCM, envHighlightingMethod, HighlightingMethod(..), ModuleToSource) import Agda.Utils.FileName import qualified Agda.Utils.IO.UTF8 as UTF8 import Agda.Utils.String import Control.Applicative import qualified Control.Exception as E import Control.Monad.Reader import Data.Char import qualified Data.Map as Map import Data.Maybe import Data.Monoid import qualified System.Directory as D import qualified System.IO as IO #include "undefined.h" import Agda.Utils.Impossible ------------------------------------------------------------------------ -- Read/show functions -- | Converts the 'aspect' and 'otherAspects' fields to atoms readable -- by the Emacs interface. toAtoms :: Aspects -> [String] toAtoms m = map toAtom (otherAspects m) ++ toAtoms' (aspect m) where toAtom :: Show a => a -> String toAtom = map toLower . show kindToAtom (Constructor Inductive) = "inductiveconstructor" kindToAtom (Constructor CoInductive) = "coinductiveconstructor" kindToAtom k = toAtom k toAtoms' Nothing = [] toAtoms' (Just (Name mKind op)) = map kindToAtom (maybeToList mKind) ++ opAtom where opAtom | op = ["operator"] | otherwise = [] toAtoms' (Just a) = [toAtom a] -- | Shows meta information in such a way that it can easily be read -- by Emacs. showAspects :: ModuleToSource -- ^ Must contain a mapping for the definition site's module, if any. -> (Range, Aspects) -> Lisp String showAspects modFile (r, m) = L $ (map (A . show) [from r, to r]) ++ [L $ map A $ toAtoms m] ++ [A $ maybe "nil" quote $ note m] ++ (maybeToList $ fmap defSite $ definitionSite m) where defSite (DefinitionSite m p _ _) = Cons (A $ quote $ filePath f) (A $ show p) where f = Map.findWithDefault __IMPOSSIBLE__ m modFile -- | Turns syntax highlighting information into a list of -- S-expressions. -- TODO: The "go-to-definition" targets can contain long strings -- (absolute paths to files). At least one of these strings (the path -- to the current module) can occur many times. Perhaps it would be a -- good idea to use a more compact format. lispifyHighlightingInfo :: HighlightingInfo -> HighlightingMethod -> ModuleToSource -- ^ Must contain a mapping for every definition site's module. -> IO (Lisp String) lispifyHighlightingInfo h method modFile = do case ranges h of _ | method == Direct -> direct ((_, mi) : _) | otherAspects mi == [TypeChecks] || mi == mempty -> direct _ -> indirect where info = map (showAspects modFile) (ranges h) direct = return $ L (A "agda2-highlight-add-annotations" : map Q info) indirect = do dir <- D.getTemporaryDirectory f <- E.bracket (IO.openTempFile dir "agda2-mode") (IO.hClose . snd) $ \ (f, h) -> do UTF8.hPutStr h (show $ L info) return f return $ L [ A "agda2-highlight-load-and-delete-action" , A (quote f) ] Agda-2.5.3/src/full/Agda/Interaction/Highlighting/Generate.hs0000644000000000000000000007606513154613124022120 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Generates data used for precise syntax highlighting. module Agda.Interaction.Highlighting.Generate ( Level(..) , generateAndPrintSyntaxInfo , generateTokenInfo, generateTokenInfoFromString , printSyntaxInfo , printErrorInfo, errorHighlighting , printUnsolvedInfo , printHighlightingInfo , highlightAsTypeChecked , computeUnsolvedMetaWarnings , computeUnsolvedConstraints , storeDisambiguatedName ) where import Prelude hiding (null) import Control.Monad import Control.Monad.Trans import Control.Monad.Reader import Control.Applicative import Control.Arrow (second) import Data.Monoid import Data.Generics.Geniplate import qualified Data.Map as Map import Data.Maybe import Data.List ((\\), isPrefixOf) import qualified Data.Foldable as Fold (fold, foldMap) import qualified Data.IntMap as IntMap import Data.Void import Agda.Interaction.Response (Response(Resp_HighlightingInfo)) import Agda.Interaction.Highlighting.Precise import Agda.Interaction.Highlighting.Range import qualified Agda.TypeChecking.Errors as E import Agda.TypeChecking.MetaVars (isBlockedTerm) import Agda.TypeChecking.Monad hiding (MetaInfo, Primitive, Constructor, Record, Function, Datatype) import qualified Agda.TypeChecking.Monad as M import Agda.TypeChecking.Positivity.Occurrence import Agda.TypeChecking.Warnings (runPM) import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Concrete (FieldAssignment'(..)) import qualified Agda.Syntax.Common as Common import qualified Agda.Syntax.Concrete.Name as C import qualified Agda.Syntax.Concrete as C import Agda.Syntax.Fixity import qualified Agda.Syntax.Info as SI import qualified Agda.Syntax.Internal as I import qualified Agda.Syntax.Literal as L import qualified Agda.Syntax.Parser as Pa import qualified Agda.Syntax.Parser.Tokens as T import qualified Agda.Syntax.Position as P import Agda.Utils.FileName import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import qualified Agda.Utils.Maybe.Strict as Strict import Agda.Utils.Null import Agda.Utils.Pretty import Agda.Utils.HashMap (HashMap) import qualified Agda.Utils.HashMap as HMap #include "undefined.h" import Agda.Utils.Impossible -- | @highlightAsTypeChecked rPre r m@ runs @m@ and returns its -- result. Additionally, some code may be highlighted: -- -- * If @r@ is non-empty and not a sub-range of @rPre@ (after -- 'P.continuousPerLine' has been applied to both): @r@ is -- highlighted as being type-checked while @m@ is running (this -- highlighting is removed if @m@ completes /successfully/). -- -- * Otherwise: Highlighting is removed for @rPre - r@ before @m@ -- runs, and if @m@ completes successfully, then @rPre - r@ is -- highlighted as being type-checked. highlightAsTypeChecked :: MonadTCM tcm => P.Range -> P.Range -> tcm a -> tcm a highlightAsTypeChecked rPre r m | r /= P.noRange && delta == rPre' = wrap r' highlight clear | otherwise = wrap delta clear highlight where rPre' = rToR (P.continuousPerLine rPre) r' = rToR (P.continuousPerLine r) delta = rPre' `minus` r' clear = mempty highlight = mempty { otherAspects = [TypeChecks] } wrap rs x y = do p rs x v <- m p rs y return v where p rs x = printHighlightingInfo (singletonC rs x) -- | Lispify and print the given highlighting information. printHighlightingInfo :: MonadTCM tcm => HighlightingInfo -> tcm () printHighlightingInfo info = do modToSrc <- use stModuleToSource method <- view eHighlightingMethod liftTCM $ reportSLn "highlighting" 50 $ unlines [ "Printing highlighting info:" , show info , " modToSrc = " ++ show modToSrc ] unless (null $ ranges info) $ do liftTCM $ appInteractionOutputCallback $ Resp_HighlightingInfo info method modToSrc -- | Highlighting levels. data Level = Full -- ^ Full highlighting. Should only be used after typechecking has -- completed successfully. | Partial -- ^ Highlighting without disambiguation of overloaded -- constructors. -- | Generate syntax highlighting information for the given -- declaration, and (if appropriate) print it. If the boolean is -- 'True', then the state is additionally updated with the new -- highlighting info (in case of a conflict new info takes precedence -- over old info). -- -- The procedure makes use of some of the token highlighting info in -- 'stTokens' (that corresponding to the interval covered by the -- declaration). If the boolean is 'True', then this token -- highlighting info is additionally removed from 'stTokens'. generateAndPrintSyntaxInfo :: A.Declaration -> Level -> Bool -- ^ Update the state? -> TCM () generateAndPrintSyntaxInfo decl _ _ | null $ P.getRange decl = return () generateAndPrintSyntaxInfo decl hlLevel updateState = do file <- fromMaybe __IMPOSSIBLE__ <$> asks envCurrentPath reportSLn "import.iface.create" 15 $ "Generating syntax info for " ++ filePath file ++ ' ' : case hlLevel of Full {} -> "(final)" Partial {} -> "(first approximation)" ++ "." reportSLn "highlighting.names" 60 $ "highlighting names = " ++ prettyShow names M.ignoreAbstractMode $ do modMap <- sourceToModule kinds <- nameKinds hlLevel decl let nameInfo = mconcat $ map (generate modMap file kinds) names -- Constructors are only highlighted after type checking, since they -- can be overloaded. constructorInfo <- case hlLevel of Full{} -> generateConstructorInfo modMap file kinds decl _ -> return mempty cm <- P.rangeFile <$> view eRange reportSLn "highlighting.warning" 60 $ "current path = " ++ show cm warnInfo <- Fold.foldMap warningHighlighting . filter ((cm ==) . tcWarningOrigin) <$> use stTCWarnings let (from, to) = case P.rangeToInterval (P.getRange decl) of Nothing -> __IMPOSSIBLE__ Just i -> ( fromIntegral $ P.posPos $ P.iStart i , fromIntegral $ P.posPos $ P.iEnd i) (prevTokens, (curTokens, postTokens)) <- (second (splitAtC to)) . splitAtC from <$> use stTokens -- theRest needs to be placed before nameInfo here since record -- field declarations contain QNames. constructorInfo also needs -- to be placed before nameInfo since, when typechecking is done, -- constructors are included in both lists. Finally the token -- information is placed last since token highlighting is more -- crude than the others. let syntaxInfo = compress (mconcat [ constructorInfo , theRest modMap file , nameInfo , warnInfo ]) `mappend` curTokens when updateState $ do stSyntaxInfo %= mappend syntaxInfo stTokens .= prevTokens `mappend` postTokens ifTopLevelAndHighlightingLevelIs NonInteractive $ printHighlightingInfo syntaxInfo where -- All names mentioned in the syntax tree (not bound variables). names :: [A.AmbiguousQName] names = (map (A.AmbQ . (:[])) $ filter (not . extendedLambda) $ universeBi decl) ++ universeBi decl where extendedLambda :: A.QName -> Bool extendedLambda = (extendedLambdaName `isPrefixOf`) . show . A.nameConcrete . A.qnameName -- Bound variables, dotted patterns, record fields, module names, -- the "as" and "to" symbols. theRest modMap file = mconcat [ Fold.foldMap getFieldDecl $ universeBi decl , Fold.foldMap getVarAndField $ universeBi decl , Fold.foldMap getLet $ universeBi decl , Fold.foldMap getLam $ universeBi decl , Fold.foldMap getTyped $ universeBi decl , Fold.foldMap getPattern $ universeBi decl , Fold.foldMap getPatternSyn $ universeBi decl , Fold.foldMap getExpr $ universeBi decl , Fold.foldMap getPatSynArgs $ universeBi decl , Fold.foldMap getModuleName $ universeBi decl , Fold.foldMap getModuleInfo $ universeBi decl , Fold.foldMap getNamedArg $ universeBi decl ] where bound n = nameToFile modMap file [] (A.nameConcrete n) P.noRange (\isOp -> mempty { aspect = Just $ Name (Just Bound) isOp }) (Just $ A.nameBindingSite n) patsyn n = nameToFileA modMap file n True $ \isOp -> mempty { aspect = Just $ Name (Just $ Constructor Common.Inductive) isOp } macro n = nameToFileA modMap file n True $ \isOp -> mempty { aspect = Just $ Name (Just Macro) isOp } field m n = nameToFile modMap file m n P.noRange (\isOp -> mempty { aspect = Just $ Name (Just Field) isOp }) Nothing asName n = nameToFile modMap file [] n P.noRange (\isOp -> mempty { aspect = Just $ Name (Just Module) isOp }) Nothing -- For top level modules, we set the binding site to the beginning of the file -- so that clicking on an imported module will jump to the beginning of the file -- which defines this module. mod isTopLevelModule n = nameToFile modMap file [] (A.nameConcrete n) P.noRange (\isOp -> mempty { aspect = Just $ Name (Just Module) isOp }) (Just $ applyWhen isTopLevelModule P.beginningOfFile $ A.nameBindingSite n) getVarAndField :: A.Expr -> File getVarAndField (A.Var x) = bound x getVarAndField (A.Rec _ fs) = mconcat [ field [] x | Left (FieldAssignment x _) <- fs ] getVarAndField (A.RecUpdate _ _ fs) = mconcat [ field [] x | (FieldAssignment x _) <- fs ] getVarAndField _ = mempty -- Ulf, 2014-04-09: It would be nicer to have it on Named_ a, but -- you can't have polymorphic functions in universeBi. getNamedArg :: Common.RString -> File getNamedArg x = singleton (rToR $ P.getRange x) mempty{ aspect = Just $ Name (Just Argument) False } getLet :: A.LetBinding -> File getLet (A.LetBind _ _ x _ _) = bound x getLet A.LetPatBind{} = mempty getLet A.LetApply{} = mempty getLet A.LetOpen{} = mempty getLet (A.LetDeclaredVariable x) = bound x getLam :: A.LamBinding -> File getLam (A.DomainFree _ x) = bound x getLam (A.DomainFull {}) = mempty getTyped :: A.TypedBinding -> File getTyped (A.TBind _ xs _) = mconcat $ map (bound . dget) xs getTyped A.TLet{} = mempty getPatSynArgs :: A.Declaration -> File getPatSynArgs (A.PatternSynDef _ xs _) = mconcat $ map (bound . Common.unArg) xs getPatSynArgs _ = mempty getPattern' :: A.Pattern' e -> File getPattern' (A.VarP x) = bound x getPattern' (A.AsP _ x _) = bound x getPattern' (A.DotP pi _ _) = singleton (rToR $ P.getRange pi) (mempty { otherAspects = [DottedPattern] }) getPattern' (A.PatternSynP _ q _) = patsyn q getPattern' (A.RecP _ fs) = mconcat [ field [] x | FieldAssignment x _ <- fs ] getPattern' _ = mempty getPattern :: A.Pattern -> File getPattern = getPattern' getPatternSyn :: A.Pattern' Void -> File getPatternSyn = getPattern' getExpr :: A.Expr -> File getExpr (A.PatternSyn q) = patsyn q getExpr (A.Macro q) = macro q getExpr _ = mempty getFieldDecl :: A.Declaration -> File getFieldDecl (A.RecDef _ _ _ _ _ _ _ fs) = Fold.foldMap extractField fs where extractField (A.ScopedDecl _ ds) = Fold.foldMap extractField ds extractField (A.Field _ x _) = field (concreteQualifier x) (concreteBase x) extractField _ = mempty getFieldDecl _ = mempty getModuleName :: A.ModuleName -> File getModuleName m@(A.MName { A.mnameToList = xs }) = mconcat $ map (mod isTopLevelModule) xs where isTopLevelModule = case catMaybes $ map (join . fmap (Strict.toLazy . P.srcFile) . P.rStart . A.nameBindingSite) xs of f : _ -> Map.lookup f modMap == Just (C.toTopLevelModuleName $ A.mnameToConcrete m) [] -> False getModuleInfo :: SI.ModuleInfo -> File getModuleInfo (SI.ModuleInfo { SI.minfoAsTo = asTo , SI.minfoAsName = name }) = singleton (rToR asTo) (mempty { aspect = Just Symbol }) `mappend` maybe mempty asName name -- | Generate and return the syntax highlighting information for the -- tokens in the given file. generateTokenInfo :: AbsolutePath -- ^ The module to highlight. -> TCM CompressedFile generateTokenInfo file = runPM $ tokenHighlighting <$> Pa.parseFile' Pa.tokensParser file -- | Same as 'generateTokenInfo' but takes a string instead of a filename. generateTokenInfoFromString :: P.Range -> String -> TCM CompressedFile generateTokenInfoFromString r _ | r == P.noRange = return mempty generateTokenInfoFromString r s = do runPM $ tokenHighlighting <$> Pa.parsePosString Pa.tokensParser p s where Just p = P.rStart r -- | Compute syntax highlighting for the given tokens. tokenHighlighting :: [T.Token] -> CompressedFile tokenHighlighting = merge . map tokenToCFile where -- Converts an aspect and a range to a file. aToF a r = singletonC (rToR r) (mempty { aspect = Just a }) -- Merges /sorted, non-overlapping/ compressed files. merge = CompressedFile . concat . map ranges tokenToCFile :: T.Token -> CompressedFile tokenToCFile (T.TokSetN (i, _)) = aToF PrimitiveType (P.getRange i) tokenToCFile (T.TokKeyword T.KwSet i) = aToF PrimitiveType (P.getRange i) tokenToCFile (T.TokKeyword T.KwProp i) = aToF PrimitiveType (P.getRange i) tokenToCFile (T.TokKeyword T.KwForall i) = aToF Symbol (P.getRange i) tokenToCFile (T.TokKeyword _ i) = aToF Keyword (P.getRange i) tokenToCFile (T.TokSymbol _ i) = aToF Symbol (P.getRange i) tokenToCFile (T.TokLiteral (L.LitNat r _)) = aToF Number r tokenToCFile (T.TokLiteral (L.LitFloat r _)) = aToF Number r tokenToCFile (T.TokLiteral (L.LitString r _)) = aToF String r tokenToCFile (T.TokLiteral (L.LitChar r _)) = aToF String r tokenToCFile (T.TokLiteral (L.LitQName r _)) = aToF String r tokenToCFile (T.TokLiteral (L.LitMeta r _ _)) = aToF String r tokenToCFile (T.TokComment (i, _)) = aToF Comment (P.getRange i) tokenToCFile (T.TokTeX (i, _)) = aToF Comment (P.getRange i) tokenToCFile (T.TokId {}) = mempty tokenToCFile (T.TokQId {}) = mempty tokenToCFile (T.TokString (i,s)) | "--" `isPrefixOf` s = aToF Option (P.getRange i) | otherwise = mempty tokenToCFile (T.TokDummy {}) = mempty tokenToCFile (T.TokEOF {}) = mempty -- | A function mapping names to the kind of name they stand for. type NameKinds = A.QName -> Maybe NameKind -- | Builds a 'NameKinds' function. nameKinds :: Level -- ^ This should only be @'Full'@ if -- type-checking completed successfully (without any -- errors). -> A.Declaration -> TCM NameKinds nameKinds hlLevel decl = do imported <- fix <$> use stImports local <- case hlLevel of Full{} -> fix <$> use stSignature _ -> return HMap.empty -- Traverses the syntax tree and constructs a map from qualified -- names to name kinds. TODO: Handle open public. let syntax = foldr ($) HMap.empty $ map declToKind $ universeBi decl let merged = unions [local, imported, syntax] return (\n -> HMap.lookup n merged) where fix = HMap.map (defnToKind . theDef) . (^. sigDefinitions) -- | The 'M.Axiom' constructor is used to represent various things -- which are not really axioms, so when maps are merged 'Postulate's -- are thrown away whenever possible. The 'declToKind' function -- below can return several explanations for one qualified name; the -- 'Postulate's are bogus. merge Postulate k = k merge _ Macro = Macro -- If the abstract syntax says macro, it's a macro. merge k _ = k unions = foldr (HMap.unionWith merge) HMap.empty insert = HMap.insertWith merge defnToKind :: Defn -> NameKind defnToKind M.Axiom{} = Postulate defnToKind d@M.Function{} | isProperProjection d = Field | otherwise = Function defnToKind M.Datatype{} = Datatype defnToKind M.Record{} = Record defnToKind M.Constructor{ M.conInd = i } = Constructor i defnToKind M.Primitive{} = Primitive defnToKind M.AbstractDefn{} = __IMPOSSIBLE__ declToKind :: A.Declaration -> HashMap A.QName NameKind -> HashMap A.QName NameKind declToKind (A.Axiom _ i _ _ q _) | SI.defMacro i == Common.MacroDef = insert q Macro | otherwise = insert q Postulate declToKind (A.Field _ q _) = insert q Field -- Function -- Note that the name q can be used both as a field name and as a -- projection function. Highlighting of field names is taken care -- of by "theRest" above, which does not use NameKinds. declToKind (A.Primitive _ q _) = insert q Primitive declToKind (A.Mutual {}) = id declToKind (A.Section {}) = id declToKind (A.Apply {}) = id declToKind (A.Import {}) = id declToKind (A.Pragma {}) = id declToKind (A.ScopedDecl {}) = id declToKind (A.Open {}) = id declToKind (A.PatternSynDef q _ _) = insert q (Constructor Common.Inductive) declToKind (A.FunDef _ q _ _) = insert q Function declToKind (A.UnquoteDecl _ _ qs _) = foldr (\ q f -> insert q Function . f) id qs declToKind (A.UnquoteDef _ qs _) = foldr (\ q f -> insert q Function . f) id qs declToKind (A.DataSig _ q _ _) = insert q Datatype declToKind (A.DataDef _ q _ cs) = \m -> insert q Datatype $ foldr (\d -> insert (A.axiomName d) (Constructor Common.Inductive)) m cs declToKind (A.RecSig _ q _ _) = insert q Record declToKind (A.RecDef _ q _ _ c _ _ _) = insert q Record . case c of Nothing -> id Just q -> insert q (Constructor Common.Inductive) -- | Generates syntax highlighting information for all constructors -- occurring in patterns and expressions in the given declaration. -- -- This function should only be called after type checking. -- Constructors can be overloaded, and the overloading is resolved by -- the type checker. generateConstructorInfo :: SourceToModule -- ^ Maps source file paths to module names. -> AbsolutePath -- ^ The module to highlight. -> NameKinds -> A.Declaration -> TCM File generateConstructorInfo modMap file kinds decl = do -- Get boundaries of current declaration. -- @noRange@ should be impossible, but in case of @noRange@ -- it makes sense to return the empty File. ifNull (P.rangeIntervals $ P.getRange decl) (return mempty) $ \is -> do let start = fromIntegral $ P.posPos $ P.iStart $ head is end = fromIntegral $ P.posPos $ P.iEnd $ last is -- Get all disambiguated names that fall within the range of decl. m0 <- use stDisambiguatedNames let (_, m1) = IntMap.split (pred start) m0 (m2, _) = IntMap.split end m1 constrs = IntMap.elems m2 -- Return suitable syntax highlighting information. let files = for constrs $ \ q -> generate modMap file kinds $ A.AmbQ [q] return $ Fold.fold files printSyntaxInfo :: P.Range -> TCM () printSyntaxInfo r = do syntaxInfo <- use stSyntaxInfo ifTopLevelAndHighlightingLevelIs NonInteractive $ printHighlightingInfo (selectC r syntaxInfo) -- | Prints syntax highlighting info for an error. printErrorInfo :: TCErr -> TCM () printErrorInfo e = printHighlightingInfo . compress =<< errorHighlighting e -- | Generate highlighting for error. -- Does something special for termination errors. errorHighlighting :: TCErr -> TCM File errorHighlighting (TypeError s cl@Closure{ clValue = TerminationCheckFailed termErrs }) = -- For termination errors, we keep the previous highlighting, -- just additionally mark the bad calls. return $ terminationErrorHighlighting termErrs errorHighlighting e = do -- Erase previous highlighting. let r = P.getRange e erase = singleton (rToR $ P.continuousPerLine r) mempty -- Print new highlighting. s <- E.prettyError e let error = singleton (rToR r) $ mempty { otherAspects = [Error] , note = Just s } return $ mconcat [ erase, error ] -- | Generate syntax highlighting for warnings. warningHighlighting :: TCWarning -> File warningHighlighting w = case tcWarning w of TerminationIssue terrs -> terminationErrorHighlighting terrs NotStrictlyPositive d ocs -> positivityErrorHighlighting d ocs UnreachableClauses{} -> unreachableErrorHighlighting $ P.getRange w CoverageIssue{} -> coverageErrorHighlighting $ P.getRange w CoverageNoExactSplit{} -> catchallHighlighting $ P.getRange w -- expanded catch-all case to get a warning for new constructors UnsolvedMetaVariables{} -> mempty UnsolvedInteractionMetas{} -> mempty UnsolvedConstraints{} -> mempty OldBuiltin{} -> mempty EmptyRewritePragma{} -> mempty UselessPublic{} -> mempty UselessInline{} -> mempty ParseWarning{} -> mempty GenericWarning{} -> mempty GenericNonFatalError{} -> mempty SafeFlagPostulate{} -> mempty SafeFlagPragma{} -> mempty SafeFlagNonTerminating -> mempty SafeFlagTerminating -> mempty SafeFlagPrimTrustMe -> mempty SafeFlagNoPositivityCheck -> mempty SafeFlagPolarity -> mempty DeprecationWarning{} -> mempty NicifierIssue{} -> mempty -- | Generate syntax highlighting for termination errors. terminationErrorHighlighting :: [TerminationError] -> File terminationErrorHighlighting termErrs = functionDefs `mappend` callSites where m = mempty { otherAspects = [TerminationProblem] } functionDefs = Fold.foldMap (\x -> singleton (rToR $ bindingSite x) m) $ concatMap M.termErrFunctions termErrs callSites = Fold.foldMap (\r -> singleton (rToR r) m) $ concatMap (map M.callInfoRange . M.termErrCalls) termErrs -- | Generate syntax highlighting for not-strictly-positive inductive -- definitions. -- TODO: highlight also the problematic occurrences positivityErrorHighlighting :: I.QName -> OccursWhere -> File positivityErrorHighlighting q o = several (rToR <$> P.getRange q : rs) m where rs = case o of Unknown -> []; Known r _ -> [r] m = mempty { otherAspects = [PositivityProblem] } unreachableErrorHighlighting :: P.Range -> File unreachableErrorHighlighting r = singleton (rToR $ P.continuousPerLine r) m where m = mempty { otherAspects = [ReachabilityProblem] } coverageErrorHighlighting :: P.Range -> File coverageErrorHighlighting r = singleton (rToR $ P.continuousPerLine r) m where m = mempty { otherAspects = [CoverageProblem] } catchallHighlighting :: P.Range -> File catchallHighlighting r = singleton (rToR $ P.continuousPerLine r) m where m = mempty { otherAspects = [CatchallClause] } -- | Generates and prints syntax highlighting information for unsolved -- meta-variables and certain unsolved constraints. printUnsolvedInfo :: TCM () printUnsolvedInfo = do metaInfo <- computeUnsolvedMetaWarnings constraintInfo <- computeUnsolvedConstraints printHighlightingInfo (compress $ metaInfo `mappend` constraintInfo) -- | Generates syntax highlighting information for unsolved meta -- variables. computeUnsolvedMetaWarnings :: TCM File computeUnsolvedMetaWarnings = do is <- getInteractionMetas -- We don't want to highlight blocked terms, since -- * there is always at least one proper meta responsible for the blocking -- * in many cases the blocked term covers the highlighting for this meta let notBlocked m = not <$> isBlockedTerm m ms <- filterM notBlocked =<< getOpenMetas rs <- mapM getMetaRange (ms \\ is) return $ several (map (rToR . P.continuousPerLine) rs) (mempty { otherAspects = [UnsolvedMeta] }) -- | Generates syntax highlighting information for unsolved constraints -- that are not connected to a meta variable. computeUnsolvedConstraints :: TCM File computeUnsolvedConstraints = do cs <- getAllConstraints -- get ranges of emptyness constraints let rs = [ r | PConstr{ theConstraint = Closure{ clValue = IsEmpty r t }} <- cs ] return $ several (map (rToR . P.continuousPerLine) rs) (mempty { otherAspects = [UnsolvedConstraint] }) -- | Generates a suitable file for a possibly ambiguous name. generate :: SourceToModule -- ^ Maps source file paths to module names. -> AbsolutePath -- ^ The module to highlight. -> NameKinds -> A.AmbiguousQName -> File generate modMap file kinds (A.AmbQ qs) = mconcat $ map (\q -> nameToFileA modMap file q include m) qs where ks = map kinds qs -- Ulf, 2014-06-03: [issue1064] It's better to pick the first rather -- than doing no highlighting if there's an ambiguity between an -- inductive and coinductive constructor. kind = case [ k | Just k <- ks ] of k : _ -> Just k [] -> Nothing -- kind = case (allEqual ks, ks) of -- (True, Just k : _) -> Just k -- _ -> Nothing -- Note that all names in an AmbiguousQName should have the same -- concrete name, so either they are all operators, or none of -- them are. m isOp = mempty { aspect = Just $ Name kind isOp } include = allEqual (map bindingSite qs) -- | Converts names to suitable 'File's. nameToFile :: SourceToModule -- ^ Maps source file paths to module names. -> AbsolutePath -- ^ The file name of the current module. Used for -- consistency checking. -> [C.Name] -- ^ The name qualifier (may be empty). -> C.Name -- ^ The base name. -> P.Range -- ^ The 'Range' of the name in its fixity declaration (if any). -> (Bool -> Aspects) -- ^ Meta information to be associated with the name. -- The argument is 'True' iff the name is an operator. -> Maybe P.Range -- ^ The definition site of the name. The calculated -- meta information is extended with this information, -- if possible. -> File nameToFile modMap file xs x fr m mR = -- We don't care if we get any funny ranges. if all (== Strict.Just file) fileNames then frFile `mappend` several (map rToR rs) (aspects { definitionSite = mFilePos }) else mempty where aspects = m $ C.isOperator x fileNames = catMaybes $ map (fmap P.srcFile . P.rStart . P.getRange) (x : xs) frFile = singleton (rToR fr) (aspects { definitionSite = notHere <$> mFilePos }) rs = map P.getRange (x : xs) -- The fixity declaration should not get a symbolic anchor. notHere d = d { defSiteHere = False } mFilePos :: Maybe DefinitionSite mFilePos = do r <- mR P.Pn { P.srcFile = Strict.Just f, P.posPos = p } <- P.rStart r mod <- Map.lookup f modMap -- Andreas, 2017-06-16, Issue #2604: Symbolic anchors. -- We drop the file name part from the qualifiers, since -- this is contained in the html file name already. -- We want to get anchors of the form: -- @@ let qualifiers = drop (length $ C.moduleNameParts mod) xs -- For bound variables, we do not create symbolic anchors. local = maybe True isLocalAspect $ aspect aspects return $ DefinitionSite { defSiteModule = mod , defSitePos = fromIntegral p -- Is our current position the definition site? , defSiteHere = r == P.getRange x -- For bound variables etc. we do not create a symbolic anchor name. -- Also not for names that include anonymous modules, -- otherwise, we do not get unique anchors. , defSiteAnchor = if local || C.isNoName x || any Common.isUnderscore qualifiers then Nothing else Just $ prettyShow $ foldr C.Qual (C.QName x) qualifiers } -- Is the name a bound variable or similar? If in doubt, yes. isLocalAspect :: Aspect -> Bool isLocalAspect = \case Name mkind _ -> maybe True isLocal mkind _ -> True isLocal :: NameKind -> Bool isLocal = \case Bound -> True Argument -> True Constructor{} -> False Datatype -> False Field -> False Function -> False Module -> False Postulate -> False Primitive -> False Record -> False Macro -> False -- | A variant of 'nameToFile' for qualified abstract names. nameToFileA :: SourceToModule -- ^ Maps source file paths to module names. -> AbsolutePath -- ^ The file name of the current module. Used for -- consistency checking. -> A.QName -- ^ The name. -> Bool -- ^ Should the binding site be included in the file? -> (Bool -> Aspects) -- ^ Meta information to be associated with the name. -- ^ The argument is 'True' iff the name is an operator. -> File nameToFileA modMap file x include m = nameToFile modMap file (concreteQualifier x) (concreteBase x) r m (if include then Just $ bindingSite x else Nothing) where -- Andreas, 2016-09-08, for issue #2140: -- Range of name from fixity declaration: fr = theNameRange $ A.nameFixity $ A.qnameName x -- Somehow we import fixity ranges from other files, we should ignore them. -- (I do not understand how we get them as they should not be serialized...) r = if P.rangeFile fr == Strict.Just file then fr else P.noRange concreteBase :: I.QName -> C.Name concreteBase = A.nameConcrete . A.qnameName concreteQualifier :: I.QName -> [C.Name] concreteQualifier = map A.nameConcrete . A.mnameToList . A.qnameModule bindingSite :: I.QName -> P.Range bindingSite = A.nameBindingSite . A.qnameName -- | Remember a name disambiguation (during type checking). -- To be used later during syntax highlighting. storeDisambiguatedName :: A.QName -> TCM () storeDisambiguatedName q = whenJust (start $ P.getRange q) $ \ i -> stDisambiguatedNames %= IntMap.insert i q where start r = fromIntegral . P.posPos <$> P.rStart' r Agda-2.5.3/src/full/Agda/Interaction/Highlighting/Range.hs0000644000000000000000000000617713154613124021417 0ustar0000000000000000{-# LANGUAGE DeriveDataTypeable #-} -- | Ranges. module Agda.Interaction.Highlighting.Range ( Range(..) , rangeInvariant , Ranges(..) , rangesInvariant , overlapping , empty , rangeToPositions , rangesToPositions , rToR , rangeToEndPoints , minus ) where import Control.Applicative ((<$>)) import Data.Typeable (Typeable) import qualified Agda.Syntax.Position as P import Agda.Utils.List -- | Character ranges. The first character in the file has position 1. -- Note that the 'to' position is considered to be outside of the -- range. -- -- Invariant: @'from' '<=' 'to'@. data Range = Range { from, to :: Int } deriving (Eq, Ord, Show, Typeable) -- | The 'Range' invariant. rangeInvariant :: Range -> Bool rangeInvariant r = from r <= to r -- | Zero or more consecutive and separated ranges. newtype Ranges = Ranges [Range] deriving (Eq, Show) -- | The 'Ranges' invariant. rangesInvariant :: Ranges -> Bool rangesInvariant (Ranges []) = True rangesInvariant (Ranges rs) = and (zipWith (<) (map to $ init rs) (map from $ tail rs)) ------------------------------------------------------------------------ -- Queries -- | 'True' iff the ranges overlap. -- -- The ranges are assumed to be well-formed. overlapping :: Range -> Range -> Bool overlapping r1 r2 = not $ to r1 <= from r2 || to r2 <= from r1 -- | 'True' iff the range is empty. empty :: Range -> Bool empty r = to r <= from r ------------------------------------------------------------------------ -- Conversion -- | Converts a range to a list of positions. rangeToPositions :: Range -> [Int] rangeToPositions r = [from r .. to r - 1] -- | Converts several ranges to a list of positions. rangesToPositions :: Ranges -> [Int] rangesToPositions (Ranges rs) = concatMap rangeToPositions rs -- | Converts a 'P.Range' to a 'Ranges'. rToR :: P.Range -> Ranges rToR r = Ranges (map iToR (P.rangeIntervals r)) where iToR (P.Interval { P.iStart = P.Pn { P.posPos = pos1 } , P.iEnd = P.Pn { P.posPos = pos2 } }) = Range { from = fromIntegral pos1, to = fromIntegral pos2 } rangeToEndPoints :: P.Range -> Maybe (Int,Int) rangeToEndPoints r = case P.rangeToInterval r of Nothing -> Nothing Just i -> Just ( fromIntegral $ P.posPos $ P.iStart i , fromIntegral $ P.posPos $ P.iEnd i) ------------------------------------------------------------------------ -- Operations -- | @minus xs ys@ computes the difference between @xs@ and @ys@: the -- result contains those positions which are present in @xs@ but not -- in @ys@. -- -- Linear in the lengths of the input ranges. minus :: Ranges -> Ranges -> Ranges minus (Ranges rs1) (Ranges rs2) = Ranges (m rs1 rs2) where m [] _ = [] m xs [] = xs m (x:xs) (y:ys) | empty y = m (x:xs) ys | to x < from y = x : m xs (y:ys) | to y < from x = m (x:xs) ys | from x < from y = Range { from = from x, to = from y } : m (Range { from = from y, to = to x } : xs) (y:ys) | to y < to x = m (Range { from = to y, to = to x } : xs) ys | otherwise = m xs (y:ys) Agda-2.5.3/src/full/Agda/Interaction/Options/0000755000000000000000000000000013154613124017042 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Interaction/Options/Lenses.hs0000644000000000000000000001777513154613124020650 0ustar0000000000000000-- | Lenses for 'CommandLineOptions' and 'PragmaOptions'. -- -- Add as needed. -- -- Nothing smart happening here. module Agda.Interaction.Options.Lenses where import Control.Monad.State import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.State import Agda.Interaction.Options import Agda.Utils.Lens import Agda.Utils.FileName --------------------------------------------------------------------------- -- * Pragma options --------------------------------------------------------------------------- class LensPragmaOptions a where getPragmaOptions :: a -> PragmaOptions setPragmaOptions :: PragmaOptions -> a -> a mapPragmaOptions :: (PragmaOptions -> PragmaOptions) -> a -> a -- default implementations setPragmaOptions = mapPragmaOptions . const mapPragmaOptions f a = setPragmaOptions (f $ getPragmaOptions a) a instance LensPragmaOptions CommandLineOptions where getPragmaOptions = optPragmaOptions setPragmaOptions opts st = st { optPragmaOptions = opts } instance LensPragmaOptions TCState where getPragmaOptions = (^.stPragmaOptions) setPragmaOptions = set stPragmaOptions modifyPragmaOptions :: (PragmaOptions -> PragmaOptions) -> TCM () modifyPragmaOptions = modify . mapPragmaOptions --------------------------------------------------------------------------- -- ** Verbosity in the local pragma options --------------------------------------------------------------------------- class LensVerbosity a where getVerbosity :: a -> Verbosity setVerbosity :: Verbosity -> a -> a mapVerbosity :: (Verbosity -> Verbosity) -> a -> a -- default implementations setVerbosity = mapVerbosity . const mapVerbosity f a = setVerbosity (f $ getVerbosity a) a instance LensVerbosity PragmaOptions where getVerbosity = optVerbose setVerbosity is opts = opts { optVerbose = is } instance LensVerbosity TCState where getVerbosity = getVerbosity . getPragmaOptions mapVerbosity = mapPragmaOptions . mapVerbosity modifyVerbosity :: (Verbosity -> Verbosity) -> TCM () modifyVerbosity = modify . mapVerbosity putVerbosity :: Verbosity -> TCM () putVerbosity = modify . setVerbosity --------------------------------------------------------------------------- -- * Command line options --------------------------------------------------------------------------- class LensCommandLineOptions a where getCommandLineOptions :: a -> CommandLineOptions setCommandLineOptions :: CommandLineOptions -> a -> a mapCommandLineOptions :: (CommandLineOptions -> CommandLineOptions) -> a -> a -- default implementations setCommandLineOptions = mapCommandLineOptions . const mapCommandLineOptions f a = setCommandLineOptions (f $ getCommandLineOptions a) a instance LensCommandLineOptions PersistentTCState where getCommandLineOptions = stPersistentOptions setCommandLineOptions opts st = st { stPersistentOptions = opts } instance LensCommandLineOptions TCState where getCommandLineOptions = getCommandLineOptions . stPersistentState mapCommandLineOptions = updatePersistentState . mapCommandLineOptions modifyCommandLineOptions :: (CommandLineOptions -> CommandLineOptions) -> TCM () modifyCommandLineOptions = modify . mapCommandLineOptions --------------------------------------------------------------------------- -- ** Safe mode --------------------------------------------------------------------------- type SafeMode = Bool class LensSafeMode a where getSafeMode :: a -> SafeMode setSafeMode :: SafeMode -> a -> a mapSafeMode :: (SafeMode -> SafeMode) -> a -> a -- default implementations setSafeMode = mapSafeMode . const mapSafeMode f a = setSafeMode (f $ getSafeMode a) a instance LensSafeMode PragmaOptions where getSafeMode = optSafe setSafeMode is opts = opts { optSafe = is } instance LensSafeMode CommandLineOptions where getSafeMode = getSafeMode . getPragmaOptions mapSafeMode = mapPragmaOptions . mapSafeMode instance LensSafeMode PersistentTCState where getSafeMode = getSafeMode . getCommandLineOptions mapSafeMode = mapCommandLineOptions . mapSafeMode instance LensSafeMode TCState where getSafeMode = getSafeMode . getCommandLineOptions mapSafeMode = mapCommandLineOptions . mapSafeMode modifySafeMode :: (SafeMode -> SafeMode) -> TCM () modifySafeMode = modify . mapSafeMode putSafeMode :: SafeMode -> TCM () putSafeMode = modify . setSafeMode --------------------------------------------------------------------------- -- ** Include directories --------------------------------------------------------------------------- class LensIncludePaths a where getIncludePaths :: a -> [FilePath] setIncludePaths :: [FilePath] -> a -> a mapIncludePaths :: ([FilePath] -> [FilePath]) -> a -> a getAbsoluteIncludePaths :: a -> [AbsolutePath] setAbsoluteIncludePaths :: [AbsolutePath] -> a -> a mapAbsoluteIncludePaths :: ([AbsolutePath] -> [AbsolutePath]) -> a -> a -- default implementations setIncludePaths = mapIncludePaths . const mapIncludePaths f a = setIncludePaths (f $ getIncludePaths a) a setAbsoluteIncludePaths = mapAbsoluteIncludePaths . const mapAbsoluteIncludePaths f a = setAbsoluteIncludePaths (f $ getAbsoluteIncludePaths a) a instance LensIncludePaths CommandLineOptions where getIncludePaths = optIncludePaths setIncludePaths is opts = opts { optIncludePaths = is } getAbsoluteIncludePaths = optAbsoluteIncludePaths setAbsoluteIncludePaths is opts = opts { optAbsoluteIncludePaths = is } instance LensIncludePaths PersistentTCState where getIncludePaths = getIncludePaths . getCommandLineOptions mapIncludePaths = mapCommandLineOptions . mapIncludePaths getAbsoluteIncludePaths = getAbsoluteIncludePaths . getCommandLineOptions mapAbsoluteIncludePaths = mapCommandLineOptions . mapAbsoluteIncludePaths instance LensIncludePaths TCState where getIncludePaths = getIncludePaths . getCommandLineOptions mapIncludePaths = mapCommandLineOptions . mapIncludePaths getAbsoluteIncludePaths = getAbsoluteIncludePaths . getCommandLineOptions mapAbsoluteIncludePaths = mapCommandLineOptions . mapAbsoluteIncludePaths modifyIncludePaths :: ([FilePath] -> [FilePath]) -> TCM () modifyIncludePaths = modify . mapIncludePaths putIncludePaths :: [FilePath] -> TCM () putIncludePaths = modify . setIncludePaths modifyAbsoluteIncludePaths :: ([AbsolutePath] -> [AbsolutePath]) -> TCM () modifyAbsoluteIncludePaths = modify . mapAbsoluteIncludePaths putAbsoluteIncludePaths :: [AbsolutePath] -> TCM () putAbsoluteIncludePaths = modify . setAbsoluteIncludePaths --------------------------------------------------------------------------- -- ** Include directories --------------------------------------------------------------------------- type PersistentVerbosity = Verbosity class LensPersistentVerbosity a where getPersistentVerbosity :: a -> PersistentVerbosity setPersistentVerbosity :: PersistentVerbosity -> a -> a mapPersistentVerbosity :: (PersistentVerbosity -> PersistentVerbosity) -> a -> a -- default implementations setPersistentVerbosity = mapPersistentVerbosity . const mapPersistentVerbosity f a = setPersistentVerbosity (f $ getPersistentVerbosity a) a instance LensPersistentVerbosity PragmaOptions where getPersistentVerbosity = getVerbosity setPersistentVerbosity = setVerbosity instance LensPersistentVerbosity CommandLineOptions where getPersistentVerbosity = getPersistentVerbosity . getPragmaOptions mapPersistentVerbosity = mapPragmaOptions . mapPersistentVerbosity instance LensPersistentVerbosity PersistentTCState where getPersistentVerbosity = getPersistentVerbosity . getCommandLineOptions mapPersistentVerbosity = mapCommandLineOptions . mapPersistentVerbosity instance LensPersistentVerbosity TCState where getPersistentVerbosity = getPersistentVerbosity . getCommandLineOptions mapPersistentVerbosity = mapCommandLineOptions . mapPersistentVerbosity modifyPersistentVerbosity :: (PersistentVerbosity -> PersistentVerbosity) -> TCM () modifyPersistentVerbosity = modify . mapPersistentVerbosity putPersistentVerbosity :: PersistentVerbosity -> TCM () putPersistentVerbosity = modify . setPersistentVerbosity Agda-2.5.3/src/full/Agda/Syntax/0000755000000000000000000000000013154613124014416 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Syntax/IdiomBrackets.hs0000644000000000000000000000343713154613124017501 0ustar0000000000000000module Agda.Syntax.IdiomBrackets (parseIdiomBrackets) where import Control.Applicative import Control.Monad import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Syntax.Concrete import Agda.Syntax.Concrete.Operators import Agda.Syntax.Scope.Monad import Agda.TypeChecking.Monad import Agda.Utils.Pretty ( prettyShow ) parseIdiomBrackets :: Range -> Expr -> ScopeM Expr parseIdiomBrackets r e = do let qPure = QName $ Name noRange [Id "pure"] qAp = QName $ Name noRange [Hole, Id "<*>", Hole] ePure = App r (Ident qPure) . defaultNamedArg eAp a b = App r (App r (Ident qAp) (defaultNamedArg a)) (defaultNamedArg b) mapM_ ensureInScope [qPure, qAp] case e of RawApp _ es -> do e : es <- appViewM =<< parseApplication es return $ foldl eAp (ePure e) es _ -> return $ ePure e appViewM :: Expr -> ScopeM [Expr] appViewM e = case e of App{} -> let AppView e' es = appView e in (e' :) <$> mapM onlyVisible es OpApp _ op _ es -> (Ident op :) <$> mapM (ordinary <=< noPlaceholder <=< onlyVisible) es _ -> return [e] where onlyVisible a | defaultNamedArg () == (fmap (() <$) a) = return $ namedArg a | otherwise = genericError $ "Only regular arguments are allowed in idiom brackets (no implicit or instance arguments)" noPlaceholder Placeholder{} = genericError "Naked sections are not allowed in idiom brackets" noPlaceholder (NoPlaceholder _ x) = return x ordinary (Ordinary a) = return a ordinary _ = genericError "Binding syntax is not allowed in idiom brackets" ensureInScope :: QName -> ScopeM () ensureInScope q = do r <- resolveName q case r of UnknownName -> genericError $ prettyShow q ++ " needs to be in scope to use idiom brackets (| ... |)" _ -> return () Agda-2.5.3/src/full/Agda/Syntax/Reflected.hs0000644000000000000000000000247313154613124016655 0ustar0000000000000000{-# OPTIONS_GHC -fwarn-missing-signatures #-} module Agda.Syntax.Reflected where import Agda.Syntax.Common import Agda.Syntax.Literal import Agda.Syntax.Abstract.Name type Args = [Arg Term] data Elim' a = Apply (Arg a) -- no record projections for now deriving (Show) type Elim = Elim' Term type Elims = [Elim] argsToElims :: Args -> Elims argsToElims = map Apply data Abs a = Abs String a deriving (Show) data Term = Var Int Elims | Con QName Elims | Def QName Elims | Meta MetaId Elims | Lam Hiding (Abs Term) | ExtLam [Clause] Elims | Pi (Dom Type) (Abs Type) | Sort Sort | Lit Literal | Unknown deriving (Show) type Type = Term data Sort = SetS Term | LitS Integer | UnknownS deriving (Show) data Pattern = ConP QName [Arg Pattern] | DotP | VarP String | LitP Literal | AbsurdP | ProjP QName deriving (Show) data Clause = Clause [Arg Pattern] Term | AbsurdClause [Arg Pattern] deriving (Show) data Definition = FunDef Type [Clause] | DataDef -- nothing for now | RecordDef -- nothing for now | DataConstructor | Axiom | Primitive deriving (Show) Agda-2.5.3/src/full/Agda/Syntax/Fixity.hs-boot0000644000000000000000000000041013154613124017162 0ustar0000000000000000module Agda.Syntax.Fixity where import Control.DeepSeq (NFData) import Data.Data (Data) import Agda.Syntax.Position ( KillRange ) data Fixity' instance KillRange Fixity' instance Data Fixity' instance NFData Fixity' instance Show Fixity' noFixity' :: Fixity' Agda-2.5.3/src/full/Agda/Syntax/Fixity.hs0000644000000000000000000003330613154613124016233 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-| Definitions for fixity, precedence levels, and declared syntax. -} module Agda.Syntax.Fixity where import Prelude hiding (concatMap) import Control.DeepSeq import Data.Foldable import Data.Function import qualified Data.List as List import Data.Maybe import Data.Set (Set) import qualified Data.Set as Set import Data.Traversable import Data.Data (Data) import Data.Typeable (Typeable) import Agda.Syntax.Position import Agda.Syntax.Common import qualified Agda.Syntax.Abstract.Name as A import Agda.Syntax.Concrete.Name import Agda.Syntax.Notation import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible -- * Notation coupled with 'Fixity' -- | The notation is handled as the fixity in the renamer. -- Hence, they are grouped together in this type. data Fixity' = Fixity' { theFixity :: !Fixity , theNotation :: Notation , theNameRange :: Range -- ^ Range of the name in the fixity declaration -- (used for correct highlighting, see issue #2140). } deriving (Typeable, Data, Show) instance Eq Fixity' where Fixity' f n _ == Fixity' f' n' _ = f == f' && n == n' -- | Decorating something with @Fixity'@. data ThingWithFixity x = ThingWithFixity x Fixity' deriving (Functor, Foldable, Traversable, Typeable, Data, Show) -- | All the notation information related to a name. data NewNotation = NewNotation { notaName :: QName , notaNames :: Set A.Name -- ^ The names the syntax and/or fixity belong to. -- -- Invariant: The set is non-empty. Every name in the list matches -- 'notaName'. , notaFixity :: Fixity -- ^ Associativity and precedence (fixity) of the names. , notation :: Notation -- ^ Syntax associated with the names. , notaIsOperator :: Bool -- ^ True if the notation comes from an operator (rather than a -- syntax declaration). } deriving (Typeable, Show) -- | If an operator has no specific notation, then it is computed from -- its name. namesToNotation :: QName -> A.Name -> NewNotation namesToNotation q n = NewNotation { notaName = q , notaNames = Set.singleton n , notaFixity = f , notation = if null syn then syntaxOf (unqualify q) else syn , notaIsOperator = null syn } where Fixity' f syn _ = A.nameFixity n -- | Replace 'noFixity' by 'defaultFixity'. useDefaultFixity :: NewNotation -> NewNotation useDefaultFixity n | notaFixity n == noFixity = n { notaFixity = defaultFixity } | otherwise = n -- | Return the 'IdPart's of a notation, the first part qualified, -- the other parts unqualified. -- This allows for qualified use of operators, e.g., -- @M.for x ∈ xs return e@, or @x ℕ.+ y@. notationNames :: NewNotation -> [QName] notationNames (NewNotation q _ _ parts _) = zipWith ($) (reQualify : repeat QName) [Name noRange [Id x] | IdPart x <- parts ] where -- The qualification of @q@. modules = init (qnameParts q) -- Putting the qualification onto @x@. reQualify x = List.foldr Qual (QName x) modules -- | Create a 'Notation' (without binders) from a concrete 'Name'. -- Does the obvious thing: -- 'Hole's become 'NormalHole's, 'Id's become 'IdParts'. -- If 'Name' has no 'Hole's, it returns 'noNotation'. syntaxOf :: Name -> Notation syntaxOf (NoName _ _) = noNotation syntaxOf (Name _ [_]) = noNotation syntaxOf (Name _ xs) = mkSyn 0 xs where -- Turn a concrete name into a Notation, -- numbering the holes from left to right. -- Result will have no 'BindingHole's. mkSyn :: Int -> [NamePart] -> Notation mkSyn n [] = [] mkSyn n (Hole : xs) = NormalHole (defaultNamedArg n) : mkSyn (1 + n) xs mkSyn n (Id x : xs) = IdPart x : mkSyn n xs noFixity' :: Fixity' noFixity' = Fixity' noFixity noNotation noRange -- | Merges 'NewNotation's that have the same precedence level and -- notation, with two exceptions: -- -- * Operators and notations coming from syntax declarations are kept -- separate. -- -- * If /all/ instances of a given 'NewNotation' have the same -- precedence level or are \"unrelated\", then they are merged. They -- get the given precedence level, if any, and otherwise they become -- unrelated (but related to each other). -- -- If 'NewNotation's that are merged have distinct associativities, -- then they get 'NonAssoc' as their associativity. -- -- Precondition: No 'A.Name' may occur in more than one list element. -- Every 'NewNotation' must have the same 'notaName'. -- -- Postcondition: No 'A.Name' occurs in more than one list element. mergeNotations :: [NewNotation] -> [NewNotation] mergeNotations = map merge . concatMap groupIfLevelsMatch . groupOn (\n -> ( notation n , notaIsOperator n )) where groupIfLevelsMatch :: [NewNotation] -> [[NewNotation]] groupIfLevelsMatch [] = __IMPOSSIBLE__ groupIfLevelsMatch ns@(n : _) = if allEqual (map fixityLevel related) then [sameAssoc (sameLevel ns)] else map (: []) ns where -- Fixities of operators whose precedence level is not Unrelated. related = mapMaybe (\f -> case fixityLevel f of Unrelated -> Nothing Related {} -> Just f) (map notaFixity ns) -- Precondition: All related operators have the same precedence -- level. -- -- Gives all unrelated operators the same level. sameLevel = map (set (_notaFixity . _fixityLevel) level) where level = case related of f : _ -> fixityLevel f [] -> Unrelated -- If all related operators have the same associativity, then the -- unrelated operators get the same associativity, and otherwise -- all operators get the associativity NonAssoc. sameAssoc = map (set (_notaFixity . _fixityAssoc) assoc) where assoc = case related of f : _ | allEqual (map fixityAssoc related) -> fixityAssoc f _ -> NonAssoc merge :: [NewNotation] -> NewNotation merge [] = __IMPOSSIBLE__ merge ns@(n : _) = n { notaNames = Set.unions $ map notaNames ns } -- * Sections -- | Sections, as well as non-sectioned operators. data NotationSection = NotationSection { sectNotation :: NewNotation , sectKind :: NotationKind -- ^ For non-sectioned operators this should match the notation's -- 'notationKind'. , sectLevel :: Maybe PrecedenceLevel -- ^ Effective precedence level. 'Nothing' for closed notations. , sectIsSection :: Bool -- ^ 'False' for non-sectioned operators. } deriving Show -- | Converts a notation to a (non-)section. noSection :: NewNotation -> NotationSection noSection n = NotationSection { sectNotation = n , sectKind = notationKind (notation n) , sectLevel = Just (fixityLevel (notaFixity n)) , sectIsSection = False } -- * Fixity -- | Precedence levels for operators. data PrecedenceLevel = Unrelated -- ^ No fixity declared. | Related !Integer -- ^ Fixity level declared as the @Integer@. deriving (Eq, Ord, Show, Typeable, Data) -- | Associativity. data Associativity = NonAssoc | LeftAssoc | RightAssoc deriving (Eq, Ord, Show, Typeable, Data) -- | Fixity of operators. data Fixity = Fixity { fixityRange :: Range -- ^ Range of the whole fixity declaration. , fixityLevel :: !PrecedenceLevel , fixityAssoc :: !Associativity } deriving (Typeable, Data, Show) instance Eq Fixity where f1 == f2 = compare f1 f2 == EQ instance Ord Fixity where compare = compare `on` (\f -> (fixityLevel f, fixityAssoc f)) -- For @instance Pretty Fixity@, see Agda.Syntax.Concrete.Pretty noFixity :: Fixity noFixity = Fixity noRange Unrelated NonAssoc defaultFixity :: Fixity defaultFixity = Fixity noRange (Related 20) NonAssoc -- * Precendence -- | Precedence is associated with a context. data Precedence = TopCtx | FunctionSpaceDomainCtx | LeftOperandCtx Fixity | RightOperandCtx Fixity | FunctionCtx | ArgumentCtx | InsideOperandCtx | WithFunCtx | WithArgCtx | DotPatternCtx deriving (Show, Typeable, Data, Eq) instance Pretty Precedence where pretty = text . show -- | When printing we keep track of a stack of precedences in order to be able -- to decide whether it's safe to leave out parens around lambdas. An empty -- stack is equivalent to `TopCtx`. Invariant: `notElem TopCtx`. type PrecedenceStack = [Precedence] pushPrecedence :: Precedence -> PrecedenceStack -> PrecedenceStack pushPrecedence TopCtx _ = [] pushPrecedence p ps = p : ps headPrecedence :: PrecedenceStack -> Precedence headPrecedence [] = TopCtx headPrecedence (p : _) = p -- | The precedence corresponding to a possibly hidden argument. hiddenArgumentCtx :: Hiding -> Precedence hiddenArgumentCtx NotHidden = ArgumentCtx hiddenArgumentCtx Hidden = TopCtx hiddenArgumentCtx Instance{} = TopCtx -- | Do we need to bracket an operator application of the given fixity -- in a context with the given precedence. opBrackets :: Fixity -> PrecedenceStack -> Bool opBrackets = opBrackets' False -- | Do we need to bracket an operator application of the given fixity -- in a context with the given precedence. opBrackets' :: Bool -> -- Is the last argument a parenless lambda? Fixity -> PrecedenceStack -> Bool opBrackets' isLam f ps = brack f (headPrecedence ps) where false = isLam && lamBrackets' False ps -- require more parens for `e >>= λ x → e₁` than `e >>= e₁` brack (Fixity _ (Related n1) LeftAssoc) (LeftOperandCtx (Fixity _ (Related n2) LeftAssoc)) | n1 >= n2 = false brack (Fixity _ (Related n1) RightAssoc) (RightOperandCtx (Fixity _ (Related n2) RightAssoc)) | n1 >= n2 = false brack f1 (LeftOperandCtx f2) | Related f1 <- fixityLevel f1 , Related f2 <- fixityLevel f2 , f1 > f2 = false brack f1 (RightOperandCtx f2) | Related f1 <- fixityLevel f1 , Related f2 <- fixityLevel f2 , f1 > f2 = false brack _ TopCtx = false brack _ FunctionSpaceDomainCtx = false brack _ InsideOperandCtx = false brack _ WithArgCtx = false brack _ WithFunCtx = false brack _ _ = True -- | Does a lambda-like thing (lambda, let or pi) need brackets in the -- given context? A peculiar thing with lambdas is that they don't -- need brackets in certain right operand contexts. To decide we need to look -- at the stack of precedences and not just the current precedence. -- Example: @m₁ >>= (λ x → x) >>= m₂@ (for @_>>=_@ left associative). -- The first argument is the parenthesis preference. If True, we add -- parentheses also in right operand contexts where they aren't strictly -- needed. lamBrackets' :: Bool -> PrecedenceStack -> Bool lamBrackets' _ [] = False lamBrackets' strict (p : ps) = case p of TopCtx -> __IMPOSSIBLE__ ArgumentCtx -> strict || lamBrackets' strict ps RightOperandCtx{} -> strict || lamBrackets' strict ps FunctionSpaceDomainCtx -> True LeftOperandCtx{} -> True FunctionCtx -> True InsideOperandCtx -> True WithFunCtx -> True WithArgCtx -> True DotPatternCtx -> True -- | Preference for adding parentheses. lamBrackets :: PrecedenceStack -> Bool lamBrackets = lamBrackets' True -- | Does a function application need brackets? appBrackets :: PrecedenceStack -> Bool appBrackets = appBrackets' False -- | Does a function application need brackets? appBrackets' :: Bool -> -- Is the argument of the application a parenless lambda? PrecedenceStack -> Bool appBrackets' isLam ps = brack (headPrecedence ps) where brack ArgumentCtx = True brack DotPatternCtx = True brack _ = isLam && lamBrackets' False ps -- allow e + e₁ λ x → e₂ -- | Does a with application need brackets? withAppBrackets :: PrecedenceStack -> Bool withAppBrackets = brack . headPrecedence where brack TopCtx = False brack FunctionSpaceDomainCtx = False brack WithFunCtx = False brack _ = True -- | Does a function space need brackets? piBrackets :: PrecedenceStack -> Bool piBrackets [] = False piBrackets _ = True roundFixBrackets :: PrecedenceStack -> Bool roundFixBrackets ps = DotPatternCtx == headPrecedence ps instance HasRange Fixity where getRange = fixityRange instance KillRange Fixity where killRange f = f { fixityRange = noRange } instance KillRange Fixity' where killRange (Fixity' f n r) = killRange3 Fixity' f n r instance KillRange x => KillRange (ThingWithFixity x) where killRange (ThingWithFixity c f) = ThingWithFixity (killRange c) f -- * Some lenses _notaFixity :: Lens' Fixity NewNotation _notaFixity f r = f (notaFixity r) <&> \x -> r { notaFixity = x } _fixityAssoc :: Lens' Associativity Fixity _fixityAssoc f r = f (fixityAssoc r) <&> \x -> r { fixityAssoc = x } _fixityLevel :: Lens' PrecedenceLevel Fixity _fixityLevel f r = f (fixityLevel r) <&> \x -> r { fixityLevel = x } ------------------------------------------------------------------------ -- * Printing ------------------------------------------------------------------------ -- deriving instance Show Fixity' ------------------------------------------------------------------------ -- * NFData instances ------------------------------------------------------------------------ instance NFData Fixity' where rnf (Fixity' _ a _) = rnf a -- | Ranges are not forced. instance NFData Fixity where rnf (Fixity _ _ _) = () Agda-2.5.3/src/full/Agda/Syntax/Position.hs0000644000000000000000000007430613154613124016570 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE NoMonomorphismRestriction #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE OverlappingInstances #-} #endif {-| Position information for syntax. Crucial for giving good error messages. -} module Agda.Syntax.Position ( -- * Positions Position , PositionWithoutFile , Position'(..) , SrcFile , positionInvariant , startPos , movePos , movePosByString , backupPos , startPos' -- * Intervals , Interval , IntervalWithoutFile , Interval'(..) , intervalInvariant , posToInterval , takeI , dropI , getIntervalFile , iLength , fuseIntervals , setIntervalFile -- * Ranges , Range , Range'(..) , rangeInvariant , consecutiveAndSeparated , intervalsToRange , intervalToRange , rangeIntervals , rangeFile , rightMargin , noRange , posToRange, posToRange' , rStart, rStart' , rEnd, rEnd' , rangeToInterval , rangeToIntervalWithFile , continuous , continuousPerLine , PrintRange(..) , HasRange(..) , SetRange(..) , KillRange(..) , KillRangeT , killRangeMap , killRange1, killRange2, killRange3, killRange4, killRange5, killRange6, killRange7 , killRange8, killRange9, killRange10, killRange11, killRange12, killRange13, killRange14 , killRange15, killRange16, killRange17, killRange18, killRange19 , withRangeOf , fuseRange , fuseRanges , beginningOf , beginningOfFile , interleaveRanges ) where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad import Control.Monad.Writer (runWriter, Writer, tell) import Data.Foldable (Foldable) import qualified Data.Foldable as Fold import Data.Function import Data.Int import Data.List hiding (null) import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import Data.Traversable (Traversable) import Data.Data (Data) import Data.Typeable (Typeable) import Data.Sequence (Seq) import qualified Data.Sequence as Seq import Data.Void import GHC.Generics (Generic) import Agda.Utils.FileName import Agda.Utils.List import qualified Agda.Utils.Maybe.Strict as Strict import Agda.Utils.Null import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible {-------------------------------------------------------------------------- Types and classes --------------------------------------------------------------------------} -- | Represents a point in the input. -- -- If two positions have the same 'srcFile' and 'posPos' components, -- then the final two components should be the same as well, but since -- this can be hard to enforce the program should not rely too much on -- the last two components; they are mainly there to improve error -- messages for the user. -- -- Note the invariant which positions have to satisfy: 'positionInvariant'. data Position' a = Pn { srcFile :: !a -- ^ File. , posPos :: !Int32 -- ^ Position, counting from 1. , posLine :: !Int32 -- ^ Line number, counting from 1. , posCol :: !Int32 -- ^ Column number, counting from 1. } deriving (Data, Typeable, Functor, Foldable, Traversable, Generic) positionInvariant :: Position' a -> Bool positionInvariant p = posPos p > 0 && posLine p > 0 && posCol p > 0 importantPart :: Position' a -> (a, Int32) importantPart p = (srcFile p, posPos p) instance Eq a => Eq (Position' a) where (==) = (==) `on` importantPart instance Ord a => Ord (Position' a) where compare = compare `on` importantPart type SrcFile = Strict.Maybe AbsolutePath type Position = Position' SrcFile type PositionWithoutFile = Position' () -- | An interval. The @iEnd@ position is not included in the interval. -- -- Note the invariant which intervals have to satisfy: 'intervalInvariant'. data Interval' a = Interval { iStart, iEnd :: !(Position' a) } deriving (Data, Typeable, Eq, Ord, Functor, Foldable, Traversable, Generic) type Interval = Interval' SrcFile type IntervalWithoutFile = Interval' () intervalInvariant :: Ord a => Interval' a -> Bool intervalInvariant i = all positionInvariant [iStart i, iEnd i] && iStart i <= iEnd i && srcFile (iStart i) == srcFile (iEnd i) -- | Sets the 'srcFile' components of the interval. setIntervalFile :: a -> Interval' b -> Interval' a setIntervalFile f (Interval p1 p2) = Interval (p1 { srcFile = f }) (p2 { srcFile = f }) -- | Gets the 'srcFile' component of the interval. Because of the invariant, -- they are both the same. getIntervalFile :: Interval' a -> a getIntervalFile = srcFile . iStart -- | Converts a file name and two positions to an interval. posToInterval :: a -> PositionWithoutFile -> PositionWithoutFile -> Interval' a posToInterval f p1 p2 = setIntervalFile f $ if p1 < p2 then Interval p1 p2 else Interval p2 p1 -- | The length of an interval. iLength :: Interval' a -> Int32 iLength i = posPos (iEnd i) - posPos (iStart i) -- | A range is a file name, plus a sequence of intervals, assumed to -- point to the given file. The intervals should be consecutive and -- separated. -- -- Note the invariant which ranges have to satisfy: 'rangeInvariant'. data Range' a = NoRange | Range !a (Seq IntervalWithoutFile) deriving (Typeable, Data, Eq, Ord, Functor, Foldable, Traversable, Generic) type Range = Range' SrcFile instance Null (Range' a) where null NoRange = True null Range{} = False empty = NoRange -- | The intervals that make up the range. The intervals are -- consecutive and separated ('consecutiveAndSeparated'). rangeIntervals :: Range' a -> [IntervalWithoutFile] rangeIntervals NoRange = [] rangeIntervals (Range _ is) = Fold.toList is -- | Turns a file name plus a list of intervals into a range. -- -- Precondition: 'consecutiveAndSeparated'. intervalsToRange :: a -> [IntervalWithoutFile] -> Range' a intervalsToRange _ [] = NoRange intervalsToRange f is = Range f (Seq.fromList is) -- | Are the intervals consecutive and separated, do they all point to -- the same file, and do they satisfy the interval invariant? consecutiveAndSeparated :: Ord a => [Interval' a] -> Bool consecutiveAndSeparated is = all intervalInvariant is && allEqual (map (srcFile . iStart) is) && (null is || and (zipWith (<) (map iEnd (init is)) (map iStart (tail is)))) -- | Range invariant. rangeInvariant :: Ord a => Range' a -> Bool rangeInvariant r = consecutiveAndSeparated (rangeIntervals r) && case r of Range _ is -> not (null is) NoRange -> True -- | The file the range is pointing to. rangeFile :: Range -> SrcFile rangeFile NoRange = Strict.Nothing rangeFile (Range f _) = f -- | Conflate a range to its right margin. rightMargin :: Range -> Range rightMargin r@NoRange = r rightMargin r@(Range f is) = case Seq.viewr is of Seq.EmptyR -> __IMPOSSIBLE__ _ Seq.:> i -> intervalToRange f (i { iStart = iEnd i }) -- | Wrapper to indicate that range should be printed. newtype PrintRange a = PrintRange a deriving (Eq, Ord, HasRange, SetRange, KillRange) -- | Things that have a range are instances of this class. class HasRange t where getRange :: t -> Range instance HasRange Interval where getRange i = intervalToRange (srcFile (iStart i)) (setIntervalFile () i) instance HasRange Range where getRange = id instance HasRange Bool where getRange _ = noRange -- | Precondition: The ranges of the list elements must point to the -- same file (or be empty). instance HasRange a => HasRange [a] where getRange = foldr fuseRange noRange -- | Precondition: The ranges of the tuple elements must point to the -- same file (or be empty). instance (HasRange a, HasRange b) => HasRange (a,b) where getRange = uncurry fuseRange -- | Precondition: The ranges of the tuple elements must point to the -- same file (or be empty). instance (HasRange a, HasRange b, HasRange c) => HasRange (a,b,c) where getRange (x,y,z) = getRange (x,(y,z)) -- | Precondition: The ranges of the tuple elements must point to the -- same file (or be empty). instance (HasRange a, HasRange b, HasRange c, HasRange d) => HasRange (a,b,c,d) where getRange (x,y,z,w) = getRange (x,(y,(z,w))) -- | Precondition: The ranges of the tuple elements must point to the -- same file (or be empty). instance (HasRange a, HasRange b, HasRange c, HasRange d, HasRange e) => HasRange (a,b,c,d,e) where getRange (x,y,z,w,v) = getRange (x,(y,(z,(w,v)))) -- | Precondition: The ranges of the tuple elements must point to the -- same file (or be empty). instance (HasRange a, HasRange b, HasRange c, HasRange d, HasRange e, HasRange f) => HasRange (a,b,c,d,e,f) where getRange (x,y,z,w,v,u) = getRange (x,(y,(z,(w,(v,u))))) -- | Precondition: The ranges of the tuple elements must point to the -- same file (or be empty). instance (HasRange a, HasRange b, HasRange c, HasRange d, HasRange e, HasRange f, HasRange g) => HasRange (a,b,c,d,e,f,g) where getRange (x,y,z,w,v,u,t) = getRange (x,(y,(z,(w,(v,(u,t)))))) instance HasRange a => HasRange (Maybe a) where getRange Nothing = noRange getRange (Just a) = getRange a instance (HasRange a, HasRange b) => HasRange (Either a b) where getRange = either getRange getRange -- | If it is also possible to set the range, this is the class. -- -- Instances should satisfy @'getRange' ('setRange' r x) == r@. class HasRange t => SetRange t where setRange :: Range -> t -> t instance SetRange Range where setRange = const instance SetRange a => SetRange [a] where setRange r = fmap $ setRange r -- | Killing the range of an object sets all range information to 'noRange'. class KillRange a where killRange :: KillRangeT a type KillRangeT a = a -> a -- | Remove ranges in keys and values of a map. killRangeMap :: (KillRange k, KillRange v) => KillRangeT (Map k v) killRangeMap = Map.mapKeysMonotonic killRange . Map.map killRange killRange1 :: KillRange a => (a -> b) -> a -> b killRange2 :: (KillRange a, KillRange b) => (a -> b -> c) -> a -> b -> c killRange3 :: (KillRange a, KillRange b, KillRange c) => (a -> b -> c -> d) -> a -> b -> c -> d killRange4 :: (KillRange a, KillRange b, KillRange c, KillRange d) => (a -> b -> c -> d -> e) -> a -> b -> c -> d -> e killRange5 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e ) => (a -> b -> c -> d -> e -> f) -> a -> b -> c -> d -> e -> f killRange6 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f ) => (a -> b -> c -> d -> e -> f -> g) -> a -> b -> c -> d -> e -> f -> g killRange7 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g ) => (a -> b -> c -> d -> e -> f -> g -> h) -> a -> b -> c -> d -> e -> f -> g -> h killRange8 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h ) => (a -> b -> c -> d -> e -> f -> g -> h -> i) -> a -> b -> c -> d -> e -> f -> g -> h -> i killRange9 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j killRange10 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i, KillRange j ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k killRange11 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i, KillRange j, KillRange k ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l killRange12 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i, KillRange j, KillRange k, KillRange l ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m killRange13 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i, KillRange j, KillRange k, KillRange l , KillRange m ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n killRange14 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i, KillRange j, KillRange k, KillRange l , KillRange m, KillRange n ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o killRange15 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i, KillRange j, KillRange k, KillRange l , KillRange m, KillRange n, KillRange o ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p killRange16 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i, KillRange j, KillRange k, KillRange l , KillRange m, KillRange n, KillRange o, KillRange p ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q killRange17 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i, KillRange j, KillRange k, KillRange l , KillRange m, KillRange n, KillRange o, KillRange p , KillRange q ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r killRange18 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i, KillRange j, KillRange k, KillRange l , KillRange m, KillRange n, KillRange o, KillRange p , KillRange q, KillRange r ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s killRange19 :: ( KillRange a, KillRange b, KillRange c, KillRange d , KillRange e, KillRange f, KillRange g, KillRange h , KillRange i, KillRange j, KillRange k, KillRange l , KillRange m, KillRange n, KillRange o, KillRange p , KillRange q, KillRange r, KillRange s ) => (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t) -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t killRange1 f a = f (killRange a) killRange2 f a = killRange1 (f $ killRange a) killRange3 f a = killRange2 (f $ killRange a) killRange4 f a = killRange3 (f $ killRange a) killRange5 f a = killRange4 (f $ killRange a) killRange6 f a = killRange5 (f $ killRange a) killRange7 f a = killRange6 (f $ killRange a) killRange8 f a = killRange7 (f $ killRange a) killRange9 f a = killRange8 (f $ killRange a) killRange10 f a = killRange9 (f $ killRange a) killRange11 f a = killRange10 (f $ killRange a) killRange12 f a = killRange11 (f $ killRange a) killRange13 f a = killRange12 (f $ killRange a) killRange14 f a = killRange13 (f $ killRange a) killRange15 f a = killRange14 (f $ killRange a) killRange16 f a = killRange15 (f $ killRange a) killRange17 f a = killRange16 (f $ killRange a) killRange18 f a = killRange17 (f $ killRange a) killRange19 f a = killRange18 (f $ killRange a) instance KillRange Range where killRange _ = noRange instance KillRange Void where killRange = id instance KillRange () where killRange = id instance KillRange Bool where killRange = id instance KillRange Int where killRange = id instance KillRange Integer where killRange = id #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPABLE #-} KillRange a => KillRange [a] where #else instance KillRange a => KillRange [a] where #endif killRange = map killRange -- | Overlaps with @KillRange [a]@. #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} KillRange String where #else instance KillRange String where #endif killRange = id #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPABLE #-} KillRange a => KillRange (Map k a) where #else instance KillRange a => KillRange (Map k a) where #endif killRange = fmap killRange #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPABLE #-} (Ord a, KillRange a) => KillRange (Set a) where #else instance (Ord a, KillRange a) => KillRange (Set a) where #endif killRange = Set.map killRange instance (KillRange a, KillRange b) => KillRange (a, b) where killRange (x, y) = (killRange x, killRange y) instance (KillRange a, KillRange b, KillRange c) => KillRange (a, b, c) where killRange (x, y, z) = killRange3 (,,) x y z instance (KillRange a, KillRange b, KillRange c, KillRange d) => KillRange (a, b, c, d) where killRange (x, y, z, u) = killRange4 (,,,) x y z u instance KillRange a => KillRange (Maybe a) where killRange = fmap killRange instance (KillRange a, KillRange b) => KillRange (Either a b) where killRange (Left x) = Left $ killRange x killRange (Right x) = Right $ killRange x ------------------------------------------------------------------------ -- Showing ------------------------------------------------------------------------ -- TODO: 'Show' should output Haskell-parseable representations. -- The following instances are deprecated, and Pretty should be used -- instead. Later, simply derive Show for these types. -- ASR (02 December 2014). This instance is not used anymore (module -- the test suite) when reporting errors. See Issue 1293. instance Show a => Show (Position' (Strict.Maybe a)) where show (Pn (Strict.Just f) _ l c) = show f ++ ":" ++ show l ++ "," ++ show c show (Pn Strict.Nothing _ l c) = show l ++ "," ++ show c instance Show PositionWithoutFile where show p = show (p { srcFile = Strict.Nothing } :: Position) instance Show IntervalWithoutFile where show (Interval s e) = start ++ "-" ++ end where sl = posLine s el = posLine e sc = posCol s ec = posCol e start :: String start = show sl ++ "," ++ show sc end :: String end | sl == el = show ec | otherwise = show el ++ "," ++ show ec instance Show a => Show (Interval' (Strict.Maybe a)) where show i@(Interval s _) = file ++ show (setIntervalFile () i) where file :: String file = case srcFile s of Strict.Nothing -> "" Strict.Just f -> show f ++ ":" instance Show a => Show (Range' (Strict.Maybe a)) where show r = case rangeToIntervalWithFile r of Nothing -> "" Just i -> show i instance Show a => Show (Range' (Maybe a)) where show = show . fmap Strict.toStrict ------------------------------------------------------------------------ -- Printing ------------------------------------------------------------------------ instance Pretty a => Pretty (Position' (Strict.Maybe a)) where pretty (Pn Strict.Nothing _ l c) = pretty l <> pretty "," <> pretty c pretty (Pn (Strict.Just f) _ l c) = pretty f <> pretty ":" <> pretty l <> pretty "," <> pretty c instance Pretty PositionWithoutFile where pretty p = pretty (p { srcFile = Strict.Nothing } :: Position) instance Pretty IntervalWithoutFile where pretty (Interval s e) = start <> pretty "-" <> end where sl = posLine s el = posLine e sc = posCol s ec = posCol e start :: Doc start = pretty sl <> comma <> pretty sc end :: Doc | sl == el = pretty ec | otherwise = pretty el <> comma <> pretty ec instance Pretty a => Pretty (Interval' (Strict.Maybe a)) where pretty i@(Interval s _) = file <> pretty (setIntervalFile () i) where file :: Doc file = case srcFile s of Strict.Nothing -> empty Strict.Just f -> pretty f <> colon instance Pretty a => Pretty (Range' (Strict.Maybe a)) where pretty r = case rangeToIntervalWithFile r of Nothing -> empty Just i -> pretty i instance (Pretty a, HasRange a) => Pretty (PrintRange a) where pretty (PrintRange a) = pretty a <+> parens (text "at" <+> pretty (getRange a)) {-------------------------------------------------------------------------- Functions on positions and ranges --------------------------------------------------------------------------} -- | The first position in a file: position 1, line 1, column 1. startPos' :: a -> Position' a startPos' f = Pn { srcFile = f , posPos = 1 , posLine = 1 , posCol = 1 } -- | The first position in a file: position 1, line 1, column 1. startPos :: Maybe AbsolutePath -> Position startPos = startPos' . Strict.toStrict -- | Ranges between two unknown positions noRange :: Range' a noRange = NoRange -- | Advance the position by one character. -- A newline character (@'\n'@) moves the position to the first -- character in the next line. Any other character moves the -- position to the next column. movePos :: Position' a -> Char -> Position' a movePos (Pn f p l c) '\n' = Pn f (p + 1) (l + 1) 1 movePos (Pn f p l c) _ = Pn f (p + 1) l (c + 1) -- | Advance the position by a string. -- -- > movePosByString = foldl' movePos movePosByString :: Position' a -> String -> Position' a movePosByString = foldl' movePos -- | Backup the position by one character. -- -- Precondition: The character must not be @'\n'@. backupPos :: Position' a -> Position' a backupPos (Pn f p l c) = Pn f (p - 1) l (c - 1) -- | Extracts the interval corresponding to the given string, assuming -- that the string starts at the beginning of the given interval. -- -- Precondition: The string must not be too long for the interval. takeI :: String -> Interval' a -> Interval' a takeI s i | genericLength s > iLength i = __IMPOSSIBLE__ | otherwise = i { iEnd = movePosByString (iStart i) s } -- | Removes the interval corresponding to the given string from the -- given interval, assuming that the string starts at the beginning of -- the interval. -- -- Precondition: The string must not be too long for the interval. dropI :: String -> Interval' a -> Interval' a dropI s i | genericLength s > iLength i = __IMPOSSIBLE__ | otherwise = i { iStart = movePosByString (iStart i) s } -- | Converts a file name and two positions to a range. posToRange' :: a -> PositionWithoutFile -> PositionWithoutFile -> Range' a posToRange' f p1 p2 = intervalToRange f (posToInterval () p1 p2) -- | Converts two positions to a range. -- -- Precondition: The positions have to point to the same file. posToRange :: Position' a -> Position' a -> Range' a posToRange p1 p2 = posToRange' (srcFile p1) (p1 { srcFile = () }) (p2 { srcFile = () }) -- | Converts a file name and an interval to a range. intervalToRange :: a -> IntervalWithoutFile -> Range' a intervalToRange f i = Range f (Seq.singleton i) -- | Converts a range to an interval, if possible. rangeToIntervalWithFile :: Range' a -> Maybe (Interval' a) rangeToIntervalWithFile NoRange = Nothing rangeToIntervalWithFile (Range f is) = case (Seq.viewl is, Seq.viewr is) of (head Seq.:< _, _ Seq.:> last) -> Just $ setIntervalFile f $ Interval { iStart = iStart head , iEnd = iEnd last } _ -> __IMPOSSIBLE__ -- | Converts a range to an interval, if possible. Note that the -- information about the source file is lost. rangeToInterval :: Range' a -> Maybe IntervalWithoutFile rangeToInterval NoRange = Nothing rangeToInterval (Range _ is) = case (Seq.viewl is, Seq.viewr is) of (head Seq.:< _, _ Seq.:> last) -> Just $ Interval { iStart = iStart head , iEnd = iEnd last } _ -> __IMPOSSIBLE__ -- | Returns the shortest continuous range containing the given one. continuous :: Range' a -> Range' a continuous NoRange = NoRange continuous r@(Range f _) = case rangeToInterval r of Nothing -> __IMPOSSIBLE__ Just i -> intervalToRange f i -- | Removes gaps between intervals on the same line. continuousPerLine :: Ord a => Range' a -> Range' a continuousPerLine r@NoRange = r continuousPerLine r@(Range f _) = Range f (Seq.unfoldr step (rangeIntervals r)) where step [] = Nothing step [i] = Just (i, []) step (i : is@(j : js)) | sameLine = step (fuseIntervals i j : js) | otherwise = Just (i, is) where sameLine = posLine (iEnd i) == posLine (iStart j) -- | The initial position in the range, if any. rStart' :: Range' a -> Maybe PositionWithoutFile rStart' r = iStart <$> rangeToInterval r -- | The initial position in the range, if any. rStart :: Range' a -> Maybe (Position' a) rStart NoRange = Nothing rStart r@(Range f _) = (\p -> p { srcFile = f }) <$> rStart' r -- | The position after the final position in the range, if any. rEnd' :: Range' a -> Maybe PositionWithoutFile rEnd' r = iEnd <$> rangeToInterval r -- | The position after the final position in the range, if any. rEnd :: Range' a -> Maybe (Position' a) rEnd NoRange = Nothing rEnd r@(Range f _) = (\p -> p { srcFile = f }) <$> rEnd' r -- | Finds the least interval which covers the arguments. -- -- Precondition: The intervals must point to the same file. fuseIntervals :: Ord a => Interval' a -> Interval' a -> Interval' a fuseIntervals x y = Interval { iStart = head ss, iEnd = last es } where ss = sort [iStart x, iStart y] es = sort [iEnd x, iEnd y] -- | @fuseRanges r r'@ unions the ranges @r@ and @r'@. -- -- Meaning it finds the least range @r0@ that covers @r@ and @r'@. -- -- Precondition: The ranges must point to the same file (or be empty). fuseRanges :: (Ord a) => Range' a -> Range' a -> Range' a fuseRanges NoRange is2 = is2 fuseRanges is1 NoRange = is1 fuseRanges (Range f is1) (Range _ is2) = Range f (fuse is1 is2) where fuse is1 is2 = case (Seq.viewl is1, Seq.viewr is1, Seq.viewl is2, Seq.viewr is2) of (Seq.EmptyL, _, _, _) -> is2 (_, _, Seq.EmptyL, _) -> is1 (s1 Seq.:< r1, l1 Seq.:> e1, s2 Seq.:< r2, l2 Seq.:> e2) -- Special cases. | iEnd e1 < iStart s2 -> is1 Seq.>< is2 | iEnd e2 < iStart s1 -> is2 Seq.>< is1 | iEnd e1 == iStart s2 -> mergeTouching l1 e1 s2 r2 | iEnd e2 == iStart s1 -> mergeTouching l2 e2 s1 r1 -- General cases. | iEnd s1 < iStart s2 -> outputLeftPrefix s1 r1 s2 is2 | iEnd s2 < iStart s1 -> outputLeftPrefix s2 r2 s1 is1 | iEnd s1 < iEnd s2 -> fuseSome s1 r1 s2 r2 | otherwise -> fuseSome s2 r2 s1 r1 _ -> __IMPOSSIBLE__ mergeTouching l e s r = l Seq.>< i Seq.<| r where i = Interval { iStart = iStart e, iEnd = iEnd s } -- The following two functions could use binary search instead of -- linear. outputLeftPrefix s1 r1 s2 is2 = s1 Seq.<| r1' Seq.>< fuse r1'' is2 where (r1', r1'') = Seq.spanl (\s -> iEnd s < iStart s2) r1 fuseSome s1 r1 s2 r2 = fuse r1' (fuseIntervals s1 s2 Seq.<| r2) where r1' = Seq.dropWhileL (\s -> iEnd s <= iEnd s2) r1 -- | Precondition: The ranges must point to the same file (or be -- empty). fuseRange :: (HasRange u, HasRange t) => u -> t -> Range fuseRange x y = fuseRanges (getRange x) (getRange y) -- | @beginningOf r@ is an empty range (a single, empty interval) -- positioned at the beginning of @r@. If @r@ does not have a -- beginning, then 'noRange' is returned. beginningOf :: Range -> Range beginningOf NoRange = NoRange beginningOf r@(Range f _) = case rStart' r of Nothing -> __IMPOSSIBLE__ Just pos -> posToRange' f pos pos -- | @beginningOfFile r@ is an empty range (a single, empty interval) -- at the beginning of @r@'s starting position's file. If there is no -- such position, then an empty range is returned. beginningOfFile :: Range -> Range beginningOfFile NoRange = NoRange beginningOfFile (Range f _) = posToRange' f p p where p = startPos' () -- | @x \`withRangeOf\` y@ sets the range of @x@ to the range of @y@. withRangeOf :: (SetRange t, HasRange u) => t -> u -> t x `withRangeOf` y = setRange (getRange y) x -- | Interleaves two streams of ranged elements -- -- It will report the conflicts as a list of conflicting pairs. -- In case of conflict, the element with the earliest start position -- is placed first. In case of a tie, the element with the earliest -- ending position is placed first. If both tie, the element from the -- first list is placed first. interleaveRanges :: (HasRange a) => [a] -> [a] -> ([a], [(a,a)]) interleaveRanges as bs = runWriter$ go as bs where go [] as = return as go as [] = return as go as@(a:as') bs@(b:bs') = let ra = getRange a rb = getRange b ra0 = rStart ra rb0 = rStart rb ra1 = rEnd ra rb1 = rEnd rb in if ra1 <= rb0 then (a:) <$> go as' bs else if rb1 <= ra0 then (b:) <$> go as bs' else do tell [(a,b)] if ra0 < rb0 || (ra0 == rb0 && ra1 <= rb1) then (a:) <$> go as' bs else (b:) <$> go as bs' Agda-2.5.3/src/full/Agda/Syntax/Treeless.hs0000644000000000000000000001274613154613124016552 0ustar0000000000000000{-# LANGUAGE DeriveDataTypeable #-} -- | The treeless syntax is intended to be used as input for the compiler backends. -- It is more low-level than Internal syntax and is not used for type checking. -- -- Some of the features of treeless syntax are: -- - case expressions instead of case trees -- - no instantiated datatypes / constructors module Agda.Syntax.Treeless ( module Agda.Syntax.Abstract.Name , module Agda.Syntax.Treeless ) where import Control.Arrow (first, second) import Data.Map (Map) import Data.Data (Data) import Data.Typeable (Typeable) import Agda.Syntax.Position import Agda.Syntax.Literal import Agda.Syntax.Abstract.Name data Compiled = Compiled { cTreeless :: TTerm , cArgUsage :: [Bool] } deriving (Typeable, Data, Show, Eq, Ord) type Args = [TTerm] -- this currently assumes that TApp is translated in a lazy/cbn fashion. -- The AST should also support strict translation. -- -- All local variables are using de Bruijn indices. data TTerm = TVar Int | TPrim TPrim | TDef QName | TApp TTerm Args | TLam TTerm | TLit Literal | TCon QName | TLet TTerm TTerm -- ^ introduces a new local binding. The bound term -- MUST only be evaluated if it is used inside the body. -- Sharing may happen, but is optional. -- It is also perfectly valid to just inline the bound term in the body. | TCase Int CaseType TTerm [TAlt] -- ^ Case scrutinee (always variable), case type, default value, alternatives -- First, all TACon alternatives are tried; then all TAGuard alternatives -- in top to bottom order. -- TACon alternatives must not overlap. | TUnit -- used for levels right now | TSort | TErased | TError TError -- ^ A runtime error, something bad has happened. deriving (Typeable, Data, Show, Eq, Ord) -- | Compiler-related primitives. This are NOT the same thing as primitives -- in Agda's surface or internal syntax! -- Some of the primitives have a suffix indicating which type of arguments they take, -- using the following naming convention: -- Char | Type -- C | Character -- F | Float -- I | Integer -- Q | QName -- S | String data TPrim = PAdd | PSub | PMul | PQuot | PRem | PGeq | PLt | PEqI | PEqF | PEqS | PEqC | PEqQ | PIf | PSeq deriving (Typeable, Data, Show, Eq, Ord) isPrimEq :: TPrim -> Bool isPrimEq p = p `elem` [PEqI, PEqF, PEqS, PEqC, PEqQ] mkTApp :: TTerm -> Args -> TTerm mkTApp x [] = x mkTApp (TApp x as) bs = TApp x (as ++ bs) mkTApp x as = TApp x as tAppView :: TTerm -> [TTerm] tAppView = view where view t = case t of TApp a bs -> view a ++ bs _ -> [t] tLetView :: TTerm -> ([TTerm], TTerm) tLetView (TLet e b) = first (e :) $ tLetView b tLetView e = ([], e) tLamView :: TTerm -> (Int, TTerm) tLamView = go 0 where go n (TLam b) = go (n + 1) b go n t = (n, t) mkTLam :: Int -> TTerm -> TTerm mkTLam n b = foldr ($) b $ replicate n TLam -- | Introduces a new binding mkLet :: TTerm -> TTerm -> TTerm mkLet x body = TLet x body tInt :: Integer -> TTerm tInt = TLit . LitNat noRange intView :: TTerm -> Maybe Integer intView (TLit (LitNat _ x)) = Just x intView _ = Nothing tPlusK :: Integer -> TTerm -> TTerm tPlusK 0 n = n tPlusK k n | k < 0 = tOp PSub n (tInt (-k)) tPlusK k n = tOp PAdd (tInt k) n -- -(k + n) tNegPlusK :: Integer -> TTerm -> TTerm tNegPlusK k n = tOp PSub (tInt (-k)) n plusKView :: TTerm -> Maybe (Integer, TTerm) plusKView (TApp (TPrim PAdd) [k, n]) | Just k <- intView k = Just (k, n) plusKView (TApp (TPrim PSub) [n, k]) | Just k <- intView k = Just (-k, n) plusKView _ = Nothing negPlusKView :: TTerm -> Maybe (Integer, TTerm) negPlusKView (TApp (TPrim PSub) [k, n]) | Just k <- intView k = Just (-k, n) negPlusKView _ = Nothing tOp :: TPrim -> TTerm -> TTerm -> TTerm tOp op a b = TApp (TPrim op) [a, b] tUnreachable :: TTerm tUnreachable = TError TUnreachable tIfThenElse :: TTerm -> TTerm -> TTerm -> TTerm tIfThenElse c i e = TApp (TPrim PIf) [c, i, e] data CaseType = CTData QName -- case on datatype | CTNat | CTInt | CTChar | CTString | CTFloat | CTQName deriving (Typeable, Data, Show, Eq, Ord) data TAlt = TACon { aCon :: QName, aArity :: Int, aBody :: TTerm } -- ^ Matches on the given constructor. If the match succeeds, -- the pattern variables are prepended to the current environment -- (pushes all existing variables aArity steps further away) | TAGuard { aGuard :: TTerm, aBody :: TTerm } -- ^ Binds no variables | TALit { aLit :: Literal, aBody:: TTerm } deriving (Typeable, Data, Show, Eq, Ord) data TError = TUnreachable -- ^ Code which is unreachable. E.g. absurd branches or missing case defaults. -- Runtime behaviour of unreachable code is undefined, but preferably -- the program will exit with an error message. The compiler is free -- to assume that this code is unreachable and to remove it. deriving (Typeable, Data, Show, Eq, Ord) class Unreachable a where -- | Checks if the given expression is unreachable or not. isUnreachable :: a -> Bool instance Unreachable TAlt where isUnreachable = isUnreachable . aBody instance Unreachable TTerm where isUnreachable (TError TUnreachable{}) = True isUnreachable (TLet _ b) = isUnreachable b isUnreachable _ = False instance KillRange Compiled where killRange c = c -- bogus, but not used anyway Agda-2.5.3/src/full/Agda/Syntax/Notation.hs0000644000000000000000000001522013154613124016545 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-| As a concrete name, a notation is a non-empty list of alternating 'IdPart's and holes. In contrast to concrete names, holes can be binders. Example: @ syntax fmap (λ x → e) xs = for x ∈ xs return e @ The declared notation for @fmap@ is @for_∈_return_@ where the first hole is a binder. -} module Agda.Syntax.Notation where import Control.Applicative import Control.DeepSeq import Control.Monad import qualified Data.List as List import Data.Maybe import Data.Data (Data) import Data.Typeable (Typeable) import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Utils.Except ( MonadError(throwError) ) import Agda.Utils.List import Agda.Utils.Impossible #include "undefined.h" -- | Data type constructed in the Happy parser; converted to 'GenPart' -- before it leaves the Happy code. data HoleName = LambdaHole { _bindHoleName :: RawName , holeName :: RawName } -- ^ @\ x -> y@; 1st argument is the bound name (unused for now). | ExprHole { holeName :: RawName } -- ^ Simple named hole with hiding. -- | Is the hole a binder? isLambdaHole :: HoleName -> Bool isLambdaHole (LambdaHole _ _) = True isLambdaHole _ = False -- | Notation as provided by the @syntax@ declaration. type Notation = [GenPart] -- | Part of a Notation data GenPart = BindHole !Int -- ^ Argument is the position of the hole (with binding) where the binding should occur. | NormalHole (NamedArg Int) -- ^ Argument is where the expression should go. | WildHole !Int -- ^ An underscore in binding position. | IdPart RawName deriving (Typeable, Data, Show, Eq, Ord) instance KillRange GenPart where killRange p = case p of IdPart x -> IdPart x BindHole i -> BindHole i WildHole i -> WildHole i NormalHole x -> NormalHole $ killRange x instance NFData GenPart where rnf (BindHole _) = () rnf (NormalHole a) = rnf a rnf (WildHole _) = () rnf (IdPart a) = rnf a -- | Get a flat list of identifier parts of a notation. stringParts :: Notation -> [RawName] stringParts gs = [ x | IdPart x <- gs ] -- | Target argument position of a part (Nothing if it is not a hole). holeTarget :: GenPart -> Maybe Int holeTarget (BindHole n) = Just n holeTarget (WildHole n) = Just n holeTarget (NormalHole n) = Just $ namedArg n holeTarget IdPart{} = Nothing -- | Is the part a hole? WildHoles don't count since they don't correspond to -- anything the user writes. isAHole :: GenPart -> Bool isAHole BindHole{} = True isAHole NormalHole{} = True isAHole WildHole{} = False isAHole IdPart{} = False -- | Is the part a normal hole? isNormalHole :: GenPart -> Bool isNormalHole NormalHole{} = True isNormalHole BindHole{} = False isNormalHole WildHole{} = False isNormalHole IdPart{} = False -- | Is the part a binder? isBindingHole :: GenPart -> Bool isBindingHole (BindHole _) = True isBindingHole (WildHole _) = True isBindingHole _ = False -- | Classification of notations. data NotationKind = InfixNotation -- ^ Ex: @_bla_blub_@. | PrefixNotation -- ^ Ex: @_bla_blub@. | PostfixNotation -- ^ Ex: @bla_blub_@. | NonfixNotation -- ^ Ex: @bla_blub@. | NoNotation deriving (Eq, Show) -- | Classify a notation by presence of leading and/or trailing -- /normal/ holes. notationKind :: Notation -> NotationKind notationKind [] = NoNotation notationKind syn = case (isNormalHole $ head syn, isNormalHole $ last syn) of (True , True ) -> InfixNotation (True , False) -> PostfixNotation (False, True ) -> PrefixNotation (False, False) -> NonfixNotation -- | From notation with names to notation with indices. -- -- Example: -- @ -- ids = ["for", "x", "∈", "xs", "return", "e"] -- holes = [ LambdaHole "x" "e", ExprHole "xs" ] -- @ -- creates the notation -- @ -- [ IdPart "for" , BindHole 0 -- , IdPart "∈" , NormalHole 1 -- , IdPart "return" , NormalHole 0 -- ] -- @ mkNotation :: [NamedArg HoleName] -> [RawName] -> Either String Notation mkNotation _ [] = throwError "empty notation is disallowed" mkNotation holes ids = do unless uniqueHoleNames $ throwError "syntax must use unique argument names" let xs :: Notation = map mkPart ids unless (isAlternating xs) $ throwError "syntax must alternate holes and non-holes" unless (isExprLinear xs) $ throwError "syntax must use holes exactly once" unless (isLambdaLinear xs) $ throwError "syntax must use binding holes exactly once" return $ insertWildHoles xs where mkPart ident = fromMaybe (IdPart ident) $ lookup ident holeMap holeNumbers = [0 .. length holes - 1] numberedHoles = zip holeNumbers holes -- The WildHoles don't correspond to anything in the right-hand side so -- we add them next to their corresponding body. Slightly subtle: due to -- the way the operator parsing works they can't be added first or last. insertWildHoles xs = foldr ins xs wilds where wilds = [ i | (_, WildHole i) <- holeMap ] ins w (NormalHole h : hs) | namedArg h == w = NormalHole h : WildHole w : hs ins w (h : hs) = h : insBefore w hs ins _ [] = __IMPOSSIBLE__ insBefore w (NormalHole h : hs) | namedArg h == w = WildHole w : NormalHole h : hs insBefore w (h : hs) = h : insBefore w hs insBefore _ [] = __IMPOSSIBLE__ -- Create a map (association list) from hole names to holes. -- A @LambdaHole@ contributes two entries: -- both names are mapped to the same number, -- but distinguished by BindHole vs. NormalHole. holeMap = do (i, h) <- numberedHoles let normalHole = NormalHole $ fmap (i <$) h case namedArg h of ExprHole y -> [(y, normalHole)] LambdaHole "_" y -> [("_", WildHole i), (y, normalHole)] LambdaHole x y -> [(x, BindHole i), (y, normalHole)] -- Check whether all hole names are distinct. -- The hole names are the keys of the @holeMap@. uniqueHoleNames = distinct [ x | (x, _) <- holeMap, x /= "_" ] isExprLinear xs = List.sort [ i | x <- xs, isNormalHole x, let Just i = holeTarget x ] == holeNumbers isLambdaLinear xs = List.sort [ x | BindHole x <- xs ] == [ i | (i, h) <- numberedHoles, LambdaHole x _ <- [namedArg h], x /= "_" ] isAlternating :: [GenPart] -> Bool isAlternating [] = __IMPOSSIBLE__ isAlternating [x] = True isAlternating (x:y:xs) = isAHole x /= isAHole y && isAlternating (y:xs) noNotation :: Notation noNotation = [] Agda-2.5.3/src/full/Agda/Syntax/Literal.hs0000644000000000000000000001116013154613124016345 0ustar0000000000000000{-# LANGUAGE DeriveDataTypeable #-} module Agda.Syntax.Literal where import Control.DeepSeq import Data.Char import Data.Typeable (Typeable) import Data.Data (Data) import Numeric.IEEE ( IEEE(identicalIEEE) ) import Agda.Syntax.Position import Agda.Syntax.Common import Agda.Syntax.Abstract.Name import Agda.Utils.Pretty import Agda.Utils.FileName data Literal = LitNat Range !Integer | LitFloat Range !Double | LitString Range String | LitChar Range !Char | LitQName Range QName | LitMeta Range AbsolutePath MetaId deriving (Typeable, Data) instance Show Literal where showsPrec p l = showParen (p > 9) $ case l of LitNat _ n -> sh "LitNat _" n LitFloat _ x -> sh "LitFloat _" x LitString _ s -> sh "LitString _" s LitChar _ c -> sh "LitChar _" c LitQName _ q -> sh "LitQName _" q LitMeta _ _ x -> sh "LitMeta _ _" x where sh :: Show a => String -> a -> ShowS sh c x = showString (c ++ " ") . shows x instance Pretty Literal where pretty (LitNat _ n) = text $ show n pretty (LitFloat _ d) = text $ show d pretty (LitString _ s) = text $ showString' s "" pretty (LitChar _ c) = text $ "'" ++ showChar' c "" ++ "'" pretty (LitQName _ x) = pretty x pretty (LitMeta _ _ x) = pretty x showString' :: String -> ShowS showString' s = foldr (.) id $ [ showString "\"" ] ++ map showChar' s ++ [ showString "\"" ] showChar' :: Char -> ShowS showChar' '"' = showString "\\\"" showChar' c | escapeMe c = showLitChar c | otherwise = showString [c] where escapeMe c = not (isPrint c) || c == '\\' instance Eq Literal where LitNat _ n == LitNat _ m = n == m -- ASR (2016-09-29). We use bitwise equality for comparing Double -- because Haskell's Eq, which equates 0.0 and -0.0, allows to prove -- a contradiction (see Issue #2169). LitFloat _ x == LitFloat _ y = identicalIEEE x y || (isNaN x && isNaN y) LitString _ s == LitString _ t = s == t LitChar _ c == LitChar _ d = c == d LitQName _ x == LitQName _ y = x == y LitMeta _ f x == LitMeta _ g y = (f, x) == (f, y) _ == _ = False instance Ord Literal where LitNat _ n `compare` LitNat _ m = n `compare` m LitFloat _ x `compare` LitFloat _ y = compareFloat x y LitString _ s `compare` LitString _ t = s `compare` t LitChar _ c `compare` LitChar _ d = c `compare` d LitQName _ x `compare` LitQName _ y = x `compare` y LitMeta _ f x `compare` LitMeta _ g y = (f, x) `compare` (g, y) compare LitNat{} _ = LT compare _ LitNat{} = GT compare LitFloat{} _ = LT compare _ LitFloat{} = GT compare LitString{} _ = LT compare _ LitString{} = GT compare LitChar{} _ = LT compare _ LitChar{} = GT compare LitQName{} _ = LT compare _ LitQName{} = GT -- compare LitMeta{} _ = LT -- compare _ LitMeta{} = GT -- NOTE: This is not the same ordering as primFloatNumericalEquality! -- This ordering must be a total order of all allowed float values, -- while primFloatNumericalEquality is only a preorder compareFloat :: Double -> Double -> Ordering compareFloat x y | identicalIEEE x y = EQ | isNegInf x = LT | isNegInf y = GT | isNaN x && isNaN y = EQ | isNaN x = LT | isNaN y = GT | isNegativeZero x && x == y = LT | isNegativeZero y && x == y = GT | otherwise = compare x y where isNegInf z = z < 0 && isInfinite z instance HasRange Literal where getRange (LitNat r _) = r getRange (LitFloat r _) = r getRange (LitString r _) = r getRange (LitChar r _) = r getRange (LitQName r _) = r getRange (LitMeta r _ _) = r instance SetRange Literal where setRange r (LitNat _ x) = LitNat r x setRange r (LitFloat _ x) = LitFloat r x setRange r (LitString _ x) = LitString r x setRange r (LitChar _ x) = LitChar r x setRange r (LitQName _ x) = LitQName r x setRange r (LitMeta _ f x) = LitMeta r f x instance KillRange Literal where killRange (LitNat r x) = LitNat (killRange r) x killRange (LitFloat r x) = LitFloat (killRange r) x killRange (LitString r x) = LitString (killRange r) x killRange (LitChar r x) = LitChar (killRange r) x killRange (LitQName r x) = killRange2 LitQName r x killRange (LitMeta r f x) = LitMeta (killRange r) f x -- | Ranges are not forced. instance NFData Literal where rnf (LitNat _ _) = () rnf (LitFloat _ _) = () rnf (LitString _ a) = rnf a rnf (LitChar _ _) = () rnf (LitQName _ a) = rnf a rnf (LitMeta _ _ x) = rnf x Agda-2.5.3/src/full/Agda/Syntax/Info.hs0000644000000000000000000001742613154613124015657 0ustar0000000000000000{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-| An info object contains additional information about a piece of abstract syntax that isn't part of the actual syntax. For instance, it might contain the source code position of an expression or the concrete syntax that an internal expression originates from. -} module Agda.Syntax.Info where import Prelude hiding (null) import Data.Data (Data) import Data.Typeable (Typeable) import qualified Agda.Syntax.Concrete.Name as C import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Syntax.Concrete import Agda.Syntax.Fixity import Agda.Syntax.Scope.Base (ScopeInfo, emptyScopeInfo) import Agda.Utils.Function import Agda.Utils.Null {-------------------------------------------------------------------------- Meta information --------------------------------------------------------------------------} data MetaInfo = MetaInfo { metaRange :: Range , metaScope :: ScopeInfo , metaNumber :: Maybe MetaId , metaNameSuggestion :: String } deriving (Typeable, Data, Show, Eq) emptyMetaInfo :: MetaInfo emptyMetaInfo = MetaInfo { metaRange = noRange , metaScope = emptyScopeInfo , metaNumber = Nothing , metaNameSuggestion = "" } instance HasRange MetaInfo where getRange = metaRange instance KillRange MetaInfo where killRange m = m { metaRange = noRange } {-------------------------------------------------------------------------- General expression information --------------------------------------------------------------------------} newtype ExprInfo = ExprRange Range deriving (Typeable, Data, Show, Eq, Null) exprNoRange :: ExprInfo exprNoRange = ExprRange noRange instance HasRange ExprInfo where getRange (ExprRange r) = r instance KillRange ExprInfo where killRange (ExprRange r) = exprNoRange {-------------------------------------------------------------------------- Lambda information --------------------------------------------------------------------------} -- | Information about lambdas. data LamInfo = LamInfo { lamRange :: Range , lamOrigin :: Origin , lamParens :: Bool -- ^ Do we prefer the lambda with or without parens? } deriving (Typeable, Data, Show, Eq, Ord) -- | Default is system inserted and prefer parens. defaultLamInfo :: Range -> LamInfo defaultLamInfo r = LamInfo{ lamRange = r, lamOrigin = Inserted, lamParens = True } -- | `LamInfo` with no range information. defaultLamInfo_ :: LamInfo defaultLamInfo_ = defaultLamInfo noRange instance HasRange LamInfo where getRange = lamRange instance KillRange LamInfo where killRange (LamInfo r o p) = LamInfo (killRange r) o p instance LensOrigin LamInfo where getOrigin = lamOrigin setOrigin o i = i { lamOrigin = o } {-------------------------------------------------------------------------- Module information --------------------------------------------------------------------------} data ModuleInfo = ModuleInfo { minfoRange :: Range , minfoAsTo :: Range -- ^ The range of the \"as\" and \"to\" keywords, -- if any. Retained for highlighting purposes. , minfoAsName :: Maybe C.Name -- ^ The \"as\" module name, if any. Retained for highlighting purposes. , minfoOpenShort :: Maybe OpenShortHand , minfoDirective :: Maybe ImportDirective -- ^ Retained for @abstractToConcrete@ of 'ModuleMacro'. } deriving (Typeable, Data, Eq) deriving instance Show ModuleInfo instance HasRange ModuleInfo where getRange = minfoRange instance SetRange ModuleInfo where setRange r i = i { minfoRange = r } instance KillRange ModuleInfo where killRange m = m { minfoRange = noRange } --------------------------------------------------------------------------- -- Let info --------------------------------------------------------------------------- newtype LetInfo = LetRange Range deriving (Typeable, Data, Show, Eq, Null) instance HasRange LetInfo where getRange (LetRange r) = r instance KillRange LetInfo where killRange (LetRange r) = LetRange noRange {-------------------------------------------------------------------------- Definition information (declarations that actually define something) --------------------------------------------------------------------------} data DefInfo = DefInfo { defFixity :: Fixity' , defAccess :: Access , defAbstract :: IsAbstract , defInstance :: IsInstance , defMacro :: IsMacro , defInfo :: DeclInfo } deriving (Typeable, Data, Show, Eq) mkDefInfo :: Name -> Fixity' -> Access -> IsAbstract -> Range -> DefInfo mkDefInfo x f a ab r = DefInfo f a ab NotInstanceDef NotMacroDef (DeclInfo x r) -- | Same as @mkDefInfo@ but where we can also give the @IsInstance@ mkDefInfoInstance :: Name -> Fixity' -> Access -> IsAbstract -> IsInstance -> IsMacro -> Range -> DefInfo mkDefInfoInstance x f a ab i m r = DefInfo f a ab i m (DeclInfo x r) instance HasRange DefInfo where getRange = getRange . defInfo instance SetRange DefInfo where setRange r i = i { defInfo = setRange r (defInfo i) } instance KillRange DefInfo where killRange i = i { defInfo = killRange $ defInfo i } {-------------------------------------------------------------------------- General declaration information --------------------------------------------------------------------------} data DeclInfo = DeclInfo { declName :: Name , declRange :: Range } deriving (Typeable, Data, Show, Eq) instance HasRange DeclInfo where getRange = declRange instance SetRange DeclInfo where setRange r i = i { declRange = r } instance KillRange DeclInfo where killRange i = i { declRange = noRange } {-------------------------------------------------------------------------- Mutual block information --------------------------------------------------------------------------} data MutualInfo = MutualInfo { mutualTermCheck :: TerminationCheck Name , mutualPositivityCheck :: PositivityCheck , mutualRange :: Range } deriving (Typeable, Data, Show, Eq) -- | Default value for 'MutualInfo'. instance Null MutualInfo where empty = MutualInfo TerminationCheck True noRange instance HasRange MutualInfo where getRange = mutualRange instance KillRange MutualInfo where killRange i = i { mutualRange = noRange } {-------------------------------------------------------------------------- Left hand side information --------------------------------------------------------------------------} newtype LHSInfo = LHSRange Range deriving (Typeable, Data, Show, Eq, Null) instance HasRange LHSInfo where getRange (LHSRange r) = r instance KillRange LHSInfo where killRange (LHSRange r) = LHSRange noRange {-------------------------------------------------------------------------- Pattern information --------------------------------------------------------------------------} -- | For a general pattern we remember the source code position. newtype PatInfo = PatRange Range deriving (Typeable, Data, Eq, Null, Show, HasRange, KillRange) -- | Empty range for patterns. patNoRange :: PatInfo patNoRange = PatRange noRange -- | Constructor pattern info. data ConPatInfo = ConPatInfo { patOrigin :: ConOrigin -- ^ Does this pattern come form the eta-expansion of an implicit pattern? --- Or from a user written constructor or record pattern? , patInfo :: PatInfo } deriving (Typeable, Data, Eq) instance Show ConPatInfo where show (ConPatInfo po i) = applyWhen (po == ConOSystem) ("implicit " ++) $ show i instance HasRange ConPatInfo where getRange = getRange . patInfo instance KillRange ConPatInfo where killRange (ConPatInfo b i) = ConPatInfo b $ killRange i instance SetRange ConPatInfo where setRange r (ConPatInfo b i) = ConPatInfo b $ PatRange r Agda-2.5.3/src/full/Agda/Syntax/Parser.hs0000644000000000000000000001521013154613124016205 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} module Agda.Syntax.Parser ( -- * Types Parser -- * Parse functions , Agda.Syntax.Parser.parse , Agda.Syntax.Parser.parsePosString , parseFile' -- * Parsers , moduleParser , moduleNameParser , exprParser , exprWhereParser , tokensParser -- * Parse errors , ParseError(..) , ParseWarning(..) , PM(..) , runPMIO ) where import Control.Arrow (second) import Control.Exception import Control.Monad ((>=>), forM_) import Control.Monad.State import Control.Monad.Reader import Control.Monad.Writer hiding ((<>)) import qualified Data.List as List import Data.Typeable ( Typeable ) import Agda.Syntax.Position import Agda.Syntax.Parser.Monad as M hiding (Parser, parseFlags) import qualified Agda.Syntax.Parser.Monad as M import qualified Agda.Syntax.Parser.Parser as P import Agda.Syntax.Parser.Lexer import Agda.Syntax.Parser.Literate import Agda.Syntax.Concrete import Agda.Syntax.Concrete.Definitions import Agda.Syntax.Parser.Tokens import Agda.Utils.Except ( ExceptT , MonadError(catchError, throwError) , runExceptT ) import Agda.Utils.FileName import Agda.Utils.IO.UTF8 (readTextFile) import qualified Agda.Utils.Maybe.Strict as Strict import Agda.Utils.Pretty #if __GLASGOW_HASKELL__ <= 708 import Control.Applicative ((<$>), Applicative) #endif #include "undefined.h" import Agda.Utils.Impossible ------------------------------------------------------------------------ -- Wrapping parse results wrap :: ParseResult a -> PM a wrap (ParseOk _ x) = return x wrap (ParseFailed err) = throwError err wrapIOM :: (MonadError e m, MonadIO m) => (IOError -> e) -> IO a -> m a wrapIOM f m = do a <- liftIO$ (Right <$> m) `catch` (\err -> return$ Left (err :: IOError)) case a of Right x -> return x Left err -> throwError (f err) wrapM :: IO (ParseResult a) -> PM a wrapM m = liftIO m >>= wrap -- | A monad for handling parse results newtype PM a = PM { unPM :: ExceptT ParseError (StateT [ParseWarning] IO) a } deriving (Functor, Applicative, Monad, MonadError ParseError, MonadIO) warning :: ParseWarning -> PM () warning w = PM (modify (w:)) runPMIO :: (MonadIO m) => PM a -> m (Either ParseError a, [ParseWarning]) runPMIO = liftIO . fmap (second reverse) . flip runStateT [] . runExceptT . unPM ------------------------------------------------------------------------ -- Parse functions -- | Wrapped Parser type. data Parser a = Parser { parser :: M.Parser a , parseFlags :: ParseFlags , parseLiterate :: LiterateParser a } type LiterateParser a = Parser a -> [Layer] -> PM a parse :: Parser a -> String -> PM a parse p = wrapM . return . M.parse (parseFlags p) [normal] (parser p) parseFile :: Parser a -> AbsolutePath -> PM a parseFile p = wrapM . M.parseFile (parseFlags p) [layout, normal] (parser p) parseString :: Parser a -> String -> PM a parseString = parseStringFromFile Strict.Nothing parseStringFromFile :: SrcFile -> Parser a -> String -> PM a parseStringFromFile src p = wrapM . return . M.parseFromSrc (parseFlags p) [layout, normal] (parser p) src parseLiterateWithoutComments :: LiterateParser a parseLiterateWithoutComments p layers = parseStringFromFile (literateSrcFile layers) p $ illiterate layers parseLiterateWithComments :: LiterateParser [Token] parseLiterateWithComments p layers = do code <- map Left <$> parseLiterateWithoutComments p layers let literate = Right <$> filter (not . isCodeLayer) layers let (terms, overlaps) = interleaveRanges code literate forM_ (map fst overlaps) $ \c -> warning$ OverlappingTokensWarning { warnRange = getRange c } return$ concat [ case m of Left t -> [t] Right (Layer Comment interval s) -> [TokTeX (interval, s)] Right (Layer Markup _ _) -> [] Right (Layer Code _ _) -> [] | m <- terms ] readFilePM :: AbsolutePath -> PM String readFilePM path = wrapIOM (ReadFileError path) (readTextFile (filePath path)) parseLiterateFile :: Processor -> Parser a -> AbsolutePath -> PM a parseLiterateFile po p path = readFilePM path >>= parseLiterate p p . po (startPos (Just path)) parsePosString :: Parser a -> Position -> String -> PM a parsePosString p pos = wrapM . return . M.parsePosString pos (parseFlags p) [normal] (parser p) -- | Extensions supported by `parseFile'` parseFileExts :: [String] parseFileExts = ".agda":literateExts parseFile' :: (Show a) => Parser a -> AbsolutePath -> PM a parseFile' p file = if ".agda" `List.isSuffixOf` filePath file then Agda.Syntax.Parser.parseFile p file else go literateProcessors where go [] = throwError InvalidExtensionError { errPath = file , errValidExts = parseFileExts } go ((ext, po):pos) | ext `List.isSuffixOf` filePath file = parseLiterateFile po p file go (_:pos) = go pos ------------------------------------------------------------------------ -- Specific parsers -- | Parses a module. moduleParser :: Parser Module moduleParser = Parser { parser = P.moduleParser , parseFlags = withoutComments , parseLiterate = parseLiterateWithoutComments } -- | Parses a module name. moduleNameParser :: Parser QName moduleNameParser = Parser { parser = P.moduleNameParser , parseFlags = withoutComments , parseLiterate = parseLiterateWithoutComments } -- | Parses an expression. exprParser :: Parser Expr exprParser = Parser { parser = P.exprParser , parseFlags = withoutComments , parseLiterate = parseLiterateWithoutComments } -- | Parses an expression followed by a where clause. exprWhereParser :: Parser ExprWhere exprWhereParser = Parser { parser = P.exprWhereParser , parseFlags = withoutComments , parseLiterate = parseLiterateWithoutComments } -- | Gives the parsed token stream (including comments). tokensParser :: Parser [Token] tokensParser = Parser { parser = P.tokensParser , parseFlags = withComments , parseLiterate = parseLiterateWithComments } -- | Keep comments in the token stream generated by the lexer. withComments :: ParseFlags withComments = defaultParseFlags { parseKeepComments = True } -- | Do not keep comments in the token stream generated by the lexer. withoutComments :: ParseFlags withoutComments = defaultParseFlags { parseKeepComments = False } Agda-2.5.3/src/full/Agda/Syntax/Common.hs0000644000000000000000000010505213154613124016205 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-| Some common syntactic entities are defined in this module. -} module Agda.Syntax.Common where import Control.Applicative import Control.DeepSeq import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as ByteString import Data.Foldable import Data.Hashable (Hashable(..)) import qualified Data.Strict.Maybe as Strict import Data.Semigroup hiding (Arg) import Data.Traversable import Data.Data (Data) import Data.Typeable (Typeable) import Data.Word import GHC.Generics (Generic) import Agda.Syntax.Position import Agda.Utils.Functor import Agda.Utils.Pretty hiding ((<>)) #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Delayed --------------------------------------------------------------------------- -- | Used to specify whether something should be delayed. data Delayed = Delayed | NotDelayed deriving (Typeable, Data, Show, Eq, Ord) instance KillRange Delayed where killRange = id --------------------------------------------------------------------------- -- * Induction --------------------------------------------------------------------------- data Induction = Inductive | CoInductive deriving (Typeable, Data, Eq, Ord) instance Show Induction where show Inductive = "inductive" show CoInductive = "coinductive" instance HasRange Induction where getRange _ = noRange instance KillRange Induction where killRange = id instance NFData Induction where rnf Inductive = () rnf CoInductive = () --------------------------------------------------------------------------- -- * Hiding --------------------------------------------------------------------------- data Overlappable = YesOverlap | NoOverlap deriving (Typeable, Data, Show, Eq, Ord) data Hiding = Hidden | Instance Overlappable | NotHidden deriving (Typeable, Data, Show, Eq, Ord) -- | Just for the 'Hiding' instance. Should never combine different -- overlapping. instance Semigroup Overlappable where NoOverlap <> NoOverlap = NoOverlap YesOverlap <> YesOverlap = YesOverlap _ <> _ = __IMPOSSIBLE__ -- | 'Hiding' is an idempotent partial monoid, with unit 'NotHidden'. -- 'Instance' and 'NotHidden' are incompatible. instance Semigroup Hiding where NotHidden <> h = h h <> NotHidden = h Hidden <> Hidden = Hidden Instance o <> Instance o' = Instance (o <> o') _ <> _ = __IMPOSSIBLE__ instance Monoid Overlappable where mempty = NoOverlap mappend = (<>) instance Monoid Hiding where mempty = NotHidden mappend = (<>) instance KillRange Hiding where killRange = id instance NFData Overlappable where rnf NoOverlap = () rnf YesOverlap = () instance NFData Hiding where rnf Hidden = () rnf (Instance o) = rnf o rnf NotHidden = () -- | Decorating something with 'Hiding' information. data WithHiding a = WithHiding { whHiding :: !Hiding , whThing :: a } deriving (Typeable, Data, Eq, Ord, Show, Functor, Foldable, Traversable) instance Decoration WithHiding where traverseF f (WithHiding h a) = WithHiding h <$> f a instance Applicative WithHiding where pure = WithHiding mempty WithHiding h f <*> WithHiding h' a = WithHiding (mappend h h') (f a) instance HasRange a => HasRange (WithHiding a) where getRange = getRange . dget instance SetRange a => SetRange (WithHiding a) where setRange = fmap . setRange instance KillRange a => KillRange (WithHiding a) where killRange = fmap killRange instance NFData a => NFData (WithHiding a) where rnf (WithHiding _ a) = rnf a -- | A lens to access the 'Hiding' attribute in data structures. -- Minimal implementation: @getHiding@ and one of @setHiding@ or @mapHiding@. class LensHiding a where getHiding :: a -> Hiding setHiding :: Hiding -> a -> a setHiding h = mapHiding (const h) mapHiding :: (Hiding -> Hiding) -> a -> a mapHiding f a = setHiding (f $ getHiding a) a instance LensHiding Hiding where getHiding = id setHiding = const mapHiding = id instance LensHiding (WithHiding a) where getHiding (WithHiding h _) = h setHiding h (WithHiding _ a) = WithHiding h a mapHiding f (WithHiding h a) = WithHiding (f h) a -- | Monoidal composition of 'Hiding' information in some data. mergeHiding :: LensHiding a => WithHiding a -> a mergeHiding (WithHiding h a) = mapHiding (mappend h) a -- | 'NotHidden' arguments are @visible@. visible :: LensHiding a => a -> Bool visible a = getHiding a == NotHidden -- | 'Instance' and 'Hidden' arguments are @notVisible@. notVisible :: LensHiding a => a -> Bool notVisible a = getHiding a /= NotHidden -- | 'Hidden' arguments are @hidden@. hidden :: LensHiding a => a -> Bool hidden a = getHiding a == Hidden hide :: LensHiding a => a -> a hide = setHiding Hidden hideOrKeepInstance :: LensHiding a => a -> a hideOrKeepInstance x = case getHiding x of Hidden -> x Instance{} -> x NotHidden -> setHiding Hidden x makeInstance :: LensHiding a => a -> a makeInstance = makeInstance' NoOverlap makeInstance' :: LensHiding a => Overlappable -> a -> a makeInstance' o = setHiding (Instance o) isOverlappable :: LensHiding a => a -> Bool isOverlappable x = case getHiding x of Instance YesOverlap -> True _ -> False isInstance :: LensHiding a => a -> Bool isInstance x = case getHiding x of Instance{} -> True _ -> False -- | Ignores 'Overlappable'. sameHiding :: (LensHiding a, LensHiding b) => a -> b -> Bool sameHiding x y = case (getHiding x, getHiding y) of (Instance{}, Instance{}) -> True (hx, hy) -> hx == hy --------------------------------------------------------------------------- -- * Relevance --------------------------------------------------------------------------- -- | An constructor argument is big if the sort of its type is bigger than -- the sort of the data type. Only parameters (and maybe forced arguments) -- are allowed to be big. -- @ -- List : Set -> Set -- nil : (A : Set) -> List A -- @ -- @A@ is big in constructor @nil@ as the sort @Set1@ of its type @Set@ -- is bigger than the sort @Set@ of the data type @List@. data Big = Big | Small deriving (Typeable, Data, Show, Eq, Enum, Bounded) instance Ord Big where Big <= Small = False _ <= _ = True instance NFData Big where rnf Big = () rnf Small = () -- | A function argument can be relevant or irrelevant. -- See "Agda.TypeChecking.Irrelevance". data Relevance = Relevant -- ^ The argument is (possibly) relevant at compile-time. | NonStrict -- ^ The argument may never flow into evaluation position. -- Therefore, it is irrelevant at run-time. -- It is treated relevantly during equality checking. | Irrelevant -- ^ The argument is irrelevant at compile- and runtime. | Forced Big -- ^ The argument can be skipped during equality checking -- because its value is already determined by the type. -- If a constructor argument is big, it has to be regarded -- absent, otherwise we get into paradoxes. deriving (Typeable, Data, Show, Eq) allRelevances :: [Relevance] allRelevances = [ Relevant , NonStrict , Irrelevant , Forced Small , Forced Big ] instance KillRange Relevance where killRange rel = rel -- no range to kill instance Ord Relevance where (<=) = moreRelevant instance NFData Relevance where rnf Relevant = () rnf NonStrict = () rnf Irrelevant = () rnf (Forced a) = rnf a -- | A lens to access the 'Relevance' attribute in data structures. -- Minimal implementation: @getRelevance@ and one of @setRelevance@ or @mapRelevance@. class LensRelevance a where getRelevance :: a -> Relevance setRelevance :: Relevance -> a -> a setRelevance h = mapRelevance (const h) mapRelevance :: (Relevance -> Relevance) -> a -> a mapRelevance f a = setRelevance (f $ getRelevance a) a instance LensRelevance Relevance where getRelevance = id setRelevance = const mapRelevance = id isRelevant :: LensRelevance a => a -> Bool isRelevant a = getRelevance a == Relevant isIrrelevant :: LensRelevance a => a -> Bool isIrrelevant a = getRelevance a == Irrelevant -- | Information ordering. -- @Relevant \`moreRelevant\` -- Forced \`moreRelevant\` -- NonStrict \`moreRelevant\` -- Irrelevant@ moreRelevant :: Relevance -> Relevance -> Bool moreRelevant r r' = case (r, r') of -- top (_, Irrelevant) -> True (Irrelevant, _) -> False -- bottom (Relevant, _) -> True (_, Relevant) -> False -- second bottom (Forced{}, _) -> True (_, Forced{}) -> False -- remaining case (NonStrict,NonStrict) -> True irrelevant :: Relevance -> Bool irrelevant r = case r of Irrelevant -> True NonStrict -> False Relevant -> False Forced{} -> False -- | @unusableRelevance rel == True@ iff we cannot use a variable of @rel@. unusableRelevance :: LensRelevance a => a -> Bool unusableRelevance a = case getRelevance a of Irrelevant -> True NonStrict -> True Forced{} -> False -- @Forced@ has no semantic relevance Relevant -> False -- | 'Relevance' composition. -- 'Irrelevant' is dominant, 'Relevant' is neutral. composeRelevance :: Relevance -> Relevance -> Relevance composeRelevance r r' = case (r, r') of (Irrelevant, _) -> Irrelevant (_, Irrelevant) -> Irrelevant (NonStrict, _) -> NonStrict (_, NonStrict) -> NonStrict (Forced b, Forced b') -> Forced (max b b') -- prefer Big over Small (Forced b, _) -> Forced b (_, Forced b) -> Forced b (Relevant, Relevant) -> Relevant -- | @inverseComposeRelevance r x@ returns the most irrelevant @y@ -- such that forall @x@, @y@ we have -- @x \`moreRelevant\` (r \`composeRelevance\` y)@ -- iff -- @(r \`inverseComposeRelevance\` x) \`moreRelevant\` y@ (Galois connection). inverseComposeRelevance :: Relevance -> Relevance -> Relevance inverseComposeRelevance r x = case (r, x) of (Relevant, x) -> x -- going to relevant arg.: nothing changes _ | r == x -> Relevant -- because Relevant is comp.-neutral (Forced{}, Forced{}) -> Relevant -- same, but (==) does not ignore Big (Forced{}, x) -> x (Irrelevant, x) -> Relevant -- going irrelevant: every thing usable (_, Irrelevant) -> Irrelevant -- otherwise: irrelevant things remain unusable (NonStrict, _) -> Relevant -- but @NonStrict@s become usable -- | For comparing @Relevance@ ignoring @Forced@. ignoreForced :: Relevance -> Relevance ignoreForced Forced{} = Relevant ignoreForced Relevant = Relevant ignoreForced NonStrict = NonStrict ignoreForced Irrelevant = Irrelevant -- | Irrelevant function arguments may appear non-strictly in the codomain type. irrToNonStrict :: Relevance -> Relevance irrToNonStrict Irrelevant = NonStrict -- irrToNonStrict NonStrict = Relevant -- TODO: this is bad if we apply irrToNonStrict several times! irrToNonStrict rel = rel -- | Applied when working on types (unless --experimental-irrelevance). nonStrictToRel :: Relevance -> Relevance nonStrictToRel NonStrict = Relevant nonStrictToRel rel = rel nonStrictToIrr :: Relevance -> Relevance nonStrictToIrr NonStrict = Irrelevant nonStrictToIrr rel = rel --------------------------------------------------------------------------- -- * Origin of arguments (user-written, inserted or reflected) --------------------------------------------------------------------------- -- | Origin of arguments. data Origin = UserWritten -- ^ From the source file / user input. (Preserve!) | Inserted -- ^ E.g. inserted hidden arguments. | Reflected -- ^ Produced by the reflection machinery. | CaseSplit -- ^ Produced by an interactive case split. deriving (Typeable, Data, Show, Eq, Ord) instance KillRange Origin where killRange = id instance NFData Origin where rnf UserWritten = () rnf Inserted = () rnf Reflected = () rnf CaseSplit = () -- | Decorating something with 'Origin' information. data WithOrigin a = WithOrigin { woOrigin :: !Origin , woThing :: a } deriving (Typeable, Data, Eq, Ord, Show, Functor, Foldable, Traversable) instance Decoration WithOrigin where traverseF f (WithOrigin h a) = WithOrigin h <$> f a instance HasRange a => HasRange (WithOrigin a) where getRange = getRange . dget instance SetRange a => SetRange (WithOrigin a) where setRange = fmap . setRange instance KillRange a => KillRange (WithOrigin a) where killRange = fmap killRange instance NFData a => NFData (WithOrigin a) where rnf (WithOrigin _ a) = rnf a -- | A lens to access the 'Origin' attribute in data structures. -- Minimal implementation: @getOrigin@ and one of @setOrigin@ or @mapOrigin@. class LensOrigin a where getOrigin :: a -> Origin setOrigin :: Origin -> a -> a setOrigin o = mapOrigin (const o) mapOrigin :: (Origin -> Origin) -> a -> a mapOrigin f a = setOrigin (f $ getOrigin a) a instance LensOrigin Origin where getOrigin = id setOrigin = const mapOrigin = id instance LensOrigin (WithOrigin a) where getOrigin (WithOrigin h _) = h setOrigin h (WithOrigin _ a) = WithOrigin h a mapOrigin f (WithOrigin h a) = WithOrigin (f h) a --------------------------------------------------------------------------- -- * Argument decoration --------------------------------------------------------------------------- -- | A function argument can be hidden and/or irrelevant. data ArgInfo = ArgInfo { argInfoHiding :: Hiding , argInfoRelevance :: Relevance , argInfoOrigin :: Origin } deriving (Typeable, Data, Eq, Ord, Show) instance KillRange ArgInfo where killRange (ArgInfo h r o) = killRange3 ArgInfo h r o class LensArgInfo a where getArgInfo :: a -> ArgInfo setArgInfo :: ArgInfo -> a -> a setArgInfo ai = mapArgInfo (const ai) mapArgInfo :: (ArgInfo -> ArgInfo) -> a -> a mapArgInfo f a = setArgInfo (f $ getArgInfo a) a instance LensArgInfo ArgInfo where getArgInfo = id setArgInfo = const mapArgInfo = id instance NFData ArgInfo where rnf (ArgInfo a b c) = rnf a `seq` rnf b `seq` rnf c instance LensHiding ArgInfo where getHiding = argInfoHiding setHiding h ai = ai { argInfoHiding = h } mapHiding f ai = ai { argInfoHiding = f (argInfoHiding ai) } instance LensRelevance ArgInfo where getRelevance = argInfoRelevance setRelevance h ai = ai { argInfoRelevance = h } mapRelevance f ai = ai { argInfoRelevance = f (argInfoRelevance ai) } instance LensOrigin ArgInfo where getOrigin = argInfoOrigin setOrigin o ai = ai { argInfoOrigin = o } mapOrigin f ai = ai { argInfoOrigin = f (argInfoOrigin ai) } defaultArgInfo :: ArgInfo defaultArgInfo = ArgInfo { argInfoHiding = NotHidden , argInfoRelevance = Relevant , argInfoOrigin = UserWritten } --------------------------------------------------------------------------- -- * Arguments --------------------------------------------------------------------------- data Arg e = Arg { argInfo :: ArgInfo , unArg :: e } deriving (Data, Typeable, Ord, Functor, Foldable, Traversable) instance Decoration Arg where traverseF f (Arg ai a) = Arg ai <$> f a instance HasRange a => HasRange (Arg a) where getRange = getRange . unArg instance SetRange a => SetRange (Arg a) where setRange r = fmap $ setRange r instance KillRange a => KillRange (Arg a) where killRange (Arg info a) = killRange2 Arg info a instance Eq a => Eq (Arg a) where Arg (ArgInfo h1 _ _) x1 == Arg (ArgInfo h2 _ _) x2 = (h1, x1) == (h2, x2) instance Show a => Show (Arg a) where show (Arg (ArgInfo h r o) a) = showR r $ showO o $ showH h $ show a where showH Hidden s = "{" ++ s ++ "}" showH NotHidden s = "(" ++ s ++ ")" showH (Instance o) s = showOv o ++ "{{" ++ s ++ "}}" where showOv YesOverlap = "overlap " showOv NoOverlap = "" showR r s = case r of Irrelevant -> "." ++ s NonStrict -> "?" ++ s Forced Big -> "!b" ++ s Forced Small -> "!" ++ s Relevant -> "r" ++ s -- Andreas: I want to see it explicitly showO o s = case o of UserWritten -> "u" ++ s Inserted -> "i" ++ s Reflected -> "g" ++ s -- generated by reflection CaseSplit -> "c" ++ s -- generated by case split instance NFData e => NFData (Arg e) where rnf (Arg a b) = rnf a `seq` rnf b instance LensHiding (Arg e) where getHiding = getHiding . argInfo mapHiding = mapArgInfo . mapHiding instance LensRelevance (Arg e) where getRelevance = getRelevance . argInfo mapRelevance = mapArgInfo . mapRelevance instance LensOrigin (Arg e) where getOrigin = getOrigin . argInfo mapOrigin = mapArgInfo . mapOrigin instance LensArgInfo (Arg a) where getArgInfo = argInfo mapArgInfo f arg = arg { argInfo = f $ argInfo arg } defaultArg :: a -> Arg a defaultArg = Arg defaultArgInfo -- | @xs \`withArgsFrom\` args@ translates @xs@ into a list of 'Arg's, -- using the elements in @args@ to fill in the non-'unArg' fields. -- -- Precondition: The two lists should have equal length. withArgsFrom :: [a] -> [Arg b] -> [Arg a] xs `withArgsFrom` args = zipWith (\x arg -> fmap (const x) arg) xs args withNamedArgsFrom :: [a] -> [NamedArg b] -> [NamedArg a] xs `withNamedArgsFrom` args = zipWith (\x -> fmap (x <$)) xs args --------------------------------------------------------------------------- -- * Names --------------------------------------------------------------------------- class Eq a => Underscore a where underscore :: a isUnderscore :: a -> Bool isUnderscore = (== underscore) instance Underscore String where underscore = "_" instance Underscore ByteString where underscore = ByteString.pack underscore instance Underscore Doc where underscore = text underscore --------------------------------------------------------------------------- -- * Function type domain --------------------------------------------------------------------------- -- | Similar to 'Arg', but we need to distinguish -- an irrelevance annotation in a function domain -- (the domain itself is not irrelevant!) -- from an irrelevant argument. -- -- @Dom@ is used in 'Pi' of internal syntax, in 'Context' and 'Telescope'. -- 'Arg' is used for actual arguments ('Var', 'Con', 'Def' etc.) -- and in 'Abstract' syntax and other situations. data Dom e = Dom { domInfo :: ArgInfo , unDom :: e } deriving (Typeable, Data, Ord, Functor, Foldable, Traversable) instance Decoration Dom where traverseF f (Dom ai a) = Dom ai <$> f a instance HasRange a => HasRange (Dom a) where getRange = getRange . unDom instance KillRange a => KillRange (Dom a) where killRange (Dom info a) = killRange2 Dom info a instance Eq a => Eq (Dom a) where Dom (ArgInfo h1 r1 _) x1 == Dom (ArgInfo h2 r2 _) x2 = (h1, ignoreForced r1, x1) == (h2, ignoreForced r2, x2) instance Show a => Show (Dom a) where show = show . argFromDom instance LensHiding (Dom e) where getHiding = getHiding . domInfo mapHiding = mapArgInfo . mapHiding instance LensRelevance (Dom e) where getRelevance = getRelevance . domInfo mapRelevance = mapArgInfo . mapRelevance instance LensArgInfo (Dom e) where getArgInfo = domInfo mapArgInfo f arg = arg { domInfo = f $ domInfo arg } instance LensOrigin (Dom e) where getOrigin = getOrigin . getArgInfo mapOrigin = mapArgInfo . mapOrigin argFromDom :: Dom a -> Arg a argFromDom (Dom i a) = Arg i a domFromArg :: Arg a -> Dom a domFromArg (Arg i a) = Dom i a defaultDom :: a -> Dom a defaultDom = Dom defaultArgInfo --------------------------------------------------------------------------- -- * Named arguments --------------------------------------------------------------------------- -- | Something potentially carrying a name. data Named name a = Named { nameOf :: Maybe name , namedThing :: a } deriving (Eq, Ord, Typeable, Data, Functor, Foldable, Traversable) -- | Standard naming. type Named_ = Named RString unnamed :: a -> Named name a unnamed = Named Nothing named :: name -> a -> Named name a named = Named . Just instance Decoration (Named name) where traverseF f (Named n a) = Named n <$> f a instance HasRange a => HasRange (Named name a) where getRange = getRange . namedThing instance SetRange a => SetRange (Named name a) where setRange r = fmap $ setRange r instance (KillRange name, KillRange a) => KillRange (Named name a) where killRange (Named n a) = Named (killRange n) (killRange a) instance Show a => Show (Named_ a) where show (Named Nothing a) = show a show (Named (Just n) a) = rawNameToString (rangedThing n) ++ " = " ++ show a instance (NFData name, NFData a) => NFData (Named name a) where rnf (Named a b) = rnf a `seq` rnf b -- | Only 'Hidden' arguments can have names. type NamedArg a = Arg (Named_ a) -- | Get the content of a 'NamedArg'. namedArg :: NamedArg a -> a namedArg = namedThing . unArg defaultNamedArg :: a -> NamedArg a defaultNamedArg = defaultArg . unnamed -- | The functor instance for 'NamedArg' would be ambiguous, -- so we give it another name here. updateNamedArg :: (a -> b) -> NamedArg a -> NamedArg b updateNamedArg = fmap . fmap -- | @setNamedArg a b = updateNamedArg (const b) a@ setNamedArg :: NamedArg a -> b -> NamedArg b setNamedArg a b = (b <$) <$> a --------------------------------------------------------------------------- -- * Range decoration. --------------------------------------------------------------------------- -- | Thing with range info. data Ranged a = Ranged { rangeOf :: Range , rangedThing :: a } deriving (Typeable, Data, Functor, Foldable, Traversable) -- | Thing with no range info. unranged :: a -> Ranged a unranged = Ranged noRange instance Show a => Show (Ranged a) where show = show . rangedThing instance Eq a => Eq (Ranged a) where Ranged _ x == Ranged _ y = x == y instance Ord a => Ord (Ranged a) where compare (Ranged _ x) (Ranged _ y) = compare x y instance HasRange (Ranged a) where getRange = rangeOf instance KillRange (Ranged a) where killRange (Ranged _ x) = Ranged noRange x instance Decoration Ranged where traverseF f (Ranged r x) = Ranged r <$> f x -- | Ranges are not forced. instance NFData a => NFData (Ranged a) where rnf (Ranged _ a) = rnf a --------------------------------------------------------------------------- -- * Raw names (before parsing into name parts). --------------------------------------------------------------------------- -- | A @RawName@ is some sort of string. type RawName = String rawNameToString :: RawName -> String rawNameToString = id stringToRawName :: String -> RawName stringToRawName = id -- | String with range info. type RString = Ranged RawName --------------------------------------------------------------------------- -- * Further constructor and projection info --------------------------------------------------------------------------- -- | Where does the 'ConP' or 'Con' come from? data ConOrigin = ConOSystem -- ^ Inserted by system or expanded from an implicit pattern. | ConOCon -- ^ User wrote a constructor (pattern). | ConORec -- ^ User wrote a record (pattern). | ConOSplit -- ^ Generated by interactive case splitting. deriving (Typeable, Data, Show, Eq, Ord, Enum, Bounded) instance KillRange ConOrigin where killRange = id -- | Prefer user-written over system-inserted. bestConInfo :: ConOrigin -> ConOrigin -> ConOrigin bestConInfo ConOSystem o = o bestConInfo o _ = o -- | Where does a projection come from? data ProjOrigin = ProjPrefix -- ^ User wrote a prefix projection. | ProjPostfix -- ^ User wrote a postfix projection. | ProjSystem -- ^ Projection was generated by the system. deriving (Typeable, Data, Show, Eq, Ord, Enum, Bounded) instance KillRange ProjOrigin where killRange = id data DataOrRecord = IsData | IsRecord deriving (Typeable, Data, Eq, Ord, Show) --------------------------------------------------------------------------- -- * Infixity, access, abstract, etc. --------------------------------------------------------------------------- -- | Functions can be defined in both infix and prefix style. See -- 'Agda.Syntax.Concrete.LHS'. data IsInfix = InfixDef | PrefixDef deriving (Typeable, Data, Show, Eq, Ord) -- | Access modifier. data Access = PrivateAccess Origin -- ^ Store the 'Origin' of the private block that lead to this qualifier. -- This is needed for more faithful printing of declarations. | PublicAccess | OnlyQualified -- ^ Visible from outside, but not exported when opening the module -- Used for qualified constructors. deriving (Typeable, Data, Show, Eq, Ord) instance Pretty Access where pretty = text . \case PrivateAccess _ -> "private" PublicAccess -> "public" OnlyQualified -> "only-qualified" instance NFData Access where rnf _ = () instance HasRange Access where getRange _ = noRange instance KillRange Access where killRange = id -- | Abstract or concrete data IsAbstract = AbstractDef | ConcreteDef deriving (Typeable, Data, Show, Eq, Ord) instance KillRange IsAbstract where killRange = id -- | Is this definition eligible for instance search? data IsInstance = InstanceDef | NotInstanceDef deriving (Typeable, Data, Show, Eq, Ord) instance KillRange IsInstance where killRange = id instance HasRange IsInstance where getRange _ = noRange instance NFData IsInstance where rnf InstanceDef = () rnf NotInstanceDef = () -- | Is this a macro definition? data IsMacro = MacroDef | NotMacroDef deriving (Typeable, Data, Show, Eq, Ord) instance KillRange IsMacro where killRange = id instance HasRange IsMacro where getRange _ = noRange type Nat = Int type Arity = Nat --------------------------------------------------------------------------- -- * NameId --------------------------------------------------------------------------- -- | The unique identifier of a name. Second argument is the top-level module -- identifier. data NameId = NameId {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64 deriving (Eq, Ord, Typeable, Data, Generic) instance KillRange NameId where killRange = id instance Show NameId where show (NameId n m) = show n ++ "@" ++ show m instance Enum NameId where succ (NameId n m) = NameId (n + 1) m pred (NameId n m) = NameId (n - 1) m toEnum n = __IMPOSSIBLE__ -- should not be used fromEnum (NameId n _) = fromIntegral n instance NFData NameId where rnf (NameId _ _) = () instance Hashable NameId where {-# INLINE hashWithSalt #-} hashWithSalt salt (NameId n m) = hashWithSalt salt (n, m) --------------------------------------------------------------------------- -- * Meta variables --------------------------------------------------------------------------- -- | A meta variable identifier is just a natural number. -- newtype MetaId = MetaId { metaId :: Nat } deriving (Eq, Ord, Num, Real, Enum, Integral, Typeable, Data) instance Pretty MetaId where pretty (MetaId n) = text $ "_" ++ show n -- | Show non-record version of this newtype. instance Show MetaId where showsPrec p (MetaId n) = showParen (p > 0) $ showString "MetaId " . shows n instance NFData MetaId where rnf (MetaId x) = rnf x newtype Constr a = Constr a ------------------------------------------------------------------------ -- * Placeholders (used to parse sections) ------------------------------------------------------------------------ -- | The position of a name part or underscore in a name. data PositionInName = Beginning -- ^ The following underscore is at the beginning of the name: -- @_foo@. | Middle -- ^ The following underscore is in the middle of the name: -- @foo_bar@. | End -- ^ The following underscore is at the end of the name: @foo_@. deriving (Show, Eq, Ord, Typeable, Data) -- | Placeholders are used to represent the underscores in a section. data MaybePlaceholder e = Placeholder !PositionInName | NoPlaceholder !(Strict.Maybe PositionInName) e -- ^ The second argument is used only (but not always) for name -- parts other than underscores. deriving (Typeable, Data, Eq, Ord, Functor, Foldable, Traversable, Show) -- | An abbreviation: @noPlaceholder = 'NoPlaceholder' -- 'Strict.Nothing'@. noPlaceholder :: e -> MaybePlaceholder e noPlaceholder = NoPlaceholder Strict.Nothing instance HasRange a => HasRange (MaybePlaceholder a) where getRange Placeholder{} = noRange getRange (NoPlaceholder _ e) = getRange e instance KillRange a => KillRange (MaybePlaceholder a) where killRange p@Placeholder{} = p killRange (NoPlaceholder p e) = killRange1 (NoPlaceholder p) e instance NFData a => NFData (MaybePlaceholder a) where rnf (Placeholder _) = () rnf (NoPlaceholder _ a) = rnf a --------------------------------------------------------------------------- -- * Interaction meta variables --------------------------------------------------------------------------- newtype InteractionId = InteractionId { interactionId :: Nat } deriving ( Eq , Ord , Num , Integral , Real , Enum , Data #if __GLASGOW_HASKELL__ <= 708 , Typeable #endif ) instance Show InteractionId where show (InteractionId i) = "?" ++ show i instance KillRange InteractionId where killRange = id ----------------------------------------------------------------------------- -- * Import directive ----------------------------------------------------------------------------- -- | The things you are allowed to say when you shuffle names between name -- spaces (i.e. in @import@, @namespace@, or @open@ declarations). data ImportDirective' a b = ImportDirective { importDirRange :: Range , using :: Using' a b , hiding :: [ImportedName' a b] , impRenaming :: [Renaming' a b] , publicOpen :: Bool -- ^ Only for @open@. Exports the opened names from the current module. } deriving (Typeable, Data, Eq) data Using' a b = UseEverything | Using [ImportedName' a b] deriving (Typeable, Data, Eq) instance Semigroup (Using' a b) where UseEverything <> u = u u <> UseEverything = u Using xs <> Using ys = Using (xs ++ ys) instance Monoid (Using' a b) where mempty = UseEverything mappend = (<>) -- | Default is directive is @private@ (use everything, but do not export). defaultImportDir :: ImportDirective' a b defaultImportDir = ImportDirective noRange UseEverything [] [] False isDefaultImportDir :: ImportDirective' a b -> Bool isDefaultImportDir (ImportDirective _ UseEverything [] [] False) = True isDefaultImportDir _ = False -- | An imported name can be a module or a defined name data ImportedName' a b = ImportedModule b | ImportedName a deriving (Typeable, Data, Eq, Ord) setImportedName :: ImportedName' a a -> a -> ImportedName' a a setImportedName (ImportedName x) y = ImportedName y setImportedName (ImportedModule x) y = ImportedModule y instance (Show a, Show b) => Show (ImportedName' a b) where show (ImportedModule b) = "module " ++ show b show (ImportedName a) = show a data Renaming' a b = Renaming { renFrom :: ImportedName' a b -- ^ Rename from this name. , renTo :: ImportedName' a b -- ^ To this one. Must be same kind as 'renFrom'. , renToRange :: Range -- ^ The range of the \"to\" keyword. Retained for highlighting purposes. } deriving (Typeable, Data, Eq) -- ** HasRange instances instance (HasRange a, HasRange b) => HasRange (ImportDirective' a b) where getRange = importDirRange instance (HasRange a, HasRange b) => HasRange (Using' a b) where getRange (Using xs) = getRange xs getRange UseEverything = noRange instance (HasRange a, HasRange b) => HasRange (Renaming' a b) where getRange r = getRange (renFrom r, renTo r) instance (HasRange a, HasRange b) => HasRange (ImportedName' a b) where getRange (ImportedName x) = getRange x getRange (ImportedModule x) = getRange x -- ** KillRange instances instance (KillRange a, KillRange b) => KillRange (ImportDirective' a b) where killRange (ImportDirective _ u h r p) = killRange3 (\u h r -> ImportDirective noRange u h r p) u h r instance (KillRange a, KillRange b) => KillRange (Using' a b) where killRange (Using i) = killRange1 Using i killRange UseEverything = UseEverything instance (KillRange a, KillRange b) => KillRange (Renaming' a b) where killRange (Renaming i n _) = killRange2 (\i n -> Renaming i n noRange) i n instance (KillRange a, KillRange b) => KillRange (ImportedName' a b) where killRange (ImportedModule n) = killRange1 ImportedModule n killRange (ImportedName n) = killRange1 ImportedName n -- ** NFData instances -- | Ranges are not forced. instance (NFData a, NFData b) => NFData (ImportDirective' a b) where rnf (ImportDirective _ a b c _) = rnf a `seq` rnf b `seq` rnf c instance (NFData a, NFData b) => NFData (Using' a b) where rnf UseEverything = () rnf (Using a) = rnf a -- | Ranges are not forced. instance (NFData a, NFData b) => NFData (Renaming' a b) where rnf (Renaming a b _) = rnf a `seq` rnf b instance (NFData a, NFData b) => NFData (ImportedName' a b) where rnf (ImportedModule a) = rnf a rnf (ImportedName a) = rnf a ----------------------------------------------------------------------------- -- * Termination ----------------------------------------------------------------------------- -- | Termination check? (Default = TerminationCheck). data TerminationCheck m = TerminationCheck -- ^ Run the termination checker. | NoTerminationCheck -- ^ Skip termination checking (unsafe). | NonTerminating -- ^ Treat as non-terminating. | Terminating -- ^ Treat as terminating (unsafe). Same effect as 'NoTerminationCheck'. | TerminationMeasure Range m -- ^ Skip termination checking but use measure instead. deriving (Typeable, Data, Show, Eq, Functor) instance KillRange m => KillRange (TerminationCheck m) where killRange (TerminationMeasure _ m) = TerminationMeasure noRange (killRange m) killRange t = t instance NFData a => NFData (TerminationCheck a) where rnf TerminationCheck = () rnf NoTerminationCheck = () rnf NonTerminating = () rnf Terminating = () rnf (TerminationMeasure _ a) = rnf a ----------------------------------------------------------------------------- -- * Positivity ----------------------------------------------------------------------------- -- | Positivity check? (Default = True). type PositivityCheck = Bool Agda-2.5.3/src/full/Agda/Syntax/Internal.hs0000644000000000000000000013165313154613124016537 0ustar0000000000000000{-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE UndecidableInstances #-} -- because of shortcomings of FunctionalDependencies #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE OverlappingInstances #-} #endif module Agda.Syntax.Internal ( module Agda.Syntax.Internal , module Agda.Syntax.Abstract.Name , module Agda.Utils.Pointer , MetaId(..) ) where import Prelude hiding (foldr, mapM, null) import Control.Applicative hiding (empty) import Control.Monad.Identity hiding (mapM) import Control.DeepSeq import Data.Foldable ( Foldable, foldMap ) import Data.Function import qualified Data.List as List import Data.Maybe import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend, Sum(..)) import Data.Traversable import Data.Data (Data) import Data.Typeable (Typeable) import Agda.Syntax.Position import Agda.Syntax.Common import Agda.Syntax.Literal import Agda.Syntax.Concrete.Pretty (prettyHiding) import Agda.Syntax.Abstract.Name import Agda.Utils.Empty import Agda.Utils.Functor import Agda.Utils.Geniplate import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Pointer import Agda.Utils.Size import qualified Agda.Utils.Pretty as P import Agda.Utils.Pretty hiding ((<>)) import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- | Type of argument lists. -- type Args = [Arg Term] type NamedArgs = [NamedArg Term] -- | Store the names of the record fields in the constructor. -- This allows reduction of projection redexes outside of TCM. -- For instance, during substitution and application. data ConHead = ConHead { conName :: QName -- ^ The name of the constructor. , conInductive :: Induction -- ^ Record constructors can be coinductive. , conFields :: [QName] -- ^ The name of the record fields. -- Empty list for data constructors. -- 'Arg' is not needed here since it -- is stored in the constructor args. } deriving (Typeable, Data, Show) instance Eq ConHead where (==) = (==) `on` conName instance Ord ConHead where (<=) = (<=) `on` conName instance Pretty ConHead where pretty = pretty . conName instance HasRange ConHead where getRange = getRange . conName instance SetRange ConHead where setRange r = mapConName (setRange r) class LensConName a where getConName :: a -> QName setConName :: QName -> a -> a setConName = mapConName . const mapConName :: (QName -> QName) -> a -> a mapConName f a = setConName (f (getConName a)) a instance LensConName ConHead where getConName = conName setConName c con = con { conName = c } -- | Raw values. -- -- @Def@ is used for both defined and undefined constants. -- Assume there is a type declaration and a definition for -- every constant, even if the definition is an empty -- list of clauses. -- data Term = Var {-# UNPACK #-} !Int Elims -- ^ @x es@ neutral | Lam ArgInfo (Abs Term) -- ^ Terms are beta normal. Relevance is ignored | Lit Literal | Def QName Elims -- ^ @f es@, possibly a delta/iota-redex | Con ConHead ConInfo Args -- ^ @c vs@ or @record { fs = vs }@ | Pi (Dom Type) (Abs Type) -- ^ dependent or non-dependent function space | Sort Sort | Level Level | MetaV {-# UNPACK #-} !MetaId Elims | DontCare Term -- ^ Irrelevant stuff in relevant position, but created -- in an irrelevant context. Basically, an internal -- version of the irrelevance axiom @.irrAx : .A -> A@. | Shared !(Ptr Term) -- ^ Explicit sharing deriving (Typeable, Data, Show) type ConInfo = ConOrigin -- | Eliminations, subsuming applications and projections. -- data Elim' a = Apply (Arg a) -- ^ Application. | Proj ProjOrigin QName -- ^ Projection. 'QName' is name of a record projection. deriving (Typeable, Data, Show, Functor, Foldable, Traversable) type Elim = Elim' Term type Elims = [Elim] -- ^ eliminations ordered left-to-right. -- | This instance cheats on 'Proj', use with care. -- 'Proj's are always assumed to be 'UserWritten', since they have no 'ArgInfo'. instance LensOrigin (Elim' a) where getOrigin (Apply a) = getOrigin a getOrigin Proj{} = UserWritten mapOrigin f (Apply a) = Apply $ mapOrigin f a mapOrigin f e@Proj{} = e -- | Names in binders and arguments. type ArgName = String argNameToString :: ArgName -> String argNameToString = id stringToArgName :: String -> ArgName stringToArgName = id appendArgNames :: ArgName -> ArgName -> ArgName appendArgNames = (++) nameToArgName :: Name -> ArgName nameToArgName = stringToArgName . prettyShow -- | Binder. -- 'Abs': The bound variable might appear in the body. -- 'NoAbs' is pseudo-binder, it does not introduce a fresh variable, -- similar to the @const@ of Haskell. data Abs a = Abs { absName :: ArgName, unAbs :: a } -- ^ The body has (at least) one free variable. -- Danger: 'unAbs' doesn't shift variables properly | NoAbs { absName :: ArgName, unAbs :: a } deriving (Typeable, Data, Functor, Foldable, Traversable) instance Decoration Abs where traverseF f (Abs x a) = Abs x <$> f a traverseF f (NoAbs x a) = NoAbs x <$> f a -- | Types are terms with a sort annotation. -- data Type' a = El { _getSort :: Sort, unEl :: a } deriving (Typeable, Data, Show, Functor, Foldable, Traversable) type Type = Type' Term instance Decoration Type' where traverseF f (El s a) = El s <$> f a class LensSort a where lensSort :: Lens' Sort a getSort :: a -> Sort getSort a = a ^. lensSort instance LensSort (Type' a) where lensSort f (El s a) = f s <&> \ s' -> El s' a -- General instance leads to overlapping instances. -- instance (Decoration f, LensSort a) => LensSort (f a) where instance LensSort a => LensSort (Dom a) where lensSort = traverseF . lensSort instance LensSort a => LensSort (Abs a) where lensSort = traverseF . lensSort -- | Sequence of types. An argument of the first type is bound in later types -- and so on. data Tele a = EmptyTel | ExtendTel a (Abs (Tele a)) -- ^ 'Abs' is never 'NoAbs'. deriving (Typeable, Data, Show, Functor, Foldable, Traversable) type Telescope = Tele (Dom Type) -- | Sorts. -- data Sort = Type Level -- ^ @Set ℓ@. | Prop -- ^ Dummy sort. | Inf -- ^ @Setω@. | SizeUniv -- ^ @SizeUniv@, a sort inhabited by type @Size@. | DLub Sort (Abs Sort) -- ^ Dependent least upper bound. -- If the free variable occurs in the second sort, -- the whole thing should reduce to Inf, -- otherwise it's the normal lub. deriving (Typeable, Data, Show) -- | A level is a maximum expression of 0..n 'PlusLevel' expressions -- each of which is a number or an atom plus a number. -- -- The empty maximum is the canonical representation for level 0. newtype Level = Max [PlusLevel] deriving (Show, Typeable, Data) data PlusLevel = ClosedLevel Integer -- ^ @n@, to represent @Setₙ@. | Plus Integer LevelAtom -- ^ @n + ℓ@. deriving (Show, Typeable, Data) -- | An atomic term of type @Level@. data LevelAtom = MetaLevel MetaId Elims -- ^ A meta variable targeting @Level@ under some eliminations. | BlockedLevel MetaId Term -- ^ A term of type @Level@ whose reduction is blocked by a meta. | NeutralLevel NotBlocked Term -- ^ A neutral term of type @Level@. | UnreducedLevel Term -- ^ Introduced by 'instantiate', removed by 'reduce'. deriving (Show, Typeable, Data) --------------------------------------------------------------------------- -- * Blocked Terms --------------------------------------------------------------------------- -- | Even if we are not stuck on a meta during reduction -- we can fail to reduce a definition by pattern matching -- for another reason. data NotBlocked = StuckOn Elim -- ^ The 'Elim' is neutral and blocks a pattern match. | Underapplied -- ^ Not enough arguments were supplied to complete the matching. | AbsurdMatch -- ^ We matched an absurd clause, results in a neutral 'Def'. | MissingClauses -- ^ We ran out of clauses, all considered clauses -- produced an actual mismatch. -- This can happen when try to reduce a function application -- but we are still missing some function clauses. -- See "Agda.TypeChecking.Patterns.Match". | ReallyNotBlocked -- ^ Reduction was not blocked, we reached a whnf -- which can be anything but a stuck @'Def'@. deriving (Show, Typeable, Data) -- | 'ReallyNotBlocked' is the unit. -- 'MissingClauses' is dominant. -- @'StuckOn'{}@ should be propagated, if tied, we take the left. instance Semigroup NotBlocked where ReallyNotBlocked <> b = b -- MissingClauses is dominant (absorptive) b@MissingClauses <> _ = b _ <> b@MissingClauses = b -- StuckOn is second strongest b@StuckOn{} <> _ = b _ <> b@StuckOn{} = b b <> _ = b instance Monoid NotBlocked where -- ReallyNotBlocked is neutral mempty = ReallyNotBlocked mappend = (<>) -- | Something where a meta variable may block reduction. data Blocked t = Blocked { theBlockingMeta :: MetaId , ignoreBlocking :: t } | NotBlocked { blockingStatus :: NotBlocked, ignoreBlocking :: t } deriving (Typeable, Show, Functor, Foldable, Traversable) -- deriving (Typeable, Eq, Ord, Functor, Foldable, Traversable) -- | Blocking by a meta is dominant. instance Applicative Blocked where pure = notBlocked f <*> e = ((f $> ()) `mappend` (e $> ())) $> ignoreBlocking f (ignoreBlocking e) -- -- | Blocking by a meta is dominant. -- instance Applicative Blocked where -- pure = notBlocked -- Blocked x f <*> e = Blocked x $ f (ignoreBlocking e) -- NotBlocked nb f <*> Blocked x e = Blocked x $ f e -- NotBlocked nb f <*> NotBlocked nb' e = NotBlocked (nb `mappend` nb') $ f e -- | @'Blocked' t@ without the @t@. type Blocked_ = Blocked () instance Semigroup Blocked_ where b@Blocked{} <> _ = b _ <> b@Blocked{} = b NotBlocked x _ <> NotBlocked y _ = NotBlocked (x <> y) () instance Monoid Blocked_ where mempty = notBlocked () mappend = (<>) -- | When trying to reduce @f es@, on match failed on one -- elimination @e ∈ es@ that came with info @r :: NotBlocked@. -- @stuckOn e r@ produces the new @NotBlocked@ info. -- -- 'MissingClauses' must be propagated, as this is blockage -- that can be lifted in the future (as more clauses are added). -- -- @'StuckOn' e0@ is also propagated, since it provides more -- precise information as @StuckOn e@ (as @e0@ is the original -- reason why reduction got stuck and usually a subterm of @e@). -- An information like @StuckOn (Apply (Arg info (Var i [])))@ -- (stuck on a variable) could be used by the lhs/coverage checker -- to trigger a split on that (pattern) variable. -- -- In the remaining cases for @r@, we are terminally stuck -- due to @StuckOn e@. Propagating @'AbsurdMatch'@ does not -- seem useful. -- -- 'Underapplied' must not be propagated, as this would mean -- that @f es@ is underapplied, which is not the case (it is stuck). -- Note that 'Underapplied' can only arise when projection patterns were -- missing to complete the original match (in @e@). -- (Missing ordinary pattern would mean the @e@ is of function type, -- but we cannot match against something of function type.) stuckOn :: Elim -> NotBlocked -> NotBlocked stuckOn e r = case r of MissingClauses -> r StuckOn{} -> r Underapplied -> r' AbsurdMatch -> r' ReallyNotBlocked -> r' where r' = StuckOn e --------------------------------------------------------------------------- -- * Definitions --------------------------------------------------------------------------- -- | A clause is a list of patterns and the clause body. -- -- The telescope contains the types of the pattern variables and the -- de Bruijn indices say how to get from the order the variables occur in -- the patterns to the order they occur in the telescope. The body -- binds the variables in the order they appear in the telescope. -- -- @clauseTel ~ permute clausePerm (patternVars namedClausePats)@ -- -- Terms in dot patterns are valid in the clause telescope. -- -- For the purpose of the permutation and the body dot patterns count -- as variables. TODO: Change this! data Clause = Clause { clauseLHSRange :: Range , clauseFullRange :: Range , clauseTel :: Telescope -- ^ @Δ@: The types of the pattern variables in dependency order. , namedClausePats :: [NamedArg DeBruijnPattern] -- ^ @Δ ⊢ ps@. The de Bruijn indices refer to @Δ@. , clauseBody :: Maybe Term -- ^ @Just v@ with @Δ ⊢ v@ for a regular clause, or @Nothing@ for an -- absurd one. , clauseType :: Maybe (Arg Type) -- ^ @Δ ⊢ t@. The type of the rhs under @clauseTel@. -- Used, e.g., by @TermCheck@. -- Can be 'Irrelevant' if we encountered an irrelevant projection -- pattern on the lhs. , clauseCatchall :: Bool -- ^ Clause has been labelled as CATCHALL. , clauseUnreachable :: Maybe Bool -- ^ Clause has been labelled as unreachable by the coverage checker. -- @Nothing@ means coverage checker has not run yet (clause may be unreachable). -- @Just False@ means clause is not unreachable. -- @Just True@ means clause is unreachable. } deriving (Typeable, Data, Show) clausePats :: Clause -> [Arg DeBruijnPattern] clausePats = map (fmap namedThing) . namedClausePats instance HasRange Clause where getRange = clauseLHSRange -- | Pattern variables. type PatVarName = ArgName patVarNameToString :: PatVarName -> String patVarNameToString = argNameToString nameToPatVarName :: Name -> PatVarName nameToPatVarName = nameToArgName -- | Patterns are variables, constructors, or wildcards. -- @QName@ is used in @ConP@ rather than @Name@ since -- a constructor might come from a particular namespace. -- This also meshes well with the fact that values (i.e. -- the arguments we are matching with) use @QName@. -- data Pattern' x = VarP x -- ^ @x@ | DotP Term -- ^ @.t@ | ConP ConHead ConPatternInfo [NamedArg (Pattern' x)] -- ^ @c ps@ -- The subpatterns do not contain any projection copatterns. | AbsurdP (Pattern' x) -- ^ @()@ -- The argument is to keep track of the original pattern -- (before the absurd match). | LitP Literal -- ^ E.g. @5@, @"hello"@. | ProjP ProjOrigin QName -- ^ Projection copattern. Can only appear by itself. deriving (Typeable, Data, Show, Functor, Foldable, Traversable) type Pattern = Pattern' PatVarName -- ^ The @PatVarName@ is a name suggestion. varP :: ArgName -> Pattern varP = VarP -- | Type used when numbering pattern variables. data DBPatVar = DBPatVar { dbPatVarName :: PatVarName , dbPatVarIndex :: Int } deriving (Typeable, Data, Show) type DeBruijnPattern = Pattern' DBPatVar namedVarP :: PatVarName -> Named_ Pattern namedVarP x = Named named $ varP x where named = if isUnderscore x then Nothing else Just $ unranged x namedDBVarP :: Int -> PatVarName -> Named_ DeBruijnPattern namedDBVarP m = (fmap . fmap) (\x -> DBPatVar x m) . namedVarP -- | The @ConPatternInfo@ states whether the constructor belongs to -- a record type (@Just@) or data type (@Nothing@). -- In the former case, the @Bool@ says whether the record pattern -- orginates from the expansion of an implicit pattern. -- The @Type@ is the type of the whole record pattern. -- The scope used for the type is given by any outer scope -- plus the clause's telescope ('clauseTel'). data ConPatternInfo = ConPatternInfo { conPRecord :: Maybe ConOrigin -- ^ @Nothing@ if data constructor. -- @Just@ if record constructor. , conPType :: Maybe (Arg Type) -- ^ The type of the whole constructor pattern. -- Should be present (@Just@) if constructor pattern is -- is generated ordinarily by type-checking. -- Could be absent (@Nothing@) if pattern comes from some -- plugin (like Agsy). -- Needed e.g. for with-clause stripping. } deriving (Typeable, Data, Show) noConPatternInfo :: ConPatternInfo noConPatternInfo = ConPatternInfo Nothing Nothing -- | Build partial 'ConPatternInfo' from 'ConInfo' toConPatternInfo :: ConInfo -> ConPatternInfo toConPatternInfo ConORec = ConPatternInfo (Just ConORec) Nothing toConPatternInfo _ = noConPatternInfo -- | Build 'ConInfo' from 'ConPatternInfo'. fromConPatternInfo :: ConPatternInfo -> ConInfo fromConPatternInfo = fromMaybe ConOSystem . conPRecord -- | Extract pattern variables in left-to-right order. -- A 'DotP' is also treated as variable (see docu for 'Clause'). class PatternVars a b | b -> a where patternVars :: b -> [Arg (Either a Term)] instance PatternVars a (Arg (Pattern' a)) where -- patternVars :: Arg (Pattern' a) -> [Arg (Either a Term)] patternVars (Arg i (VarP x) ) = [Arg i $ Left x] patternVars (Arg i (DotP t) ) = [Arg i $ Right t] patternVars (Arg i (AbsurdP p) ) = patternVars (Arg i p) patternVars (Arg _ (ConP _ _ ps)) = patternVars ps patternVars (Arg _ (LitP _) ) = [] patternVars (Arg _ ProjP{} ) = [] instance PatternVars a (NamedArg (Pattern' a)) where patternVars = patternVars . fmap namedThing instance PatternVars a b => PatternVars a [b] where patternVars = concatMap patternVars -- | Does the pattern perform a match that could fail? properlyMatching :: DeBruijnPattern -> Bool properlyMatching (VarP x) = False properlyMatching DotP{} = False properlyMatching AbsurdP{} = True properlyMatching LitP{} = True properlyMatching (ConP _ ci ps) = isNothing (conPRecord ci) || -- not a record cons List.any (properlyMatching . namedArg) ps -- or one of subpatterns is a proper m properlyMatching ProjP{} = True instance IsProjP (Pattern' a) where isProjP (ProjP o d) = Just (o, AmbQ [d]) isProjP _ = Nothing ----------------------------------------------------------------------------- -- * Explicit substitutions ----------------------------------------------------------------------------- -- | Substitutions. data Substitution' a = IdS -- ^ Identity substitution. -- @Γ ⊢ IdS : Γ@ | EmptyS Empty -- ^ Empty substitution, lifts from the empty context. First argument is @__IMPOSSIBLE__@. -- Apply this to closed terms you want to use in a non-empty context. -- @Γ ⊢ EmptyS : ()@ | a :# Substitution' a -- ^ Substitution extension, ``cons''. -- @ -- Γ ⊢ u : Aρ Γ ⊢ ρ : Δ -- ---------------------- -- Γ ⊢ u :# ρ : Δ, A -- @ | Strengthen Empty (Substitution' a) -- ^ Strengthening substitution. First argument is @__IMPOSSIBLE__@. -- Apply this to a term which does not contain variable 0 -- to lower all de Bruijn indices by one. -- @ -- Γ ⊢ ρ : Δ -- --------------------------- -- Γ ⊢ Strengthen ρ : Δ, A -- @ | Wk !Int (Substitution' a) -- ^ Weakning substitution, lifts to an extended context. -- @ -- Γ ⊢ ρ : Δ -- ------------------- -- Γ, Ψ ⊢ Wk |Ψ| ρ : Δ -- @ | Lift !Int (Substitution' a) -- ^ Lifting substitution. Use this to go under a binder. -- @Lift 1 ρ == var 0 :# Wk 1 ρ@. -- @ -- Γ ⊢ ρ : Δ -- ------------------------- -- Γ, Ψρ ⊢ Lift |Ψ| ρ : Δ, Ψ -- @ deriving ( Show , Functor , Foldable , Traversable , Data #if __GLASGOW_HASKELL__ <= 708 , Typeable #endif ) type Substitution = Substitution' Term type PatternSubstitution = Substitution' DeBruijnPattern infixr 4 :# instance Null (Substitution' a) where empty = IdS null IdS = True null _ = False --------------------------------------------------------------------------- -- * Views --------------------------------------------------------------------------- -- | View type as equality type. data EqualityView = EqualityType { eqtSort :: Sort -- ^ Sort of this type. , eqtName :: QName -- ^ Builtin EQUALITY. , eqtParams :: [Arg Term] -- ^ Hidden. Empty or @Level@. , eqtType :: Arg Term -- ^ Hidden , eqtLhs :: Arg Term -- ^ NotHidden , eqtRhs :: Arg Term -- ^ NotHidden } | OtherType Type -- ^ reduced isEqualityType :: EqualityView -> Bool isEqualityType EqualityType{} = True isEqualityType OtherType{} = False --------------------------------------------------------------------------- -- * Absurd Lambda --------------------------------------------------------------------------- -- | Absurd lambdas are internally represented as identity -- with variable name "()". absurdBody :: Abs Term absurdBody = Abs absurdPatternName $ Var 0 [] isAbsurdBody :: Abs Term -> Bool isAbsurdBody (Abs x (Var 0 [])) = isAbsurdPatternName x isAbsurdBody _ = False absurdPatternName :: PatVarName absurdPatternName = "()" isAbsurdPatternName :: PatVarName -> Bool isAbsurdPatternName x = x == absurdPatternName --------------------------------------------------------------------------- -- * Pointers and Sharing --------------------------------------------------------------------------- -- | Remove top-level @Shared@ data constructors. ignoreSharing :: Term -> Term ignoreSharing (Shared p) = ignoreSharing $ derefPtr p ignoreSharing v = v ignoreSharingType :: Type -> Type ignoreSharingType (El s v) = El s (ignoreSharing v) -- ignoreSharingType v = v -- | Introduce sharing. shared_ :: Term -> Term shared_ v@Shared{} = v shared_ v@(Var _ []) = v shared_ v@(Con _ _ []) = v -- Issue 1691: sharing (zero : Nat) destroys constructorForm shared_ v = Shared (newPtr v) -- | Typically m would be TCM and f would be Blocked. updateSharedFM #if __GLASGOW_HASKELL__ <= 708 :: (Applicative m, Monad m, Traversable f) #else :: (Monad m, Traversable f) #endif => (Term -> m (f Term)) -> Term -> m (f Term) updateSharedFM f v0@(Shared p) = do fv <- f (derefPtr p) flip traverse fv $ \v -> case derefPtr (setPtr v p) of Var _ [] -> return v _ -> return $! compressPointerChain v0 updateSharedFM f v = f v updateSharedM :: Monad m => (Term -> m Term) -> Term -> m Term updateSharedM f v0@(Shared p) = do v <- f (derefPtr p) case derefPtr (setPtr v p) of Var _ [] -> return v _ -> return $! compressPointerChain v0 updateSharedM f v = f v updateShared :: (Term -> Term) -> Term -> Term updateShared f v0@(Shared p) = case derefPtr (setPtr (f $ derefPtr p) p) of v@(Var _ []) -> v _ -> compressPointerChain v0 updateShared f v = f v pointerChain :: Term -> [Ptr Term] pointerChain (Shared p) = p : pointerChain (derefPtr p) pointerChain _ = [] -- Redirect all top-level pointers to point to the last pointer. So, after -- compression there are at most two top-level indirections. Then return the -- inner-most pointer so we have only one pointer for the result. compressPointerChain :: Term -> Term compressPointerChain v = case reverse $ pointerChain v of p:_:ps@(_:_) -> setPointers (Shared p) ps p:_:_ -> (Shared p) _ -> v where setPointers u [] = u setPointers u (p : ps) = setPtr u p `seq` setPointers u ps --------------------------------------------------------------------------- -- * Smart constructors --------------------------------------------------------------------------- -- | An unapplied variable. var :: Nat -> Term var i | i >= 0 = Var i [] | otherwise = __IMPOSSIBLE__ -- | Add 'DontCare' is it is not already a @DontCare@. dontCare :: Term -> Term dontCare v = case ignoreSharing v of DontCare{} -> v _ -> DontCare v -- | A dummy type. typeDontCare :: Type typeDontCare = El Prop (Sort Prop) -- | Top sort (Set\omega). topSort :: Type topSort = El Inf (Sort Inf) sort :: Sort -> Type sort s = El (sSuc s) $ Sort s varSort :: Int -> Sort varSort n = Type $ Max [Plus 0 $ NeutralLevel mempty $ var n] -- | Get the next higher sort. sSuc :: Sort -> Sort sSuc Prop = mkType 1 sSuc Inf = Inf sSuc SizeUniv = SizeUniv sSuc (DLub a b) = DLub (sSuc a) (fmap sSuc b) sSuc (Type l) = Type $ levelSuc l levelSuc :: Level -> Level levelSuc (Max []) = Max [ClosedLevel 1] levelSuc (Max as) = Max $ map inc as where inc (ClosedLevel n) = ClosedLevel (n + 1) inc (Plus n l) = Plus (n + 1) l mkType :: Integer -> Sort mkType n = Type $ Max [ClosedLevel n | n > 0] isSort :: Term -> Maybe Sort isSort v = case ignoreSharing v of Sort s -> Just s _ -> Nothing impossibleTerm :: String -> Int -> Term impossibleTerm file line = Lit $ LitString noRange $ unlines [ "An internal error has occurred. Please report this as a bug." , "Location of the error: " ++ file ++ ":" ++ show line ] hackReifyToMeta :: Term hackReifyToMeta = DontCare $ Lit $ LitNat noRange (-42) isHackReifyToMeta :: Term -> Bool isHackReifyToMeta (DontCare (Lit (LitNat r (-42)))) = r == noRange isHackReifyToMeta _ = False --------------------------------------------------------------------------- -- * Telescopes. --------------------------------------------------------------------------- -- | A traversal for the names in a telescope. mapAbsNamesM :: Applicative m => (ArgName -> m ArgName) -> Tele a -> m (Tele a) mapAbsNamesM f EmptyTel = pure EmptyTel mapAbsNamesM f (ExtendTel a ( Abs x b)) = ExtendTel a <$> ( Abs <$> f x <*> mapAbsNamesM f b) mapAbsNamesM f (ExtendTel a (NoAbs x b)) = ExtendTel a <$> (NoAbs <$> f x <*> mapAbsNamesM f b) -- Ulf, 2013-11-06: Last case is really impossible but I'd rather find out we -- violated that invariant somewhere other than here. mapAbsNames :: (ArgName -> ArgName) -> Tele a -> Tele a mapAbsNames f = runIdentity . mapAbsNamesM (Identity . f) -- Ulf, 2013-11-06 -- The record parameter is named "" inside the record module so we can avoid -- printing it (issue 208), but we don't want that to show up in the type of -- the functions in the module (issue 892). This function is used on the record -- module telescope before adding it to a type in -- TypeChecking.Monad.Signature.addConstant (to handle functions defined in -- record modules) and TypeChecking.Rules.Record.checkProjection (to handle -- record projections). replaceEmptyName :: ArgName -> Tele a -> Tele a replaceEmptyName x = mapAbsNames $ \ y -> if null y then x else y -- | Telescope as list. type ListTel' a = [Dom (a, Type)] type ListTel = ListTel' ArgName telFromList' :: (a -> ArgName) -> ListTel' a -> Telescope telFromList' f = List.foldr extTel EmptyTel where extTel (Dom info (x, a)) = ExtendTel (Dom info a) . Abs (f x) -- | Convert a list telescope to a telescope. telFromList :: ListTel -> Telescope telFromList = telFromList' id -- | Convert a telescope to its list form. telToList :: Telescope -> ListTel telToList EmptyTel = [] telToList (ExtendTel arg (Abs x tel)) = fmap (x,) arg : telToList tel telToList (ExtendTel _ NoAbs{} ) = __IMPOSSIBLE__ -- | Drop the types from a telescope. class TelToArgs a where telToArgs :: a -> [Arg ArgName] instance TelToArgs ListTel where telToArgs = map $ \ dom -> Arg (domInfo dom) (fst $ unDom dom) instance TelToArgs Telescope where telToArgs = telToArgs . telToList -- | Constructing a singleton telescope. class SgTel a where sgTel :: a -> Telescope instance SgTel (ArgName, Dom Type) where sgTel (x, dom) = ExtendTel dom $ Abs x EmptyTel instance SgTel (Dom (ArgName, Type)) where sgTel (Dom ai (x, t)) = ExtendTel (Dom ai t) $ Abs x EmptyTel instance SgTel (Dom Type) where sgTel dom = sgTel (stringToArgName "_", dom) --------------------------------------------------------------------------- -- * Handling blocked terms. --------------------------------------------------------------------------- blockingMeta :: Blocked t -> Maybe MetaId blockingMeta (Blocked m _) = Just m blockingMeta NotBlocked{} = Nothing blocked :: MetaId -> a -> Blocked a blocked x = Blocked x notBlocked :: a -> Blocked a notBlocked = NotBlocked ReallyNotBlocked --------------------------------------------------------------------------- -- * Simple operations on terms and types. --------------------------------------------------------------------------- -- | Removing a topmost 'DontCare' constructor. stripDontCare :: Term -> Term stripDontCare v = case ignoreSharing v of DontCare v -> v _ -> v -- | Doesn't do any reduction. arity :: Type -> Nat arity t = case ignoreSharing $ unEl t of Pi _ b -> 1 + arity (unAbs b) _ -> 0 -- | Make a name that is not in scope. notInScopeName :: ArgName -> ArgName notInScopeName = stringToArgName . ("." ++) . argNameToString -- | Pick the better name suggestion, i.e., the one that is not just underscore. class Suggest a b where suggest :: a -> b -> String instance Suggest String String where suggest "_" y = y suggest x _ = x instance Suggest (Abs a) (Abs b) where suggest b1 b2 = suggest (absName b1) (absName b2) instance Suggest String (Abs b) where suggest x b = suggest x (absName b) instance Suggest Name (Abs b) where suggest n b = suggest (nameToArgName n) (absName b) --------------------------------------------------------------------------- -- * Eliminations. --------------------------------------------------------------------------- -- | Convert top-level postfix projections into prefix projections. unSpine :: Term -> Term unSpine = unSpine' $ const True -- | Convert 'Proj' projection eliminations -- according to their 'ProjOrigin' into -- 'Def' projection applications. unSpine' :: (ProjOrigin -> Bool) -> Term -> Term unSpine' p v = case hasElims v of Just (h, es) -> loop h [] es Nothing -> v where loop :: (Elims -> Term) -> Elims -> Elims -> Term loop h res es = case es of [] -> v Proj o f : es' | p o -> loop (Def f) [Apply (defaultArg v)] es' e : es' -> loop h (e : res) es' where v = h $ reverse res -- | A view distinguishing the neutrals @Var@, @Def@, and @MetaV@ which -- can be projected. hasElims :: Term -> Maybe (Elims -> Term, Elims) hasElims v = case ignoreSharing v of Var i es -> Just (Var i, es) Def f es -> Just (Def f, es) MetaV x es -> Just (MetaV x, es) Con{} -> Nothing Lit{} -> Nothing -- Andreas, 2016-04-13, Issue 1932: We convert λ x → x .f into f Lam _ (Abs _ v) -> case ignoreSharing v of Var 0 [Proj _o f] -> Just (Def f, []) _ -> Nothing Lam{} -> Nothing Pi{} -> Nothing Sort{} -> Nothing Level{} -> Nothing DontCare{} -> Nothing Shared{} -> __IMPOSSIBLE__ -- | Drop 'Apply' constructor. (Unsafe!) argFromElim :: Elim' a -> Arg a argFromElim (Apply u) = u argFromElim Proj{} = __IMPOSSIBLE__ -- | Drop 'Apply' constructor. (Safe) isApplyElim :: Elim' a -> Maybe (Arg a) isApplyElim (Apply u) = Just u isApplyElim Proj{} = Nothing -- | Drop 'Apply' constructors. (Safe) allApplyElims :: [Elim' a] -> Maybe [Arg a] allApplyElims = mapM isApplyElim -- | Split at first non-'Apply' splitApplyElims :: [Elim' a] -> ([Arg a], [Elim' a]) splitApplyElims (Apply u : es) = mapFst (u :) $ splitApplyElims es splitApplyElims es = ([], es) class IsProjElim e where isProjElim :: e -> Maybe (ProjOrigin, QName) instance IsProjElim Elim where isProjElim (Proj o d) = Just (o, d) isProjElim Apply{} = Nothing -- | Discard @Proj f@ entries. dropProjElims :: IsProjElim e => [e] -> [e] dropProjElims = filter (isNothing . isProjElim) -- | Discards @Proj f@ entries. argsFromElims :: Elims -> Args argsFromElims = map argFromElim . dropProjElims -- | Drop 'Proj' constructors. (Safe) allProjElims :: Elims -> Maybe [(ProjOrigin, QName)] allProjElims = mapM isProjElim --------------------------------------------------------------------------- -- * Null instances. --------------------------------------------------------------------------- instance Null (Tele a) where empty = EmptyTel null EmptyTel = True null ExtendTel{} = False -- | A 'null' clause is one with no patterns and no rhs. -- Should not exist in practice. instance Null Clause where empty = Clause empty empty empty empty empty empty False Nothing null (Clause _ _ tel pats body _ _ _) = null tel && null pats && null body --------------------------------------------------------------------------- -- * Show instances. --------------------------------------------------------------------------- instance Show a => Show (Abs a) where showsPrec p (Abs x a) = showParen (p > 0) $ showString "Abs " . shows x . showString " " . showsPrec 10 a showsPrec p (NoAbs x a) = showParen (p > 0) $ showString "NoAbs " . shows x . showString " " . showsPrec 10 a -- instance Show t => Show (Blocked t) where -- showsPrec p (Blocked m x) = showParen (p > 0) $ -- showString "Blocked " . shows m . showString " " . showsPrec 10 x -- showsPrec p (NotBlocked x) = showsPrec p x --------------------------------------------------------------------------- -- * Sized instances and TermSize. --------------------------------------------------------------------------- -- | The size of a telescope is its length (as a list). instance Sized (Tele a) where size EmptyTel = 0 size (ExtendTel _ tel) = 1 + size tel instance Sized a => Sized (Abs a) where size = size . unAbs -- | The size of a term is roughly the number of nodes in its -- syntax tree. This number need not be precise for logical -- correctness of Agda, it is only used for reporting -- (and maybe decisions regarding performance). -- -- Not counting towards the term size are: -- -- * sort and color annotations, -- * projections. -- class TermSize a where termSize :: a -> Int termSize = getSum . tsize tsize :: a -> Sum Int #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPABLE #-} (Foldable t, TermSize a) => TermSize (t a) where #else instance (Foldable t, TermSize a) => TermSize (t a) where #endif tsize = foldMap tsize instance TermSize Term where tsize v = case v of Var _ vs -> 1 + tsize vs Def _ vs -> 1 + tsize vs Con _ _ vs -> 1 + tsize vs MetaV _ vs -> 1 + tsize vs Level l -> tsize l Lam _ f -> 1 + tsize f Lit _ -> 1 Pi a b -> 1 + tsize a + tsize b Sort s -> tsize s DontCare mv -> tsize mv Shared p -> tsize (derefPtr p) instance TermSize Sort where tsize s = case s of Type l -> 1 + tsize l Prop -> 1 Inf -> 1 SizeUniv -> 1 DLub s s' -> 1 + tsize s + tsize s' instance TermSize Level where tsize (Max as) = 1 + tsize as instance TermSize PlusLevel where tsize (ClosedLevel _) = 1 tsize (Plus _ a) = tsize a instance TermSize LevelAtom where tsize (MetaLevel _ vs) = 1 + tsize vs tsize (BlockedLevel _ v) = tsize v tsize (NeutralLevel _ v) = tsize v tsize (UnreducedLevel v) = tsize v instance TermSize a => TermSize (Substitution' a) where tsize IdS = 1 tsize (EmptyS _) = 1 tsize (Wk _ rho) = 1 + tsize rho tsize (t :# rho) = 1 + tsize t + tsize rho tsize (Strengthen _ rho) = 1 + tsize rho tsize (Lift _ rho) = 1 + tsize rho --------------------------------------------------------------------------- -- * KillRange instances. --------------------------------------------------------------------------- instance KillRange ConHead where killRange (ConHead c i fs) = killRange3 ConHead c i fs instance KillRange Term where killRange v = case v of Var i vs -> killRange1 (Var i) vs Def c vs -> killRange2 Def c vs Con c ci vs -> killRange3 Con c ci vs MetaV m vs -> killRange1 (MetaV m) vs Lam i f -> killRange2 Lam i f Lit l -> killRange1 Lit l Level l -> killRange1 Level l Pi a b -> killRange2 Pi a b Sort s -> killRange1 Sort s DontCare mv -> killRange1 DontCare mv Shared p -> Shared $ updatePtr killRange p instance KillRange Level where killRange (Max as) = killRange1 Max as instance KillRange PlusLevel where killRange l@ClosedLevel{} = l killRange (Plus n l) = killRange1 (Plus n) l instance KillRange LevelAtom where killRange (MetaLevel n as) = killRange1 (MetaLevel n) as killRange (BlockedLevel m v) = killRange1 (BlockedLevel m) v killRange (NeutralLevel r v) = killRange1 (NeutralLevel r) v killRange (UnreducedLevel v) = killRange1 UnreducedLevel v instance (KillRange a) => KillRange (Type' a) where killRange (El s v) = killRange2 El s v instance KillRange Sort where killRange s = case s of Prop -> Prop Inf -> Inf SizeUniv -> SizeUniv Type a -> killRange1 Type a DLub s1 s2 -> killRange2 DLub s1 s2 instance KillRange Substitution where killRange IdS = IdS killRange (EmptyS err) = EmptyS err killRange (Wk n rho) = killRange1 (Wk n) rho killRange (t :# rho) = killRange2 (:#) t rho killRange (Strengthen err rho) = killRange1 (Strengthen err) rho killRange (Lift n rho) = killRange1 (Lift n) rho instance KillRange ConPatternInfo where killRange (ConPatternInfo mr mt) = killRange1 (ConPatternInfo mr) mt instance KillRange DBPatVar where killRange (DBPatVar x i) = killRange2 DBPatVar x i instance KillRange a => KillRange (Pattern' a) where killRange p = case p of VarP x -> killRange1 VarP x DotP v -> killRange1 DotP v AbsurdP p -> killRange1 AbsurdP p ConP con info ps -> killRange3 ConP con info ps LitP l -> killRange1 LitP l ProjP o q -> killRange1 (ProjP o) q instance KillRange Clause where killRange (Clause rl rf tel ps body t catchall unreachable) = killRange8 Clause rl rf tel ps body t catchall unreachable instance KillRange a => KillRange (Tele a) where killRange = fmap killRange instance KillRange a => KillRange (Blocked a) where killRange = fmap killRange instance KillRange a => KillRange (Abs a) where killRange = fmap killRange instance KillRange a => KillRange (Elim' a) where killRange = fmap killRange --------------------------------------------------------------------------- -- * UniverseBi instances. --------------------------------------------------------------------------- instanceUniverseBiT' [] [t| (([Type], [Clause]), Pattern) |] instanceUniverseBiT' [] [t| (Args, Pattern) |] instanceUniverseBiT' [] [t| (Elims, Pattern) |] -- ? instanceUniverseBiT' [] [t| (([Type], [Clause]), Term) |] instanceUniverseBiT' [] [t| (Args, Term) |] instanceUniverseBiT' [] [t| (Elims, Term) |] -- ? instanceUniverseBiT' [] [t| ([Term], Term) |] ----------------------------------------------------------------------------- -- * Simple pretty printing ----------------------------------------------------------------------------- instance Pretty a => Pretty (Substitution' a) where prettyPrec p rho = pr p rho where pr p rho = case rho of IdS -> text "idS" EmptyS err -> text "emptyS" t :# rho -> mparens (p > 2) $ sep [ pr 2 rho P.<> text ",", prettyPrec 3 t ] Strengthen _ rho -> mparens (p > 9) $ text "strS" <+> pr 10 rho Wk n rho -> mparens (p > 9) $ text ("wkS " ++ show n) <+> pr 10 rho Lift n rho -> mparens (p > 9) $ text ("liftS " ++ show n) <+> pr 10 rho instance Pretty Term where prettyPrec p v = case ignoreSharing v of Var x els -> text ("@" ++ show x) `pApp` els Lam ai b -> mparens (p > 0) $ sep [ text "λ" <+> prettyHiding ai id (text . absName $ b) <+> text "->" , nest 2 $ pretty (unAbs b) ] Lit l -> pretty l Def q els -> pretty q `pApp` els Con c ci vs -> pretty (conName c) `pApp` map Apply vs Pi a (NoAbs _ b) -> mparens (p > 0) $ sep [ prettyPrec 1 (unDom a) <+> text "->" , nest 2 $ pretty b ] Pi a b -> mparens (p > 0) $ sep [ pDom (domInfo a) (text (absName b) <+> text ":" <+> pretty (unDom a)) <+> text "->" , nest 2 $ pretty (unAbs b) ] Sort s -> prettyPrec p s Level l -> prettyPrec p l MetaV x els -> pretty x `pApp` els DontCare v -> prettyPrec p v Shared{} -> __IMPOSSIBLE__ where pApp d els = mparens (not (null els) && p > 9) $ sep [d, nest 2 $ fsep (map (prettyPrec 10) els)] pDom :: LensHiding a => a -> Doc -> Doc pDom i = case getHiding i of NotHidden -> parens Hidden -> braces Instance{} -> braces . braces instance Pretty Clause where pretty Clause{clauseTel = tel, namedClausePats = ps, clauseBody = b, clauseType = t} = sep [ pretty tel <+> text "|-" , nest 2 $ sep [ fsep (map (prettyPrec 10) ps) <+> text "=" , nest 2 $ pBody b t ] ] where pBody Nothing _ = text "(absurd)" pBody (Just b) Nothing = pretty b pBody (Just b) (Just t) = sep [ pretty b <+> text ":", nest 2 $ pretty t ] instance Pretty a => Pretty (Tele (Dom a)) where pretty tel = fsep [ pDom a (text x <+> text ":" <+> pretty (unDom a)) | (x, a) <- telToList tel ] where telToList EmptyTel = [] telToList (ExtendTel a tel) = (absName tel, a) : telToList (unAbs tel) instance Pretty Level where prettyPrec p (Max as) = case as of [] -> prettyPrec p (ClosedLevel 0) [a] -> prettyPrec p a _ -> mparens (p > 9) $ List.foldr1 (\a b -> text "lub" <+> a <+> b) $ map (prettyPrec 10) as instance Pretty PlusLevel where prettyPrec p l = case l of ClosedLevel n -> sucs p n $ \_ -> text "lzero" Plus n a -> sucs p n $ \p -> prettyPrec p a where sucs p 0 d = d p sucs p n d = mparens (p > 9) $ text "lsuc" <+> sucs 10 (n - 1) d instance Pretty LevelAtom where prettyPrec p a = case a of MetaLevel x els -> prettyPrec p (MetaV x els) BlockedLevel _ v -> prettyPrec p v NeutralLevel _ v -> prettyPrec p v UnreducedLevel v -> prettyPrec p v instance Pretty Sort where prettyPrec p s = case s of Type (Max []) -> text "Set" Type (Max [ClosedLevel n]) -> text $ "Set" ++ show n Type l -> mparens (p > 9) $ text "Set" <+> prettyPrec 10 l Prop -> text "Prop" Inf -> text "Setω" SizeUniv -> text "SizeUniv" DLub s b -> mparens (p > 9) $ text "dlub" <+> prettyPrec 10 s <+> parens (sep [ text ("λ " ++ absName b ++ " ->") , nest 2 $ pretty (unAbs b) ]) instance Pretty Type where prettyPrec p (El _ a) = prettyPrec p a instance Pretty tm => Pretty (Elim' tm) where prettyPrec p (Apply v) = prettyPrec p v prettyPrec _ (Proj _o x) = text ("." ++ prettyShow x) instance Pretty DBPatVar where prettyPrec _ x = text $ patVarNameToString (dbPatVarName x) ++ "@" ++ show (dbPatVarIndex x) instance Pretty a => Pretty (Pattern' a) where prettyPrec n (VarP x) = prettyPrec n x prettyPrec _ (DotP t) = text "." P.<> prettyPrec 10 t prettyPrec _ (AbsurdP _) = text "()" prettyPrec n (ConP c i nps)= mparens (n > 0) $ pretty (conName c) <+> fsep (map pretty ps) where ps = map (fmap namedThing) nps -- -- Version with printing record type: -- prettyPrec _ (ConP c i ps) = (if b then braces else parens) $ prTy $ -- text (show $ conName c) <+> fsep (map (pretty . namedArg) ps) -- where -- b = maybe False (== ConOSystem) $ conPRecord i -- prTy d = caseMaybe (conPType i) d $ \ t -> d <+> text ":" <+> pretty t prettyPrec _ (LitP l) = pretty l prettyPrec _ (ProjP _o q) = text ("." ++ prettyShow q) ----------------------------------------------------------------------------- -- * NFData instances ----------------------------------------------------------------------------- -- Note: only strict in the shape of the terms. instance NFData Term where rnf v = case v of Var _ es -> rnf es Lam _ b -> rnf (unAbs b) Lit l -> rnf l Def _ es -> rnf es Con _ _ vs -> rnf vs Pi a b -> rnf (unDom a, unAbs b) Sort s -> rnf s Level l -> rnf l MetaV _ es -> rnf es DontCare v -> rnf v Shared{} -> () instance NFData Type where rnf (El s v) = rnf (s, v) instance NFData Sort where rnf s = case s of Type l -> rnf l Prop -> () Inf -> () SizeUniv -> () DLub a b -> rnf (a, unAbs b) instance NFData Level where rnf (Max as) = rnf as instance NFData PlusLevel where rnf (ClosedLevel n) = rnf n rnf (Plus n l) = rnf (n, l) instance NFData LevelAtom where rnf (MetaLevel _ es) = rnf es rnf (BlockedLevel _ v) = rnf v rnf (NeutralLevel _ v) = rnf v rnf (UnreducedLevel v) = rnf v instance NFData a => NFData (Elim' a) where rnf (Apply x) = rnf x rnf Proj{} = () Agda-2.5.3/src/full/Agda/Syntax/Concrete.hs0000644000000000000000000012451313154613124016522 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-| The concrete syntax is a raw representation of the program text without any desugaring at all. This is what the parser produces. The idea is that if we figure out how to keep the concrete syntax around, it can be printed exactly as the user wrote it. -} module Agda.Syntax.Concrete ( -- * Expressions Expr(..) , OpApp(..), fromOrdinary , module Agda.Syntax.Concrete.Name , appView, AppView(..) -- * Bindings , LamBinding , LamBinding'(..) , TypedBindings , TypedBindings'(..) , TypedBinding , TypedBinding'(..) , RecordAssignment , RecordAssignments , FieldAssignment, FieldAssignment'(..), nameFieldA, exprFieldA , ModuleAssignment(..) , BoundName(..), mkBoundName_, mkBoundName , Telescope -- (..) , countTelVars -- * Declarations , Declaration(..) , ModuleApplication(..) , TypeSignature , TypeSignatureOrInstanceBlock , ImportDirective, Using, ImportedName , Renaming , AsName(..) , OpenShortHand(..), RewriteEqn, WithExpr , LHS(..), Pattern(..), LHSCore(..) , RHS, RHS'(..), WhereClause, WhereClause'(..), ExprWhere(..) , Pragma(..) , Module , ThingWithFixity(..) , topLevelModuleName , spanAllowedBeforeModule -- * Pattern tools , patternNames, patternQNames -- * Lenses , mapLhsOriginalPattern ) where import Prelude hiding (null) import Control.DeepSeq import Data.Foldable (Foldable) import Data.Traversable (Traversable) import Data.List hiding (null) import Data.Set (Set) import Data.Monoid import Data.Typeable (Typeable) import Data.Data (Data) import Agda.Syntax.Position import Agda.Syntax.Common import Agda.Syntax.Fixity import Agda.Syntax.Notation import Agda.Syntax.Literal import Agda.Syntax.Concrete.Name import qualified Agda.Syntax.Abstract.Name as A import Agda.TypeChecking.Positivity.Occurrence import Agda.Utils.Lens import Agda.Utils.Null #include "undefined.h" import Agda.Utils.Impossible data OpApp e = SyntaxBindingLambda Range [LamBinding] e -- ^ An abstraction inside a special syntax declaration -- (see Issue 358 why we introduce this). | Ordinary e deriving (Typeable, Data, Functor, Foldable, Traversable) fromOrdinary :: e -> OpApp e -> e fromOrdinary d (Ordinary e) = e fromOrdinary d _ = d data FieldAssignment' a = FieldAssignment { _nameFieldA :: Name, _exprFieldA :: a } deriving (Typeable, Data, Functor, Foldable, Traversable, Show, Eq) type FieldAssignment = FieldAssignment' Expr data ModuleAssignment = ModuleAssignment { _qnameModA :: QName , _exprModA :: [Expr] , _importDirModA :: ImportDirective } deriving (Typeable, Data) type RecordAssignment = Either FieldAssignment ModuleAssignment type RecordAssignments = [RecordAssignment] nameFieldA :: Lens' Name (FieldAssignment' a) nameFieldA f r = f (_nameFieldA r) <&> \x -> r { _nameFieldA = x } exprFieldA :: Lens' a (FieldAssignment' a) exprFieldA f r = f (_exprFieldA r) <&> \x -> r { _exprFieldA = x } qnameModA :: Lens' QName ModuleAssignment qnameModA f r = f (_qnameModA r) <&> \x -> r { _qnameModA = x } exprModA :: Lens' [Expr] ModuleAssignment exprModA f r = f (_exprModA r) <&> \x -> r { _exprModA = x } importDirModA :: Lens' ImportDirective ModuleAssignment importDirModA f r = f (_importDirModA r) <&> \x -> r { _importDirModA = x } -- | Concrete expressions. Should represent exactly what the user wrote. data Expr = Ident QName -- ^ ex: @x@ | Lit Literal -- ^ ex: @1@ or @\"foo\"@ | QuestionMark Range (Maybe Nat) -- ^ ex: @?@ or @{! ... !}@ | Underscore Range (Maybe String) -- ^ ex: @_@ or @_A_5@ | RawApp Range [Expr] -- ^ before parsing operators | App Range Expr (NamedArg Expr) -- ^ ex: @e e@, @e {e}@, or @e {x = e}@ | OpApp Range QName (Set A.Name) [NamedArg (MaybePlaceholder (OpApp Expr))] -- ^ ex: @e + e@ -- The 'QName' is -- possibly ambiguous, -- but it must -- correspond to one of -- the names in the -- set. | WithApp Range Expr [Expr] -- ^ ex: @e | e1 | .. | en@ | HiddenArg Range (Named_ Expr) -- ^ ex: @{e}@ or @{x=e}@ | InstanceArg Range (Named_ Expr) -- ^ ex: @{{e}}@ or @{{x=e}}@ | Lam Range [LamBinding] Expr -- ^ ex: @\\x {y} -> e@ or @\\(x:A){y:B} -> e@ | AbsurdLam Range Hiding -- ^ ex: @\\ ()@ | ExtendedLam Range [(LHS,RHS,WhereClause,Bool)] -- ^ ex: @\\ { p11 .. p1a -> e1 ; .. ; pn1 .. pnz -> en }@ | Fun Range Expr Expr -- ^ ex: @e -> e@ or @.e -> e@ (NYI: @{e} -> e@) | Pi Telescope Expr -- ^ ex: @(xs:e) -> e@ or @{xs:e} -> e@ | Set Range -- ^ ex: @Set@ | Prop Range -- ^ ex: @Prop@ | SetN Range Integer -- ^ ex: @Set0, Set1, ..@ | Rec Range RecordAssignments -- ^ ex: @record {x = a; y = b}@, or @record { x = a; M1; M2 }@ | RecUpdate Range Expr [FieldAssignment] -- ^ ex: @record e {x = a; y = b}@ | Let Range [Declaration] Expr -- ^ ex: @let Ds in e@ | Paren Range Expr -- ^ ex: @(e)@ | IdiomBrackets Range Expr -- ^ ex: @(| e |)@ | Absurd Range -- ^ ex: @()@ or @{}@, only in patterns | As Range Name Expr -- ^ ex: @x\@p@, only in patterns | Dot Range Expr -- ^ ex: @.p@, only in patterns | ETel Telescope -- ^ only used for printing telescopes | QuoteGoal Range Name Expr -- ^ ex: @quoteGoal x in e@ | QuoteContext Range -- ^ ex: @quoteContext@ | Quote Range -- ^ ex: @quote@, should be applied to a name | QuoteTerm Range -- ^ ex: @quoteTerm@, should be applied to a term | Tactic Range Expr [Expr] -- ^ @tactic solve | subgoal1 | .. | subgoalN@ | Unquote Range -- ^ ex: @unquote@, should be applied to a term of type @Term@ | DontCare Expr -- ^ to print irrelevant things | Equal Range Expr Expr -- ^ ex: @a = b@, used internally in the parser deriving (Typeable, Data) -- | Concrete patterns. No literals in patterns at the moment. data Pattern = IdentP QName -- ^ @c@ or @x@ | QuoteP Range -- ^ @quote@ | AppP Pattern (NamedArg Pattern) -- ^ @p p'@ or @p {x = p'}@ | RawAppP Range [Pattern] -- ^ @p1..pn@ before parsing operators | OpAppP Range QName (Set A.Name) [NamedArg Pattern] -- ^ eg: @p => p'@ for operator @_=>_@ -- The 'QName' is possibly -- ambiguous, but it must -- correspond to one of -- the names in the set. | HiddenP Range (Named_ Pattern) -- ^ @{p}@ or @{x = p}@ | InstanceP Range (Named_ Pattern) -- ^ @{{p}}@ or @{{x = p}}@ | ParenP Range Pattern -- ^ @(p)@ | WildP Range -- ^ @_@ | AbsurdP Range -- ^ @()@ | AsP Range Name Pattern -- ^ @x\@p@ unused | DotP Range Origin Expr -- ^ @.e@ (the Origin keeps track -- whether this dot pattern was -- written by the user or inserted -- by the system) | LitP Literal -- ^ @0@, @1@, etc. | RecP Range [FieldAssignment' Pattern] -- ^ @record {x = p; y = q}@ deriving (Typeable, Data) -- | A lambda binding is either domain free or typed. type LamBinding = LamBinding' TypedBindings data LamBinding' a = DomainFree ArgInfo BoundName -- ^ . @x@ or @{x}@ or @.x@ or @.{x}@ or @{.x}@ | DomainFull a -- ^ . @(xs : e)@ or @{xs : e}@ deriving (Typeable, Data, Functor, Foldable, Traversable) -- | A sequence of typed bindings with hiding information. Appears in dependent -- function spaces, typed lambdas, and telescopes. -- -- If the individual binding contains hiding information as well, the -- 'Hiding' in @TypedBindings@ must be the unit 'NotHidden'. type TypedBindings = TypedBindings' TypedBinding data TypedBindings' a = TypedBindings Range (Arg a) -- ^ . @(xs : e)@ or @{xs : e}@ or something like @(x {y} _ : e)@. deriving (Typeable, Data, Functor, Foldable, Traversable) data BoundName = BName { boundName :: Name , boundLabel :: Name -- ^ for implicit function types the label matters and can't be alpha-renamed , bnameFixity :: Fixity' } deriving (Typeable, Data, Eq, Show) mkBoundName_ :: Name -> BoundName mkBoundName_ x = mkBoundName x noFixity' mkBoundName :: Name -> Fixity' -> BoundName mkBoundName x f = BName x x f -- | A typed binding. type TypedBinding = TypedBinding' Expr data TypedBinding' e = TBind Range [WithHiding BoundName] e -- ^ Binding @(x1 ... xn : A)@. | TLet Range [Declaration] -- ^ Let binding @(let Ds)@ or @(open M args)@. deriving (Typeable, Data, Functor, Foldable, Traversable) -- | A telescope is a sequence of typed bindings. Bound variables are in scope -- in later types. type Telescope = [TypedBindings] countTelVars :: Telescope -> Nat countTelVars tel = sum [ case unArg b of TBind _ xs _ -> genericLength xs TLet{} -> 0 | TypedBindings _ b <- tel ] {-| Left hand sides can be written in infix style. For example: > n + suc m = suc (n + m) > (f ∘ g) x = f (g x) We use fixity information to see which name is actually defined. -} data LHS = LHS { lhsOriginalPattern :: Pattern -- ^ @f ps@ , lhsWithPattern :: [Pattern] -- ^ @| p@ (many) , lhsRewriteEqn :: [RewriteEqn] -- ^ @rewrite e@ (many) , lhsWithExpr :: [WithExpr] -- ^ @with e@ (many) } -- ^ original pattern, with-patterns, rewrite equations and with-expressions | Ellipsis Range [Pattern] [RewriteEqn] [WithExpr] -- ^ new with-patterns, rewrite equations and with-expressions deriving (Typeable, Data) type RewriteEqn = Expr type WithExpr = Expr -- | Processed (scope-checked) intermediate form of the core @f ps@ of 'LHS'. -- Corresponds to 'lhsOriginalPattern'. data LHSCore = LHSHead { lhsDefName :: QName -- ^ @f@ , lhsPats :: [NamedArg Pattern] -- ^ @ps@ } | LHSProj { lhsDestructor :: QName -- ^ record projection identifier , lhsPatsLeft :: [NamedArg Pattern] -- ^ side patterns , lhsFocus :: NamedArg LHSCore -- ^ main branch , lhsPatsRight :: [NamedArg Pattern] -- ^ side patterns } deriving (Typeable) type RHS = RHS' Expr data RHS' e = AbsurdRHS -- ^ No right hand side because of absurd match. | RHS e deriving (Typeable, Data, Functor, Foldable, Traversable) type WhereClause = WhereClause' [Declaration] data WhereClause' decls = NoWhere -- ^ No @where@ clauses. | AnyWhere decls -- ^ Ordinary @where@. | SomeWhere Name Access decls -- ^ Named where: @module M where@. -- The 'Access' flag applies to the 'Name' (not the module contents!) -- and is propagated from the parent function. deriving (Typeable, Data, Functor, Foldable, Traversable) -- | An expression followed by a where clause. -- Currently only used to give better a better error message in interaction. data ExprWhere = ExprWhere Expr WhereClause -- | The things you are allowed to say when you shuffle names between name -- spaces (i.e. in @import@, @namespace@, or @open@ declarations). type ImportDirective = ImportDirective' Name Name type Using = Using' Name Name type Renaming = Renaming' Name Name -- | An imported name can be a module or a defined name. type ImportedName = ImportedName' Name Name data AsName = AsName { asName :: Name -- ^ The \"as\" name. , asRange :: Range -- ^ The range of the \"as\" keyword. Retained for highlighting purposes. } deriving (Typeable, Data, Show) {-------------------------------------------------------------------------- Declarations --------------------------------------------------------------------------} -- | Just type signatures. type TypeSignature = Declaration -- | Just type signatures or instance blocks. type TypeSignatureOrInstanceBlock = Declaration {-| The representation type of a declaration. The comments indicate which type in the intended family the constructor targets. -} data Declaration = TypeSig ArgInfo Name Expr -- ^ Axioms and functions can be irrelevant. (Hiding should be NotHidden) | Field IsInstance Name (Arg Expr) -- ^ Record field, can be hidden and/or irrelevant. | FunClause LHS RHS WhereClause Bool | DataSig Range Induction Name [LamBinding] Expr -- ^ lone data signature in mutual block | Data Range Induction Name [LamBinding] (Maybe Expr) [TypeSignatureOrInstanceBlock] | RecordSig Range Name [LamBinding] Expr -- ^ lone record signature in mutual block | Record Range Name (Maybe (Ranged Induction)) (Maybe Bool) (Maybe (Name, IsInstance)) [LamBinding] (Maybe Expr) [Declaration] -- ^ The optional name is a name for the record constructor. | Infix Fixity [Name] | Syntax Name Notation -- ^ notation declaration for a name | PatternSyn Range Name [Arg Name] Pattern | Mutual Range [Declaration] | Abstract Range [Declaration] | Private Range Origin [Declaration] -- ^ In "Agda.Syntax.Concrete.Definitions" we generate private blocks -- temporarily, which should be treated different that user-declared -- private blocks. Thus the 'Origin'. | InstanceB Range [Declaration] | Macro Range [Declaration] | Postulate Range [TypeSignatureOrInstanceBlock] | Primitive Range [TypeSignature] | Open Range QName ImportDirective | Import Range QName (Maybe AsName) !OpenShortHand ImportDirective | ModuleMacro Range Name ModuleApplication !OpenShortHand ImportDirective | Module Range QName [TypedBindings] [Declaration] | UnquoteDecl Range [Name] Expr | UnquoteDef Range [Name] Expr | Pragma Pragma deriving (Typeable, Data) data ModuleApplication = SectionApp Range [TypedBindings] Expr -- ^ @tel. M args@ | RecordModuleIFS Range QName -- ^ @M {{...}}@ deriving (Typeable, Data) data OpenShortHand = DoOpen | DontOpen deriving (Typeable, Data, Eq, Show) -- Pragmas ---------------------------------------------------------------- data Pragma = OptionsPragma Range [String] | BuiltinPragma Range String Expr | RewritePragma Range [QName] | CompiledDataPragma Range QName String [String] | CompiledTypePragma Range QName String | CompiledPragma Range QName String | CompiledExportPragma Range QName String | CompiledJSPragma Range QName String | CompiledUHCPragma Range QName String | CompiledDataUHCPragma Range QName String [String] | HaskellCodePragma Range String | ForeignPragma Range String String -- ^ first string is backend name | CompilePragma Range String QName String -- ^ first string is backend name | StaticPragma Range QName | InjectivePragma Range QName | InlinePragma Range QName | ImportPragma Range String -- ^ Invariant: The string must be a valid Haskell module name. | ImportUHCPragma Range String -- ^ same as above, but for the UHC backend | ImpossiblePragma Range | EtaPragma Range QName -- ^ For coinductive records, use pragma instead of regular -- @eta-equality@ definition (as it is might make Agda loop). | TerminationCheckPragma Range (TerminationCheck Name) | CatchallPragma Range | DisplayPragma Range Pattern Expr | NoPositivityCheckPragma Range | PolarityPragma Range Name [Occurrence] deriving (Typeable, Data) --------------------------------------------------------------------------- -- | Modules: Top-level pragmas plus other top-level declarations. type Module = ([Pragma], [Declaration]) -- | Computes the top-level module name. -- -- Precondition: The 'Module' has to be well-formed. -- This means that there are only allowed declarations before the -- first module declaration, typically import declarations. -- See 'spanAllowedBeforeModule'. topLevelModuleName :: Module -> TopLevelModuleName topLevelModuleName (_, []) = __IMPOSSIBLE__ topLevelModuleName (_, ds) = case spanAllowedBeforeModule ds of (_, Module _ n _ _ : _) -> toTopLevelModuleName n _ -> __IMPOSSIBLE__ -- | Splits off allowed (= import) declarations before the first -- non-allowed declaration. -- After successful parsing, the first non-allowed declaration -- should be a module declaration. spanAllowedBeforeModule :: [Declaration] -> ([Declaration], [Declaration]) spanAllowedBeforeModule = span isAllowedBeforeModule where isAllowedBeforeModule (Pragma OptionsPragma{}) = True isAllowedBeforeModule (Private _ _ ds) = all isAllowedBeforeModule ds isAllowedBeforeModule Import{} = True isAllowedBeforeModule ModuleMacro{} = True isAllowedBeforeModule Open{} = True isAllowedBeforeModule _ = False {-------------------------------------------------------------------------- Lenses --------------------------------------------------------------------------} mapLhsOriginalPattern :: (Pattern -> Pattern) -> LHS -> LHS mapLhsOriginalPattern f lhs@Ellipsis{} = lhs mapLhsOriginalPattern f lhs@LHS{ lhsOriginalPattern = p } = lhs { lhsOriginalPattern = f p } {-------------------------------------------------------------------------- Views --------------------------------------------------------------------------} -- | The 'Expr' is not an application. data AppView = AppView Expr [NamedArg Expr] appView :: Expr -> AppView appView e = case e of App r e1 e2 -> vApp (appView e1) e2 RawApp _ (e:es) -> AppView e $ map arg es _ -> AppView e [] where vApp (AppView e es) arg = AppView e (es ++ [arg]) arg (HiddenArg _ e) = hide $ defaultArg e arg (InstanceArg _ e) = makeInstance $ defaultArg e arg e = defaultArg (unnamed e) {-------------------------------------------------------------------------- Patterns --------------------------------------------------------------------------} -- | Get all the identifiers in a pattern in left-to-right order. patternQNames :: Pattern -> [QName] patternQNames p = case p of IdentP x -> [x] AppP p p' -> concatMap patternQNames [p, namedArg p'] RawAppP _ ps -> concatMap patternQNames ps OpAppP _ x _ ps -> x : concatMap (patternQNames . namedArg) ps HiddenP _ (namedPat) -> patternQNames (namedThing namedPat) ParenP _ p -> patternQNames p WildP _ -> [] AbsurdP _ -> [] AsP _ x p -> patternQNames p DotP{} -> [] LitP _ -> [] QuoteP _ -> [] InstanceP _ (namedPat) -> patternQNames (namedThing namedPat) RecP _ fs -> concatMap (patternQNames . (^. exprFieldA)) fs -- | Get all the identifiers in a pattern in left-to-right order. patternNames :: Pattern -> [Name] patternNames = map unqualify . patternQNames {-------------------------------------------------------------------------- Instances --------------------------------------------------------------------------} -- Null ------------------------------------------------------------------------ -- | A 'WhereClause' is 'null' when the @where@ keyword is absent. -- An empty list of declarations does not count as 'null' here. instance Null (WhereClause' a) where empty = NoWhere null NoWhere = True null AnyWhere{} = False null SomeWhere{} = False -- Lenses ------------------------------------------------------------------------ instance LensRelevance TypedBindings where getRelevance (TypedBindings _ b) = getRelevance b mapRelevance f (TypedBindings r b) = TypedBindings r $ mapRelevance f b instance LensHiding TypedBindings where getHiding (TypedBindings _ b) = getHiding b mapHiding f (TypedBindings r b) = TypedBindings r $ mapHiding f b instance LensHiding LamBinding where getHiding (DomainFree ai _) = getHiding ai getHiding (DomainFull a) = getHiding a mapHiding f (DomainFree ai x) = DomainFree (mapHiding f ai) x mapHiding f (DomainFull a) = DomainFull $ mapHiding f a -- HasRange instances ------------------------------------------------------------------------ instance HasRange e => HasRange (OpApp e) where getRange e = case e of Ordinary e -> getRange e SyntaxBindingLambda r _ _ -> r instance HasRange Expr where getRange e = case e of Ident x -> getRange x Lit x -> getRange x QuestionMark r _ -> r Underscore r _ -> r App r _ _ -> r RawApp r _ -> r OpApp r _ _ _ -> r WithApp r _ _ -> r Lam r _ _ -> r AbsurdLam r _ -> r ExtendedLam r _ -> r Fun r _ _ -> r Pi b e -> fuseRange b e Set r -> r Prop r -> r SetN r _ -> r Let r _ _ -> r Paren r _ -> r IdiomBrackets r _ -> r As r _ _ -> r Dot r _ -> r Absurd r -> r HiddenArg r _ -> r InstanceArg r _ -> r Rec r _ -> r RecUpdate r _ _ -> r ETel tel -> getRange tel QuoteGoal r _ _ -> r QuoteContext r -> r Quote r -> r QuoteTerm r -> r Unquote r -> r Tactic r _ _ -> r DontCare{} -> noRange Equal r _ _ -> r -- instance HasRange Telescope where -- getRange (TeleBind bs) = getRange bs -- getRange (TeleFun x y) = fuseRange x y instance HasRange TypedBindings where getRange (TypedBindings r _) = r instance HasRange TypedBinding where getRange (TBind r _ _) = r getRange (TLet r _) = r instance HasRange LamBinding where getRange (DomainFree _ x) = getRange x getRange (DomainFull b) = getRange b instance HasRange BoundName where getRange = getRange . boundName instance HasRange WhereClause where getRange NoWhere = noRange getRange (AnyWhere ds) = getRange ds getRange (SomeWhere _ _ ds) = getRange ds instance HasRange ModuleApplication where getRange (SectionApp r _ _) = r getRange (RecordModuleIFS r _) = r instance HasRange a => HasRange (FieldAssignment' a) where getRange (FieldAssignment a b) = fuseRange a b instance HasRange ModuleAssignment where getRange (ModuleAssignment a b c) = fuseRange a b `fuseRange` c instance HasRange Declaration where getRange (TypeSig _ x t) = fuseRange x t getRange (Field _ x t) = fuseRange x t getRange (FunClause lhs rhs wh _) = fuseRange lhs rhs `fuseRange` wh getRange (DataSig r _ _ _ _) = r getRange (Data r _ _ _ _ _) = r getRange (RecordSig r _ _ _) = r getRange (Record r _ _ _ _ _ _ _) = r getRange (Mutual r _) = r getRange (Abstract r _) = r getRange (Open r _ _) = r getRange (ModuleMacro r _ _ _ _) = r getRange (Import r _ _ _ _) = r getRange (InstanceB r _) = r getRange (Macro r _) = r getRange (Private r _ _) = r getRange (Postulate r _) = r getRange (Primitive r _) = r getRange (Module r _ _ _) = r getRange (Infix f _) = getRange f getRange (Syntax n _) = getRange n getRange (PatternSyn r _ _ _) = r getRange (UnquoteDecl r _ _) = r getRange (UnquoteDef r _ _) = r getRange (Pragma p) = getRange p instance HasRange LHS where getRange (LHS p ps eqns ws) = fuseRange p (fuseRange ps (eqns ++ ws)) getRange (Ellipsis r _ _ _) = r instance HasRange LHSCore where getRange (LHSHead f ps) = fuseRange f ps getRange (LHSProj d ps1 lhscore ps2) = d `fuseRange` ps1 `fuseRange` lhscore `fuseRange` ps2 instance HasRange RHS where getRange AbsurdRHS = noRange getRange (RHS e) = getRange e instance HasRange Pragma where getRange (OptionsPragma r _) = r getRange (BuiltinPragma r _ _) = r getRange (RewritePragma r _) = r getRange (CompiledDataPragma r _ _ _) = r getRange (CompiledTypePragma r _ _) = r getRange (CompiledPragma r _ _) = r getRange (CompiledExportPragma r _ _) = r getRange (CompiledJSPragma r _ _) = r getRange (CompiledUHCPragma r _ _) = r getRange (CompiledDataUHCPragma r _ _ _) = r getRange (HaskellCodePragma r _) = r getRange (CompilePragma r _ _ _) = r getRange (ForeignPragma r _ _) = r getRange (StaticPragma r _) = r getRange (InjectivePragma r _) = r getRange (InlinePragma r _) = r getRange (ImportPragma r _) = r getRange (ImportUHCPragma r _) = r getRange (ImpossiblePragma r) = r getRange (EtaPragma r _) = r getRange (TerminationCheckPragma r _) = r getRange (CatchallPragma r) = r getRange (DisplayPragma r _ _) = r getRange (NoPositivityCheckPragma r) = r getRange (PolarityPragma r _ _) = r instance HasRange AsName where getRange a = getRange (asRange a, asName a) instance HasRange Pattern where getRange (IdentP x) = getRange x getRange (AppP p q) = fuseRange p q getRange (OpAppP r _ _ _) = r getRange (RawAppP r _) = r getRange (ParenP r _) = r getRange (WildP r) = r getRange (AsP r _ _) = r getRange (AbsurdP r) = r getRange (LitP l) = getRange l getRange (QuoteP r) = r getRange (HiddenP r _) = r getRange (InstanceP r _) = r getRange (DotP r _ _) = r getRange (RecP r _) = r -- SetRange instances ------------------------------------------------------------------------ instance SetRange TypedBindings where setRange r (TypedBindings _ b) = TypedBindings r b instance SetRange Pattern where setRange r (IdentP x) = IdentP (setRange r x) setRange r (AppP p q) = AppP (setRange r p) (setRange r q) setRange r (OpAppP _ x ns ps) = OpAppP r x ns ps setRange r (RawAppP _ ps) = RawAppP r ps setRange r (ParenP _ p) = ParenP r p setRange r (WildP _) = WildP r setRange r (AsP _ x p) = AsP r (setRange r x) p setRange r (AbsurdP _) = AbsurdP r setRange r (LitP l) = LitP (setRange r l) setRange r (QuoteP _) = QuoteP r setRange r (HiddenP _ p) = HiddenP r p setRange r (InstanceP _ p) = InstanceP r p setRange r (DotP _ o e) = DotP r o e setRange r (RecP _ fs) = RecP r fs -- KillRange instances ------------------------------------------------------------------------ instance KillRange a => KillRange (FieldAssignment' a) where killRange (FieldAssignment a b) = killRange2 FieldAssignment a b instance KillRange ModuleAssignment where killRange (ModuleAssignment a b c) = killRange3 ModuleAssignment a b c instance KillRange AsName where killRange (AsName n _) = killRange1 (flip AsName noRange) n instance KillRange BoundName where killRange (BName n l f) = killRange3 BName n l f instance KillRange Declaration where killRange (TypeSig i n e) = killRange2 (TypeSig i) n e killRange (Field i n a) = killRange2 (Field i) n a killRange (FunClause l r w ca) = killRange4 FunClause l r w ca killRange (DataSig _ i n l e) = killRange4 (DataSig noRange) i n l e killRange (Data _ i n l e c) = killRange4 (Data noRange i) n l e c killRange (RecordSig _ n l e) = killRange3 (RecordSig noRange) n l e killRange (Record _ n mi mb mn k e d)= killRange7 (Record noRange) n mi mb mn k e d killRange (Infix f n) = killRange2 Infix f n killRange (Syntax n no) = killRange1 (\n -> Syntax n no) n killRange (PatternSyn _ n ns p) = killRange3 (PatternSyn noRange) n ns p killRange (Mutual _ d) = killRange1 (Mutual noRange) d killRange (Abstract _ d) = killRange1 (Abstract noRange) d killRange (Private _ o d) = killRange2 (Private noRange) o d killRange (InstanceB _ d) = killRange1 (InstanceB noRange) d killRange (Macro _ d) = killRange1 (Macro noRange) d killRange (Postulate _ t) = killRange1 (Postulate noRange) t killRange (Primitive _ t) = killRange1 (Primitive noRange) t killRange (Open _ q i) = killRange2 (Open noRange) q i killRange (Import _ q a o i) = killRange3 (\q a -> Import noRange q a o) q a i killRange (ModuleMacro _ n m o i) = killRange3 (\n m -> ModuleMacro noRange n m o) n m i killRange (Module _ q t d) = killRange3 (Module noRange) q t d killRange (UnquoteDecl _ x t) = killRange2 (UnquoteDecl noRange) x t killRange (UnquoteDef _ x t) = killRange2 (UnquoteDef noRange) x t killRange (Pragma p) = killRange1 Pragma p instance KillRange Expr where killRange (Ident q) = killRange1 Ident q killRange (Lit l) = killRange1 Lit l killRange (QuestionMark _ n) = QuestionMark noRange n killRange (Underscore _ n) = Underscore noRange n killRange (RawApp _ e) = killRange1 (RawApp noRange) e killRange (App _ e a) = killRange2 (App noRange) e a killRange (OpApp _ n ns o) = killRange3 (OpApp noRange) n ns o killRange (WithApp _ e es) = killRange2 (WithApp noRange) e es killRange (HiddenArg _ n) = killRange1 (HiddenArg noRange) n killRange (InstanceArg _ n) = killRange1 (InstanceArg noRange) n killRange (Lam _ l e) = killRange2 (Lam noRange) l e killRange (AbsurdLam _ h) = killRange1 (AbsurdLam noRange) h killRange (ExtendedLam _ lrw) = killRange1 (ExtendedLam noRange) lrw killRange (Fun _ e1 e2) = killRange2 (Fun noRange) e1 e2 killRange (Pi t e) = killRange2 Pi t e killRange (Set _) = Set noRange killRange (Prop _) = Prop noRange killRange (SetN _ n) = SetN noRange n killRange (Rec _ ne) = killRange1 (Rec noRange) ne killRange (RecUpdate _ e ne) = killRange2 (RecUpdate noRange) e ne killRange (Let _ d e) = killRange2 (Let noRange) d e killRange (Paren _ e) = killRange1 (Paren noRange) e killRange (IdiomBrackets _ e) = killRange1 (IdiomBrackets noRange) e killRange (Absurd _) = Absurd noRange killRange (As _ n e) = killRange2 (As noRange) n e killRange (Dot _ e) = killRange1 (Dot noRange) e killRange (ETel t) = killRange1 ETel t killRange (QuoteGoal _ n e) = killRange2 (QuoteGoal noRange) n e killRange (QuoteContext _) = QuoteContext noRange killRange (Quote _) = Quote noRange killRange (QuoteTerm _) = QuoteTerm noRange killRange (Unquote _) = Unquote noRange killRange (Tactic _ t es) = killRange2 (Tactic noRange) t es killRange (DontCare e) = killRange1 DontCare e killRange (Equal _ x y) = Equal noRange x y instance KillRange LamBinding where killRange (DomainFree i b) = killRange2 DomainFree i b killRange (DomainFull t) = killRange1 DomainFull t instance KillRange LHS where killRange (LHS p ps r w) = killRange4 LHS p ps r w killRange (Ellipsis _ p r w) = killRange3 (Ellipsis noRange) p r w instance KillRange ModuleApplication where killRange (SectionApp _ t e) = killRange2 (SectionApp noRange) t e killRange (RecordModuleIFS _ q) = killRange1 (RecordModuleIFS noRange) q instance KillRange e => KillRange (OpApp e) where killRange (SyntaxBindingLambda _ l e) = killRange2 (SyntaxBindingLambda noRange) l e killRange (Ordinary e) = killRange1 Ordinary e instance KillRange Pattern where killRange (IdentP q) = killRange1 IdentP q killRange (AppP p n) = killRange2 AppP p n killRange (RawAppP _ p) = killRange1 (RawAppP noRange) p killRange (OpAppP _ n ns p) = killRange3 (OpAppP noRange) n ns p killRange (HiddenP _ n) = killRange1 (HiddenP noRange) n killRange (InstanceP _ n) = killRange1 (InstanceP noRange) n killRange (ParenP _ p) = killRange1 (ParenP noRange) p killRange (WildP _) = WildP noRange killRange (AbsurdP _) = AbsurdP noRange killRange (AsP _ n p) = killRange2 (AsP noRange) n p killRange (DotP _ o e) = killRange1 (DotP noRange) o e killRange (LitP l) = killRange1 LitP l killRange (QuoteP _) = QuoteP noRange killRange (RecP _ fs) = killRange1 (RecP noRange) fs instance KillRange Pragma where killRange (OptionsPragma _ s) = OptionsPragma noRange s killRange (BuiltinPragma _ s e) = killRange1 (BuiltinPragma noRange s) e killRange (RewritePragma _ qs) = killRange1 (RewritePragma noRange) qs killRange (CompiledDataPragma _ q s ss) = killRange1 (\q -> CompiledDataPragma noRange q s ss) q killRange (CompiledTypePragma _ q s) = killRange1 (\q -> CompiledTypePragma noRange q s) q killRange (CompiledPragma _ q s) = killRange1 (\q -> CompiledPragma noRange q s) q killRange (CompiledExportPragma _ q s) = killRange1 (\q -> CompiledExportPragma noRange q s) q killRange (CompiledJSPragma _ q s) = killRange1 (\q -> CompiledJSPragma noRange q s) q killRange (CompiledUHCPragma _ q s) = killRange1 (\q -> CompiledUHCPragma noRange q s) q killRange (CompiledDataUHCPragma _ q s ss) = killRange1 (\q -> CompiledDataUHCPragma noRange q s ss) q killRange (HaskellCodePragma _ s) = HaskellCodePragma noRange s killRange (StaticPragma _ q) = killRange1 (StaticPragma noRange) q killRange (InjectivePragma _ q) = killRange1 (InjectivePragma noRange) q killRange (InlinePragma _ q) = killRange1 (InlinePragma noRange) q killRange (ImportPragma _ s) = ImportPragma noRange s killRange (ImportUHCPragma _ s) = ImportUHCPragma noRange s killRange (CompilePragma _ b q s) = killRange1 (\ q -> CompilePragma noRange b q s) q killRange (ForeignPragma _ b s) = ForeignPragma noRange b s killRange (ImpossiblePragma _) = ImpossiblePragma noRange killRange (TerminationCheckPragma _ t) = TerminationCheckPragma noRange (killRange t) killRange (CatchallPragma _) = CatchallPragma noRange killRange (DisplayPragma _ lhs rhs) = killRange2 (DisplayPragma noRange) lhs rhs killRange (EtaPragma _ q) = killRange1 (EtaPragma noRange) q killRange (NoPositivityCheckPragma _) = NoPositivityCheckPragma noRange killRange (PolarityPragma _ q occs) = killRange1 (\q -> PolarityPragma noRange q occs) q instance KillRange RHS where killRange AbsurdRHS = AbsurdRHS killRange (RHS e) = killRange1 RHS e instance KillRange TypedBinding where killRange (TBind _ b e) = killRange2 (TBind noRange) b e killRange (TLet r ds) = killRange2 TLet r ds instance KillRange TypedBindings where killRange (TypedBindings _ t) = killRange1 (TypedBindings noRange) t instance KillRange WhereClause where killRange NoWhere = NoWhere killRange (AnyWhere d) = killRange1 AnyWhere d killRange (SomeWhere n a d) = killRange3 SomeWhere n a d ------------------------------------------------------------------------ -- NFData instances -- | Ranges are not forced. instance NFData Expr where rnf (Ident a) = rnf a rnf (Lit a) = rnf a rnf (QuestionMark _ a) = rnf a rnf (Underscore _ a) = rnf a rnf (RawApp _ a) = rnf a rnf (App _ a b) = rnf a `seq` rnf b rnf (OpApp _ a b c) = rnf a `seq` rnf b `seq` rnf c rnf (WithApp _ a b) = rnf a `seq` rnf b rnf (HiddenArg _ a) = rnf a rnf (InstanceArg _ a) = rnf a rnf (Lam _ a b) = rnf a `seq` rnf b rnf (AbsurdLam _ a) = rnf a rnf (ExtendedLam _ a) = rnf a rnf (Fun _ a b) = rnf a `seq` rnf b rnf (Pi a b) = rnf a `seq` rnf b rnf (Set _) = () rnf (Prop _) = () rnf (SetN _ a) = rnf a rnf (Rec _ a) = rnf a rnf (RecUpdate _ a b) = rnf a `seq` rnf b rnf (Let _ a b) = rnf a `seq` rnf b rnf (Paren _ a) = rnf a rnf (IdiomBrackets _ a)= rnf a rnf (Absurd _) = () rnf (As _ a b) = rnf a `seq` rnf b rnf (Dot _ a) = rnf a rnf (ETel a) = rnf a rnf (QuoteGoal _ a b) = rnf a `seq` rnf b rnf (QuoteContext _) = () rnf (Quote _) = () rnf (QuoteTerm _) = () rnf (Tactic _ a b) = rnf a `seq` rnf b rnf (Unquote _) = () rnf (DontCare a) = rnf a rnf (Equal _ a b) = rnf a `seq` rnf b -- | Ranges are not forced. instance NFData Pattern where rnf (IdentP a) = rnf a rnf (QuoteP _) = () rnf (AppP a b) = rnf a `seq` rnf b rnf (RawAppP _ a) = rnf a rnf (OpAppP _ a b c) = rnf a `seq` rnf b `seq` rnf c rnf (HiddenP _ a) = rnf a rnf (InstanceP _ a) = rnf a rnf (ParenP _ a) = rnf a rnf (WildP _) = () rnf (AbsurdP _) = () rnf (AsP _ a b) = rnf a `seq` rnf b rnf (DotP _ a b) = rnf a `seq` rnf b rnf (LitP a) = rnf a rnf (RecP _ a) = rnf a -- | Ranges are not forced. instance NFData Declaration where rnf (TypeSig a b c) = rnf a `seq` rnf b `seq` rnf c rnf (Field a b c) = rnf a `seq` rnf b `seq` rnf c rnf (FunClause a b c d) = rnf a `seq` rnf b `seq` rnf c `seq` rnf d rnf (DataSig _ a b c d) = rnf a `seq` rnf b `seq` rnf c `seq` rnf d rnf (Data _ a b c d e) = rnf a `seq` rnf b `seq` rnf c `seq` rnf d `seq` rnf e rnf (RecordSig _ a b c) = rnf a `seq` rnf b `seq` rnf c rnf (Record _ a b c d e f g) = rnf a `seq` rnf b `seq` rnf c `seq` rnf d `seq` rnf e `seq` rnf f `seq` rnf g rnf (Infix a b) = rnf a `seq` rnf b rnf (Syntax a b) = rnf a `seq` rnf b rnf (PatternSyn _ a b c) = rnf a `seq` rnf b `seq` rnf c rnf (Mutual _ a) = rnf a rnf (Abstract _ a) = rnf a rnf (Private _ _ a) = rnf a rnf (InstanceB _ a) = rnf a rnf (Macro _ a) = rnf a rnf (Postulate _ a) = rnf a rnf (Primitive _ a) = rnf a rnf (Open _ a b) = rnf a `seq` rnf b rnf (Import _ a b _ c) = rnf a `seq` rnf b `seq` rnf c rnf (ModuleMacro _ a b _ c) = rnf a `seq` rnf b `seq` rnf c rnf (Module _ a b c) = rnf a `seq` rnf b `seq` rnf c rnf (UnquoteDecl _ a b) = rnf a `seq` rnf b rnf (UnquoteDef _ a b) = rnf a `seq` rnf b rnf (Pragma a) = rnf a -- | Ranges are not forced. instance NFData Pragma where rnf (OptionsPragma _ a) = rnf a rnf (BuiltinPragma _ a b) = rnf a `seq` rnf b rnf (RewritePragma _ a) = rnf a rnf (CompiledDataPragma _ a b c) = rnf a `seq` rnf b `seq` rnf c rnf (CompiledTypePragma _ a b) = rnf a `seq` rnf b rnf (CompiledPragma _ a b) = rnf a `seq` rnf b rnf (CompiledExportPragma _ a b) = rnf a `seq` rnf b rnf (CompiledJSPragma _ a b) = rnf a `seq` rnf b rnf (CompiledUHCPragma _ a b) = rnf a `seq` rnf b rnf (CompiledDataUHCPragma _ a b c) = rnf a `seq` rnf b `seq` rnf c rnf (HaskellCodePragma _ s) = rnf s rnf (CompilePragma _ a b c) = rnf a `seq` rnf b `seq` rnf c rnf (ForeignPragma _ b s) = rnf b `seq` rnf s rnf (StaticPragma _ a) = rnf a rnf (InjectivePragma _ a) = rnf a rnf (InlinePragma _ a) = rnf a rnf (ImportPragma _ a) = rnf a rnf (ImportUHCPragma _ a) = rnf a rnf (ImpossiblePragma _) = () rnf (EtaPragma _ a) = rnf a rnf (TerminationCheckPragma _ a) = rnf a rnf (CatchallPragma _) = () rnf (DisplayPragma _ a b) = rnf a `seq` rnf b rnf (NoPositivityCheckPragma _) = () rnf (PolarityPragma _ a b) = rnf a `seq` rnf b -- | Ranges are not forced. instance NFData a => NFData (TypedBindings' a) where rnf (TypedBindings _ a) = rnf a -- | Ranges are not forced. instance NFData AsName where rnf (AsName a _) = rnf a -- | Ranges are not forced. instance NFData a => NFData (TypedBinding' a) where rnf (TBind _ a b) = rnf a `seq` rnf b rnf (TLet _ a) = rnf a -- | Ranges are not forced. instance NFData ModuleApplication where rnf (SectionApp _ a b) = rnf a `seq` rnf b rnf (RecordModuleIFS _ a) = rnf a -- | Ranges are not forced. instance NFData a => NFData (OpApp a) where rnf (SyntaxBindingLambda _ a b) = rnf a `seq` rnf b rnf (Ordinary a) = rnf a -- | Ranges are not forced. instance NFData LHS where rnf (LHS a b c d) = rnf a `seq` rnf b `seq` rnf c `seq` rnf d rnf (Ellipsis _ a b c) = rnf a `seq` rnf b `seq` rnf c instance NFData a => NFData (FieldAssignment' a) where rnf (FieldAssignment a b) = rnf a `seq` rnf b instance NFData ModuleAssignment where rnf (ModuleAssignment a b c) = rnf a `seq` rnf b `seq` rnf c instance NFData a => NFData (WhereClause' a) where rnf NoWhere = () rnf (AnyWhere a) = rnf a rnf (SomeWhere a b c) = rnf a `seq` rnf b `seq` rnf c instance NFData a => NFData (LamBinding' a) where rnf (DomainFree a b) = rnf a `seq` rnf b rnf (DomainFull a) = rnf a instance NFData BoundName where rnf (BName a b c) = rnf a `seq` rnf b `seq` rnf c instance NFData a => NFData (RHS' a) where rnf AbsurdRHS = () rnf (RHS a) = rnf a Agda-2.5.3/src/full/Agda/Syntax/Abstract.hs0000644000000000000000000014062313154613124016523 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE TemplateHaskell #-} {-| The abstract syntax. This is what you get after desugaring and scope analysis of the concrete syntax. The type checker works on abstract syntax, producing internal syntax ("Agda.Syntax.Internal"). -} module Agda.Syntax.Abstract ( module Agda.Syntax.Abstract , module Agda.Syntax.Abstract.Name ) where import Prelude import Control.Arrow (first) import Control.Applicative import Data.Foldable (Foldable) import qualified Data.Foldable as Fold import Data.Map (Map) import Data.Maybe import Data.Sequence (Seq, (<|), (><)) import qualified Data.Sequence as Seq import Data.Traversable import Data.Void import Data.Data (Data) import Data.Typeable (Typeable) import Agda.Syntax.Concrete.Name (NumHoles(..)) import Agda.Syntax.Concrete (FieldAssignment'(..), exprFieldA) import qualified Agda.Syntax.Concrete as C import Agda.Syntax.Concrete.Pretty () import Agda.Syntax.Info import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Syntax.Abstract.Name import Agda.Syntax.Abstract.Name as A (QNamed) import Agda.Syntax.Literal import Agda.Syntax.Scope.Base import qualified Agda.Syntax.Internal as I import Agda.TypeChecking.Positivity.Occurrence import Agda.Utils.Functor import Agda.Utils.Geniplate import Agda.Utils.Lens import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible type Args = [NamedArg Expr] -- | Expressions after scope checking (operators parsed, names resolved). data Expr = Var Name -- ^ Bound variable. | Def QName -- ^ Constant: axiom, function, data or record type. | Proj ProjOrigin AmbiguousQName -- ^ Projection (overloaded). | Con AmbiguousQName -- ^ Constructor (overloaded). | PatternSyn QName -- ^ Pattern synonym. | Macro QName -- ^ Macro. | Lit Literal -- ^ Literal. | QuestionMark MetaInfo InteractionId -- ^ Meta variable for interaction. -- The 'InteractionId' is usually identical with the -- 'metaNumber' of 'MetaInfo'. -- However, if you want to print an interaction meta as -- just @?@ instead of @?n@, you should set the -- 'metaNumber' to 'Nothing' while keeping the 'InteractionId'. | Underscore MetaInfo -- ^ Meta variable for hidden argument (must be inferred locally). | Dot ExprInfo Expr -- ^ @.e@, for postfix projection. | App ExprInfo Expr (NamedArg Expr) -- ^ Ordinary (binary) application. | WithApp ExprInfo Expr [Expr] -- ^ With application. | Lam LamInfo LamBinding Expr -- ^ @λ bs → e@. | AbsurdLam LamInfo Hiding -- ^ @λ()@ or @λ{}@. | ExtendedLam LamInfo DefInfo QName [Clause] | Pi ExprInfo Telescope Expr -- ^ Dependent function space @Γ → A@. | Fun ExprInfo (Arg Expr) Expr -- ^ Non-dependent function space. | Set ExprInfo Integer -- ^ @Set@, @Set1@, @Set2@, ... | Prop ExprInfo -- ^ @Prop@ (no longer supported, used as dummy type). | Let ExprInfo [LetBinding] Expr -- ^ @let bs in e@. | ETel Telescope -- ^ Only used when printing telescopes. | Rec ExprInfo RecordAssigns -- ^ Record construction. | RecUpdate ExprInfo Expr Assigns -- ^ Record update. | ScopedExpr ScopeInfo Expr -- ^ Scope annotation. | QuoteGoal ExprInfo Name Expr -- ^ Binds @Name@ to current type in @Expr@. | QuoteContext ExprInfo -- ^ Returns the current context. | Quote ExprInfo -- ^ Quote an identifier 'QName'. | QuoteTerm ExprInfo -- ^ Quote a term. | Unquote ExprInfo -- ^ The splicing construct: unquote ... | Tactic ExprInfo Expr [NamedArg Expr] [NamedArg Expr] -- ^ @tactic e x1 .. xn | y1 | .. | yn@ | DontCare Expr -- ^ For printing @DontCare@ from @Syntax.Internal@. deriving (Typeable, Data, Show) -- | Record field assignment @f = e@. type Assign = FieldAssignment' Expr type Assigns = [Assign] type RecordAssign = Either Assign ModuleName type RecordAssigns = [RecordAssign] -- | Is a type signature a `postulate' or a function signature? data Axiom = FunSig -- ^ A function signature. | NoFunSig -- ^ Not a function signature, i.e., a postulate (in user input) -- or another (e.g. data/record) type signature (internally). deriving (Typeable, Data, Eq, Ord, Show) -- | Renaming (generic). type Ren a = [(a, a)] data ScopeCopyInfo = ScopeCopyInfo { renModules :: Ren ModuleName , renNames :: Ren QName } deriving (Eq, Show, Typeable, Data) initCopyInfo :: ScopeCopyInfo initCopyInfo = ScopeCopyInfo { renModules = [] , renNames = [] } instance Pretty ScopeCopyInfo where pretty i = vcat [ prRen "renModules =" (renModules i) , prRen "renNames =" (renNames i) ] where prRen s r = sep [ text s, nest 2 $ vcat (map pr r) ] pr (x, y) = pretty x <+> text "->" <+> pretty y data Declaration = Axiom Axiom DefInfo ArgInfo (Maybe [Occurrence]) QName Expr -- ^ Type signature (can be irrelevant, but not hidden). -- -- The fourth argument contains an optional assignment of -- polarities to arguments. | Field DefInfo QName (Arg Expr) -- ^ record field | Primitive DefInfo QName Expr -- ^ primitive function | Mutual MutualInfo [Declaration] -- ^ a bunch of mutually recursive definitions | Section ModuleInfo ModuleName [TypedBindings] [Declaration] | Apply ModuleInfo ModuleName ModuleApplication ScopeCopyInfo ImportDirective -- ^ The @ImportDirective@ is for highlighting purposes. | Import ModuleInfo ModuleName ImportDirective -- ^ The @ImportDirective@ is for highlighting purposes. | Pragma Range Pragma | Open ModuleInfo ModuleName ImportDirective -- ^ only retained for highlighting purposes | FunDef DefInfo QName Delayed [Clause] -- ^ sequence of function clauses | DataSig DefInfo QName Telescope Expr -- ^ lone data signature | DataDef DefInfo QName [LamBinding] [Constructor] -- ^ the 'LamBinding's are 'DomainFree' and bind the parameters of the datatype. | RecSig DefInfo QName Telescope Expr -- ^ lone record signature | RecDef DefInfo QName (Maybe (Ranged Induction)) (Maybe Bool) (Maybe QName) [LamBinding] Expr [Declaration] -- ^ The 'LamBinding's are 'DomainFree' and bind the parameters of the datatype. -- The 'Expr' gives the constructor type telescope, @(x1 : A1)..(xn : An) -> Prop@, -- and the optional name is the constructor's name. | PatternSynDef QName [Arg Name] (Pattern' Void) -- ^ Only for highlighting purposes | UnquoteDecl MutualInfo [DefInfo] [QName] Expr | UnquoteDef [DefInfo] [QName] Expr | ScopedDecl ScopeInfo [Declaration] -- ^ scope annotation deriving (Typeable, Data, Show) class GetDefInfo a where getDefInfo :: a -> Maybe DefInfo instance GetDefInfo Declaration where getDefInfo (Axiom _ i _ _ _ _) = Just i getDefInfo (Field i _ _) = Just i getDefInfo (Primitive i _ _) = Just i getDefInfo (ScopedDecl _ (d:_)) = getDefInfo d getDefInfo (FunDef i _ _ _) = Just i getDefInfo (DataSig i _ _ _) = Just i getDefInfo (DataDef i _ _ _) = Just i getDefInfo (RecSig i _ _ _) = Just i getDefInfo (RecDef i _ _ _ _ _ _ _) = Just i getDefInfo _ = Nothing type ImportDirective = ImportDirective' QName ModuleName type Renaming = Renaming' QName ModuleName type ImportedName = ImportedName' QName ModuleName data ModuleApplication = SectionApp Telescope ModuleName [NamedArg Expr] -- ^ @tel. M args@: applies @M@ to @args@ and abstracts @tel@. | RecordModuleIFS ModuleName -- ^ @M {{...}}@ deriving (Typeable, Data, Show, Eq) data Pragma = OptionsPragma [String] | BuiltinPragma String Expr | BuiltinNoDefPragma String QName -- ^ Builtins that do not come with a definition, -- but declare a name for an Agda concept. | RewritePragma QName | CompilePragma String QName String | CompiledPragma QName String | CompiledExportPragma QName String | CompiledTypePragma QName String | CompiledDataPragma QName String [String] | CompiledJSPragma QName String | CompiledUHCPragma QName String | CompiledDataUHCPragma QName String [String] | StaticPragma QName | EtaPragma QName -- ^ For coinductive records, use pragma instead of regular -- @eta-equality@ definition (as it is might make Agda loop). | InjectivePragma QName | InlinePragma QName | DisplayPragma QName [NamedArg Pattern] Expr deriving (Typeable, Data, Show, Eq) -- | Bindings that are valid in a @let@. data LetBinding = LetBind LetInfo ArgInfo Name Expr Expr -- ^ @LetBind info rel name type defn@ | LetPatBind LetInfo Pattern Expr -- ^ Irrefutable pattern binding. | LetApply ModuleInfo ModuleName ModuleApplication ScopeCopyInfo ImportDirective -- ^ @LetApply mi newM (oldM args) renamings dir@. -- The @ImportDirective@ is for highlighting purposes. | LetOpen ModuleInfo ModuleName ImportDirective -- ^ only for highlighting and abstractToConcrete | LetDeclaredVariable Name -- ^ Only used for highlighting. Refers to the first occurrence of -- @x@ in @let x : A; x = e@. deriving (Typeable, Data, Show, Eq) -- | Only 'Axiom's. type TypeSignature = Declaration type Constructor = TypeSignature type Field = TypeSignature -- | A lambda binding is either domain free or typed. data LamBinding = DomainFree ArgInfo Name -- ^ . @x@ or @{x}@ or @.x@ or @.{x}@ | DomainFull TypedBindings -- ^ . @(xs:e)@ or @{xs:e}@ or @(let Ds)@ deriving (Typeable, Data, Show, Eq) -- | Typed bindings with hiding information. data TypedBindings = TypedBindings Range (Arg TypedBinding) -- ^ . @(xs : e)@ or @{xs : e}@ deriving (Typeable, Data, Show, Eq) -- | A typed binding. Appears in dependent function spaces, typed lambdas, and -- telescopes. It might be tempting to simplify this to only bind a single -- name at a time, and translate, say, @(x y : A)@ to @(x : A)(y : A)@ -- before type-checking. However, this would be slightly problematic: -- -- 1. We would have to typecheck the type @A@ several times. -- -- 2. If @A@ contains a meta variable or hole, it would be duplicated -- by such a translation. -- -- While 1. is only slightly inefficient, 2. would be an outright bug. -- Duplicating @A@ could not be done naively, we would have to make sure -- that the metas of the copy are aliases of the metas of the original. data TypedBinding = TBind Range [WithHiding Name] Expr -- ^ As in telescope @(x y z : A)@ or type @(x y z : A) -> B@. | TLet Range [LetBinding] -- ^ E.g. @(let x = e)@ or @(let open M)@. deriving (Typeable, Data, Show, Eq) type Telescope = [TypedBindings] data NamedDotPattern = NamedDot Name I.Term I.Type deriving (Typeable, Data, Show) data StrippedDotPattern = StrippedDot Expr I.Term I.Type deriving (Typeable, Data, Show) -- These are not relevant for caching purposes instance Eq NamedDotPattern where _ == _ = True instance Eq StrippedDotPattern where _ == _ = True -- | We could throw away @where@ clauses at this point and translate them to -- @let@. It's not obvious how to remember that the @let@ was really a -- @where@ clause though, so for the time being we keep it here. data Clause' lhs = Clause { clauseLHS :: lhs , clauseNamedDots :: [NamedDotPattern] -- ^ Only in with-clauses where we inherit some already checked dot patterns from the parent. -- These live in the context of the parent clause left-hand side. , clauseStrippedDots :: [StrippedDotPattern] -- ^ In with-clauses where a dot pattern from the parent clause is -- repeated in the with-clause. In this case it's not actually part of -- the clause, but it still needs to be checked (Issue 142). , clauseRHS :: RHS , clauseWhereDecls :: [Declaration] , clauseCatchall :: Bool } deriving (Typeable, Data, Show, Functor, Foldable, Traversable, Eq) type Clause = Clause' LHS type SpineClause = Clause' SpineLHS data RHS = RHS { rhsExpr :: Expr , rhsConcrete :: Maybe C.Expr -- ^ We store the original concrete expression in case -- we have to reproduce it during interactive case splitting. -- 'Nothing' for internally generated rhss. } | AbsurdRHS | WithRHS QName [Expr] [Clause] -- ^ The 'QName' is the name of the with function. | RewriteRHS { rewriteExprs :: [(QName, Expr)] -- ^ The 'QName's are the names of the generated with functions, -- one for each 'Expr'. , rewriteRHS :: RHS -- ^ The RHS should not be another @RewriteRHS@. , rewriteWhereDecls :: [Declaration] -- ^ The where clauses are attached to the @RewriteRHS@ by --- the scope checker (instead of to the clause). } deriving (Typeable, Data, Show) instance Eq RHS where RHS e _ == RHS e' _ = e == e' AbsurdRHS == AbsurdRHS = True WithRHS a b c == WithRHS a' b' c' = and [ a == a', b == b', c == c' ] RewriteRHS a b c == RewriteRHS a' b' c' = and [ a == a', b == b', c == c' ] _ == _ = False -- | The lhs of a clause in spine view (inside-out). -- Projection patterns are contained in @spLhsPats@, -- represented as @ProjP d@. data SpineLHS = SpineLHS { spLhsInfo :: LHSInfo -- ^ Range. , spLhsDefName :: QName -- ^ Name of function we are defining. , spLhsPats :: [NamedArg Pattern] -- ^ Function parameters (patterns). , spLhsWithPats :: [Pattern] -- ^ @with@ patterns (after @|@). } deriving (Typeable, Data, Show, Eq) instance Eq LHS where (LHS _ core wps) == (LHS _ core' wps') = core == core' && wps == wps' -- | The lhs of a clause in focused (projection-application) view (outside-in). -- Projection patters are represented as 'LHSProj's. data LHS = LHS { lhsInfo :: LHSInfo -- ^ Range. , lhsCore :: LHSCore -- ^ Copatterns. , lhsWithPats :: [Pattern] -- ^ @with@ patterns (after @|@). } deriving (Typeable, Data, Show) -- | The lhs minus @with@-patterns in projection-application view. -- Parameterised over the type @e@ of dot patterns. data LHSCore' e -- | The head applied to ordinary patterns. = LHSHead { lhsDefName :: QName -- ^ Head @f@. , lhsPats :: [NamedArg (Pattern' e)] -- ^ Applied to patterns @ps@. } -- | Projection | LHSProj { lhsDestructor :: AmbiguousQName -- ^ Record projection identifier. , lhsFocus :: NamedArg (LHSCore' e) -- ^ Main branch. , lhsPatsRight :: [NamedArg (Pattern' e)] -- ^ Further applied to patterns. } deriving (Typeable, Data, Show, Functor, Foldable, Traversable, Eq) type LHSCore = LHSCore' Expr -- | Convert a focused lhs to spine view and back. class LHSToSpine a b where lhsToSpine :: a -> b spineToLhs :: b -> a -- | Clause instance. instance LHSToSpine Clause SpineClause where lhsToSpine = fmap lhsToSpine spineToLhs = fmap spineToLhs -- | List instance (for clauses). instance LHSToSpine a b => LHSToSpine [a] [b] where lhsToSpine = map lhsToSpine spineToLhs = map spineToLhs -- | LHS instance. instance LHSToSpine LHS SpineLHS where lhsToSpine (LHS i core wps) = SpineLHS i f ps wps where QNamed f ps = lhsCoreToSpine core spineToLhs (SpineLHS i f ps wps) = LHS i (spineToLhsCore $ QNamed f ps) wps lhsCoreToSpine :: LHSCore' e -> A.QNamed [NamedArg (Pattern' e)] lhsCoreToSpine (LHSHead f ps) = QNamed f ps lhsCoreToSpine (LHSProj d h ps) = (++ (p : ps)) <$> lhsCoreToSpine (namedArg h) where p = updateNamedArg (const $ ProjP patNoRange ProjPrefix d) h spineToLhsCore :: IsProjP e => QNamed [NamedArg (Pattern' e)] -> LHSCore' e spineToLhsCore (QNamed f ps) = lhsCoreAddSpine (LHSHead f []) ps -- | Add applicative patterns (non-projection patterns) to the right. lhsCoreApp :: IsProjP e => LHSCore' e -> [NamedArg (Pattern' e)] -> LHSCore' e lhsCoreApp (LHSHead f ps) ps' = LHSHead f $ ps ++ ps' lhsCoreApp (LHSProj d h ps) ps' = LHSProj d h $ ps ++ ps' -- | Add projection and applicative patterns to the right. lhsCoreAddSpine :: IsProjP e => LHSCore' e -> [NamedArg (Pattern' e)] -> LHSCore' e lhsCoreAddSpine core ps = case ps2 of [] -> lhsCoreApp core ps p@(Arg info (Named n (ProjP i o d))) : ps2' | let nh = numHoles d-> -- Andreas, 2016-06-13 -- If the projection was written prefix by the user -- or it is fully applied an operator -- we turn it to prefix projection form. (if o == ProjPrefix || nh > 0 && nh <= 1 + length ps2' then LHSProj d (Arg info $ Named n $ lhsCoreApp core ps1) [] else lhsCoreApp core $ ps1 ++ [p]) `lhsCoreAddSpine` ps2' _ -> __IMPOSSIBLE__ where (ps1, ps2) = break (isJust . isProjP) ps -- | Used for checking pattern linearity. lhsCoreAllPatterns :: LHSCore' e -> [Pattern' e] lhsCoreAllPatterns = map namedArg . qnamed . lhsCoreToSpine -- | Used in AbstractToConcrete. lhsCoreToPattern :: LHSCore -> Pattern lhsCoreToPattern lc = case lc of LHSHead f aps -> DefP noInfo (AmbQ [f]) aps LHSProj d lhscore aps -> DefP noInfo d $ fmap (fmap lhsCoreToPattern) lhscore : aps where noInfo = patNoRange -- TODO, preserve range! mapLHSHead :: (QName -> [NamedArg Pattern] -> LHSCore) -> LHSCore -> LHSCore mapLHSHead f (LHSHead x ps) = f x ps mapLHSHead f (LHSProj d l ps) = LHSProj d (fmap (fmap (mapLHSHead f)) l) ps --------------------------------------------------------------------------- -- * Patterns --------------------------------------------------------------------------- -- | Parameterised over the type of dot patterns. data Pattern' e = VarP Name | ConP ConPatInfo AmbiguousQName [NamedArg (Pattern' e)] | ProjP PatInfo ProjOrigin AmbiguousQName -- ^ Destructor pattern @d@. | DefP PatInfo AmbiguousQName [NamedArg (Pattern' e)] -- ^ Defined pattern: function definition @f ps@. -- It is also abused to convert destructor patterns into concrete syntax -- thus, we put AmbiguousQName here as well. | WildP PatInfo -- ^ Underscore pattern entered by user. -- Or generated at type checking for implicit arguments. | AsP PatInfo Name (Pattern' e) | DotP PatInfo Origin e -- ^ Dot pattern @.e@: the Origin keeps track whether this dot pattern was -- written by the user or inserted by the system (e.g. while expanding -- the ellipsis in a with clause). | AbsurdP PatInfo | LitP Literal | PatternSynP PatInfo QName [NamedArg (Pattern' e)] | RecP PatInfo [FieldAssignment' (Pattern' e)] deriving (Typeable, Data, Show, Functor, Foldable, Traversable, Eq) type Pattern = Pattern' Expr type Patterns = [NamedArg Pattern] instance IsProjP (Pattern' e) where isProjP (ProjP _ o d) = Just (o, d) isProjP _ = Nothing instance IsProjP Expr where isProjP (Proj o ds) = Just (o, ds) isProjP (ScopedExpr _ e) = isProjP e isProjP _ = Nothing class MaybePostfixProjP a where maybePostfixProjP :: a -> Maybe (ProjOrigin, AmbiguousQName) instance IsProjP e => MaybePostfixProjP (Pattern' e) where maybePostfixProjP (DotP _ _ e) = isProjP e <&> \ (_o, d) -> (ProjPostfix, d) maybePostfixProjP (ProjP _ o d) = Just (o, d) maybePostfixProjP _ = Nothing instance MaybePostfixProjP a => MaybePostfixProjP (Arg a) where maybePostfixProjP = maybePostfixProjP . unArg instance MaybePostfixProjP a => MaybePostfixProjP (Named n a) where maybePostfixProjP = maybePostfixProjP . namedThing {-------------------------------------------------------------------------- Instances --------------------------------------------------------------------------} -- | Does not compare 'ScopeInfo' fields. -- Does not distinguish between prefix and postfix projections. instance Eq Expr where ScopedExpr _ a1 == ScopedExpr _ a2 = a1 == a2 Var a1 == Var a2 = a1 == a2 Def a1 == Def a2 = a1 == a2 Proj _ a1 == Proj _ a2 = a1 == a2 Con a1 == Con a2 = a1 == a2 PatternSyn a1 == PatternSyn a2 = a1 == a2 Macro a1 == Macro a2 = a1 == a2 Lit a1 == Lit a2 = a1 == a2 QuestionMark a1 b1 == QuestionMark a2 b2 = (a1, b1) == (a2, b2) Underscore a1 == Underscore a2 = a1 == a2 Dot r1 e1 == Dot r2 e2 = (r1, e1) == (r2, e2) App a1 b1 c1 == App a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) WithApp a1 b1 c1 == WithApp a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) Lam a1 b1 c1 == Lam a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) AbsurdLam a1 b1 == AbsurdLam a2 b2 = (a1, b1) == (a2, b2) ExtendedLam a1 b1 c1 d1 == ExtendedLam a2 b2 c2 d2 = (a1, b1, c1, d1) == (a2, b2, c2, d2) Pi a1 b1 c1 == Pi a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) Fun a1 b1 c1 == Fun a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) Set a1 b1 == Set a2 b2 = (a1, b1) == (a2, b2) Prop a1 == Prop a2 = a1 == a2 Let a1 b1 c1 == Let a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) ETel a1 == ETel a2 = a1 == a2 Rec a1 b1 == Rec a2 b2 = (a1, b1) == (a2, b2) RecUpdate a1 b1 c1 == RecUpdate a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) QuoteGoal a1 b1 c1 == QuoteGoal a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) QuoteContext a1 == QuoteContext a2 = a1 == a2 Quote a1 == Quote a2 = a1 == a2 QuoteTerm a1 == QuoteTerm a2 = a1 == a2 Unquote a1 == Unquote a2 = a1 == a2 Tactic a1 b1 c1 d1 == Tactic a2 b2 c2 d2 = (a1, b1, c1, d1) == (a2, b2, c2, d2) DontCare a1 == DontCare a2 = a1 == a2 _ == _ = False -- | Does not compare 'ScopeInfo' fields. instance Eq Declaration where ScopedDecl _ a1 == ScopedDecl _ a2 = a1 == a2 Axiom a1 b1 c1 d1 e1 f1 == Axiom a2 b2 c2 d2 e2 f2 = (a1, b1, c1, d1, e1, f1) == (a2, b2, c2, d2, e2, f2) Field a1 b1 c1 == Field a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) Primitive a1 b1 c1 == Primitive a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) Mutual a1 b1 == Mutual a2 b2 = (a1, b1) == (a2, b2) Section a1 b1 c1 d1 == Section a2 b2 c2 d2 = (a1, b1, c1, d1) == (a2, b2, c2, d2) Apply a1 b1 c1 d1 e1 == Apply a2 b2 c2 d2 e2 = (a1, b1, c1, d1, e1) == (a2, b2, c2, d2, e2) Import a1 b1 c1 == Import a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) Pragma a1 b1 == Pragma a2 b2 = (a1, b1) == (a2, b2) Open a1 b1 c1 == Open a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) FunDef a1 b1 c1 d1 == FunDef a2 b2 c2 d2 = (a1, b1, c1, d1) == (a2, b2, c2, d2) DataSig a1 b1 c1 d1 == DataSig a2 b2 c2 d2 = (a1, b1, c1, d1) == (a2, b2, c2, d2) DataDef a1 b1 c1 d1 == DataDef a2 b2 c2 d2 = (a1, b1, c1, d1) == (a2, b2, c2, d2) RecSig a1 b1 c1 d1 == RecSig a2 b2 c2 d2 = (a1, b1, c1, d1) == (a2, b2, c2, d2) RecDef a1 b1 c1 d1 e1 f1 g1 h1 == RecDef a2 b2 c2 d2 e2 f2 g2 h2 = (a1, b1, c1, d1, e1, f1, g1, h1) == (a2, b2, c2, d2, e2, f2, g2, h2) PatternSynDef a1 b1 c1 == PatternSynDef a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) UnquoteDecl a1 b1 c1 d1 == UnquoteDecl a2 b2 c2 d2 = (a1, b1, c1, d1) == (a2, b2, c2, d2) UnquoteDef a1 b1 c1 == UnquoteDef a2 b2 c2 = (a1, b1, c1) == (a2, b2, c2) _ == _ = False instance Underscore Expr where underscore = Underscore emptyMetaInfo isUnderscore = __IMPOSSIBLE__ instance LensHiding TypedBindings where getHiding (TypedBindings _ a) = getHiding a mapHiding f (TypedBindings r a) = TypedBindings r $ mapHiding f a instance LensHiding LamBinding where getHiding (DomainFree ai _) = getHiding ai getHiding (DomainFull tb) = getHiding tb mapHiding f (DomainFree ai x) = mapHiding f ai `DomainFree` x mapHiding f (DomainFull tb) = DomainFull $ mapHiding f tb instance HasRange LamBinding where getRange (DomainFree _ x) = getRange x getRange (DomainFull b) = getRange b instance HasRange TypedBindings where getRange (TypedBindings r _) = r instance HasRange TypedBinding where getRange (TBind r _ _) = r getRange (TLet r _) = r instance HasRange Expr where getRange (Var x) = getRange x getRange (Def x) = getRange x getRange (Proj _ x) = getRange x getRange (Con x) = getRange x getRange (Lit l) = getRange l getRange (QuestionMark i _) = getRange i getRange (Underscore i) = getRange i getRange (Dot i _) = getRange i getRange (App i _ _) = getRange i getRange (WithApp i _ _) = getRange i getRange (Lam i _ _) = getRange i getRange (AbsurdLam i _) = getRange i getRange (ExtendedLam i _ _ _) = getRange i getRange (Pi i _ _) = getRange i getRange (Fun i _ _) = getRange i getRange (Set i _) = getRange i getRange (Prop i) = getRange i getRange (Let i _ _) = getRange i getRange (Rec i _) = getRange i getRange (RecUpdate i _ _) = getRange i getRange (ETel tel) = getRange tel getRange (ScopedExpr _ e) = getRange e getRange (QuoteGoal _ _ e) = getRange e getRange (QuoteContext i) = getRange i getRange (Quote i) = getRange i getRange (QuoteTerm i) = getRange i getRange (Unquote i) = getRange i getRange (Tactic i _ _ _) = getRange i getRange (DontCare{}) = noRange getRange (PatternSyn x) = getRange x getRange (Macro x) = getRange x instance HasRange Declaration where getRange (Axiom _ i _ _ _ _ ) = getRange i getRange (Field i _ _ ) = getRange i getRange (Mutual i _ ) = getRange i getRange (Section i _ _ _ ) = getRange i getRange (Apply i _ _ _ _) = getRange i getRange (Import i _ _ ) = getRange i getRange (Primitive i _ _ ) = getRange i getRange (Pragma i _ ) = getRange i getRange (Open i _ _ ) = getRange i getRange (ScopedDecl _ d ) = getRange d getRange (FunDef i _ _ _ ) = getRange i getRange (DataSig i _ _ _ ) = getRange i getRange (DataDef i _ _ _ ) = getRange i getRange (RecSig i _ _ _ ) = getRange i getRange (RecDef i _ _ _ _ _ _ _) = getRange i getRange (PatternSynDef x _ _ ) = getRange x getRange (UnquoteDecl _ i _ _) = getRange i getRange (UnquoteDef i _ _) = getRange i instance HasRange (Pattern' e) where getRange (VarP x) = getRange x getRange (ConP i _ _) = getRange i getRange (ProjP i _ _) = getRange i getRange (DefP i _ _) = getRange i getRange (WildP i) = getRange i getRange (AsP i _ _) = getRange i getRange (DotP i _ _) = getRange i getRange (AbsurdP i) = getRange i getRange (LitP l) = getRange l getRange (PatternSynP i _ _) = getRange i getRange (RecP i _) = getRange i instance HasRange SpineLHS where getRange (SpineLHS i _ _ _) = getRange i instance HasRange LHS where getRange (LHS i _ _) = getRange i instance HasRange (LHSCore' e) where getRange (LHSHead f ps) = fuseRange f ps getRange (LHSProj d lhscore ps) = d `fuseRange` lhscore `fuseRange` ps instance HasRange a => HasRange (Clause' a) where getRange (Clause lhs _ _ rhs ds catchall) = getRange (lhs, rhs, ds) instance HasRange RHS where getRange AbsurdRHS = noRange getRange (RHS e _) = getRange e getRange (WithRHS _ e cs) = fuseRange e cs getRange (RewriteRHS xes rhs wh) = getRange (map snd xes, rhs, wh) instance HasRange LetBinding where getRange (LetBind i _ _ _ _ ) = getRange i getRange (LetPatBind i _ _ ) = getRange i getRange (LetApply i _ _ _ _ ) = getRange i getRange (LetOpen i _ _ ) = getRange i getRange (LetDeclaredVariable x) = getRange x -- setRange for patterns applies the range to the outermost pattern constructor instance SetRange (Pattern' a) where setRange r (VarP x) = VarP (setRange r x) setRange r (ConP i ns as) = ConP (setRange r i) ns as setRange r (ProjP _ o ns) = ProjP (PatRange r) o ns setRange r (DefP _ ns as) = DefP (PatRange r) ns as -- (setRange r n) as setRange r (WildP _) = WildP (PatRange r) setRange r (AsP _ n p) = AsP (PatRange r) (setRange r n) p setRange r (DotP _ o e) = DotP (PatRange r) o e setRange r (AbsurdP _) = AbsurdP (PatRange r) setRange r (LitP l) = LitP (setRange r l) setRange r (PatternSynP _ n as) = PatternSynP (PatRange r) (setRange r n) as setRange r (RecP i as) = RecP (PatRange r) as instance KillRange LamBinding where killRange (DomainFree info x) = killRange1 (DomainFree info) x killRange (DomainFull b) = killRange1 DomainFull b instance KillRange TypedBindings where killRange (TypedBindings r b) = TypedBindings (killRange r) (killRange b) instance KillRange TypedBinding where killRange (TBind r xs e) = killRange3 TBind r xs e killRange (TLet r lbs) = killRange2 TLet r lbs instance KillRange Expr where killRange (Var x) = killRange1 Var x killRange (Def x) = killRange1 Def x killRange (Proj o x) = killRange1 (Proj o) x killRange (Con x) = killRange1 Con x killRange (Lit l) = killRange1 Lit l killRange (QuestionMark i ii) = killRange2 QuestionMark i ii killRange (Underscore i) = killRange1 Underscore i killRange (Dot i e) = killRange2 Dot i e killRange (App i e1 e2) = killRange3 App i e1 e2 killRange (WithApp i e es) = killRange3 WithApp i e es killRange (Lam i b e) = killRange3 Lam i b e killRange (AbsurdLam i h) = killRange2 AbsurdLam i h killRange (ExtendedLam i n d ps) = killRange4 ExtendedLam i n d ps killRange (Pi i a b) = killRange3 Pi i a b killRange (Fun i a b) = killRange3 Fun i a b killRange (Set i n) = killRange2 Set i n killRange (Prop i) = killRange1 Prop i killRange (Let i ds e) = killRange3 Let i ds e killRange (Rec i fs) = killRange2 Rec i fs killRange (RecUpdate i e fs) = killRange3 RecUpdate i e fs killRange (ETel tel) = killRange1 ETel tel killRange (ScopedExpr s e) = killRange1 (ScopedExpr s) e killRange (QuoteGoal i x e) = killRange3 QuoteGoal i x e killRange (QuoteContext i) = killRange1 QuoteContext i killRange (Quote i) = killRange1 Quote i killRange (QuoteTerm i) = killRange1 QuoteTerm i killRange (Unquote i) = killRange1 Unquote i killRange (Tactic i e xs ys) = killRange4 Tactic i e xs ys killRange (DontCare e) = killRange1 DontCare e killRange (PatternSyn x) = killRange1 PatternSyn x killRange (Macro x) = killRange1 Macro x instance KillRange Declaration where killRange (Axiom p i a b c d ) = killRange4 (\i a c d -> Axiom p i a b c d) i a c d killRange (Field i a b ) = killRange3 Field i a b killRange (Mutual i a ) = killRange2 Mutual i a killRange (Section i a b c ) = killRange4 Section i a b c killRange (Apply i a b c d ) = killRange5 Apply i a b c d killRange (Import i a b ) = killRange3 Import i a b killRange (Primitive i a b ) = killRange3 Primitive i a b killRange (Pragma i a ) = Pragma (killRange i) a killRange (Open i x dir ) = killRange3 Open i x dir killRange (ScopedDecl a d ) = killRange1 (ScopedDecl a) d killRange (FunDef i a b c ) = killRange4 FunDef i a b c killRange (DataSig i a b c ) = killRange4 DataSig i a b c killRange (DataDef i a b c ) = killRange4 DataDef i a b c killRange (RecSig i a b c ) = killRange4 RecSig i a b c killRange (RecDef i a b c d e f g ) = killRange8 RecDef i a b c d e f g killRange (PatternSynDef x xs p ) = killRange3 PatternSynDef x xs p killRange (UnquoteDecl mi i x e ) = killRange4 UnquoteDecl mi i x e killRange (UnquoteDef i x e ) = killRange3 UnquoteDef i x e instance KillRange ModuleApplication where killRange (SectionApp a b c ) = killRange3 SectionApp a b c killRange (RecordModuleIFS a ) = killRange1 RecordModuleIFS a instance KillRange ScopeCopyInfo where killRange (ScopeCopyInfo a b) = killRange2 ScopeCopyInfo a b instance KillRange e => KillRange (Pattern' e) where killRange (VarP x) = killRange1 VarP x killRange (ConP i a b) = killRange3 ConP i a b killRange (ProjP i o a) = killRange3 ProjP i o a killRange (DefP i a b) = killRange3 DefP i a b killRange (WildP i) = killRange1 WildP i killRange (AsP i a b) = killRange3 AsP i a b killRange (DotP i o a) = killRange3 DotP i o a killRange (AbsurdP i) = killRange1 AbsurdP i killRange (LitP l) = killRange1 LitP l killRange (PatternSynP i a p) = killRange3 PatternSynP i a p killRange (RecP i as) = killRange2 RecP i as instance KillRange SpineLHS where killRange (SpineLHS i a b c) = killRange4 SpineLHS i a b c instance KillRange LHS where killRange (LHS i a b) = killRange3 LHS i a b instance KillRange e => KillRange (LHSCore' e) where killRange (LHSHead a b) = killRange2 LHSHead a b killRange (LHSProj a b c) = killRange3 LHSProj a b c instance KillRange a => KillRange (Clause' a) where killRange (Clause lhs dots sdots rhs ds catchall) = killRange6 Clause lhs dots sdots rhs ds catchall instance KillRange NamedDotPattern where killRange (NamedDot a b c) = killRange3 NamedDot a b c instance KillRange StrippedDotPattern where killRange (StrippedDot a b c) = killRange3 StrippedDot a b c instance KillRange RHS where killRange AbsurdRHS = AbsurdRHS killRange (RHS e c) = killRange2 RHS e c killRange (WithRHS q e cs) = killRange3 WithRHS q e cs killRange (RewriteRHS xes rhs wh) = killRange3 RewriteRHS xes rhs wh instance KillRange LetBinding where killRange (LetBind i info a b c) = killRange5 LetBind i info a b c killRange (LetPatBind i a b ) = killRange3 LetPatBind i a b killRange (LetApply i a b c d ) = killRange5 LetApply i a b c d killRange (LetOpen i x dir ) = killRange3 LetOpen i x dir killRange (LetDeclaredVariable x) = killRange1 LetDeclaredVariable x -- See Agda.Utils.GeniPlate: -- Does not descend into ScopeInfo and renaming maps, for instance. instanceUniverseBiT' [] [t| (Declaration, QName) |] instanceUniverseBiT' [] [t| (Declaration, AmbiguousQName) |] instanceUniverseBiT' [] [t| (Declaration, Expr) |] instanceUniverseBiT' [] [t| (Declaration, LetBinding) |] instanceUniverseBiT' [] [t| (Declaration, LamBinding) |] instanceUniverseBiT' [] [t| (Declaration, TypedBinding) |] instanceUniverseBiT' [] [t| (Declaration, Pattern) |] instanceUniverseBiT' [] [t| (Declaration, Pattern' Void) |] instanceUniverseBiT' [] [t| (Declaration, Declaration) |] instanceUniverseBiT' [] [t| (Declaration, ModuleName) |] instanceUniverseBiT' [] [t| (Declaration, ModuleInfo) |] instanceUniverseBiT' [] [t| (Declaration, RString) |] -- RString is not quite what you want but we put names on lots of things... ------------------------------------------------------------------------ -- Queries ------------------------------------------------------------------------ -- | Extracts all the names which are declared in a 'Declaration'. -- This does not include open public or let expressions, but it does -- include local modules, where clauses and the names of extended -- lambdas. class AllNames a where allNames :: a -> Seq QName instance AllNames a => AllNames [a] where allNames = Fold.foldMap allNames instance AllNames a => AllNames (Maybe a) where allNames = Fold.foldMap allNames instance AllNames a => AllNames (Arg a) where allNames = Fold.foldMap allNames instance AllNames a => AllNames (Named name a) where allNames = Fold.foldMap allNames instance (AllNames a, AllNames b) => AllNames (a,b) where allNames (a,b) = allNames a >< allNames b instance AllNames QName where allNames q = Seq.singleton q instance AllNames Declaration where allNames (Axiom _ _ _ _ q _) = Seq.singleton q allNames (Field _ q _) = Seq.singleton q allNames (Primitive _ q _) = Seq.singleton q allNames (Mutual _ defs) = allNames defs allNames (DataSig _ q _ _) = Seq.singleton q allNames (DataDef _ q _ decls) = q <| allNames decls allNames (RecSig _ q _ _) = Seq.singleton q allNames (RecDef _ q _ _ c _ _ decls) = q <| allNames c >< allNames decls allNames (PatternSynDef q _ _) = Seq.singleton q allNames (UnquoteDecl _ _ qs _) = Seq.fromList qs allNames (UnquoteDef _ qs _) = Seq.fromList qs allNames (FunDef _ q _ cls) = q <| allNames cls allNames (Section _ _ _ decls) = allNames decls allNames Apply{} = Seq.empty allNames Import{} = Seq.empty allNames Pragma{} = Seq.empty allNames Open{} = Seq.empty allNames (ScopedDecl _ decls) = allNames decls instance AllNames Clause where allNames cl = allNames (clauseRHS cl, clauseWhereDecls cl) instance AllNames RHS where allNames (RHS e _) = allNames e allNames AbsurdRHS{} = Seq.empty allNames (WithRHS q _ cls) = q <| allNames cls allNames (RewriteRHS qes rhs cls) = Seq.fromList (map fst qes) >< allNames rhs >< allNames cls instance AllNames Expr where allNames Var{} = Seq.empty allNames Def{} = Seq.empty allNames Proj{} = Seq.empty allNames Con{} = Seq.empty allNames Lit{} = Seq.empty allNames QuestionMark{} = Seq.empty allNames Underscore{} = Seq.empty allNames (Dot _ e) = allNames e allNames (App _ e1 e2) = allNames e1 >< allNames e2 allNames (WithApp _ e es) = allNames e >< allNames es allNames (Lam _ b e) = allNames b >< allNames e allNames AbsurdLam{} = Seq.empty allNames (ExtendedLam _ _ q cls) = q <| allNames cls allNames (Pi _ tel e) = allNames tel >< allNames e allNames (Fun _ e1 e2) = allNames e1 >< allNames e2 allNames Set{} = Seq.empty allNames Prop{} = Seq.empty allNames (Let _ lbs e) = allNames lbs >< allNames e allNames ETel{} = __IMPOSSIBLE__ allNames (Rec _ fields) = allNames [ a ^. exprFieldA | Left a <- fields ] allNames (RecUpdate _ e fs) = allNames e >< allNames (map (view exprFieldA) fs) allNames (ScopedExpr _ e) = allNames e allNames (QuoteGoal _ _ e) = allNames e allNames (QuoteContext _) = Seq.empty allNames Quote{} = Seq.empty allNames QuoteTerm{} = Seq.empty allNames Unquote{} = Seq.empty allNames (Tactic _ e xs ys) = allNames e >< allNames xs >< allNames ys allNames DontCare{} = Seq.empty allNames PatternSyn{} = Seq.empty allNames Macro{} = Seq.empty instance AllNames LamBinding where allNames DomainFree{} = Seq.empty allNames (DomainFull binds) = allNames binds instance AllNames TypedBindings where allNames (TypedBindings _ bs) = allNames bs instance AllNames TypedBinding where allNames (TBind _ _ e) = allNames e allNames (TLet _ lbs) = allNames lbs instance AllNames LetBinding where allNames (LetBind _ _ _ e1 e2) = allNames e1 >< allNames e2 allNames (LetPatBind _ _ e) = allNames e allNames (LetApply _ _ app _ _) = allNames app allNames LetOpen{} = Seq.empty allNames (LetDeclaredVariable _) = Seq.empty instance AllNames ModuleApplication where allNames (SectionApp bindss _ es) = allNames bindss >< allNames es allNames RecordModuleIFS{} = Seq.empty -- | The name defined by the given axiom. -- -- Precondition: The declaration has to be a (scoped) 'Axiom'. axiomName :: Declaration -> QName axiomName (Axiom _ _ _ _ q _) = q axiomName (ScopedDecl _ (d:_)) = axiomName d axiomName _ = __IMPOSSIBLE__ -- | Are we in an abstract block? -- -- In that case some definition is abstract. class AnyAbstract a where anyAbstract :: a -> Bool instance AnyAbstract a => AnyAbstract [a] where anyAbstract = Fold.any anyAbstract instance AnyAbstract Declaration where anyAbstract (Axiom _ i _ _ _ _) = defAbstract i == AbstractDef anyAbstract (Field i _ _) = defAbstract i == AbstractDef anyAbstract (Mutual _ ds) = anyAbstract ds anyAbstract (ScopedDecl _ ds) = anyAbstract ds anyAbstract (Section _ _ _ ds) = anyAbstract ds anyAbstract (FunDef i _ _ _) = defAbstract i == AbstractDef anyAbstract (DataDef i _ _ _) = defAbstract i == AbstractDef anyAbstract (RecDef i _ _ _ _ _ _ _) = defAbstract i == AbstractDef anyAbstract (DataSig i _ _ _) = defAbstract i == AbstractDef anyAbstract (RecSig i _ _ _) = defAbstract i == AbstractDef anyAbstract _ = __IMPOSSIBLE__ -- | Turn an 'AbstractName' to an expression. nameExpr :: AbstractName -> Expr nameExpr d = mk (anameKind d) $ anameName d where mk DefName x = Def x mk FldName x = Proj ProjSystem $ AmbQ [x] mk ConName x = Con $ AmbQ [x] mk PatternSynName x = PatternSyn x mk MacroName x = Macro x mk QuotableName x = App i (Quote i) (defaultNamedArg $ Def x) where i = ExprRange (getRange x) app :: Expr -> [NamedArg Expr] -> Expr app = foldl (App (ExprRange noRange)) mkLet :: ExprInfo -> [LetBinding] -> Expr -> Expr mkLet i [] e = e mkLet i ds e = Let i ds e patternToExpr :: Pattern -> Expr patternToExpr (VarP x) = Var x patternToExpr (ConP _ c ps) = Con c `app` map (fmap (fmap patternToExpr)) ps patternToExpr (ProjP _ o ds) = Proj o ds patternToExpr (DefP _ (AmbQ [f]) ps) = Def f `app` map (fmap (fmap patternToExpr)) ps patternToExpr (DefP _ (AmbQ _) ps) = __IMPOSSIBLE__ patternToExpr (WildP _) = Underscore emptyMetaInfo patternToExpr (AsP _ _ p) = patternToExpr p patternToExpr (DotP _ _ e) = e patternToExpr (AbsurdP _) = Underscore emptyMetaInfo -- TODO: could this happen? patternToExpr (LitP l) = Lit l patternToExpr (PatternSynP _ _ _) = __IMPOSSIBLE__ patternToExpr (RecP _ as) = Rec exprNoRange $ map (Left . fmap patternToExpr) as type PatternSynDefn = ([Arg Name], Pattern' Void) type PatternSynDefns = Map QName PatternSynDefn lambdaLiftExpr :: [Name] -> Expr -> Expr lambdaLiftExpr [] e = e lambdaLiftExpr (n:ns) e = Lam defaultLamInfo_ (DomainFree defaultArgInfo n) $ lambdaLiftExpr ns e substPattern :: [(Name, Pattern)] -> Pattern -> Pattern substPattern s p = case p of VarP z -> fromMaybe p (lookup z s) ConP i q ps -> ConP i q (map (fmap (fmap (substPattern s))) ps) RecP i ps -> RecP i (map (fmap (substPattern s)) ps) ProjP{} -> p WildP i -> p DotP i o e -> DotP i o (substExpr (map (fmap patternToExpr) s) e) AbsurdP i -> p LitP l -> p DefP{} -> p -- destructor pattern AsP i x p -> AsP i x (substPattern s p) -- Note: cannot substitute into as-variable PatternSynP{} -> __IMPOSSIBLE__ -- pattern synonyms (already gone) class SubstExpr a where substExpr :: [(Name, Expr)] -> a -> a instance SubstExpr a => SubstExpr [a] where substExpr = fmap . substExpr instance SubstExpr a => SubstExpr (Arg a) where substExpr = fmap . substExpr instance SubstExpr a => SubstExpr (Named name a) where substExpr = fmap . substExpr instance (SubstExpr a, SubstExpr b) => SubstExpr (a, b) where substExpr s (x, y) = (substExpr s x, substExpr s y) instance (SubstExpr a, SubstExpr b) => SubstExpr (Either a b) where substExpr s (Left x) = Left (substExpr s x) substExpr s (Right y) = Right (substExpr s y) instance SubstExpr C.Name where substExpr _ = id instance SubstExpr ModuleName where substExpr _ = id instance SubstExpr Assign where substExpr s (FieldAssignment n x) = FieldAssignment n (substExpr s x) instance SubstExpr Expr where substExpr s e = case e of Var n -> fromMaybe e (lookup n s) Def _ -> e Proj{} -> e Con _ -> e Lit _ -> e QuestionMark{} -> e Underscore _ -> e Dot i e -> Dot i (substExpr s e) App i e e' -> App i (substExpr s e) (substExpr s e') WithApp i e es -> WithApp i (substExpr s e) (substExpr s es) Lam i lb e -> Lam i lb (substExpr s e) AbsurdLam i h -> e ExtendedLam i di n cs -> __IMPOSSIBLE__ -- Maybe later... Pi i t e -> Pi i (substExpr s t) (substExpr s e) Fun i ae e -> Fun i (substExpr s ae) (substExpr s e) Set i n -> e Prop i -> e Let i ls e -> Let i (substExpr s ls) (substExpr s e) ETel t -> e Rec i nes -> Rec i (substExpr s nes) RecUpdate i e nes -> RecUpdate i (substExpr s e) (substExpr s nes) -- XXX: Do we need to do more with ScopedExprs? ScopedExpr si e -> ScopedExpr si (substExpr s e) QuoteGoal i n e -> QuoteGoal i n (substExpr s e) QuoteContext i -> e Quote i -> e QuoteTerm i -> e Unquote i -> e Tactic i e xs ys -> Tactic i (substExpr s e) (substExpr s xs) (substExpr s ys) DontCare e -> DontCare (substExpr s e) PatternSyn{} -> e Macro{} -> e instance SubstExpr LetBinding where substExpr s lb = case lb of LetBind i r n e e' -> LetBind i r n (substExpr s e) (substExpr s e') LetPatBind i p e -> LetPatBind i p (substExpr s e) -- Andreas, 2012-06-04: what about the pattern p _ -> lb -- Nicolas, 2013-11-11: what about "LetApply" there is experessions in there instance SubstExpr TypedBindings where substExpr s (TypedBindings r atb) = TypedBindings r (substExpr s atb) instance SubstExpr TypedBinding where substExpr s tb = case tb of TBind r ns e -> TBind r ns $ substExpr s e TLet r lbs -> TLet r $ substExpr s lbs -- TODO: more informative failure insertImplicitPatSynArgs :: HasRange a => (Range -> a) -> Range -> [Arg Name] -> [NamedArg a] -> Maybe ([(Name, a)], [Arg Name]) insertImplicitPatSynArgs wild r ns as = matchArgs r ns as where matchNextArg r n as@(~(a : as')) | matchNext n as = return (namedArg a, as') | visible n = Nothing | otherwise = return (wild r, as) matchNext _ [] = False matchNext n (a:as) = sameHiding n a && matchName where x = unranged $ C.nameToRawName $ nameConcrete $ unArg n matchName = maybe True (== x) (nameOf $ unArg a) matchArgs r [] [] = return ([], []) matchArgs r [] as = Nothing matchArgs r (n:ns) [] | visible n = return ([], n : ns) -- under-applied matchArgs r (n:ns) as = do (p, as) <- matchNextArg r n as first ((unArg n, p) :) <$> matchArgs (getRange p) ns as Agda-2.5.3/src/full/Agda/Syntax/Internal/0000755000000000000000000000000013154613124016172 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Syntax/Internal/Names.hs0000644000000000000000000001407313154613124017576 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE TypeFamilies #-} -- | Extract all names from things. module Agda.Syntax.Internal.Names where import Data.Foldable import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import Control.Applicative import Agda.Syntax.Common import Agda.Syntax.Literal import Agda.Syntax.Internal import qualified Agda.Syntax.Concrete as C import qualified Agda.Syntax.Abstract as A import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.CompiledClause import Agda.Utils.Functor import Agda.Utils.Impossible #include "undefined.h" class NamesIn a where namesIn :: a -> Set QName default namesIn :: (Foldable f, NamesIn b, f b ~ a) => a -> Set QName namesIn = foldMap namesIn instance NamesIn a => NamesIn (Maybe a) where instance NamesIn a => NamesIn [a] where instance NamesIn a => NamesIn (Arg a) where instance NamesIn a => NamesIn (Dom a) where instance NamesIn a => NamesIn (Named n a) where instance NamesIn a => NamesIn (Abs a) where instance NamesIn a => NamesIn (WithArity a) where instance NamesIn a => NamesIn (Tele a) where instance NamesIn a => NamesIn (C.FieldAssignment' a) where instance (NamesIn a, NamesIn b) => NamesIn (a, b) where namesIn (x, y) = Set.union (namesIn x) (namesIn y) instance (NamesIn a, NamesIn b, NamesIn c) => NamesIn (a, b, c) where namesIn (x, y, z) = namesIn (x, (y, z)) -- Andreas, 2017-07-27 -- In the following clauses, the choice of fields is not obvious -- to the reader. Please comment on the choices. -- -- Also, this would be more robust if these were constructor-style -- matches instead of record-style matches. -- If someone adds a field containing names, this would go unnoticed. instance NamesIn Definition where namesIn def = namesIn (defType def, theDef def) instance NamesIn Defn where namesIn def = case def of Axiom -> Set.empty -- Andreas 2017-07-27, Q: which names can be in @cc@ which are not already in @cl@? Function { funClauses = cl, funCompiled = cc } -> namesIn (cl, cc) Datatype { dataClause = cl, dataCons = cs, dataSort = s } -> namesIn (cl, cs, s) Record { recClause = cl, recConHead = c, recFields = fs } -> namesIn (cl, c, fs) -- Don't need recTel since those will be reachable from the constructor Constructor { conSrcCon = c, conData = d } -> namesIn (c, d) Primitive { primClauses = cl, primCompiled = cc } -> namesIn (cl, cc) AbstractDefn{} -> __IMPOSSIBLE__ instance NamesIn Clause where namesIn Clause{ clauseTel = tel, namedClausePats = ps, clauseBody = b, clauseType = t } = namesIn ((tel, ps, b), t) instance NamesIn CompiledClauses where namesIn (Case _ c) = namesIn c namesIn (Done _ v) = namesIn v namesIn Fail = Set.empty -- Andreas, 2017-07-27 -- Why ignoring the litBranches? instance NamesIn a => NamesIn (Case a) where namesIn Branches{ conBranches = bs, catchAllBranch = c } = namesIn (Map.toList bs, c) instance NamesIn (Pattern' a) where namesIn p = case p of VarP{} -> Set.empty LitP l -> namesIn l DotP v -> namesIn v AbsurdP p -> namesIn p ConP c _ args -> namesIn (c, args) ProjP _ f -> namesIn f instance NamesIn a => NamesIn (Type' a) where namesIn (El s t) = namesIn (s, t) instance NamesIn Sort where namesIn s = case s of Type l -> namesIn l Prop -> Set.empty Inf -> Set.empty SizeUniv -> Set.empty DLub a b -> namesIn (a, b) instance NamesIn Term where namesIn v = case ignoreSharing v of Var _ args -> namesIn args Lam _ b -> namesIn b Lit l -> namesIn l Def f args -> namesIn (f, args) Con c _ args -> namesIn (c, args) Pi a b -> namesIn (a, b) Sort s -> namesIn s Level l -> namesIn l MetaV _ args -> namesIn args DontCare v -> namesIn v Shared{} -> __IMPOSSIBLE__ instance NamesIn Level where namesIn (Max ls) = namesIn ls instance NamesIn PlusLevel where namesIn ClosedLevel{} = Set.empty namesIn (Plus _ l) = namesIn l instance NamesIn LevelAtom where namesIn l = case l of MetaLevel _ args -> namesIn args BlockedLevel _ v -> namesIn v NeutralLevel _ v -> namesIn v UnreducedLevel v -> namesIn v -- For QName literals! instance NamesIn Literal where namesIn l = case l of LitNat{} -> Set.empty LitString{} -> Set.empty LitChar{} -> Set.empty LitFloat{} -> Set.empty LitQName _ x -> namesIn x LitMeta{} -> Set.empty instance NamesIn a => NamesIn (Elim' a) where namesIn (Apply arg) = namesIn arg namesIn (Proj _ f) = namesIn f instance NamesIn QName where namesIn x = Set.singleton x -- interesting case instance NamesIn ConHead where namesIn h = namesIn (conName h) instance NamesIn a => NamesIn (Open a) where instance NamesIn a => NamesIn (Local a) where instance NamesIn DisplayForm where namesIn (Display _ ps v) = namesIn (ps, v) instance NamesIn DisplayTerm where namesIn v = case v of DWithApp v us es -> namesIn (v, us, es) DCon c _ vs -> namesIn (c, vs) DDef f es -> namesIn (f, es) DDot v -> namesIn v DTerm v -> namesIn v -- Pattern synonym stuff -- newtype PSyn = PSyn A.PatternSynDefn instance NamesIn PSyn where namesIn (PSyn (_args, p)) = namesIn p instance NamesIn (A.Pattern' a) where namesIn p = case p of A.VarP{} -> Set.empty A.ConP _ c args -> namesIn (c, args) A.ProjP _ _ d -> namesIn d A.DefP _ f args -> namesIn (f, args) A.WildP{} -> Set.empty A.AsP _ _ p -> namesIn p A.AbsurdP{} -> Set.empty A.LitP l -> namesIn l A.PatternSynP _ c args -> namesIn (c, args) A.RecP _ fs -> namesIn fs A.DotP{} -> __IMPOSSIBLE__ -- Dot patterns are not allowed in pattern synonyms instance NamesIn AmbiguousQName where namesIn (AmbQ cs) = namesIn cs Agda-2.5.3/src/full/Agda/Syntax/Internal/SanityCheck.hs0000644000000000000000000000556713154613124020750 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Sanity checking for internal syntax. Mostly checking variable scoping. module Agda.Syntax.Internal.SanityCheck where import Control.Monad import qualified Data.IntSet as Set import Text.PrettyPrint (empty) import Agda.Syntax.Internal import Agda.TypeChecking.Free import Agda.TypeChecking.Monad import Agda.TypeChecking.Substitute import Agda.Utils.Pretty import Agda.Utils.Size import Agda.Utils.Impossible #include "undefined.h" sanityCheckVars :: (Pretty a, Free a) => Telescope -> a -> TCM () sanityCheckVars tel v = case filter bad (Set.toList $ allFreeVars v) of [] -> return () xs -> do reportSDoc "impossible" 1 . return $ sep [ hang (text "Sanity check failed for") 2 (hang (pretty tel <+> text "|-") 2 (pretty v)) , text $ "out of scope: " ++ show xs ] __IMPOSSIBLE__ where n = size tel bad x = x < 0 || x >= n -- | Check that @Γ ⊢ ρ : Δ@. sanityCheckSubst :: (Pretty a, Free a) => Telescope -> Substitution' a -> Telescope -> TCM () sanityCheckSubst gamma rho delta = go gamma rho delta where go gamma rho delta = case rho of IdS -> unless (size gamma == size delta) $ err $ text "idS:" <+> hang (pretty gamma <+> text "/=") 2 (pretty delta) EmptyS _ -> unless (size delta == 0) $ err $ text "emptyS:" <+> pretty delta <+> text "is not empty" v :# rho -> do unless (size delta > 0) $ err $ text "consS: empty target" sanityCheckVars gamma v sanityCheckSubst gamma rho (dropLast delta) Strengthen _ rho -> do unless (size delta > 0) $ err $ text "strS: empty target" sanityCheckSubst gamma rho (dropLast delta) Wk n rho -> do unless (size gamma >= n) $ err $ text "wkS:" <+> sep [ text "|" <> pretty gamma <> text "|" , text $ "< " ++ show n ] sanityCheckSubst (dropLastN n gamma) rho delta Lift n rho -> do unless (size gamma >= n) $ err $ text "liftS: source" <+> sep [ text "|" <> pretty gamma <> text "|" , text $ "< " ++ show n ] unless (size delta >= n) $ err $ text "liftS: target" <+> sep [ text "|" <> pretty delta <> text "|" , text $ "< " ++ show n ] sanityCheckSubst (dropLastN n gamma) rho (dropLastN n delta) dropLast = telFromList . init . telToList dropLastN n = telFromList . reverse . drop n . reverse . telToList err reason = do reportSDoc "impossible" 1 . return $ sep [ hang (text "Sanity check failed for") 2 $ hang (pretty gamma <+> text "|-") 2 $ hang (pretty rho <+> text ":") 2 $ pretty delta , reason ] __IMPOSSIBLE__ Agda-2.5.3/src/full/Agda/Syntax/Internal/Generic.hs0000644000000000000000000001222413154613124020103 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE TypeFamilies #-} -- | Tree traversal for internal syntax. module Agda.Syntax.Internal.Generic where import Control.Applicative import Data.Traversable import Data.Monoid import Data.Foldable import Agda.Syntax.Common import Agda.Syntax.Internal -- | Generic term traversal. -- -- Note: ignores sorts in terms! -- (Does not traverse into or collect from them.) class TermLike a where -- | Generic traversal with post-traversal action. -- Ignores sorts. traverseTermM :: (Monad m #if __GLASGOW_HASKELL__ <= 708 , Applicative m #endif ) => (Term -> m Term) -> a -> m a default traverseTermM :: (Monad m, Traversable f, TermLike b, f b ~ a #if __GLASGOW_HASKELL__ <= 708 , Applicative m #endif ) => (Term -> m Term) -> a -> m a traverseTermM = traverse . traverseTermM -- | Generic fold, ignoring sorts. foldTerm :: Monoid m => (Term -> m) -> a -> m default foldTerm :: (Monoid m, Foldable f, TermLike b, f b ~ a) => (Term -> m) -> a -> m foldTerm = foldMap . foldTerm -- Constants instance TermLike Bool where traverseTermM _ = pure foldTerm _ = mempty instance TermLike Int where traverseTermM _ = pure foldTerm _ = mempty instance TermLike Integer where traverseTermM _ = pure foldTerm _ = mempty instance TermLike Char where traverseTermM _ = pure foldTerm _ = mempty instance TermLike QName where traverseTermM _ = pure foldTerm _ = mempty -- Functors instance TermLike a => TermLike (Elim' a) where instance TermLike a => TermLike (Arg a) where instance TermLike a => TermLike (Dom a) where instance TermLike a => TermLike [a] where instance TermLike a => TermLike (Maybe a) where instance TermLike a => TermLike (Abs a) where instance TermLike a => TermLike (Ptr a) where instance TermLike a => TermLike (Blocked a) where -- Tuples instance (TermLike a, TermLike b) => TermLike (a, b) where traverseTermM f (x, y) = (,) <$> traverseTermM f x <*> traverseTermM f y foldTerm f (x, y) = foldTerm f x `mappend` foldTerm f y instance (TermLike a, TermLike b, TermLike c) => TermLike (a, b, c) where traverseTermM f (x, y, z) = (,,) <$> traverseTermM f x <*> traverseTermM f y <*> traverseTermM f z foldTerm f (x, y, z) = mconcat [foldTerm f x, foldTerm f y, foldTerm f z] instance (TermLike a, TermLike b, TermLike c, TermLike d) => TermLike (a, b, c, d) where traverseTermM f (x, y, z, u) = (,,,) <$> traverseTermM f x <*> traverseTermM f y <*> traverseTermM f z <*> traverseTermM f u foldTerm f (x, y, z, u) = mconcat [foldTerm f x, foldTerm f y, foldTerm f z, foldTerm f u] -- Real terms instance TermLike Term where traverseTermM f t = case t of Var i xs -> f =<< Var i <$> traverseTermM f xs Def c xs -> f =<< Def c <$> traverseTermM f xs Con c ci xs -> f =<< Con c ci <$> traverseTermM f xs Lam h b -> f =<< Lam h <$> traverseTermM f b Pi a b -> f =<< uncurry Pi <$> traverseTermM f (a, b) MetaV m xs -> f =<< MetaV m <$> traverseTermM f xs Level l -> f =<< Level <$> traverseTermM f l Lit _ -> f t Sort _ -> f t DontCare mv -> f =<< DontCare <$> traverseTermM f mv Shared p -> f =<< Shared <$> traverseTermM f p foldTerm f t = f t `mappend` case t of Var i xs -> foldTerm f xs Def c xs -> foldTerm f xs Con c ci xs -> foldTerm f xs Lam h b -> foldTerm f b Pi a b -> foldTerm f (a, b) MetaV m xs -> foldTerm f xs Level l -> foldTerm f l Lit _ -> mempty Sort _ -> mempty DontCare mv -> foldTerm f mv Shared p -> foldTerm f p instance TermLike Level where traverseTermM f (Max as) = Max <$> traverseTermM f as foldTerm f (Max as) = foldTerm f as instance TermLike PlusLevel where traverseTermM f l = case l of ClosedLevel{} -> return l Plus n l -> Plus n <$> traverseTermM f l foldTerm f ClosedLevel{} = mempty foldTerm f (Plus _ l) = foldTerm f l instance TermLike LevelAtom where traverseTermM f l = case l of MetaLevel m vs -> MetaLevel m <$> traverseTermM f vs NeutralLevel r v -> NeutralLevel r <$> traverseTermM f v BlockedLevel m v -> BlockedLevel m <$> traverseTermM f v UnreducedLevel v -> UnreducedLevel <$> traverseTermM f v foldTerm f l = case l of MetaLevel m vs -> foldTerm f vs NeutralLevel _ v -> foldTerm f v BlockedLevel _ v -> foldTerm f v UnreducedLevel v -> foldTerm f v instance TermLike Type where traverseTermM f (El s t) = El s <$> traverseTermM f t foldTerm f (El s t) = foldTerm f t instance TermLike EqualityView where traverseTermM f v = case v of OtherType t -> OtherType <$> traverseTermM f t EqualityType s eq l t a b -> EqualityType s eq <$> traverse (traverseTermM f) l <*> traverseTermM f t <*> traverseTermM f a <*> traverseTermM f b foldTerm f v = case v of OtherType t -> foldTerm f t EqualityType s eq l t a b -> foldTerm f (l ++ [t, a, b]) -- | Put it in a monad to make it possible to do strictly. copyTerm :: (TermLike a, Monad m #if __GLASGOW_HASKELL__ <= 708 , Applicative m #endif ) => a -> m a copyTerm = traverseTermM return Agda-2.5.3/src/full/Agda/Syntax/Internal/Pattern.hs0000644000000000000000000002417613154613124020155 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE TypeFamilies #-} -- because of type equality ~ {-# LANGUAGE UndecidableInstances #-} -- because of func. deps. #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE OverlappingInstances #-} #endif module Agda.Syntax.Internal.Pattern where import Control.Applicative import Control.Monad.State import Data.Maybe import Data.Monoid import qualified Data.List as List import Data.Foldable (Foldable, foldMap) import Data.Traversable (Traversable, traverse) import Agda.Syntax.Common import Agda.Syntax.Abstract (IsProjP(..)) import Agda.Syntax.Internal import qualified Agda.Syntax.Internal as I import Agda.Utils.Empty import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Permutation import Agda.Utils.Size (size) import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- * Tools for clauses -- | Translate the clause patterns to terms with free variables bound by the -- clause telescope. -- -- Precondition: no projection patterns. clauseArgs :: Clause -> Args clauseArgs cl = fromMaybe __IMPOSSIBLE__ $ allApplyElims $ clauseElims cl -- | Translate the clause patterns to an elimination spine -- with free variables bound by the clause telescope. clauseElims :: Clause -> Elims clauseElims cl = patternsToElims $ namedClausePats cl -- | Arity of a function, computed from clauses. class FunArity a where funArity :: a -> Int -- | Get the number of initial 'Apply' patterns. #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPABLE #-} IsProjP p => FunArity [p] where #else instance IsProjP p => FunArity [p] where #endif funArity = length . takeWhile (isNothing . isProjP) -- | Get the number of initial 'Apply' patterns in a clause. instance FunArity Clause where funArity = funArity . namedClausePats -- | Get the number of common initial 'Apply' patterns in a list of clauses. #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} FunArity [Clause] where #else instance FunArity [Clause] where #endif funArity [] = 0 funArity cls = minimum $ map funArity cls -- * Tools for patterns -- | Label the pattern variables from left to right -- using one label for each variable pattern and one for each dot pattern. class LabelPatVars a b i | b -> i where labelPatVars :: a -> State [i] b unlabelPatVars :: b -> a -- ^ Intended, but unpractical due to the absence of type-level lambda, is: -- @labelPatVars :: f (Pattern' x) -> State [i] (f (Pattern' (i,x)))@ default labelPatVars :: (Traversable f, LabelPatVars a' b' i, f a' ~ a, f b' ~ b) => a -> State [i] b labelPatVars = traverse labelPatVars default unlabelPatVars :: (Traversable f, LabelPatVars a' b' i, f a' ~ a, f b' ~ b) => b -> a unlabelPatVars = fmap unlabelPatVars instance LabelPatVars a b i => LabelPatVars (Arg a) (Arg b) i where instance LabelPatVars a b i => LabelPatVars (Named x a) (Named x b) i where instance LabelPatVars a b i => LabelPatVars [a] [b] i where instance LabelPatVars Pattern DeBruijnPattern Int where labelPatVars p = case p of VarP x -> do i <- next return $ VarP (DBPatVar x i) DotP t -> DotP t <$ next AbsurdP p -> AbsurdP <$> labelPatVars p ConP c mt ps -> ConP c mt <$> labelPatVars ps LitP l -> return $ LitP l ProjP o q -> return $ ProjP o q where next = do (x:xs) <- get; put xs; return x unlabelPatVars = fmap dbPatVarName -- | Augment pattern variables with their de Bruijn index. {-# SPECIALIZE numberPatVars :: Int -> Permutation -> [NamedArg Pattern] -> [NamedArg DeBruijnPattern] #-} -- -- Example: -- @ -- f : (A : Set) (n : Nat) (v : Vec A n) -> ... -- f A .(suc n) (cons n x xs) -- -- clauseTel = (A : Set) (n : Nat) (x : A) (xs : Vec A n) -- perm = Perm 5 [0,2,3,4] -- invertP __IMPOSSIBLE__ perm = Perm 4 [0,__IMPOSSIBLE__,1,2,3] -- flipP ... = Perm 4 [3,__IMPOSSIBLE__,2,1,0] -- pats = A .(suc 2) (cons n x xs) -- dBpats = 3 .(suc 2) (cons 2 1 0 ) -- @ -- numberPatVars :: LabelPatVars a b Int => Int -> Permutation -> a -> b numberPatVars err perm ps = evalState (labelPatVars ps) $ permPicks $ flipP $ invertP err perm unnumberPatVars :: LabelPatVars a b i => b -> a unnumberPatVars = unlabelPatVars dbPatPerm :: [NamedArg DeBruijnPattern] -> Maybe Permutation dbPatPerm = dbPatPerm' True -- | Computes the permutation from the clause telescope -- to the pattern variables. -- -- Use as @fromMaybe __IMPOSSIBLE__ . dbPatPerm@ to crash -- in a controlled way if a de Bruijn index is out of scope here. -- -- The first argument controls whether dot patterns counts as variables or -- not. dbPatPerm' :: Bool -> [NamedArg DeBruijnPattern] -> Maybe Permutation dbPatPerm' countDots ps = Perm (size ixs) <$> picks where ixs = concatMap (getIndices . namedThing . unArg) ps n = size $ catMaybes ixs picks = forM (downFrom n) $ \ i -> List.findIndex (Just i ==) ixs getIndices :: DeBruijnPattern -> [Maybe Int] getIndices (VarP x) = [Just $ dbPatVarIndex x] getIndices (ConP c _ ps) = concatMap (getIndices . namedThing . unArg) ps getIndices (DotP _) = [Nothing | countDots] getIndices (AbsurdP p) = getIndices p getIndices (LitP _) = [] getIndices ProjP{} = [] -- | Computes the permutation from the clause telescope -- to the pattern variables. -- -- Use as @fromMaybe __IMPOSSIBLE__ . clausePerm@ to crash -- in a controlled way if a de Bruijn index is out of scope here. clausePerm :: Clause -> Maybe Permutation clausePerm = dbPatPerm . namedClausePats -- | Turn a pattern into a term. -- Projection patterns are turned into projection eliminations, -- other patterns into apply elimination. patternToElim :: Arg DeBruijnPattern -> Elim patternToElim (Arg ai (VarP x)) = Apply $ Arg ai $ var $ dbPatVarIndex x patternToElim (Arg ai (ConP c cpi ps)) = Apply $ Arg ai $ Con c ci $ map (argFromElim . patternToElim . fmap namedThing) ps where ci = fromConPatternInfo cpi patternToElim (Arg ai (DotP t) ) = Apply $ Arg ai t patternToElim (Arg ai (AbsurdP p)) = patternToElim $ Arg ai p patternToElim (Arg ai (LitP l) ) = Apply $ Arg ai $ Lit l patternToElim (Arg ai (ProjP o dest)) = Proj o dest patternsToElims :: [NamedArg DeBruijnPattern] -> [Elim] patternsToElims ps = map build ps where build :: NamedArg DeBruijnPattern -> Elim build = patternToElim . fmap namedThing patternToTerm :: DeBruijnPattern -> Term patternToTerm p = case patternToElim (defaultArg p) of Apply x -> unArg x Proj{} -> __IMPOSSIBLE__ class MapNamedArg f where mapNamedArg :: (NamedArg a -> NamedArg b) -> NamedArg (f a) -> NamedArg (f b) -- | Modify the content of @VarP@, and the closest surrounding @NamedArg@. -- -- Note: the @mapNamedArg@ for @Pattern'@ is not expressible simply -- by @fmap@ or @traverse@ etc., since @ConP@ has @NamedArg@ subpatterns, -- which are taken into account by @mapNamedArg@. instance MapNamedArg Pattern' where mapNamedArg f np = case namedArg np of VarP x -> updateNamedArg VarP $ f $ setNamedArg np x AbsurdP p -> updateNamedArg AbsurdP $ mapNamedArg f $ setNamedArg np p DotP t -> setNamedArg np $ DotP t -- just Haskell type conversion LitP l -> setNamedArg np $ LitP l -- ditto ProjP o q -> setNamedArg np $ ProjP o q -- ditto ConP c i ps -> setNamedArg np $ ConP c i $ map (mapNamedArg f) ps -- | Generic pattern traversal. -- -- Pre-applies a pattern modification, recurses, and post-applies another one. class PatternLike a b where -- | Fold pattern. foldrPattern :: Monoid m => (Pattern' a -> m -> m) -- ^ Combine a pattern and the value computed from its subpatterns. -> b -> m default foldrPattern :: (Monoid m, Foldable f, PatternLike a p, f p ~ b) => (Pattern' a -> m -> m) -> b -> m foldrPattern = foldMap . foldrPattern -- | Traverse pattern. traversePatternM :: (Monad m #if __GLASGOW_HASKELL__ <= 708 , Applicative m, Functor m #endif ) => (Pattern' a -> m (Pattern' a)) -- ^ @pre@: Modification before recursion. -> (Pattern' a -> m (Pattern' a)) -- ^ @post@: Modification after recursion. -> b -> m b default traversePatternM :: (Traversable f, PatternLike a p, f p ~ b, Monad m #if __GLASGOW_HASKELL__ <= 708 , Applicative m, Functor m #endif ) => (Pattern' a -> m (Pattern' a)) -> (Pattern' a -> m (Pattern' a)) -> b -> m b traversePatternM pre post = traverse $ traversePatternM pre post -- | Compute from each subpattern a value and collect them all in a monoid. foldPattern :: (PatternLike a b, Monoid m) => (Pattern' a -> m) -> b -> m foldPattern f = foldrPattern $ \ p m -> f p `mappend` m -- | Traverse pattern(s) with a modification before the recursive descent. preTraversePatternM :: (PatternLike a b, Monad m #if __GLASGOW_HASKELL__ <= 708 , Applicative m, Functor m #endif ) => (Pattern' a -> m (Pattern' a)) -- ^ @pre@: Modification before recursion. -> b -> m b preTraversePatternM pre = traversePatternM pre return -- | Traverse pattern(s) with a modification after the recursive descent. postTraversePatternM :: (PatternLike a b, Monad m #if __GLASGOW_HASKELL__ <= 708 , Applicative m, Functor m #endif ) => (Pattern' a -> m (Pattern' a)) -- ^ @post@: Modification after recursion. -> b -> m b postTraversePatternM = traversePatternM return -- This is where the action is: instance PatternLike a (Pattern' a) where foldrPattern f p = f p $ case p of ConP _ _ ps -> foldrPattern f ps VarP _ -> mempty AbsurdP _ -> mempty LitP _ -> mempty DotP _ -> mempty ProjP _ _ -> mempty traversePatternM pre post = pre >=> recurse >=> post where recurse p = case p of ConP c ci ps -> ConP c ci <$> traversePatternM pre post ps VarP _ -> return p LitP _ -> return p DotP _ -> return p AbsurdP _ -> return p ProjP _ _ -> return p -- Boilerplate instances: instance PatternLike a b => PatternLike a [b] where instance PatternLike a b => PatternLike a (Arg b) where instance PatternLike a b => PatternLike a (Named x b) where Agda-2.5.3/src/full/Agda/Syntax/Internal/Defs.hs0000644000000000000000000000641613154613124017416 0ustar0000000000000000{-# LANGUAGE TypeFamilies #-} -- | Extract used definitions from terms. module Agda.Syntax.Internal.Defs where import Control.Monad.Reader import Control.Monad.Writer import Data.Foldable (Foldable) import qualified Data.Foldable as Fold import Agda.Syntax.Common import Agda.Syntax.Internal -- | @getDefs' lookup emb a@ extracts all used definitions -- (functions, data/record types) from @a@, embedded into a monoid via @emb@. -- Instantiations of meta variables are obtained via @lookup@. -- -- Typical monoid instances would be @[QName]@ or @Set QName@. -- Note that @emb@ can also choose to discard a used definition -- by mapping to the unit of the monoid. getDefs' :: (GetDefs a, Monoid b) => (MetaId -> Maybe Term) -> (QName -> b) -> a -> b getDefs' lookup emb = execWriter . (`runReaderT` GetDefsEnv lookup emb) . getDefs -- | Inputs to and outputs of @getDefs'@ are organized as a monad. type GetDefsM b = ReaderT (GetDefsEnv b) (Writer b) data GetDefsEnv b = GetDefsEnv { lookupMeta :: MetaId -> Maybe Term , embDef :: QName -> b } -- | What it takes to get the used definitions. class Monad m => MonadGetDefs m where doDef :: QName -> m () doMeta :: MetaId -> m () instance Monoid b => MonadGetDefs (GetDefsM b) where doDef d = tell . ($ d) =<< asks embDef doMeta x = getDefs . ($ x) =<< asks lookupMeta -- | Getting the used definitions. -- -- Note: in contrast to 'Agda.Syntax.Internal.Generic.foldTerm' -- @getDefs@ also collects from sorts in terms. -- Thus, this is not an instance of @foldTerm@. class GetDefs a where getDefs :: MonadGetDefs m => a -> m () default getDefs :: (MonadGetDefs m, Foldable f, GetDefs b, f b ~ a) => a -> m () getDefs = Fold.mapM_ getDefs instance GetDefs Clause where getDefs = getDefs . clauseBody instance GetDefs Term where getDefs v = case v of Def d vs -> doDef d >> getDefs vs Con _ _ vs -> getDefs vs Lit l -> return () Var i vs -> getDefs vs Lam _ v -> getDefs v Pi a b -> getDefs a >> getDefs b Sort s -> getDefs s Level l -> getDefs l MetaV x vs -> getDefs x >> getDefs vs DontCare v -> getDefs v Shared p -> getDefs $ derefPtr p -- TODO: exploit sharing! instance GetDefs MetaId where getDefs x = doMeta x instance GetDefs Type where getDefs (El s t) = getDefs s >> getDefs t instance GetDefs Sort where getDefs s = case s of Type l -> getDefs l Prop -> return () Inf -> return () SizeUniv -> return () DLub s s' -> getDefs s >> getDefs s' instance GetDefs Level where getDefs (Max ls) = getDefs ls instance GetDefs PlusLevel where getDefs ClosedLevel{} = return () getDefs (Plus _ l) = getDefs l instance GetDefs LevelAtom where getDefs a = case a of MetaLevel x vs -> getDefs x >> getDefs vs BlockedLevel _ v -> getDefs v NeutralLevel _ v -> getDefs v UnreducedLevel v -> getDefs v -- collection instances instance GetDefs a => GetDefs (Maybe a) where instance GetDefs a => GetDefs [a] where instance GetDefs a => GetDefs (Elim' a) where instance GetDefs a => GetDefs (Arg a) where instance GetDefs a => GetDefs (Dom a) where instance GetDefs a => GetDefs (Abs a) where instance (GetDefs a, GetDefs b) => GetDefs (a,b) where getDefs (a,b) = getDefs a >> getDefs b Agda-2.5.3/src/full/Agda/Syntax/Abstract/0000755000000000000000000000000013154613124016161 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Syntax/Abstract/Pretty.hs0000644000000000000000000000162613154613124020011 0ustar0000000000000000 module Agda.Syntax.Abstract.Pretty where import Control.Applicative import Agda.Syntax.Concrete.Pretty () import Agda.Syntax.Fixity import Agda.Syntax.Translation.AbstractToConcrete import Agda.TypeChecking.Monad import Agda.Utils.Pretty showA :: (Show c, ToConcrete a c) => a -> TCM String showA x = show <$> abstractToConcrete_ x prettyA :: (Pretty c, ToConcrete a c) => a -> TCM Doc prettyA x = pretty <$> abstractToConcrete_ x prettyAs :: (Pretty c, ToConcrete a [c]) => a -> TCM Doc prettyAs x = fsep . map pretty <$> abstractToConcrete_ x -- | Variant of 'showA' which does not insert outermost parentheses. showATop :: (Show c, ToConcrete a c) => a -> TCM String showATop x = show <$> abstractToConcreteCtx TopCtx x -- | Variant of 'prettyA' which does not insert outermost parentheses. prettyATop :: (Pretty c, ToConcrete a c) => a -> TCM Doc prettyATop x = pretty <$> abstractToConcreteCtx TopCtx x Agda-2.5.3/src/full/Agda/Syntax/Abstract/Views.hs0000644000000000000000000003560013154613124017616 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE NoMonoLocalBinds #-} {-# LANGUAGE NoMonomorphismRestriction #-} module Agda.Syntax.Abstract.Views where import Control.Applicative import Control.Arrow (first) import Control.Monad.Identity import Data.Foldable (foldMap) import Data.Monoid import Data.Traversable import Data.Void import Agda.Syntax.Position import Agda.Syntax.Common import Agda.Syntax.Abstract as A import Agda.Syntax.Concrete (FieldAssignment', exprFieldA) import Agda.Syntax.Info import Agda.Syntax.Scope.Base (emptyScopeInfo) import Agda.Utils.Either import Agda.Utils.Lens data AppView = Application Expr [NamedArg Expr] -- | Gather applications to expose head and spine. -- -- Note: everything is an application, possibly of itself to 0 arguments appView :: Expr -> AppView appView e = case e of App _ e1 e2 | Dot _ e2' <- unScope $ namedArg e2 , Just f <- maybeProjTurnPostfix e2' -> Application f [defaultNamedArg e1] App i e1 arg | Application hd es <- appView e1 -> Application hd $ es ++ [arg] ScopedExpr _ e -> appView e _ -> Application e [] maybeProjTurnPostfix :: Expr -> Maybe Expr maybeProjTurnPostfix e = case e of ScopedExpr i e' -> ScopedExpr i <$> maybeProjTurnPostfix e' Proj _ x -> return $ Proj ProjPostfix x _ -> Nothing unAppView :: AppView -> Expr unAppView (Application h es) = foldl (App (ExprRange noRange)) h es -- | Collects plain lambdas. data LamView = LamView [(LamInfo, LamBinding)] Expr lamView :: Expr -> LamView lamView (Lam i b e) = cons (i, b) $ lamView e where cons b (LamView bs e) = LamView (b : bs) e lamView (ScopedExpr _ e) = lamView e lamView e = LamView [] e -- | Gather top-level 'AsP'atterns to expose underlying pattern. asView :: A.Pattern -> ([Name], A.Pattern) asView (A.AsP _ x p) = first (x :) $ asView p asView p = ([], p) -- | Check whether we are dealing with a universe. isSet :: Expr -> Bool isSet (ScopedExpr _ e) = isSet e isSet (App _ e _) = isSet e isSet (Set{}) = True isSet _ = False -- | Remove top 'ScopedExpr' wrappers. unScope :: Expr -> Expr unScope (ScopedExpr scope e) = unScope e unScope (QuestionMark i ii) = QuestionMark (i {metaScope = emptyScopeInfo}) ii unScope (Underscore i) = Underscore (i {metaScope = emptyScopeInfo}) unScope e = e -- | Remove 'ScopedExpr' wrappers everywhere. -- -- NB: Unless the implementation of 'ExprLike' for clauses -- has been finished, this does not work for clauses yet. deepUnscope :: ExprLike a => a -> a deepUnscope = mapExpr unScope deepUnscopeDecls :: [A.Declaration] -> [A.Declaration] deepUnscopeDecls = concatMap deepUnscopeDecl deepUnscopeDecl :: A.Declaration -> [A.Declaration] deepUnscopeDecl (A.ScopedDecl _ ds) = deepUnscopeDecls ds deepUnscopeDecl (A.Mutual i ds) = [A.Mutual i (deepUnscopeDecls ds)] deepUnscopeDecl (A.Section i m tel ds) = [A.Section i m (deepUnscope tel) (deepUnscopeDecls ds)] deepUnscopeDecl (A.RecDef i x ind eta c bs e ds) = [A.RecDef i x ind eta c (deepUnscope bs) (deepUnscope e) (deepUnscopeDecls ds)] deepUnscopeDecl d = [deepUnscope d] -- * Traversal -- | Apply an expression rewriting to every subexpression, inside-out. -- See "Agda.Syntax.Internal.Generic". class ExprLike a where -- | The first expression is pre-traversal, the second one post-traversal. recurseExpr :: (Applicative m) => (Expr -> m Expr -> m Expr) -> a -> m a default recurseExpr :: (Traversable f, ExprLike a', a ~ f a', Applicative m) => (Expr -> m Expr -> m Expr) -> a -> m a recurseExpr = traverse . recurseExpr foldExpr :: Monoid m => (Expr -> m) -> a -> m foldExpr f = getConst . recurseExpr (\ pre post -> Const (f pre) <* post) traverseExpr #if __GLASGOW_HASKELL__ <= 708 :: (Applicative m, Monad m) #else :: Monad m #endif => (Expr -> m Expr) -> a -> m a traverseExpr f = recurseExpr (\ pre post -> f =<< post) mapExpr :: (Expr -> Expr) -> (a -> a) mapExpr f = runIdentity . traverseExpr (Identity . f) instance ExprLike Expr where recurseExpr f e0 = f e0 $ do let recurse e = recurseExpr f e case e0 of Var{} -> pure e0 Def{} -> pure e0 Proj{} -> pure e0 Con{} -> pure e0 Lit{} -> pure e0 QuestionMark{} -> pure e0 Underscore{} -> pure e0 Dot ei e -> Dot ei <$> recurse e App ei e arg -> App ei <$> recurse e <*> recurse arg WithApp ei e es -> WithApp ei <$> recurse e <*> recurse es Lam ei b e -> Lam ei <$> recurse b <*> recurse e AbsurdLam{} -> pure e0 ExtendedLam ei di x cls -> ExtendedLam ei di x <$> recurse cls Pi ei tel e -> Pi ei <$> recurse tel <*> recurse e Fun ei arg e -> Fun ei <$> recurse arg <*> recurse e Set{} -> pure e0 Prop{} -> pure e0 Let ei bs e -> Let ei <$> recurse bs <*> recurse e ETel tel -> ETel <$> recurse tel Rec ei bs -> Rec ei <$> recurse bs RecUpdate ei e bs -> RecUpdate ei <$> recurse e <*> recurse bs ScopedExpr sc e -> ScopedExpr sc <$> recurse e QuoteGoal ei n e -> QuoteGoal ei n <$> recurse e QuoteContext ei -> pure e0 Quote{} -> pure e0 QuoteTerm{} -> pure e0 Unquote{} -> pure e0 DontCare e -> DontCare <$> recurse e PatternSyn{} -> pure e0 Tactic ei e xs ys -> Tactic ei <$> recurse e <*> recurse xs <*> recurse ys Macro{} -> pure e0 foldExpr f e = case e of Var{} -> m Def{} -> m Proj{} -> m Con{} -> m PatternSyn{} -> m Macro{} -> m Lit{} -> m QuestionMark{} -> m Underscore{} -> m Dot _ e -> m `mappend` fold e App _ e e' -> m `mappend` fold e `mappend` fold e' WithApp _ e es -> m `mappend` fold e `mappend` fold es Lam _ b e -> m `mappend` fold b `mappend` fold e AbsurdLam{} -> m ExtendedLam _ _ _ cs -> m `mappend` fold cs Pi _ tel e -> m `mappend` fold tel `mappend` fold e Fun _ e e' -> m `mappend` fold e `mappend` fold e' Set{} -> m Prop{} -> m Let _ bs e -> m `mappend` fold bs `mappend` fold e ETel tel -> m `mappend` fold tel Rec _ as -> m `mappend` fold as RecUpdate _ e as -> m `mappend` fold e `mappend` fold as ScopedExpr _ e -> m `mappend` fold e QuoteGoal _ _ e -> m `mappend` fold e QuoteContext _ -> m Quote{} -> m QuoteTerm{} -> m Unquote{} -> m Tactic _ e xs ys -> m `mappend` fold e `mappend` fold xs `mappend` fold ys DontCare e -> m `mappend` fold e where m = f e fold = foldExpr f traverseExpr f e = do let trav e = traverseExpr f e case e of Var{} -> f e Def{} -> f e Proj{} -> f e Con{} -> f e Lit{} -> f e QuestionMark{} -> f e Underscore{} -> f e Dot ei e -> f =<< Dot ei <$> trav e App ei e arg -> f =<< App ei <$> trav e <*> trav arg WithApp ei e es -> f =<< WithApp ei <$> trav e <*> trav es Lam ei b e -> f =<< Lam ei <$> trav b <*> trav e AbsurdLam{} -> f e ExtendedLam ei di x cls -> f =<< ExtendedLam ei di x <$> trav cls Pi ei tel e -> f =<< Pi ei <$> trav tel <*> trav e Fun ei arg e -> f =<< Fun ei <$> trav arg <*> trav e Set{} -> f e Prop{} -> f e Let ei bs e -> f =<< Let ei <$> trav bs <*> trav e ETel tel -> f =<< ETel <$> trav tel Rec ei bs -> f =<< Rec ei <$> trav bs RecUpdate ei e bs -> f =<< RecUpdate ei <$> trav e <*> trav bs ScopedExpr sc e -> f =<< ScopedExpr sc <$> trav e QuoteGoal ei n e -> f =<< QuoteGoal ei n <$> trav e QuoteContext{} -> f e Quote{} -> f e QuoteTerm{} -> f e Unquote{} -> f e Tactic ei e xs ys -> f =<< Tactic ei <$> trav e <*> trav xs <*> trav ys DontCare e -> f =<< DontCare <$> trav e PatternSyn{} -> f e Macro{} -> f e instance ExprLike a => ExprLike (Arg a) where instance ExprLike a => ExprLike (Named x a) where instance ExprLike a => ExprLike [a] where instance (ExprLike a, ExprLike b) => ExprLike (a, b) where recurseExpr f (x, y) = (,) <$> recurseExpr f x <*> recurseExpr f y instance ExprLike Void where recurseExpr f = absurd instance ExprLike a => ExprLike (FieldAssignment' a) where recurseExpr = exprFieldA . recurseExpr instance (ExprLike a, ExprLike b) => ExprLike (Either a b) where recurseExpr f = traverseEither (recurseExpr f) (recurseExpr f) instance ExprLike ModuleName where recurseExpr f = pure instance ExprLike QName where recurseExpr _ = pure instance ExprLike LamBinding where recurseExpr f e = case e of DomainFree{} -> pure e DomainFull bs -> DomainFull <$> recurseExpr f bs foldExpr f e = case e of DomainFree{} -> mempty DomainFull bs -> foldExpr f bs traverseExpr f e = case e of DomainFree{} -> pure e DomainFull bs -> DomainFull <$> traverseExpr f bs instance ExprLike TypedBindings where recurseExpr f (TypedBindings r b) = TypedBindings r <$> recurseExpr f b foldExpr f (TypedBindings r b) = foldExpr f b traverseExpr f (TypedBindings r b) = TypedBindings r <$> traverseExpr f b instance ExprLike TypedBinding where recurseExpr f e = case e of TBind r xs e -> TBind r xs <$> recurseExpr f e TLet r ds -> TLet r <$> recurseExpr f ds foldExpr f e = case e of TBind _ _ e -> foldExpr f e TLet _ ds -> foldExpr f ds traverseExpr f e = case e of TBind r xs e -> TBind r xs <$> traverseExpr f e TLet r ds -> TLet r <$> traverseExpr f ds instance ExprLike LetBinding where recurseExpr f e = do let recurse e = recurseExpr f e case e of LetBind li ai x e e' -> LetBind li ai x <$> recurse e <*> recurse e' LetPatBind li p e -> LetPatBind li <$> recurse p <*> recurse e LetApply{} -> pure e LetOpen{} -> pure e LetDeclaredVariable _ -> pure e foldExpr f e = case e of LetBind _ _ _ e e' -> fold e `mappend` fold e' LetPatBind _ p e -> fold p `mappend` fold e LetApply{} -> mempty LetOpen{} -> mempty LetDeclaredVariable _ -> mempty where fold e = foldExpr f e traverseExpr f e = do let trav e = traverseExpr f e case e of LetBind li ai x e e' -> LetBind li ai x <$> trav e <*> trav e' LetPatBind li p e -> LetPatBind li <$> trav p <*> trav e LetApply{} -> pure e LetOpen{} -> pure e LetDeclaredVariable _ -> pure e instance ExprLike a => ExprLike (Pattern' a) where instance ExprLike a => ExprLike (Clause' a) where recurseExpr f (Clause lhs dots sdots rhs ds ca) = Clause <$> rec lhs <*> pure dots <*> pure sdots <*> rec rhs <*> rec ds <*> pure ca where rec = recurseExpr f instance ExprLike RHS where recurseExpr f rhs = case rhs of RHS e c -> RHS <$> rec e <*> pure c AbsurdRHS{} -> pure rhs WithRHS x es cs -> WithRHS x <$> rec es <*> rec cs RewriteRHS xes rhs ds -> RewriteRHS <$> rec xes <*> rec rhs <*> rec ds where rec e = recurseExpr f e instance ExprLike ModuleApplication where recurseExpr f a = case a of SectionApp tel m es -> SectionApp <$> rec tel <*> rec m <*> rec es RecordModuleIFS{} -> pure a where rec e = recurseExpr f e instance ExprLike Pragma where recurseExpr f p = case p of BuiltinPragma s e -> BuiltinPragma s <$> rec e OptionsPragma{} -> pure p BuiltinNoDefPragma{} -> pure p RewritePragma{} -> pure p CompilePragma{} -> pure p CompiledPragma{} -> pure p CompiledExportPragma{} -> pure p CompiledTypePragma{} -> pure p CompiledDataPragma{} -> pure p CompiledJSPragma{} -> pure p CompiledUHCPragma{} -> pure p CompiledDataUHCPragma{} -> pure p StaticPragma{} -> pure p InjectivePragma{} -> pure p InlinePragma{} -> pure p EtaPragma{} -> pure p DisplayPragma f xs e -> DisplayPragma f <$> rec xs <*> rec e where rec e = recurseExpr f e instance ExprLike LHS where recurseExpr f (LHS i c ps) = LHS i <$> recurseExpr f c <*> recurseExpr f ps instance ExprLike a => ExprLike (LHSCore' a) where instance ExprLike SpineLHS where recurseExpr f (SpineLHS i x ps wps) = SpineLHS i x <$> recurseExpr f ps <*> recurseExpr f wps instance ExprLike Declaration where recurseExpr f d = case d of Axiom a d i mp x e -> Axiom a d i mp x <$> rec e Field i x e -> Field i x <$> rec e Primitive i x e -> Primitive i x <$> rec e Mutual i ds -> Mutual i <$> rec ds Section i m tel ds -> Section i m <$> rec tel <*> rec ds Apply i m a ci d -> (\ a -> Apply i m a ci d) <$> rec a Import{} -> pure d Pragma i p -> Pragma i <$> rec p Open{} -> pure d FunDef i f d cs -> FunDef i f d <$> rec cs DataSig i d tel e -> DataSig i d <$> rec tel <*> rec e DataDef i d bs cs -> DataDef i d <$> rec bs <*> rec cs RecSig i r tel e -> RecSig i r <$> rec tel <*> rec e RecDef i r n co c bs e ds -> RecDef i r n co c <$> rec bs <*> rec e <*> rec ds PatternSynDef f xs p -> PatternSynDef f xs <$> rec p UnquoteDecl i is xs e -> UnquoteDecl i is xs <$> rec e UnquoteDef i xs e -> UnquoteDef i xs <$> rec e ScopedDecl s ds -> ScopedDecl s <$> rec ds where rec e = recurseExpr f e Agda-2.5.3/src/full/Agda/Syntax/Abstract/Pattern.hs0000644000000000000000000001644013154613124020137 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} -- | Auxiliary functions to handle patterns in the abstract syntax. -- -- Generic and specific traversals. module Agda.Syntax.Abstract.Pattern where import Control.Monad ((>=>)) import Control.Applicative (Applicative) import Data.Foldable (Foldable, foldMap) import Data.Traversable (Traversable, traverse) import Data.Functor import Data.Monoid import Agda.Syntax.Common import Agda.Syntax.Concrete (FieldAssignment', exprFieldA) import Agda.Syntax.Abstract as A #include "undefined.h" import Agda.Utils.Impossible -- * Generic traversals type NAP = NamedArg Pattern class MapNamedArgPattern a where mapNamedArgPattern :: (NAP -> NAP) -> a -> a default mapNamedArgPattern :: (Functor f, MapNamedArgPattern a', a ~ f a') => (NAP -> NAP) -> a -> a mapNamedArgPattern = fmap . mapNamedArgPattern instance MapNamedArgPattern NAP where mapNamedArgPattern f p = case namedArg p of -- no sub patterns: VarP{} -> f p WildP{} -> f p DotP{} -> f p LitP{} -> f p AbsurdP{} -> f p ProjP{} -> f p -- list of NamedArg subpatterns: ConP i qs ps -> f $ setNamedArg p $ ConP i qs $ mapNamedArgPattern f ps DefP i qs ps -> f $ setNamedArg p $ DefP i qs $ mapNamedArgPattern f ps PatternSynP i x ps -> f $ setNamedArg p $ PatternSynP i x $ mapNamedArgPattern f ps -- Pattern subpattern(s): -- RecP: we copy the NamedArg info to the subpatterns but discard it after recursion RecP i fs -> f $ setNamedArg p $ RecP i $ map (fmap namedArg) $ mapNamedArgPattern f $ map (fmap (setNamedArg p)) fs -- AsP: we hand the NamedArg info to the subpattern AsP i x p0 -> f $ updateNamedArg (AsP i x) $ mapNamedArgPattern f $ setNamedArg p p0 instance MapNamedArgPattern a => MapNamedArgPattern [a] where instance MapNamedArgPattern a => MapNamedArgPattern (FieldAssignment' a) where instance MapNamedArgPattern a => MapNamedArgPattern (Maybe a) where -- | Generic pattern traversal. class APatternLike a p | p -> a where -- | Fold pattern. foldrAPattern :: Monoid m => (Pattern' a -> m -> m) -- ^ Combine a pattern and the value computed from its subpatterns. -> p -> m default foldrAPattern :: (Monoid m, Foldable f, APatternLike a b, f b ~ p) => (Pattern' a -> m -> m) -> p -> m foldrAPattern = foldMap . foldrAPattern -- | Traverse pattern. traverseAPatternM :: (Monad m #if __GLASGOW_HASKELL__ <= 708 , Applicative m, Functor m #endif ) => (Pattern' a -> m (Pattern' a)) -- ^ @pre@: Modification before recursion. -> (Pattern' a -> m (Pattern' a)) -- ^ @post@: Modification after recursion. -> p -> m p default traverseAPatternM :: (Traversable f, APatternLike a q, f q ~ p, Monad m #if __GLASGOW_HASKELL__ <= 708 , Applicative m, Functor m #endif ) => (Pattern' a -> m (Pattern' a)) -> (Pattern' a -> m (Pattern' a)) -> p -> m p traverseAPatternM pre post = traverse $ traverseAPatternM pre post -- | Compute from each subpattern a value and collect them all in a monoid. foldAPattern :: (APatternLike a p, Monoid m) => (Pattern' a -> m) -> p -> m foldAPattern f = foldrAPattern $ \ p m -> f p `mappend` m -- | Traverse pattern(s) with a modification before the recursive descent. preTraverseAPatternM :: (APatternLike a b, Monad m #if __GLASGOW_HASKELL__ <= 708 , Applicative m, Functor m #endif ) => (Pattern' a -> m (Pattern' a)) -- ^ @pre@: Modification before recursion. -> b -> m b preTraverseAPatternM pre p = traverseAPatternM pre return p -- | Traverse pattern(s) with a modification after the recursive descent. postTraverseAPatternM :: (APatternLike a b, Monad m #if __GLASGOW_HASKELL__ <= 708 , Applicative m, Functor m #endif ) => (Pattern' a -> m (Pattern' a)) -- ^ @post@: Modification after recursion. -> b -> m b postTraverseAPatternM post p = traverseAPatternM return post p -- Interesting instance: instance APatternLike a (Pattern' a) where foldrAPattern f p = f p $ case p of AsP _ _ p -> foldrAPattern f p ConP _ _ ps -> foldrAPattern f ps DefP _ _ ps -> foldrAPattern f ps RecP _ ps -> foldrAPattern f ps PatternSynP _ _ ps -> foldrAPattern f ps VarP _ -> mempty ProjP _ _ _ -> mempty WildP _ -> mempty DotP _ _ _ -> mempty AbsurdP _ -> mempty LitP _ -> mempty traverseAPatternM pre post = pre >=> recurse >=> post where recurse p = case p of -- Non-recursive cases: A.VarP{} -> return p A.WildP{} -> return p A.DotP{} -> return p A.LitP{} -> return p A.AbsurdP{} -> return p A.ProjP{} -> return p -- Recursive cases: A.ConP i ds ps -> A.ConP i ds <$> traverseAPatternM pre post ps A.DefP i q ps -> A.DefP i q <$> traverseAPatternM pre post ps A.AsP i x p -> A.AsP i x <$> traverseAPatternM pre post p A.RecP i ps -> A.RecP i <$> traverseAPatternM pre post ps A.PatternSynP i x ps -> A.PatternSynP i x <$> traverseAPatternM pre post ps -- The following instances need UndecidableInstances -- for the FunctionalDependency (since injectivity is not taken into account). instance APatternLike a b => APatternLike a (Arg b) where instance APatternLike a b => APatternLike a (Named n b) where instance APatternLike a b => APatternLike a [b] where instance APatternLike a b => APatternLike a (Maybe b) where instance APatternLike a b => APatternLike a (FieldAssignment' b) where -- * Specific folds -- | Collect pattern variables in left-to-right textual order. patternVars :: forall a p. APatternLike a p => p -> [A.Name] patternVars p = foldAPattern f p `appEndo` [] where -- We use difference lists @[A.Name] -> [A.Name]@ to avoid reconcatenation. f :: Pattern' a -> Endo [A.Name] f = \case A.VarP x -> Endo (x :) A.AsP _ x _ -> Endo (x :) A.LitP {} -> mempty A.ConP {} -> mempty A.RecP {} -> mempty A.DefP {} -> mempty A.ProjP {} -> mempty A.WildP {} -> mempty A.DotP {} -> mempty A.AbsurdP {} -> mempty A.PatternSynP {} -> mempty -- | Check if a pattern contains a specific (sub)pattern. containsAPattern :: APatternLike a p => (Pattern' a -> Bool) -> p -> Bool containsAPattern f = getAny . foldAPattern (Any . f) -- | Check if a pattern contains an absurd pattern. -- For instance, @suc ()@, does so. -- -- Precondition: contains no pattern synonyms. containsAbsurdPattern :: APatternLike a p => p -> Bool containsAbsurdPattern = containsAPattern $ \case A.PatternSynP{} -> __IMPOSSIBLE__ A.AbsurdP{} -> True _ -> False -- | Check if a pattern contains an @-pattern. -- -- Precondition: contains no pattern synonyms. containsAsPattern :: APatternLike a p => p -> Bool containsAsPattern = containsAPattern $ \case A.PatternSynP{} -> __IMPOSSIBLE__ A.AsP{} -> True _ -> False Agda-2.5.3/src/full/Agda/Syntax/Abstract/Copatterns.hs0000644000000000000000000003373713154613124020654 0ustar0000000000000000{-# LANGUAGE CPP #-} module Agda.Syntax.Abstract.Copatterns (translateCopatternClauses) where import Prelude hiding (mapM) import Control.Applicative import Control.Monad hiding (mapM) import Control.Monad.Writer hiding (mapM) import Data.Function import Data.List import Data.Traversable as T import Agda.Syntax.Abstract import Agda.Syntax.Common import qualified Agda.Syntax.Concrete.Name as C import Agda.Syntax.Concrete (FieldAssignment'(..)) import Agda.Syntax.Info import Agda.Syntax.Position import Agda.Syntax.Scope.Monad import Agda.TypeChecking.Monad.Base (TypeError(..), typeError) import Agda.Utils.Either import Agda.Utils.Maybe import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible {- Andreas 2012-04-07, 2012-05-08 Translating copatterns into record expressions This is a preliminary solution until we have proper copattern type checking and evaluation. Example 1: record Stream (A : Set) : Set where field head : A tail : Stream A open Stream alternate : Stream Nat ( head alternate ) = zero (head (tail alternate)) = suc zero (tail (tail alternate)) = alternate with pathes Path [head] zero Path [tail,head] (suc zero) Path [tail,tail] alternate is translated into alternate = record { head = zero ; tail = record { head = suc zero ; tail = alternate } } Example 2: record State (S A : Set) : Set where constructor state field runState : S → A × S open State record Monad (M : Set → Set) : Set1 where constructor monad field return : {A : Set} → A → M A _>>=_ : {A B : Set} → M A → (A → M B) → M B open Monad stateMonad : {S : Set} → Monad (State S) runState (return stateMonad a ) s = a , s runState (_>>=_ stateMonad m k) s₀ = let as₁ = runState m s₀ in runState (k (proj₁ as₁)) (proj₂ as₁) with pathes Path [(return,[a] ), (runstate,[s ])] (a,s) Path [(_>>=_, [m,k]), (runstate,[s₀])] (let...) is translated to stateMonad = record { return = λ a → record { runState = λ s → a , s } ; _>>=_ = λ m k → record { runState = λ s₀ → let as₁ = runState m s₀ in runState (k (proj₁ as₁)) (proj₂ as₁) } Example 3: swap3 : {A B C X : Set} → (X → A) × ((X → B) × C) → (X → C) × (X → (B × A)) fst (swap3 t) x = snd (snd t) fst (snd (swap3 t) y) = fst (snd t) y snd (snd (swap3 t) z) = fst t z with pathes Path [(fst,[x])] (snd (snd t)) Path [(snd,[y]), (fst,[])] (fst (snd t) y) Path [(snd,[z]), (snd,[])] (fst t z) ist translated to swap3 t = record { fst = λ x → snd (snd t) ; snd = λ y → record { fst = fst (snd t) y ; snd = (fst t z){z := y} } } How to translate: - group clauses into those with same LHSCore and same withpatterns -} translateCopatternClauses :: [Clause] -> ScopeM (Delayed, [Clause]) translateCopatternClauses cs = if all noCopats cs then return (NotDelayed, cs) else (Delayed,) <$> do pcs :: [ProjPath Clause] <- mapM clauseToPath cs let cps :: [(Clause, [ProjPath Expr])] cps = groupClauses pcs ces <- mapM (mapSndM pathToRecord) $ map (mapSnd $ sortBy (compare `on` thePath)) cps return $ map (\ (c, e) -> c { clauseRHS = RHS e Nothing }) ces -- TODO: preserve C.Expr where noCopats Clause{ clauseLHS = LHS _ LHSHead{} _ } = True noCopats _ = False -- | A sequence of decisions @b@ leading to a head @a@. data Path a b = Path { thePath :: [a] -- ^ the list of choices , theContent :: b } deriving (Functor) -- NB: this means @Path a@ is a functor for any @a@ mapContent :: (b -> c) -> Path a b -> Path a c mapContent f (Path p c) = Path p (f c) data ProjEntry = ProjEntry { projPE :: AmbiguousQName , patsPE :: [NamedArg Name] -- ^ currently we only support variable patterns } deriving (Eq, Ord) type ProjPath = Path ProjEntry instance HasRange ProjEntry where getRange (ProjEntry p ps) = getRange (p,ps) -- | This is a n^2 grouping algorithm which uses only alpha-equality groupClauses :: [ProjPath Clause] -> [(Clause, [ProjPath Expr])] groupClauses [] = [] groupClauses (pc@(Path p c) : pcs) = (c, Path p (rhs c) : grp) : groupClauses rest where (grp, rest) = collect pcs -- Collect l splits l into pc's group and the remainder -- If the lhs of the next clause is alpha-equivalent to the current lhs -- then add the next clause to this group, performing the alpha-conversion collect (Path p' c' : pcs) | Just rho <- alpha (clauseLHS c') (clauseLHS c) = mapFst (Path p' (rename' rho (rhs c')) :) $ collect pcs -- we go through all the clauses, since they could be in random order... collect (pc : pcs) = mapSnd (pc :) $ collect pcs collect [] = ([], []) rhs = rhsExpr . clauseRHS rhsExpr (RHS e _ ) = e -- TODO: preserve C.Expr rhsExpr _ = __IMPOSSIBLE__ clauseToPath :: Clause -> ScopeM (ProjPath Clause) clauseToPath (Clause (LHS i lhs wps) dots sdots (RHS e c) [] catchall) = fmap (\ lhs -> Clause (LHS i lhs wps) dots sdots (RHS e c) [] catchall) <$> lhsToPath [] lhs clauseToPath (Clause lhs _ _ (RHS e _) (_:_) _) = typeError $ NotImplemented $ "copattern clauses with where declarations" clauseToPath (Clause lhs _ _ _ wheredecls _) = typeError $ NotImplemented $ "copattern clauses with absurd, with or rewrite right hand side" lhsToPath :: [ProjEntry] -> LHSCore -> ScopeM (ProjPath LHSCore) lhsToPath acc lhs@LHSHead{} = return $ Path acc lhs lhsToPath acc (LHSProj f lhs ps) = do let xs = fromMaybe __IMPOSSIBLE__ $ mapM (T.mapM (T.mapM fromVarP)) ps lhsToPath (ProjEntry f xs : acc) $ namedArg lhs where fromVarP :: Pattern -> Maybe Name fromVarP (VarP n) = Just n fromVarP _ = Nothing -- | Expects a sorted list. pathToRecord :: [ProjPath Expr] -> ScopeM Expr pathToRecord [] = __IMPOSSIBLE__ pathToRecord [Path [] e] = return e pathToRecord pps = case pathHeads pps of Nothing -> typeError $ GenericError $ "overlapping copattern clauses" Just pps -> do pes <- mapM (mapSndM pathToRecord) $ groupPathes pps let ei = ExprRange $ getRange $ map fst pes Rec ei <$> mapM abstractions pes where abstractions :: (ProjEntry, Expr) -> ScopeM RecordAssign abstractions (ProjEntry (AmbQ []) xs, e) = __IMPOSSIBLE__ abstractions (ProjEntry (AmbQ (p:_)) xs, e) = Left . FieldAssignment (C.unqualify $ qnameToConcrete p) <$> foldr abstract (return e) xs abstract :: NamedArg Name -> ScopeM Expr -> ScopeM Expr abstract (Arg info (Named Nothing x)) me = Lam defaultLamInfo_ (DomainFree info x) <$> me abstract (Arg _ (Named Just{} _)) me = typeError $ NotImplemented $ "named arguments in projection patterns" -- | Similar to 'groupClauses'. groupPathes :: [(ProjEntry, ProjPath Expr)] -> [(ProjEntry, [ProjPath Expr])] groupPathes [] = [] groupPathes ((pe@(ProjEntry p xs), path) : pps) = (pe, path : grp) : groupPathes rest -- Now group all following pps that have the same projection p -- We expect that they have alpha-equivalent xs where (grp, rest) = collect pps collect l@((ProjEntry p' xs', path) : pps) | p == p', Just rho <- alpha xs' xs = -- add the alpha-converted path to the group -- NOTE: because the path contains only projections and pattern vars -- we only alpha-convert the content (rhs Expr) -- When the path will contain dot patterns, we have to rename in them mapFst (mapContent (rename' rho) path :) $ collect pps collect l = ([], l) -- null or different projection: close group pathHeads :: [Path a b] -> Maybe [(a, Path a b)] pathHeads = mapM pathSplit pathSplit :: Path a b -> Maybe (a, Path a b) pathSplit (Path [] b) = Nothing pathSplit (Path (a:as) b) = Just (a, Path as b) -- * Alpha conversion type NameMap = [(Name,Name)] class Rename e where rename :: (Name -> Maybe Name) -> e -> e rename' :: NameMap -> e -> e rename' rho = rename (flip lookup rho) -- | 'QName's are not renamed. instance Rename QName where rename _ q = q instance Rename Name where rename rho x = fromMaybe x (rho x) instance Rename Expr where rename rho e = case e of Var x -> Var (rename rho x) Def f -> e Proj{} -> e Con c -> e Lit l -> e QuestionMark{} -> e Underscore i -> e Dot i e -> Dot i (rename rho e) App i e es -> App i (rename rho e) (rename rho es) WithApp i e es -> WithApp i (rename rho e) (rename rho es) Lam i lb e -> Lam i (rename rho lb) (rename rho e) AbsurdLam{} -> e ExtendedLam i i' n cs -> ExtendedLam i i' n (rename rho cs) Pi i tel e -> Pi i (rename rho tel) (rename rho e) Fun i a e -> Fun i (rename rho a) (rename rho e) Set{} -> e Prop{} -> e Let i bs e -> Let i (rename rho bs) (rename rho e) ETel tel -> ETel (rename rho tel) Rec i fes -> Rec i $ rename rho fes RecUpdate i e fes -> RecUpdate i (rename rho e) (rename rho fes) ScopedExpr i e -> ScopedExpr i (rename rho e) QuoteGoal i n e -> QuoteGoal i n (rename rho e) QuoteContext i -> e Quote i -> e QuoteTerm i -> e Unquote i -> e Tactic i e xs ys -> Tactic i (rename rho e) (rename rho xs) (rename rho ys) DontCare e -> DontCare (rename rho e) PatternSyn{} -> e Macro{} -> e instance Rename ModuleName where rename rho x = x instance Rename a => Rename (FieldAssignment' a) where rename rho = fmap (rename rho) instance Rename LetBinding where rename rho e = case e of LetBind i r n e e' -> LetBind i r n (rename rho e) (rename rho e') LetPatBind i p e -> LetPatBind i (rename rho p) (rename rho e) LetApply{} -> e LetOpen{} -> e LetDeclaredVariable x -> LetDeclaredVariable (rename rho x) instance Rename LamBinding where rename rho e = case e of DomainFree{} -> e DomainFull tb -> DomainFull (rename rho tb) instance Rename TypedBindings where rename rho (TypedBindings r tb) = TypedBindings r (rename rho tb) instance Rename TypedBinding where rename rho (TBind r ns e) = TBind r ns (rename rho e) rename rho (TLet r lbs) = TLet r (rename rho lbs) instance Rename Clause where rename rho (Clause lhs dots sdots rhs wheredecls catchall) = Clause (rename rho lhs) (rename rho dots) (rename rho sdots) (rename rho rhs) (rename rho wheredecls) catchall instance Rename NamedDotPattern where rename rho (NamedDot x v t) = NamedDot (rename rho x) v t instance Rename StrippedDotPattern where rename rho (StrippedDot e v t) = StrippedDot (rename rho e) v t instance Rename RHS where rename rho e = case e of RHS e c -> RHS (rename rho e) c AbsurdRHS -> e WithRHS n es cs -> WithRHS n (rename rho es) (rename rho cs) RewriteRHS nes r ds -> RewriteRHS (rename rho nes) (rename rho r) (rename rho ds) instance Rename LHS where rename rho (LHS i core ps) = LHS i (rename rho core) (rename rho ps) instance Rename LHSCore where rename rho = fmap (rename rho) -- only rename in dot patterns instance Rename Pattern where rename rho = fmap (rename rho) -- only rename in dot patterns instance Rename Declaration where rename rho d = __IMPOSSIBLE__ instance Rename a => Rename (Arg a) where rename rho = fmap (rename rho) instance Rename a => Rename (Named n a) where rename rho = fmap (rename rho) instance Rename a => Rename [a] where rename rho = map (rename rho) instance (Rename a, Rename b) => Rename (Either a b) where rename rho = mapEither (rename rho) (rename rho) instance (Rename a, Rename b) => Rename (a, b) where rename rho (a,b) = (rename rho a, rename rho b) -- | Alpha-Equivalence of patterns, ignoring dot patterns class Alpha t where alpha :: t -> t -> Maybe NameMap alpha t t' = fmap snd $ runWriterT $ alpha' t t' alpha' :: t -> t -> WriterT NameMap Maybe () alpha' t t' = WriterT $ fmap ((),) $ alpha t t' instance Alpha Name where alpha' x x' = tell1 (x, x') instance Alpha (Pattern' e) where alpha' p p' = case (p,p') of ((VarP x) , (VarP x') ) -> tell1 (x, x') ((ConP _ x ps) , (ConP _ x' ps') ) -> guard (x == x') >> alpha' ps ps' ((DefP _ x ps) , (DefP _ x' ps') ) -> guard (x == x') >> alpha' ps ps' ((WildP _) , (WildP _) ) -> return () ((AsP _ x p) , (AsP _ x' p') ) -> tell1 (x, x') >> alpha' p p' ((DotP _ _ _) , (DotP _ _ _) ) -> return () (AbsurdP{} , AbsurdP{} ) -> return () ((LitP l) , (LitP l') ) -> guard (l == l') ((PatternSynP _ x ps) , (PatternSynP _ x' ps')) -> guard (x == x') >> alpha' ps ps' (_ , _ ) -> fail "not alpha equivalent" tell1 :: (MonadWriter [a] m) => a -> m () tell1 a = tell [a] instance Alpha (LHSCore' e) where alpha' (LHSHead f ps) (LHSHead f' ps') = guard (f == f') >> alpha' ps ps' alpha' (LHSProj d lhs ps) (LHSProj d' lhs' ps') = guard (d == d') >> alpha' lhs lhs' >> alpha' ps ps' alpha' _ _ = fail "not alpha equivalent" instance Alpha LHS where alpha' (LHS _ core wps) (LHS _ core' wps') = alpha' core core' >> alpha' wps wps' instance Alpha a => Alpha (Arg a) where alpha' a a' = alpha' (unArg a) (unArg a') instance (Eq n, Alpha a) => Alpha (Named n a) where alpha' a a' = guard (nameOf a == nameOf a') >> alpha' (namedThing a) (namedThing a') instance Alpha a => Alpha [a] where alpha' l l' = guard (length l == length l') >> zipWithM_ alpha' l l' Agda-2.5.3/src/full/Agda/Syntax/Abstract/Name.hs0000644000000000000000000003242113154613124017377 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-| Abstract names carry unique identifiers and stuff. -} module Agda.Syntax.Abstract.Name ( module Agda.Syntax.Abstract.Name , IsNoName(..) ) where import Control.DeepSeq import Data.Foldable (Foldable) import Data.Traversable (Traversable) import Data.Data (Data) import Data.Typeable (Typeable) import Data.List import Data.Function import Data.Hashable (Hashable(..)) import Agda.Syntax.Position import Agda.Syntax.Common import {-# SOURCE #-} Agda.Syntax.Fixity import Agda.Syntax.Concrete.Name (IsNoName(..), NumHoles(..)) import qualified Agda.Syntax.Concrete.Name as C import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Pretty import Agda.Utils.Size import Agda.Utils.Suffix #include "undefined.h" import Agda.Utils.Impossible -- | A name is a unique identifier and a suggestion for a concrete name. The -- concrete name contains the source location (if any) of the name. The -- source location of the binding site is also recorded. data Name = Name { nameId :: !NameId , nameConcrete :: C.Name , nameBindingSite :: Range , nameFixity :: Fixity' } deriving (Typeable, Data) -- | Qualified names are non-empty lists of names. Equality on qualified names -- are just equality on the last name, i.e. the module part is just -- for show. -- -- The 'SetRange' instance for qualified names sets all individual -- ranges (including those of the module prefix) to the given one. data QName = QName { qnameModule :: ModuleName , qnameName :: Name } deriving (Typeable, Data) -- | Something preceeded by a qualified name. data QNamed a = QNamed { qname :: QName , qnamed :: a } deriving (Typeable, Functor, Foldable, Traversable) -- | A module name is just a qualified name. -- -- The 'SetRange' instance for module names sets all individual ranges -- to the given one. newtype ModuleName = MName { mnameToList :: [Name] } deriving (Eq, Ord, Typeable, Data) -- | Ambiguous qualified names. Used for overloaded constructors. -- -- Invariant: All the names in the list must have the same concrete, -- unqualified name. (This implies that they all have the same 'Range'). newtype AmbiguousQName = AmbQ { unAmbQ :: [QName] } deriving (Eq, Ord, Typeable, Data) -- | Check whether we are a projection pattern. class IsProjP a where isProjP :: a -> Maybe (ProjOrigin, AmbiguousQName) instance IsProjP a => IsProjP (Arg a) where isProjP = isProjP . unArg instance IsProjP a => IsProjP (Named n a) where isProjP = isProjP . namedThing -- | A module is anonymous if the qualification path ends in an underscore. isAnonymousModuleName :: ModuleName -> Bool isAnonymousModuleName (MName []) = False isAnonymousModuleName (MName ms) = isNoName $ last ms -- | Sets the ranges of the individual names in the module name to -- match those of the corresponding concrete names. If the concrete -- names are fewer than the number of module name name parts, then the -- initial name parts get the range 'noRange'. -- -- @C.D.E \`withRangesOf\` [A, B]@ returns @C.D.E@ but with ranges set -- as follows: -- -- * @C@: 'noRange'. -- -- * @D@: the range of @A@. -- -- * @E@: the range of @B@. -- -- Precondition: The number of module name name parts has to be at -- least as large as the length of the list. withRangesOf :: ModuleName -> [C.Name] -> ModuleName MName ms `withRangesOf` ns = if m < n then __IMPOSSIBLE__ else MName $ zipWith setRange (replicate (m - n) noRange ++ map getRange ns) ms where m = length ms n = length ns -- | Like 'withRangesOf', but uses the name parts (qualifier + name) -- of the qualified name as the list of concrete names. withRangesOfQ :: ModuleName -> C.QName -> ModuleName m `withRangesOfQ` q = m `withRangesOf` C.qnameParts q mnameFromList :: [Name] -> ModuleName mnameFromList = MName noModuleName :: ModuleName noModuleName = mnameFromList [] commonParentModule :: ModuleName -> ModuleName -> ModuleName commonParentModule m1 m2 = mnameFromList $ commonPrefix (mnameToList m1) (mnameToList m2) -- | Make a 'Name' from some kind of string. class MkName a where -- | The 'Range' sets the /definition site/ of the name, not the use site. mkName :: Range -> NameId -> a -> Name mkName_ :: NameId -> a -> Name mkName_ = mkName noRange instance MkName String where mkName r i s = Name i (C.Name noRange (C.stringNameParts s)) r noFixity' qnameToList :: QName -> [Name] qnameToList (QName m x) = mnameToList m ++ [x] qnameFromList :: [Name] -> QName qnameFromList [] = __IMPOSSIBLE__ qnameFromList xs = QName (mnameFromList $ init xs) (last xs) qnameToMName :: QName -> ModuleName qnameToMName = mnameFromList . qnameToList mnameToQName :: ModuleName -> QName mnameToQName = qnameFromList . mnameToList showQNameId :: QName -> String showQNameId q = show ns ++ "@" ++ show m where is = map nameId $ mnameToList (qnameModule q) ++ [qnameName q] ns = [ n | NameId n _ <- is ] m = head [ m | NameId _ m <- is ] -- | Turn a qualified name into a concrete name. This should only be used as a -- fallback when looking up the right concrete name in the scope fails. qnameToConcrete :: QName -> C.QName qnameToConcrete (QName m x) = foldr C.Qual (C.QName $ nameConcrete x) $ map nameConcrete $ mnameToList m mnameToConcrete :: ModuleName -> C.QName mnameToConcrete (MName []) = __IMPOSSIBLE__ -- C.QName C.noName_ -- should never happen? mnameToConcrete (MName xs) = foldr C.Qual (C.QName $ last cs) $ init cs where cs = map nameConcrete xs -- | Computes the 'TopLevelModuleName' corresponding to the given -- module name, which is assumed to represent a top-level module name. -- -- Precondition: The module name must be well-formed. toTopLevelModuleName :: ModuleName -> C.TopLevelModuleName toTopLevelModuleName (MName []) = __IMPOSSIBLE__ toTopLevelModuleName (MName ms) = C.TopLevelModuleName (getRange ms) $ map (C.nameToRawName . nameConcrete) ms qualifyM :: ModuleName -> ModuleName -> ModuleName qualifyM m1 m2 = mnameFromList $ mnameToList m1 ++ mnameToList m2 qualifyQ :: ModuleName -> QName -> QName qualifyQ m x = qnameFromList $ mnameToList m ++ qnameToList x qualify :: ModuleName -> Name -> QName qualify = QName -- | Convert a 'Name' to a 'QName' (add no module name). qualify_ :: Name -> QName qualify_ = qualify noModuleName -- | Is the name an operator? isOperator :: QName -> Bool isOperator q = C.isOperator (nameConcrete (qnameName q)) isSubModuleOf :: ModuleName -> ModuleName -> Bool isSubModuleOf x y = xs /= ys && isPrefixOf ys xs where xs = mnameToList x ys = mnameToList y isInModule :: QName -> ModuleName -> Bool isInModule q m = mnameToList m `isPrefixOf` qnameToList q -- | Get the next version of the concrete name. For instance, @nextName "x" = "x₁"@. -- The name must not be a 'NoName'. nextName :: Name -> Name nextName x = x { nameConcrete = C.Name noRange $ nextSuf ps } where C.Name _ ps = nameConcrete x -- NoName cannot appear here nextSuf [C.Id s] = [C.Id $ nextStr s] nextSuf [C.Id s, C.Hole] = [C.Id $ nextStr s, C.Hole] nextSuf (p : ps) = p : nextSuf ps nextSuf [] = __IMPOSSIBLE__ nextStr s = case suffixView s of (s0, suf) -> addSuffix s0 (nextSuffix suf) ------------------------------------------------------------------------ -- * Important instances: Eq, Ord, Hashable -- -- For the identity and comparing of names, only the 'NameId' matters! ------------------------------------------------------------------------ instance Eq Name where (==) = (==) `on` nameId instance Ord Name where compare = compare `on` nameId instance Hashable Name where {-# INLINE hashWithSalt #-} hashWithSalt salt = hashWithSalt salt . nameId instance Eq QName where (==) = (==) `on` qnameName instance Ord QName where compare = compare `on` qnameName instance Hashable QName where {-# INLINE hashWithSalt #-} hashWithSalt salt = hashWithSalt salt . qnameName ------------------------------------------------------------------------ -- * IsNoName instances (checking for "_") ------------------------------------------------------------------------ -- | An abstract name is empty if its concrete name is empty. instance IsNoName Name where isNoName = isNoName . nameConcrete instance NumHoles Name where numHoles = numHoles . nameConcrete instance NumHoles QName where numHoles = numHoles . qnameName -- | We can have an instance for ambiguous names as all share a common concrete name. instance NumHoles AmbiguousQName where numHoles (AmbQ qs) = numHoles $ fromMaybe __IMPOSSIBLE__ $ headMaybe qs ------------------------------------------------------------------------ -- * Show instances (only for debug printing!) -- -- | Use 'prettyShow' to print names to the user. ------------------------------------------------------------------------ -- deriving instance Show Name -- deriving instance Show ModuleName -- deriving instance Show QName deriving instance Show a => Show (QNamed a) deriving instance Show AmbiguousQName -- | Only use this @show@ function in debugging! To convert an -- abstract 'Name' into a string use @prettyShow@. instance Show Name where -- Andreas, 2014-10-02: Reverted to nice printing. -- Reason: I do not have time just now to properly fix the -- use of Show Name for pretty printing everywhere. -- But I want to push the fix for Issue 836 now. show = prettyShow -- | Only use this @show@ function in debugging! To convert an -- abstract 'ModuleName' into a string use @prettyShow@. instance Show ModuleName where show = prettyShow -- | Only use this @show@ function in debugging! To convert an -- abstract 'QName' into a string use @prettyShow@. instance Show QName where show = prettyShow ------------------------------------------------------------------------ -- * Pretty instances ------------------------------------------------------------------------ instance Pretty Name where pretty = pretty . nameConcrete instance Pretty ModuleName where pretty = hcat . punctuate (text ".") . map pretty . mnameToList instance Pretty QName where pretty = hcat . punctuate (text ".") . map pretty . qnameToList instance Pretty AmbiguousQName where pretty (AmbQ qs) = hcat $ punctuate (text " | ") $ map pretty qs instance Pretty a => Pretty (QNamed a) where pretty (QNamed a b) = pretty a <> text "." <> pretty b ------------------------------------------------------------------------ -- * Range instances ------------------------------------------------------------------------ -- ** HasRange instance HasRange Name where getRange = getRange . nameConcrete instance HasRange ModuleName where getRange (MName []) = noRange getRange (MName xs) = getRange xs instance HasRange QName where getRange q = getRange (qnameModule q, qnameName q) -- | The range of an @AmbiguousQName@ is the range of any of its -- disambiguations (they are the same concrete name). instance HasRange AmbiguousQName where getRange (AmbQ []) = noRange getRange (AmbQ (c:_)) = getRange c -- ** SetRange instance SetRange Name where setRange r x = x { nameConcrete = setRange r $ nameConcrete x } instance SetRange QName where setRange r q = q { qnameModule = setRange r $ qnameModule q , qnameName = setRange r $ qnameName q } instance SetRange ModuleName where setRange r (MName ns) = MName (zipWith setRange rs ns) where -- Put the range only on the last name. Otherwise -- we get overlapping jump-to-definition links for all -- the parts (See #2666). rs = replicate (length ns - 1) noRange ++ [r] -- ** KillRange instance KillRange Name where killRange (Name a b c d) = (killRange4 Name a b c d) { nameBindingSite = c } -- Andreas, 2017-07-25, issue #2649 -- Preserve the nameBindingSite for error message. -- -- Older remarks: -- -- Andreas, 2014-03-30 -- An experiment: what happens if we preserve -- the range of the binding site, but kill all -- other ranges before serialization? -- -- Andreas, Makoto, 2014-10-18 AIM XX -- Kill all ranges in signature, including nameBindingSite. instance KillRange ModuleName where killRange (MName xs) = MName $ killRange xs instance KillRange QName where killRange (QName a b) = killRange2 QName a b -- killRange q = q { qnameModule = killRange $ qnameModule q -- , qnameName = killRange $ qnameName q -- } instance KillRange AmbiguousQName where killRange (AmbQ xs) = AmbQ $ killRange xs ------------------------------------------------------------------------ -- * Sized instances ------------------------------------------------------------------------ instance Sized QName where size = size . qnameToList instance Sized ModuleName where size = size . mnameToList ------------------------------------------------------------------------ -- * NFData instances ------------------------------------------------------------------------ -- | The range is not forced. instance NFData Name where rnf (Name _ a _ b) = rnf a `seq` rnf b instance NFData QName where rnf (QName a b) = rnf a `seq` rnf b instance NFData ModuleName where rnf (MName a) = rnf a Agda-2.5.3/src/full/Agda/Syntax/Parser/0000755000000000000000000000000013154613124015652 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Syntax/Parser/Tokens.hs0000644000000000000000000000656513154613124017465 0ustar0000000000000000module Agda.Syntax.Parser.Tokens ( Token(..) , Keyword(..) , layoutKeywords , Symbol(..) ) where import Agda.Syntax.Literal (Literal) import Agda.Syntax.Position data Keyword = KwLet | KwIn | KwWhere | KwData | KwCoData | KwPostulate | KwMutual | KwAbstract | KwPrivate | KwInstance | KwOverlap | KwOpen | KwImport | KwModule | KwPrimitive | KwMacro | KwInfix | KwInfixL | KwInfixR | KwWith | KwRewrite | KwSet | KwProp | KwForall | KwRecord | KwConstructor | KwField | KwInductive | KwCoInductive | KwEta | KwNoEta | KwHiding | KwUsing | KwRenaming | KwTo | KwPublic | KwOPTIONS | KwBUILTIN | KwLINE | KwCOMPILED_DATA | KwCOMPILED_TYPE | KwCOMPILED | KwCOMPILED_EXPORT | KwHASKELL | KwFOREIGN | KwCOMPILE | KwCOMPILED_JS | KwCOMPILED_UHC | KwCOMPILED_DATA_UHC | KwIMPORT | KwIMPORT_UHC | KwIMPOSSIBLE | KwSTATIC | KwINJECTIVE | KwINLINE | KwETA | KwNO_TERMINATION_CHECK | KwTERMINATING | KwNON_TERMINATING | KwMEASURE | KwDISPLAY | KwREWRITE | KwQuoteGoal | KwQuoteContext | KwQuote | KwQuoteTerm | KwUnquote | KwUnquoteDecl | KwUnquoteDef | KwSyntax | KwPatternSyn | KwTactic | KwCATCHALL | KwNO_POSITIVITY_CHECK | KwPOLARITY deriving (Eq, Show) layoutKeywords :: [Keyword] layoutKeywords = [ KwLet, KwWhere, KwPostulate, KwMutual, KwAbstract, KwPrivate, KwInstance, KwMacro, KwPrimitive, KwField ] data Symbol = SymDot | SymSemi | SymVirtualSemi | SymBar | SymColon | SymArrow | SymEqual | SymLambda | SymUnderscore | SymQuestionMark | SymAs | SymOpenParen | SymCloseParen | SymOpenIdiomBracket | SymCloseIdiomBracket | SymDoubleOpenBrace | SymDoubleCloseBrace | SymOpenBrace | SymCloseBrace | SymOpenVirtualBrace | SymCloseVirtualBrace | SymOpenPragma | SymClosePragma | SymEllipsis | SymDotDot | SymEndComment -- ^ A misplaced end-comment "-}". deriving (Eq, Show) data Token -- Keywords = TokKeyword Keyword Interval -- Identifiers and operators | TokId (Interval, String) | TokQId [(Interval, String)] -- Non-empty namespace. The intervals for -- "A.B.x" correspond to "A.", "B." and "x". -- Literals | TokLiteral Literal -- Special symbols | TokSymbol Symbol Interval -- Other tokens | TokString (Interval, String) -- arbitrary string, used in pragmas | TokSetN (Interval, Integer) | TokTeX (Interval, String) | TokComment (Interval, String) | TokDummy -- Dummy token to make Happy not complain -- about overlapping cases. | TokEOF deriving (Eq, Show) instance HasRange Token where getRange (TokKeyword _ i) = getRange i getRange (TokId (i, _)) = getRange i getRange (TokQId iss) = getRange (map fst iss) getRange (TokLiteral lit) = getRange lit getRange (TokSymbol _ i) = getRange i getRange (TokString (i, _)) = getRange i getRange (TokSetN (i, _)) = getRange i getRange (TokTeX (i, _)) = getRange i getRange (TokComment (i, _)) = getRange i getRange TokDummy = noRange getRange TokEOF = noRange Agda-2.5.3/src/full/Agda/Syntax/Parser/Parser.y0000644000000000000000000024071713154613124017313 0ustar0000000000000000{ {-# LANGUAGE TupleSections #-} {-| The parser is generated by Happy (). - - Ideally, ranges should be as precise as possible, to get messages that - emphasize precisely the faulting term(s) upon error. - - However, interactive highlighting is only applied at the end of each - mutual block, keywords are only highlighted once (see - `TypeChecking.Rules.Decl'). So if the ranges of two declarations - interleave, one must ensure that keyword ranges are not included in - the intersection. (Otherwise they are uncolored by the interactive - highlighting.) - -} module Agda.Syntax.Parser.Parser ( moduleParser , moduleNameParser , exprParser , exprWhereParser , tokensParser , splitOnDots -- only used by the internal test-suite ) where import Control.Monad import Data.Char import Data.Functor import Data.List import Data.Maybe import Data.Monoid import qualified Data.Traversable as T import Debug.Trace import Agda.Syntax.Position hiding (tests) import Agda.Syntax.Parser.Monad import Agda.Syntax.Parser.Lexer import Agda.Syntax.Parser.Tokens import Agda.Syntax.Concrete as C import Agda.Syntax.Concrete.Pretty () import Agda.Syntax.Common import Agda.Syntax.Fixity import Agda.Syntax.Notation import Agda.Syntax.Literal import Agda.TypeChecking.Positivity.Occurrence hiding (tests) import Agda.Utils.Either hiding (tests) import Agda.Utils.Hash import Agda.Utils.List ( spanJust, chopWhen ) import Agda.Utils.Monad import Agda.Utils.Pretty import Agda.Utils.Singleton import Agda.Utils.Tuple import Agda.Utils.Impossible #include "undefined.h" } %name tokensParser Tokens %name exprParser Expr %name exprWhereParser ExprWhere %name moduleParser File %name moduleNameParser ModuleName %name funclauseParser FunClause %tokentype { Token } %monad { Parser } %lexer { lexer } { TokEOF } %expect 1 -- shift/reduce for \ x y z -> foo = bar -- shifting means it'll parse as \ x y z -> (foo = bar) rather than -- (\ x y z -> foo) = bar -- This is a trick to get rid of shift/reduce conflicts arising because we want -- to parse things like "m >>= \x -> k x". See the Expr rule for more -- information. %nonassoc LOWEST %nonassoc '->' %token 'abstract' { TokKeyword KwAbstract $$ } 'codata' { TokKeyword KwCoData $$ } 'coinductive' { TokKeyword KwCoInductive $$ } 'constructor' { TokKeyword KwConstructor $$ } 'data' { TokKeyword KwData $$ } 'eta-equality' { TokKeyword KwEta $$ } 'field' { TokKeyword KwField $$ } 'forall' { TokKeyword KwForall $$ } 'hiding' { TokKeyword KwHiding $$ } 'import' { TokKeyword KwImport $$ } 'in' { TokKeyword KwIn $$ } 'inductive' { TokKeyword KwInductive $$ } 'infix' { TokKeyword KwInfix $$ } 'infixl' { TokKeyword KwInfixL $$ } 'infixr' { TokKeyword KwInfixR $$ } 'instance' { TokKeyword KwInstance $$ } 'overlap' { TokKeyword KwOverlap $$ } 'let' { TokKeyword KwLet $$ } 'macro' { TokKeyword KwMacro $$ } 'module' { TokKeyword KwModule $$ } 'mutual' { TokKeyword KwMutual $$ } 'no-eta-equality' { TokKeyword KwNoEta $$ } 'open' { TokKeyword KwOpen $$ } 'pattern' { TokKeyword KwPatternSyn $$ } 'postulate' { TokKeyword KwPostulate $$ } 'primitive' { TokKeyword KwPrimitive $$ } 'private' { TokKeyword KwPrivate $$ } 'Prop' { TokKeyword KwProp $$ } 'public' { TokKeyword KwPublic $$ } 'quote' { TokKeyword KwQuote $$ } 'quoteContext' { TokKeyword KwQuoteContext $$ } 'quoteGoal' { TokKeyword KwQuoteGoal $$ } 'quoteTerm' { TokKeyword KwQuoteTerm $$ } 'record' { TokKeyword KwRecord $$ } 'renaming' { TokKeyword KwRenaming $$ } 'rewrite' { TokKeyword KwRewrite $$ } 'Set' { TokKeyword KwSet $$ } 'syntax' { TokKeyword KwSyntax $$ } 'tactic' { TokKeyword KwTactic $$ } 'to' { TokKeyword KwTo $$ } 'unquote' { TokKeyword KwUnquote $$ } 'unquoteDecl' { TokKeyword KwUnquoteDecl $$ } 'unquoteDef' { TokKeyword KwUnquoteDef $$ } 'using' { TokKeyword KwUsing $$ } 'where' { TokKeyword KwWhere $$ } 'with' { TokKeyword KwWith $$ } 'BUILTIN' { TokKeyword KwBUILTIN $$ } 'CATCHALL' { TokKeyword KwCATCHALL $$ } 'COMPILED' { TokKeyword KwCOMPILED $$ } 'COMPILED_DATA' { TokKeyword KwCOMPILED_DATA $$ } 'COMPILED_DATA_UHC' { TokKeyword KwCOMPILED_DATA_UHC $$ } 'COMPILED_EXPORT' { TokKeyword KwCOMPILED_EXPORT $$ } 'COMPILED_JS' { TokKeyword KwCOMPILED_JS $$ } 'COMPILED_TYPE' { TokKeyword KwCOMPILED_TYPE $$ } 'COMPILED_UHC' { TokKeyword KwCOMPILED_UHC $$ } 'DISPLAY' { TokKeyword KwDISPLAY $$ } 'ETA' { TokKeyword KwETA $$ } 'HASKELL' { TokKeyword KwHASKELL $$ } 'IMPORT' { TokKeyword KwIMPORT $$ } 'IMPORT_UHC' { TokKeyword KwIMPORT_UHC $$ } 'FOREIGN' { TokKeyword KwFOREIGN $$ } 'COMPILE' { TokKeyword KwCOMPILE $$ } 'IMPOSSIBLE' { TokKeyword KwIMPOSSIBLE $$ } 'INJECTIVE' { TokKeyword KwINJECTIVE $$ } 'INLINE' { TokKeyword KwINLINE $$ } 'MEASURE' { TokKeyword KwMEASURE $$ } 'NO_TERMINATION_CHECK' { TokKeyword KwNO_TERMINATION_CHECK $$ } 'NO_POSITIVITY_CHECK' { TokKeyword KwNO_POSITIVITY_CHECK $$ } 'NON_TERMINATING' { TokKeyword KwNON_TERMINATING $$ } 'OPTIONS' { TokKeyword KwOPTIONS $$ } 'POLARITY' { TokKeyword KwPOLARITY $$ } 'REWRITE' { TokKeyword KwREWRITE $$ } 'STATIC' { TokKeyword KwSTATIC $$ } 'TERMINATING' { TokKeyword KwTERMINATING $$ } setN { TokSetN $$ } tex { TokTeX $$ } comment { TokComment $$ } '...' { TokSymbol SymEllipsis $$ } '..' { TokSymbol SymDotDot $$ } '.' { TokSymbol SymDot $$ } ';' { TokSymbol SymSemi $$ } ':' { TokSymbol SymColon $$ } '=' { TokSymbol SymEqual $$ } '_' { TokSymbol SymUnderscore $$ } '?' { TokSymbol SymQuestionMark $$ } '->' { TokSymbol SymArrow $$ } '\\' { TokSymbol SymLambda $$ } '@' { TokSymbol SymAs $$ } '|' { TokSymbol SymBar $$ } '(' { TokSymbol SymOpenParen $$ } ')' { TokSymbol SymCloseParen $$ } '(|' { TokSymbol SymOpenIdiomBracket $$ } '|)' { TokSymbol SymCloseIdiomBracket $$ } '{{' { TokSymbol SymDoubleOpenBrace $$ } '}}' { TokSymbol SymDoubleCloseBrace $$ } '{' { TokSymbol SymOpenBrace $$ } '}' { TokSymbol SymCloseBrace $$ } -- ':{' { TokSymbol SymColonBrace $$ } vopen { TokSymbol SymOpenVirtualBrace $$ } vclose { TokSymbol SymCloseVirtualBrace $$ } vsemi { TokSymbol SymVirtualSemi $$ } '{-#' { TokSymbol SymOpenPragma $$ } '#-}' { TokSymbol SymClosePragma $$ } id { TokId $$ } q_id { TokQId $$ } string { TokString $$ } literal { TokLiteral $$ } %% {-------------------------------------------------------------------------- Parsing the token stream. Used by the TeX compiler. --------------------------------------------------------------------------} -- Parse a list of tokens. Tokens :: { [Token] } Tokens : TokensR { reverse $1 } -- Happy is much better at parsing left recursive grammars (constant -- stack size vs. linear stack size for right recursive). TokensR :: { [Token] } TokensR : TokensR Token { $2 : $1 } | { [] } -- Parse single token. Token :: { Token } Token : 'abstract' { TokKeyword KwAbstract $1 } | 'codata' { TokKeyword KwCoData $1 } | 'coinductive' { TokKeyword KwCoInductive $1 } | 'constructor' { TokKeyword KwConstructor $1 } | 'data' { TokKeyword KwData $1 } | 'eta-equality' { TokKeyword KwEta $1 } | 'field' { TokKeyword KwField $1 } | 'forall' { TokKeyword KwForall $1 } | 'hiding' { TokKeyword KwHiding $1 } | 'import' { TokKeyword KwImport $1 } | 'in' { TokKeyword KwIn $1 } | 'inductive' { TokKeyword KwInductive $1 } | 'infix' { TokKeyword KwInfix $1 } | 'infixl' { TokKeyword KwInfixL $1 } | 'infixr' { TokKeyword KwInfixR $1 } | 'instance' { TokKeyword KwInstance $1 } | 'overlap' { TokKeyword KwOverlap $1 } | 'let' { TokKeyword KwLet $1 } | 'macro' { TokKeyword KwMacro $1 } | 'module' { TokKeyword KwModule $1 } | 'mutual' { TokKeyword KwMutual $1 } | 'no-eta-equality' { TokKeyword KwNoEta $1 } | 'open' { TokKeyword KwOpen $1 } | 'pattern' { TokKeyword KwPatternSyn $1 } | 'postulate' { TokKeyword KwPostulate $1 } | 'primitive' { TokKeyword KwPrimitive $1 } | 'private' { TokKeyword KwPrivate $1 } | 'Prop' { TokKeyword KwProp $1 } | 'public' { TokKeyword KwPublic $1 } | 'quote' { TokKeyword KwQuote $1 } | 'quoteContext' { TokKeyword KwQuoteContext $1 } | 'quoteGoal' { TokKeyword KwQuoteGoal $1 } | 'quoteTerm' { TokKeyword KwQuoteTerm $1 } | 'record' { TokKeyword KwRecord $1 } | 'renaming' { TokKeyword KwRenaming $1 } | 'rewrite' { TokKeyword KwRewrite $1 } | 'Set' { TokKeyword KwSet $1 } | 'syntax' { TokKeyword KwSyntax $1 } | 'tactic' { TokKeyword KwTactic $1 } | 'to' { TokKeyword KwTo $1 } | 'unquote' { TokKeyword KwUnquote $1 } | 'unquoteDecl' { TokKeyword KwUnquoteDecl $1 } | 'unquoteDef' { TokKeyword KwUnquoteDef $1 } | 'using' { TokKeyword KwUsing $1 } | 'where' { TokKeyword KwWhere $1 } | 'with' { TokKeyword KwWith $1 } | 'BUILTIN' { TokKeyword KwBUILTIN $1 } | 'CATCHALL' { TokKeyword KwCATCHALL $1 } | 'COMPILED' { TokKeyword KwCOMPILED $1 } | 'COMPILED_DATA' { TokKeyword KwCOMPILED_DATA $1 } | 'COMPILED_DATA_UHC' { TokKeyword KwCOMPILED_DATA_UHC $1 } | 'COMPILED_EXPORT' { TokKeyword KwCOMPILED_EXPORT $1 } | 'COMPILED_JS' { TokKeyword KwCOMPILED_JS $1 } | 'COMPILED_TYPE' { TokKeyword KwCOMPILED_TYPE $1 } | 'COMPILED_UHC' { TokKeyword KwCOMPILED_UHC $1 } | 'DISPLAY' { TokKeyword KwDISPLAY $1 } | 'ETA' { TokKeyword KwETA $1 } | 'HASKELL' { TokKeyword KwHASKELL $1 } | 'IMPORT' { TokKeyword KwIMPORT $1 } | 'IMPORT_UHC' { TokKeyword KwIMPORT_UHC $1 } | 'FOREIGN' { TokKeyword KwFOREIGN $1 } | 'COMPILE' { TokKeyword KwCOMPILE $1 } | 'IMPOSSIBLE' { TokKeyword KwIMPOSSIBLE $1 } | 'INJECTIVE' { TokKeyword KwINJECTIVE $1 } | 'INLINE' { TokKeyword KwINLINE $1 } | 'MEASURE' { TokKeyword KwMEASURE $1 } | 'NO_TERMINATION_CHECK' { TokKeyword KwNO_TERMINATION_CHECK $1 } | 'NO_POSITIVITY_CHECK' { TokKeyword KwNO_POSITIVITY_CHECK $1 } | 'NON_TERMINATING' { TokKeyword KwNON_TERMINATING $1 } | 'OPTIONS' { TokKeyword KwOPTIONS $1 } | 'POLARITY' { TokKeyword KwPOLARITY $1 } | 'REWRITE' { TokKeyword KwREWRITE $1 } | 'STATIC' { TokKeyword KwSTATIC $1 } | 'TERMINATING' { TokKeyword KwTERMINATING $1 } | setN { TokSetN $1 } | tex { TokTeX $1 } | comment { TokComment $1 } | '...' { TokSymbol SymEllipsis $1 } | '..' { TokSymbol SymDotDot $1 } | '.' { TokSymbol SymDot $1 } | ';' { TokSymbol SymSemi $1 } | ':' { TokSymbol SymColon $1 } | '=' { TokSymbol SymEqual $1 } | '_' { TokSymbol SymUnderscore $1 } | '?' { TokSymbol SymQuestionMark $1 } | '->' { TokSymbol SymArrow $1 } | '\\' { TokSymbol SymLambda $1 } | '@' { TokSymbol SymAs $1 } | '|' { TokSymbol SymBar $1 } | '(' { TokSymbol SymOpenParen $1 } | ')' { TokSymbol SymCloseParen $1 } | '(|' { TokSymbol SymOpenIdiomBracket $1 } | '|)' { TokSymbol SymCloseIdiomBracket $1 } | '{{' { TokSymbol SymDoubleOpenBrace $1 } | '}}' { TokSymbol SymDoubleCloseBrace $1 } | '{' { TokSymbol SymOpenBrace $1 } | '}' { TokSymbol SymCloseBrace $1 } | vopen { TokSymbol SymOpenVirtualBrace $1 } | vclose { TokSymbol SymCloseVirtualBrace $1 } | vsemi { TokSymbol SymVirtualSemi $1 } | '{-#' { TokSymbol SymOpenPragma $1 } | '#-}' { TokSymbol SymClosePragma $1 } | id { TokId $1 } | q_id { TokQId $1 } | string { TokString $1 } | literal { TokLiteral $1 } {-------------------------------------------------------------------------- Top level --------------------------------------------------------------------------} File :: { ([Pragma], [Declaration]) } File : vopen TopLevel maybe_vclose { takeOptionsPragmas $2 } maybe_vclose :: { () } maybe_vclose : {- empty -} { () } | vclose { () } {-------------------------------------------------------------------------- Meta rules --------------------------------------------------------------------------} -- The first token in a file decides the indentation of the top-level layout -- block. Or not. It will if we allow the top-level module to be omitted. -- topen : {- empty -} {% pushCurrentContext } {- A layout block might have to be closed by a parse error. Example: let x = e in e' Here the 'let' starts a layout block which should end before the 'in'. The problem is that the lexer doesn't know this, so there is no virtual close brace. However when the parser sees the 'in' there will be a parse error. This is our cue to close the layout block. -} close :: { () } close : vclose { () } | error {% popContext } -- You can use concrete semi colons in a layout block started with a virtual -- brace, so we don't have to distinguish between the two semi colons. You can't -- use a virtual semi colon in a block started by a concrete brace, but this is -- simply because the lexer will not generate virtual semis in this case. semi :: { Interval } semi : ';' { $1 } | vsemi { $1 } -- Enter the 'imp_dir' lex state, where we can parse the keyword 'to'. beginImpDir :: { () } beginImpDir : {- empty -} {% pushLexState imp_dir } {-------------------------------------------------------------------------- Helper rules --------------------------------------------------------------------------} -- An integer. Used in fixity declarations. Int :: { Integer } Int : literal {% case $1 of { LitNat _ i -> return i; _ -> fail $ "Expected integer" } } {-------------------------------------------------------------------------- Names --------------------------------------------------------------------------} -- A name is really a sequence of parts, but the lexer just sees it as a -- string, so we have to do the translation here. Id :: { Name } Id : id {% mkName $1 } -- Space separated list of one or more identifiers. SpaceIds :: { [Name] } SpaceIds : Id SpaceIds { $1 : $2 } | Id { [$1] } -- When looking for a double closed brace, we accept either a single token '}}' -- (which is what the unicode character "RIGHT WHITE CURLY BRACKET" is -- postprocessed into in LexActions.hs), but also two consecutive tokens '}' -- (which a string '}}' is lexed to). This small hack allows us to keep -- "record { a = record { }}" working. In the second case, we check that the two -- tokens '}' are immediately consecutive. DoubleCloseBrace :: { Range } DoubleCloseBrace : '}}' { getRange $1 } | '}' '}' {% if posPos (fromJust (rEnd' (getRange $2))) - posPos (fromJust (rStart' (getRange $1))) > 2 then parseErrorAt (fromJust (rStart' (getRange $2))) "Expecting '}}', found separated '}'s." else return $ getRange ($1, $2) } -- A possibly dotted identifier. MaybeDottedId :: { Arg Name } MaybeDottedId : '..' Id { setRelevance NonStrict $ defaultArg $2 } | '.' Id { setRelevance Irrelevant $ defaultArg $2 } | Id { defaultArg $1 } -- Space separated list of one or more possibly dotted identifiers. MaybeDottedIds :: { [Arg Name] } MaybeDottedIds : MaybeDottedId MaybeDottedIds { $1 : $2 } | MaybeDottedId { [$1] } -- Space separated list of one or more identifiers, some of which may -- be surrounded by braces or dotted. ArgIds :: { [Arg Name] } ArgIds : MaybeDottedId ArgIds { $1 : $2 } | MaybeDottedId { [$1] } | '{{' MaybeDottedIds DoubleCloseBrace ArgIds { map makeInstance $2 ++ $4 } | '{{' MaybeDottedIds DoubleCloseBrace { map makeInstance $2 } | '{' MaybeDottedIds '}' ArgIds { map hide $2 ++ $4 } | '{' MaybeDottedIds '}' { map hide $2 } | '.' '{' SpaceIds '}' ArgIds { map (hide . setRelevance Irrelevant . defaultArg) $3 ++ $5 } | '.' '{' SpaceIds '}' { map (hide . setRelevance Irrelevant . defaultArg) $3 } | '.' '{{' SpaceIds DoubleCloseBrace ArgIds { map (makeInstance . setRelevance Irrelevant . defaultArg) $3 ++ $5 } | '.' '{{' SpaceIds DoubleCloseBrace { map (makeInstance . setRelevance Irrelevant . defaultArg) $3 } | '..' '{' SpaceIds '}' ArgIds { map (hide . setRelevance NonStrict . defaultArg) $3 ++ $5 } | '..' '{' SpaceIds '}' { map (hide . setRelevance NonStrict . defaultArg) $3 } | '..' '{{' SpaceIds DoubleCloseBrace ArgIds { map (makeInstance . setRelevance NonStrict . defaultArg) $3 ++ $5 } | '..' '{{' SpaceIds DoubleCloseBrace { map (makeInstance . setRelevance NonStrict . defaultArg) $3 } QId :: { QName } QId : q_id {% mkQName $1 } | Id { QName $1 } -- A module name is just a qualified name ModuleName :: { QName } ModuleName : QId { $1 } -- A binding variable. Can be '_' BId :: { Name } BId : Id { $1 } | '_' { Name (getRange $1) [Hole] } {- UNUSED -- A binding variable. Can be '_' MaybeDottedBId :: { (Relevance, Name) } MaybeDottedBId : BId { (Relevant , $1) } | '.' BId { (Irrelevant, $2) } | '..' BId { (NonStrict, $2) } -} -- Space separated list of binding identifiers. Used in fixity -- declarations infixl 100 + - SpaceBIds :: { [Name] } SpaceBIds : BId SpaceBIds { $1 : $2 } | BId { [$1] } {- DOES PRODUCE REDUCE/REDUCE CONFLICTS! -- Space-separated list of binding identifiers. Used in dependent -- function spaces: (x y z : Nat) -> ... -- (Used to be comma-separated; hence the name) -- QUESTION: Should this be replaced by SpaceBIds above? --CommaBIds :: { [(Relevance,Name)] } CommaBIds :: { [Name] } CommaBIds : CommaBIds BId { $1 ++ [$2] } -- SWITCHING DOES NOT HELP | BId { [$1] } -} -- Space-separated list of binding identifiers. Used in dependent -- function spaces: (x y z : Nat) -> ... -- (Used to be comma-separated; hence the name) -- QUESTION: Should this be replaced by SpaceBIds above? -- Andreas, 2011-04-07 the trick avoids reduce/reduce conflicts -- when parsing (x y z : A) -> B -- at point (x y it is not clear whether x y is an application or -- a variable list. We could be parsing (x y z) -> B -- with ((x y) z) being a type. CommaBIds :: { [Name] } CommaBIds : CommaBIdAndAbsurds { case $1 of Left ns -> ns Right _ -> fail $ "expected sequence of bound identifiers, not absurd pattern" } CommaBIdAndAbsurds :: { Either [Name] [Expr] } CommaBIdAndAbsurds : Application {% let getName :: Expr -> Maybe Name getName (Ident (QName x)) = Just x getName (Underscore r _) = Just (Name r [Hole]) getName _ = Nothing isAbsurd :: Expr -> Bool isAbsurd (Absurd _) = True isAbsurd (HiddenArg _ (Named _ e)) = isAbsurd e isAbsurd (InstanceArg _ (Named _ e)) = isAbsurd e isAbsurd (Paren _ expr) = isAbsurd expr isAbsurd (RawApp _ exprs) = any isAbsurd exprs isAbsurd _ = False in if any isAbsurd $1 then return $ Right $1 else case mapM getName $1 of Just good -> return $ Left good Nothing -> fail $ "expected sequence of bound identifiers" } -- Parse a sequence of identifiers, including hiding info. -- Does not include instance arguments. -- E.g. x {y z} _ {v} -- To be used in typed bindings, like (x {y z} _ {v} : Nat). BIdsWithHiding :: { [WithHiding Name] } BIdsWithHiding : Application {% let -- interpret an expression as name getName :: Expr -> Maybe Name getName (Ident (QName x)) = Just x getName (Underscore r _) = Just (Name r [Hole]) getName _ = Nothing getNames :: Expr -> Maybe [Name] getNames (RawApp _ es) = mapM getName es getNames e = singleton `fmap` getName e -- interpret an expression as name or list of hidden names getName1 :: Expr -> Maybe [WithHiding Name] getName1 (Ident (QName x)) = Just [WithHiding NotHidden x] getName1 (Underscore r _) = Just [WithHiding NotHidden $ Name r [Hole]] getName1 (HiddenArg _ (Named Nothing e)) = map (WithHiding Hidden) `fmap` getNames e getName1 _ = Nothing in case mapM getName1 $1 of Just good -> return $ concat good Nothing -> fail $ "expected sequence of possibly hidden bound identifiers" } -- Space separated list of strings in a pragma. PragmaStrings :: { [String] } PragmaStrings : {- empty -} { [] } | string PragmaStrings { snd $1 : $2 } PragmaString :: { String } PragmaString : string { snd $1 } Strings :: { [(Interval, String)] } Strings : {- empty -} { [] } | string Strings { $1 : $2 } ForeignCode :: { [(Interval, String)] } ForeignCode : {- empty -} { [] } | string ForeignCode { $1 : $2 } | '{-#' ForeignCode '#-}' ForeignCode { [($1, "{-#")] ++ $2 ++ [($3, "#-}")] ++ $4 } PragmaName :: { Name } PragmaName : string {% mkName $1 } PragmaQName :: { QName } PragmaQName : string {% pragmaQName $1 } -- Issue 2125. WAS: string {% fmap QName (mkName $1) } PragmaQNames :: { [QName] } PragmaQNames : Strings {% mapM pragmaQName $1 } {-------------------------------------------------------------------------- Expressions (terms and types) --------------------------------------------------------------------------} {- Expressions. You might expect lambdas and lets to appear in the first expression category (lowest precedence). The reason they don't is that we want to parse things like m >>= \x -> k x This will leads to a conflict in the following case m >>= \x -> k x >>= \y -> k' y At the second '>>=' we can either shift or reduce. We solve this problem using Happy's precedence directives. The rule 'Expr -> Expr1' (which is the rule you shouldn't use to reduce when seeing '>>=') is given LOWEST precedence. The terminals '->' and op (which is what you should shift) is given higher precedence. -} -- Top level: Function types. Expr :: { Expr } Expr : TeleArrow Expr { Pi $1 $2 } | Application3 '->' Expr { Fun (getRange ($1,$2,$3)) (RawApp (getRange $1) $1) $3 } | Expr1 '=' Expr { Equal (getRange ($1, $2, $3)) $1 $3 } | Expr1 %prec LOWEST { $1 } -- Level 1: Application Expr1 :: { Expr } Expr1 : WithExprs {% case $1 of { [e] -> return e ; e : es -> return $ WithApp (fuseRange e es) e es ; [] -> fail "impossible: empty with expressions" } } WithExprs :: { [Expr] } WithExprs : Application3 '|' WithExprs { RawApp (getRange $1) $1 : $3 } | Application { [RawApp (getRange $1) $1] } Application :: { [Expr] } Application : Expr2 { [$1] } | Expr3 Application { $1 : $2 } -- Level 2: Lambdas and lets Expr2 :: { Expr } Expr2 : '\\' LamBindings Expr { Lam (getRange ($1,$2,$3)) $2 $3 } | ExtendedOrAbsurdLam { $1 } | 'forall' ForallBindings Expr { forallPi $2 $3 } | 'let' Declarations 'in' Expr { Let (getRange ($1,$2,$3,$4)) $2 $4 } | Expr3 { $1 } | 'quoteGoal' Id 'in' Expr { QuoteGoal (getRange ($1,$2,$3,$4)) $2 $4 } | 'tactic' Application3 { Tactic (getRange ($1, $2)) (RawApp (getRange $2) $2) [] } | 'tactic' Application3 '|' WithExprs { Tactic (getRange ($1, $2, $3, $4)) (RawApp (getRange $2) $2) $4 } ExtendedOrAbsurdLam :: { Expr } ExtendedOrAbsurdLam : '\\' '{' LamClauses '}' { ExtendedLam (getRange ($1,$2,$3,$4)) (reverse $3) } | '\\' 'where' vopen LamWhereClauses close { ExtendedLam (getRange ($1, $2, $4)) (reverse $4) } | '\\' AbsurdLamBindings {% case $2 of Left (bs, h) -> if null bs then return $ AbsurdLam r h else return $ Lam r bs (AbsurdLam r h) where r = fuseRange $1 bs Right es -> do -- it is of the form @\ { p1 ... () }@ p <- exprToLHS (RawApp (getRange es) es); return $ ExtendedLam (fuseRange $1 es) [(p [] [], AbsurdRHS, NoWhere, False)] } Application3 :: { [Expr] } Application3 : Expr3 { [$1] } | Expr3 Application3 { $1 : $2 } -- Christian Sattler, 2017-08-04, issue #2671 -- We allow empty lists of expressions for the LHS of extended lambda clauses. -- I am not sure what Application3 is otherwise used for, so I keep the -- original type and create this copy solely for extended lambda clauses. Application3PossiblyEmpty :: { [Expr] } Application3PossiblyEmpty : { [] } | Expr3 Application3PossiblyEmpty { $1 : $2 } -- Level 3: Atoms Expr3Curly :: { Expr } Expr3Curly : '{' Expr '}' { HiddenArg (getRange ($1,$2,$3)) (maybeNamed $2) } | '{' '}' { let r = fuseRange $1 $2 in HiddenArg r $ unnamed $ Absurd r } Expr3NoCurly :: { Expr } Expr3NoCurly : QId { Ident $1 } | literal { Lit $1 } | '?' { QuestionMark (getRange $1) Nothing } | '_' { Underscore (getRange $1) Nothing } | 'Prop' { Prop (getRange $1) } | 'Set' { Set (getRange $1) } | 'quote' { Quote (getRange $1) } | 'quoteTerm' { QuoteTerm (getRange $1) } | 'quoteContext' { QuoteContext (getRange $1) } | 'unquote' { Unquote (getRange $1) } | setN { SetN (getRange (fst $1)) (snd $1) } | '{{' Expr DoubleCloseBrace { InstanceArg (getRange ($1,$2,$3)) (maybeNamed $2) } | '(' Expr ')' { Paren (getRange ($1,$2,$3)) $2 } | '(|' Expr '|)' { IdiomBrackets (getRange ($1,$2,$3)) $2 } | '(' ')' { Absurd (fuseRange $1 $2) } | '{{' DoubleCloseBrace { let r = fuseRange $1 $2 in InstanceArg r $ unnamed $ Absurd r } | Id '@' Expr3 { As (getRange ($1,$2,$3)) $1 $3 } | '.' Expr3 { Dot (fuseRange $1 $2) $2 } | 'record' '{' RecordAssignments '}' { Rec (getRange ($1,$2,$3,$4)) $3 } | 'record' Expr3NoCurly '{' FieldAssignments '}' { RecUpdate (getRange ($1,$2,$3,$4,$5)) $2 $4 } Expr3 :: { Expr } Expr3 : Expr3Curly { $1 } | Expr3NoCurly { $1 } RecordAssignments :: { RecordAssignments } RecordAssignments : {- empty -} { [] } | RecordAssignments1 { $1 } RecordAssignments1 :: { RecordAssignments } RecordAssignments1 : RecordAssignment { [$1] } | RecordAssignment ';' RecordAssignments1 { $1 : $3 } RecordAssignment :: { RecordAssignment } RecordAssignment : FieldAssignment { Left $1 } | ModuleAssignment { Right $1 } ModuleAssignment :: { ModuleAssignment } ModuleAssignment : ModuleName OpenArgs ImportDirective { ModuleAssignment $1 $2 $3 } FieldAssignments :: { [FieldAssignment] } FieldAssignments : {- empty -} { [] } | FieldAssignments1 { $1 } FieldAssignments1 :: { [FieldAssignment] } FieldAssignments1 : FieldAssignment { [$1] } | FieldAssignment ';' FieldAssignments1 { $1 : $3 } FieldAssignment :: { FieldAssignment } FieldAssignment : Id '=' Expr { FieldAssignment $1 $3 } {-------------------------------------------------------------------------- Bindings --------------------------------------------------------------------------} -- "Delta ->" to avoid conflict between Delta -> Gamma and Delta -> A. TeleArrow :: { Telescope } TeleArrow : Telescope1 '->' { $1 } Telescope1 :: { Telescope } Telescope1 : TypedBindingss { {-TeleBind-} $1 } TypedBindingss :: { [TypedBindings] } TypedBindingss : TypedBindings TypedBindingss { $1 : $2 } | TypedBindings { [$1] } -- A typed binding is either (x1 .. xn : A) or {y1 .. ym : B} -- Andreas, 2011-04-07: or .(x1 .. xn : A) or .{y1 .. ym : B} -- Andreas, 2011-04-27: or ..(x1 .. xn : A) or ..{y1 .. ym : B} TypedBindings :: { TypedBindings } TypedBindings : '.' '(' TBindWithHiding ')' { setRange (getRange ($2,$3,$4)) $ setRelevance Irrelevant $3 } | '.' '{' TBind '}' { setRange (getRange ($2,$3,$4)) $ setHiding Hidden $ setRelevance Irrelevant $3 } | '.' '{{' TBind DoubleCloseBrace { setRange (getRange ($2,$3,$4)) $ makeInstance $ setRelevance Irrelevant $3 } | '..' '(' TBindWithHiding ')' { setRange (getRange ($2,$3,$4)) $ setRelevance NonStrict $3 } | '..' '{' TBind '}' { setRange (getRange ($2,$3,$4)) $ setHiding Hidden $ setRelevance NonStrict $3 } | '..' '{{' TBind DoubleCloseBrace { setRange (getRange ($2,$3,$4)) $ makeInstance $ setRelevance NonStrict $3 } | '(' TBindWithHiding ')' { setRange (getRange ($1,$2,$3)) $2 } | '{{' TBind DoubleCloseBrace { setRange (getRange ($1,$2,$3)) $ makeInstance $2 } | '{' TBind '}' { setRange (getRange ($1,$2,$3)) $ setHiding Hidden $2 } | '(' Open ')' { tLet (getRange ($1,$3)) $2 } | '(' 'let' Declarations ')' { tLet (getRange ($1,$4)) $3 } -- x1 .. xn : A -- x1 .. xn :{i1 i2 ..} A TBind :: { TypedBindings } TBind : CommaBIds ':' Expr { let r = getRange ($1,$2,$3) -- the range is approximate only for TypedBindings in TypedBindings r $ defaultArg $ TBind r (map (pure . mkBoundName_) $1) $3 } -- | Colors are not yet allowed in the syntax. -- | CommaBIds ':{' Colors '}' Expr { ( $3, TBind (getRange ($1,$2,$3,$4,$5)) (map mkBoundName_ $1) $5 ) } {- Colors :: { [Color] } Colors : QId Colors { Ident $1 : $2 } | QId { [Ident $1] } -} -- x {y z} _ {v} : A TBindWithHiding :: { TypedBindings } TBindWithHiding : BIdsWithHiding ':' Expr { let r = getRange ($1,$2,$3) -- the range is approximate only for TypedBindings in TypedBindings r $ defaultArg $ TBind r (map (fmap mkBoundName_) $1) $3 } -- A non-empty sequence of lambda bindings. LamBindings :: { [LamBinding] } LamBindings : LamBinds '->' {% case reverse $1 of Left _ : _ -> parseError "Absurd lambda cannot have a body." _ : _ -> return [ b | Right b <- $1 ] [] -> parsePanic "Empty LamBinds" } AbsurdLamBindings :: { Either ([LamBinding], Hiding) [Expr] } AbsurdLamBindings : LamBindsAbsurd {% case $1 of Left lb -> case reverse lb of Right _ : _ -> parseError "Missing body for lambda" Left h : _ -> return $ Left ([ b | Right b <- init lb], h) _ -> parseError "Unsupported variant of lambda" Right es -> return $ Right es } -- absurd lambda is represented by @Left hiding@ LamBinds :: { [Either Hiding LamBinding] } LamBinds : DomainFreeBinding LamBinds { map Right $1 ++ $2 } | TypedBindings LamBinds { Right (DomainFull $1) : $2 } | DomainFreeBinding { map Right $1 } | TypedBindings { [Right $ DomainFull $1] } | '(' ')' { [Left NotHidden] } | '{' '}' { [Left Hidden] } | '{{' DoubleCloseBrace { [Left (Instance NoOverlap)] } -- Like LamBinds, but could also parse an absurd LHS of an extended lambda @{ p1 ... () }@ LamBindsAbsurd :: { Either [Either Hiding LamBinding] [Expr] } LamBindsAbsurd : DomainFreeBinding LamBinds { Left $ map Right $1 ++ $2 } | TypedBindings LamBinds { Left $ Right (DomainFull $1) : $2 } | DomainFreeBindingAbsurd { case $1 of Left lb -> Left $ map Right lb Right es -> Right es } | TypedBindings { Left [Right $ DomainFull $1] } | '(' ')' { Left [Left NotHidden] } | '{' '}' { Left [Left Hidden] } | '{{' DoubleCloseBrace { Left [Left (Instance NoOverlap)] } -- FNF, 2011-05-05: No where clauses in extended lambdas for now NonAbsurdLamClause :: { (LHS,RHS,WhereClause,Bool) } NonAbsurdLamClause : Application3PossiblyEmpty '->' Expr {% do p <- exprToLHS (RawApp (getRange $1) $1) ; return (p [] [], RHS $3, NoWhere, False) } | CatchallPragma Application3PossiblyEmpty '->' Expr {% do p <- exprToLHS (RawApp (getRange $2) $2) ; return (p [] [], RHS $4, NoWhere, True) } AbsurdLamClause :: { (LHS,RHS,WhereClause,Bool) } AbsurdLamClause -- FNF, 2011-05-09: By being more liberal here, we avoid shift/reduce and reduce/reduce errors. -- Later stages such as scope checking will complain if we let something through which we should not : Application {% do p <- exprToLHS (RawApp (getRange $1) $1); return (p [] [], AbsurdRHS, NoWhere, False) } | CatchallPragma Application {% do p <- exprToLHS (RawApp (getRange $2) $2); return (p [] [], AbsurdRHS, NoWhere, True) } LamClause :: { (LHS,RHS,WhereClause,Bool) } LamClause : NonAbsurdLamClause { $1 } | AbsurdLamClause { $1 } -- Parses all extended lambda clauses except for a single absurd clause, which is taken care of -- in AbsurdLambda LamClauses :: { [(LHS,RHS,WhereClause,Bool)] } LamClauses : LamClauses semi LamClause { $3 : $1 } | AbsurdLamClause semi LamClause { [$3, $1] } | NonAbsurdLamClause { [$1] } -- | {- empty -} { [] } -- Parses all extended lambda clauses including a single absurd clause. For λ -- where this is not taken care of in AbsurdLambda LamWhereClauses :: { [(LHS,RHS,WhereClause,Bool)] } LamWhereClauses : LamWhereClauses semi LamClause { $3 : $1 } | LamClause { [$1] } ForallBindings :: { [LamBinding] } ForallBindings : TypedUntypedBindings1 '->' { $1 } -- A non-empty sequence of possibly untyped bindings. TypedUntypedBindings1 :: { [LamBinding] } TypedUntypedBindings1 : DomainFreeBinding TypedUntypedBindings1 { $1 ++ $2 } | TypedBindings TypedUntypedBindings1 { DomainFull $1 : $2 } | DomainFreeBinding { $1 } | TypedBindings { [DomainFull $1] } -- A possibly empty sequence of possibly untyped bindings. -- This is used as telescope in data and record decls. TypedUntypedBindings :: { [LamBinding] } TypedUntypedBindings : DomainFreeBinding TypedUntypedBindings { $1 ++ $2 } | TypedBindings TypedUntypedBindings { DomainFull $1 : $2 } | { [] } -- A domain free binding is either x or {x1 .. xn} DomainFreeBinding :: { [LamBinding] } DomainFreeBinding : DomainFreeBindingAbsurd { case $1 of Left lbs -> lbs Right _ -> fail "expected sequence of bound identifiers, not absurd pattern" } -- A domain free binding is either x or {x1 .. xn} DomainFreeBindingAbsurd :: { Either [LamBinding] [Expr]} DomainFreeBindingAbsurd : BId { Left [DomainFree defaultArgInfo $ mkBoundName_ $1] } | '.' BId { Left [DomainFree (setRelevance Irrelevant $ defaultArgInfo) $ mkBoundName_ $2] } | '..' BId { Left [DomainFree (setRelevance NonStrict $ defaultArgInfo) $ mkBoundName_ $2] } | '{' CommaBIdAndAbsurds '}' { mapLeft (map (DomainFree (setHiding Hidden $ defaultArgInfo) . mkBoundName_)) $2 } | '{{' CommaBIds DoubleCloseBrace { Left $ map (DomainFree (makeInstance $ defaultArgInfo) . mkBoundName_) $2 } | '.' '{' CommaBIds '}' { Left $ map (DomainFree (setHiding Hidden $ setRelevance Irrelevant $ defaultArgInfo) . mkBoundName_) $3 } | '.' '{{' CommaBIds DoubleCloseBrace { Left $ map (DomainFree (makeInstance $ setRelevance Irrelevant $ defaultArgInfo) . mkBoundName_) $3 } | '..' '{' CommaBIds '}' { Left $ map (DomainFree (setHiding Hidden $ setRelevance NonStrict $ defaultArgInfo) . mkBoundName_) $3 } | '..' '{{' CommaBIds DoubleCloseBrace { Left $ map (DomainFree (makeInstance $ setRelevance NonStrict $ defaultArgInfo) . mkBoundName_) $3 } {-------------------------------------------------------------------------- Modules and imports --------------------------------------------------------------------------} -- Import directives ImportDirective :: { ImportDirective } ImportDirective : ImportDirectives {% mergeImportDirectives $1 } ImportDirectives :: { [ImportDirective] } ImportDirectives : ImportDirective1 ImportDirectives { $1 : $2 } | {- empty -} { [] } ImportDirective1 :: { ImportDirective } : 'public' { defaultImportDir { importDirRange = getRange $1, publicOpen = True } } | Using { defaultImportDir { importDirRange = snd $1, using = fst $1 } } | Hiding { defaultImportDir { importDirRange = snd $1, hiding = fst $1 } } | RenamingDir { defaultImportDir { importDirRange = snd $1, impRenaming = fst $1 } } Using :: { (Using, Range) } Using : 'using' '(' CommaImportNames ')' { (Using $3 , getRange ($1,$2,$3,$4)) } -- using can have an empty list Hiding :: { ([ImportedName], Range) } Hiding : 'hiding' '(' CommaImportNames ')' { ($3 , getRange ($1,$2,$3,$4)) } -- if you want to hide nothing that's fine, isn't it? RenamingDir :: { ([Renaming] , Range) } RenamingDir : 'renaming' '(' Renamings ')' { ($3 , getRange ($1,$2,$3,$4)) } | 'renaming' '(' ')' { ([] , getRange ($1,$2,$3)) } -- Renamings of the form 'x to y' Renamings :: { [Renaming] } Renamings : Renaming ';' Renamings { $1 : $3 } | Renaming { [$1] } Renaming :: { Renaming } Renaming : ImportName_ 'to' Id { Renaming $1 (setImportedName $1 $3) (getRange $2) } -- We need a special imported name here, since we have to trigger -- the imp_dir state exactly one token before the 'to' ImportName_ :: { ImportedName } ImportName_ : beginImpDir Id { ImportedName $2 } | 'module' beginImpDir Id { ImportedModule $3 } ImportName :: { ImportedName } ImportName : Id { ImportedName $1 } | 'module' Id { ImportedModule $2 } -- Actually semi-colon separated CommaImportNames :: { [ImportedName] } CommaImportNames : {- empty -} { [] } | CommaImportNames1 { $1 } CommaImportNames1 :: { [ImportedName] } CommaImportNames1 : ImportName { [$1] } | ImportName ';' CommaImportNames1 { $1 : $3 } {-------------------------------------------------------------------------- Function clauses --------------------------------------------------------------------------} -- A left hand side of a function clause. We parse it as an expression, and -- then check that it is a valid left hand side. LHS :: { LHS } LHS : Expr1 RewriteEquations WithExpressions {% exprToLHS $1 >>= \p -> return (p $2 $3) } | '...' WithPats RewriteEquations WithExpressions { Ellipsis (getRange ($1,$2,$3,$4)) $2 $3 $4 } WithPats :: { [Pattern] } WithPats : {- empty -} { [] } | '|' Application3 WithPats {% exprToPattern (RawApp (getRange $2) $2) >>= \p -> return (p : $3) } WithExpressions :: { [Expr] } WithExpressions : {- empty -} { [] } | 'with' Expr { case $2 of { WithApp _ e es -> e : es; e -> [e] } } RewriteEquations :: { [Expr] } RewriteEquations : {- empty -} { [] } | 'rewrite' Expr1 { case $2 of { WithApp _ e es -> e : es; e -> [e] } } -- Where clauses are optional. WhereClause :: { WhereClause } WhereClause : {- empty -} { NoWhere } | 'where' Declarations0 { AnyWhere $2 } | 'module' Id 'where' Declarations0 { SomeWhere $2 PublicAccess $4 } | 'module' Underscore 'where' Declarations0 { SomeWhere $2 PublicAccess $4 } ExprWhere :: { ExprWhere } ExprWhere : Expr WhereClause { ExprWhere $1 $2 } {-------------------------------------------------------------------------- Different kinds of declarations --------------------------------------------------------------------------} -- Top-level definitions. Declaration :: { [Declaration] } Declaration : Fields { $1 } | FunClause { $1 } | Data { [$1] } | DataSig { [$1] } -- lone data type signature in mutual block | Record { [$1] } | RecordSig { [$1] } -- lone record signature in mutual block | Infix { [$1] } | Mutual { [$1] } | Abstract { [$1] } | Private { [$1] } | Instance { [$1] } | Macro { [$1] } | Postulate { [$1] } | Primitive { [$1] } | Open { $1 } -- | Import { [$1] } | ModuleMacro { [$1] } | Module { [$1] } | Pragma { [$1] } | Syntax { [$1] } | PatternSyn { [$1] } | UnquoteDecl { [$1] } {-------------------------------------------------------------------------- Individual declarations --------------------------------------------------------------------------} -- Type signatures of the form "n1 n2 n3 ... : Type", with at least -- one bound name. TypeSigs :: { [Declaration] } TypeSigs : SpaceIds ':' Expr { map (\ x -> TypeSig defaultArgInfo x $3) $1 } -- A variant of TypeSigs where any sub-sequence of names can be marked -- as hidden or irrelevant using braces and dots: -- {n1 .n2} n3 .n4 {n5} .{n6 n7} ... : Type. ArgTypeSigs :: { [Arg Declaration] } ArgTypeSigs : ArgIds ':' Expr { map (fmap (\ x -> TypeSig defaultArgInfo x $3)) $1 } | 'overlap' ArgIds ':' Expr {% let setOverlap x = case getHiding x of Instance _ -> return $ makeInstance' YesOverlap x _ -> parseErrorAt (fromJust $ rStart' $ getRange $1) "The 'overlap' keyword only applies to instance fields (fields marked with {{ }})" in T.traverse (setOverlap . fmap (\ x -> TypeSig defaultArgInfo x $4)) $2 } | 'instance' ArgTypeSignatures { let setInstance (TypeSig info x t) = TypeSig (makeInstance info) x t setInstance _ = __IMPOSSIBLE__ in map (fmap setInstance) $2 } -- Function declarations. The left hand side is parsed as an expression to allow -- declarations like 'x::xs ++ ys = e', when '::' has higher precedence than '++'. -- FunClause also handle possibly dotted type signatures. FunClause :: { [Declaration] } FunClause : LHS RHS WhereClause {% funClauseOrTypeSigs $1 $2 $3 } RHS :: { RHSOrTypeSigs } RHS : '=' Expr { JustRHS (RHS $2) } | ':' Expr { TypeSigsRHS $2 } | {- empty -} { JustRHS AbsurdRHS } -- Data declaration. Can be local. Data :: { Declaration } Data : 'data' Id TypedUntypedBindings ':' Expr 'where' Declarations0 { Data (getRange ($1,$2,$3,$4,$5,$6,$7)) Inductive $2 $3 (Just $5) $7 } | 'codata' Id TypedUntypedBindings ':' Expr 'where' Declarations0 { Data (getRange ($1,$2,$3,$4,$5,$6,$7)) CoInductive $2 $3 (Just $5) $7 } -- New cases when we already had a DataSig. Then one can omit the sort. | 'data' Id TypedUntypedBindings 'where' Declarations0 { Data (getRange ($1,$2,$3,$4,$5)) Inductive $2 $3 Nothing $5 } | 'codata' Id TypedUntypedBindings 'where' Declarations0 { Data (getRange ($1,$2,$3,$4,$5)) CoInductive $2 $3 Nothing $5 } -- Data type signature. Found in mutual blocks. DataSig :: { Declaration } DataSig : 'data' Id TypedUntypedBindings ':' Expr { DataSig (getRange ($1,$2,$3,$4,$5)) Inductive $2 $3 $5 } -- Andreas, 2012-03-16: The Expr3NoCurly instead of Id in everything -- following 'record' is to remove the (harmless) shift/reduce conflict -- introduced by record update expressions. -- Record declarations. Record :: { Declaration } Record : 'record' Expr3NoCurly TypedUntypedBindings ':' Expr 'where' RecordDeclarations {% exprToName $2 >>= \ n -> let ((x,y,z),ds) = $7 in return $ Record (getRange ($1,$2,$3,$4,$5,$6,$7)) n x y z $3 (Just $5) ds } | 'record' Expr3NoCurly TypedUntypedBindings 'where' RecordDeclarations {% exprToName $2 >>= \ n -> let ((x,y,z),ds) = $5 in return $ Record (getRange ($1,$2,$3,$4,$5)) n x y z $3 Nothing ds } -- Record type signature. In mutual blocks. RecordSig :: { Declaration } RecordSig : 'record' Expr3NoCurly TypedUntypedBindings ':' Expr {% exprToName $2 >>= \ n -> return $ RecordSig (getRange ($1,$2,$3,$4,$5)) n $3 $5 } -- Declaration of record constructor name. RecordConstructorName :: { (Name, IsInstance) } RecordConstructorName : 'constructor' Id { ($2, NotInstanceDef) } | 'instance' vopen 'constructor' Id vclose { ($4, InstanceDef) } -- Fixity declarations. Infix :: { Declaration } Infix : 'infix' Int SpaceBIds { Infix (Fixity (getRange ($1,$3)) (Related $2) NonAssoc) $3 } | 'infixl' Int SpaceBIds { Infix (Fixity (getRange ($1,$3)) (Related $2) LeftAssoc) $3 } | 'infixr' Int SpaceBIds { Infix (Fixity (getRange ($1,$3)) (Related $2) RightAssoc) $3 } -- Field declarations. Fields :: { [Declaration] } Fields : 'field' ArgTypeSignatures { let inst i = case getHiding i of Instance _ -> InstanceDef _ -> NotInstanceDef toField (Arg info (TypeSig info' x t)) = Field (inst info') x (Arg info t) in map toField $2 } -- Mutually recursive declarations. Mutual :: { Declaration } Mutual : 'mutual' Declarations { Mutual (fuseRange $1 $2) $2 } -- Abstract declarations. Abstract :: { Declaration } Abstract : 'abstract' Declarations { Abstract (fuseRange $1 $2) $2 } -- Private can only appear on the top-level (or rather the module level). Private :: { Declaration } Private : 'private' Declarations { Private (fuseRange $1 $2) UserWritten $2 } -- Instance declarations. Instance :: { Declaration } Instance : 'instance' Declarations { InstanceB (fuseRange $1 $2) $2 } -- Macro declarations. Macro :: { Declaration } Macro : 'macro' Declarations { Macro (fuseRange $1 $2) $2 } -- Postulates. Postulate :: { Declaration } Postulate : 'postulate' Declarations { Postulate (fuseRange $1 $2) $2 } -- Primitives. Can only contain type signatures. Primitive :: { Declaration } Primitive : 'primitive' TypeSignatures { Primitive (fuseRange $1 $2) $2 } -- Unquoting declarations. UnquoteDecl :: { Declaration } UnquoteDecl : 'unquoteDecl' '=' Expr { UnquoteDecl (fuseRange $1 $3) [] $3 } | 'unquoteDecl' SpaceIds '=' Expr { UnquoteDecl (fuseRange $1 $4) $2 $4 } | 'unquoteDef' SpaceIds '=' Expr { UnquoteDef (fuseRange $1 $4) $2 $4 } -- Syntax declaration (To declare eg. mixfix binders) Syntax :: { Declaration } Syntax : 'syntax' Id HoleNames '=' SimpleIds {% case $2 of Name _ [_] -> case mkNotation $3 (map rangedThing $5) of Left err -> parseError $ "Malformed syntax declaration: " ++ err Right n -> return $ Syntax $2 n _ -> parseError "Syntax declarations are allowed only for simple names (without holes)" } -- Pattern synonyms. PatternSyn :: { Declaration } PatternSyn : 'pattern' Id PatternSynArgs '=' Expr {% do p <- exprToPattern $5 return (PatternSyn (getRange ($1,$2,$3,$4,$5)) $2 $3 p) } PatternSynArgs :: { [Arg Name] } PatternSynArgs : {- empty -} { [] } | LamBinds {% patternSynArgs $1 } SimpleIds :: { [RString] } SimpleIds : SimpleId { [$1] } | SimpleIds SimpleId {$1 ++ [$2]} HoleNames :: { [NamedArg HoleName] } HoleNames : HoleName { [$1] } | HoleNames HoleName {$1 ++ [$2]} HoleName :: { NamedArg HoleName } HoleName : SimpleTopHole { defaultNamedArg $1 } | '{' SimpleHole '}' { hide $ defaultNamedArg $2 } | '{{' SimpleHole '}}' { makeInstance $ defaultNamedArg $2 } | '{' SimpleId '=' SimpleHole '}' { hide $ defaultArg $ named $2 $4 } | '{{' SimpleId '=' SimpleHole '}}' { makeInstance $ defaultArg $ named $2 $4 } SimpleTopHole :: { HoleName } SimpleTopHole : SimpleId { ExprHole (rangedThing $1) } | '(' '\\' SimpleId '->' SimpleId ')' { LambdaHole (rangedThing $3) (rangedThing $5) } | '(' '\\' '_' '->' SimpleId ')' { LambdaHole "_" (rangedThing $5) } SimpleHole :: { HoleName } SimpleHole : SimpleId { ExprHole (rangedThing $1) } | '\\' SimpleId '->' SimpleId { LambdaHole (rangedThing $2) (rangedThing $4) } | '\\' '_' '->' SimpleId { LambdaHole "_" (rangedThing $4) } -- Variable name hole to be implemented later. -- Discard the interval. SimpleId :: { RString } SimpleId : id { Ranged (getRange $ fst $1) (stringToRawName $ snd $1) } MaybeOpen :: { Maybe Range } MaybeOpen : 'open' { Just (getRange $1) } | {- empty -} { Nothing } -- Open Open :: { [Declaration] } Open : MaybeOpen 'import' ModuleName OpenArgs ImportDirective {% let { doOpen = maybe DontOpen (const DoOpen) $1 ; m = $3 ; es = $4 ; dir = $5 ; r = getRange (m, es, dir) ; mr = getRange m ; unique = hashString $ show $ (Nothing :: Maybe ()) <$ r -- turn range into unique id, but delete file path -- which is absolute and messes up suite of failing tests -- (different hashs on different installations) -- TODO: Don't use (insecure) hashes in this way. ; fresh = Name mr [ Id $ stringToRawName $ ".#" ++ show m ++ "-" ++ show unique ] ; impStm asR = Import mr m (Just (AsName fresh asR)) DontOpen defaultImportDir ; appStm m' es = let r = getRange (m, es) in Private r Inserted [ ModuleMacro r m' (SectionApp (getRange es) [] (RawApp (getRange es) (Ident (QName fresh) : es))) doOpen dir ] ; (initArgs, last2Args) = splitAt (length es - 2) es ; parseAsClause = case last2Args of { [ Ident (QName (Name asR [Id x])) , Ident (QName m') ] | rawNameToString x == "as" -> Just (asR, m') ; _ -> Nothing } } in case es of { [] -> return [Import mr m Nothing doOpen dir] ; _ | Just (asR, m') <- parseAsClause -> if null initArgs then return [ Import (getRange (m, asR, m', dir)) m (Just (AsName m' asR)) doOpen dir ] else return [ impStm asR, appStm m' initArgs ] -- Andreas, 2017-05-13, issue #2579 -- Nisse reports that importing with instantation but without open -- could be usefule for bringing instances into scope. -- -- | DontOpen <- doOpen -> parseErrorAt (fromJust $ rStart' $ getRange $2) "An import statement with module instantiation does not actually import the module. This statement achieves nothing. Either add the `open' keyword or bind the instantiated module with an `as' clause." | otherwise -> return [ impStm noRange , appStm (noName $ beginningOf $ getRange m) es ] } } |'open' ModuleName OpenArgs ImportDirective { let { m = $2 ; es = $3 ; dir = $4 ; r = getRange (m, es, dir) } in [ case es of { [] -> Open r m dir ; _ -> Private r Inserted [ ModuleMacro r (noName $ beginningOf $ getRange m) (SectionApp (getRange (m , es)) [] (RawApp (fuseRange m es) (Ident m : es))) DoOpen dir ] } ] } | 'open' ModuleName '{{' '...' DoubleCloseBrace ImportDirective { let r = getRange $2 in [ Private r Inserted [ ModuleMacro r (noName $ beginningOf $ getRange $2) (RecordModuleIFS r $2) DoOpen $6 ] ] } OpenArgs :: { [Expr] } OpenArgs : {- empty -} { [] } | Expr3 OpenArgs { $1 : $2 } ModuleApplication :: { [TypedBindings] -> Parser ModuleApplication } ModuleApplication : ModuleName '{{' '...' DoubleCloseBrace { (\ts -> if null ts then return $ RecordModuleIFS (getRange ($1,$2,$3,$4)) $1 else parseError "No bindings allowed for record module with non-canonical implicits" ) } | ModuleName OpenArgs { (\ts -> return $ SectionApp (getRange ($1, $2)) ts (RawApp (fuseRange $1 $2) (Ident $1 : $2)) ) } -- Module instantiation ModuleMacro :: { Declaration } ModuleMacro : 'module' ModuleName TypedUntypedBindings '=' ModuleApplication ImportDirective {% do { ma <- $5 (map addType $3) ; name <- ensureUnqual $2 ; return $ ModuleMacro (getRange ($1, $2, ma, $6)) name ma DontOpen $6 } } | 'open' 'module' Id TypedUntypedBindings '=' ModuleApplication ImportDirective {% do {ma <- $6 (map addType $4); return $ ModuleMacro (getRange ($1, $2, $3, ma, $7)) $3 ma DoOpen $7 } } -- Module Module :: { Declaration } Module : 'module' ModuleName TypedUntypedBindings 'where' Declarations0 { Module (getRange ($1,$2,$3,$4,$5)) $2 (map addType $3) $5 } | 'module' Underscore TypedUntypedBindings 'where' Declarations0 { Module (getRange ($1,$2,$3,$4,$5)) (QName $2) (map addType $3) $5 } Underscore :: { Name } Underscore : '_' { noName (getRange $1) } TopLevel :: { [Declaration] } TopLevel : TopDeclarations { figureOutTopLevelModule $1 } Pragma :: { Declaration } Pragma : DeclarationPragma { Pragma $1 } DeclarationPragma :: { Pragma } DeclarationPragma : BuiltinPragma { $1 } | RewritePragma { $1 } | CompiledPragma { $1 } | CompiledExportPragma { $1 } | CompiledDataPragma { $1 } | CompiledTypePragma { $1 } | CompiledJSPragma { $1 } | CompiledUHCPragma { $1 } | CompiledDataUHCPragma { $1 } | HaskellPragma { $1 } | CompilePragma { $1 } | ForeignPragma { $1 } | StaticPragma { $1 } | InjectivePragma { $1 } | InlinePragma { $1 } | ImportPragma { $1 } | ImportUHCPragma { $1 } | ImpossiblePragma { $1 } | TerminatingPragma { $1 } | NonTerminatingPragma { $1 } | NoTerminationCheckPragma { $1 } | MeasurePragma { $1 } | CatchallPragma { $1 } | DisplayPragma { $1 } | EtaPragma { $1 } | NoPositivityCheckPragma { $1 } | PolarityPragma { $1 } | OptionsPragma { $1 } -- Andreas, 2014-03-06 -- OPTIONS pragma not allowed everywhere, but don't give parse error. -- Give better error during type checking instead. OptionsPragma :: { Pragma } OptionsPragma : '{-#' 'OPTIONS' PragmaStrings '#-}' { OptionsPragma (getRange ($1,$2,$4)) $3 } BuiltinPragma :: { Pragma } BuiltinPragma : '{-#' 'BUILTIN' string PragmaQName '#-}' { BuiltinPragma (getRange ($1,$2,fst $3,$4,$5)) (snd $3) (Ident $4) } -- Extra rule to accept keword REWRITE also as built-in: | '{-#' 'BUILTIN' 'REWRITE' PragmaQName '#-}' { BuiltinPragma (getRange ($1,$2,$3,$4,$5)) "REWRITE" (Ident $4) } RewritePragma :: { Pragma } RewritePragma : '{-#' 'REWRITE' PragmaQNames '#-}' { RewritePragma (getRange ($1,$2,$3,$4)) $3 } CompiledPragma :: { Pragma } CompiledPragma : '{-#' 'COMPILED' PragmaQName PragmaStrings '#-}' { CompiledPragma (getRange ($1,$2,$3,$5)) $3 (unwords $4) } CompiledExportPragma :: { Pragma } CompiledExportPragma : '{-#' 'COMPILED_EXPORT' PragmaQName PragmaString '#-}' { CompiledExportPragma (getRange ($1,$2,$3,$5)) $3 $4 } CompiledTypePragma :: { Pragma } CompiledTypePragma : '{-#' 'COMPILED_TYPE' PragmaQName PragmaStrings '#-}' { CompiledTypePragma (getRange ($1,$2,$3,$5)) $3 (unwords $4) } CompiledDataPragma :: { Pragma } CompiledDataPragma : '{-#' 'COMPILED_DATA' PragmaQName string PragmaStrings '#-}' { CompiledDataPragma (getRange ($1,$2,$3,fst $4,$6)) $3 (snd $4) $5 } CompiledJSPragma :: { Pragma } CompiledJSPragma : '{-#' 'COMPILED_JS' PragmaQName PragmaStrings '#-}' { CompiledJSPragma (getRange ($1,$2,$3,$5)) $3 (unwords $4) } CompiledUHCPragma :: { Pragma } CompiledUHCPragma : '{-#' 'COMPILED_UHC' PragmaQName PragmaStrings '#-}' { CompiledUHCPragma (getRange ($1,$2,$3,$5)) $3 (unwords $4) } CompiledDataUHCPragma :: { Pragma } CompiledDataUHCPragma : '{-#' 'COMPILED_DATA_UHC' PragmaQName string PragmaStrings '#-}' { CompiledDataUHCPragma (getRange ($1,$2,$3,fst $4,$6)) $3 (snd $4) $5 } HaskellPragma :: { Pragma } HaskellPragma : '{-#' 'HASKELL' ForeignCode '#-}' { HaskellCodePragma (getRange ($1, $2, $4)) (recoverLayout $3) } ForeignPragma :: { Pragma } ForeignPragma : '{-#' 'FOREIGN' string ForeignCode '#-}' { ForeignPragma (getRange ($1, $2, fst $3, $5)) (snd $3) (recoverLayout $4) } CompilePragma :: { Pragma } CompilePragma : '{-#' 'COMPILE' string PragmaQName PragmaStrings '#-}' { CompilePragma (getRange ($1,$2,fst $3,$4,$6)) (snd $3) $4 (unwords $5) } StaticPragma :: { Pragma } StaticPragma : '{-#' 'STATIC' PragmaQName '#-}' { StaticPragma (getRange ($1,$2,$3,$4)) $3 } InlinePragma :: { Pragma } InlinePragma : '{-#' 'INLINE' PragmaQName '#-}' { InlinePragma (getRange ($1,$2,$3,$4)) $3 } InjectivePragma :: { Pragma } InjectivePragma : '{-#' 'INJECTIVE' PragmaQName '#-}' { InjectivePragma (getRange ($1,$2,$3,$4)) $3 } DisplayPragma :: { Pragma } DisplayPragma : '{-#' 'DISPLAY' string PragmaStrings '#-}' {% let (r, s) = $3 in parseDisplayPragma (fuseRange $1 $5) (iStart r) (unwords (s : $4)) } EtaPragma :: { Pragma } EtaPragma : '{-#' 'ETA' PragmaQName '#-}' { EtaPragma (getRange ($1,$2,$3,$4)) $3 } NoTerminationCheckPragma :: { Pragma } NoTerminationCheckPragma : '{-#' 'NO_TERMINATION_CHECK' '#-}' { TerminationCheckPragma (getRange ($1,$2,$3)) NoTerminationCheck } NonTerminatingPragma :: { Pragma } NonTerminatingPragma : '{-#' 'NON_TERMINATING' '#-}' { TerminationCheckPragma (getRange ($1,$2,$3)) NonTerminating } TerminatingPragma :: { Pragma } TerminatingPragma : '{-#' 'TERMINATING' '#-}' { TerminationCheckPragma (getRange ($1,$2,$3)) Terminating } MeasurePragma :: { Pragma } MeasurePragma : '{-#' 'MEASURE' PragmaName '#-}' { let r = getRange ($1, $2, $3, $4) in TerminationCheckPragma r (TerminationMeasure r $3) } CatchallPragma :: { Pragma } CatchallPragma : '{-#' 'CATCHALL' '#-}' { CatchallPragma (getRange ($1,$2,$3)) } ImportPragma :: { Pragma } ImportPragma : '{-#' 'IMPORT' string '#-}' {% let s = snd $3 in if validHaskellModuleName s then return $ ImportPragma (getRange ($1,$2,fst $3,$4)) s else parseError $ "Malformed module name: " ++ s ++ "." } ImportUHCPragma :: { Pragma } ImportUHCPragma : '{-#' 'IMPORT_UHC' string '#-}' {% let s = snd $3 in if validHaskellModuleName s then return $ ImportUHCPragma (getRange ($1,$2,fst $3,$4)) s else parseError $ "Malformed module name: " ++ s ++ "." } ImpossiblePragma :: { Pragma } : '{-#' 'IMPOSSIBLE' '#-}' { ImpossiblePragma (getRange ($1,$2,$3)) } NoPositivityCheckPragma :: { Pragma } NoPositivityCheckPragma : '{-#' 'NO_POSITIVITY_CHECK' '#-}' { NoPositivityCheckPragma (getRange ($1,$2,$3)) } PolarityPragma :: { Pragma } PolarityPragma : '{-#' 'POLARITY' PragmaName Polarities '#-}' { let (rs, occs) = unzip (reverse $4) in PolarityPragma (getRange ($1,$2,$3,rs,$5)) $3 occs } -- Possibly empty list of polarities. Reversed. Polarities :: { [(Range, Occurrence)] } Polarities : {- empty -} { [] } | Polarities Polarity { $2 : $1 } Polarity :: { (Range, Occurrence) } Polarity : string {% polarity $1 } {-------------------------------------------------------------------------- Sequences of declarations --------------------------------------------------------------------------} -- Non-empty list of type signatures, with several identifiers allowed -- for every signature. TypeSignatures :: { [TypeSignature] } TypeSignatures : vopen TypeSignatures1 close { reverse $2 } -- Inside the layout block. TypeSignatures1 :: { [TypeSignature] } TypeSignatures1 : TypeSignatures1 semi TypeSigs { reverse $3 ++ $1 } | TypeSigs { reverse $1 } -- A variant of TypeSignatures which uses ArgTypeSigs instead of -- TypeSigs. ArgTypeSignatures :: { [Arg TypeSignature] } ArgTypeSignatures : vopen ArgTypeSignatures1 close { reverse $2 } -- Inside the layout block. ArgTypeSignatures1 :: { [Arg TypeSignature] } ArgTypeSignatures1 : ArgTypeSignatures1 semi ArgTypeSigs { reverse $3 ++ $1 } | ArgTypeSigs { reverse $1 } -- Record declarations, including an optional record constructor name. RecordDeclarations :: { ((Maybe (Ranged Induction), Maybe Bool, Maybe (Name, IsInstance)), [Declaration]) } RecordDeclarations : vopen RecordDirectives close {% ((,) `fmap` verifyRecordDirectives $2 <*> pure []) } | vopen RecordDirectives semi Declarations1 close {% ((,) `fmap` verifyRecordDirectives $2 <*> pure $4) } | vopen Declarations1 close {% ((,) `fmap` verifyRecordDirectives [] <*> pure $2) } RecordDirectives :: { [RecordDirective] } RecordDirectives : { [] } | RecordDirectives semi RecordDirective { $3 : $1 } | RecordDirective { [$1] } RecordDirective :: { RecordDirective } RecordDirective : RecordConstructorName { Constructor $1 } | RecordInduction { Induction $1 } | RecordEta { Eta $1 } RecordEta :: { Ranged Bool } RecordEta : 'eta-equality' { Ranged (getRange $1) True } | 'no-eta-equality' { Ranged (getRange $1) False } -- Declaration of record as 'inductive' or 'coinductive'. RecordInduction :: { Ranged Induction } RecordInduction : 'inductive' { Ranged (getRange $1) Inductive } | 'coinductive' { Ranged (getRange $1) CoInductive } -- Arbitrary declarations Declarations :: { [Declaration] } Declarations : vopen Declarations1 close { $2 } -- Arbitrary declarations (possibly empty) Declarations0 :: { [Declaration] } Declarations0 : vopen close { [] } | Declarations { $1 } Declarations1 :: { [Declaration] } Declarations1 : Declaration semi Declarations1 { $1 ++ $3 } | Declaration { $1 } TopDeclarations :: { [Declaration] } TopDeclarations : {- empty -} { [] } | Declarations1 { $1 } { {-------------------------------------------------------------------------- Parsers --------------------------------------------------------------------------} -- | Parse the token stream. Used by the TeX compiler. tokensParser :: Parser [Token] -- | Parse an expression. Could be used in interactions. exprParser :: Parser Expr -- | Parse an expression followed by a where clause. Could be used in interactions. exprWhereParser :: Parser ExprWhere -- | Parse a module. moduleParser :: Parser Module {-------------------------------------------------------------------------- Happy stuff --------------------------------------------------------------------------} -- | Required by Happy. happyError :: Parser a happyError = parseError "Parse error" {-------------------------------------------------------------------------- Utility functions --------------------------------------------------------------------------} -- | Grab leading OPTIONS pragmas. takeOptionsPragmas :: [Declaration] -> ([Pragma], [Declaration]) takeOptionsPragmas = spanJust $ \ d -> case d of Pragma p@OptionsPragma{} -> Just p _ -> Nothing -- | Insert a top-level module if there is none. -- Also fix-up for the case the declarations in the top-level module -- are not indented (this is allowed as a special case). figureOutTopLevelModule :: [Declaration] -> [Declaration] figureOutTopLevelModule ds = case spanAllowedBeforeModule ds of -- Andreas 2016-02-01, issue #1388. -- We need to distinguish two additional cases. -- Case 1: Regular file layout: imports followed by one module. Nothing to do. (ds0, [ Module{} ]) -> ds -- Case 2: The declarations in the module are not indented. -- This is allowed for the top level module, and thus rectified here. (ds0, Module r m tel [] : ds2) -> ds0 ++ [Module r m tel ds2] -- Case 3: There is a module with indented declarations, -- followed by non-indented declarations. This should be a -- parse error and be reported later (see @toAbstract TopLevel{}@), -- thus, we do not do anything here. (ds0, Module r m tel ds1 : ds2) -> ds -- Gives parse error in scope checker. -- OLD code causing issue 1388: -- (ds0, Module r m tel ds1 : ds2) -> ds0 ++ [Module r m tel $ ds1 ++ ds2] -- Case 4: a top-level module declaration is missing. -- Andreas, 2017-01-01, issue #2229: -- Put everything (except OPTIONS pragmas) into an anonymous module. _ -> ds0 ++ [Module r (QName $ noName r) [] ds1] where (ds0, ds1) = (`span` ds) $ \case Pragma OptionsPragma{} -> True _ -> False -- Andreas, 2017-05-17, issue #2574. -- Since the module noName will act as jump target, it needs a range. -- We use the beginning of the file as beginning of the top level module. r = beginningOfFile $ getRange ds1 -- | Create a name from a string. mkName :: (Interval, String) -> Parser Name mkName (i, s) = do let xs = C.stringNameParts s mapM_ isValidId xs unless (alternating xs) $ fail $ "a name cannot contain two consecutive underscores" return $ Name (getRange i) xs where isValidId Hole = return () isValidId (Id y) = do let x = rawNameToString y case parse defaultParseFlags [0] (lexer return) x of ParseOk _ (TokId _) -> return () _ -> fail $ "in the name " ++ s ++ ", the part " ++ x ++ " is not valid" -- we know that there are no two Ids in a row alternating (Hole : Hole : _) = False alternating (_ : xs) = alternating xs alternating [] = True -- | Create a qualified name from a list of strings mkQName :: [(Interval, String)] -> Parser QName mkQName ss = do xs <- mapM mkName ss return $ foldr Qual (QName $ last xs) (init xs) -- | Create a qualified name from a string (used in pragmas). -- Range of each name component is range of whole string. -- TODO: precise ranges! pragmaQName :: (Interval, String) -> Parser QName pragmaQName (r, s) = do let ss = chopWhen (== '.') s mkQName $ map (r,) ss -- | Polarity parser. polarity :: (Interval, String) -> Parser (Range, Occurrence) polarity (i, s) = case s of "_" -> ret Unused "++" -> ret StrictPos "+" -> ret JustPos "-" -> ret JustNeg "*" -> ret Mixed _ -> fail $ "Not a valid polarity: " ++ s where ret x = return (getRange i, x) recoverLayout :: [(Interval, String)] -> String recoverLayout [] = "" recoverLayout xs@((i, _) : _) = go (iStart i) xs where c0 = posCol (iStart i) go cur [] = "" go cur ((i, s) : xs) = padding cur (iStart i) ++ s ++ go (iEnd i) xs padding Pn{ posLine = l1, posCol = c1 } Pn{ posLine = l2, posCol = c2 } | l1 < l2 = genericReplicate (l2 - l1) '\n' ++ genericReplicate (max 0 (c2 - c0)) ' ' | l1 == l2 = genericReplicate (c2 - c1) ' ' ensureUnqual :: QName -> Parser Name ensureUnqual (QName x) = return x ensureUnqual q@Qual{} = parseError' (rStart' $ getRange q) "Qualified name not allowed here" -- | Match a particular name. isName :: String -> (Interval, String) -> Parser () isName s (_,s') | s == s' = return () | otherwise = fail $ "expected " ++ s ++ ", found " ++ s' -- | Build a forall pi (forall x y z -> ...) forallPi :: [LamBinding] -> Expr -> Expr forallPi bs e = Pi (map addType bs) e -- | Build a telescoping let (let Ds) tLet :: Range -> [Declaration] -> TypedBindings tLet r = TypedBindings r . Arg defaultArgInfo . TLet r -- | Converts lambda bindings to typed bindings. addType :: LamBinding -> TypedBindings addType (DomainFull b) = b addType (DomainFree info x) = TypedBindings r $ Arg info $ TBind r [pure x] $ Underscore r Nothing where r = getRange x mergeImportDirectives :: [ImportDirective] -> Parser ImportDirective mergeImportDirectives is = do i <- foldl merge (return defaultImportDir) is verifyImportDirective i where merge mi i2 = do i1 <- mi let err = parseError' (rStart' $ getRange i2) "Cannot mix using and hiding module directives" return $ ImportDirective { importDirRange = fuseRange i1 i2 , using = mappend (using i1) (using i2) , hiding = hiding i1 ++ hiding i2 , impRenaming = impRenaming i1 ++ impRenaming i2 , publicOpen = publicOpen i1 || publicOpen i2 } -- | Check that an import directive doesn't contain repeated names verifyImportDirective :: ImportDirective -> Parser ImportDirective verifyImportDirective i = case filter ((>1) . length) $ group $ sort xs of [] -> return i yss -> let Just pos = rStart' $ getRange $ head $ concat yss in parseErrorAt pos $ "Repeated name" ++ s ++ " in import directive: " ++ concat (intersperse ", " $ map (show . head) yss) where s = case yss of [_] -> "" _ -> "s" where xs = names (using i) ++ hiding i ++ map renFrom (impRenaming i) names (Using xs) = xs names UseEverything = [] data RecordDirective = Induction (Ranged Induction) | Constructor (Name, IsInstance) | Eta (Ranged Bool) deriving (Eq,Show) verifyRecordDirectives :: [RecordDirective] -> Parser (Maybe (Ranged Induction), Maybe Bool, Maybe (Name, IsInstance)) verifyRecordDirectives xs | null rs = return (ltm is, ltm es, ltm cs) | otherwise = let Just pos = rStart' $ (head rs) in parseErrorAt pos $ "Repeated record directives at: \n" ++ intercalate "\n" (map show rs) where ltm :: [a] -> Maybe a ltm [] = Nothing ltm (x:xs) = Just x errorFromList [] = [] errorFromList [x] = [] errorFromList xs = map getRange xs rs = sort (concat ([errorFromList is, errorFromList es', errorFromList cs])) is = [ i | Induction i <- xs ] es' = [ i | Eta i <- xs ] es = map rangedThing es' cs = [ i | Constructor i <- xs ] -- | Breaks up a string into substrings. Returns every maximal -- subsequence of zero or more characters distinct from @'.'@. -- -- > splitOnDots "" == [""] -- > splitOnDots "foo.bar" == ["foo", "bar"] -- > splitOnDots ".foo.bar" == ["", "foo", "bar"] -- > splitOnDots "foo.bar." == ["foo", "bar", ""] -- > splitOnDots "foo..bar" == ["foo", "", "bar"] splitOnDots :: String -> [String] splitOnDots "" = [""] splitOnDots ('.' : s) = [] : splitOnDots s splitOnDots (c : s) = case splitOnDots s of p : ps -> (c : p) : ps -- | Returns 'True' iff the name is a valid Haskell (hierarchical) -- module name. validHaskellModuleName :: String -> Bool validHaskellModuleName = all ok . splitOnDots where -- Checks if a dot-less module name is well-formed. ok :: String -> Bool ok [] = False ok (c : s) = isUpper c && all (\c -> isLower c || c == '_' || isUpper c || generalCategory c == DecimalNumber || c == '\'') s {-------------------------------------------------------------------------- Patterns --------------------------------------------------------------------------} -- | Turn an expression into a left hand side. exprToLHS :: Expr -> Parser ([Expr] -> [Expr] -> LHS) exprToLHS e = case e of WithApp r e es -> LHS <$> exprToPattern e <*> mapM exprToPattern es _ -> LHS <$> exprToPattern e <*> return [] -- | Turn an expression into a pattern. Fails if the expression is not a -- valid pattern. exprToPattern :: Expr -> Parser Pattern exprToPattern e = do let Just pos = rStart' $ getRange e failure = parseErrorAt pos $ "Not a valid pattern: " ++ show e case e of Ident x -> return $ IdentP x App _ e1 e2 -> AppP <$> exprToPattern e1 <*> T.mapM (T.mapM exprToPattern) e2 Paren r e -> ParenP r <$> exprToPattern e Underscore r _ -> return $ WildP r Absurd r -> return $ AbsurdP r As r x e -> AsP r x <$> exprToPattern e Dot r (HiddenArg _ e) -> return $ HiddenP r $ fmap (DotP r UserWritten) e Dot r e -> return $ DotP r UserWritten e Lit l -> return $ LitP l HiddenArg r e -> HiddenP r <$> T.mapM exprToPattern e InstanceArg r e -> InstanceP r <$> T.mapM exprToPattern e RawApp r es -> RawAppP r <$> mapM exprToPattern es Quote r -> return $ QuoteP r Rec r es | Just fs <- mapM maybeLeft es -> do RecP r <$> T.mapM (T.mapM exprToPattern) fs _ -> failure opAppExprToPattern :: OpApp Expr -> Parser Pattern opAppExprToPattern (SyntaxBindingLambda _ _ _) = parseError "Syntax binding lambda cannot appear in a pattern" opAppExprToPattern (Ordinary e) = exprToPattern e -- | Turn an expression into a name. Fails if the expression is not a -- valid identifier. exprToName :: Expr -> Parser Name exprToName (Ident (QName x)) = return x exprToName e = let Just pos = rStart' $ getRange e in parseErrorAt pos $ "Not a valid identifier: " ++ show e stripSingletonRawApp :: Expr -> Expr stripSingletonRawApp (RawApp _ [e]) = stripSingletonRawApp e stripSingletonRawApp e = e isEqual :: Expr -> Maybe (Expr, Expr) isEqual e = case stripSingletonRawApp e of Equal _ a b -> Just (stripSingletonRawApp a, stripSingletonRawApp b) _ -> Nothing maybeNamed :: Expr -> Named_ Expr maybeNamed e = case isEqual e of Just (Ident (QName x), b) -> named (Ranged (getRange x) (nameToRawName x)) b _ -> unnamed e patternSynArgs :: [Either Hiding LamBinding] -> Parser [Arg Name] patternSynArgs = mapM pSynArg where pSynArg Left{} = parseError "Absurd patterns are not allowed in pattern synonyms" pSynArg (Right DomainFull{}) = parseError "Unexpected type signature in pattern synonym argument" pSynArg (Right (DomainFree a x)) | getHiding a `notElem` [Hidden, NotHidden] = parseError $ show (getHiding a) ++ " arguments not allowed to pattern synonyms" | getRelevance a /= Relevant = parseError "Arguments to pattern synonyms must be relevant" | otherwise = return $ Arg a (boundName x) parsePanic s = parseError $ "Internal parser error: " ++ s ++ ". Please report this as a bug." {- RHS or type signature -} data RHSOrTypeSigs = JustRHS RHS | TypeSigsRHS Expr deriving Show patternToNames :: Pattern -> Parser [(ArgInfo, Name)] patternToNames p = case p of IdentP (QName i) -> return [(defaultArgInfo, i)] WildP r -> return [(defaultArgInfo, C.noName r)] DotP _ _ (Ident (QName i)) -> return [(setRelevance Irrelevant defaultArgInfo, i)] RawAppP _ ps -> concat <$> mapM patternToNames ps _ -> parseError $ "Illegal name in type signature: " ++ prettyShow p funClauseOrTypeSigs :: LHS -> RHSOrTypeSigs -> WhereClause -> Parser [Declaration] funClauseOrTypeSigs lhs mrhs wh = do -- traceShowM lhs case mrhs of JustRHS rhs -> return [FunClause lhs rhs wh False] TypeSigsRHS e -> case wh of NoWhere -> case lhs of Ellipsis{} -> parseError "The ellipsis ... cannot have a type signature" LHS _ _ _ (_:_) -> parseError "Illegal: with in type signature" LHS _ _ (_:_) _ -> parseError "Illegal: rewrite in type signature" LHS _ (_:_) _ _ -> parseError "Illegal: with patterns in type signature" LHS p [] [] [] -> map (\ (x, y) -> TypeSig x y e) <$> patternToNames p _ -> parseError "A type signature cannot have a where clause" parseDisplayPragma :: Range -> Position -> String -> Parser Pragma parseDisplayPragma r pos s = case parsePosString pos defaultParseFlags [normal] funclauseParser s of ParseOk s [FunClause (LHS lhs [] [] []) (RHS rhs) NoWhere ca] | null (parseInp s) -> return $ DisplayPragma r lhs rhs _ -> parseError "Invalid DISPLAY pragma. Should have form {-# DISPLAY LHS = RHS #-}." } Agda-2.5.3/src/full/Agda/Syntax/Parser/Layout.hs-boot0000644000000000000000000000032513154613124020424 0ustar0000000000000000module Agda.Syntax.Parser.Layout where import Agda.Syntax.Parser.Alex import Agda.Syntax.Parser.Tokens offsideRule :: LexAction Token newLayoutContext :: LexAction Token emptyLayout :: LexAction Token Agda-2.5.3/src/full/Agda/Syntax/Parser/LookAhead.hs0000644000000000000000000001033013154613124020032 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-| When lexing by hands (for instance string literals) we need to do some looking ahead. The 'LookAhead' monad keeps track of the position we are currently looking at, and provides facilities to synchronise the look-ahead position with the actual position of the 'Parser' monad (see 'sync' and 'rollback'). -} module Agda.Syntax.Parser.LookAhead ( -- * The LookAhead monad LookAhead , runLookAhead -- * Operations , getInput, setInput, liftP , nextChar, eatNextChar , sync, rollback , match, match' ) where import Control.Applicative import Control.Monad.Reader import Control.Monad.State import Agda.Syntax.Parser.Alex import Agda.Syntax.Parser.Monad {-------------------------------------------------------------------------- The look-ahead monad --------------------------------------------------------------------------} {-| The LookAhead monad is basically a state monad keeping with an extra 'AlexInput', wrapped around the 'Parser' monad. -} newtype LookAhead a = LookAhead { unLookAhead :: ReaderT ErrorFunction (StateT AlexInput Parser) a } deriving (Functor, Applicative) newtype ErrorFunction = ErrorFun { throwError :: forall a. String -> LookAhead a } {-------------------------------------------------------------------------- Monad instances --------------------------------------------------------------------------} instance Monad LookAhead where return = pure m >>= k = LookAhead $ unLookAhead m >>= unLookAhead . k fail s = do err <- LookAhead ask throwError err s {-------------------------------------------------------------------------- Operations --------------------------------------------------------------------------} -- | Get the current look-ahead position. getInput :: LookAhead AlexInput getInput = LookAhead get -- | Set the look-ahead position. setInput :: AlexInput -> LookAhead () setInput = LookAhead . put -- | Lift a computation in the 'Parser' monad to the 'LookAhead' monad. liftP :: Parser a -> LookAhead a liftP = LookAhead . lift . lift -- | Look at the next character. Fails if there are no more characters. nextChar :: LookAhead Char nextChar = do inp <- getInput case alexGetChar inp of Nothing -> fail "unexpected end of file" Just (c,inp') -> do setInput inp' return c -- | Consume all the characters up to the current look-ahead position. sync :: LookAhead () sync = do inp <- getInput liftP $ setLexInput inp -- | Undo look-ahead. Restores the input from the 'ParseState'. rollback :: LookAhead () rollback = do inp <- liftP getLexInput setInput inp -- | Consume the next character. Does 'nextChar' followed by 'sync'. eatNextChar :: LookAhead Char eatNextChar = do c <- nextChar sync return c {-| Do a case on the current input string. If any of the given strings match we move past it and execute the corresponding action. If no string matches, we execute a default action, advancing the input one character. This function only affects the look-ahead position. -} match :: [(String, LookAhead a)] -> LookAhead a -> LookAhead a match xs def = do c <- nextChar match' c xs def {-| Same as 'match' but takes the initial character from the first argument instead of reading it from the input. Consequently, in the default case the input is not advanced. -} match' :: Char -> [(String, LookAhead a)] -> LookAhead a -> LookAhead a match' c xs def = do inp <- getInput match'' inp xs c where match'' inp bs c = case bs' of [] -> setInput inp >> def [("",p)] -> p _ -> match'' inp bs' =<< nextChar where bs' = [ (s, p) | (c':s, p) <- bs, c == c' ] -- | Run a 'LookAhead' computation. The first argument is the error function. runLookAhead :: (forall b. String -> LookAhead b) -> LookAhead a -> Parser a runLookAhead err (LookAhead m) = do inp <- getLexInput evalStateT (runReaderT m (ErrorFun err)) inp Agda-2.5.3/src/full/Agda/Syntax/Parser/StringLiterals.hs0000644000000000000000000001512713154613124021162 0ustar0000000000000000{-| The code to lex string and character literals. Basically the same code as in GHC. -} module Agda.Syntax.Parser.StringLiterals ( litString, litChar ) where import Data.Char import Agda.Syntax.Parser.Alex import Agda.Syntax.Parser.Monad import Agda.Syntax.Parser.Tokens import Agda.Syntax.Parser.LookAhead import Agda.Syntax.Position import Agda.Syntax.Literal import Agda.Utils.Char ( decDigit, hexDigit, octDigit ) import Agda.Utils.Tuple ( (-*-) ) {-------------------------------------------------------------------------- Exported actions --------------------------------------------------------------------------} -- | Lex a string literal. Assumes that a double quote has been lexed. litString :: LexAction Token litString = stringToken '"' (\i s -> return $ TokLiteral $ LitString (getRange i) s) {-| Lex a character literal. Assumes that a single quote has been lexed. A character literal is lexed in exactly the same way as a string literal. Only before returning the token do we check that the lexed string is of length 1. This is maybe not the most efficient way of doing things, but on the other hand it will only be inefficient if there is a lexical error. -} litChar :: LexAction Token litChar = stringToken '\'' $ \i s -> do case s of [c] -> return $ TokLiteral $ LitChar (getRange i) c _ -> lexError "character literal must contain a single character" {-------------------------------------------------------------------------- Errors --------------------------------------------------------------------------} -- | Custom error function. litError :: String -> LookAhead a litError msg = do sync liftP $ lexError $ "Lexical error in string or character literal: " ++ msg {-------------------------------------------------------------------------- The meat --------------------------------------------------------------------------} -- | The general function to lex a string or character literal token. The -- character argument is the delimiter (@\"@ for strings and @\'@ for -- characters). stringToken :: Char -> (Interval -> String -> Parser tok) -> LexAction tok stringToken del mkTok inp inp' n = do setLastPos (backupPos $ lexPos inp') setLexInput inp' -- TODO: Should setPrevToken be run here? Compare with -- Agda.Syntax.Parser.LexActions.token. tok <- runLookAhead litError $ lexString del "" i <- getParseInterval mkTok i tok -- | This is where the work happens. The string argument is an accumulating -- parameter for the string being lexed. lexString :: Char -> String -> LookAhead String lexString del s = do c <- nextChar case c of c | c == del -> sync >> return (reverse s) '\\' -> do c' <- nextChar case c' of '&' -> sync >> lexString del s c | isSpace c -> sync >> lexStringGap del s _ -> normalChar _ -> normalChar where normalChar = do rollback c <- lexChar lexString del (c:s) -- | A string gap consists of whitespace (possibly including line breaks) -- enclosed in backslashes. The gap is not part of the resulting string. lexStringGap :: Char -> String -> LookAhead String lexStringGap del s = do c <- eatNextChar case c of '\\' -> lexString del s c | isSpace c -> lexStringGap del s _ -> fail "non-space in string gap" -- | Lex a single character. lexChar :: LookAhead Char lexChar = do c <- eatNextChar case c of '\\' -> lexEscape _ -> return c -- | Lex an escaped character. Assumes the backslash has been lexed. lexEscape :: LookAhead Char lexEscape = do c <- eatNextChar case c of '^' -> do c <- eatNextChar if c >= '@' && c <= '_' then return (chr (ord c - ord '@')) else fail "invalid control character" 'x' -> readNum isHexDigit 16 hexDigit 'o' -> readNum isOctDigit 8 octDigit x | isDigit x -> readNumAcc isDigit 10 decDigit (decDigit x) c -> -- Try to match the input (starting with c) against the -- silly escape codes. do esc <- match' c (map (id -*- return) sillyEscapeChars) (fail "bad escape code") sync return esc -- | Read a number in the specified base. readNum :: (Char -> Bool) -> Int -> (Char -> Int) -> LookAhead Char readNum isDigit base conv = do c <- eatNextChar if isDigit c then readNumAcc isDigit base conv (conv c) else fail "non-digit in numeral" -- | Same as 'readNum' but with an accumulating parameter. readNumAcc :: (Char -> Bool) -> Int -> (Char -> Int) -> Int -> LookAhead Char readNumAcc isDigit base conv i = scan i where scan i = do inp <- getInput c <- nextChar case c of c | isDigit c -> scan (i*base + conv c) _ -> do setInput inp sync if i >= ord minBound && i <= ord maxBound then return (chr i) else fail "character literal out of bounds" -- | The escape codes. sillyEscapeChars :: [(String, Char)] sillyEscapeChars = [ ("a", '\a') , ("b", '\b') , ("f", '\f') , ("n", '\n') , ("r", '\r') , ("t", '\t') , ("v", '\v') , ("\\", '\\') , ("\"", '\"') , ("'", '\'') , ("NUL", '\NUL') , ("SOH", '\SOH') , ("STX", '\STX') , ("ETX", '\ETX') , ("EOT", '\EOT') , ("ENQ", '\ENQ') , ("ACK", '\ACK') , ("BEL", '\BEL') , ("BS", '\BS') , ("HT", '\HT') , ("LF", '\LF') , ("VT", '\VT') , ("FF", '\FF') , ("CR", '\CR') , ("SO", '\SO') , ("SI", '\SI') , ("DLE", '\DLE') , ("DC1", '\DC1') , ("DC2", '\DC2') , ("DC3", '\DC3') , ("DC4", '\DC4') , ("NAK", '\NAK') , ("SYN", '\SYN') , ("ETB", '\ETB') , ("CAN", '\CAN') , ("EM", '\EM') , ("SUB", '\SUB') , ("ESC", '\ESC') , ("FS", '\FS') , ("GS", '\GS') , ("RS", '\RS') , ("US", '\US') , ("SP", '\SP') , ("DEL", '\DEL') ] Agda-2.5.3/src/full/Agda/Syntax/Parser/Comments.hs0000644000000000000000000000514513154613124020000 0ustar0000000000000000-- {-# LANGUAGE CPP #-} {-| This module defines the lex action to lex nested comments. As is well-known this cannot be done by regular expressions (which, incidently, is probably the reason why C-comments don't nest). When scanning nested comments we simply keep track of the nesting level, counting up for /open comments/ and down for /close comments/. -} module Agda.Syntax.Parser.Comments where import qualified Data.List as List import {-# SOURCE #-} Agda.Syntax.Parser.LexActions import Agda.Syntax.Parser.Monad import Agda.Syntax.Parser.Tokens import Agda.Syntax.Parser.Alex import Agda.Syntax.Parser.LookAhead import Agda.Syntax.Position import Agda.Utils.Monad -- | Should comment tokens be output? keepComments :: LexPredicate keepComments (_, s) _ _ _ = parseKeepComments s -- | Should comment tokens be output? keepCommentsM :: Parser Bool keepCommentsM = fmap parseKeepComments getParseFlags -- | Manually lexing a block comment. Assumes an /open comment/ has been lexed. -- In the end the comment is discarded and 'lexToken' is called to lex a real -- token. nestedComment :: LexAction Token nestedComment inp inp' _ = do setLexInput inp' runLookAhead err $ skipBlock "{-" "-}" keep <- keepCommentsM if keep then do inp'' <- getLexInput let p1 = lexPos inp; p2 = lexPos inp'' i = posToInterval (lexSrcFile inp) p1 p2 s = case (p1, p2) of (Pn { posPos = p1 }, Pn { posPos = p2 }) -> List.genericTake (p2 - p1) $ lexInput inp return $ TokComment (i, s) else lexToken where err _ = liftP $ parseErrorAt (lexPos inp) "Unterminated '{-'" -- | Lex a hole (@{! ... !}@). Holes can be nested. -- Returns @'TokSymbol' 'SymQuestionMark'@. hole :: LexAction Token hole inp inp' _ = do setLexInput inp' runLookAhead err $ skipBlock "{!" "!}" p <- lexPos <$> getLexInput return $ TokSymbol SymQuestionMark $ posToInterval (lexSrcFile inp) (lexPos inp) p where err _ = liftP $ parseErrorAt (lexPos inp) "Unterminated '{!'" -- | Skip a block of text enclosed by the given open and close strings. Assumes -- the first open string has been consumed. Open-close pairs may be nested. skipBlock :: String -> String -> LookAhead () skipBlock open close = scan 1 where scan 0 = sync scan n = match [ open ==> scan (n + 1) , close ==> scan (n - 1) ] `other` scan n where (==>) = (,) other = ($) Agda-2.5.3/src/full/Agda/Syntax/Parser/LexActions.hs-boot0000644000000000000000000000165713154613124021231 0ustar0000000000000000module Agda.Syntax.Parser.LexActions where import Agda.Syntax.Literal import Agda.Syntax.Parser.Alex import Agda.Syntax.Parser.Monad import Agda.Syntax.Parser.Tokens import Agda.Syntax.Position lexToken :: Parser Token token :: (String -> Parser tok) -> LexAction tok withInterval :: ((Interval, String) -> tok) -> LexAction tok withInterval' :: (String -> a) -> ((Interval, a) -> tok) -> LexAction tok withLayout :: LexAction r -> LexAction r begin :: LexState -> LexAction Token beginWith :: LexState -> LexAction a -> LexAction a endWith :: LexAction a -> LexAction a begin_ :: LexState -> LexAction Token end_ :: LexAction Token keyword :: Keyword -> LexAction Token symbol :: Symbol -> LexAction Token identifier :: LexAction Token literal :: Read a => (Range -> a -> Literal) -> LexAction Token followedBy :: Char -> LexPredicate eof :: LexPredicate inState :: LexState -> LexPredicate Agda-2.5.3/src/full/Agda/Syntax/Parser/LexActions.hs0000644000000000000000000002107513154613124020264 0ustar0000000000000000 {-| This module contains the building blocks used to construct the lexer. -} module Agda.Syntax.Parser.LexActions ( -- * Main function lexToken -- * Lex actions -- ** General actions , token , withInterval, withInterval', withInterval_ , withLayout , begin, end, beginWith, endWith , begin_, end_ , lexError -- ** Specialized actions , keyword, symbol, identifier, literal -- * Lex predicates , followedBy, eof, inState ) where import Data.Char import Agda.Syntax.Parser.Lexer import Agda.Syntax.Parser.Alex import Agda.Syntax.Parser.Monad import Agda.Syntax.Parser.Tokens import Agda.Syntax.Position import Agda.Syntax.Literal import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Tuple {-------------------------------------------------------------------------- Scan functions --------------------------------------------------------------------------} -- | Called at the end of a file. Returns 'TokEOF'. returnEOF :: AlexInput -> Parser Token returnEOF inp = do setLastPos $ lexPos inp setPrevToken "" return TokEOF -- | Set the current input and lex a new token (calls 'lexToken'). skipTo :: AlexInput -> Parser Token skipTo inp = setLexInput inp >> lexToken {-| Scan the input to find the next token. Calls 'Agda.Syntax.Parser.Lexer.alexScanUser'. This is the main lexing function where all the work happens. The function 'Agda.Syntax.Parser.Lexer.lexer', used by the parser is the continuation version of this function. -} lexToken :: Parser Token lexToken = do inp <- getLexInput lss@(ls:_) <- getLexState flags <- getParseFlags case alexScanUser (lss, flags) (foolAlex inp) ls of AlexEOF -> returnEOF inp AlexSkip inp' len -> skipTo (newInput inp inp' len) AlexToken inp' len action -> fmap postToken $ action inp (newInput inp inp' len) len AlexError i -> parseError $ "Lexical error" ++ (case lexInput i of '\t' : _ -> " (you may want to replace tabs with spaces)" _ -> "") ++ ":" postToken :: Token -> Token postToken (TokId (r, "\x03bb")) = TokSymbol SymLambda r postToken (TokId (r, "\x2026")) = TokSymbol SymEllipsis r postToken (TokId (r, "\x2192")) = TokSymbol SymArrow r postToken (TokId (r, "\x2983")) = TokSymbol SymDoubleOpenBrace r postToken (TokId (r, "\x2984")) = TokSymbol SymDoubleCloseBrace r postToken (TokId (r, "\x2987")) = TokSymbol SymOpenIdiomBracket r postToken (TokId (r, "\x2988")) = TokSymbol SymCloseIdiomBracket r postToken (TokId (r, "\x2200")) = TokKeyword KwForall r postToken (TokId (r, s)) | set == "Set" && all isSub n = TokSetN (r, readSubscript n) where (set, n) = splitAt 3 s isSub c = c `elem` ['\x2080'..'\x2089'] readSubscript = read . map (\c -> toEnum (fromEnum c - 0x2080 + fromEnum '0')) postToken t = t -- | Use the input string from the previous input (with the appropriate -- number of characters dropped) instead of the fake input string that -- was given to Alex (with unicode characters removed). newInput :: PreviousInput -> CurrentInput -> TokenLength -> CurrentInput newInput inp inp' len = case drop (len - 1) (lexInput inp) of c:s' -> inp' { lexInput = s' , lexPrevChar = c } [] -> inp' { lexInput = [] } -- we do get empty tokens moving between states -- | Alex 2 can't handle unicode characters. To solve this we -- translate all Unicode (non-ASCII) identifiers to @z@, all Unicode -- operator characters to @+@, and all whitespace characters (except -- for @\t@ and @\n@) to ' '. -- Further, non-printable Unicode characters are translated to an -- arbitrary, harmless ASCII non-printable character, @'\1'@. -- -- It is important that there aren't any keywords containing @z@, @+@ or @ @. foolAlex :: AlexInput -> AlexInput foolAlex = over lensLexInput $ map $ \ c -> case c of _ | isSpace c && not (c `elem` "\t\n") -> ' ' _ | isAscii c -> c _ | isPrint c -> if isAlpha c then 'z' else '+' _ | otherwise -> '\1' {-------------------------------------------------------------------------- Lex actions --------------------------------------------------------------------------} -- | The most general way of parsing a token. token :: (String -> Parser tok) -> LexAction tok token action inp inp' len = do setLexInput inp' setPrevToken t setLastPos $ lexPos inp action t where t = take len $ lexInput inp -- | Parse a token from an 'Interval' and the lexed string. withInterval :: ((Interval, String) -> tok) -> LexAction tok withInterval f = token $ \s -> do r <- getParseInterval return $ f (r,s) -- | Like 'withInterval', but applies a function to the string. withInterval' :: (String -> a) -> ((Interval, a) -> tok) -> LexAction tok withInterval' f t = withInterval (t . (id -*- f)) -- | Return a token without looking at the lexed string. withInterval_ :: (Interval -> r) -> LexAction r withInterval_ f = withInterval (f . fst) -- | Executed for layout keywords. Enters the 'Agda.Syntax.Parser.Lexer.layout' -- state and performs the given action. withLayout :: LexAction r -> LexAction r withLayout a i1 i2 n = do pushLexState layout a i1 i2 n -- | Enter a new state without consuming any input. begin :: LexState -> LexAction Token begin code _ _ _ = do pushLexState code lexToken -- | Enter a new state throwing away the current lexeme. begin_ :: LexState -> LexAction Token begin_ code _ inp' _ = do pushLexState code skipTo inp' -- | Exit the current state throwing away the current lexeme. end_ :: LexAction Token end_ _ inp' _ = do popLexState skipTo inp' -- | Enter a new state and perform the given action. beginWith :: LexState -> LexAction a -> LexAction a beginWith code a inp inp' n = do pushLexState code a inp inp' n -- | Exit the current state and perform the given action. endWith :: LexAction a -> LexAction a endWith a inp inp' n = do popLexState a inp inp' n -- | Exit the current state without consuming any input end :: LexAction Token end _ _ _ = do popLexState lexToken -- | Parse a 'Keyword' token, triggers layout for 'layoutKeywords'. keyword :: Keyword -> LexAction Token keyword k = layout $ withInterval_ (TokKeyword k) where layout | elem k layoutKeywords = withLayout | otherwise = id -- | Parse a 'Symbol' token. symbol :: Symbol -> LexAction Token symbol s = withInterval_ (TokSymbol s) -- | Parse a literal. literal :: Read a => (Range -> a -> Literal) -> LexAction Token literal lit = withInterval' read (TokLiteral . uncurry lit . mapFst getRange) -- | Parse an identifier. Identifiers can be qualified (see 'Name'). -- Example: @Foo.Bar.f@ identifier :: LexAction Token identifier = qualified (either TokId TokQId) -- | Parse a possibly qualified name. qualified :: (Either (Interval, String) [(Interval, String)] -> a) -> LexAction a qualified tok = token $ \s -> do i <- getParseInterval case mkName i $ wordsBy (=='.') s of [] -> lexError "lex error on .." [x] -> return $ tok $ Left x xs -> return $ tok $ Right xs where -- Compute the ranges for the substrings (separated by '.') of -- a name. Dots are included: the intervals generated for -- "A.B.x" correspond to "A.", "B." and "x". mkName :: Interval -> [String] -> [(Interval, String)] mkName _ [] = [] mkName i [x] = [(i, x)] mkName i (x:xs) = (i0, x) : mkName i1 xs where p0 = iStart i p1 = iEnd i p' = movePos (movePosByString p0 x) '.' i0 = Interval p0 p' i1 = Interval p' p1 {-------------------------------------------------------------------------- Predicates --------------------------------------------------------------------------} -- | True when the given character is the next character of the input string. followedBy :: Char -> LexPredicate followedBy c' _ _ _ inp = case lexInput inp of [] -> False c:_ -> c == c' -- | True if we are at the end of the file. eof :: LexPredicate eof _ _ _ inp = null $ lexInput inp -- | True if the given state appears somewhere on the state stack inState :: LexState -> LexPredicate inState s (ls, _) _ _ _ = s `elem` ls Agda-2.5.3/src/full/Agda/Syntax/Parser/Literate.hs0000644000000000000000000002032213154613124017756 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE ViewPatterns #-} -- | Preprocessors for literate code formats module Agda.Syntax.Parser.Literate ( literateProcessors ,literateExts ,literateExtsShortList ,literateSrcFile ,literateTeX ,literateRsT ,literateMd ,illiterate ,atomizeLayers ,Processor ,Layers ,Layer(..) ,LayerRole(..) ,isCode ,isCodeLayer ) where import Prelude hiding (getLine) import Data.Char (isSpace, isControl) import Data.List (isPrefixOf) import Agda.Syntax.Position import Text.Regex.TDFA #if __GLASGOW_HASKELL__ <= 708 import Control.Applicative ((<$>),(<*>)) #endif #include "undefined.h" import Agda.Utils.Impossible -- | Role of a character in the file data LayerRole = Markup | Comment | Code deriving (Show, Eq) -- | A sequence of characters in a file playing the same role data Layer = Layer { layerRole :: LayerRole ,interval :: Interval ,layerContent :: String } deriving (Show) -- | A list of contiguous layers type Layers = [Layer] instance HasRange Layer where getRange = getRange . interval -- | Annotates a tokenized string with position information. mkLayers :: Position -> [(LayerRole, String)] -> Layers mkLayers pos [] = emptyLiterate pos -- Ignore empty layers mkLayers pos ((_,""):xs) = mkLayers pos xs mkLayers pos ((ty,s):xs) = let next = movePosByString pos s in (Layer ty (Interval pos next) s):(mkLayers next xs) unMkLayers :: Layers -> [(LayerRole, String)] unMkLayers = map ((,) <$> layerRole <*> layerContent) atomizeLayers :: Layers -> [(LayerRole, Char)] atomizeLayers = (>>= fmap <$> ((,) . fst) <*> snd) . unMkLayers -- | Type of a literate preprocessor: -- Invariants: -- -- > f : Processor -- -- prop> f pos s /= [] -- -- prop> f pos s >>= layerContent == s type Processor = Position -> String -> [Layer] literateSrcFile :: [Layer] -> SrcFile literateSrcFile [] = __IMPOSSIBLE__ literateSrcFile (Layer{interval}:_) = getIntervalFile interval -- | List of valid extensions for literate Agda files, and their corresponding -- preprocessors. -- -- If you add new extensions, remember to update test/Utils.hs so that test -- cases ending in the new extensions are found. literateProcessors :: [(String, Processor)] literateProcessors = map ((,) <$> (".lagda" ++) . fst <*> snd) [("" , literateTeX) ,(".rst", literateRsT) ,(".tex", literateTeX) ,(".md", literateMd) ] -- | Returns `True` if a role corresponds to Agda code isCode :: LayerRole -> Bool isCode Code = True isCode Markup = False isCode Comment = False -- | Returns `True` a layer contains Agda code isCodeLayer :: Layer -> Bool isCodeLayer = isCode . layerRole -- | Blanks the non-code parts of a given file, preserving positions of -- characters corresponding to code. This way, there is a direct -- correspondence between source positions and positions in the -- processed result. illiterate :: [Layer] -> String illiterate xs = concat [ (if isCode layerRole then id else bleach) layerContent | Layer{layerRole,layerContent} <- xs] -- | Replaces non-space characters in a string with spaces. bleach :: String -> String bleach s = map go s where go c | isSpace c = c go _ = ' ' -- | Check if a character is a blank character. isBlank :: Char -> Bool isBlank = (&&) <$> isSpace <*> not . (== '\n') -- | Possible extensions for a literate Agda file literateExts :: [String] literateExts = map fst literateProcessors -- | Short list of extensions for literate Agda files -- For display purposes. literateExtsShortList :: [String] literateExtsShortList = [".lagda"] -- | break a list just *after* an element satisfying the predicate is found -- -- >>> break1 even [1,3,5,2,4,7,8] -- ([1,3,5,2],[4,7,8]) -- break1 :: (a -> Bool) -> [a] -> ([a],[a]) break1 _ [] = ([], []) break1 p (x:xs) | p x = (x:[],xs) break1 p (x:xs) = let (ys,zs) = break1 p xs in (x:ys,zs) -- | Returns a tuple consisting of the first line of the input, and the rest -- of the input. getLine :: String -> (String, String) getLine = break1 (== '\n') -- | Canonical decomposition of an empty literate file emptyLiterate :: Position -> [Layer] emptyLiterate pos = [Layer (Markup) (Interval pos pos) ""] -- | Create a regular expression that: -- - Must match the whole string -- - Works across line boundaries rex :: String -> Regex rex s = makeRegexOpts blankCompOpt{newSyntax = True} blankExecOpt$ "\\`" ++ s ++ "\\'" -- | Preprocessor for literate TeX literateTeX :: Position -> String -> [Layer] literateTeX pos s = mkLayers pos$ tex s where tex :: String -> [(LayerRole, String)] tex [] = [] tex s = let (line, rest) = getLine s in case r_begin `matchM` line of Just (getAllTextSubmatches -> [_, pre, _, markup, blanks]) -> (Comment, pre):(Markup, markup):(Code, blanks):code rest Just _ -> __IMPOSSIBLE__ Nothing -> (Comment, line):tex rest r_begin = rex "(([^\\%]|\\\\.)*)(\\\\begin\\{code\\})([[:space:]]*)" code :: String -> [(LayerRole, String)] code [] = [] code s = let (line, rest) = getLine s in case r_end `matchM` line of Just (getAllTextSubmatches -> [_, code, markup, post]) -> (Code,code):(Markup, markup):(Comment, post):tex rest Just _ -> __IMPOSSIBLE__ Nothing -> (Code, line):code rest r_end = rex "([[:blank:]]*)(\\\\end\\{code\\})(.*)" -- | Preprocessor for Markdown literateMd :: Position -> String -> [Layer] literateMd pos s = mkLayers pos$ md s where md :: String -> [(LayerRole, String)] md [] = [] md s = let (line, rest) = getLine s in case md_begin `matchM` line of Just (getAllTextSubmatches -> [_, pre, markup, _]) -> (Comment, pre):(Markup, markup):code rest Just _ -> __IMPOSSIBLE__ Nothing -> (Comment, line): if md_begin_other `match` line then code_other rest else md rest md_begin = rex "(.*)([[:space:]]*```(agda)?[[:space:]]*)" md_begin_other = rex "[[:space:]]*```[a-zA-Z0-9-]*[[:space:]]*" code :: String -> [(LayerRole, String)] code [] = [] code s = let (line, rest) = getLine s in case md_end `matchM` line of Just (getAllTextSubmatches -> [_, markup]) -> (Markup, markup):md rest Just _ -> __IMPOSSIBLE__ Nothing -> (Code, line):code rest -- A non-Agda code block. code_other :: String -> [(LayerRole, String)] code_other [] = [] code_other s = let (line, rest) = getLine s in (Comment, line): if md_end `match` line then md rest else code_other rest md_end = rex "([[:space:]]*```[[:space:]]*)" -- | Preprocessor for reStructuredText literateRsT :: Position -> String -> [Layer] literateRsT pos s = mkLayers pos$ rst s where rst :: String -> [(LayerRole, String)] rst [] = [] rst s = maybe_code s maybe_code s = if r_comment `match` line then not_code else case r_code `match` line of [] -> not_code [[_, before, "::", after]] -> -- Code starts if null before || isBlank (last before) then (Markup, line):code rest else (Comment, before ++ ":"):(Markup, ':':after):code rest _ -> __IMPOSSIBLE__ where (line, rest) = getLine s not_code = (Comment, line):rst rest -- | Finds the next indented block in the input code :: String -> [(LayerRole, String)] code [] = [] code s = let (line, rest) = getLine s in if all isSpace line then (Markup, line):(code rest) else let (xs,ys) = span isBlank line in case xs of [] -> maybe_code s _ -> (Code, line): (indented xs rest) -- | Process an indented block indented :: String -> String -> [(LayerRole, String)] indented _ [] = [] indented ind s = let (line, rest) = getLine s in if all isSpace line then (Code, line):(indented ind rest) else if ind `isPrefixOf` line then (Code, line):(indented ind rest) else maybe_code s -- | Beginning of a code block r_code = rex "(.*)(::)([[:space:]]*)" -- | Beginning of a comment block r_comment = rex "[[:space:]]*\\.\\.([[:space:]].*)?" Agda-2.5.3/src/full/Agda/Syntax/Parser/Lexer.x0000644000000000000000000002754713154613124017141 0ustar0000000000000000{ {-# OPTIONS_GHC -fno-warn-deprecated-flags #-} {-# OPTIONS_GHC -fno-warn-missing-signatures #-} {-# OPTIONS_GHC -fno-warn-tabs #-} {-# OPTIONS_GHC -fno-warn-unused-imports #-} {-# LANGUAGE BangPatterns #-} {-| The lexer is generated by Alex () and is an adaptation of GHC's lexer. The main lexing function 'lexer' is called by the "Agda.Syntax.Parser.Parser" to get the next token from the input. -} module Agda.Syntax.Parser.Lexer ( -- * The main function lexer -- * Lex states , normal, code , layout, empty_layout, bol, imp_dir -- * Alex generated functions , AlexReturn(..), alexScanUser ) where import Agda.Syntax.Parser.Alex import Agda.Syntax.Parser.Comments #ifndef __HADDOCK__ import {-# SOURCE #-} Agda.Syntax.Parser.Layout import {-# SOURCE #-} Agda.Syntax.Parser.LexActions #endif import Agda.Syntax.Parser.Monad import Agda.Syntax.Parser.StringLiterals import Agda.Syntax.Parser.Tokens import Agda.Syntax.Literal } -- Unicode is not handled by the following regular expressions. -- Instead, unicode characters are translated to 7-bit ASCII -- by Agda.Syntax.Parser.LexActions.foolAlex in a preprocessing pass. $digit = 0-9 $hexdigit = [ $digit a-f A-F ] $alpha = [ A-Z a-z _ ] $op = [ \- \! \# \$ \% \& \* \+ \/ \< \= \> \^ \| \~ \? \` \[ \] \, \: ] $idstart = [ $digit $alpha $op ] $idchar = [ $idstart ' \\ ] $nonalpha = $idchar # $alpha $nonalphanum = $nonalpha # $digit $white_notab = $white # \t $white_nonl = $white_notab # \n @number = $digit+ | "0x" $hexdigit+ @integer = [\-]? @number @exponent = [eE] [\-\+]? @number @float = @integer \. @number @exponent? | @number @exponent -- A name can't start with \x (to allow \x -> x). -- Bug in alex: [ _ op ]+ doesn't seem to work! @start = ($idstart # [_]) | \\ [ $nonalpha ] @ident = @start $idchar* | [_] $idchar+ @namespace = (@ident \.)* @q_ident = @namespace @ident tokens :- -- White space <0,code,bol_,layout_,empty_layout_,imp_dir_> $white_nonl+ ; $white_notab ; -- Pragmas <0,code,pragma_> "{-#" { beginWith pragma $ symbol SymOpenPragma } "#-}" { endWith $ symbol SymClosePragma } "BUILTIN" { keyword KwBUILTIN } "CATCHALL" { keyword KwCATCHALL } "COMPILED" { keyword KwCOMPILED } "COMPILED_DATA" { keyword KwCOMPILED_DATA } "COMPILED_DATA_UHC" { keyword KwCOMPILED_DATA_UHC } "COMPILED_EXPORT" { keyword KwCOMPILED_EXPORT } "COMPILED_JS" { keyword KwCOMPILED_JS } "COMPILED_TYPE" { keyword KwCOMPILED_TYPE } "COMPILED_UHC" { keyword KwCOMPILED_UHC } "COMPILE" { keyword KwCOMPILE } "FOREIGN" { keyword KwFOREIGN } "DISPLAY" { keyword KwDISPLAY } "ETA" { keyword KwETA } "HASKELL" { keyword KwHASKELL } "IMPORT" { keyword KwIMPORT } "IMPORT_UHC" { keyword KwIMPORT_UHC } "IMPOSSIBLE" { keyword KwIMPOSSIBLE } "INJECTIVE" { keyword KwINJECTIVE } "INLINE" { keyword KwINLINE } "LINE" { keyword KwLINE } "MEASURE" { keyword KwMEASURE } "NO_POSITIVITY_CHECK" { keyword KwNO_POSITIVITY_CHECK } "NO_TERMINATION_CHECK" { keyword KwNO_TERMINATION_CHECK } "NON_TERMINATING" { keyword KwNON_TERMINATING } "OPTIONS" { keyword KwOPTIONS } "POLARITY" { keyword KwPOLARITY } "REWRITE" { keyword KwREWRITE } "STATIC" { keyword KwSTATIC } "TERMINATING" { keyword KwTERMINATING } . # [ $white ] + { withInterval $ TokString } -- Comments -- We need to rule out pragmas here. Usually longest match would take -- precedence, but in some states pragmas aren't valid but comments are. <0,code,bol_,layout_,empty_layout_,imp_dir_> "{-" / { not' (followedBy '#') } { nestedComment } -- A misplaced end-comment, like in @f {x-} = x-@ gives a parse error. "-}" { symbol SymEndComment } @ident "-}" { symbol SymEndComment } -- Dashes followed by a name symbol should be parsed as a name. <0,code,bol_,layout_,empty_layout_,imp_dir_> "--" .* / { keepComments .&&. (followedBy '\n' .||. eof) } { withInterval TokComment } <0,code,bol_,layout_,empty_layout_,imp_dir_> "--" .* / { followedBy '\n' .||. eof } ; -- We need to check the offside rule for the first token on each line. We -- should not check the offside rule for the end of file token or an -- '\end{code}' <0,code,imp_dir_> \n { begin bol_ } { \n ; -- ^ \\ "end{code}" { end } () / { not' eof } { offsideRule } } -- After a layout keyword there is either an open brace (no layout) or the -- indentation of the first token decides the column of the layout block. { \n ; -- \{ { endWith openBrace } () { endWith newLayoutContext } } -- The only rule for the empty_layout state. Generates a close brace. () { emptyLayout } -- Keywords <0,code> let { keyword KwLet } <0,code> in { keyword KwIn } <0,code> where { keyword KwWhere } <0,code> field { keyword KwField } <0,code> with { keyword KwWith } <0,code> rewrite { keyword KwRewrite } <0,code> postulate { keyword KwPostulate } <0,code> primitive { keyword KwPrimitive } <0,code> open { keyword KwOpen } <0,code> import { keyword KwImport } <0,code> module { keyword KwModule } <0,code> data { keyword KwData } <0,code> codata { keyword KwCoData } <0,code> record { keyword KwRecord } <0,code> constructor { keyword KwConstructor } <0,code> inductive { keyword KwInductive } <0,code> coinductive { keyword KwCoInductive } <0,code> "eta-equality" { keyword KwEta } <0,code> "no-eta-equality" { keyword KwNoEta } <0,code> infix { keyword KwInfix } <0,code> infixl { keyword KwInfixL } <0,code> infixr { keyword KwInfixR } <0,code> mutual { keyword KwMutual } <0,code> abstract { keyword KwAbstract } <0,code> private { keyword KwPrivate } <0,code> instance { keyword KwInstance } <0,code> overlap { keyword KwOverlap } <0,code> macro { keyword KwMacro } <0,code> Set { keyword KwSet } <0,code> forall { keyword KwForall } <0,code> Set @number { withInterval' (read . drop 3) TokSetN } <0,code> quoteGoal { keyword KwQuoteGoal } <0,code> quoteContext { keyword KwQuoteContext } <0,code> quote { keyword KwQuote } <0,code> quoteTerm { keyword KwQuoteTerm } <0,code> unquote { keyword KwUnquote } <0,code> unquoteDecl { keyword KwUnquoteDecl } <0,code> unquoteDef { keyword KwUnquoteDef } <0,code> tactic { keyword KwTactic } <0,code> syntax { keyword KwSyntax } <0,code> pattern { keyword KwPatternSyn } -- The parser is responsible to put the lexer in the imp_dir_ state when it -- expects an import directive keyword. This means that if you run the -- tokensParser you will never see these keywords. <0,code> using { keyword KwUsing } <0,code> hiding { keyword KwHiding } <0,code> renaming { keyword KwRenaming } to { endWith $ keyword KwTo } <0,code> public { keyword KwPublic } -- Holes <0,code> "{!" { hole } -- Special symbols <0,code> "..." { symbol SymEllipsis } <0,code> ".." { symbol SymDotDot } <0,code> "." { symbol SymDot } <0,code> ";" { symbol SymSemi } <0,code> ":" { symbol SymColon } <0,code> "=" { symbol SymEqual } <0,code> "_" { symbol SymUnderscore } <0,code> "?" { symbol SymQuestionMark } <0,code> "|" { symbol SymBar } <0,code> "(|" /[$white] { symbol SymOpenIdiomBracket } <0,code> "|)" { symbol SymCloseIdiomBracket } <0,code> "(" { symbol SymOpenParen } <0,code> ")" { symbol SymCloseParen } <0,code> "->" { symbol SymArrow } <0,code> "\" { symbol SymLambda } -- " <0,code> "@" { symbol SymAs } <0,code> "{{" /[^!] { symbol SymDoubleOpenBrace } -- We don't lex '}}' into a SymDoubleCloseBrace. Instead, we lex it as -- two SymCloseBrace's. When the parser is looking for a double -- closing brace, it will also accept two SymCloseBrace's, after -- verifying that they are immediately next to each other. -- This trick allows us to keep "record { a = record {}}" working -- properly. -- <0,code> "}}" { symbol SymDoubleCloseBrace } <0,code> "{" { symbol SymOpenBrace } -- you can't use braces for layout <0,code> "}" { symbol SymCloseBrace } -- Literals <0,code> \' { litChar } <0,code> \" { litString } <0,code> @integer { literal LitNat } <0,code> @float { literal LitFloat } -- Identifiers <0,code,imp_dir_> @q_ident { identifier } -- Andreas, 2013-02-21, added identifiers to the 'imp_dir_' state. -- This is to fix issue 782: 'toz' should not be lexed as 'to' -- (followed by 'z' after leaving imp_dir_). -- With identifiers in state imp_dir_, 'toz' should be lexed as -- identifier 'toz' in imp_dir_ state, leading to a parse error later. { -- | This is the initial state for parsing a regular, non-literate file. normal :: LexState normal = 0 {-| The layout state. Entered when we see a layout keyword ('withLayout') and exited either when seeing an open brace ('openBrace') or at the next token ('newLayoutContext'). Update: we don't use braces for layout anymore. -} layout :: LexState layout = layout_ {-| The state inside a pragma. -} pragma :: LexState pragma = pragma_ {-| We enter this state from 'newLayoutContext' when the token following a layout keyword is to the left of (or at the same column as) the current layout context. Example: > data Empty : Set where > foo : Empty -> Nat Here the second line is not part of the @where@ clause since it is has the same indentation as the @data@ definition. What we have to do is insert an empty layout block @{}@ after the @where@. The only thing that can happen in this state is that 'emptyLayout' is executed, generating the closing brace. The open brace is generated when entering by 'newLayoutContext'. -} empty_layout :: LexState empty_layout = empty_layout_ -- | This state is entered at the beginning of each line. You can't lex -- anything in this state, and to exit you have to check the layout rule. -- Done with 'offsideRule'. bol :: LexState bol = bol_ -- | This state can only be entered by the parser. In this state you can only -- lex the keywords @using@, @hiding@, @renaming@ and @to@. Moreover they are -- only keywords in this particular state. The lexer will never enter this -- state by itself, that has to be done in the parser. imp_dir :: LexState imp_dir = imp_dir_ -- | Return the next token. This is the function used by Happy in the parser. -- -- @lexer k = 'lexToken' >>= k@ lexer :: (Token -> Parser a) -> Parser a lexer k = lexToken >>= k -- | Do not use this function; it sets the 'ParseFlags' to -- 'undefined'. alexScan :: AlexInput -> Int -> AlexReturn (LexAction Token) -- | This is the main lexing function generated by Alex. alexScanUser :: ([LexState], ParseFlags) -> AlexInput -> Int -> AlexReturn (LexAction Token) } Agda-2.5.3/src/full/Agda/Syntax/Parser/Monad.hs0000644000000000000000000003273713154613124017260 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} module Agda.Syntax.Parser.Monad ( -- * The parser monad Parser , ParseResult(..) , ParseState(..) , ParseError(..), ParseWarning(..) , LexState , LayoutContext(..) , ParseFlags (..) -- * Running the parser , initState , defaultParseFlags , parse , parseFile , parsePosString , parseFromSrc -- * Manipulating the state , setParsePos, setLastPos, getParseInterval , setPrevToken , getParseFlags , getLexState, pushLexState, popLexState -- ** Layout , topContext, popContext, pushContext , pushCurrentContext -- ** Errors , parseError, parseErrorAt, parseError' , lexError ) where import Control.Exception (catch) import Data.Int import Data.Typeable ( Typeable ) import Data.Data (Data) import Control.Monad.State import Control.Applicative import Agda.Syntax.Position import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.FileName import qualified Agda.Utils.IO.UTF8 as UTF8 import qualified Agda.Utils.Maybe.Strict as Strict import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible {-------------------------------------------------------------------------- The parse monad --------------------------------------------------------------------------} -- | The parse monad. Equivalent to @StateT 'ParseState' (Either 'ParseError')@ -- except for the definition of @fail@, which builds a suitable 'ParseError' -- object. newtype Parser a = P { unP :: ParseState -> ParseResult a } -- | The parser state. Contains everything the parser and the lexer could ever -- need. data ParseState = PState { parseSrcFile :: !SrcFile , parsePos :: !PositionWithoutFile -- ^ position at current input location , parseLastPos :: !PositionWithoutFile -- ^ position of last token , parseInp :: String -- ^ the current input , parsePrevChar :: !Char -- ^ the character before the input , parsePrevToken:: String -- ^ the previous token , parseLayout :: [LayoutContext] -- ^ the stack of layout contexts , parseLexState :: [LexState] -- ^ the state of the lexer -- (states can be nested so we need a stack) , parseFlags :: ParseFlags -- ^ currently there are no flags } deriving Show {-| To do context sensitive lexing alex provides what is called /start codes/ in the Alex documentation. It is really an integer representing the state of the lexer, so we call it @LexState@ instead. -} type LexState = Int -- | We need to keep track of the context to do layout. The context -- specifies the indentation (if any) of a layout block. See -- "Agda.Syntax.Parser.Layout" for more informaton. data LayoutContext = NoLayout -- ^ no layout | Layout Int32 -- ^ layout at specified column deriving Show -- | There aren't any parser flags at the moment. data ParseFlags = ParseFlags { parseKeepComments :: Bool -- ^ Should comment tokens be returned by the lexer? } deriving Show -- | What you get if parsing fails. data ParseError = -- | Errors that arise at a specific position in the file ParseError { errSrcFile :: !SrcFile -- ^ The file in which the error occurred. , errPos :: !PositionWithoutFile -- ^ Where the error occurred. , errInput :: String -- ^ The remaining input. , errPrevToken :: String -- ^ The previous token. , errMsg :: String -- ^ Hopefully an explanation of what happened. } | -- | Parse errors that concern a range in a file. OverlappingTokensError { errRange :: !(Range' SrcFile) -- ^ The range of the bigger overlapping token } | -- | Parse errors that concern a whole file. InvalidExtensionError { errPath :: !AbsolutePath -- ^ The file which the error concerns. , errValidExts :: [String] } | ReadFileError { errPath :: !AbsolutePath , errIOError :: IOError } deriving (Typeable) -- | Warnings for parsing data ParseWarning = -- | Parse errors that concern a range in a file. OverlappingTokensWarning { warnRange :: !(Range' SrcFile) -- ^ The range of the bigger overlapping token } deriving (Typeable, Data) -- | The result of parsing something. data ParseResult a = ParseOk ParseState a | ParseFailed ParseError {-------------------------------------------------------------------------- Instances --------------------------------------------------------------------------} instance Monad Parser where return = pure P m >>= f = P $ \s -> case m s of ParseFailed e -> ParseFailed e ParseOk s' x -> unP (f x) s' fail msg = P $ \s -> ParseFailed $ ParseError { errSrcFile = parseSrcFile s , errPos = parseLastPos s , errInput = parseInp s , errPrevToken = parsePrevToken s , errMsg = msg } instance Functor Parser where fmap = liftM instance Applicative Parser where pure x = P $ \s -> ParseOk s x (<*>) = ap instance MonadError ParseError Parser where throwError e = P $ \_ -> ParseFailed e P m `catchError` h = P $ \s -> case m s of ParseFailed err -> unP (h err) s m' -> m' instance MonadState ParseState Parser where get = P $ \s -> ParseOk s s put s = P $ \_ -> ParseOk s () instance Show ParseError where show = prettyShow instance Pretty ParseError where pretty ParseError{errPos,errSrcFile,errMsg,errPrevToken,errInput} = vcat [ pretty (errPos { srcFile = errSrcFile }) <> colon <+> text errMsg , text $ errPrevToken ++ "" , text $ take 30 errInput ++ "..." ] pretty OverlappingTokensError{errRange} = vcat [ pretty errRange <> colon <+> text "Multi-line comment spans one or more literate text blocks." ] pretty InvalidExtensionError{errPath,errValidExts} = vcat [ pretty errPath <> colon <+> text "Unsupported extension." , text "Supported extensions are:" <+> prettyList_ errValidExts ] pretty ReadFileError{errPath,errIOError} = vcat [ text "Cannot read file" <+> pretty errPath -- TODO: `show` should be replaced by `displayException` once we -- cease to support versions of GHC under 7.10. , text "Error:" <+> text (show errIOError) ] instance HasRange ParseError where getRange ParseError{errSrcFile,errPos=p} = posToRange' errSrcFile p p getRange OverlappingTokensError{errRange} = errRange getRange InvalidExtensionError{errPath} = posToRange p p where p = startPos (Just errPath) getRange ReadFileError{errPath} = posToRange p p where p = startPos (Just errPath) instance Show ParseWarning where show = prettyShow instance Pretty ParseWarning where pretty OverlappingTokensWarning{warnRange} = vcat [ pretty warnRange <> colon <+> text "Multi-line comment spans one or more literate text blocks." ] instance HasRange ParseWarning where getRange OverlappingTokensWarning{warnRange} = warnRange {-------------------------------------------------------------------------- Running the parser --------------------------------------------------------------------------} initStatePos :: Position -> ParseFlags -> String -> [LexState] -> ParseState initStatePos pos flags inp st = PState { parseSrcFile = srcFile pos , parsePos = pos' , parseLastPos = pos' , parseInp = inp , parsePrevChar = '\n' , parsePrevToken = "" , parseLexState = st , parseLayout = [NoLayout] , parseFlags = flags } where pos' = pos { srcFile = () } -- | Constructs the initial state of the parser. The string argument -- is the input string, the file path is only there because it's part -- of a position. initState :: Maybe AbsolutePath -> ParseFlags -> String -> [LexState] -> ParseState initState file = initStatePos (startPos file) -- | The default flags. defaultParseFlags :: ParseFlags defaultParseFlags = ParseFlags { parseKeepComments = False } -- | The most general way of parsing a string. The "Agda.Syntax.Parser" will define -- more specialised functions that supply the 'ParseFlags' and the -- 'LexState'. parse :: ParseFlags -> [LexState] -> Parser a -> String -> ParseResult a parse flags st p input = parseFromSrc flags st p Strict.Nothing input -- | The even more general way of parsing a string. parsePosString :: Position -> ParseFlags -> [LexState] -> Parser a -> String -> ParseResult a parsePosString pos flags st p input = unP p (initStatePos pos flags input st) -- | The most general way of parsing a file. The "Agda.Syntax.Parser" will define -- more specialised functions that supply the 'ParseFlags' and the -- 'LexState'. -- -- Note that Agda source files always use the UTF-8 character -- encoding. parseFile :: ParseFlags -> [LexState] -> Parser a -> AbsolutePath -> IO (ParseResult a) parseFile flags st p file = do res <- (Right <$> (UTF8.readTextFile (filePath file))) `catch` (return . Left . ReadFileError file) case res of Left error -> return$ ParseFailed error Right input -> return$ parseFromSrc flags st p (Strict.Just file) input -- | Parses a string as if it were the contents of the given file -- Useful for integrating preprocessors. parseFromSrc :: ParseFlags -> [LexState] -> Parser a -> SrcFile -> String -> ParseResult a parseFromSrc flags st p src input = unP p (initState (Strict.toLazy src) flags input st) {-------------------------------------------------------------------------- Manipulating the state --------------------------------------------------------------------------} setParsePos :: PositionWithoutFile -> Parser () setParsePos p = modify $ \s -> s { parsePos = p } setLastPos :: PositionWithoutFile -> Parser () setLastPos p = modify $ \s -> s { parseLastPos = p } setPrevToken :: String -> Parser () setPrevToken t = modify $ \s -> s { parsePrevToken = t } getLastPos :: Parser PositionWithoutFile getLastPos = get >>= return . parseLastPos -- | The parse interval is between the last position and the current position. getParseInterval :: Parser Interval getParseInterval = do s <- get return $ posToInterval (parseSrcFile s) (parseLastPos s) (parsePos s) getLexState :: Parser [LexState] getLexState = parseLexState <$> get setLexState :: [LexState] -> Parser () setLexState ls = do s <- get put $ s { parseLexState = ls } pushLexState :: LexState -> Parser () pushLexState l = do s <- getLexState setLexState (l:s) popLexState :: Parser () popLexState = do _:ls <- getLexState setLexState ls getParseFlags :: Parser ParseFlags getParseFlags = parseFlags <$> get -- | @parseError = fail@ parseError :: String -> Parser a parseError = fail -- | Fake a parse error at the specified position. Used, for instance, when -- lexing nested comments, which when failing will always fail at the end -- of the file. A more informative position is the beginning of the failing -- comment. parseErrorAt :: PositionWithoutFile -> String -> Parser a parseErrorAt p msg = do setLastPos p parseError msg -- | Use 'parseErrorAt' or 'parseError' as appropriate. parseError' :: Maybe PositionWithoutFile -> String -> Parser a parseError' = maybe parseError parseErrorAt -- | For lexical errors we want to report the current position as the site of -- the error, whereas for parse errors the previous position is the one -- we're interested in (since this will be the position of the token we just -- lexed). This function does 'parseErrorAt' the current position. lexError :: String -> Parser a lexError msg = do p <- parsePos <$> get parseErrorAt p msg {-------------------------------------------------------------------------- Layout --------------------------------------------------------------------------} getContext :: Parser [LayoutContext] getContext = parseLayout <$> get setContext :: [LayoutContext] -> Parser () setContext ctx = do s <- get put $ s { parseLayout = ctx } -- | Return the current layout context. topContext :: Parser LayoutContext topContext = do ctx <- getContext case ctx of [] -> parseError "No layout context in scope" l:_ -> return l popContext :: Parser () popContext = do ctx <- getContext case ctx of [] -> parseError "There is no layout block to close at this point." _:ctx -> setContext ctx pushContext :: LayoutContext -> Parser () pushContext l = do ctx <- getContext setContext (l : ctx) -- | Should only be used at the beginning of a file. When we start parsing -- we should be in layout mode. Instead of forcing zero indentation we use -- the indentation of the first token. pushCurrentContext :: Parser () pushCurrentContext = do p <- getLastPos pushContext (Layout (posCol p)) Agda-2.5.3/src/full/Agda/Syntax/Parser/Layout.hs0000644000000000000000000001236513154613124017472 0ustar0000000000000000-- {-# LANGUAGE CPP #-} {-| This module contains the lex actions that handle the layout rules. The way it works is that the 'Parser' monad keeps track of a stack of 'LayoutContext's specifying the indentation of the layout blocks in scope. For instance, consider the following incomplete (Haskell) program: > f x = x' > where > x' = case x of { True -> False; False -> ... At the @...@ the layout context would be > [NoLayout, Layout 4, Layout 0] The closest layout block is the one containing the @case@ branches. This block starts with an open brace (@\'{\'@) and so doesn't use layout. The second closest block is the @where@ clause. Here, there is no open brace so the block is started by the @x'@ token which has indentation 4. Finally there is a top-level layout block with indentation 0. -} module Agda.Syntax.Parser.Layout ( openBrace, closeBrace , withLayout , offsideRule , newLayoutContext , emptyLayout ) where import Agda.Syntax.Parser.Lexer import Agda.Syntax.Parser.Alex import Agda.Syntax.Parser.Monad import Agda.Syntax.Parser.Tokens import Agda.Syntax.Parser.LexActions import Agda.Syntax.Position -- | Executed upon lexing an open brace (@\'{\'@). Enters the 'NoLayout' -- context. openBrace :: LexAction Token openBrace = token $ \_ -> do pushContext NoLayout i <- getParseInterval return (TokSymbol SymOpenBrace i) {-| Executed upon lexing a close brace (@\'}\'@). Exits the current layout context. This might look a bit funny--the lexer will happily use a close brace to close a context open by a virtual brace. This is not a problem since the parser will make sure the braces are appropriately matched. -} closeBrace :: LexAction Token closeBrace = token $ \_ -> do popContext i <- getParseInterval return (TokSymbol SymCloseBrace i) {-| Executed for the first token in each line (see 'Agda.Syntax.Parser.Lexer.bol'). Checks the position of the token relative to the current layout context. If the token is - /to the left/ : Exit the current context and a return virtual close brace (stay in the 'Agda.Syntax.Parser.Lexer.bol' state). - /same column/ : Exit the 'Agda.Syntax.Parser.Lexer.bol' state and return a virtual semi colon. - /to the right/ : Exit the 'Agda.Syntax.Parser.Lexer.bol' state and continue lexing. If the current block doesn't use layout (i.e. it was started by 'openBrace') all positions are considered to be /to the right/. -} offsideRule :: LexAction Token offsideRule inp _ _ = do offs <- getOffside p case offs of LT -> do popContext return (TokSymbol SymCloseVirtualBrace i) EQ -> do popLexState return (TokSymbol SymVirtualSemi i) GT -> do popLexState lexToken where p = lexPos inp i = posToInterval (lexSrcFile inp) p p {-| This action is only executed from the 'Agda.Syntax.Parser.Lexer.empty_layout' state. It will exit this state, enter the 'Agda.Syntax.Parser.Lexer.bol' state, and return a virtual close brace (closing the empty layout block started by 'newLayoutContext'). -} emptyLayout :: LexAction Token emptyLayout inp _ _ = do popLexState pushLexState bol return (TokSymbol SymCloseVirtualBrace i) where p = lexPos inp i = posToInterval (lexSrcFile inp) p p {-| Start a new layout context. This is one of two ways to get out of the 'Agda.Syntax.Parser.Lexer.layout' state (the other is 'openBrace'). There are two possibilities: - The current token is to the right of the current layout context (or we're in a no layout context). - The current token is to the left of or in the same column as the current context. In the first case everything is fine and we enter a new layout context at the column of the current token. In the second case we have an empty layout block so we enter the 'Agda.Syntax.Parser.Lexer.empty_layout' state. In both cases we return a virtual open brace without consuming any input. Entering a new state when we know we want to generate a virtual @{}@ may seem a bit roundabout. The thing is that we can only generate one token at a time, so the way to generate two tokens is to generate the first one and then enter a state in which the only thing you can do is generate the second one. -} newLayoutContext :: LexAction Token newLayoutContext inp _ _ = do let offset = posCol p ctx <- topContext case ctx of Layout prevOffs | prevOffs >= offset -> do pushLexState empty_layout return (TokSymbol SymOpenVirtualBrace i) _ -> do pushContext (Layout offset) return (TokSymbol SymOpenVirtualBrace i) where p = lexPos inp i = posToInterval (lexSrcFile inp) p p -- | Compute the relative position of a location to the -- current layout context. getOffside :: Position' a -> Parser Ordering getOffside loc = do ctx <- topContext return $ case ctx of Layout n -> compare (posCol loc) n _ -> GT Agda-2.5.3/src/full/Agda/Syntax/Parser/Alex.hs0000644000000000000000000000764213154613124017110 0ustar0000000000000000{-| This module defines the things required by Alex and some other Alex related things. -} module Agda.Syntax.Parser.Alex ( -- * Alex requirements AlexInput(..) , lensLexInput , alexInputPrevChar , alexGetChar, alexGetByte -- * Lex actions , LexAction, LexPredicate , (.&&.), (.||.), not' , PreviousInput, CurrentInput, TokenLength -- * Monad operations , getLexInput, setLexInput ) where import Control.Monad.State import Data.Word import Agda.Syntax.Position import Agda.Syntax.Parser.Monad import Agda.Utils.Lens import Agda.Utils.Monad import Agda.Utils.Tuple -- | This is what the lexer manipulates. data AlexInput = AlexInput { lexSrcFile :: !SrcFile -- ^ File. , lexPos :: !PositionWithoutFile -- ^ Current position. , lexInput :: String -- ^ Current input. , lexPrevChar :: !Char -- ^ Previously read character. } -- | A lens for 'lexInput'. lensLexInput :: Lens' String AlexInput lensLexInput f r = f (lexInput r) <&> \ s -> r { lexInput = s } -- | Get the previously lexed character. Same as 'lexPrevChar'. Alex needs this -- to be defined to handle \"patterns with a left-context\". alexInputPrevChar :: AlexInput -> Char alexInputPrevChar = lexPrevChar -- | Lex a character. No surprises. -- -- This function is used by Alex 2. alexGetChar :: AlexInput -> Maybe (Char, AlexInput) alexGetChar (AlexInput { lexInput = [] }) = Nothing alexGetChar inp@(AlexInput { lexInput = c:s, lexPos = p }) = Just (c, AlexInput { lexSrcFile = lexSrcFile inp , lexInput = s , lexPos = movePos p c , lexPrevChar = c } ) -- | A variant of 'alexGetChar'. -- -- This function is used by Alex 3. alexGetByte :: AlexInput -> Maybe (Word8, AlexInput) alexGetByte ai = -- Note that we ensure that every character presented to Alex fits -- in seven bits. mapFst (fromIntegral . fromEnum) <$> alexGetChar ai {-------------------------------------------------------------------------- Monad operations --------------------------------------------------------------------------} getLexInput :: Parser AlexInput getLexInput = getInp <$> get where getInp s = AlexInput { lexSrcFile = parseSrcFile s , lexPos = parsePos s , lexInput = parseInp s , lexPrevChar = parsePrevChar s } setLexInput :: AlexInput -> Parser () setLexInput inp = modify upd where upd s = s { parseSrcFile = lexSrcFile inp , parsePos = lexPos inp , parseInp = lexInput inp , parsePrevChar = lexPrevChar inp } {-------------------------------------------------------------------------- Lex actions --------------------------------------------------------------------------} type PreviousInput = AlexInput type CurrentInput = AlexInput type TokenLength = Int -- | In the lexer, regular expressions are associated with lex actions who's -- task it is to construct the tokens. type LexAction r = PreviousInput -> CurrentInput -> TokenLength -> Parser r -- | Sometimes regular expressions aren't enough. Alex provides a way to do -- arbitrary computations to see if the input matches. This is done with a -- lex predicate. type LexPredicate = ([LexState], ParseFlags) -> PreviousInput -> TokenLength -> CurrentInput -> Bool -- | Conjunction of 'LexPredicate's. (.&&.) :: LexPredicate -> LexPredicate -> LexPredicate p1 .&&. p2 = \x y z u -> p1 x y z u && p2 x y z u -- | Disjunction of 'LexPredicate's. (.||.) :: LexPredicate -> LexPredicate -> LexPredicate p1 .||. p2 = \x y z u -> p1 x y z u || p2 x y z u -- | Negation of 'LexPredicate's. not' :: LexPredicate -> LexPredicate not' p = \x y z u -> not (p x y z u) Agda-2.5.3/src/full/Agda/Syntax/Translation/0000755000000000000000000000000013154613124016714 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Syntax/Translation/AbstractToConcrete.hs0000644000000000000000000014410513154613124023006 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE UndecidableInstances #-} -- {-# OPTIONS -fwarn-unused-binds #-} {-| The translation of abstract syntax to concrete syntax has two purposes. First it allows us to pretty print abstract syntax values without having to write a dedicated pretty printer, and second it serves as a sanity check for the concrete to abstract translation: translating from concrete to abstract and then back again should be (more or less) the identity. -} module Agda.Syntax.Translation.AbstractToConcrete ( ToConcrete(..) , toConcreteCtx , abstractToConcrete_ , abstractToConcreteEnv , runAbsToCon , RangeAndPragma(..) , abstractToConcreteCtx , withScope , makeEnv , AbsToCon, DontTouchMe, Env , noTakenNames ) where import Prelude hiding (null) import Control.Applicative hiding (empty) import Control.Monad.Reader import Control.Monad.State import qualified Data.Map as Map import Data.Maybe import Data.Monoid import Data.Set (Set) import qualified Data.Set as Set import Data.Traversable (traverse) import Data.Void import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Syntax.Literal import Agda.Syntax.Info import Agda.Syntax.Internal (MetaId(..)) import qualified Agda.Syntax.Internal as I import Agda.Syntax.Fixity import Agda.Syntax.Concrete as C import Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Views as AV import Agda.Syntax.Scope.Base import Agda.TypeChecking.Monad.State (getScope) import Agda.TypeChecking.Monad.Base (TCM, NamedMeta(..), stBuiltinThings, BuiltinThings, Builtin(..)) import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Options import qualified Agda.Utils.AssocList as AssocList import Agda.Utils.Either import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Singleton import Agda.Utils.Tuple import Agda.Utils.Pretty (prettyShow) #include "undefined.h" import Agda.Utils.Impossible -- Environment ------------------------------------------------------------ data Env = Env { takenNames :: Set C.Name , currentScope :: ScopeInfo } -- -- UNUSED -- defaultEnv :: Env -- defaultEnv = Env { takenNames = Set.empty -- , currentScope = emptyScopeInfo -- } makeEnv :: ScopeInfo -> Env makeEnv scope = Env { takenNames = Set.union vars defs , currentScope = scope } where vars = Set.fromList $ map fst $ scopeLocals scope defs = Map.keysSet $ nsNames $ everythingInScope scope currentPrecedence :: AbsToCon PrecedenceStack currentPrecedence = asks $ scopePrecedence . currentScope withPrecedence' :: PrecedenceStack -> AbsToCon a -> AbsToCon a withPrecedence' ps = local $ \e -> e { currentScope = (currentScope e) { scopePrecedence = ps } } withPrecedence :: Precedence -> AbsToCon a -> AbsToCon a withPrecedence p ret = do ps <- currentPrecedence withPrecedence' (pushPrecedence p ps) ret withScope :: ScopeInfo -> AbsToCon a -> AbsToCon a withScope scope = local $ \e -> e { currentScope = scope } noTakenNames :: AbsToCon a -> AbsToCon a noTakenNames = local $ \e -> e { takenNames = Set.empty } -- | Bind a concrete name to an abstract in the translation environment. addBinding :: C.Name -> A.Name -> Env -> Env addBinding y x e = e { takenNames = Set.insert y $ takenNames e , currentScope = (`updateScopeLocals` currentScope e) $ AssocList.insert y (LocalVar x __IMPOSSIBLE__ []) } -- The Monad -------------------------------------------------------------- -- | We put the translation into TCM in order to print debug messages. type AbsToCon = ReaderT Env TCM runAbsToCon :: AbsToCon c -> TCM c runAbsToCon m = do scope <- getScope runReaderT m (makeEnv scope) abstractToConcreteEnv :: ToConcrete a c => Env -> a -> TCM c abstractToConcreteEnv flags a = runReaderT (toConcrete a) flags abstractToConcreteCtx :: ToConcrete a c => Precedence -> a -> TCM c abstractToConcreteCtx ctx x = runAbsToCon $ withPrecedence ctx (toConcrete x) abstractToConcrete_ :: ToConcrete a c => a -> TCM c abstractToConcrete_ = runAbsToCon . toConcrete -- Dealing with names ----------------------------------------------------- -- | Names in abstract syntax are fully qualified, but the concrete syntax -- requires non-qualified names in places. In theory (if all scopes are -- correct), we should get a non-qualified name when translating back to a -- concrete name, but I suspect the scope isn't always perfect. In these -- cases we just throw away the qualified part. It's just for pretty printing -- anyway... unsafeQNameToName :: C.QName -> C.Name unsafeQNameToName = C.unqualify lookupName :: A.Name -> AbsToCon C.Name lookupName x = do names <- asks $ scopeLocals . currentScope case lookup x $ mapMaybe (\ (c,x) -> (,c) <$> notShadowedLocal x) names of Just y -> return y Nothing -> return $ nameConcrete x lookupQName :: AllowAmbiguousNames -> A.QName -> AbsToCon C.QName lookupQName ambCon x = do ys <- inverseScopeLookupName' ambCon x <$> asks currentScope lift $ reportSLn "scope.inverse" 100 $ "inverse looking up abstract name " ++ prettyShow x ++ " yields " ++ prettyShow ys case ys of (y : _) -> return y [] -> do let y = qnameToConcrete x if isUnderscore y -- -- || any (isUnderscore . A.nameConcrete) (A.mnameToList $ A.qnameModule x) then return y else return $ C.Qual (C.Name noRange [Id empty]) y -- this is what happens for names that are not in scope (private names) lookupModule :: A.ModuleName -> AbsToCon C.QName lookupModule (A.MName []) = return $ C.QName $ C.Name noRange [Id "-1"] -- Andreas, 2016-10-10 it can happen that we have an empty module name -- for instance when we query the current module inside the -- frontmatter or module telescope of the top level module. -- In this case, we print it as an invalid module name. -- (Should only affect debug printing.) lookupModule x = do scope <- asks currentScope case inverseScopeLookupModule x scope of (y : _) -> return y [] -> return $ mnameToConcrete x -- this is what happens for names that are not in scope (private names) -- | Add a abstract name to the scope and produce an available concrete version of it. bindName :: A.Name -> (C.Name -> AbsToCon a) -> AbsToCon a bindName x ret = do let y = nameConcrete x if isNoName y then ret y else do names <- asks takenNames let loop x = do let y = nameConcrete x if y `Set.member` names then loop $ nextName x else local (addBinding y x) $ ret y loop x -- | Like 'bindName', but do not care whether name is already taken. bindName' :: A.Name -> AbsToCon a -> AbsToCon a bindName' x = applyUnless (isNoName y) $ local $ addBinding y x where y = nameConcrete x -- Dealing with precedences ----------------------------------------------- -- | General bracketing function. bracket' :: (e -> e) -- ^ the bracketing function -> (PrecedenceStack -> Bool) -- ^ Should we bracket things -- which have the given -- precedence? -> e -> AbsToCon e bracket' paren needParen e = do p <- currentPrecedence return $ if needParen p then paren e else e -- | Expression bracketing bracket :: (PrecedenceStack -> Bool) -> AbsToCon C.Expr -> AbsToCon C.Expr bracket par m = do e <- m bracket' (Paren (getRange e)) par e -- | Pattern bracketing bracketP_ :: (PrecedenceStack -> Bool) -> AbsToCon C.Pattern -> AbsToCon C.Pattern bracketP_ par m = do e <- m bracket' (ParenP (getRange e)) par e {- UNUSED -- | Pattern bracketing bracketP :: (PrecedenceStack -> Bool) -> (C.Pattern -> AbsToCon a) -> ((C.Pattern -> AbsToCon a) -> AbsToCon a) -> AbsToCon a bracketP par ret m = m $ \p -> do p <- bracket' (ParenP $ getRange p) par p ret p -} -- | Applications where the argument is a lambda without parentheses need -- parens more often than other applications. isParenlessLambda :: NamedArg A.Expr -> Bool isParenlessLambda e | notVisible e = False isParenlessLambda e = case unScope $ namedArg e of A.Lam i _ _ -> not $ lamParens i A.AbsurdLam i _ -> not $ lamParens i A.ExtendedLam i _ _ _ -> not $ lamParens i _ -> False -- Dealing with infix declarations ---------------------------------------- -- | If a name is defined with a fixity that differs from the default, we have -- to generate a fixity declaration for that name. withInfixDecl :: DefInfo -> C.Name -> AbsToCon [C.Declaration] -> AbsToCon [C.Declaration] withInfixDecl i x m = do ds <- m return $ fixDecl ++ synDecl ++ ds where fixDecl = [C.Infix (theFixity $ defFixity i) [x] | theFixity (defFixity i) /= noFixity] synDecl = [C.Syntax x (theNotation (defFixity i))] {- UNUSED withInfixDecls :: [(DefInfo, C.Name)] -> AbsToCon [C.Declaration] -> AbsToCon [C.Declaration] withInfixDecls = foldr (.) id . map (uncurry withInfixDecl) -} -- Dealing with private definitions --------------------------------------- -- | Add @abstract@, @private@, @instance@ modifiers. withAbstractPrivate :: DefInfo -> AbsToCon [C.Declaration] -> AbsToCon [C.Declaration] withAbstractPrivate i m = priv (defAccess i) . abst (defAbstract i) . addInstanceB (defInstance i == InstanceDef) <$> m where priv (PrivateAccess UserWritten) ds = [ C.Private (getRange ds) UserWritten ds ] priv _ ds = ds abst AbstractDef ds = [ C.Abstract (getRange ds) ds ] abst ConcreteDef ds = ds addInstanceB :: Bool -> [C.Declaration] -> [C.Declaration] addInstanceB True ds = [ C.InstanceB (getRange ds) ds ] addInstanceB False ds = ds -- The To Concrete Class -------------------------------------------------- class ToConcrete a c | a -> c where toConcrete :: a -> AbsToCon c bindToConcrete :: a -> (c -> AbsToCon b) -> AbsToCon b -- Christian Sattler, 2017-08-05: -- These default implementations are not valid semantically (at least -- the second one). Perhaps they (it) should be removed. toConcrete x = bindToConcrete x return bindToConcrete x ret = ret =<< toConcrete x -- | Translate something in a context of the given precedence. toConcreteCtx :: ToConcrete a c => Precedence -> a -> AbsToCon c toConcreteCtx p x = withPrecedence p $ toConcrete x -- | Translate something in a context of the given precedence. bindToConcreteCtx :: ToConcrete a c => Precedence -> a -> (c -> AbsToCon b) -> AbsToCon b bindToConcreteCtx p x ret = withPrecedence p $ bindToConcrete x ret -- | Translate something in the top context. toConcreteTop :: ToConcrete a c => a -> AbsToCon c toConcreteTop = toConcreteCtx TopCtx -- | Translate something in the top context. bindToConcreteTop :: ToConcrete a c => a -> (c -> AbsToCon b) -> AbsToCon b bindToConcreteTop = bindToConcreteCtx TopCtx -- | Translate something in a context indicated by 'Hiding' info. toConcreteHiding :: (LensHiding h, ToConcrete a c) => h -> a -> AbsToCon c toConcreteHiding h = case getHiding h of NotHidden -> toConcrete Hidden -> toConcreteTop Instance{} -> toConcreteTop -- | Translate something in a context indicated by 'Hiding' info. bindToConcreteHiding :: (LensHiding h, ToConcrete a c) => h -> a -> (c -> AbsToCon b) -> AbsToCon b bindToConcreteHiding h = case getHiding h of NotHidden -> bindToConcrete Hidden -> bindToConcreteTop Instance{} -> bindToConcreteTop -- General instances ------------------------------------------------------ instance ToConcrete a c => ToConcrete [a] [c] where toConcrete = mapM toConcrete -- Andreas, 2017-04-11, Issue #2543 -- The naive `thread'ing does not work as we have to undo -- changes to the Precedence. -- bindToConcrete = thread bindToConcrete bindToConcrete [] ret = ret [] bindToConcrete (a:as) ret = do p <- currentPrecedence -- save precedence bindToConcrete a $ \ c -> withPrecedence' p $ -- reset precedence bindToConcrete as $ \ cs -> ret (c : cs) instance (ToConcrete a1 c1, ToConcrete a2 c2) => ToConcrete (Either a1 a2) (Either c1 c2) where toConcrete = traverseEither toConcrete toConcrete bindToConcrete (Left x) ret = bindToConcrete x $ \x -> ret (Left x) bindToConcrete (Right y) ret = bindToConcrete y $ \y -> ret (Right y) instance (ToConcrete a1 c1, ToConcrete a2 c2) => ToConcrete (a1,a2) (c1,c2) where toConcrete (x,y) = liftM2 (,) (toConcrete x) (toConcrete y) bindToConcrete (x,y) ret = bindToConcrete x $ \x -> bindToConcrete y $ \y -> ret (x,y) instance (ToConcrete a1 c1, ToConcrete a2 c2, ToConcrete a3 c3) => ToConcrete (a1,a2,a3) (c1,c2,c3) where toConcrete (x,y,z) = reorder <$> toConcrete (x,(y,z)) where reorder (x,(y,z)) = (x,y,z) bindToConcrete (x,y,z) ret = bindToConcrete (x,(y,z)) $ ret . reorder where reorder (x,(y,z)) = (x,y,z) instance ToConcrete a c => ToConcrete (Arg a) (Arg c) where toConcrete (Arg i a) = Arg i <$> toConcreteHiding i a bindToConcrete (Arg info x) ret = bindToConcreteCtx (hiddenArgumentCtx $ getHiding info) x $ ret . Arg info instance ToConcrete a c => ToConcrete (WithHiding a) (WithHiding c) where toConcrete (WithHiding h a) = WithHiding h <$> toConcreteHiding h a bindToConcrete (WithHiding h a) ret = bindToConcreteHiding h a $ \ a -> ret $ WithHiding h a instance ToConcrete a c => ToConcrete (Named name a) (Named name c) where toConcrete (Named n x) = Named n <$> toConcrete x bindToConcrete (Named n x) ret = bindToConcrete x $ ret . Named n newtype DontTouchMe a = DontTouchMe a instance ToConcrete (DontTouchMe a) a where toConcrete (DontTouchMe x) = return x -- Names ------------------------------------------------------------------ instance ToConcrete A.Name C.Name where toConcrete = lookupName bindToConcrete x = bindName x instance ToConcrete A.QName C.QName where toConcrete = lookupQName AmbiguousConProjs instance ToConcrete A.ModuleName C.QName where toConcrete = lookupModule -- Expression instance ---------------------------------------------------- instance ToConcrete A.Expr C.Expr where toConcrete (Var x) = Ident . C.QName <$> toConcrete x toConcrete (Def x) = Ident <$> toConcrete x toConcrete (Proj ProjPrefix (AmbQ (x:_))) = Ident <$> toConcrete x toConcrete (Proj _ (AmbQ (x:_))) = C.Dot (getRange x) . Ident <$> toConcrete x toConcrete Proj{} = __IMPOSSIBLE__ toConcrete (A.Macro x) = Ident <$> toConcrete x toConcrete (Con (AmbQ (x:_))) = Ident <$> toConcrete x toConcrete (Con (AmbQ [])) = __IMPOSSIBLE__ -- for names we have to use the name from the info, since the abstract -- name has been resolved to a fully qualified name (except for -- variables) toConcrete (A.Lit (LitQName r x)) = do x <- lookupQName AmbiguousNothing x bracket appBrackets $ return $ C.App r (C.Quote r) (defaultNamedArg $ C.Ident x) toConcrete (A.Lit l) = return $ C.Lit l -- Andreas, 2014-05-17 We print question marks with their -- interaction id, in case @metaNumber /= Nothing@ toConcrete (A.QuestionMark i ii)= return $ C.QuestionMark (getRange i) $ interactionId ii <$ metaNumber i toConcrete (A.Underscore i) = return $ C.Underscore (getRange i) $ prettyShow . NamedMeta (metaNameSuggestion i) . MetaId . metaId <$> metaNumber i toConcrete (A.Dot i e) = C.Dot (getRange i) <$> toConcrete e toConcrete e@(A.App i e1 e2) = tryToRecoverOpApp e $ tryToRecoverNatural e -- or fallback to App $ bracket (appBrackets' $ isParenlessLambda e2) $ do e1' <- toConcreteCtx FunctionCtx e1 e2' <- toConcreteCtx ArgumentCtx e2 return $ C.App (getRange i) e1' e2' toConcrete (A.WithApp i e es) = bracket withAppBrackets $ do e <- toConcreteCtx WithFunCtx e es <- mapM (toConcreteCtx WithArgCtx) es return $ C.WithApp (getRange i) e es toConcrete (A.AbsurdLam i h) = bracket (lamBrackets' $ lamParens i) $ return $ C.AbsurdLam (getRange i) h toConcrete e@(A.Lam i _ _) = tryToRecoverOpApp e -- recover sections $ bracket (lamBrackets' $ lamParens i) $ case lamView e of (bs, e) -> bindToConcrete (map makeDomainFree bs) $ \bs -> do e <- toConcreteTop e return $ C.Lam (getRange i) (concat bs) e where lamView (A.Lam _ b@(A.DomainFree _ _) e) = case lamView e of ([], e) -> ([b], e) (bs@(A.DomainFree _ _ : _), e) -> (b:bs, e) _ -> ([b], e) lamView (A.Lam _ b@(A.DomainFull _) e) = case lamView e of ([], e) -> ([b], e) (bs@(A.DomainFull _ : _), e) -> (b:bs, e) _ -> ([b], e) lamView e = ([], e) toConcrete (A.ExtendedLam i di qname cs) = bracket (lamBrackets' $ lamParens i) $ do decls <- concat <$> toConcrete cs let namedPat np = case getHiding np of NotHidden -> namedArg np Hidden -> C.HiddenP noRange (unArg np) Instance{} -> C.InstanceP noRange (unArg np) -- we know all lhs are of the form `.extlam p1 p2 ... pn`, -- with the name .extlam leftmost. It is our mission to remove it. let removeApp (C.RawAppP r (_:es)) = return $ C.RawAppP r es removeApp (C.AppP (C.IdentP _) np) = return $ namedPat np removeApp (C.AppP p np) = do p <- removeApp p return $ C.AppP p np removeApp p = do lift $ reportSLn "extendedlambda" 50 $ "abstractToConcrete removeApp p = " ++ show p return p -- __IMPOSSIBLE__ -- Andreas, this is actually not impossible, my strictification exposed this sleeping bug let decl2clause (C.FunClause lhs rhs wh ca) = do let p = lhsOriginalPattern lhs lift $ reportSLn "extendedlambda" 50 $ "abstractToConcrete extended lambda pattern p = " ++ show p p' <- removeApp p lift $ reportSLn "extendedlambda" 50 $ "abstractToConcrete extended lambda pattern p' = " ++ show p' return (lhs{ lhsOriginalPattern = p' }, rhs, wh, ca) decl2clause _ = __IMPOSSIBLE__ C.ExtendedLam (getRange i) <$> mapM decl2clause decls toConcrete (A.Pi _ [] e) = toConcrete e toConcrete t@(A.Pi i _ _) = case piTel t of (tel, e) -> bracket piBrackets $ bindToConcrete tel $ \b' -> do e' <- toConcreteTop e return $ C.Pi (concat b') e' where piTel (A.Pi _ tel e) = (tel ++) -*- id $ piTel e piTel e = ([], e) toConcrete (A.Fun i a b) = bracket piBrackets $ do a' <- toConcreteCtx (if irr then DotPatternCtx else FunctionSpaceDomainCtx) a b' <- toConcreteTop b return $ C.Fun (getRange i) (addRel a' $ mkArg a') b' where irr = getRelevance a `elem` [Irrelevant, NonStrict] addRel a e = case getRelevance a of Irrelevant -> addDot a e NonStrict -> addDot a (addDot a e) _ -> e addDot a e = C.Dot (getRange a) e mkArg (Arg info e) = case getHiding info of Hidden -> HiddenArg (getRange e) (unnamed e) Instance{} -> InstanceArg (getRange e) (unnamed e) NotHidden -> e toConcrete (A.Set i 0) = return $ C.Set (getRange i) toConcrete (A.Set i n) = return $ C.SetN (getRange i) n toConcrete (A.Prop i) = return $ C.Prop (getRange i) toConcrete (A.Let i ds e) = bracket lamBrackets $ bindToConcrete ds $ \ds' -> do e' <- toConcreteTop e return $ C.Let (getRange i) (concat ds') e' toConcrete (A.Rec i fs) = bracket appBrackets $ do C.Rec (getRange i) . map (fmap (\x -> ModuleAssignment x [] defaultImportDir)) <$> toConcreteTop fs toConcrete (A.RecUpdate i e fs) = bracket appBrackets $ do C.RecUpdate (getRange i) <$> toConcrete e <*> toConcreteTop fs toConcrete (A.ETel tel) = do tel <- concat <$> toConcrete tel return $ C.ETel tel toConcrete (A.ScopedExpr _ e) = toConcrete e toConcrete (A.QuoteGoal i x e) = bracket lamBrackets $ bindToConcrete x $ \ x' -> do e' <- toConcrete e return $ C.QuoteGoal (getRange i) x' e' toConcrete (A.QuoteContext i) = return $ C.QuoteContext (getRange i) toConcrete (A.Quote i) = return $ C.Quote (getRange i) toConcrete (A.QuoteTerm i) = return $ C.QuoteTerm (getRange i) toConcrete (A.Unquote i) = return $ C.Unquote (getRange i) toConcrete (A.Tactic i e xs ys) = do e' <- toConcrete e xs' <- toConcrete xs ys' <- toConcrete ys let r = getRange i rawtac = foldl (C.App r) e' xs' return $ C.Tactic (getRange i) rawtac (map namedArg ys') -- Andreas, 2012-04-02: TODO! print DontCare as irrAxiom -- Andreas, 2010-10-05 print irrelevant things as ordinary things toConcrete (A.DontCare e) = C.Dot r . C.Paren r <$> toConcrete e where r = getRange e toConcrete (A.PatternSyn n) = C.Ident <$> toConcrete n makeDomainFree :: A.LamBinding -> A.LamBinding makeDomainFree b@(A.DomainFull (A.TypedBindings r (Arg info (A.TBind _ [WithHiding h x] t)))) = case unScope t of A.Underscore MetaInfo{metaNumber = Nothing} -> A.DomainFree (mapHiding (mappend h) info) x _ -> b makeDomainFree b = b -- Christian Sattler, 2017-08-05, fixing #2669 -- Both methods of ToConcrete (FieldAssignment' a) (FieldAssignment' c) need -- to be implemented, each in terms of the corresponding one of ToConcrete a c. -- This mirrors the instance ToConcrete (Arg a) (Arg c). -- The default implementations of ToConcrete are not valid semantically. instance ToConcrete a c => ToConcrete (FieldAssignment' a) (FieldAssignment' c) where toConcrete = traverse toConcrete bindToConcrete (FieldAssignment name a) ret = bindToConcrete a $ ret . FieldAssignment name -- Binder instances ------------------------------------------------------- instance ToConcrete A.LamBinding [C.LamBinding] where bindToConcrete (A.DomainFree info x) ret = bindToConcrete x $ ret . (:[]) . C.DomainFree info . mkBoundName_ bindToConcrete (A.DomainFull b) ret = bindToConcrete b $ ret . map C.DomainFull instance ToConcrete A.TypedBindings [C.TypedBindings] where bindToConcrete (A.TypedBindings r bs) ret = bindToConcrete bs $ \cbs -> ret (map (C.TypedBindings r) $ recoverLabels bs cbs) where recoverLabels :: Arg A.TypedBinding -> Arg C.TypedBinding -> [Arg C.TypedBinding] recoverLabels b cb | visible b = [cb] -- We don't care about labels for explicit args | otherwise = traverse (recover (unArg b)) cb recover (A.TBind _ xs _) (C.TBind r ys e) = tbind r e (zipWith label xs ys) recover A.TLet{} c@C.TLet{} = [c] recover _ _ = __IMPOSSIBLE__ tbinds r e [] = [] tbinds r e xs = [ C.TBind r xs e ] tbind r e xs = case span ((\ x -> boundLabel x == boundName x) . dget) xs of (xs, x:ys) -> tbinds r e xs ++ [ C.TBind r [x] e ] ++ tbind r e ys (xs, []) -> tbinds r e xs label x = fmap $ \ y -> y { boundLabel = nameConcrete $ dget x } instance ToConcrete A.TypedBinding C.TypedBinding where bindToConcrete (A.TBind r xs e) ret = bindToConcrete xs $ \ xs -> do e <- toConcreteTop e ret $ C.TBind r (map (fmap mkBoundName_) xs) e bindToConcrete (A.TLet r lbs) ret = bindToConcrete lbs $ \ ds -> do ret $ C.TLet r $ concat ds instance ToConcrete LetBinding [C.Declaration] where bindToConcrete (LetBind i info x t e) ret = bindToConcrete x $ \x -> do (t,(e, [], [], [])) <- toConcrete (t, A.RHS e Nothing) ret $ addInstanceB (isInstance info) $ [ C.TypeSig info x t , C.FunClause (C.LHS (C.IdentP $ C.QName x) [] [] []) e C.NoWhere False ] -- TODO: bind variables bindToConcrete (LetPatBind i p e) ret = do p <- toConcrete p e <- toConcrete e ret [ C.FunClause (C.LHS p [] [] []) (C.RHS e) NoWhere False ] bindToConcrete (LetApply i x modapp _ _) ret = do x' <- unqualify <$> toConcrete x modapp <- toConcrete modapp let r = getRange modapp open = fromMaybe DontOpen $ minfoOpenShort i dir = fromMaybe defaultImportDir{ importDirRange = r } $ minfoDirective i -- This is no use since toAbstract LetDefs is in localToAbstract. local (openModule' x dir id) $ ret [ C.ModuleMacro (getRange i) x' modapp open dir ] bindToConcrete (LetOpen i x _) ret = do x' <- toConcrete x let dir = fromMaybe defaultImportDir $ minfoDirective i local (openModule' x dir restrictPrivate) $ ret [ C.Open (getRange i) x' dir ] bindToConcrete (LetDeclaredVariable _) ret = -- Note that the range of the declaration site is dropped. ret [] data AsWhereDecls = AsWhereDecls [A.Declaration] instance ToConcrete AsWhereDecls WhereClause where bindToConcrete (AsWhereDecls []) ret = ret C.NoWhere bindToConcrete (AsWhereDecls ds@[Section _ am _ _]) ret = do ds' <- declsToConcrete ds cm <- unqualify <$> lookupModule am -- Andreas, 2016-07-08 I put PublicAccess in the following SomeWhere -- Should not really matter for printing... let wh' = (if isNoName cm then AnyWhere else SomeWhere cm PublicAccess) $ ds' local (openModule' am defaultImportDir id) $ ret wh' bindToConcrete (AsWhereDecls ds) ret = ret . AnyWhere =<< declsToConcrete ds mergeSigAndDef :: [C.Declaration] -> [C.Declaration] mergeSigAndDef (C.RecordSig _ x bs e : C.Record r y ind eta c _ Nothing fs : ds) | x == y = C.Record r y ind eta c bs (Just e) fs : mergeSigAndDef ds mergeSigAndDef (C.DataSig _ _ x bs e : C.Data r i y _ Nothing cs : ds) | x == y = C.Data r i y bs (Just e) cs : mergeSigAndDef ds mergeSigAndDef (d : ds) = d : mergeSigAndDef ds mergeSigAndDef [] = [] openModule' :: A.ModuleName -> C.ImportDirective -> (Scope -> Scope) -> Env -> Env openModule' x dir restrict env = env{currentScope = sInfo{scopeModules = mods'}} where sInfo = currentScope env amod = scopeCurrent sInfo mods = scopeModules sInfo news = setScopeAccess PrivateNS $ applyImportDirective dir $ maybe emptyScope restrict $ Map.lookup x mods mods' = Map.update (Just . (`mergeScope` news)) amod mods -- Declaration instances -------------------------------------------------- declsToConcrete :: [A.Declaration] -> AbsToCon [C.Declaration] declsToConcrete ds = mergeSigAndDef . concat <$> toConcrete ds instance ToConcrete A.RHS (C.RHS, [C.Expr], [C.Expr], [C.Declaration]) where toConcrete (A.RHS e (Just c)) = return (C.RHS c, [], [], []) toConcrete (A.RHS e Nothing) = do e <- toConcrete e return (C.RHS e, [], [], []) toConcrete A.AbsurdRHS = return (C.AbsurdRHS, [], [], []) toConcrete (A.WithRHS _ es cs) = do es <- toConcrete es cs <- noTakenNames $ concat <$> toConcrete cs return (C.AbsurdRHS, [], es, cs) toConcrete (A.RewriteRHS xeqs rhs wh) = do wh <- declsToConcrete wh (rhs, eqs', es, whs) <- toConcrete rhs unless (null eqs') __IMPOSSIBLE__ eqs <- toConcrete $ map snd xeqs return (rhs, eqs, es, wh ++ whs) instance ToConcrete (Maybe A.QName) (Maybe C.Name) where toConcrete Nothing = return Nothing toConcrete (Just x) = do x' <- toConcrete (qnameName x) return $ Just x' instance ToConcrete (Constr A.Constructor) C.Declaration where toConcrete (Constr (A.ScopedDecl scope [d])) = withScope scope $ toConcrete (Constr d) toConcrete (Constr (A.Axiom _ i info Nothing x t)) = do x' <- unsafeQNameToName <$> toConcrete x t' <- toConcreteTop t return $ C.TypeSig info x' t' toConcrete (Constr (A.Axiom _ _ _ (Just _) _ _)) = __IMPOSSIBLE__ toConcrete (Constr d) = head <$> toConcrete d instance ToConcrete a C.LHS => ToConcrete (A.Clause' a) [C.Declaration] where toConcrete (A.Clause lhs _ _ rhs wh catchall) = bindToConcrete lhs $ \lhs -> case lhs of C.LHS p wps _ _ -> do bindToConcrete (AsWhereDecls wh) $ \wh' -> do (rhs', eqs, with, wcs) <- toConcreteTop rhs return $ FunClause (C.LHS p wps eqs with) rhs' wh' catchall : wcs C.Ellipsis {} -> __IMPOSSIBLE__ -- TODO: Is the case above impossible? Previously there was -- no code for it, but GHC 7's completeness checker spotted -- that the case was not covered. instance ToConcrete A.ModuleApplication C.ModuleApplication where toConcrete (A.SectionApp tel y es) = do y <- toConcreteCtx FunctionCtx y bindToConcrete tel $ \tel -> do es <- toConcreteCtx ArgumentCtx es let r = fuseRange y es return $ C.SectionApp r (concat tel) (foldl (C.App r) (C.Ident y) es) toConcrete (A.RecordModuleIFS recm) = do recm <- toConcrete recm return $ C.RecordModuleIFS (getRange recm) recm instance ToConcrete A.Declaration [C.Declaration] where toConcrete (ScopedDecl scope ds) = withScope scope (declsToConcrete ds) toConcrete (Axiom _ i info mp x t) = do x' <- unsafeQNameToName <$> toConcrete x withAbstractPrivate i $ withInfixDecl i x' $ do t' <- toConcreteTop t return $ (case mp of Nothing -> [] Just occs -> [C.Pragma (PolarityPragma noRange x' occs)]) ++ [C.Postulate (getRange i) [C.TypeSig info x' t']] toConcrete (A.Field i x t) = do x' <- unsafeQNameToName <$> toConcrete x withAbstractPrivate i $ withInfixDecl i x' $ do t' <- toConcreteTop t return [C.Field (defInstance i) x' t'] toConcrete (A.Primitive i x t) = do x' <- unsafeQNameToName <$> toConcrete x withAbstractPrivate i $ withInfixDecl i x' $ do t' <- toConcreteTop t return [C.Primitive (getRange i) [C.TypeSig defaultArgInfo x' t']] -- Primitives are always relevant. toConcrete (A.FunDef i _ _ cs) = withAbstractPrivate i $ concat <$> toConcrete cs toConcrete (A.DataSig i x bs t) = withAbstractPrivate i $ bindToConcrete bs $ \tel' -> do x' <- unsafeQNameToName <$> toConcrete x t' <- toConcreteTop t return [ C.DataSig (getRange i) Inductive x' (map C.DomainFull $ concat tel') t' ] toConcrete (A.DataDef i x bs cs) = withAbstractPrivate i $ bindToConcrete (map makeDomainFree bs) $ \tel' -> do (x',cs') <- (unsafeQNameToName -*- id) <$> toConcrete (x, map Constr cs) return [ C.Data (getRange i) Inductive x' (concat tel') Nothing cs' ] toConcrete (A.RecSig i x bs t) = withAbstractPrivate i $ bindToConcrete bs $ \tel' -> do x' <- unsafeQNameToName <$> toConcrete x t' <- toConcreteTop t return [ C.RecordSig (getRange i) x' (map C.DomainFull $ concat tel') t' ] toConcrete (A.RecDef i x ind eta c bs t cs) = withAbstractPrivate i $ bindToConcrete (map makeDomainFree bs) $ \tel' -> do (x',cs') <- (unsafeQNameToName -*- id) <$> toConcrete (x, map Constr cs) return [ C.Record (getRange i) x' ind eta Nothing (concat tel') Nothing cs' ] toConcrete (A.Mutual i ds) = declsToConcrete ds toConcrete (A.Section i x tel ds) = do x <- toConcrete x bindToConcrete tel $ \tel -> do ds <- declsToConcrete ds return [ C.Module (getRange i) x (concat tel) ds ] toConcrete (A.Apply i x modapp _ _) = do x <- unsafeQNameToName <$> toConcrete x modapp <- toConcrete modapp let r = getRange modapp open = fromMaybe DontOpen $ minfoOpenShort i dir = fromMaybe defaultImportDir{ importDirRange = r } $ minfoDirective i return [ C.ModuleMacro (getRange i) x modapp open dir ] toConcrete (A.Import i x _) = do x <- toConcrete x let open = fromMaybe DontOpen $ minfoOpenShort i dir = fromMaybe defaultImportDir $ minfoDirective i return [ C.Import (getRange i) x Nothing open dir] toConcrete (A.Pragma i p) = do p <- toConcrete $ RangeAndPragma (getRange i) p return [C.Pragma p] toConcrete (A.Open i x _) = do x <- toConcrete x return [C.Open (getRange i) x defaultImportDir] toConcrete (A.PatternSynDef x xs p) = do C.QName x <- toConcrete x bindToConcrete xs $ \xs -> (:[]) . C.PatternSyn (getRange x) x xs <$> toConcrete (vacuous p :: A.Pattern) toConcrete (A.UnquoteDecl _ i xs e) = do let unqual (C.QName x) = return x unqual _ = __IMPOSSIBLE__ xs <- mapM (unqual <=< toConcrete) xs (:[]) . C.UnquoteDecl (getRange i) xs <$> toConcrete e toConcrete (A.UnquoteDef i xs e) = do let unqual (C.QName x) = return x unqual _ = __IMPOSSIBLE__ xs <- mapM (unqual <=< toConcrete) xs (:[]) . C.UnquoteDef (getRange i) xs <$> toConcrete e data RangeAndPragma = RangeAndPragma Range A.Pragma instance ToConcrete RangeAndPragma C.Pragma where toConcrete (RangeAndPragma r p) = case p of A.OptionsPragma xs -> return $ C.OptionsPragma r xs A.BuiltinPragma b e -> C.BuiltinPragma r b <$> toConcrete e A.BuiltinNoDefPragma b x -> C.BuiltinPragma r b . C.Ident <$> toConcrete x A.RewritePragma x -> C.RewritePragma r . singleton <$> toConcrete x A.CompiledTypePragma x hs -> do x <- toConcrete x return $ C.CompiledTypePragma r x hs A.CompiledDataPragma x hs hcs -> do x <- toConcrete x return $ C.CompiledDataPragma r x hs hcs A.CompiledPragma x hs -> do x <- toConcrete x return $ C.CompiledPragma r x hs A.CompilePragma b x s -> do x <- toConcrete x return $ C.CompilePragma r b x s A.CompiledExportPragma x hs -> do x <- toConcrete x return $ C.CompiledExportPragma r x hs A.CompiledJSPragma x e -> do x <- toConcrete x return $ C.CompiledJSPragma r x e A.CompiledUHCPragma x cr -> do x <- toConcrete x return $ C.CompiledUHCPragma r x cr A.CompiledDataUHCPragma x crd crcs -> do x <- toConcrete x return $ C.CompiledDataUHCPragma r x crd crcs A.StaticPragma x -> C.StaticPragma r <$> toConcrete x A.InjectivePragma x -> C.InjectivePragma r <$> toConcrete x A.InlinePragma x -> C.InlinePragma r <$> toConcrete x A.EtaPragma x -> C.EtaPragma r <$> toConcrete x A.DisplayPragma f ps rhs -> C.DisplayPragma r <$> toConcrete (A.DefP (PatRange noRange) (AmbQ [f]) ps) <*> toConcrete rhs -- Left hand sides -------------------------------------------------------- instance ToConcrete A.SpineLHS C.LHS where bindToConcrete lhs = bindToConcrete (A.spineToLhs lhs :: A.LHS) instance ToConcrete A.LHS C.LHS where bindToConcrete (A.LHS i lhscore wps) ret = do bindToConcreteCtx TopCtx lhscore $ \lhs -> bindToConcreteCtx TopCtx wps $ \wps -> ret $ C.LHS lhs wps [] [] instance ToConcrete A.LHSCore C.Pattern where bindToConcrete = bindToConcrete . lhsCoreToPattern appBracketsArgs :: [arg] -> PrecedenceStack -> Bool appBracketsArgs [] _ = False appBracketsArgs (_:_) ctx = appBrackets ctx -- Auxiliary wrappers for processing the bindings in patterns in the right order. newtype UserPattern a = UserPattern a newtype SplitPattern a = SplitPattern a newtype BindingPattern = BindingPat A.Pattern newtype FreshName = FreshenName A.Name instance ToConcrete FreshName A.Name where bindToConcrete (FreshenName x) ret = bindToConcrete x $ \ y -> ret x{ nameConcrete = y } -- Pass 1: (Issue #2729) -- Takes care of binding the originally user-written pattern variables, but doesn't actually -- translate anything to Concrete. instance ToConcrete (UserPattern A.Pattern) A.Pattern where bindToConcrete (UserPattern p) ret = case p of A.VarP x -> bindName' x $ ret $ A.VarP x A.WildP{} -> ret p A.ProjP{} -> ret p A.AbsurdP{} -> ret p A.LitP{} -> ret p A.DotP{} -> ret p -- Andreas, 2017-09-03, issue #2729: -- Do not go into patterns generated by case-split here! -- They are treated in a second pass. A.ConP i c args | patOrigin i == ConOSplit -> ret p | otherwise -> bindToConcrete (map UserPattern args) $ ret . A.ConP i c A.DefP i f args -> bindToConcrete (map UserPattern args) $ ret . A.DefP i f A.PatternSynP i f args -> bindToConcrete (map UserPattern args) $ ret . A.PatternSynP i f A.RecP i args -> bindToConcrete ((map . fmap) UserPattern args) $ ret . A.RecP i A.AsP i x p -> bindName' x $ bindToConcrete (UserPattern p) $ \ p -> ret (A.AsP i x p) instance ToConcrete (UserPattern (NamedArg A.Pattern)) (NamedArg A.Pattern) where bindToConcrete (UserPattern np) ret = case getOrigin np of CaseSplit -> ret np _ -> bindToConcrete (fmap (fmap UserPattern) np) ret -- Pass 2a: locate case-split pattern. Don't bind anything! instance ToConcrete (SplitPattern A.Pattern) A.Pattern where bindToConcrete (SplitPattern p) ret = case p of A.VarP x -> ret p A.WildP{} -> ret p A.ProjP{} -> ret p A.AbsurdP{} -> ret p A.LitP{} -> ret p A.DotP{} -> ret p -- Andreas, 2017-09-03, issue #2729: -- For patterns generated by case-split here, switch to freshening & binding. A.ConP i c args | patOrigin i == ConOSplit -> bindToConcrete ((map . fmap . fmap) BindingPat args) $ ret . A.ConP i c | otherwise -> bindToConcrete (map SplitPattern args) $ ret . A.ConP i c A.DefP i f args -> bindToConcrete (map SplitPattern args) $ ret . A.DefP i f A.PatternSynP i f args -> bindToConcrete (map SplitPattern args) $ ret . A.PatternSynP i f A.RecP i args -> bindToConcrete ((map . fmap) SplitPattern args) $ ret . A.RecP i A.AsP i x p -> bindToConcrete (SplitPattern p) $ \ p -> ret (A.AsP i x p) instance ToConcrete (SplitPattern (NamedArg A.Pattern)) (NamedArg A.Pattern) where bindToConcrete (SplitPattern np) ret = case getOrigin np of CaseSplit -> bindToConcrete (fmap (fmap BindingPat ) np) ret _ -> bindToConcrete (fmap (fmap SplitPattern) np) ret -- Pass 2b: -- Takes care of freshening and binding pattern variables introduced by case split. -- Still does not translate anything to Concrete. instance ToConcrete BindingPattern A.Pattern where bindToConcrete (BindingPat p) ret = case p of A.VarP x -> bindToConcrete (FreshenName x) $ ret . A.VarP A.WildP{} -> ret p A.ProjP{} -> ret p A.AbsurdP{} -> ret p A.LitP{} -> ret p A.DotP{} -> ret p A.ConP i c args -> bindToConcrete ((map . fmap . fmap) BindingPat args) $ ret . A.ConP i c A.DefP i f args -> bindToConcrete ((map . fmap . fmap) BindingPat args) $ ret . A.DefP i f A.PatternSynP i f args -> bindToConcrete ((map . fmap . fmap) BindingPat args) $ ret . A.PatternSynP i f A.RecP i args -> bindToConcrete ((map . fmap) BindingPat args) $ ret . A.RecP i A.AsP i x p -> bindToConcrete (FreshenName x) $ \ x -> bindToConcrete (BindingPat p) $ \ p -> ret (A.AsP i x p) instance ToConcrete A.Pattern C.Pattern where bindToConcrete p ret = do prec <- currentPrecedence bindToConcrete (UserPattern p) $ \ p -> do bindToConcrete (SplitPattern p) $ \ p -> do ret =<< do withPrecedence' prec $ toConcrete p toConcrete p = case p of A.VarP x -> C.IdentP . C.QName <$> toConcrete x A.WildP i -> return $ C.WildP (getRange i) A.ConP i (AmbQ []) args -> __IMPOSSIBLE__ A.ConP i xs@(AmbQ (x:_)) args -> tryOp x (A.ConP i xs) args A.ProjP _ _ (AmbQ []) -> __IMPOSSIBLE__ A.ProjP i ProjPrefix xs@(AmbQ (x:_)) -> C.IdentP <$> toConcrete x A.ProjP i _ xs@(AmbQ (x:_)) -> C.DotP (getRange x) UserWritten . C.Ident <$> toConcrete x A.DefP i (AmbQ []) _ -> __IMPOSSIBLE__ A.DefP i xs@(AmbQ (x:_)) args -> tryOp x (A.DefP i xs) args A.AsP i x p -> do (x, p) <- toConcreteCtx ArgumentCtx (x,p) return $ C.AsP (getRange i) x p A.AbsurdP i -> return $ C.AbsurdP (getRange i) A.LitP (LitQName r x) -> do x <- lookupQName AmbiguousNothing x bracketP_ appBrackets $ return $ C.AppP (C.QuoteP r) (defaultNamedArg (C.IdentP x)) A.LitP l -> return $ C.LitP l A.DotP i o e -> do c <- toConcreteCtx DotPatternCtx e case c of -- Andreas, 2016-02-04 print ._ pattern as _ pattern, -- following the fusing of WildP and ImplicitP. C.Underscore{} -> return $ C.WildP $ getRange i _ -> return $ C.DotP (getRange i) o c A.PatternSynP i n _ -> -- Ulf, 2016-11-29: This doesn't seem right. The underscore is a list -- of arguments, which we shouldn't really throw away! I guess this -- case is __IMPOSSIBLE__? C.IdentP <$> toConcrete n A.RecP i as -> C.RecP (getRange i) <$> mapM (traverse toConcrete) as where tryOp :: A.QName -> (A.Patterns -> A.Pattern) -> A.Patterns -> AbsToCon C.Pattern tryOp x f args = do -- Andreas, 2016-02-04, Issue #1792 -- To prevent failing of tryToRecoverOpAppP for overapplied operators, -- we take off the exceeding arguments first -- and apply them pointwise with C.AppP later. let (args1, args2) = splitAt (numHoles x) args let funCtx = if null args2 then id else withPrecedence FunctionCtx funCtx (tryToRecoverOpAppP $ f args1) >>= \case Just c -> applyTo args2 c Nothing -> applyTo args . C.IdentP =<< toConcrete x -- Note: applyTo [] c = return c applyTo args c = bracketP_ (appBracketsArgs args) $ do foldl C.AppP c <$> toConcreteCtx ArgumentCtx args -- Helpers for recovering C.OpApp ------------------------------------------ data Hd = HdVar A.Name | HdCon A.QName | HdDef A.QName cOpApp :: Range -> C.QName -> A.Name -> [Maybe C.Expr] -> C.Expr cOpApp r x n es = C.OpApp r x (Set.singleton n) (map (defaultNamedArg . placeholder) eps) where x0 = C.unqualify x positions | isPrefix x0 = [ Middle | _ <- drop 1 es ] ++ [End] | isPostfix x0 = [Beginning] ++ [ Middle | _ <- drop 1 es ] | isInfix x0 = [Beginning] ++ [ Middle | _ <- drop 2 es ] ++ [End] | otherwise = [ Middle | _ <- es ] eps = zip es positions placeholder (Nothing, pos) = Placeholder pos placeholder (Just e, _) = noPlaceholder (Ordinary e) tryToRecoverNatural :: A.Expr -> AbsToCon C.Expr -> AbsToCon C.Expr tryToRecoverNatural e def = do builtins <- stBuiltinThings <$> lift get let reified = do zero <- getAQName "ZERO" builtins suc <- getAQName "SUC" builtins explore zero suc 0 e case reified of Just n -> return $ C.Lit $ LitNat noRange n Nothing -> def where getAQName :: String -> BuiltinThings a -> Maybe A.QName getAQName str bs = do Builtin (I.Con hd _ _) <- Map.lookup str bs return $ I.conName hd explore :: A.QName -> A.QName -> Integer -> A.Expr -> Maybe Integer explore z s k (A.App _ (A.Con (AmbQ [f])) t) | f == s = let v = 1+k in v `seq` explore z s v $ namedArg t explore z s k (A.Con (AmbQ [x])) | x == z = Just k explore z s k (A.Lit (LitNat _ l)) = Just (k + l) explore _ _ _ _ = Nothing tryToRecoverOpApp :: A.Expr -> AbsToCon C.Expr -> AbsToCon C.Expr tryToRecoverOpApp e def = caseMaybeM (recoverOpApp bracket (isParenlessLambda . defaultNamedArg) cOpApp view e) def return where view e -- Do we have a series of inserted lambdas? | Just xs@(_:_) <- traverse insertedName bs = (,) <$> getHead hd <*> sectionArgs xs args where LamView bs body = AV.lamView e Application hd args = AV.appView body -- Only inserted domain-free visible lambdas come from sections. insertedName (i, A.DomainFree ai x) | getOrigin i == Inserted && visible ai = Just x insertedName _ = Nothing -- Build section arguments. Need to check that: -- lambda bound variables appear in the right order and only as -- top-level arguments. sectionArgs :: [A.Name] -> [NamedArg A.Expr] -> Maybe [NamedArg (Maybe A.Expr)] sectionArgs xs = go xs where noXs = getAll . foldExpr (\ case A.Var x -> All (notElem x xs) _ -> All True) go [] [] = return [] go (y : ys) (arg : args) | visible arg , A.Var y' <- namedArg arg , y == y' = (fmap (Nothing <$) arg :) <$> go ys args go ys (arg : args) | visible arg, noXs arg = ((fmap . fmap) Just arg :) <$> go ys args go _ _ = Nothing view e = (, (map . fmap . fmap) Just args) <$> getHead hd where Application hd args = AV.appView e getHead (Var x) = Just (HdVar x) getHead (Def f) = Just (HdDef f) getHead (Con (AmbQ (c : _))) = Just (HdCon c) getHead (Con (AmbQ [])) = __IMPOSSIBLE__ getHead _ = Nothing tryToRecoverOpAppP :: A.Pattern -> AbsToCon (Maybe C.Pattern) tryToRecoverOpAppP = recoverOpApp bracketP_ (const False) opApp view where opApp r x n ps = C.OpAppP r x (Set.singleton n) (map (defaultNamedArg . fromjust) ps) fromjust (Just x) = x fromjust Nothing = __IMPOSSIBLE__ -- `view` does not generate any `Nothing`s view p = case p of ConP _ (AmbQ (c:_)) ps -> Just (HdCon c, (map . fmap . fmap) Just ps) DefP _ (AmbQ (f:_)) ps -> Just (HdDef f, (map . fmap . fmap) Just ps) _ -> __IMPOSSIBLE__ -- ProjP _ _ (AmbQ (d:_)) -> Just (HdDef d, []) -- ? Andreas, 2016-04-21 -- _ -> Nothing recoverOpApp :: (ToConcrete a c, HasRange c) => ((PrecedenceStack -> Bool) -> AbsToCon c -> AbsToCon c) -> (a -> Bool) -- ^ Check for parenless lambdas -> (Range -> C.QName -> A.Name -> [Maybe c] -> c) -> (a -> Maybe (Hd, [NamedArg (Maybe a)])) -- ^ `Nothing` for sections -> a -> AbsToCon (Maybe c) recoverOpApp bracket isParenlessLam opApp view e = case view e of Nothing -> mDefault Just (hd, args) | all visible args -> do let args' = map namedArg args case hd of HdVar n | isNoName n -> mDefault | otherwise -> doQNameHelper id C.QName n args' HdDef qn -> doQNameHelper qnameName id qn args' HdCon qn -> doQNameHelper qnameName id qn args' | otherwise -> mDefault where mDefault = return Nothing doQNameHelper fixityHelper conHelper n as = do x <- conHelper <$> toConcrete n doQName (theFixity $ nameFixity n') x n' as (C.nameParts $ C.unqualify x) where n' = fixityHelper n -- fall-back (wrong number of arguments or no holes) doQName _ x _ es xs | null es = mDefault | length es /= numHoles x = mDefault -- binary case doQName fixity x n as xs | Hole <- head xs , Hole <- last xs = do let a1 = head as an = last as as' = case as of as@(_ : _ : _) -> init $ tail as _ -> __IMPOSSIBLE__ Just <$> do bracket (opBrackets' (maybe False isParenlessLam an) fixity) $ do e1 <- traverse (toConcreteCtx $ LeftOperandCtx fixity) a1 es <- (mapM . traverse) (toConcreteCtx InsideOperandCtx) as' en <- traverse (toConcreteCtx $ RightOperandCtx fixity) an return $ opApp (getRange (e1, en)) x n ([e1] ++ es ++ [en]) -- prefix doQName fixity x n as xs | Hole <- last xs = do let an = last as as' = case as of as@(_ : _) -> init as _ -> __IMPOSSIBLE__ Just <$> do bracket (opBrackets' (maybe False isParenlessLam an) fixity) $ do es <- (mapM . traverse) (toConcreteCtx InsideOperandCtx) as' en <- traverse (toConcreteCtx $ RightOperandCtx fixity) an return $ opApp (getRange (n, en)) x n (es ++ [en]) -- postfix doQName fixity x n as xs | Hole <- head xs = do let a1 = head as as' = tail as e1 <- traverse (toConcreteCtx $ LeftOperandCtx fixity) a1 es <- (mapM . traverse) (toConcreteCtx InsideOperandCtx) as' Just <$> do bracket (opBrackets fixity) $ return $ opApp (getRange (e1, n)) x n ([e1] ++ es) -- roundfix doQName _ x n as xs = do es <- (mapM . traverse) (toConcreteCtx InsideOperandCtx) as Just <$> do bracket roundFixBrackets $ return $ opApp (getRange x) x n es -- Some instances that are related to interaction with users ----------- instance ToConcrete InteractionId C.Expr where toConcrete (InteractionId i) = return $ C.QuestionMark noRange (Just i) instance ToConcrete NamedMeta C.Expr where toConcrete i = do return $ C.Underscore noRange (Just $ prettyShow i) Agda-2.5.3/src/full/Agda/Syntax/Translation/ConcreteToAbstract.hs0000644000000000000000000026220313154613124023006 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE UndecidableInstances #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE OverlappingInstances #-} #endif {-| Translation from "Agda.Syntax.Concrete" to "Agda.Syntax.Abstract". Involves scope analysis, figuring out infix operator precedences and tidying up definitions. -} module Agda.Syntax.Translation.ConcreteToAbstract ( ToAbstract(..), localToAbstract , concreteToAbstract_ , concreteToAbstract , NewModuleQName(..) , OldName(..) , TopLevel(..) , TopLevelInfo(..) , topLevelModuleName , AbstractRHS , NewModuleName, OldModuleName , NewName, OldQName , LeftHandSide, RightHandSide , PatName, APatName, LetDef, LetDefs ) where import Prelude hiding (mapM, null) import Control.Applicative import Control.Monad.Reader hiding (mapM) import Data.Foldable (Foldable, traverse_) import Data.Traversable (mapM, traverse) import Data.List ((\\), nub, foldl') import Data.Set (Set) import qualified Data.Set as Set import qualified Data.Map as Map import Data.Maybe import Data.Void import Agda.Syntax.Concrete as C hiding (topLevelModuleName) import Agda.Syntax.Concrete.Generic import Agda.Syntax.Concrete.Operators import Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Pattern ( patternVars ) import Agda.Syntax.Abstract.Pretty import qualified Agda.Syntax.Internal as I import Agda.Syntax.Position import Agda.Syntax.Literal import Agda.Syntax.Common import Agda.Syntax.Info import Agda.Syntax.Concrete.Definitions as C import Agda.Syntax.Fixity import Agda.Syntax.Notation import Agda.Syntax.Scope.Base import Agda.Syntax.Scope.Monad import Agda.Syntax.Translation.AbstractToConcrete (ToConcrete) import Agda.Syntax.IdiomBrackets import Agda.TypeChecking.Monad.Base hiding (ModuleInfo, MetaInfo) import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Monad.Trace (traceCall, setCurrentRange) import Agda.TypeChecking.Monad.State import Agda.TypeChecking.Monad.MetaVars (registerInteractionPoint) import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.Options import Agda.TypeChecking.Monad.Env (insideDotPattern, isInsideDotPattern) import Agda.TypeChecking.Rules.Builtin (isUntypedBuiltin, bindUntypedBuiltin) import Agda.TypeChecking.Patterns.Abstract (expandPatternSynonyms) import Agda.TypeChecking.Pretty hiding (pretty, prettyA) import Agda.TypeChecking.Warnings import Agda.Interaction.FindFile (checkModuleName) -- import Agda.Interaction.Imports -- for type-checking in ghci import {-# SOURCE #-} Agda.Interaction.Imports (scopeCheckImport) import Agda.Interaction.Options import qualified Agda.Interaction.Options.Lenses as Lens import Agda.Utils.Either import Agda.Utils.Except ( MonadError(catchError, throwError) ) import Agda.Utils.FileName import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import qualified Agda.Utils.Pretty as P import Agda.Utils.Pretty (render, Pretty, pretty, prettyShow) import Agda.Utils.Tuple import Agda.Interaction.FindFile ( rootNameModule ) #include "undefined.h" import Agda.Utils.Impossible import Agda.ImpossibleTest (impossibleTest) {-------------------------------------------------------------------------- Exceptions --------------------------------------------------------------------------} -- notAModuleExpr e = typeError $ NotAModuleExpr e notAnExpression :: C.Expr -> ScopeM A.Expr notAnExpression e = typeError $ NotAnExpression e nothingAppliedToHiddenArg :: C.Expr -> ScopeM A.Expr nothingAppliedToHiddenArg e = typeError $ NothingAppliedToHiddenArg e nothingAppliedToInstanceArg :: C.Expr -> ScopeM A.Expr nothingAppliedToInstanceArg e = typeError $ NothingAppliedToInstanceArg e notAValidLetBinding :: NiceDeclaration -> ScopeM a notAValidLetBinding d = typeError $ NotAValidLetBinding d -- Debugging printLocals :: Int -> String -> ScopeM () printLocals v s = verboseS "scope.top" v $ do locals <- getLocalVars reportSLn "scope.top" v $ s ++ " " ++ prettyShow locals {-------------------------------------------------------------------------- Helpers --------------------------------------------------------------------------} annotateDecl :: ScopeM A.Declaration -> ScopeM A.Declaration annotateDecl m = annotateDecls $ (:[]) <$> m annotateDecls :: ScopeM [A.Declaration] -> ScopeM A.Declaration annotateDecls m = do ds <- m s <- getScope return $ ScopedDecl s ds annotateExpr :: ScopeM A.Expr -> ScopeM A.Expr annotateExpr m = do e <- m s <- getScope return $ ScopedExpr s e -- | Make sure that each variable occurs only once. checkPatternLinearity :: [A.Pattern' e] -> ScopeM () checkPatternLinearity ps = do unlessNull (duplicates $ map nameConcrete $ patternVars ps) $ \ ys -> do typeError $ RepeatedVariablesInPattern ys -- | Make sure that there are no dot patterns (called on pattern synonyms). noDotPattern :: String -> A.Pattern' e -> ScopeM (A.Pattern' Void) noDotPattern err = traverse $ const $ typeError $ GenericError err -- | Compute the type of the record constructor (with bogus target type) recordConstructorType :: [NiceDeclaration] -> ScopeM C.Expr recordConstructorType fields = build <$> mapM validForLet fs where -- drop all declarations after the last field declaration fs = reverse $ dropWhile notField $ reverse fields notField NiceField{} = False notField _ = True -- | Check that declarations before last field can be handled -- by current translation into let. -- -- Sometimes a declaration is valid with minor modifications. validForLet :: NiceDeclaration -> ScopeM NiceDeclaration validForLet d = do let failure = traceCall (SetRange $ getRange d) $ typeError $ NotValidBeforeField d case d of -- Andreas, 2013-11-08 -- Turn @open public@ into just @open@, since we cannot have an -- @open public@ in a @let@. Fixes issue #532. C.NiceOpen r m dir -> return $ C.NiceOpen r m dir{ publicOpen = False } C.NiceModuleMacro r p x modapp open dir -> return $ C.NiceModuleMacro r p x modapp open dir{ publicOpen = False } C.NiceField{} -> return d C.NiceMutual _ _ _ [ C.FunSig _ _ _ _ _instanc macro _info _ _ _ , C.FunDef _ _ _ abstract _ _ _ [ C.Clause _top _catchall (C.LHS _p [] [] []) (C.RHS _rhs) NoWhere [] ] ] | abstract /= AbstractDef && macro /= MacroDef -> -- TODO: this is still too generous, we also need to check that _p -- is only variable patterns. return d C.NiceMutual{} -> failure -- TODO: some of these cases might be __IMPOSSIBLE__ C.Axiom{} -> failure C.PrimitiveFunction{} -> failure C.NiceModule{} -> failure C.NiceImport{} -> failure C.NicePragma{} -> failure C.NiceRecSig{} -> failure C.NiceDataSig{} -> failure C.NiceFunClause{} -> failure C.FunSig{} -> failure -- Note: these are bundled with FunDef in NiceMutual C.FunDef{} -> failure C.DataDef{} -> failure C.RecDef{} -> failure C.NicePatternSyn{} -> failure C.NiceUnquoteDecl{} -> failure C.NiceUnquoteDef{} -> failure build fs = let (ds1, ds2) = span notField fs in lets (concatMap notSoNiceDeclarations ds1) $ fld ds2 -- Turn a field declaration into a the domain of a Pi-type fld [] = C.SetN noRange 0 -- todo: nicer fld (NiceField r f _ _ _ x (Arg info e) : fs) = C.Pi [C.TypedBindings r $ Arg info (C.TBind r [pure $ mkBoundName x f] e)] $ build fs where r = getRange x fld _ = __IMPOSSIBLE__ -- Turn non-field declarations into a let binding. -- Smart constructor for C.Let: lets [] c = c lets ds c = C.Let (getRange ds) ds c checkModuleApplication :: C.ModuleApplication -> ModuleName -> C.Name -> C.ImportDirective -> ScopeM (A.ModuleApplication, ScopeCopyInfo, A.ImportDirective) checkModuleApplication (C.SectionApp _ tel e) m0 x dir' = do reportSDoc "scope.decl" 70 $ vcat $ [ text $ "scope checking ModuleApplication " ++ prettyShow x ] -- For the following, set the current module to be m0. withCurrentModule m0 $ do -- Check that expression @e@ is of the form @m args@. (m, args) <- parseModuleApplication e -- Scope check the telescope (introduces bindings!). tel' <- toAbstract tel -- Scope check the old module name and the module args. (m1, args') <- toAbstract (OldModuleName m, args) -- Drop constructors (OnlyQualified) if there are arguments. The record constructor -- isn't properly in the record module, so copying it will lead to badness. let noRecConstr | null args = id | otherwise = removeOnlyQualified -- Copy the scope associated with m and take the parts actually imported. (adir, s) <- applyImportDirectiveM (C.QName x) dir' =<< getNamedScope m1 (s', copyInfo) <- copyScope m m0 (noRecConstr s) -- Set the current scope to @s'@ modifyCurrentScope $ const s' printScope "mod.inst" 20 "copied source module" reportSDoc "scope.mod.inst" 30 $ return $ pretty copyInfo let amodapp = A.SectionApp tel' m1 args' reportSDoc "scope.decl" 70 $ vcat $ [ text $ "scope checked ModuleApplication " ++ prettyShow x ] reportSDoc "scope.decl" 70 $ vcat $ [ nest 2 $ prettyA amodapp ] return (amodapp, copyInfo, adir) checkModuleApplication (C.RecordModuleIFS _ recN) m0 x dir' = withCurrentModule m0 $ do m1 <- toAbstract $ OldModuleName recN s <- getNamedScope m1 (adir, s) <- applyImportDirectiveM recN dir' s (s', copyInfo) <- copyScope recN m0 (removeOnlyQualified s) modifyCurrentScope $ const s' printScope "mod.inst" 20 "copied record module" return (A.RecordModuleIFS m1, copyInfo, adir) -- | @checkModuleMacro mkApply range access concreteName modapp open dir@ -- -- Preserves local variables. checkModuleMacro :: (Pretty c, ToConcrete a c) => (ModuleInfo -> ModuleName -> A.ModuleApplication -> ScopeCopyInfo -> A.ImportDirective -> a) -> Range -> Access -> C.Name -> C.ModuleApplication -> OpenShortHand -> C.ImportDirective -> ScopeM [a] checkModuleMacro apply r p x modapp open dir = do reportSDoc "scope.decl" 70 $ vcat $ [ text $ "scope checking ModuleMacro " ++ prettyShow x ] notPublicWithoutOpen open dir m0 <- toAbstract (NewModuleName x) reportSDoc "scope.decl" 90 $ text "NewModuleName: m0 =" <+> prettyA m0 printScope "mod.inst" 20 "module macro" -- If we're opening a /named/ module, the import directive is -- applied to the "open", otherwise to the module itself. However, -- "public" is always applied to the "open". let (moduleDir, openDir) = case (open, isNoName x) of (DoOpen, False) -> (defaultImportDir, dir) (DoOpen, True) -> ( dir { publicOpen = False } , defaultImportDir { publicOpen = publicOpen dir } ) (DontOpen, _) -> (dir, defaultImportDir) -- Restore the locals after module application has been checked. (modapp', copyInfo, adir') <- withLocalVars $ checkModuleApplication modapp m0 x moduleDir printScope "mod.inst.app" 20 "checkModuleMacro, after checkModuleApplication" reportSDoc "scope.decl" 90 $ text "after mod app: trying to print m0 ..." reportSDoc "scope.decl" 90 $ text "after mod app: m0 =" <+> prettyA m0 bindModule p x m0 reportSDoc "scope.decl" 90 $ text "after bindMod: m0 =" <+> prettyA m0 printScope "mod.inst.copy.after" 20 "after copying" -- Open the module if DoOpen. -- Andreas, 2014-09-02 openModule_ might shadow some locals! adir <- case open of DontOpen -> return adir' DoOpen -> openModule_ (C.QName x) openDir printScope "mod.inst" 20 $ show open reportSDoc "scope.decl" 90 $ text "after open : m0 =" <+> prettyA m0 stripNoNames printScope "mod.inst" 10 $ "after stripping" reportSDoc "scope.decl" 90 $ text "after stripNo: m0 =" <+> prettyA m0 let m = m0 `withRangesOf` [x] adecls = [ apply info m modapp' copyInfo adir ] reportSDoc "scope.decl" 70 $ vcat $ [ text $ "scope checked ModuleMacro " ++ prettyShow x ] reportSLn "scope.decl" 90 $ "info = " ++ show info reportSLn "scope.decl" 90 $ "m = " ++ prettyShow m reportSLn "scope.decl" 90 $ "modapp' = " ++ show modapp' reportSDoc "scope.decl" 90 $ return $ pretty copyInfo reportSDoc "scope.decl" 70 $ vcat $ map (nest 2 . prettyA) adecls return adecls where info = ModuleInfo { minfoRange = r , minfoAsName = Nothing , minfoAsTo = renamingRange dir , minfoOpenShort = Just open , minfoDirective = Just dir } -- | The @public@ keyword must only be used together with @open@. notPublicWithoutOpen :: OpenShortHand -> C.ImportDirective -> ScopeM () notPublicWithoutOpen DoOpen dir = return () notPublicWithoutOpen DontOpen dir = when (publicOpen dir) $ typeError $ GenericError "The public keyword must only be used together with the open keyword" -- | Computes the range of all the \"to\" keywords used in a renaming -- directive. renamingRange :: C.ImportDirective -> Range renamingRange = getRange . map renToRange . impRenaming -- | Scope check a 'NiceOpen'. checkOpen :: Range -> C.QName -> C.ImportDirective -- ^ Arguments of 'NiceOpen' -> ScopeM (ModuleInfo, A.ModuleName, A.ImportDirective) -- ^ Arguments of 'A.Open' checkOpen r x dir = do reportSDoc "scope.decl" 70 $ do cm <- getCurrentModule vcat $ [ text "scope checking NiceOpen " <> return (pretty x) , text " getCurrentModule = " <> prettyA cm , text $ " getCurrentModule (raw) = " ++ show cm , text $ " C.ImportDirective = " ++ prettyShow dir ] -- Andreas, 2017-01-01, issue #2377: warn about useless `public` when (publicOpen dir) $ do whenM ((A.noModuleName ==) <$> getCurrentModule) $ do warning $ UselessPublic m <- toAbstract (OldModuleName x) printScope "open" 20 $ "opening " ++ prettyShow x adir <- openModule_ x dir printScope "open" 20 $ "result:" let minfo = ModuleInfo { minfoRange = r , minfoAsName = Nothing , minfoAsTo = renamingRange dir , minfoOpenShort = Nothing , minfoDirective = Just dir } let adecls = [A.Open minfo m adir] reportSDoc "scope.decl" 70 $ vcat $ [ text $ "scope checked NiceOpen " ++ prettyShow x ] ++ map (nest 2 . prettyA) adecls return (minfo, m, adir) {-------------------------------------------------------------------------- Translation --------------------------------------------------------------------------} concreteToAbstract_ :: ToAbstract c a => c -> ScopeM a concreteToAbstract_ x = toAbstract x concreteToAbstract :: ToAbstract c a => ScopeInfo -> c -> ScopeM a concreteToAbstract scope x = withScope_ scope (toAbstract x) -- | Things that can be translated to abstract syntax are instances of this -- class. class ToAbstract concrete abstract | concrete -> abstract where toAbstract :: concrete -> ScopeM abstract -- | This function should be used instead of 'toAbstract' for things that need -- to keep track of precedences to make sure that we don't forget about it. toAbstractCtx :: ToAbstract concrete abstract => Precedence -> concrete -> ScopeM abstract toAbstractCtx ctx c = withContextPrecedence ctx $ toAbstract c toAbstractTopCtx :: ToAbstract c a => c -> ScopeM a toAbstractTopCtx = toAbstractCtx TopCtx toAbstractHiding :: (LensHiding h, ToAbstract c a) => h -> c -> ScopeM a toAbstractHiding h = toAbstractCtx $ hiddenArgumentCtx $ getHiding h setContextCPS :: Precedence -> (a -> ScopeM b) -> ((a -> ScopeM b) -> ScopeM b) -> ScopeM b setContextCPS p ret f = withContextPrecedence p $ f $ bracket_ popContextPrecedence (\ _ -> pushContextPrecedence p) . ret localToAbstractCtx :: ToAbstract concrete abstract => Precedence -> concrete -> (abstract -> ScopeM a) -> ScopeM a localToAbstractCtx ctx c ret = setContextCPS ctx ret (localToAbstract c) -- | This operation does not affect the scope, i.e. the original scope -- is restored upon completion. localToAbstract :: ToAbstract c a => c -> (a -> ScopeM b) -> ScopeM b localToAbstract x ret = fst <$> localToAbstract' x ret -- | Like 'localToAbstract' but returns the scope after the completion of the -- second argument. localToAbstract' :: ToAbstract c a => c -> (a -> ScopeM b) -> ScopeM (b, ScopeInfo) localToAbstract' x ret = do scope <- getScope withScope scope $ ret =<< toAbstract x instance (ToAbstract c1 a1, ToAbstract c2 a2) => ToAbstract (c1,c2) (a1,a2) where toAbstract (x,y) = (,) <$> toAbstract x <*> toAbstract y instance (ToAbstract c1 a1, ToAbstract c2 a2, ToAbstract c3 a3) => ToAbstract (c1,c2,c3) (a1,a2,a3) where toAbstract (x,y,z) = flatten <$> toAbstract (x,(y,z)) where flatten (x,(y,z)) = (x,y,z) #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPABLE #-} ToAbstract c a => ToAbstract [c] [a] where #else instance ToAbstract c a => ToAbstract [c] [a] where #endif toAbstract = mapM toAbstract instance (ToAbstract c1 a1, ToAbstract c2 a2) => ToAbstract (Either c1 c2) (Either a1 a2) where toAbstract = traverseEither toAbstract toAbstract instance ToAbstract c a => ToAbstract (Maybe c) (Maybe a) where toAbstract = traverse toAbstract -- Names ------------------------------------------------------------------ data NewName a = NewName { newLetBound :: Bool -- bound by a @let@? , newName :: a } data OldQName = OldQName C.QName (Maybe (Set A.Name)) -- ^ If a set is given, then the first name must correspond to one -- of the names in the set. newtype OldName a = OldName a data PatName = PatName C.QName (Maybe (Set A.Name)) -- ^ If a set is given, then the first name must correspond to one -- of the names in the set. instance ToAbstract (NewName C.Name) A.Name where toAbstract (NewName b x) = do y <- freshAbstractName_ x bindVariable b x y return y instance ToAbstract (NewName C.BoundName) A.Name where toAbstract (NewName b BName{ boundName = x, bnameFixity = fx }) = do y <- freshAbstractName fx x bindVariable b x y return y instance ToAbstract OldQName A.Expr where toAbstract (OldQName x ns) = do qx <- resolveName' allKindsOfNames ns x reportSLn "scope.name" 10 $ "resolved " ++ prettyShow x ++ ": " ++ prettyShow qx case qx of VarName x' _ -> return $ A.Var x' DefinedName _ d -> return $ nameExpr d FieldName ds -> return $ A.Proj ProjPrefix $ AmbQ (map anameName ds) ConstructorName ds -> return $ A.Con $ AmbQ (map anameName ds) UnknownName -> notInScope x PatternSynResName d -> return $ nameExpr d data APatName = VarPatName A.Name | ConPatName [AbstractName] | PatternSynPatName AbstractName instance ToAbstract PatName APatName where toAbstract (PatName x ns) = do reportSLn "scope.pat" 10 $ "checking pattern name: " ++ prettyShow x rx <- resolveName' [ConName, PatternSynName] ns x -- Andreas, 2013-03-21 ignore conflicting names which cannot -- be meant since we are in a pattern z <- case (rx, x) of -- TODO: warn about shadowing (VarName y _, C.QName x) -> return $ Left x -- typeError $ RepeatedVariableInPattern y x (FieldName d, C.QName x) -> return $ Left x (DefinedName _ d, C.QName x) | DefName == anameKind d -> return $ Left x (UnknownName, C.QName x) -> return $ Left x (ConstructorName ds, _) -> return $ Right (Left ds) (PatternSynResName d, _) -> return $ Right (Right d) _ -> genericError $ "Cannot pattern match on non-constructor " ++ prettyShow x case z of Left x -> do reportSLn "scope.pat" 10 $ "it was a var: " ++ prettyShow x p <- VarPatName <$> toAbstract (NewName False x) printLocals 10 "bound it:" return p Right (Left ds) -> do reportSLn "scope.pat" 10 $ "it was a con: " ++ prettyShow (map anameName ds) return $ ConPatName ds Right (Right d) -> do reportSLn "scope.pat" 10 $ "it was a pat syn: " ++ prettyShow (anameName d) return $ PatternSynPatName d class ToQName a where toQName :: a -> C.QName instance ToQName C.Name where toQName = C.QName instance ToQName C.QName where toQName = id -- Should be a defined name. instance (Show a, ToQName a) => ToAbstract (OldName a) A.QName where toAbstract (OldName x) = do rx <- resolveName (toQName x) case rx of DefinedName _ d -> return $ anameName d -- We can get the cases below for DISPLAY pragmas ConstructorName (d : _) -> return $ anameName d -- We'll throw out this one, so it doesn't matter which one we pick ConstructorName [] -> __IMPOSSIBLE__ FieldName (d:_) -> return $ anameName d FieldName [] -> __IMPOSSIBLE__ PatternSynResName d -> return $ anameName d VarName x _ -> typeError $ GenericError $ "Not a defined name: " ++ prettyShow x UnknownName -> notInScope (toQName x) newtype NewModuleName = NewModuleName C.Name newtype NewModuleQName = NewModuleQName C.QName newtype OldModuleName = OldModuleName C.QName freshQModule :: A.ModuleName -> C.Name -> ScopeM A.ModuleName freshQModule m x = A.qualifyM m . mnameFromList . (:[]) <$> freshAbstractName_ x checkForModuleClash :: C.Name -> ScopeM () checkForModuleClash x = do ms <- scopeLookup (C.QName x) <$> getScope unless (null ms) $ do reportSLn "scope.clash" 20 $ "clashing modules ms = " ++ prettyShow ms reportSLn "scope.clash" 60 $ "clashing modules ms = " ++ show ms setCurrentRange x $ typeError $ ShadowedModule x $ map ((`withRangeOf` x) . amodName) ms instance ToAbstract NewModuleName A.ModuleName where toAbstract (NewModuleName x) = do checkForModuleClash x m <- getCurrentModule y <- freshQModule m x createModule Nothing y return y instance ToAbstract NewModuleQName A.ModuleName where toAbstract (NewModuleQName m) = toAbs noModuleName m where toAbs m (C.QName x) = do y <- freshQModule m x createModule Nothing y return y toAbs m (C.Qual x q) = do m' <- freshQModule m x toAbs m' q instance ToAbstract OldModuleName A.ModuleName where toAbstract (OldModuleName q) = setCurrentRange q $ do amodName <$> resolveModule q -- Expressions ------------------------------------------------------------ -- | Peel off 'C.HiddenArg' and represent it as an 'NamedArg'. mkNamedArg :: C.Expr -> NamedArg C.Expr mkNamedArg (C.HiddenArg _ e) = Arg (hide defaultArgInfo) e mkNamedArg (C.InstanceArg _ e) = Arg (makeInstance defaultArgInfo) e mkNamedArg e = Arg defaultArgInfo $ unnamed e -- | Peel off 'C.HiddenArg' and represent it as an 'Arg', throwing away any name. mkArg' :: ArgInfo -> C.Expr -> Arg C.Expr mkArg' info (C.HiddenArg _ e) = Arg (hide info) $ namedThing e mkArg' info (C.InstanceArg _ e) = Arg (makeInstance info) $ namedThing e mkArg' info e = Arg (setHiding NotHidden info) e -- | By default, arguments are @Relevant@. mkArg :: C.Expr -> Arg C.Expr mkArg e = mkArg' defaultArgInfo e -- | Parse a possibly dotted C.Expr as A.Expr. Bool = True if dotted. toAbstractDot :: Precedence -> C.Expr -> ScopeM (A.Expr, Bool) toAbstractDot prec e = do reportSLn "scope.irrelevance" 100 $ "toAbstractDot: " ++ (render $ pretty e) traceCall (ScopeCheckExpr e) $ case e of C.Dot _ e -> do e <- toAbstractCtx prec e return (e, True) C.RawApp r es -> do e <- parseApplication es toAbstractDot prec e C.Paren _ e -> toAbstractDot TopCtx e e -> do e <- toAbstractCtx prec e return (e, False) -- | Translate concrete expression under at least one binder into nested -- lambda abstraction in abstract syntax. toAbstractLam :: Range -> [C.LamBinding] -> C.Expr -> Precedence -> ScopeM A.Expr toAbstractLam r bs e ctx = do -- Translate the binders localToAbstract (map (C.DomainFull . makeDomainFull) bs) $ \ bs -> do -- Translate the body e <- toAbstractCtx ctx e -- We have at least one binder. Get first @b@ and rest @bs@. caseList bs __IMPOSSIBLE__ $ \ b bs -> do return $ A.Lam (setOrigin UserWritten $ (defaultLamInfo r) { lamParens = False }) b $ foldr mkLam e bs where -- We set the origin of the outer lambda to `UserWritten` and the origin of -- the inner lambdas to `Inserted`. mkLam b e = A.Lam (defaultLamInfo $ fuseRange b e) b e -- | Scope check extended lambda expression. scopeCheckExtendedLam :: Range -> [(C.LHS, C.RHS, WhereClause, Bool)] -> ScopeM A.Expr scopeCheckExtendedLam r cs = do whenM isInsideDotPattern $ genericError "Extended lambdas are not allowed in dot patterns" -- Find an unused name for the extended lambda definition. cname <- nextlamname r 0 extendedLambdaName name <- freshAbstractName_ cname reportSLn "scope.extendedLambda" 10 $ "new extended lambda name: " ++ prettyShow name qname <- qualifyName_ name bindName (PrivateAccess Inserted) DefName cname qname -- Compose a function definition and scope check it. a <- aModeToDef <$> asks envAbstractMode let insertApp (C.RawAppP r es) = C.RawAppP r $ IdentP (C.QName cname) : es insertApp (C.IdentP q ) = C.RawAppP r $ IdentP (C.QName cname) : [C.IdentP q] where r = getRange q insertApp _ = __IMPOSSIBLE__ d = C.FunDef r [] noFixity' {-'-} a NotInstanceDef __IMPOSSIBLE__ cname $ for cs $ \ (lhs, rhs, wh, ca) -> -- wh == NoWhere, see parser for more info C.Clause cname ca (mapLhsOriginalPattern insertApp lhs) rhs wh [] scdef <- toAbstract d -- Create the abstract syntax for the extended lambda. case scdef of A.ScopedDecl si [A.FunDef di qname' NotDelayed cs] -> do setScope si -- This turns into an A.ScopedExpr si $ A.ExtendedLam... return $ A.ExtendedLam (setOrigin UserWritten $ (defaultLamInfo r) { lamParens = False }) di qname' cs _ -> __IMPOSSIBLE__ where -- Get a concrete name that is not yet in scope. nextlamname :: Range -> Int -> String -> ScopeM C.Name nextlamname r i s = do let cname = C.Name r [Id $ stringToRawName $ s ++ show i] rn <- resolveName $ C.QName cname case rn of UnknownName -> return cname _ -> nextlamname r (i+1) s instance ToAbstract C.Expr A.Expr where toAbstract e = traceCall (ScopeCheckExpr e) $ annotateExpr $ case e of -- Names Ident x -> toAbstract (OldQName x Nothing) -- Literals C.Lit l -> case l of LitNat r n -> do let builtin | n < 0 = Just <$> primFromNeg -- negative literals are only allowed if FROMNEG is defined | otherwise = ensureInScope =<< getBuiltin' builtinFromNat l' = LitNat r (abs n) info = ExprRange r conv <- builtin case conv of Just (I.Def q _) -> return $ A.App info (A.Def q) $ defaultNamedArg (A.Lit l') _ -> return $ A.Lit l LitString r s -> do conv <- ensureInScope =<< getBuiltin' builtinFromString let info = ExprRange r case conv of Just (I.Def q _) -> return $ A.App info (A.Def q) $ defaultNamedArg (A.Lit l) _ -> return $ A.Lit l _ -> return $ A.Lit l where ensureInScope :: Maybe I.Term -> ScopeM (Maybe I.Term) ensureInScope v@(Just (I.Def q _)) = ifM (isNameInScope q <$> getScope) (return v) (return Nothing) ensureInScope _ = return Nothing -- Meta variables C.QuestionMark r n -> do scope <- getScope -- Andreas, 2014-04-06 create interaction point. ii <- registerInteractionPoint True r n let info = MetaInfo { metaRange = r , metaScope = scope , metaNumber = Nothing , metaNameSuggestion = "" } return $ A.QuestionMark info ii C.Underscore r n -> do scope <- getScope return $ A.Underscore $ MetaInfo { metaRange = r , metaScope = scope , metaNumber = maybe Nothing __IMPOSSIBLE__ n , metaNameSuggestion = fromMaybe "" n } -- Raw application C.RawApp r es -> do e <- parseApplication es toAbstract e -- Application C.App r e1 e2 -> do e1 <- toAbstractCtx FunctionCtx e1 e2 <- toAbstractCtx ArgumentCtx e2 return $ A.App (ExprRange r) e1 e2 -- Operator application C.OpApp r op ns es -> toAbstractOpApp op ns es -- With application C.WithApp r e es -> do e <- toAbstractCtx WithFunCtx e es <- mapM (toAbstractCtx WithArgCtx) es return $ A.WithApp (ExprRange r) e es -- Misplaced hidden argument C.HiddenArg _ _ -> nothingAppliedToHiddenArg e C.InstanceArg _ _ -> nothingAppliedToInstanceArg e -- Lambda C.AbsurdLam r h -> return $ A.AbsurdLam (setOrigin UserWritten $ (defaultLamInfo r) { lamParens = False }) h C.Lam r bs e -> toAbstractLam r bs e TopCtx -- Extended Lambda C.ExtendedLam r cs -> scopeCheckExtendedLam r cs -- Relevant and irrelevant non-dependent function type C.Fun r e1 e2 -> do Arg info (e0, dotted) <- traverse (toAbstractDot FunctionSpaceDomainCtx) $ mkArg e1 let e1 = Arg ((if dotted then setRelevance Irrelevant else id) info) e0 e2 <- toAbstractCtx TopCtx e2 return $ A.Fun (ExprRange r) e1 e2 -- Dependent function type e0@(C.Pi tel e) -> localToAbstract tel $ \tel -> do e <- toAbstractCtx TopCtx e let info = ExprRange (getRange e0) return $ A.Pi info tel e -- Sorts C.Set _ -> return $ A.Set (ExprRange $ getRange e) 0 C.SetN _ n -> return $ A.Set (ExprRange $ getRange e) n C.Prop _ -> return $ A.Prop $ ExprRange $ getRange e -- Let e0@(C.Let _ ds e) -> ifM isInsideDotPattern (genericError $ "Let-expressions are not allowed in dot patterns") $ localToAbstract (LetDefs ds) $ \ds' -> do e <- toAbstractCtx TopCtx e let info = ExprRange (getRange e0) return $ A.Let info ds' e -- Record construction C.Rec r fs -> do fs' <- toAbstractCtx TopCtx fs let ds' = [ d | Right (_, ds) <- fs', d <- ds ] fs'' = map (mapRight fst) fs' i = ExprRange r return $ A.mkLet i ds' (A.Rec i fs'') -- Record update C.RecUpdate r e fs -> do A.RecUpdate (ExprRange r) <$> toAbstract e <*> toAbstractCtx TopCtx fs -- Parenthesis C.Paren _ e -> setLamParens <$> toAbstractCtx TopCtx e where setP i = i { lamParens = True } setLamParens (A.Lam i bs e) = A.Lam (setP i) bs e setLamParens (A.AbsurdLam i h) = A.AbsurdLam (setP i) h setLamParens (A.ExtendedLam i def q cs) = A.ExtendedLam (setP i) def q cs setLamParens (A.ScopedExpr s e) = A.ScopedExpr s (setLamParens e) setLamParens e = e -- Idiom brackets C.IdiomBrackets r e -> toAbstractCtx TopCtx =<< parseIdiomBrackets r e -- Post-fix projections C.Dot r e -> A.Dot (ExprRange r) <$> toAbstract e -- Pattern things C.As _ _ _ -> notAnExpression e C.Absurd _ -> notAnExpression e -- Impossible things C.ETel _ -> __IMPOSSIBLE__ C.Equal{} -> genericError "Parse error: unexpected '='" -- Quoting C.QuoteGoal _ x e -> do x' <- toAbstract (NewName False x) e' <- toAbstract e return $ A.QuoteGoal (ExprRange $ getRange e) x' e' C.QuoteContext r -> return $ A.QuoteContext (ExprRange r) C.Quote r -> return $ A.Quote (ExprRange r) C.QuoteTerm r -> return $ A.QuoteTerm (ExprRange r) C.Unquote r -> return $ A.Unquote (ExprRange r) C.Tactic r e es -> do let AppView e' args = appView e e' : es <- toAbstract (e' : es) args <- toAbstract args return $ A.Tactic (ExprRange r) e' args (map defaultNamedArg es) -- DontCare C.DontCare e -> A.DontCare <$> toAbstract e instance ToAbstract C.ModuleAssignment (A.ModuleName, [A.LetBinding]) where toAbstract (C.ModuleAssignment m es i) | null es && isDefaultImportDir i = (\x-> (x, [])) <$> toAbstract (OldModuleName m) | otherwise = do x <- C.NoName (getRange m) <$> fresh r <- checkModuleMacro LetApply (getRange (m, es, i)) PublicAccess x (C.SectionApp (getRange (m , es)) [] (RawApp (fuseRange m es) (Ident m : es))) DontOpen i case r of (LetApply _ m' _ _ _ : _) -> return (m', r) _ -> __IMPOSSIBLE__ instance ToAbstract c a => ToAbstract (FieldAssignment' c) (FieldAssignment' a) where toAbstract = traverse toAbstract instance ToAbstract C.LamBinding A.LamBinding where toAbstract (C.DomainFree info x) = A.DomainFree info <$> toAbstract (NewName False x) toAbstract (C.DomainFull tb) = A.DomainFull <$> toAbstract tb makeDomainFull :: C.LamBinding -> C.TypedBindings makeDomainFull (C.DomainFull b) = b makeDomainFull (C.DomainFree info x) = C.TypedBindings r $ Arg info $ C.TBind r [pure x] $ C.Underscore r Nothing where r = getRange x instance ToAbstract C.TypedBindings A.TypedBindings where toAbstract (C.TypedBindings r bs) = A.TypedBindings r <$> toAbstract bs instance ToAbstract C.TypedBinding A.TypedBinding where toAbstract (C.TBind r xs t) = do t' <- toAbstractCtx TopCtx t xs' <- toAbstract $ map (fmap (NewName False)) xs return $ A.TBind r xs' t' toAbstract (C.TLet r ds) = A.TLet r <$> toAbstract (LetDefs ds) -- | Scope check a module (top level function). -- scopeCheckNiceModule :: Range -> Access -> C.Name -> C.Telescope -> ScopeM [A.Declaration] -> ScopeM [A.Declaration] scopeCheckNiceModule r p name tel checkDs | telHasOpenStmsOrModuleMacros tel = do -- Andreas, 2013-12-10: -- If the module telescope contains open statements -- or module macros (Issue 1299), -- add an extra anonymous module around the current one. -- Otherwise, the open statements would create -- identifiers in the parent scope of the current module. -- But open statements in the module telescope should -- only affect the current module! scopeCheckNiceModule noRange p noName_ [] $ scopeCheckNiceModule_ | otherwise = do scopeCheckNiceModule_ where -- The actual workhorse: scopeCheckNiceModule_ = do -- Check whether we are dealing with an anonymous module. -- This corresponds to a Coq/LEGO section. (name, p', open) <- do if isNoName name then do (i :: NameId) <- fresh return (C.NoName (getRange name) i, PrivateAccess Inserted, True) else return (name, p, False) -- Check and bind the module, using the supplied check for its contents. aname <- toAbstract (NewModuleName name) ds <- snd <$> do scopeCheckModule r (C.QName name) aname tel checkDs bindModule p' name aname -- If the module was anonymous open it public -- unless it's private, in which case we just open it (#2099) when open $ void $ -- We can discard the returned default A.ImportDirective. openModule_ (C.QName name) $ defaultImportDir { publicOpen = p == PublicAccess } return ds -- | Check whether a telescope has open declarations or module macros. telHasOpenStmsOrModuleMacros :: C.Telescope -> Bool telHasOpenStmsOrModuleMacros = any yesBinds where yesBinds (C.TypedBindings _ tb) = yesBind $ unArg tb yesBind C.TBind{} = False yesBind (C.TLet _ ds) = any yes ds yes C.ModuleMacro{} = True yes C.Open{} = True yes C.Import{} = True -- not __IMPOSSIBLE__, see Issue #1718 -- However, it does not matter what we return here, as this will -- become an error later: "Not a valid let-declaration". -- (Andreas, 2015-11-17) yes (C.Mutual _ ds) = any yes ds yes (C.Abstract _ ds) = any yes ds yes (C.Private _ _ ds) = any yes ds yes _ = False {- UNUSED telHasLetStms :: C.Telescope -> Bool telHasLetStms = any isLetBinds where isLetBinds (C.TypedBindings _ tb) = isLetBind $ unArg tb isLetBind C.TBind{} = False isLetBind C.TLet{} = True -} -- | We for now disallow let-bindings in @data@ and @record@ telescopes. -- This due "nested datatypes"; there is no easy interpretation of -- @ -- data D (A : Set) (open M A) (b : B) : Set where -- c : D (A × A) b → D A b -- @ -- where @B@ is brought in scope by @open M A@. class EnsureNoLetStms a where ensureNoLetStms :: a -> ScopeM () {- From ghc 7.2, there is LANGUAGE DefaultSignatures default ensureNoLetStms :: Foldable t => t a -> ScopeM () ensureNoLetStms = traverse_ ensureNoLetStms -} instance EnsureNoLetStms C.TypedBinding where ensureNoLetStms tb = case tb of C.TLet{} -> typeError $ IllegalLetInTelescope tb C.TBind{} -> return () instance EnsureNoLetStms a => EnsureNoLetStms (LamBinding' a) where ensureNoLetStms = traverse_ ensureNoLetStms instance EnsureNoLetStms a => EnsureNoLetStms (TypedBindings' a) where ensureNoLetStms = traverse_ ensureNoLetStms instance EnsureNoLetStms a => EnsureNoLetStms [a] where ensureNoLetStms = traverse_ ensureNoLetStms -- | Returns the scope inside the checked module. scopeCheckModule :: Range -> C.QName -- ^ The concrete name of the module. -> A.ModuleName -- ^ The abstract name of the module. -> C.Telescope -- ^ The module telescope. -> ScopeM [A.Declaration] -- ^ The code for checking the module contents. -> ScopeM (ScopeInfo, [A.Declaration]) scopeCheckModule r x qm tel checkDs = do printScope "module" 20 $ "checking module " ++ prettyShow x -- Andreas, 2013-12-10: Telescope does not live in the new module -- but its parent, so check it before entering the new module. -- This is important for Nicolas Pouillard's open parametrized modules -- statements inside telescopes. res <- withLocalVars $ do tel <- toAbstract tel withCurrentModule qm $ do -- pushScope m -- qm <- getCurrentModule printScope "module" 20 $ "inside module " ++ prettyShow x ds <- checkDs scope <- getScope return (scope, [ A.Section info (qm `withRangesOfQ` x) tel ds ]) -- Binding is done by the caller printScope "module" 20 $ "after module " ++ prettyShow x return res where info = ModuleInfo r noRange Nothing Nothing Nothing -- | Temporary data type to scope check a file. data TopLevel a = TopLevel { topLevelPath :: AbsolutePath -- ^ The file path from which we loaded this module. , topLevelExpectedName :: C.TopLevelModuleName -- ^ The expected module name -- (coming from the import statement that triggered scope checking this file). , topLevelTheThing :: a -- ^ The file content. } data TopLevelInfo = TopLevelInfo { topLevelDecls :: [A.Declaration] , topLevelScope :: ScopeInfo -- ^ as seen from inside the module } -- | The top-level module name. topLevelModuleName :: TopLevelInfo -> A.ModuleName topLevelModuleName topLevel = scopeCurrent (topLevelScope topLevel) -- | Top-level declarations are always -- @ -- (import|open)* -- a bunch of possibly opened imports -- module ThisModule ... -- the top-level module of this file -- @ instance ToAbstract (TopLevel [C.Declaration]) TopLevelInfo where toAbstract (TopLevel file expectedMName ds) = -- A file is a bunch of preliminary decls (imports etc.) -- plus a single module decl. case C.spanAllowedBeforeModule ds of -- If there are declarations after the top-level module -- we have to report a parse error here. (_, C.Module{} : d : _) -> traceCall (SetRange $ getRange d) $ genericError $ "No declarations allowed after top-level module." -- Otherwise, proceed. (outsideDecls, [ C.Module r m0 tel insideDecls ]) -> do -- If the module name is _ compute the name from the file path m <- if isNoName m0 then do -- Andreas, 2017-07-28, issue #1077 -- Check if the insideDecls end in a single module which has the same -- name as the file. In this case, it is highly likely that the user -- put some non-allowed declarations before the top-level module in error. case flip span insideDecls $ \case { C.Module{} -> False; _ -> True } of (ds0, [ C.Module _ m1 _ _ ]) | C.toTopLevelModuleName m1 == expectedMName -- If the anonymous module comes from the user, -- the range cannot be the beginningOfFile. -- That is the range if the parser inserted the anon. module. , r == beginningOfFile (getRange insideDecls) -> do traceCall (SetRange $ getRange ds0) $ typeError $ GenericError $ "Illegal declaration(s) before top-level module" -- Otherwise, reconstruct the top-level module name _ -> return $ C.QName $ C.Name (getRange m0) [Id $ stringToRawName $ rootNameModule file] -- Andreas, 2017-05-17, issue #2574, keep name as jump target! -- Andreas, 2016-07-12, ALTERNATIVE: -- -- We assign an anonymous file module the name expected from -- -- its import. For flat file structures, this is the same. -- -- For hierarchical file structures, this reverses the behavior: -- -- Loading the file by itself will fail, but it can be imported. -- -- The previous behavior is: it can be loaded by itself, but not -- -- be imported -- then return $ C.fromTopLevelModuleName expectedMName else do -- Andreas, 2014-03-28 Issue 1078 -- We need to check the module name against the file name here. -- Otherwise one could sneak in a lie and confuse the scope -- checker. checkModuleName (C.toTopLevelModuleName m0) file $ Just expectedMName return m0 setTopLevelModule m am <- toAbstract (NewModuleQName m) -- Scope check the declarations outside outsideDecls <- toAbstract outsideDecls (insideScope, insideDecls) <- scopeCheckModule r m am tel $ toAbstract insideDecls let scope = mapScopeInfo (restrictLocalPrivate am) insideScope setScope scope return $ TopLevelInfo (outsideDecls ++ insideDecls) scope -- We already inserted the missing top-level module, see -- 'Agda.Syntax.Parser.Parser.figureOutTopLevelModule', -- thus, this case is impossible: _ -> __IMPOSSIBLE__ -- | runs Syntax.Concrete.Definitions.niceDeclarations on main module niceDecls :: [C.Declaration] -> ScopeM [NiceDeclaration] niceDecls ds = do let (result, warns) = runNice $ niceDeclarations ds unless (null warns) $ setCurrentRange ds $ warning $ NicifierIssue warns case result of Left e -> throwError $ Exception (getRange e) $ pretty e Right ds -> return ds #if __GLASGOW_HASKELL__ >= 710 instance {-# OVERLAPPING #-} ToAbstract [C.Declaration] [A.Declaration] where #else instance ToAbstract [C.Declaration] [A.Declaration] where #endif toAbstract ds = do -- When --safe is active the termination checker (Issue 586) and -- positivity checker (Issue 1614) may not be switched off, and -- polarities may not be assigned. ds <- ifM (Lens.getSafeMode <$> commandLineOptions) (mapM (noNoTermCheck >=> noNoPositivityCheck >=> noPolarity) ds) (return ds) toAbstract =<< niceDecls ds where -- ASR (31 December 2015). We don't pattern-match on -- @NoTerminationCheck@ because the @NO_TERMINATION_CHECK@ pragma -- was removed. See Issue 1763. noNoTermCheck :: C.Declaration -> TCM C.Declaration noNoTermCheck d@(C.Pragma (C.TerminationCheckPragma r NonTerminating)) = d <$ (setCurrentRange d $ warning SafeFlagNonTerminating) noNoTermCheck d@(C.Pragma (C.TerminationCheckPragma r Terminating)) = d <$ (setCurrentRange d $ warning SafeFlagTerminating) noNoTermCheck d = return d noNoPositivityCheck :: C.Declaration -> TCM C.Declaration noNoPositivityCheck d@(C.Pragma (C.NoPositivityCheckPragma _)) = d <$ (setCurrentRange d $ warning SafeFlagNoPositivityCheck) noNoPositivityCheck d = return d noPolarity :: C.Declaration -> TCM C.Declaration noPolarity d@(C.Pragma C.PolarityPragma{}) = d <$ (setCurrentRange d $ warning SafeFlagPolarity) noPolarity d = return d newtype LetDefs = LetDefs [C.Declaration] newtype LetDef = LetDef NiceDeclaration instance ToAbstract LetDefs [A.LetBinding] where toAbstract (LetDefs ds) = concat <$> (toAbstract =<< map LetDef <$> niceDecls ds) instance ToAbstract LetDef [A.LetBinding] where toAbstract (LetDef d) = case d of NiceMutual _ _ _ d@[C.FunSig _ fx _ _ instanc macro info _ x t, C.FunDef _ _ _ abstract _ _ _ [cl]] -> do when (abstract == AbstractDef) $ do genericError $ "abstract not allowed in let expressions" when (macro == MacroDef) $ do genericError $ "Macros cannot be defined in a let expression." (x', e) <- letToAbstract cl t <- toAbstract t x <- toAbstract (NewName True $ mkBoundName x fx) -- If InstanceDef set info to Instance let info' | instanc == InstanceDef = makeInstance info | otherwise = info -- There are sometimes two instances of the -- let-bound variable, one declaration and one -- definition. The first list element below is -- used to highlight the declared instance in the -- right way (see Issue 1618). return [ A.LetDeclaredVariable (setRange (getRange x') x) , A.LetBind (LetRange $ getRange d) info' x t e ] -- irrefutable let binding, like (x , y) = rhs NiceFunClause r PublicAccess ConcreteDef termCheck catchall d@(C.FunClause lhs@(C.LHS p [] [] []) (C.RHS rhs) NoWhere ca) -> do mp <- setCurrentRange p $ (Right <$> parsePattern p) `catchError` (return . Left) case mp of Right p -> do rhs <- toAbstract rhs p <- toAbstract p checkPatternLinearity [p] p <- toAbstract p return [ A.LetPatBind (LetRange r) p rhs ] -- It's not a record pattern, so it should be a prefix left-hand side Left err -> case definedName p of Nothing -> throwError err Just x -> toAbstract $ LetDef $ NiceMutual r termCheck True [ C.FunSig r noFixity' PublicAccess ConcreteDef NotInstanceDef NotMacroDef defaultArgInfo termCheck x (C.Underscore (getRange x) Nothing) , C.FunDef r __IMPOSSIBLE__ __IMPOSSIBLE__ ConcreteDef NotInstanceDef __IMPOSSIBLE__ __IMPOSSIBLE__ [C.Clause x (ca || catchall) lhs (C.RHS rhs) NoWhere []] ] where definedName (C.IdentP (C.QName x)) = Just x definedName C.IdentP{} = Nothing definedName (C.RawAppP _ (p : _)) = definedName p definedName (C.ParenP _ p) = definedName p definedName C.WildP{} = Nothing -- for instance let _ + x = x in ... (not allowed) definedName C.AbsurdP{} = Nothing definedName C.AsP{} = Nothing definedName C.DotP{} = Nothing definedName C.LitP{} = Nothing definedName C.RecP{} = Nothing definedName C.QuoteP{} = Nothing definedName C.HiddenP{} = Nothing -- Not impossible, see issue #2291 definedName C.InstanceP{} = Nothing definedName C.RawAppP{} = __IMPOSSIBLE__ definedName C.AppP{} = __IMPOSSIBLE__ definedName C.OpAppP{} = __IMPOSSIBLE__ -- You can't open public in a let NiceOpen r x dirs -> do when (publicOpen dirs) $ warning UselessPublic m <- toAbstract (OldModuleName x) adir <- openModule_ x dirs let minfo = ModuleInfo { minfoRange = r , minfoAsName = Nothing , minfoAsTo = renamingRange dirs , minfoOpenShort = Nothing , minfoDirective = Just dirs } return [A.LetOpen minfo m adir] NiceModuleMacro r p x modapp open dir -> do when (publicOpen dir) $ warning UselessPublic -- Andreas, 2014-10-09, Issue 1299: module macros in lets need -- to be private checkModuleMacro LetApply r (PrivateAccess Inserted) x modapp open dir _ -> notAValidLetBinding d where letToAbstract (C.Clause top catchall clhs@(C.LHS p [] [] []) (C.RHS rhs) NoWhere []) = do {- p <- parseLHS top p localToAbstract (snd $ lhsArgs p) $ \args -> -} (x, args) <- do res <- setCurrentRange p $ parseLHS (C.QName top) p case res of C.LHSHead x args -> return (x, args) C.LHSProj{} -> genericError $ "copatterns not allowed in let bindings" e <- localToAbstract args $ \args -> do rhs <- toAbstract rhs foldM lambda rhs (reverse args) -- just reverse because these DomainFree return (x, e) letToAbstract _ = notAValidLetBinding d -- Named patterns not allowed in let definitions lambda e (Arg info (Named Nothing (A.VarP x))) = return $ A.Lam i (A.DomainFree info x) e where i = defaultLamInfo (fuseRange x e) lambda e (Arg info (Named Nothing (A.WildP i))) = do x <- freshNoName (getRange i) return $ A.Lam i' (A.DomainFree info x) e where i' = defaultLamInfo (fuseRange i e) lambda _ _ = notAValidLetBinding d newtype Blind a = Blind { unBlind :: a } instance ToAbstract (Blind a) (Blind a) where toAbstract = return -- The only reason why we return a list is that open declarations disappears. -- For every other declaration we get a singleton list. instance ToAbstract NiceDeclaration A.Declaration where toAbstract d = annotateDecls $ traceCall (ScopeCheckDeclaration d) $ -- Andreas, 2015-10-05, Issue 1677: -- We record in the environment whether we are scope checking an -- abstract definition. This way, we can propagate this attribute -- the extended lambdas. caseMaybe (niceHasAbstract d) id (\ a -> local $ \ e -> e { envAbstractMode = aDefToMode a }) $ case d of -- Axiom (actual postulate) C.Axiom r f p a i rel _ x t -> do -- check that we do not postulate in --safe mode clo <- commandLineOptions when (Lens.getSafeMode clo) (warning $ SafeFlagPostulate x) -- check the postulate toAbstractNiceAxiom A.NoFunSig NotMacroDef d -- Fields C.NiceField r f p a i x t -> do unless (p == PublicAccess) $ genericError "Record fields can not be private" -- Interaction points for record fields have already been introduced -- when checking the type of the record constructor. -- To avoid introducing interaction points (IP) twice, we turn -- all question marks to underscores. (See issue 1138.) let maskIP (C.QuestionMark r _) = C.Underscore r Nothing maskIP e = e t' <- toAbstractCtx TopCtx $ mapExpr maskIP t y <- freshAbstractQName f x irrProj <- optIrrelevantProjections <$> pragmaOptions unless (isIrrelevant t && not irrProj) $ -- Andreas, 2010-09-24: irrelevant fields are not in scope -- this ensures that projections out of irrelevant fields cannot occur -- Ulf: unless you turn on --irrelevant-projections bindName p FldName x y return [ A.Field (mkDefInfoInstance x f p a i NotMacroDef r) y t' ] -- Primitive function PrimitiveFunction r f p a x t -> do t' <- toAbstractCtx TopCtx t y <- freshAbstractQName f x bindName p DefName x y return [ A.Primitive (mkDefInfo x f p a r) y t' ] -- Definitions (possibly mutual) NiceMutual r termCheck pc ds -> do ds' <- toAbstract ds -- We only termination check blocks that do not have a measure. return [ A.Mutual (MutualInfo termCheck pc r) ds' ] C.NiceRecSig r f p a _pc x ls t -> do ensureNoLetStms ls withLocalVars $ do ls' <- toAbstract (map makeDomainFull ls) t' <- toAbstract t x' <- freshAbstractQName f x bindName p DefName x x' return [ A.RecSig (mkDefInfo x f p a r) x' ls' t' ] C.NiceDataSig r f p a _pc x ls t -> withLocalVars $ do printScope "scope.data.sig" 20 ("checking DataSig for " ++ prettyShow x) ensureNoLetStms ls ls' <- toAbstract (map makeDomainFull ls) t' <- toAbstract t x' <- freshAbstractQName f x {- -- Andreas, 2012-01-16: remember number of parameters bindName p (DataName (length ls)) x x' -} bindName p DefName x x' return [ A.DataSig (mkDefInfo x f p a r) x' ls' t' ] -- Type signatures C.FunSig r f p a i m rel tc x t -> toAbstractNiceAxiom A.FunSig m (C.Axiom r f p a i rel Nothing x t) -- Function definitions C.FunDef r ds f a i tc x cs -> do printLocals 10 $ "checking def " ++ prettyShow x (x',cs) <- toAbstract (OldName x,cs) let delayed = NotDelayed -- (delayed, cs) <- translateCopatternClauses cs -- TODO return [ A.FunDef (mkDefInfoInstance x f PublicAccess a i NotMacroDef r) x' delayed cs ] -- Uncategorized function clauses C.NiceFunClause r acc abs termCheck catchall (C.FunClause lhs rhs wcls ca) -> genericError $ "Missing type signature for left hand side " ++ prettyShow lhs C.NiceFunClause{} -> __IMPOSSIBLE__ -- Data definitions C.DataDef r f a _ x pars cons -> withLocalVars $ do printScope "scope.data.def" 20 ("checking DataDef for " ++ prettyShow x) ensureNoLetStms pars -- Check for duplicate constructors do cs <- mapM conName cons let dups = nub $ cs \\ nub cs bad = filter (`elem` dups) cs unless (distinct cs) $ setCurrentRange bad $ typeError $ DuplicateConstructors dups pars <- toAbstract pars DefinedName p ax <- resolveName (C.QName x) let x' = anameName ax -- Create the module for the qualified constructors checkForModuleClash x -- disallow shadowing previously defined modules let m = mnameFromList $ qnameToList x' createModule (Just IsData) m bindModule p x m -- make it a proper module cons <- toAbstract (map (ConstrDecl NoRec m a p) cons) -- Open the module -- openModule_ (C.QName x) defaultImportDir{ publicOpen = True } printScope "data" 20 $ "Checked data " ++ prettyShow x return [ A.DataDef (mkDefInfo x f PublicAccess a r) x' pars cons ] where conName (C.Axiom _ _ _ _ _ _ _ c _) = return c conName d = errorNotConstrDecl d -- Record definitions (mucho interesting) C.RecDef r f a _ x ind eta cm pars fields -> do ensureNoLetStms pars withLocalVars $ do -- Check that the generated module doesn't clash with a previously -- defined module checkForModuleClash x pars <- toAbstract pars DefinedName p ax <- resolveName (C.QName x) let x' = anameName ax -- We scope check the fields a first time when putting together -- the type of the constructor. contel <- toAbstract =<< recordConstructorType fields m0 <- getCurrentModule let m = A.qualifyM m0 $ mnameFromList [ last $ qnameToList x' ] printScope "rec" 15 "before record" createModule (Just IsRecord) m -- We scope check the fields a second time, as actual fields. afields <- withCurrentModule m $ do afields <- toAbstract fields printScope "rec" 15 "checked fields" return afields -- Andreas, 2017-07-13 issue #2642 disallow duplicate fields -- Check for duplicate fields. (See "Check for duplicate constructors") do let fs = catMaybes $ for fields $ \case C.NiceField _ _ _ _ _ f _ -> Just f _ -> Nothing let dups = nub $ fs \\ nub fs bad = filter (`elem` dups) fs unless (distinct fs) $ setCurrentRange bad $ typeError $ DuplicateFields dups bindModule p x m cm' <- mapM (\(ThingWithFixity c f, _) -> bindConstructorName m c f a p YesRec) cm let inst = caseMaybe cm NotInstanceDef snd printScope "rec" 15 "record complete" return [ A.RecDef (mkDefInfoInstance x f PublicAccess a inst NotMacroDef r) x' ind eta cm' pars contel afields ] NiceModule r p a x@(C.QName name) tel ds -> do reportSDoc "scope.decl" 70 $ vcat $ [ text $ "scope checking NiceModule " ++ prettyShow x ] adecls <- traceCall (ScopeCheckDeclaration $ NiceModule r p a x tel []) $ do scopeCheckNiceModule r p name tel $ toAbstract ds reportSDoc "scope.decl" 70 $ vcat $ [ text $ "scope checked NiceModule " ++ prettyShow x ] ++ map (nest 2 . prettyA) adecls return adecls NiceModule _ _ _ m@C.Qual{} _ _ -> genericError $ "Local modules cannot have qualified names" NiceModuleMacro r p x modapp open dir -> do reportSDoc "scope.decl" 70 $ vcat $ [ text $ "scope checking NiceModuleMacro " ++ prettyShow x ] adecls <- checkModuleMacro Apply r p x modapp open dir reportSDoc "scope.decl" 70 $ vcat $ [ text $ "scope checked NiceModuleMacro " ++ prettyShow x ] ++ map (nest 2 . prettyA) adecls return adecls NiceOpen r x dir -> do (minfo, m, adir) <- checkOpen r x dir return [A.Open minfo m adir] NicePragma r p -> do ps <- toAbstract p return $ map (A.Pragma r) ps NiceImport r x as open dir -> setCurrentRange r $ do notPublicWithoutOpen open dir -- First scope check the imported module and return its name and -- interface. This is done with that module as the top-level module. -- This is quite subtle. We rely on the fact that when setting the -- top-level module and generating a fresh module name, the generated -- name will be exactly the same as the name generated when checking -- the imported module. (m, i) <- withCurrentModule noModuleName $ withTopLevelModule x $ do m <- toAbstract $ NewModuleQName x printScope "import" 10 "before import:" (m, i) <- scopeCheckImport m printScope "import" 10 $ "scope checked import: " ++ show i -- We don't want the top scope of the imported module (things happening -- before the module declaration) return (m, Map.delete noModuleName i) -- Merge the imported scopes with the current scopes modifyScopes $ \ ms -> Map.unionWith mergeScope (Map.delete m ms) i -- Bind the desired module name to the right abstract name. case as of Nothing -> bindQModule (PrivateAccess Inserted) x m Just y -> bindModule (PrivateAccess Inserted) (asName y) m printScope "import" 10 "merged imported sig:" -- Open if specified, otherwise apply import directives let (name, theAsSymbol, theAsName) = case as of Nothing -> (x, noRange, Nothing) Just a -> (C.QName (asName a), asRange a, Just (asName a)) adir <- case open of DoOpen -> do (_minfo, _m, adir) <- checkOpen r name dir return adir -- If not opening, import directives are applied to the original scope. DontOpen -> modifyNamedScopeM m $ applyImportDirectiveM x dir let minfo = ModuleInfo { minfoRange = r , minfoAsName = theAsName , minfoAsTo = getRange (theAsSymbol, renamingRange dir) , minfoOpenShort = Just open , minfoDirective = Just dir } return [ A.Import minfo m adir ] NiceUnquoteDecl r fxs p a i tc xs e -> do ys <- zipWithM freshAbstractQName fxs xs zipWithM_ (bindName p QuotableName) xs ys e <- toAbstract e zipWithM_ (rebindName p DefName) xs ys let mi = MutualInfo tc True r return [ A.Mutual mi [A.UnquoteDecl mi [ mkDefInfoInstance x fx p a i NotMacroDef r | (fx, x) <- zip fxs xs ] ys e] ] NiceUnquoteDef r fxs p a tc xs e -> do ys <- mapM (toAbstract . OldName) xs zipWithM_ (rebindName p QuotableName) xs ys e <- toAbstract e zipWithM_ (rebindName p DefName) xs ys return [ A.UnquoteDef [ mkDefInfo x fx PublicAccess a r | (fx, x) <- zip fxs xs ] ys e ] NicePatternSyn r fx n as p -> do reportSLn "scope.pat" 10 $ "found nice pattern syn: " ++ prettyShow n defn@(as, p) <- withLocalVars $ do p <- toAbstract =<< parsePatternSyn p checkPatternLinearity [p] let err = "Dot patterns are not allowed in pattern synonyms. Use '_' instead." p <- noDotPattern err p as <- (traverse . mapM) (unVarName <=< resolveName . C.QName) as unlessNull (patternVars p \\ map unArg as) $ \ xs -> do typeError . GenericDocError =<< do text "Unbound variables in pattern synonym: " <+> sep (map prettyA xs) return (as, p) y <- freshAbstractQName fx n bindName PublicAccess PatternSynName n y modifyPatternSyns (Map.insert y defn) return [A.PatternSynDef y as p] -- only for highlighting where unVarName (VarName a _) = return a unVarName _ = typeError $ UnusedVariableInPatternSynonym where -- checking postulate or type sig. without checking safe flag toAbstractNiceAxiom funSig isMacro (C.Axiom r f p a i info mp x t) = do t' <- toAbstractCtx TopCtx t y <- freshAbstractQName f x let kind | isMacro == MacroDef = MacroName | otherwise = DefName bindName p kind x y return [ A.Axiom funSig (mkDefInfoInstance x f p a i isMacro r) info mp y t' ] toAbstractNiceAxiom _ _ _ = __IMPOSSIBLE__ data IsRecordCon = YesRec | NoRec data ConstrDecl = ConstrDecl IsRecordCon A.ModuleName IsAbstract Access C.NiceDeclaration bindConstructorName :: ModuleName -> C.Name -> Fixity'-> IsAbstract -> Access -> IsRecordCon -> ScopeM A.QName bindConstructorName m x f a p record = do -- The abstract name is the qualified one y <- withCurrentModule m $ freshAbstractQName f x -- Bind it twice, once unqualified and once qualified bindName p' ConName x y withCurrentModule m $ bindName p'' ConName x y return y where -- An abstract constructor is private (abstract constructor means -- abstract datatype, so the constructor should not be exported). p' = case a of AbstractDef -> PrivateAccess Inserted _ -> p p'' = case (a, record) of (AbstractDef, _) -> PrivateAccess Inserted (_, YesRec) -> OnlyQualified -- record constructors aren't really in the record module _ -> PublicAccess instance ToAbstract ConstrDecl A.Declaration where toAbstract (ConstrDecl record m a p d) = do case d of C.Axiom r f p1 a1 i info Nothing x t -> do -- rel==Relevant -- unless (p1 == p) __IMPOSSIBLE__ -- This invariant is currently violated by test/Succeed/Issue282.agda unless (a1 == a) __IMPOSSIBLE__ t' <- toAbstractCtx TopCtx t -- The abstract name is the qualified one -- Bind it twice, once unqualified and once qualified y <- bindConstructorName m x f a p record printScope "con" 15 "bound constructor" return $ A.Axiom NoFunSig (mkDefInfoInstance x f p a i NotMacroDef r) info Nothing y t' C.Axiom _ _ _ _ _ _ (Just _) _ _ -> __IMPOSSIBLE__ _ -> errorNotConstrDecl d errorNotConstrDecl :: C.NiceDeclaration -> ScopeM a errorNotConstrDecl d = typeError . GenericDocError $ P.text "Illegal declaration in data type definition " P.$$ P.nest 2 (P.vcat $ map pretty (notSoNiceDeclarations d)) instance ToAbstract C.Pragma [A.Pragma] where toAbstract (C.ImpossiblePragma _) = impossibleTest toAbstract (C.OptionsPragma _ opts) = return [ A.OptionsPragma opts ] toAbstract (C.RewritePragma _ []) = [] <$ warning EmptyRewritePragma toAbstract (C.RewritePragma _ xs) = concat <$> do forM xs $ \ x -> do e <- toAbstract $ OldQName x Nothing case e of A.Def x -> return [ A.RewritePragma x ] A.Proj _ (AmbQ [x]) -> return [ A.RewritePragma x ] A.Proj _ x -> genericError $ "REWRITE used on ambiguous name " ++ prettyShow x A.Con (AmbQ [x]) -> return [ A.RewritePragma x ] A.Con x -> genericError $ "REWRITE used on ambiguous name " ++ prettyShow x A.Var x -> genericError $ "REWRITE used on parameter " ++ prettyShow x ++ " instead of on a defined symbol" _ -> __IMPOSSIBLE__ toAbstract (C.CompiledTypePragma _ x hs) = do e <- toAbstract $ OldQName x Nothing case e of A.Def x -> return [ A.CompiledTypePragma x hs ] _ -> genericError $ "Bad compiled type: " ++ prettyShow x -- TODO: error message toAbstract (C.CompiledDataPragma _ x hs hcs) = do e <- toAbstract $ OldQName x Nothing case e of A.Def x -> return [ A.CompiledDataPragma x hs hcs ] _ -> genericError $ "Not a datatype: " ++ prettyShow x -- TODO: error message toAbstract (C.CompiledPragma _ x hs) = do e <- toAbstract $ OldQName x Nothing y <- case e of A.Def x -> return x A.Proj _ (AmbQ [x]) -> return x -- TODO: do we need to do s.th. special for projections? (Andreas, 2014-10-12) A.Proj _ x -> genericError $ "COMPILED on ambiguous name " ++ prettyShow x A.Con _ -> genericError "Use COMPILED_DATA for constructors" -- TODO _ -> __IMPOSSIBLE__ return [ A.CompiledPragma y hs ] toAbstract (C.CompiledExportPragma _ x hs) = do e <- toAbstract $ OldQName x Nothing y <- case e of A.Def x -> return x _ -> __IMPOSSIBLE__ return [ A.CompiledExportPragma y hs ] toAbstract (C.CompiledJSPragma _ x ep) = do e <- toAbstract $ OldQName x Nothing y <- case e of A.Def x -> return x A.Proj _ (AmbQ [x]) -> return x A.Proj _ x -> genericError $ "COMPILED_JS used on ambiguous name " ++ prettyShow x A.Con (AmbQ [x]) -> return x A.Con x -> genericError $ "COMPILED_JS used on ambiguous name " ++ prettyShow x _ -> __IMPOSSIBLE__ return [ A.CompiledJSPragma y ep ] toAbstract (C.CompiledUHCPragma _ x cr) = do e <- toAbstract $ OldQName x Nothing y <- case e of A.Def x -> return x _ -> __IMPOSSIBLE__ return [ A.CompiledUHCPragma y cr ] toAbstract (C.CompiledDataUHCPragma _ x crd crcs) = do e <- toAbstract $ OldQName x Nothing case e of A.Def x -> return [ A.CompiledDataUHCPragma x crd crcs ] _ -> fail $ "Bad compiled type: " ++ prettyShow x -- TODO: error message toAbstract (C.ForeignPragma _ b s) = [] <$ addForeignCode b s toAbstract (C.CompilePragma _ b x s) = do e <- toAbstract $ OldQName x Nothing let err what = genericError $ "Cannot COMPILE " ++ what ++ " " ++ prettyShow x y <- case e of A.Def x -> return x A.Proj _ (AmbQ [x]) -> return x A.Proj _ x -> err "ambiguous projection" A.Con (AmbQ [x]) -> return x A.Con x -> err "ambiguous constructor" A.PatternSyn{} -> err "pattern synonym" A.Var{} -> err "local variable" _ -> __IMPOSSIBLE__ return [ A.CompilePragma b y s ] toAbstract (C.StaticPragma _ x) = do e <- toAbstract $ OldQName x Nothing y <- case e of A.Def x -> return x A.Proj _ (AmbQ [x]) -> return x A.Proj _ x -> genericError $ "STATIC used on ambiguous name " ++ prettyShow x _ -> genericError "Target of STATIC pragma should be a function" return [ A.StaticPragma y ] toAbstract (C.InjectivePragma _ x) = do e <- toAbstract $ OldQName x Nothing y <- case e of A.Def x -> return x A.Proj _ (AmbQ [x]) -> return x A.Proj _ x -> genericError $ "INJECTIVE used on ambiguous name " ++ prettyShow x _ -> genericError "Target of INJECTIVE pragma should be a defined symbol" return [ A.InjectivePragma y ] toAbstract (C.InlinePragma _ x) = do e <- toAbstract $ OldQName x Nothing y <- case e of A.Def x -> return x A.Proj _ (AmbQ [x]) -> return x A.Proj _ x -> genericError $ "INLINE used on ambiguous name " ++ prettyShow x _ -> genericError "Target of INLINE pragma should be a function" return [ A.InlinePragma y ] toAbstract (C.BuiltinPragma _ b e) | isUntypedBuiltin b = do bindUntypedBuiltin b =<< toAbstract e return [] toAbstract (C.BuiltinPragma _ b e) = do -- Andreas, 2015-02-14 -- Some builtins cannot be given a valid Agda type, -- thus, they do not come with accompanying postulate or definition. if b `elem` builtinsNoDef then do case e of C.Ident q@(C.QName x) -> do unlessM ((UnknownName ==) <$> resolveName q) $ genericError $ "BUILTIN " ++ b ++ " declares an identifier " ++ "(no longer expects an already defined identifier)" y <- freshAbstractQName noFixity' x bindName PublicAccess DefName x y return [ A.BuiltinNoDefPragma b y ] _ -> genericError $ "Pragma BUILTIN " ++ b ++ ": expected unqualified identifier, " ++ "but found expression " ++ prettyShow e else do e <- toAbstract e return [ A.BuiltinPragma b e ] toAbstract (C.ImportPragma _ i) = do addHaskellImport i return [] toAbstract (C.ImportUHCPragma _ i) = do addHaskellImportUHC i return [] toAbstract (C.HaskellCodePragma _ s) = do addInlineHaskell s return [] toAbstract (C.EtaPragma _ x) = do e <- toAbstract $ OldQName x Nothing case e of A.Def x -> return [ A.EtaPragma x ] _ -> do e <- showA e genericError $ "Pragma ETA: expected identifier, " ++ "but found expression " ++ e toAbstract (C.DisplayPragma _ lhs rhs) = withLocalVars $ do let err = genericError "DISPLAY pragma left-hand side must have form 'f e1 .. en'" getHead (C.IdentP x) = return x getHead (C.RawAppP _ (p : _)) = getHead p getHead _ = err top <- getHead lhs (isPatSyn, hd) <- do qx <- resolveName' allKindsOfNames Nothing top case qx of VarName x' _ -> return . (False,) $ A.qnameFromList [x'] DefinedName _ d -> return . (False,) $ anameName d FieldName [d] -> return . (False,) $ anameName d FieldName ds -> genericError $ "Ambiguous projection " ++ prettyShow top ++ ": " ++ prettyShow (map anameName ds) ConstructorName [d] -> return . (False,) $ anameName d ConstructorName ds -> genericError $ "Ambiguous constructor " ++ prettyShow top ++ ": " ++ prettyShow (map anameName ds) UnknownName -> notInScope top PatternSynResName d -> return . (True,) $ anameName d lhs <- toAbstract $ LeftHandSide top lhs [] ps <- case lhs of A.LHS _ (A.LHSHead _ ps) [] -> return ps _ -> err -- Andreas, 2016-08-08, issue #2132 -- Remove pattern synonyms on lhs (hd, ps) <- do let mkP | isPatSyn = A.PatternSynP (PatRange $ getRange lhs) hd | otherwise = A.DefP (PatRange $ getRange lhs) (A.AmbQ [hd]) p <- expandPatternSynonyms $ mkP ps case p of A.DefP _ (A.AmbQ [hd]) ps -> return (hd, ps) A.ConP _ (A.AmbQ [hd]) ps -> return (hd, ps) A.PatternSynP{} -> __IMPOSSIBLE__ _ -> err rhs <- toAbstract rhs return [A.DisplayPragma hd ps rhs] -- Termination checking pragmes are handled by the nicifier toAbstract C.TerminationCheckPragma{} = __IMPOSSIBLE__ toAbstract C.CatchallPragma{} = __IMPOSSIBLE__ -- No positivity checking pragmas are handled by the nicifier. toAbstract C.NoPositivityCheckPragma{} = __IMPOSSIBLE__ -- Polarity pragmas are handled by the niceifier. toAbstract C.PolarityPragma{} = __IMPOSSIBLE__ instance ToAbstract C.Clause A.Clause where toAbstract (C.Clause top _ C.Ellipsis{} _ _ _) = genericError "bad '...'" -- TODO: error message toAbstract (C.Clause top catchall lhs@(C.LHS p wps eqs with) rhs wh wcs) = withLocalVars $ do -- Andreas, 2012-02-14: need to reset local vars before checking subclauses vars <- getLocalVars let wcs' = for wcs $ \ c -> setLocalVars vars $> c lhs' <- toAbstract $ LeftHandSide (C.QName top) p wps printLocals 10 "after lhs:" let (whname, whds) = case wh of NoWhere -> (Nothing, []) -- Andreas, 2016-07-17 issues #2081 and #2101 -- where-declarations are automatically private. -- This allows their type signature to be checked InAbstractMode. AnyWhere ds -> (Nothing, [C.Private noRange Inserted ds]) -- Named where-modules do not default to private. SomeWhere m a ds -> (Just (m, a), ds) let isTerminationPragma :: C.Declaration -> Bool isTerminationPragma (C.Private _ _ ds) = any isTerminationPragma ds isTerminationPragma (C.Pragma (TerminationCheckPragma _ _)) = True isTerminationPragma _ = False if not (null eqs) then do rhs <- toAbstract =<< toAbstractCtx TopCtx (RightHandSide eqs with wcs' rhs whds) return $ A.Clause lhs' [] [] rhs [] catchall else do -- ASR (16 November 2015) Issue 1137: We ban termination -- pragmas inside `where` clause. when (any isTerminationPragma whds) $ genericError "Termination pragmas are not allowed inside where clauses" -- the right hand side is checked inside the module of the local definitions (rhs, ds) <- whereToAbstract (getRange wh) whname whds $ toAbstractCtx TopCtx (RightHandSide eqs with wcs' rhs []) rhs <- toAbstract rhs return $ A.Clause lhs' [] [] rhs ds catchall whereToAbstract :: Range -> Maybe (C.Name, Access) -> [C.Declaration] -> ScopeM a -> ScopeM (a, [A.Declaration]) whereToAbstract _ _ [] inner = (,[]) <$> inner whereToAbstract r whname whds inner = do -- Create a fresh concrete name if there isn't (a proper) one. (m, acc) <- do case whname of Just (m, acc) | not (isNoName m) -> return (m, acc) _ -> fresh <&> \ x -> (C.NoName (getRange whname) x, PrivateAccess Inserted) -- unnamed where's are private let tel = [] old <- getCurrentModule am <- toAbstract (NewModuleName m) (scope, ds) <- scopeCheckModule r (C.QName m) am tel $ toAbstract whds setScope scope x <- inner setCurrentModule old bindModule acc m am -- Issue 848: if the module was anonymous (module _ where) open it public let anonymousSomeWhere = maybe False (isNoName . fst) whname when anonymousSomeWhere $ void $ -- We can ignore the returned default A.ImportDirective. openModule_ (C.QName m) $ defaultImportDir { publicOpen = True } return (x, ds) data RightHandSide = RightHandSide { rhsRewriteEqn :: [C.RewriteEqn] -- ^ @rewrite e@ (many) , rhsWithExpr :: [C.WithExpr] -- ^ @with e@ (many) , rhsSubclauses :: [ScopeM C.Clause] -- ^ the subclauses spawned by a with (monadic because we need to reset the local vars before checking these clauses) , rhs :: C.RHS , rhsWhereDecls :: [C.Declaration] } data AbstractRHS = AbsurdRHS' | WithRHS' [A.Expr] [ScopeM C.Clause] -- ^ The with clauses haven't been translated yet | RHS' A.Expr C.Expr | RewriteRHS' [A.Expr] AbstractRHS [A.Declaration] qualifyName_ :: A.Name -> ScopeM A.QName qualifyName_ x = do m <- getCurrentModule return $ A.qualify m x withFunctionName :: String -> ScopeM A.QName withFunctionName s = do NameId i _ <- fresh qualifyName_ =<< freshName_ (s ++ show i) instance ToAbstract AbstractRHS A.RHS where toAbstract AbsurdRHS' = return A.AbsurdRHS toAbstract (RHS' e c) = return $ A.RHS e $ Just c toAbstract (RewriteRHS' eqs rhs wh) = do auxs <- replicateM (length eqs) $ withFunctionName "rewrite-" rhs <- toAbstract rhs return $ RewriteRHS (zip auxs eqs) rhs wh toAbstract (WithRHS' es cs) = do aux <- withFunctionName "with-" A.WithRHS aux es <$> do toAbstract =<< sequence cs instance ToAbstract RightHandSide AbstractRHS where toAbstract (RightHandSide eqs@(_:_) es cs rhs wh) = do eqs <- toAbstractCtx TopCtx eqs -- TODO: remember named where (rhs, ds) <- whereToAbstract (getRange wh) Nothing wh $ toAbstract (RightHandSide [] es cs rhs []) return $ RewriteRHS' eqs rhs ds toAbstract (RightHandSide [] [] (_ : _) _ _) = __IMPOSSIBLE__ toAbstract (RightHandSide [] (_ : _) _ (C.RHS _) _) = typeError $ BothWithAndRHS toAbstract (RightHandSide [] [] [] rhs []) = toAbstract rhs toAbstract (RightHandSide [] es cs C.AbsurdRHS []) = do es <- toAbstractCtx TopCtx es return $ WithRHS' es cs -- TODO: some of these might be possible toAbstract (RightHandSide [] (_ : _) _ C.AbsurdRHS (_ : _)) = __IMPOSSIBLE__ toAbstract (RightHandSide [] [] [] (C.RHS _) (_ : _)) = __IMPOSSIBLE__ toAbstract (RightHandSide [] [] [] C.AbsurdRHS (_ : _)) = __IMPOSSIBLE__ instance ToAbstract C.RHS AbstractRHS where toAbstract C.AbsurdRHS = return $ AbsurdRHS' toAbstract (C.RHS e) = RHS' <$> toAbstract e <*> pure e data LeftHandSide = LeftHandSide C.QName C.Pattern [C.Pattern] instance ToAbstract LeftHandSide A.LHS where toAbstract (LeftHandSide top lhs wps) = traceCall (ScopeCheckLHS top lhs) $ do lhscore <- parseLHS top lhs reportSLn "scope.lhs" 5 $ "parsed lhs: " ++ show lhscore printLocals 10 "before lhs:" -- error if copattern parsed but --no-copatterns option unlessM (optCopatterns <$> pragmaOptions) $ case lhscore of C.LHSProj{} -> typeError $ NeedOptionCopatterns C.LHSHead{} -> return () -- scope check patterns except for dot patterns lhscore <- toAbstract lhscore reportSLn "scope.lhs" 5 $ "parsed lhs patterns: " ++ show lhscore wps <- toAbstract =<< mapM parsePattern wps checkPatternLinearity $ lhsCoreAllPatterns lhscore ++ wps printLocals 10 "checked pattern:" -- scope check dot patterns lhscore <- toAbstract lhscore reportSLn "scope.lhs" 5 $ "parsed lhs dot patterns: " ++ show lhscore wps <- toAbstract wps printLocals 10 "checked dots:" return $ A.LHS (LHSRange $ getRange (lhs, wps)) lhscore wps -- does not check pattern linearity instance ToAbstract C.LHSCore (A.LHSCore' C.Expr) where toAbstract (C.LHSHead x ps) = do x <- withLocalVars $ setLocalVars [] >> toAbstract (OldName x) args <- toAbstract ps return $ A.LHSHead x args toAbstract c@(C.LHSProj d ps1 l ps2) = do unless (null ps1) $ typeError $ GenericDocError $ P.text "Ill-formed projection pattern" P.<+> P.pretty (foldl C.AppP (C.IdentP d) ps1) qx <- resolveName d ds <- case qx of FieldName [] -> __IMPOSSIBLE__ FieldName ds -> return $ map anameName ds UnknownName -> notInScope d _ -> genericError $ "head of copattern needs to be a field identifier, but " ++ prettyShow d ++ " isn't one" A.LHSProj (AmbQ ds) <$> toAbstract l <*> toAbstract ps2 instance ToAbstract c a => ToAbstract (WithHiding c) (WithHiding a) where toAbstract (WithHiding h a) = WithHiding h <$> toAbstractHiding h a instance ToAbstract c a => ToAbstract (Arg c) (Arg a) where toAbstract (Arg info e) = Arg info <$> toAbstractHiding info e instance ToAbstract c a => ToAbstract (Named name c) (Named name a) where toAbstract (Named n e) = Named n <$> toAbstract e {- DOES NOT WORK ANYMORE with pattern synonyms instance ToAbstract c a => ToAbstract (A.LHSCore' c) (A.LHSCore' a) where toAbstract = mapM toAbstract -} instance ToAbstract (A.LHSCore' C.Expr) (A.LHSCore' A.Expr) where toAbstract (A.LHSHead f ps) = A.LHSHead f <$> mapM toAbstract ps toAbstract (A.LHSProj d lhscore ps) = A.LHSProj d <$> mapM toAbstract lhscore <*> mapM toAbstract ps -- Patterns are done in two phases. First everything but the dot patterns, and -- then the dot patterns. This is because dot patterns can refer to variables -- bound anywhere in the pattern. instance ToAbstract (A.Pattern' C.Expr) (A.Pattern' A.Expr) where toAbstract = traverse $ insideDotPattern . toAbstract resolvePatternIdentifier :: Range -> C.QName -> Maybe (Set A.Name) -> ScopeM (A.Pattern' C.Expr) resolvePatternIdentifier r x ns = do px <- toAbstract (PatName x ns) case px of VarPatName y -> return $ VarP y ConPatName ds -> return $ ConP (ConPatInfo ConOCon $ PatRange r) (AmbQ $ map anameName ds) [] PatternSynPatName d -> return $ PatternSynP (PatRange r) (anameName d) [] instance ToAbstract C.Pattern (A.Pattern' C.Expr) where toAbstract (C.IdentP x) = resolvePatternIdentifier (getRange x) x Nothing toAbstract (AppP (QuoteP _) p) | IdentP x <- namedArg p, visible p = do e <- toAbstract (OldQName x Nothing) let quoted (A.Def x) = return x quoted (A.Macro x) = return x quoted (A.Proj _ (AmbQ [x])) = return x quoted (A.Proj _ (AmbQ xs)) = genericError $ "quote: Ambigous name: " ++ prettyShow xs quoted (A.Con (AmbQ [x])) = return x quoted (A.Con (AmbQ xs)) = genericError $ "quote: Ambigous name: " ++ prettyShow xs quoted (A.ScopedExpr _ e) = quoted e quoted _ = genericError $ "quote: not a defined name" A.LitP . LitQName (getRange x) <$> quoted e toAbstract (QuoteP r) = genericError "quote must be applied to an identifier" toAbstract p0@(AppP p q) = do (p', q') <- toAbstract (p, q) case p' of ConP i x as -> return $ ConP (i {patInfo = info}) x (as ++ [q']) ProjP i o x -> typeError $ InvalidPattern p0 DefP _ x as -> return $ DefP info x (as ++ [q']) PatternSynP _ x as -> return $ PatternSynP info x (as ++ [q']) _ -> typeError $ InvalidPattern p0 where r = getRange p0 info = PatRange r toAbstract p0@(OpAppP r op ns ps) = do p <- resolvePatternIdentifier (getRange op) op (Just ns) ps <- toAbstract ps case p of ConP i x as -> return $ ConP (i {patInfo = info}) x (as ++ ps) DefP _ x as -> return $ DefP info x (as ++ ps) PatternSynP _ x as -> return $ PatternSynP info x (as ++ ps) _ -> __IMPOSSIBLE__ where info = PatRange r -- Removed when parsing toAbstract (HiddenP _ _) = __IMPOSSIBLE__ toAbstract (InstanceP _ _) = __IMPOSSIBLE__ toAbstract (RawAppP _ _) = __IMPOSSIBLE__ toAbstract p@(C.WildP r) = return $ A.WildP (PatRange r) -- Andreas, 2015-05-28 futile attempt to fix issue 819: repeated variable on lhs "_" -- toAbstract p@(C.WildP r) = A.VarP <$> freshName r "_" toAbstract (C.ParenP _ p) = toAbstract p toAbstract (C.LitP l) = return $ A.LitP l toAbstract p0@(C.AsP r x p) = do x <- toAbstract (NewName False x) p <- toAbstract p return $ A.AsP info x p where info = PatRange r -- we have to do dot patterns at the end toAbstract p0@(C.DotP r o e) = return $ A.DotP info o e where info = PatRange r toAbstract p0@(C.AbsurdP r) = return $ A.AbsurdP info where info = PatRange r toAbstract (C.RecP r fs) = A.RecP (PatRange r) <$> mapM (traverse toAbstract) fs -- | An argument @OpApp C.Expr@ to an operator can have binders, -- in case the operator is some @syntax@-notation. -- For these binders, we have to create lambda-abstractions. toAbstractOpArg :: Precedence -> OpApp C.Expr -> ScopeM A.Expr toAbstractOpArg ctx (Ordinary e) = toAbstractCtx ctx e toAbstractOpArg ctx (SyntaxBindingLambda r bs e) = toAbstractLam r bs e ctx -- | Turn an operator application into abstract syntax. Make sure to -- record the right precedences for the various arguments. toAbstractOpApp :: C.QName -> Set A.Name -> [NamedArg (MaybePlaceholder (OpApp C.Expr))] -> ScopeM A.Expr toAbstractOpApp op ns es = do -- Replace placeholders with bound variables. (binders, es) <- replacePlaceholders es -- Get the notation for the operator. nota <- getNotation op ns let parts = notation nota -- We can throw away the @BindingHoles@, since binders -- have been preprocessed into @OpApp C.Expr@. let nonBindingParts = filter (not . isBindingHole) parts -- We should be left with as many holes as we have been given args @es@. -- If not, crash. unless (length (filter isAHole nonBindingParts) == length es) __IMPOSSIBLE__ -- Translate operator and its arguments (each in the right context). op <- toAbstract (OldQName op (Just ns)) es <- left (notaFixity nota) nonBindingParts es -- Prepend the generated section binders (if any). let body = foldl' app op es return $ foldr (A.Lam (defaultLamInfo (getRange body))) body binders where -- Build an application in the abstract syntax, with correct Range. app e arg = A.App (ExprRange (fuseRange e arg)) e arg -- Translate an argument. toAbsOpArg :: Precedence -> NamedArg (Either A.Expr (OpApp C.Expr)) -> ScopeM (NamedArg A.Expr) toAbsOpArg cxt = traverse $ traverse $ either return (toAbstractOpArg cxt) -- The hole left to the first @IdPart@ is filled with an expression in @LeftOperandCtx@. left f (IdPart _ : xs) es = inside f xs es left f (_ : xs) (e : es) = do e <- toAbsOpArg (LeftOperandCtx f) e es <- inside f xs es return (e : es) left f (_ : _) [] = __IMPOSSIBLE__ left f [] _ = __IMPOSSIBLE__ -- The holes in between the @IdPart@s is filled with an expression in @InsideOperandCtx@. inside f [x] es = right f x es inside f (IdPart _ : xs) es = inside f xs es inside f (_ : xs) (e : es) = do e <- toAbsOpArg InsideOperandCtx e es <- inside f xs es return (e : es) inside _ (_ : _) [] = __IMPOSSIBLE__ inside _ [] _ = __IMPOSSIBLE__ -- The hole right of the last @IdPart@ is filled with an expression in @RightOperandCtx@. right _ (IdPart _) [] = return [] right f _ [e] = do e <- toAbsOpArg (RightOperandCtx f) e return [e] right _ _ _ = __IMPOSSIBLE__ replacePlaceholders :: [NamedArg (MaybePlaceholder (OpApp e))] -> ScopeM ([A.LamBinding], [NamedArg (Either A.Expr (OpApp e))]) replacePlaceholders [] = return ([], []) replacePlaceholders (a : as) = case namedArg a of NoPlaceholder _ x -> mapSnd (set (Right x) a :) <$> replacePlaceholders as Placeholder _ -> do x <- freshName noRange "section" let i = setOrigin Inserted $ argInfo a (ls, ns) <- replacePlaceholders as return ( A.DomainFree i x : ls , set (Left (Var x)) a : ns ) where set :: a -> NamedArg b -> NamedArg a set x arg = fmap (fmap (const x)) arg Agda-2.5.3/src/full/Agda/Syntax/Translation/InternalToAbstract.hs0000644000000000000000000013000213154613124023007 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE NondecreasingIndentation #-} {-# LANGUAGE TypeFamilies #-} -- for type equality ~ {-# LANGUAGE UndecidableInstances #-} {-| Translating from internal syntax to abstract syntax. Enables nice pretty printing of internal syntax. TODO - numbers on metas - fake dependent functions to independent functions - meta parameters - shadowing -} module Agda.Syntax.Translation.InternalToAbstract ( Reify(..) , NamedClause(..) , reifyPatterns ) where import Prelude hiding (mapM_, mapM, null) import Control.Applicative hiding (empty) import Control.Monad.State hiding (mapM_, mapM) import Control.Monad.Reader hiding (mapM_, mapM) import Data.Foldable (Foldable, foldMap) import qualified Data.List as List import qualified Data.Map as Map import Data.Maybe import Data.Semigroup (Semigroup, Monoid, (<>), mempty, mappend) import Data.Set (Set) import qualified Data.Set as Set import Data.Traversable (Traversable, traverse, mapM) import qualified Data.Traversable as Trav import Agda.Syntax.Literal import Agda.Syntax.Position import Agda.Syntax.Common import Agda.Syntax.Fixity import qualified Agda.Syntax.Concrete.Name as C import Agda.Syntax.Concrete (FieldAssignment'(..), exprFieldA) import Agda.Syntax.Info as Info import Agda.Syntax.Abstract as A import Agda.Syntax.Abstract.Pattern ( foldAPattern ) import Agda.Syntax.Abstract.Pretty import Agda.Syntax.Internal as I import Agda.Syntax.Internal.Pattern as I import Agda.Syntax.Scope.Base (isNameInScope, inverseScopeLookupName) import Agda.TypeChecking.Monad import Agda.TypeChecking.Monad.Builtin import Agda.TypeChecking.Reduce import {-# SOURCE #-} Agda.TypeChecking.Records import Agda.TypeChecking.CompiledClause (CompiledClauses'(Fail)) import Agda.TypeChecking.DisplayForm import Agda.TypeChecking.Level import {-# SOURCE #-} Agda.TypeChecking.Datatypes import Agda.TypeChecking.Free import Agda.TypeChecking.Substitute import Agda.TypeChecking.Telescope import Agda.TypeChecking.DropArgs import Agda.Interaction.Options ( optPostfixProjections ) import Agda.Utils.Either import Agda.Utils.Except ( MonadError(catchError) ) import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import Agda.Utils.Permutation import Agda.Utils.Pretty hiding ((<>)) import Agda.Utils.Singleton import Agda.Utils.Size import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- Composition of reified applications ------------------------------------ -- | Drops hidden arguments unless --show-implicit. napps :: Expr -> [NamedArg Expr] -> TCM Expr napps e = nelims e . map I.Apply -- | Drops hidden arguments unless --show-implicit. apps :: Expr -> [Arg Expr] -> TCM Expr apps e = elims e . map I.Apply -- Composition of reified eliminations ------------------------------------ -- | Drops hidden arguments unless --show-implicit. nelims :: Expr -> [I.Elim' (Named_ Expr)] -> TCM Expr nelims e [] = return e nelims e (I.Apply arg : es) = do arg <- reify arg -- This replaces the arg by _ if irrelevant dontShowImp <- not <$> showImplicitArguments let hd | notVisible arg && dontShowImp = e | otherwise = A.App noExprInfo e arg nelims hd es nelims e (I.Proj o@ProjPrefix d : es) = nelims (A.App noExprInfo (A.Proj o $ AmbQ [d]) $ defaultNamedArg e) es nelims e (I.Proj o d : es) = nelims (A.App noExprInfo e (defaultNamedArg $ A.Proj o $ AmbQ [d])) es -- | Drops hidden arguments unless --show-implicit. elims :: Expr -> [I.Elim' Expr] -> TCM Expr elims e = nelims e . map (fmap unnamed) -- Omitting information --------------------------------------------------- noExprInfo :: ExprInfo noExprInfo = ExprRange noRange -- Conditional reification to omit terms that are not shown -------------- reifyWhenE :: Reify i Expr => Bool -> i -> TCM Expr reifyWhenE True i = reify i reifyWhenE False t = return underscore -- Reification ------------------------------------------------------------ class Reify i a | i -> a where reify :: i -> TCM a -- @reifyWhen False@ should produce an 'underscore'. -- This function serves to reify hidden/irrelevant things. reifyWhen :: Bool -> i -> TCM a reifyWhen _ = reify instance Reify Name Name where reify = return instance Reify Expr Expr where reifyWhen = reifyWhenE reify = return instance Reify MetaId Expr where reifyWhen = reifyWhenE reify x@(MetaId n) = liftTCM $ do b <- asks envPrintMetasBare mi <- mvInfo <$> lookupMeta x let mi' = Info.MetaInfo { metaRange = getRange $ miClosRange mi , metaScope = clScope $ miClosRange mi , metaNumber = if b then Nothing else Just x , metaNameSuggestion = if b then "" else miNameSuggestion mi } underscore = return $ A.Underscore mi' caseMaybeM (isInteractionMeta x) underscore $ \ ii@InteractionId{} -> return $ A.QuestionMark mi' ii -- Does not print with-applications correctly: -- instance Reify DisplayTerm Expr where -- reifyWhen = reifyWhenE -- reify d = reifyTerm False $ dtermToTerm d instance Reify DisplayTerm Expr where reifyWhen = reifyWhenE reify d = case d of DTerm v -> reifyTerm False v DDot v -> reify v DCon c ci vs -> apps (A.Con (AmbQ [conName c])) =<< reify vs DDef f es -> elims (A.Def f) =<< reify es DWithApp u us es0 -> do (e, es) <- reify (u, us) elims (if null es then e else A.WithApp noExprInfo e es) =<< reify es0 -- | @reifyDisplayForm f vs fallback@ -- tries to rewrite @f vs@ with a display form for @f@. -- If successful, reifies the resulting display term, -- otherwise, does @fallback@. reifyDisplayForm :: QName -> I.Elims -> TCM A.Expr -> TCM A.Expr reifyDisplayForm f es fallback = do ifNotM displayFormsEnabled fallback $ {- else -} do caseMaybeM (liftTCM $ displayForm f es) fallback reify -- | @reifyDisplayFormP@ tries to recursively -- rewrite a lhs with a display form. -- -- Note: we are not necessarily in the empty context upon entry! reifyDisplayFormP :: A.SpineLHS -> TCM A.SpineLHS reifyDisplayFormP lhs@(A.SpineLHS i f ps wps) = ifNotM displayFormsEnabled (return lhs) $ {- else -} do -- Try to rewrite @f 0 1 2 ... |ps|-1@ to a dt. -- Andreas, 2014-06-11 Issue 1177: -- I thought we need to add the placeholders for ps to the context, -- because otherwise displayForm will not raise the display term -- and we will have variable clashes. -- But apparently, it has no influence... -- Ulf, can you add an explanation? md <- liftTCM $ -- addContext (replicate (length ps) "x") $ displayForm f $ zipWith (\ p i -> I.Apply $ p $> I.var i) ps [0..] reportSLn "reify.display" 60 $ "display form of " ++ prettyShow f ++ " " ++ show ps ++ " " ++ show wps ++ ":\n " ++ show md case md of Just d | okDisplayForm d -> do -- In the display term @d@, @var i@ should be a placeholder -- for the @i@th pattern of @ps@. -- Andreas, 2014-06-11: -- Are we sure that @d@ did not use @var i@ otherwise? lhs' <- displayLHS ps wps d reportSDoc "reify.display" 70 $ do doc <- prettyA lhs' return $ vcat [ text "rewritten lhs to" , text " lhs' = " <+> doc ] reifyDisplayFormP lhs' _ -> do reportSLn "reify.display" 70 $ "display form absent or not valid as lhs" return lhs where -- Andreas, 2015-05-03: Ulf, please comment on what -- is the idea behind okDisplayForm. -- Ulf, 2016-04-15: okDisplayForm should return True if the display form -- can serve as a valid left-hand side. That means checking that it is a -- defined name applied to valid lhs eliminators (projections or -- applications to constructor patterns). okDisplayForm :: DisplayTerm -> Bool okDisplayForm (DWithApp d ds es) = okDisplayForm d && all okDisplayTerm ds && all okToDropE es -- Andreas, 2016-05-03, issue #1950. -- We might drop trailing hidden trivial (=variable) patterns. okDisplayForm (DTerm (I.Def f vs)) = all okElim vs okDisplayForm (DDef f es) = all okDElim es okDisplayForm DDot{} = False okDisplayForm DCon{} = False okDisplayForm DTerm{} = False okDisplayTerm :: DisplayTerm -> Bool okDisplayTerm (DTerm v) = okTerm v okDisplayTerm DDot{} = True okDisplayTerm DCon{} = True okDisplayTerm DDef{} = False okDisplayTerm _ = False okDElim :: Elim' DisplayTerm -> Bool okDElim (I.Apply v) = okDisplayTerm $ unArg v okDElim I.Proj{} = True okToDropE :: Elim' Term -> Bool okToDropE (I.Apply v) = okToDrop v okToDropE I.Proj{} = False okToDrop :: Arg I.Term -> Bool okToDrop arg = notVisible arg && case ignoreSharing $ unArg arg of I.Var _ [] -> True I.DontCare{} -> True -- no matching on irrelevant things. __IMPOSSIBLE__ anyway? I.Level{} -> True -- no matching on levels. __IMPOSSIBLE__ anyway? _ -> False okArg :: Arg I.Term -> Bool okArg = okTerm . unArg okElim :: Elim' I.Term -> Bool okElim (I.Apply a) = okArg a okElim (I.Proj{}) = True okTerm :: I.Term -> Bool okTerm (I.Var _ []) = True okTerm (I.Con c ci vs) = all okArg vs okTerm (I.Def x []) = isNoName $ qnameToConcrete x -- Handling wildcards in display forms okTerm _ = False -- Flatten a dt into (parentName, parentElims, withArgs). flattenWith :: DisplayTerm -> (QName, [I.Elim' DisplayTerm], [I.Elim' DisplayTerm]) flattenWith (DWithApp d ds1 es2) = let (f, es, ds0) = flattenWith d in (f, es, ds0 ++ map (I.Apply . defaultArg) ds1 ++ map (fmap DTerm) es2) flattenWith (DDef f es) = (f, es, []) -- .^ hacky, but we should only hit this when printing debug info flattenWith (DTerm (I.Def f es)) = (f, map (fmap DTerm) es, []) flattenWith _ = __IMPOSSIBLE__ displayLHS :: [NamedArg A.Pattern] -> [A.Pattern] -> DisplayTerm -> TCM A.SpineLHS displayLHS ps wps d = do let (f, vs, es) = flattenWith d ds <- mapM (namedArg <.> elimToPat) es vs <- mapM elimToPat vs return $ SpineLHS i f vs (ds ++ wps) where argToPat :: Arg DisplayTerm -> TCM (NamedArg A.Pattern) argToPat arg = traverse termToPat arg elimToPat :: I.Elim' DisplayTerm -> TCM (NamedArg A.Pattern) elimToPat (I.Apply arg) = argToPat arg elimToPat (I.Proj o d) = return $ defaultNamedArg $ A.ProjP patNoRange o $ AmbQ [d] termToPat :: DisplayTerm -> TCM (Named_ A.Pattern) termToPat (DTerm (I.Var n [])) = return $ unArg $ fromMaybe __IMPOSSIBLE__ $ ps !!! n termToPat (DCon c ci vs) = fmap unnamed <$> tryRecPFromConP =<< do A.ConP (ConPatInfo ci patNoRange) (AmbQ [conName c]) <$> mapM argToPat vs termToPat (DTerm (I.Con c ci vs)) = fmap unnamed <$> tryRecPFromConP =<< do A.ConP (ConPatInfo ci patNoRange) (AmbQ [conName c]) <$> mapM (argToPat . fmap DTerm) vs termToPat (DTerm (I.Def _ [])) = return $ unnamed $ A.WildP patNoRange termToPat (DDef _ []) = return $ unnamed $ A.WildP patNoRange -- Currently we don't keep track of the origin of a dot pattern in the internal syntax, -- so here we give __IMPOSSIBLE__. This is only used for printing purposes, the origin -- should not be used anyway after this point. -- Andreas, 2017-02-14: This crashes with -v 100. -- termToPat (DDot v) = A.DotP patNoRange __IMPOSSIBLE__ <$> termToExpr v -- termToPat v = A.DotP patNoRange __IMPOSSIBLE__ <$> reify v -- __IMPOSSIBLE__ termToPat (DDot v) = unnamed . A.DotP patNoRange Inserted <$> termToExpr v termToPat v = unnamed . A.DotP patNoRange Inserted <$> reify v len = length ps argsToExpr :: I.Args -> TCM [Arg A.Expr] argsToExpr = mapM (traverse termToExpr) -- TODO: restructure this to avoid having to repeat the code for reify termToExpr :: Term -> TCM A.Expr termToExpr v = do reportSLn "reify.display" 60 $ "termToExpr " ++ show v -- After unSpine, a Proj elimination is __IMPOSSIBLE__! case unSpine v of I.Con c ci vs -> apps (A.Con (AmbQ [conName c])) =<< argsToExpr vs I.Def f es -> do let vs = fromMaybe __IMPOSSIBLE__ $ mapM isApplyElim es apps (A.Def f) =<< argsToExpr vs I.Var n es -> do let vs = fromMaybe __IMPOSSIBLE__ $ mapM isApplyElim es -- Andreas, 2014-06-11 Issue 1177 -- due to β-normalization in substitution, -- even the pattern variables @n < len@ can be -- applied to some args @vs@. e <- if n < len then return $ A.patternToExpr $ namedArg $ ps !! n else reify (I.var (n - len)) apps e =<< argsToExpr vs _ -> return underscore instance Reify Literal Expr where reifyWhen = reifyWhenE reify l = return (A.Lit l) instance Reify Term Expr where reifyWhen = reifyWhenE reify v = reifyTerm True v reifyTerm :: Bool -> Term -> TCM Expr reifyTerm expandAnonDefs0 v = do metasBare <- asks envPrintMetasBare -- Ulf 2014-07-10: Don't expand anonymous when display forms are disabled -- (i.e. when we don't care about nice printing) expandAnonDefs <- return expandAnonDefs0 `and2M` displayFormsEnabled -- Andreas, 2016-07-21 if --postfix-projections -- then we print system-generated projections as postfix, else prefix. havePfp <- optPostfixProjections <$> pragmaOptions let pred = if havePfp then (== ProjPrefix) else (/= ProjPostfix) v <- ignoreSharing <$> instantiate v case applyUnless metasBare (unSpine' pred) v of I.Var n es -> do x <- liftTCM $ nameOfBV n `catchError` \_ -> freshName_ ("@" ++ show n) elims (A.Var x) =<< reify es I.Def x es -> do reifyDisplayForm x es $ reifyDef expandAnonDefs x es I.Con c ci vs -> do let x = conName c isR <- isGeneratedRecordConstructor x case isR || ci == ConORec of True -> do showImp <- showImplicitArguments let keep (a, v) = showImp || visible a r <- getConstructorData x xs <- getRecordFieldNames r vs <- map unArg <$> reify vs return $ A.Rec noExprInfo $ map (Left . uncurry FieldAssignment . mapFst unArg) $ filter keep $ zip xs vs False -> reifyDisplayForm x (map I.Apply vs) $ do def <- getConstInfo x let Constructor{conPars = np} = theDef def -- if we are the the module that defines constructor x -- then we have to drop at least the n module parameters n <- getDefFreeVars x -- the number of parameters is greater (if the data decl has -- extra parameters) or equal (if not) to n when (n > np) __IMPOSSIBLE__ let h = A.Con (AmbQ [x]) if null vs then return h else do es <- reify vs -- Andreas, 2012-04-20: do not reify parameter arguments of constructor -- if the first regular constructor argument is hidden -- we turn it into a named argument, in order to avoid confusion -- with the parameter arguments which can be supplied in abstract syntax -- -- Andreas, 2012-09-17: this does not remove all sources of confusion, -- since parameters could have the same name as regular arguments -- (see for example the parameter {i} to Data.Star.Star, which is also -- the first argument to the cons). -- @data Star {i}{I : Set i} ... where cons : {i : I} ...@ if np == 0 then apps h es else do -- Get name of first argument from type of constructor. -- Here, we need the reducing version of @telView@ -- because target of constructor could be a definition -- expanding into a function type. See test/succeed/NameFirstIfHidden.agda. TelV tel _ <- telView (defType def) let (pars, rest) = splitAt np $ telToList tel case rest of -- Andreas, 2012-09-18 -- If the first regular constructor argument is hidden, -- we keep the parameters to avoid confusion. (Dom info _ : _) | notVisible info -> do let us = for (drop n pars) $ \ (Dom ai _) -> -- setRelevance Relevant $ hideOrKeepInstance $ Arg ai underscore apps h $ us ++ es -- Note: unless --show-implicit, @apps@ will drop @us@. -- otherwise, we drop all parameters _ -> apps h es -- I.Lam info b | isAbsurdBody b -> return $ A. AbsurdLam noExprInfo $ getHiding info I.Lam info b -> do (x,e) <- reify b return $ A.Lam (setOrigin (getOrigin info) defaultLamInfo_) (DomainFree info x) e -- Andreas, 2011-04-07 we do not need relevance information at internal Lambda I.Lit l -> reify l I.Level l -> reify l I.Pi a b -> case b of NoAbs _ b' | visible a -> uncurry (A.Fun $ noExprInfo) <$> reify (a, b') -- Andreas, 2013-11-11 Hidden/Instance I.Pi must be A.Pi -- since (a) the syntax {A} -> B or {{A}} -> B is not legal -- and (b) the name of the binder might matter. -- See issue 951 (a) and 952 (b). | otherwise -> mkPi b =<< reify a b -> mkPi b =<< do ifM (domainFree a (absBody b)) {- then -} (pure $ Arg (domInfo a) underscore) {- else -} (reify a) where mkPi b (Arg info a) = do (x, b) <- reify b return $ A.Pi noExprInfo [TypedBindings noRange $ Arg info (TBind noRange [pure x] a)] b -- We can omit the domain type if it doesn't have any free variables -- and it's mentioned in the target type. domainFree a b = do df <- asks envPrintDomainFreePi return $ and [df, freeIn 0 b, closed a] I.Sort s -> reify s I.MetaV x es -> do x' <- reify x ifM (asks envPrintMetasBare) {-then-} (return x') {-else-} $ elims x' =<< reify es I.DontCare v -> A.DontCare <$> reifyTerm expandAnonDefs v I.Shared p -> reifyTerm expandAnonDefs $ derefPtr p where -- Andreas, 2012-10-20 expand a copy if not in scope -- to improve error messages. -- Don't do this if we have just expanded into a display form, -- otherwise we loop! reifyDef :: Bool -> QName -> I.Elims -> TCM Expr reifyDef True x es = ifM (not . null . inverseScopeLookupName x <$> getScope) (reifyDef' x es) $ do r <- reduceDefCopy x es case r of YesReduction _ v -> do reportSLn "reify.anon" 60 $ unlines [ "reduction on defined ident. in anonymous module" , "x = " ++ prettyShow x , "v = " ++ show v ] reify v NoReduction () -> do reportSLn "reify.anon" 60 $ unlines [ "no reduction on defined ident. in anonymous module" , "x = " ++ prettyShow x , "es = " ++ show es ] reifyDef' x es reifyDef _ x es = reifyDef' x es reifyDef' :: QName -> I.Elims -> TCM Expr reifyDef' x es = do reportSLn "reify.def" 60 $ "reifying call to " ++ prettyShow x -- We should drop this many arguments from the local context. n <- getDefFreeVars x reportSLn "reify.def" 70 $ "freeVars for " ++ prettyShow x ++ " = " ++ show n -- If the definition is not (yet) in the signature, -- we just do the obvious. let fallback _ = elims (A.Def x) =<< reify (drop n es) caseEitherM (getConstInfo' x) fallback $ \ defn -> do let def = theDef defn -- Check if we have an absurd lambda. case def of Function{ funCompiled = Just Fail, funClauses = [cl] } | isAbsurdLambdaName x -> do -- get hiding info from last pattern, which should be () let h = getHiding $ last $ namedClausePats cl elims (A.AbsurdLam defaultLamInfo_ h) =<< reify (drop n es) -- Otherwise (no absurd lambda): _ -> do -- Andrea(s), 2016-07-06 -- Extended lambdas are not considered to be projection like, -- as they are mutually recursive with their parent. -- Thus we do not have to consider padding them. -- Check whether we have an extended lambda and display forms are on. df <- displayFormsEnabled toppars <- size <$> do lookupSection $ qnameModule x let extLam = case def of Function{ funExtLam = Just{}, funProjection = Just{} } -> __IMPOSSIBLE__ Function{ funExtLam = Just (ExtLamInfo h nh) } -> Just (toppars + h + nh) _ -> Nothing case extLam of Just pars | df -> reifyExtLam x pars (defClauses defn) es -- Otherwise (ordinary function call): _ -> do (pad, nes :: [Elim' (Named_ Term)]) <- case def of Function{ funProjection = Just Projection{ projIndex = np } } | np > 0 -> do reportSLn "reify.def" 70 $ " def. is a projection with projIndex = " ++ show np -- This is tricky: -- * getDefFreeVars x tells us how many arguments -- are part of the local context -- * some of those arguments might have been dropped -- due to projection likeness -- * when showImplicits is on we'd like to see the dropped -- projection arguments TelV tel _ <- telViewUpTo np (defType defn) let (as, rest) = splitAt (np - 1) $ telToList tel dom = fromMaybe __IMPOSSIBLE__ $ headMaybe rest -- These are the dropped projection arguments scope <- getScope let underscore = A.Underscore $ Info.emptyMetaInfo { metaScope = scope } let pad = for as $ \ (Dom ai (x, _)) -> Arg ai $ Named (Just $ unranged x) underscore -- Now pad' ++ es' = drop n (pad ++ es) let pad' = drop n pad es' = drop (max 0 (n - size pad)) es -- Andreas, 2012-04-21: get rid of hidden underscores {_} and {{_}} -- Keep non-hidden arguments of the padding. -- -- Andreas, 2016-12-20, issue #2348: -- Let @padTail@ be the list of arguments of the padding -- (*) after the last visible argument of the padding, and -- (*) with the same visibility as the first regular argument. -- If @padTail@ is not empty, we need to -- print the first regular argument with name. -- We further have to print all elements of @padTail@ -- which have the same name and visibility of the -- first regular argument. showImp <- showImplicitArguments -- Get the visible arguments of the padding and the rest -- after the last visible argument. let (padVisNamed, padRest) = filterAndRest visible pad' -- Remove the names from the visible arguments. let padVis = map (fmap (unnamed . namedThing)) padVisNamed -- Keep only the rest with the same visibility of @dom@... let padTail = filter (sameHiding dom) padRest -- ... and even the same name. let padSame = filter ((Just (fst (unDom dom)) ==) . fmap rangedThing . nameOf . unArg) padTail return $ if null padTail || not showImp then (padVis , map (fmap unnamed) es') else (padVis ++ padSame, nameFirstIfHidden dom es') -- If it is not a projection(-like) function, we need no padding. _ -> return ([], map (fmap unnamed) $ drop n es) reportSLn "reify.def" 70 $ unlines [ " pad = " ++ show pad , " nes = " ++ show nes ] let hd = List.foldl' (A.App noExprInfo) (A.Def x) pad nelims hd =<< reify nes -- Andreas, 2016-07-06 Issue #2047 -- With parameter refinement, the "parameter" patterns of an extended -- lambda can now be different from variable patterns. If we just drop -- them (plus the associated arguments to the extended lambda), we produce -- something -- * that violates internal invariants. In particular, the permutation -- dbPatPerm from the patterns to the telescope can no longer be -- computed. (And in fact, dropping from the start of the telescope is -- just plainly unsound then.) -- * prints the wrong thing (old fix for #2047) -- What we do now, is more sound, although not entirely satisfying: -- When the "parameter" patterns of an external lambdas are not variable -- patterns, we fall back to printing the internal function created for the -- extended lambda, instead trying to construct the nice syntax. reifyExtLam :: QName -> Int -> [I.Clause] -> I.Elims -> TCM Expr reifyExtLam x npars cls es = do reportSLn "reify.def" 10 $ "reifying extended lambda " ++ prettyShow x reportSLn "reify.def" 50 $ render $ nest 2 $ vcat [ text "npars =" <+> pretty npars , text "es =" <+> fsep (map (prettyPrec 10) es) , text "def =" <+> vcat (map pretty cls) ] -- As extended lambda clauses live in the top level, we add the whole -- section telescope to the number of parameters. let (pars, rest) = splitAt npars es -- Since we applying the clauses to the parameters, -- we do not need to drop their initial "parameter" patterns -- (this is taken care of by @apply@). cls <- mapM (reify . NamedClause x False . (`applyE` pars)) cls let cx = nameConcrete $ qnameName x dInfo = mkDefInfo cx noFixity' PublicAccess ConcreteDef (getRange x) elims (A.ExtendedLam defaultLamInfo_ dInfo x cls) =<< reify rest -- | @nameFirstIfHidden (x:a) ({e} es) = {x = e} es@ nameFirstIfHidden :: Dom (ArgName, t) -> [Elim' a] -> [Elim' (Named_ a)] nameFirstIfHidden dom (I.Apply (Arg info e) : es) | notVisible info = I.Apply (Arg info (Named (Just $ unranged $ fst $ unDom dom) e)) : map (fmap unnamed) es nameFirstIfHidden _ es = map (fmap unnamed) es instance Reify i a => Reify (Named n i) (Named n a) where reify = traverse reify reifyWhen b = traverse (reifyWhen b) -- | Skip reification of implicit and irrelevant args if option is off. instance (Reify i a) => Reify (Arg i) (Arg a) where reify (Arg info i) = Arg info <$> (flip reifyWhen i =<< condition) where condition = (return (argInfoHiding info /= Hidden) `or2M` showImplicitArguments) `and2M` (return (argInfoRelevance info /= Irrelevant) `or2M` showIrrelevantArguments) reifyWhen b i = traverse (reifyWhen b) i data NamedClause = NamedClause QName Bool I.Clause -- ^ Also tracks whether module parameters should be dropped from the patterns. -- The Monoid instance for Data.Map doesn't require that the values are a -- monoid. newtype MonoidMap k v = MonoidMap { unMonoidMap :: Map.Map k v } instance (Ord k, Monoid v) => Semigroup (MonoidMap k v) where MonoidMap m1 <> MonoidMap m2 = MonoidMap (Map.unionWith mappend m1 m2) instance (Ord k, Monoid v) => Monoid (MonoidMap k v) where mempty = MonoidMap Map.empty mappend = (<>) -- | Removes implicit arguments that are not needed, that is, that don't bind -- any variables that are actually used and doesn't do pattern matching. -- Doesn't strip any arguments that were written explicitly by the user. stripImplicits :: ([NamedArg A.Pattern], [A.Pattern]) -> TCM ([NamedArg A.Pattern], [A.Pattern]) stripImplicits (ps, wps) = do -- v if show-implicit we don't need the names ifM showImplicitArguments (return (map (unnamed . namedThing <$>) ps, wps)) $ do reportSLn "reify.implicit" 30 $ unlines [ "stripping implicits" , " ps = " ++ show ps , " wps = " ++ show wps ] let allps = ps ++ map defaultNamedArg wps sps = blankDots $ strip allps (ps', wps') = splitAt (length sps - length wps) sps reportSLn "reify.implicit" 30 $ unlines [ " ps' = " ++ show ps' , " wps' = " ++ show (map namedArg wps') ] return (ps', map namedArg wps') where -- Replace variables in dot patterns by an underscore _ if they are hidden -- in the pattern. This is slightly nicer than making the implicts explicit. blankDots ps = blank (varsBoundIn ps) ps strip ps = stripArgs True ps where stripArgs _ [] = [] stripArgs fixedPos (a : as) -- Andreas, 2017-01-18, issue #819: preserves _ when splitting: -- An Inserted visible variable comes form a WildP and is restored as such. | visible a, getOrigin a == Inserted, varOrDot (namedArg a) = goWild -- A hidden non-UserWritten variable is removed if not needed for -- correct position of the following hidden arguments. | canStrip a = if all canStrip $ takeWhile isUnnamedHidden as then stripArgs False as else goWild -- Other arguments are kept. | otherwise = stripName fixedPos (stripArg a) : stripArgs True as where a' = setNamedArg a $ A.WildP $ Info.PatRange $ getRange a goWild = stripName fixedPos a' : stripArgs True as stripName True = fmap (unnamed . namedThing) stripName False = id canStrip a = and [ notVisible a , getOrigin a `notElem` [ UserWritten , CaseSplit ] , varOrDot (namedArg a) ] isUnnamedHidden x = notVisible x && nameOf (unArg x) == Nothing && isNothing (isProjP x) stripArg a = fmap (fmap stripPat) a stripPat p = case p of A.VarP _ -> p A.ConP i c ps -> A.ConP i c $ stripArgs True ps A.ProjP{} -> p A.DefP _ _ _ -> p A.DotP _ _ e -> p A.WildP _ -> p A.AbsurdP _ -> p A.LitP _ -> p A.AsP i x p -> A.AsP i x $ stripPat p A.PatternSynP _ _ _ -> __IMPOSSIBLE__ -- p A.RecP i fs -> A.RecP i $ map (fmap stripPat) fs -- TODO Andreas: is this right? varOrDot A.VarP{} = True varOrDot A.WildP{} = True varOrDot A.DotP{} = True varOrDot (A.ConP cpi _ ps) | patOrigin cpi == ConOSystem = all varOrDot $ map namedArg ps varOrDot _ = False -- | @blank bound e@ replaces all variables in expression @e@ that are not in @bound@ by -- an underscore @_@. It is used for printing dot patterns: we don't want to -- make implicit variables explicit, so we blank them out in the dot patterns -- instead (this is fine since dot patterns can be inferred anyway). class BlankVars a where blank :: Set Name -> a -> a default blank :: (Functor f, BlankVars b, f b ~ a) => Set Name -> a -> a blank = fmap . blank instance BlankVars a => BlankVars (Arg a) where instance BlankVars a => BlankVars (Named s a) where instance BlankVars a => BlankVars [a] where instance BlankVars a => BlankVars (A.Pattern' a) where instance BlankVars a => BlankVars (FieldAssignment' a) where instance (BlankVars a, BlankVars b) => BlankVars (a, b) where blank bound (x, y) = (blank bound x, blank bound y) instance (BlankVars a, BlankVars b) => BlankVars (Either a b) where blank bound (Left x) = Left $ blank bound x blank bound (Right y) = Right $ blank bound y instance BlankVars A.NamedDotPattern where blank bound = id instance BlankVars A.StrippedDotPattern where blank bound = id instance BlankVars A.Clause where blank bound (A.Clause lhs namedDots strippedDots rhs [] ca) = let bound' = varsBoundIn lhs `Set.union` bound in A.Clause (blank bound' lhs) (blank bound' namedDots) (blank bound' strippedDots) (blank bound' rhs) [] ca blank bound (A.Clause lhs namedDots strippedDots rhs (_:_) ca) = __IMPOSSIBLE__ instance BlankVars A.LHS where blank bound (A.LHS i core wps) = uncurry (A.LHS i) $ blank bound (core, wps) instance BlankVars A.LHSCore where blank bound (A.LHSHead f ps) = A.LHSHead f $ blank bound ps blank bound (A.LHSProj p b ps) = uncurry (A.LHSProj p) $ blank bound (b, ps) instance BlankVars A.Expr where blank bound e = case e of A.ScopedExpr i e -> A.ScopedExpr i $ blank bound e A.Var x -> if x `Set.member` bound then e else A.Underscore emptyMetaInfo -- Here is the action! A.Def _ -> e A.Proj{} -> e A.Con _ -> e A.Lit _ -> e A.QuestionMark{} -> e A.Underscore _ -> e A.Dot i e -> A.Dot i $ blank bound e A.App i e1 e2 -> uncurry (A.App i) $ blank bound (e1, e2) A.WithApp i e es -> uncurry (A.WithApp i) $ blank bound (e, es) A.Lam i b e -> let bound' = varsBoundIn b `Set.union` bound in A.Lam i (blank bound b) (blank bound' e) A.AbsurdLam _ _ -> e A.ExtendedLam i d f cs -> A.ExtendedLam i d f $ blank bound cs A.Pi i tel e -> let bound' = varsBoundIn tel `Set.union` bound in uncurry (A.Pi i) $ blank bound' (tel, e) A.Fun i a b -> uncurry (A.Fun i) $ blank bound (a, b) A.Set _ _ -> e A.Prop _ -> e A.Let _ _ _ -> __IMPOSSIBLE__ A.Rec i es -> A.Rec i $ blank bound es A.RecUpdate i e es -> uncurry (A.RecUpdate i) $ blank bound (e, es) A.ETel _ -> __IMPOSSIBLE__ A.QuoteGoal {} -> __IMPOSSIBLE__ A.QuoteContext {} -> __IMPOSSIBLE__ A.Quote {} -> __IMPOSSIBLE__ A.QuoteTerm {} -> __IMPOSSIBLE__ A.Unquote {} -> __IMPOSSIBLE__ A.Tactic {} -> __IMPOSSIBLE__ A.DontCare v -> A.DontCare $ blank bound v A.PatternSyn {} -> e A.Macro {} -> e instance BlankVars A.ModuleName where blank bound = id instance BlankVars RHS where blank bound (RHS e mc) = RHS (blank bound e) mc blank bound AbsurdRHS = AbsurdRHS blank bound (WithRHS _ es clauses) = __IMPOSSIBLE__ -- NZ blank bound (RewriteRHS xes rhs _) = __IMPOSSIBLE__ -- NZ instance BlankVars A.LamBinding where blank bound b@A.DomainFree{} = b blank bound (A.DomainFull bs) = A.DomainFull $ blank bound bs instance BlankVars TypedBindings where blank bound (TypedBindings r bs) = TypedBindings r $ blank bound bs instance BlankVars TypedBinding where blank bound (TBind r n e) = TBind r n $ blank bound e blank bound (TLet _ _) = __IMPOSSIBLE__ -- Since the internal syntax has no let bindings left -- | Collect the binders in some abstract syntax lhs. class Binder a where varsBoundIn :: a -> Set Name default varsBoundIn :: (Foldable f, Binder b, f b ~ a) => a -> Set Name varsBoundIn = foldMap varsBoundIn instance Binder A.LHS where varsBoundIn (A.LHS _ core ps) = varsBoundIn (core, ps) instance Binder A.LHSCore where varsBoundIn (A.LHSHead _ ps) = varsBoundIn ps varsBoundIn (A.LHSProj _ b ps) = varsBoundIn (b, ps) instance Binder A.Pattern where varsBoundIn = foldAPattern $ \case A.VarP x -> if prettyShow x == "()" then empty else singleton x -- TODO: get rid of this hack? A.AsP _ x _ -> empty A.ConP _ _ _ -> empty A.ProjP{} -> empty A.DefP _ _ _ -> empty A.WildP{} -> empty A.DotP{} -> empty A.AbsurdP{} -> empty A.LitP{} -> empty A.PatternSynP _ _ _ -> empty A.RecP _ _ -> empty instance Binder A.LamBinding where varsBoundIn (A.DomainFree _ x) = singleton x varsBoundIn (A.DomainFull b) = varsBoundIn b instance Binder TypedBindings where varsBoundIn (TypedBindings _ b) = varsBoundIn b instance Binder TypedBinding where varsBoundIn (TBind _ xs _) = varsBoundIn xs varsBoundIn (TLet _ bs) = varsBoundIn bs instance Binder LetBinding where varsBoundIn (LetBind _ _ x _ _) = singleton x varsBoundIn (LetPatBind _ p _) = varsBoundIn p varsBoundIn LetApply{} = empty varsBoundIn LetOpen{} = empty varsBoundIn LetDeclaredVariable{} = empty instance Binder (WithHiding Name) where varsBoundIn (WithHiding _ x) = singleton x instance Binder a => Binder (FieldAssignment' a) where instance Binder a => Binder (Arg a) where instance Binder a => Binder (Named x a) where instance Binder a => Binder [a] where instance (Binder a, Binder b) => Binder (a, b) where varsBoundIn (x, y) = varsBoundIn x `Set.union` varsBoundIn y -- | Assumes that pattern variables have been added to the context already. -- Picks pattern variable names from context. reifyPatterns :: MonadTCM tcm => [NamedArg I.DeBruijnPattern] -> tcm [NamedArg A.Pattern] reifyPatterns = mapM $ stripNameFromExplicit <.> traverse (traverse reifyPat) where stripNameFromExplicit :: NamedArg p -> NamedArg p stripNameFromExplicit a | visible a = fmap (unnamed . namedThing) a | otherwise = a reifyPat :: MonadTCM tcm => I.DeBruijnPattern -> tcm A.Pattern reifyPat p = do liftTCM $ reportSLn "reify.pat" 80 $ "reifying pattern " ++ show p case p of I.VarP x -> do n <- liftTCM $ nameOfBV $ dbPatVarIndex x case dbPatVarName x of "_" -> return $ A.VarP n -- Andreas, 2017-09-03: TODO for #2580 -- Patterns @VarP "()"@ should have been replaced by @AbsurdP@, but the -- case splitter still produces them. y -> if prettyShow (nameConcrete n) == "()" then return $ A.VarP n else -- Andreas, 2017-09-03, issue #2729 -- Restore original pattern name. AbstractToConcrete picks unique names. return $ A.VarP n { nameConcrete = C.Name noRange [ C.Id y ] } I.DotP v -> do t <- liftTCM $ reify v -- This is only used for printing purposes, so the Origin shouldn't be -- used after this point anyway. return $ A.DotP patNoRange Inserted t -- WAS: return $ A.DotP patNoRange __IMPOSSIBLE__ t -- Crashes on -v 100. I.AbsurdP p -> return $ A.AbsurdP patNoRange I.LitP l -> return $ A.LitP l I.ProjP o d -> return $ A.ProjP patNoRange o $ AmbQ [d] I.ConP c cpi ps -> do liftTCM $ reportSLn "reify.pat" 60 $ "reifying pattern " ++ show p tryRecPFromConP =<< do A.ConP ci (AmbQ [conName c]) <$> reifyPatterns ps where ci = ConPatInfo origin patNoRange origin = fromMaybe ConOCon $ I.conPRecord cpi -- | If the record constructor is generated or the user wrote a record pattern, -- turn constructor pattern into record pattern. -- Otherwise, keep constructor pattern. tryRecPFromConP :: MonadTCM tcm => A.Pattern -> tcm A.Pattern tryRecPFromConP p = do let fallback = return p case p of A.ConP ci (AmbQ [c]) ps -> do caseMaybeM (liftTCM $ isRecordConstructor c) fallback $ \ (r, def) -> do -- If the record constructor is generated or the user wrote a record pattern, -- print record pattern. -- Otherwise, print constructor pattern. if recNamedCon def && patOrigin ci /= ConORec then fallback else do fs <- liftTCM $ getRecordFieldNames r unless (length fs == length ps) __IMPOSSIBLE__ return $ A.RecP patNoRange $ zipWith mkFA fs ps where mkFA ax nap = FieldAssignment (unArg ax) (namedArg nap) _ -> __IMPOSSIBLE__ instance Reify (QNamed I.Clause) A.Clause where reify (QNamed f cl) = reify (NamedClause f True cl) instance Reify NamedClause A.Clause where reify (NamedClause f toDrop cl) = addContext (clauseTel cl) $ do reportSLn "reify.clause" 60 $ "reifying NamedClause" ++ "\n f = " ++ prettyShow f ++ "\n toDrop = " ++ show toDrop ++ "\n cl = " ++ show cl ps <- reifyPatterns $ namedClausePats cl lhs <- liftTCM $ reifyDisplayFormP $ SpineLHS info f ps [] -- LHS info (LHSHead f ps) [] -- Unless @toDrop@ we have already dropped the module patterns from the clauses -- (e.g. for extended lambdas). lhs <- if not toDrop then return lhs else do nfv <- getDefFreeVars f `catchError` \_ -> return 0 return $ dropParams nfv lhs lhs <- stripImps lhs reportSLn "reify.clause" 60 $ "reifying NamedClause, lhs = " ++ show lhs rhs <- caseMaybe (clauseBody cl) (return AbsurdRHS) $ \ e -> do RHS <$> reify e <*> pure Nothing reportSLn "reify.clause" 60 $ "reifying NamedClause, rhs = " ++ show rhs let result = A.Clause (spineToLhs lhs) [] [] rhs [] (I.clauseCatchall cl) reportSLn "reify.clause" 60 $ "reified NamedClause, result = " ++ show result return result where perm = fromMaybe __IMPOSSIBLE__ $ clausePerm cl info = LHSRange noRange dropParams n (SpineLHS i f ps wps) = SpineLHS i f (drop n ps) wps stripImps (SpineLHS i f ps wps) = do (ps, wps) <- stripImplicits (ps, wps) return $ SpineLHS i f ps wps instance Reify Type Expr where reifyWhen = reifyWhenE reify (I.El _ t) = reify t instance Reify Sort Expr where reifyWhen = reifyWhenE reify s = do s <- instantiateFull s case s of I.Type (I.Max []) -> return $ A.Set noExprInfo 0 I.Type (I.Max [I.ClosedLevel n]) -> return $ A.Set noExprInfo n I.Type a -> do a <- reify a return $ A.App noExprInfo (A.Set noExprInfo 0) (defaultNamedArg a) I.Prop -> return $ A.Prop noExprInfo I.Inf -> A.Var <$> freshName_ ("Setω" :: String) I.SizeUniv -> do I.Def sizeU [] <- primSizeUniv return $ A.Def sizeU I.DLub s1 s2 -> do lub <- freshName_ ("dLub" :: String) -- TODO: hack (e1,e2) <- reify (s1, I.Lam defaultArgInfo $ fmap Sort s2) let app x y = A.App noExprInfo x (defaultNamedArg y) return $ A.Var lub `app` e1 `app` e2 instance Reify Level Expr where reifyWhen = reifyWhenE reify l = reify =<< reallyUnLevelView l instance (Free i, Reify i a) => Reify (Abs i) (Name, a) where reify (NoAbs x v) = (,) <$> freshName_ x <*> reify v reify (Abs s v) = do -- If the bound variable is free in the body, then the name "_" is -- replaced by "z". s <- return $ if isUnderscore s && 0 `freeIn` v then "z" else s x <- freshName_ s e <- addContext' x -- type doesn't matter $ reify v return (x,e) instance Reify I.Telescope A.Telescope where reify EmptyTel = return [] reify (ExtendTel arg tel) = do Arg info e <- reify arg (x,bs) <- reify tel let r = getRange e return $ TypedBindings r (Arg info (TBind r [pure x] e)) : bs instance Reify i a => Reify (Dom i) (Arg a) where reify (Dom info i) = Arg info <$> reify i instance Reify i a => Reify (I.Elim' i) (I.Elim' a) where reify = traverse reify reifyWhen b = traverse (reifyWhen b) instance Reify i a => Reify [i] [a] where reify = traverse reify reifyWhen b = traverse (reifyWhen b) instance (Reify i1 a1, Reify i2 a2) => Reify (i1,i2) (a1,a2) where reify (x,y) = (,) <$> reify x <*> reify y instance (Reify i1 a1, Reify i2 a2, Reify i3 a3) => Reify (i1,i2,i3) (a1,a2,a3) where reify (x,y,z) = (,,) <$> reify x <*> reify y <*> reify z instance (Reify i1 a1, Reify i2 a2, Reify i3 a3, Reify i4 a4) => Reify (i1,i2,i3,i4) (a1,a2,a3,a4) where reify (x,y,z,w) = (,,,) <$> reify x <*> reify y <*> reify z <*> reify w Agda-2.5.3/src/full/Agda/Syntax/Translation/ReflectedToAbstract.hs0000644000000000000000000001474413154613124023146 0ustar0000000000000000{-# LANGUAGE UndecidableInstances #-} {-# OPTIONS_GHC -fwarn-missing-signatures #-} module Agda.Syntax.Translation.ReflectedToAbstract where import Control.Applicative import Control.Monad.Reader import Data.Traversable as Trav hiding (mapM) import Agda.Syntax.Fixity import Agda.Syntax.Literal import Agda.Syntax.Position import Agda.Syntax.Info import Agda.Syntax.Common import Agda.Syntax.Abstract as A hiding (Apply) import Agda.Syntax.Reflected as R import Agda.TypeChecking.Monad as M hiding (MetaInfo) import Agda.Syntax.Scope.Monad (getCurrentModule) import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.List import Agda.Utils.Functor import Agda.Utils.Size type Names = [Name] type WithNames a = ReaderT Names TCM a -- Note: we only need the TCM for fresh names -- | Adds a new unique name to the current context. withName :: String -> (Name -> WithNames a) -> WithNames a withName s f = do name <- freshName_ s ctx <- asks $ map nameConcrete let name' = head $ filter (notTaken ctx) $ iterate nextName name local (name:) $ f name' where notTaken xs x = isNoName x || nameConcrete x `notElem` xs -- | Returns the name of the variable with the given de Bruijn index. askName :: Int -> WithNames (Maybe Name) askName i = reader (!!! i) class ToAbstract r a | r -> a where toAbstract :: r -> WithNames a -- | Translate reflected syntax to abstract, using the names from the current typechecking context. toAbstract_ :: ToAbstract r a => r -> TCM a toAbstract_ = withShowAllArguments . toAbstractWithoutImplicit -- | Drop implicit arguments unless --show-implicit is on. toAbstractWithoutImplicit :: ToAbstract r a => r -> TCM a toAbstractWithoutImplicit x = runReaderT (toAbstract x) =<< getContextNames instance ToAbstract r a => ToAbstract (Named name r) (Named name a) where toAbstract = traverse toAbstract instance ToAbstract r a => ToAbstract (Arg r) (NamedArg a) where toAbstract (Arg i x) = Arg i <$> toAbstract (unnamed x) instance ToAbstract [Arg Term] [NamedArg Expr] where toAbstract = traverse toAbstract instance ToAbstract r Expr => ToAbstract (Dom r, Name) (A.TypedBindings) where toAbstract (Dom i x, name) = do dom <- toAbstract x return $ TypedBindings noRange $ Arg i $ TBind noRange [pure name] dom instance ToAbstract (Expr, Elim) Expr where toAbstract (f, Apply arg) = do arg <- toAbstract arg showImp <- lift showImplicitArguments return $ if showImp || visible arg then App (ExprRange noRange) f arg else f instance ToAbstract (Expr, Elims) Expr where toAbstract (f, elims) = foldM (curry toAbstract) f elims instance ToAbstract r a => ToAbstract (R.Abs r) (a, Name) where toAbstract (Abs s x) = withName s' $ \name -> (,) <$> toAbstract x <*> return name where s' = if (isNoName s) then "z" else s -- TODO: only do this when var is free instance ToAbstract Literal Expr where toAbstract l = return (A.Lit l) instance ToAbstract Term Expr where toAbstract t = case t of R.Var i es -> do mname <- askName i case mname of Nothing -> do cxt <- lift $ getContextTelescope names <- asks $ drop (size cxt) . reverse lift $ withShowAllArguments' False $ typeError $ DeBruijnIndexOutOfScope i cxt names Just name -> toAbstract (A.Var name, es) R.Con c es -> toAbstract (A.Con (AmbQ [killRange c]), es) R.Def f es -> do af <- lift $ mkDef (killRange f) toAbstract (af, es) R.Lam h t -> do (e, name) <- toAbstract t let info = setHiding h $ setOrigin Reflected defaultArgInfo return $ A.Lam (setOrigin Reflected defaultLamInfo_) (DomainFree info name) e R.ExtLam cs es -> do name <- freshName_ extendedLambdaName m <- lift $ getCurrentModule let qname = qualify m name cname = nameConcrete name defInfo = mkDefInfo cname noFixity' PublicAccess ConcreteDef noRange cs <- toAbstract $ map (QNamed qname) cs toAbstract (A.ExtendedLam (setOrigin Reflected defaultLamInfo_) defInfo qname cs, es) R.Pi a b -> do (b, name) <- toAbstract b a <- toAbstract (a, name) return $ A.Pi exprNoRange [a] b R.Sort s -> toAbstract s R.Lit l -> toAbstract l R.Meta x es -> toAbstract (A.Underscore info, es) where info = emptyMetaInfo{ metaNumber = Just x } R.Unknown -> return $ Underscore emptyMetaInfo mkDef :: QName -> TCM A.Expr mkDef f = ifM (isMacro . theDef <$> getConstInfo f) (return $ A.Macro f) (return $ A.Def f) mkSet :: Expr -> Expr mkSet e = App exprNoRange (A.Set exprNoRange 0) $ defaultNamedArg e instance ToAbstract Sort Expr where toAbstract (SetS x) = mkSet <$> toAbstract x toAbstract (LitS x) = return $ A.Set exprNoRange x toAbstract UnknownS = return $ mkSet $ Underscore emptyMetaInfo instance ToAbstract R.Pattern (Names, A.Pattern) where toAbstract pat = case pat of R.ConP c args -> do (names, args) <- toAbstractPats args return (names, A.ConP (ConPatInfo ConOCon patNoRange) (AmbQ [killRange c]) args) R.DotP -> return ([], A.WildP patNoRange) R.VarP s | isNoName s -> withName "z" $ \ name -> return ([name], A.VarP name) -- Ulf, 2016-08-09: Also bind noNames (#2129). This to make the -- behaviour consistent with lambda and pi. -- return ([], A.WildP patNoRange) R.VarP s -> withName s $ \ name -> return ([name], A.VarP name) R.LitP l -> return ([], A.LitP l) R.AbsurdP -> return ([], A.AbsurdP patNoRange) R.ProjP d -> return ([], A.ProjP patNoRange ProjSystem $ AmbQ [killRange d]) toAbstractPats :: [Arg R.Pattern] -> WithNames (Names, [NamedArg A.Pattern]) toAbstractPats pats = case pats of [] -> return ([], []) p:ps -> do (names, p) <- (distributeF . fmap distributeF) <$> toAbstract p (namess, ps) <- local (names++) $ toAbstractPats ps return (namess++names, p:ps) instance ToAbstract (QNamed R.Clause) A.Clause where toAbstract (QNamed name (R.Clause pats rhs)) = do (names, pats) <- toAbstractPats pats rhs <- local (names++) $ toAbstract rhs let lhs = spineToLhs $ SpineLHS (LHSRange noRange) name pats [] return $ A.Clause lhs [] [] (RHS rhs Nothing) [] False toAbstract (QNamed name (R.AbsurdClause pats)) = do (_, pats) <- toAbstractPats pats let lhs = spineToLhs $ SpineLHS (LHSRange noRange) name pats [] return $ A.Clause lhs [] [] AbsurdRHS [] False instance ToAbstract [QNamed R.Clause] [A.Clause] where toAbstract = traverse toAbstract Agda-2.5.3/src/full/Agda/Syntax/Concrete/0000755000000000000000000000000013154613124016160 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Syntax/Concrete/Pretty.hs0000644000000000000000000005714013154613124020012 0ustar0000000000000000{-# OPTIONS_GHC -fno-warn-orphans #-} {-# LANGUAGE CPP #-} {-| Pretty printer for the concrete syntax. -} module Agda.Syntax.Concrete.Pretty where import Prelude hiding (null) import Data.Functor import Data.Maybe import qualified Data.Strict.Maybe as Strict import Agda.Syntax.Common import Agda.Syntax.Concrete import Agda.Syntax.Fixity import Agda.Syntax.Notation import Agda.Syntax.Position import Agda.TypeChecking.Positivity.Occurrence import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.Null import Agda.Utils.Pretty import Agda.Utils.String #include "undefined.h" import Agda.Utils.Impossible instance Show Expr where show = show . pretty instance Show Declaration where show = show . pretty instance Show Pattern where show = show . pretty instance Show TypedBinding where show = show . pretty instance Show TypedBindings where show = show . pretty instance Show LamBinding where show = show . pretty instance (Pretty a, Pretty b) => Show (ImportDirective' a b) where show = show . pretty instance Show Pragma where show = show . pretty instance Show RHS where show = show . pretty braces' :: Doc -> Doc braces' d = case render d of -- Add space to avoid starting a comment '-':_ -> braces (text " " <> d) _ -> braces d -- double braces... dbraces :: Doc -> Doc dbraces = braces . braces' -- Lays out a list of documents [d₁, d₂, …] in the following way: -- @ -- { d₁ -- ; d₂ -- ⋮ -- } -- @ -- If the list is empty, then the notation @{}@ is used. bracesAndSemicolons :: [Doc] -> Doc bracesAndSemicolons [] = text "{}" bracesAndSemicolons (d : ds) = sep ([text "{" <+> d] ++ map (text ";" <+>) ds ++ [text "}"]) arrow, lambda :: Doc arrow = text "\x2192" lambda = text "\x03bb" -- | @prettyHiding info visible doc@ puts the correct braces -- around @doc@ according to info @info@ and returns -- @visible doc@ if the we deal with a visible thing. prettyHiding :: LensHiding a => a -> (Doc -> Doc) -> Doc -> Doc prettyHiding a parens = case getHiding a of Hidden -> braces' Instance{} -> dbraces NotHidden -> parens prettyRelevance :: LensRelevance a => a -> Doc -> Doc prettyRelevance a d = if render d == "_" then d else pretty (getRelevance a) <> d instance (Pretty a, Pretty b) => Pretty (a, b) where pretty (a, b) = parens $ pretty a <> comma <+> pretty b instance Pretty (ThingWithFixity Name) where pretty (ThingWithFixity n _) = pretty n instance Pretty a => Pretty (WithHiding a) where pretty w = prettyHiding w id $ pretty $ dget w instance Pretty Relevance where pretty Forced{} = empty pretty Relevant = empty pretty Irrelevant = text "." pretty NonStrict = text ".." instance Pretty Induction where pretty Inductive = text "data" pretty CoInductive = text "codata" instance Pretty (OpApp Expr) where pretty (Ordinary e) = pretty e pretty (SyntaxBindingLambda r bs e) = pretty (Lam r bs e) instance Pretty a => Pretty (MaybePlaceholder a) where pretty Placeholder{} = text "_" pretty (NoPlaceholder _ e) = pretty e instance Pretty Expr where pretty e = case e of Ident x -> pretty x Lit l -> pretty l QuestionMark _ n -> text "?" <> maybe empty (text . show) n Underscore _ n -> maybe underscore text n -- Underscore _ n -> underscore <> maybe empty (text . show) n App _ _ _ -> case appView e of AppView e1 args -> fsep $ pretty e1 : map pretty args -- sep [ pretty e1 -- , nest 2 $ fsep $ map pretty args -- ] RawApp _ es -> fsep $ map pretty es OpApp _ q _ es -> fsep $ prettyOpApp q es WithApp _ e es -> fsep $ pretty e : map ((text "|" <+>) . pretty) es HiddenArg _ e -> braces' $ pretty e InstanceArg _ e -> dbraces $ pretty e Lam _ bs e -> sep [ lambda <+> fsep (map pretty bs) <+> arrow , nest 2 $ pretty e ] AbsurdLam _ NotHidden -> lambda <+> text "()" AbsurdLam _ Instance{} -> lambda <+> text "{{}}" AbsurdLam _ Hidden -> lambda <+> text "{}" ExtendedLam _ pes -> lambda <+> bracesAndSemicolons (map (\(x,y,z,_) -> prettyClause x y z) pes) where prettyClause lhs rhs wh = sep [ pretty lhs , nest 2 $ pretty' rhs ] $$ nest 2 (pretty wh) pretty' (RHS e) = arrow <+> pretty e pretty' AbsurdRHS = empty Fun _ e1 e2 -> sep [ pretty e1 <+> arrow , pretty e2 ] Pi tel e -> sep [ pretty (Tel $ smashTel tel) <+> arrow , pretty e ] Set _ -> text "Set" Prop _ -> text "Prop" SetN _ n -> text "Set" <> text (showIndex n) Let _ ds e -> sep [ text "let" <+> vcat (map pretty ds) , text "in" <+> pretty e ] Paren _ e -> parens $ pretty e IdiomBrackets _ e -> text "(|" <+> pretty e <+> text "|)" As _ x e -> pretty x <> text "@" <> pretty e Dot _ e -> text "." <> pretty e Absurd _ -> text "()" Rec _ xs -> sep [text "record", bracesAndSemicolons (map pretty xs)] RecUpdate _ e xs -> sep [text "record" <+> pretty e, bracesAndSemicolons (map pretty xs)] ETel [] -> text "()" ETel tel -> fsep $ map pretty tel QuoteGoal _ x e -> sep [text "quoteGoal" <+> pretty x <+> text "in", nest 2 $ pretty e] QuoteContext _ -> text "quoteContext" Quote _ -> text "quote" QuoteTerm _ -> text "quoteTerm" Unquote _ -> text "unquote" Tactic _ t es -> sep [ text "tactic" <+> pretty t , fsep [ text "|" <+> pretty e | e <- es ] ] -- Andreas, 2011-10-03 print irrelevant things as .(e) DontCare e -> text "." <> parens (pretty e) Equal _ a b -> pretty a <+> text "=" <+> pretty b instance (Pretty a, Pretty b) => Pretty (Either a b) where pretty = either pretty pretty instance Pretty a => Pretty (FieldAssignment' a) where pretty (FieldAssignment x e) = sep [ pretty x <+> text "=" , nest 2 $ pretty e ] instance Pretty ModuleAssignment where pretty (ModuleAssignment m es i) = (fsep $ pretty m : map pretty es) <+> pretty i instance Pretty BoundName where pretty BName{ boundName = x, boundLabel = l } | x == l = pretty x | otherwise = pretty l <+> text "=" <+> pretty x instance Pretty LamBinding where -- TODO guilhem: colors are unused (colored syntax disallowed) pretty (DomainFree i x) = prettyRelevance i $ prettyHiding i id $ pretty x pretty (DomainFull b) = pretty b instance Pretty TypedBindings where pretty (TypedBindings _ a) = prettyRelevance a $ prettyHiding a p $ pretty $ unArg a where p | isUnderscore (unArg a) = id | otherwise = parens isUnderscore (TBind _ _ (Underscore _ Nothing)) = True isUnderscore _ = False newtype Tel = Tel Telescope instance Pretty Tel where pretty (Tel tel) | any isMeta tel = text "∀" <+> fsep (map pretty tel) | otherwise = fsep (map pretty tel) where isMeta (TypedBindings _ (Arg _ (TBind _ _ (Underscore _ Nothing)))) = True isMeta _ = False instance Pretty TypedBinding where pretty (TBind _ xs (Underscore _ Nothing)) = fsep (map pretty xs) pretty (TBind _ xs e) = sep [ fsep (map pretty xs) , text ":" <+> pretty e ] pretty (TLet _ ds) = text "let" <+> vcat (map pretty ds) smashTel :: Telescope -> Telescope smashTel (TypedBindings r (Arg i (TBind r' xs e)) : TypedBindings _ (Arg i' (TBind _ ys e')) : tel) | show i == show i' && show e == show e' && all (isUnnamed . dget) (xs ++ ys) = smashTel (TypedBindings r (Arg i (TBind r' (xs ++ ys) e)) : tel) where isUnnamed x = boundLabel x == boundName x smashTel (b : tel) = b : smashTel tel smashTel [] = [] instance Pretty RHS where pretty (RHS e) = text "=" <+> pretty e pretty AbsurdRHS = empty instance Show WhereClause where show = show . pretty instance Pretty WhereClause where pretty NoWhere = empty pretty (AnyWhere [Module _ x [] ds]) | isNoName (unqualify x) = vcat [ text "where", nest 2 (vcat $ map pretty ds) ] pretty (AnyWhere ds) = vcat [ text "where", nest 2 (vcat $ map pretty ds) ] pretty (SomeWhere m a ds) = vcat [ hsep $ applyWhen (a == PrivateAccess UserWritten) (text "private" :) [ text "module", pretty m, text "where" ] , nest 2 (vcat $ map pretty ds) ] instance Show LHS where show = show . pretty instance Pretty LHS where pretty lhs = case lhs of LHS p ps eqs es -> pr (pretty p) ps eqs es Ellipsis _ ps eqs es -> pr (text "...") ps eqs es where pr d ps eqs es = sep [ d , nest 2 $ fsep $ map ((text "|" <+>) . pretty) ps , nest 2 $ pThing "rewrite" eqs , nest 2 $ pThing "with" es ] pThing thing [] = empty pThing thing (e : es) = fsep $ (text thing <+> pretty e) : map ((text "|" <+>) . pretty) es instance Show LHSCore where show = show . pretty instance Pretty LHSCore where pretty (LHSHead f ps) = sep $ pretty f : map (parens . pretty) ps pretty (LHSProj d ps lhscore ps') = sep $ pretty d : map (parens . pretty) ps ++ parens (pretty lhscore) : map (parens . pretty) ps' instance Show ModuleApplication where show = show . pretty instance Pretty ModuleApplication where pretty (SectionApp _ bs e) = fsep (map pretty bs) <+> text "=" <+> pretty e pretty (RecordModuleIFS _ rec) = text "=" <+> pretty rec <+> text "{{...}}" instance Pretty Declaration where prettyList = vcat . map pretty pretty d = case d of TypeSig i x e -> sep [ prettyRelevance i $ pretty x <+> text ":" , nest 2 $ pretty e ] Field inst x (Arg i e) -> sep [ text "field" , nest 2 $ mkInst inst $ mkOverlap i $ prettyRelevance i $ prettyHiding i id $ pretty $ TypeSig (i {argInfoRelevance = Relevant}) x e ] where mkInst InstanceDef d = sep [ text "instance", nest 2 d ] mkInst NotInstanceDef d = d mkOverlap i d | isOverlappable i = text "overlap" <+> d | otherwise = d FunClause lhs rhs wh _ -> sep [ pretty lhs , nest 2 $ pretty rhs ] $$ nest 2 (pretty wh) DataSig _ ind x tel e -> sep [ hsep [ pretty ind , pretty x , fcat (map pretty tel) ] , nest 2 $ hsep [ text ":" , pretty e ] ] Data _ ind x tel (Just e) cs -> sep [ hsep [ pretty ind , pretty x , fcat (map pretty tel) ] , nest 2 $ hsep [ text ":" , pretty e , text "where" ] ] $$ nest 2 (vcat $ map pretty cs) Data _ ind x tel Nothing cs -> sep [ hsep [ pretty ind , pretty x , fcat (map pretty tel) ] , nest 2 $ text "where" ] $$ nest 2 (vcat $ map pretty cs) RecordSig _ x tel e -> sep [ hsep [ text "record" , pretty x , fcat (map pretty tel) ] , nest 2 $ hsep [ text ":" , pretty e ] ] Record _ x ind eta con tel me cs -> sep [ hsep [ text "record" , pretty x , fcat (map pretty tel) ] , nest 2 $ pType me ] $$ nest 2 (vcat $ pInd ++ pEta ++ pCon ++ map pretty cs) where pType (Just e) = hsep [ text ":" , pretty e , text "where" ] pType Nothing = text "where" pInd = maybeToList $ text . show . rangedThing <$> ind pEta = maybeToList $ (\x -> if x then text "eta-equality" else text "no-eta-equality") <$> eta pCon = maybeToList $ (text "constructor" <+>) . pretty <$> fst <$> con Infix f xs -> pretty f <+> (fsep $ punctuate comma $ map pretty xs) Syntax n xs -> text "syntax" <+> pretty n <+> text "..." PatternSyn _ n as p -> text "pattern" <+> pretty n <+> fsep (map pretty as) <+> text "=" <+> pretty p Mutual _ ds -> namedBlock "mutual" ds Abstract _ ds -> namedBlock "abstract" ds Private _ _ ds -> namedBlock "private" ds InstanceB _ ds -> namedBlock "instance" ds Macro _ ds -> namedBlock "macro" ds Postulate _ ds -> namedBlock "postulate" ds Primitive _ ds -> namedBlock "primitive" ds Module _ x tel ds -> hsep [ text "module" , pretty x , fcat (map pretty tel) , text "where" ] $$ nest 2 (vcat $ map pretty ds) ModuleMacro _ x (SectionApp _ [] e) DoOpen i | isNoName x -> sep [ pretty DoOpen , nest 2 $ pretty e , nest 4 $ pretty i ] ModuleMacro _ x (SectionApp _ tel e) open i -> sep [ pretty open <+> text "module" <+> pretty x <+> fcat (map pretty tel) , nest 2 $ text "=" <+> pretty e <+> pretty i ] ModuleMacro _ x (RecordModuleIFS _ rec) open i -> sep [ pretty open <+> text "module" <+> pretty x , nest 2 $ text "=" <+> pretty rec <+> text "{{...}}" ] Open _ x i -> hsep [ text "open", pretty x, pretty i ] Import _ x rn open i -> hsep [ pretty open, text "import", pretty x, as rn, pretty i ] where as Nothing = empty as (Just x) = text "as" <+> pretty (asName x) UnquoteDecl _ xs t -> sep [ text "unquoteDecl" <+> fsep (map pretty xs) <+> text "=", nest 2 $ pretty t ] UnquoteDef _ xs t -> sep [ text "unquoteDef" <+> fsep (map pretty xs) <+> text "=", nest 2 $ pretty t ] Pragma pr -> sep [ text "{-#" <+> pretty pr, text "#-}" ] where namedBlock s ds = sep [ text s , nest 2 $ vcat $ map pretty ds ] instance Pretty OpenShortHand where pretty DoOpen = text "open" pretty DontOpen = empty instance Pretty Pragma where pretty (OptionsPragma _ opts) = fsep $ map text $ "OPTIONS" : opts pretty (BuiltinPragma _ b x) = hsep [ text "BUILTIN", text b, pretty x ] pretty (RewritePragma _ xs) = hsep [ text "REWRITE", hsep $ map pretty xs ] pretty (CompiledPragma _ x hs) = hsep [ text "COMPILED", pretty x, text hs ] pretty (CompiledExportPragma _ x hs) = hsep [ text "COMPILED_EXPORT", pretty x, text hs ] pretty (CompiledTypePragma _ x hs) = hsep [ text "COMPILED_TYPE", pretty x, text hs ] pretty (CompiledDataPragma _ x hs hcs) = hsep $ [text "COMPILED_DATA", pretty x] ++ map text (hs : hcs) pretty (CompiledJSPragma _ x e) = hsep [ text "COMPILED_JS", pretty x, text e ] pretty (CompiledUHCPragma _ x e) = hsep [ text "COMPILED_UHC", pretty x, text e ] pretty (CompiledDataUHCPragma _ x crd crcs) = hsep $ [ text "COMPILED_DATA_UHC", pretty x] ++ map text (crd : crcs) pretty (HaskellCodePragma _ s) = vcat (text "HASKELL" : map text (lines s)) pretty (CompilePragma _ b x e) = hsep [ text "COMPILE", text b, pretty x, text e ] pretty (ForeignPragma _ b s) = vcat $ text ("FOREIGN " ++ b) : map text (lines s) pretty (StaticPragma _ i) = hsep $ [text "STATIC", pretty i] pretty (InjectivePragma _ i) = hsep $ [text "INJECTIVE", pretty i] pretty (InlinePragma _ i) = hsep $ [text "INLINE", pretty i] pretty (ImportPragma _ i) = hsep $ [text "IMPORT", text i] pretty (ImportUHCPragma _ i) = hsep $ [text "IMPORT_UHC", text i] pretty (ImpossiblePragma _) = hsep $ [text "IMPOSSIBLE"] pretty (EtaPragma _ x) = hsep $ [text "ETA", pretty x] pretty (TerminationCheckPragma _ tc) = case tc of TerminationCheck -> __IMPOSSIBLE__ NoTerminationCheck -> text "NO_TERMINATION_CHECK" NonTerminating -> text "NON_TERMINATING" Terminating -> text "TERMINATING" TerminationMeasure _ x -> hsep $ [text "MEASURE", pretty x] pretty (CatchallPragma _) = text "CATCHALL" pretty (DisplayPragma _ lhs rhs) = text "DISPLAY" <+> sep [ pretty lhs <+> text "=", nest 2 $ pretty rhs ] pretty (NoPositivityCheckPragma _) = text "NO_POSITIVITY_CHECK" pretty (PolarityPragma _ q occs) = hsep (text "POLARITY" : pretty q : map pretty occs) instance Pretty Fixity where pretty (Fixity _ Unrelated _) = __IMPOSSIBLE__ pretty (Fixity _ (Related n) ass) = text s <+> text (show n) where s = case ass of LeftAssoc -> "infixl" RightAssoc -> "infixr" NonAssoc -> "infix" instance Pretty GenPart where pretty (IdPart x) = text x pretty BindHole{} = underscore pretty NormalHole{} = underscore pretty WildHole{} = underscore prettyList = hcat . map pretty instance Pretty Fixity' where pretty (Fixity' fix nota _) | nota == noNotation = pretty fix | otherwise = text "syntax" <+> pretty nota -- Andreas 2010-09-21: do not print relevance in general, only in function types! -- Andreas 2010-09-24: and in record fields instance Pretty a => Pretty (Arg a) where prettyPrec p (Arg ai e) = prettyHiding ai id $ prettyPrec p' e where p' | visible ai = p | otherwise = 0 instance Pretty e => Pretty (Named_ e) where prettyPrec p (Named Nothing e) = prettyPrec p e prettyPrec p (Named (Just s) e) = mparens (p > 0) $ sep [ text (rawNameToString $ rangedThing s) <+> text "=", pretty e ] instance Pretty Pattern where prettyList = fsep . map pretty pretty p = case p of IdentP x -> pretty x AppP p1 p2 -> sep [ pretty p1, nest 2 $ pretty p2 ] RawAppP _ ps -> fsep $ map pretty ps OpAppP _ q _ ps -> fsep $ prettyOpApp q (fmap (fmap (fmap (NoPlaceholder Strict.Nothing))) ps) HiddenP _ p -> braces' $ pretty p InstanceP _ p -> dbraces $ pretty p ParenP _ p -> parens $ pretty p WildP _ -> underscore AsP _ x p -> pretty x <> text "@" <> pretty p DotP _ _ p -> text "." <> pretty p AbsurdP _ -> text "()" LitP l -> pretty l QuoteP _ -> text "quote" RecP _ fs -> sep [ text "record", bracesAndSemicolons (map pretty fs) ] prettyOpApp :: forall a . Pretty a => QName -> [NamedArg (MaybePlaceholder a)] -> [Doc] prettyOpApp q es = merge [] $ prOp ms xs es where -- ms: the module part of the name. ms = init (qnameParts q) -- xs: the concrete name (alternation of @Id@ and @Hole@) xs = case unqualify q of Name _ xs -> xs NoName{} -> __IMPOSSIBLE__ prOp :: [Name] -> [NamePart] -> [NamedArg (MaybePlaceholder a)] -> [(Doc, Maybe PositionInName)] prOp ms (Hole : xs) (e : es) = (pretty e, case namedArg e of Placeholder p -> Just p _ -> Nothing) : prOp ms xs es prOp _ (Hole : _) [] = __IMPOSSIBLE__ prOp ms (Id x : xs) es = ( pretty (foldr Qual (QName (Name noRange $ [Id x])) ms) , Nothing ) : prOp [] xs es -- Qualify the name part with the module. -- We then clear @ms@ such that the following name parts will not be qualified. prOp _ [] es = map (\e -> (pretty e, Nothing)) es -- Section underscores should be printed without surrounding -- whitespace. This function takes care of that. merge :: [Doc] -> [(Doc, Maybe PositionInName)] -> [Doc] merge before [] = reverse before merge before ((d, Nothing) : after) = merge (d : before) after merge before ((d, Just Beginning) : after) = mergeRight before d after merge before ((d, Just End) : after) = case mergeLeft d before of (d, bs) -> merge (d : bs) after merge before ((d, Just Middle) : after) = case mergeLeft d before of (d, bs) -> mergeRight bs d after mergeRight before d after = reverse before ++ case merge [] after of [] -> [d] a : as -> (d <> a) : as mergeLeft d before = case before of [] -> (d, []) b : bs -> (b <> d, bs) instance (Pretty a, Pretty b) => Pretty (ImportDirective' a b) where pretty i = sep [ public (publicOpen i) , pretty $ using i , prettyHiding $ hiding i , rename $ impRenaming i ] where public True = text "public" public False = empty prettyHiding [] = empty prettyHiding xs = text "hiding" <+> parens (fsep $ punctuate (text ";") $ map pretty xs) rename [] = empty rename xs = hsep [ text "renaming" , parens $ fsep $ punctuate (text ";") $ map pr xs ] pr r = hsep [ pretty (renFrom r), text "to", pretty (renTo r) ] instance (Pretty a, Pretty b) => Pretty (Using' a b) where pretty UseEverything = empty pretty (Using xs) = text "using" <+> parens (fsep $ punctuate (text ";") $ map pretty xs) instance (Pretty a, Pretty b) => Pretty (ImportedName' a b) where pretty (ImportedName x) = pretty x pretty (ImportedModule x) = text "module" <+> pretty x Agda-2.5.3/src/full/Agda/Syntax/Concrete/Operators.hs0000644000000000000000000007541013154613124020501 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GADTs #-} {-| The parser doesn't know about operators and parses everything as normal function application. This module contains the functions that parses the operators properly. For a stand-alone implementation of this see @src\/prototyping\/mixfix\/old@. It also contains the function that puts parenthesis back given the precedence of the context. -} module Agda.Syntax.Concrete.Operators ( parseApplication , parseModuleApplication , parseLHS , parsePattern , parsePatternSyn ) where import Control.Arrow ((***), (&&&), first, second) import Control.DeepSeq import Control.Applicative import Control.Monad import Data.Either (partitionEithers) import qualified Data.Foldable as Fold import Data.Function import qualified Data.List as List import Data.Maybe import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import Data.Traversable (traverse) import qualified Data.Traversable as Trav import Agda.Syntax.Concrete.Pretty () import Agda.Syntax.Common import Agda.Syntax.Concrete hiding (appView) import Agda.Syntax.Concrete.Operators.Parser import Agda.Syntax.Concrete.Operators.Parser.Monad hiding (parse) import qualified Agda.Syntax.Abstract.Name as A import Agda.Syntax.Position import Agda.Syntax.Fixity import Agda.Syntax.Notation import Agda.Syntax.Scope.Base import Agda.Syntax.Scope.Monad import Agda.TypeChecking.Monad.Base (typeError, TypeError(..), LHSOrPatSyn(..)) import qualified Agda.TypeChecking.Monad.Benchmark as Bench import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.State (getScope) import Agda.TypeChecking.Monad.Options import Agda.Utils.Either import Agda.Utils.Pretty import Agda.Utils.List import Agda.Utils.Trie (Trie) import qualified Agda.Utils.Trie as Trie #include "undefined.h" import Agda.Utils.Impossible --------------------------------------------------------------------------- -- * Billing --------------------------------------------------------------------------- -- | Bills the operator parser. billToParser :: ExprKind -> ScopeM a -> ScopeM a billToParser k = Bench.billTo [ Bench.Parsing , case k of IsExpr -> Bench.OperatorsExpr IsPattern -> Bench.OperatorsPattern ] --------------------------------------------------------------------------- -- * Building the parser --------------------------------------------------------------------------- type FlatScope = Map QName [AbstractName] -- | Compute all defined names in scope and their fixities/notations. -- Note that overloaded names (constructors) can have several -- fixities/notations. Then we 'mergeNotations'. (See issue 1194.) getDefinedNames :: [KindOfName] -> FlatScope -> [[NewNotation]] getDefinedNames kinds names = [ mergeNotations $ map (\d -> namesToNotation x (A.qnameName $ anameName d)) ds | (x, ds) <- Map.toList names , any ((`elem` kinds) . anameKind) ds , not (null ds) -- Andreas, 2013-03-21 see Issue 822 -- Names can have different kinds, i.e., 'defined' and 'constructor'. -- We need to consider all names that have *any* matching kind, -- not only those whose first appearing kind is matching. ] -- | Compute all names (first component) and operators/notations -- (second component) in scope. localNames :: FlatScope -> ScopeM ([QName], [NewNotation]) localNames flat = do let defs = getDefinedNames allKindsOfNames flat locals <- nubOn fst . notShadowedLocals <$> getLocalVars -- Note: Debug printout aligned with the one in buildParsers. reportSLn "scope.operators" 50 $ unlines [ "flat = " ++ show flat , "defs = " ++ show defs , "locals= " ++ show locals ] let localNots = map localOp locals localNames = Set.fromList $ map notaName localNots otherNots = filter (\n -> not (Set.member (notaName n) localNames)) (concat defs) return $ second (map useDefaultFixity) $ split $ localNots ++ otherNots where localOp (x, y) = namesToNotation (QName x) y split ops = partitionEithers $ concatMap opOrNot ops opOrNot n = Left (notaName n) : if null (notation n) then [] else [Right n] -- | Data structure filled in by @buildParsers@. -- The top-level parser @pTop@ is of primary interest, -- but @pArgs@ is used to convert module application -- from concrete to abstract syntax. data Parsers e = Parsers { pTop :: Parser e e , pApp :: Parser e e , pArgs :: Parser e [NamedArg e] , pNonfix :: Parser e e , pAtom :: Parser e e } data ExprKind = IsPattern | IsExpr deriving (Eq, Show) -- | Builds a parser for operator applications from all the operators -- and function symbols in scope. -- -- When parsing a pattern we do not use bound names. The effect is -- that unqualified operator parts (that are not constructor parts) -- can be used as atomic names in the pattern (so they can be -- rebound). See @test/succeed/OpBind.agda@ for an example. -- -- When parsing a pattern we also disallow the use of sections, mainly -- because there is little need for sections in patterns. Note that -- sections are parsed by splitting up names into multiple tokens -- (@_+_@ is replaced by @_@, @+@ and @_@), and if we were to support -- sections in patterns, then we would have to accept certain such -- sequences of tokens as single pattern variables. -- -- The list of names must include every name part in the -- expression/pattern to be parsed (excluding name parts inside things -- like parenthesised subexpressions that are treated as atoms). The -- list is used to optimise the parser. For instance, a given notation -- is only included in the generated grammar if all of the notation's -- name parts are present in the list of names. -- -- The returned list contains all operators/notations/sections that -- were used to generate the grammar. buildParsers :: forall e. IsExpr e => Range -> FlatScope -> ExprKind -> [QName] -> ScopeM (ParseSections, [NotationSection], Parsers e) buildParsers r flat kind exprNames = do (names, ops) <- localNames flat let -- All names. namesInExpr :: Set QName namesInExpr = Set.fromList exprNames partListsInExpr' = map (nameParts . unqualify) $ Set.toList namesInExpr partListTrie f = foldr (\ps -> Trie.union (Trie.everyPrefix ps ())) Trie.empty (f partListsInExpr') -- All names. partListsInExpr :: Trie NamePart () partListsInExpr = partListTrie id -- All names, with the name parts in reverse order. reversedPartListsInExpr :: Trie NamePart () reversedPartListsInExpr = partListTrie (map reverse) -- Every regular name part (not holes etc.). partsInExpr :: Set RawName partsInExpr = Set.fromList [ s | Id s <- concat partListsInExpr' ] -- Are all name parts present in the expression? partsPresent n = [ Set.member p partsInExpr | p <- stringParts (notation n) ] addHole True p = [Hole, Id p] addHole False p = [Id p] -- Is the first identifier part present in n present in the -- expression, without any preceding name parts, except for a -- leading underscore iff withHole is True? firstPartPresent withHole n = Trie.member (addHole withHole p) partListsInExpr where p = case n of NormalHole{} : IdPart p : _ -> p IdPart p : _ -> p _ -> __IMPOSSIBLE__ -- Is the last identifier part present in n present in the -- expression, without any succeeding name parts, except for a -- trailing underscore iff withHole is True? lastPartPresent withHole n = Trie.member (addHole withHole p) reversedPartListsInExpr where p = case reverse n of NormalHole{} : IdPart p : _ -> p IdPart p : _ -> p _ -> __IMPOSSIBLE__ -- | Are the initial and final identifier parts present with -- the right mix of leading and trailing underscores? correctUnderscores :: Bool -> Bool -> Notation -> Bool correctUnderscores withInitialHole withFinalHole n = firstPartPresent withInitialHole n && lastPartPresent withFinalHole n -- Should be used with operators (not sections) and notations -- coming from syntax declarations. filterCorrectUnderscoresOp :: [NewNotation] -> [NotationSection] filterCorrectUnderscoresOp ns = [ noSection n | n <- ns , if notaIsOperator n then correctUnderscores False False (notation n) else all (\s -> Trie.member [Id s] partListsInExpr) (stringParts $ notation n) ] -- Should be used with sections. correctUnderscoresSect :: NotationKind -> Notation -> Bool correctUnderscoresSect k n = case (k, notationKind n) of (PrefixNotation, InfixNotation) -> correctUnderscores True False n (PostfixNotation, InfixNotation) -> correctUnderscores False True n (NonfixNotation, InfixNotation) -> correctUnderscores True True n (NonfixNotation, PrefixNotation) -> correctUnderscores False True n (NonfixNotation, PostfixNotation) -> correctUnderscores True False n _ -> __IMPOSSIBLE__ -- If "or" is replaced by "and" in conParts/allParts below, -- then the misspelled operator application "if x thenn x else -- x" can be parsed as "if" applied to five arguments, -- resulting in a confusing error message claiming that "if" -- is not in scope. (non, fix) = List.partition nonfix (filter (and . partsPresent) ops) cons = getDefinedNames [ConName, PatternSynName] flat conNames = Set.fromList $ filter (flip Set.member namesInExpr) $ map (notaName . head) cons conParts = Set.fromList $ concatMap notationNames $ filter (or . partsPresent) $ concat cons allNames = Set.fromList $ filter (flip Set.member namesInExpr) names allParts = Set.union conParts (Set.fromList $ concatMap notationNames $ filter (or . partsPresent) ops) isAtom x | kind == IsPattern && not (isQualified x) = not (Set.member x conParts) || Set.member x conNames | otherwise = not (Set.member x allParts) || Set.member x allNames -- If string is a part of notation, it cannot be used as an identifier, -- unless it is also used as an identifier. See issue 307. parseSections = case kind of IsPattern -> DoNotParseSections IsExpr -> ParseSections let nonClosedSections l ns = case parseSections of DoNotParseSections -> [] ParseSections -> [ NotationSection n k (Just l) True | n <- ns , isinfix n && notaIsOperator n , k <- [PrefixNotation, PostfixNotation] , correctUnderscoresSect k (notation n) ] unrelatedOperators :: [NotationSection] unrelatedOperators = filterCorrectUnderscoresOp unrelated ++ nonClosedSections Unrelated unrelated where unrelated = filter ((== Unrelated) . level) fix nonWithSections :: [NotationSection] nonWithSections = map (\s -> s { sectLevel = Nothing }) (filterCorrectUnderscoresOp non) ++ case parseSections of DoNotParseSections -> [] ParseSections -> [ NotationSection n NonfixNotation Nothing True | n <- fix , notaIsOperator n , correctUnderscoresSect NonfixNotation (notation n) ] -- The triples have the form (level, operators). The lowest -- level comes first. relatedOperators :: [(Integer, [NotationSection])] relatedOperators = map (\((l, ns) : rest) -> (l, ns ++ concat (map snd rest))) . List.groupBy ((==) `on` fst) . List.sortBy (compare `on` fst) . mapMaybe (\n -> case level n of Unrelated -> Nothing r@(Related l) -> Just (l, filterCorrectUnderscoresOp [n] ++ nonClosedSections r [n])) $ fix everything :: [NotationSection] everything = concatMap snd relatedOperators ++ unrelatedOperators ++ nonWithSections reportSLn "scope.operators" 50 $ unlines [ "unrelatedOperators = " ++ show unrelatedOperators , "nonWithSections = " ++ show nonWithSections , "relatedOperators = " ++ show relatedOperators ] let g = Data.Function.fix $ \p -> Parsers { pTop = memoise TopK $ Fold.asum $ foldr ($) (pApp p) (map (\(l, ns) higher -> mkP (Right l) parseSections (pTop p) ns higher True) relatedOperators) : map (\(k, n) -> mkP (Left k) parseSections (pTop p) [n] (pApp p) False) (zip [0..] unrelatedOperators) , pApp = memoise AppK $ appP (pNonfix p) (pArgs p) , pArgs = argsP (pNonfix p) , pNonfix = memoise NonfixK $ Fold.asum $ pAtom p : flip map nonWithSections (\sect -> let n = sectNotation sect inner :: forall k. NK k -> Parser e (OperatorType k e) inner = opP parseSections (pTop p) n in case notationKind (notation n) of InfixNotation -> flip ($) <$> placeholder Beginning <*> inner In <*> placeholder End PrefixNotation -> inner Pre <*> placeholder End PostfixNotation -> flip ($) <$> placeholder Beginning <*> inner Post NonfixNotation -> inner Non NoNotation -> __IMPOSSIBLE__) , pAtom = atomP isAtom } reportSDoc "scope.grammar" 10 $ return $ text "Operator grammar:" $$ nest 2 (grammar (pTop g)) return (parseSections, everything, g) where level :: NewNotation -> PrecedenceLevel level = fixityLevel . notaFixity nonfix, isinfix, isprefix, ispostfix :: NewNotation -> Bool nonfix = (== NonfixNotation) . notationKind . notation isinfix = (== InfixNotation) . notationKind . notation isprefix = (== PrefixNotation) . notationKind . notation ispostfix = (== PostfixNotation) . notationKind . notation isPrefix, isPostfix :: NotationSection -> Bool isPrefix = (== PrefixNotation) . sectKind isPostfix = (== PostfixNotation) . sectKind isInfix :: Associativity -> NotationSection -> Bool isInfix ass s = sectKind s == InfixNotation && fixityAssoc (notaFixity (sectNotation s)) == ass mkP :: Either Integer Integer -- ^ Memoisation key. -> ParseSections -> Parser e e -> [NotationSection] -> Parser e e -- ^ A parser for an expression of higher precedence. -> Bool -- ^ Include the \"expression of higher precedence\" -- parser as one of the choices? -> Parser e e mkP key parseSections p0 ops higher includeHigher = memoise (NodeK key) $ Fold.asum $ (if includeHigher then (higher :) else id) $ catMaybes [nonAssoc, preRights, postLefts] where choice :: forall k. NK k -> [NotationSection] -> Parser e (OperatorType k e) choice k = Fold.asum . map (\sect -> let n = sectNotation sect inner :: forall k. NK k -> Parser e (OperatorType k e) inner = opP parseSections p0 n in case k of In -> inner In Pre -> if isinfix n || ispostfix n then flip ($) <$> placeholder Beginning <*> inner In else inner Pre Post -> if isinfix n || isprefix n then flip <$> inner In <*> placeholder End else inner Post Non -> __IMPOSSIBLE__) nonAssoc :: Maybe (Parser e e) nonAssoc = case filter (isInfix NonAssoc) ops of [] -> Nothing ops -> Just $ (\x f y -> f (noPlaceholder x) (noPlaceholder y)) <$> higher <*> choice In ops <*> higher or p1 [] p2 [] = Nothing or p1 [] p2 ops2 = Just (p2 ops2) or p1 ops1 p2 [] = Just (p1 ops1) or p1 ops1 p2 ops2 = Just (p1 ops1 <|> p2 ops2) preRight :: Maybe (Parser e (MaybePlaceholder e -> e)) preRight = or (choice Pre) (filter isPrefix ops) (\ops -> flip ($) <$> (noPlaceholder <$> higher) <*> choice In ops) (filter (isInfix RightAssoc) ops) preRights :: Maybe (Parser e e) preRights = do preRight <- preRight return $ Data.Function.fix $ \preRights -> memoiseIfPrinting (PreRightsK key) $ preRight <*> (noPlaceholder <$> (preRights <|> higher)) postLeft :: Maybe (Parser e (MaybePlaceholder e -> e)) postLeft = or (choice Post) (filter isPostfix ops) (\ops -> flip <$> choice In ops <*> (noPlaceholder <$> higher)) (filter (isInfix LeftAssoc) ops) postLefts :: Maybe (Parser e e) postLefts = do postLeft <- postLeft return $ Data.Function.fix $ \postLefts -> memoise (PostLeftsK key) $ flip ($) <$> (noPlaceholder <$> (postLefts <|> higher)) <*> postLeft --------------------------------------------------------------------------- -- * Helpers for pattern and lhs parsing --------------------------------------------------------------------------- -- | View a pattern @p@ as a list @p0 .. pn@ where @p0@ is the identifier -- (in most cases a constructor). -- -- Pattern needs to be parsed already (operators resolved). patternAppView :: Pattern -> [NamedArg Pattern] patternAppView p = case p of AppP p arg -> patternAppView p ++ [arg] OpAppP _ x _ ps -> defaultNamedArg (IdentP x) : ps ParenP _ p -> patternAppView p RawAppP _ _ -> __IMPOSSIBLE__ _ -> [ defaultNamedArg p ] --------------------------------------------------------------------------- -- * Parse functions --------------------------------------------------------------------------- -- | Returns the list of possible parses. parsePat :: (ParseSections, Parser Pattern Pattern) -> Pattern -> [Pattern] parsePat prs p = case p of AppP p (Arg info q) -> fullParen' <$> (AppP <$> parsePat prs p <*> (Arg info <$> traverse (parsePat prs) q)) RawAppP _ ps -> fullParen' <$> (parsePat prs =<< parse prs ps) OpAppP r d ns ps -> fullParen' . OpAppP r d ns <$> (mapM . traverse . traverse) (parsePat prs) ps HiddenP _ _ -> fail "bad hidden argument" InstanceP _ _ -> fail "bad instance argument" AsP r x p -> AsP r x <$> parsePat prs p DotP r o e -> return $ DotP r o e ParenP r p -> fullParen' <$> parsePat prs p WildP _ -> return p AbsurdP _ -> return p LitP _ -> return p QuoteP _ -> return p IdentP _ -> return p RecP r fs -> RecP r <$> mapM (traverse (parsePat prs)) fs {- Implement parsing of copattern left hand sides, e.g. record Tree (A : Set) : Set where field label : A child : Bool -> Tree A -- corecursive function defined by copattern matching alternate : {A : Set}(a b : A) -> Tree A -- shallow copatterns label (alternate a b) = a child (alternate a b) True = alternate b a -- deep copatterns: label (child (alternate a b) False) = b child (child (alternate a b) False) True = alternate a b child (child (alternate a b) False) False = alternate a b Delivers an infinite tree a b b a a a a b b b b b b b b ... Each lhs is a pattern tree with a distinct path of destructors ("child", "label") from the root to the defined symbol ("alternate"). All branches besides this distinct path are patterns. Syntax.Concrete.LHSCore represents a lhs - the destructor path - the side patterns - the defined function symbol - the applied patterns -} type ParseLHS = Either Pattern (QName, LHSCore) -- | The returned list contains all operators/notations/sections that -- were used to generate the grammar. parseLHS' :: LHSOrPatSyn -> Maybe QName -> Pattern -> ScopeM (ParseLHS, [NotationSection]) parseLHS' IsLHS (Just qn) (RawAppP _ [WildP{}]) = return (Right (qn, LHSHead qn []), []) parseLHS' lhsOrPatSyn top p = do let names = patternQNames p ms = qualifierModules names flat <- flattenScope ms <$> getScope (parseSections, ops, parsers) <- buildParsers (getRange p) flat IsPattern names let patP = (parseSections, pTop parsers) let cons = getNames [ConName, PatternSynName] flat let flds = getNames [FldName] flat case [ res | let result = parsePat patP p , p' <- foldr seq () result `seq` result , res <- validPattern (PatternCheckConfig top cons flds) p' ] of [(p,lhs)] -> do reportSDoc "scope.operators" 50 $ return $ text "Parsed lhs:" <+> pretty lhs return (lhs, ops) [] -> typeError $ OperatorInformation ops $ NoParseForLHS lhsOrPatSyn p rs -> typeError $ OperatorInformation ops $ AmbiguousParseForLHS lhsOrPatSyn p $ map (fullParen . fst) rs where getNames kinds flat = map (notaName . head) $ getDefinedNames kinds flat -- validPattern returns an empty or singleton list (morally a Maybe) validPattern :: PatternCheckConfig -> Pattern -> [(Pattern, ParseLHS)] validPattern conf p = case (classifyPattern conf p, top) of (Just r@(Left _), Nothing) -> [(p, r)] -- expect pattern (Just r@(Right _), Just{}) -> [(p, r)] -- expect lhs _ -> [] -- | Name sets for classifying a pattern. data PatternCheckConfig = PatternCheckConfig { topName :: Maybe QName -- ^ name of defined symbol , conNames :: [QName] -- ^ valid constructor names , fldNames :: [QName] -- ^ valid field names } -- | Returns zero or one classified patterns. classifyPattern :: PatternCheckConfig -> Pattern -> Maybe ParseLHS classifyPattern conf p = case patternAppView p of -- case @f ps@ Arg _ (Named _ (IdentP x)) : ps | Just x == topName conf -> do guard $ all validPat ps return $ Right (x, LHSHead x ps) -- case @d ps@ Arg _ (Named _ (IdentP x)) : ps | x `elem` fldNames conf -> do -- ps0 :: [NamedArg ParseLHS] ps0 <- mapM classPat ps let (ps1, rest) = span (isLeft . namedArg) ps0 (p2, ps3) <- uncons rest -- when (null rest): no field pattern or def pattern found guard $ all (isLeft . namedArg) ps3 let (f, lhs) = fromR p2 (ps', _:ps'') = splitAt (length ps1) ps return $ Right (f, LHSProj x ps' lhs ps'') -- case: ordinary pattern _ -> do guard $ validConPattern (conNames conf) p return $ Left p where -- allNames = conNames conf ++ fldNames conf validPat = validConPattern (conNames conf) . namedArg classPat :: NamedArg Pattern -> Maybe (NamedArg ParseLHS) classPat = Trav.mapM (Trav.mapM (classifyPattern conf)) fromR :: NamedArg (Either a (b, c)) -> (b, NamedArg c) fromR (Arg info (Named n (Right (b, c)))) = (b, Arg info (Named n c)) fromR (Arg info (Named n (Left a ))) = __IMPOSSIBLE__ -- | Parses a left-hand side, and makes sure that it defined the expected name. parseLHS :: QName -> Pattern -> ScopeM LHSCore parseLHS top p = billToParser IsPattern $ do (res, ops) <- parseLHS' IsLHS (Just top) p case res of Right (f, lhs) -> return lhs _ -> typeError $ OperatorInformation ops $ NoParseForLHS IsLHS p -- | Parses a pattern. parsePattern :: Pattern -> ScopeM Pattern parsePattern = parsePatternOrSyn IsLHS parsePatternSyn :: Pattern -> ScopeM Pattern parsePatternSyn = parsePatternOrSyn IsPatSyn parsePatternOrSyn :: LHSOrPatSyn -> Pattern -> ScopeM Pattern parsePatternOrSyn lhsOrPatSyn p = billToParser IsPattern $ do (res, ops) <- parseLHS' lhsOrPatSyn Nothing p case res of Left p -> return p _ -> typeError $ OperatorInformation ops $ NoParseForLHS lhsOrPatSyn p -- | Helper function for 'parseLHS' and 'parsePattern'. validConPattern :: [QName] -> Pattern -> Bool validConPattern cons p = case appView p of [_] -> True IdentP x : ps -> elem x cons && all (validConPattern cons) ps [QuoteP _, _] -> True _ -> False -- Andreas, 2012-06-04: I do not know why the following line was -- the catch-all case. It seems that the new catch-all works also -- and is more logical. -- ps -> all (validConPattern cons) ps -- | Helper function for 'parseLHS' and 'parsePattern'. appView :: Pattern -> [Pattern] appView p = case p of AppP p a -> appView p ++ [namedArg a] OpAppP _ op _ ps -> IdentP op : map namedArg ps ParenP _ p -> appView p RawAppP _ _ -> __IMPOSSIBLE__ HiddenP _ _ -> __IMPOSSIBLE__ InstanceP _ _ -> __IMPOSSIBLE__ _ -> [p] -- | Return all qualifiers occuring in a list of 'QName's. -- Each qualifier is returned as a list of names, e.g. -- for @Data.Nat._+_@ we return the list @[Data,Nat]@. qualifierModules :: [QName] -> [[Name]] qualifierModules qs = List.nub $ filter (not . null) $ map (init . qnameParts) qs -- | Parse a list of expressions into an application. parseApplication :: [Expr] -> ScopeM Expr parseApplication [e] = return e parseApplication es = billToParser IsExpr $ do -- Build the parser let names = [ q | Ident q <- es ] ms = qualifierModules names flat <- flattenScope ms <$> getScope (parseSections, ops, p) <- buildParsers (getRange es) flat IsExpr names -- Parse let result = parse (parseSections, pTop p) es case foldr seq () result `seq` result of [e] -> do reportSDoc "scope.operators" 50 $ return $ text "Parsed an operator application:" <+> pretty e return e [] -> typeError $ OperatorInformation ops $ NoParseForApplication es es' -> typeError $ OperatorInformation ops $ AmbiguousParseForApplication es $ map fullParen es' parseModuleIdentifier :: Expr -> ScopeM QName parseModuleIdentifier (Ident m) = return m parseModuleIdentifier e = typeError $ NotAModuleExpr e parseRawModuleApplication :: [Expr] -> ScopeM (QName, [NamedArg Expr]) parseRawModuleApplication es = billToParser IsExpr $ do let e : es_args = es m <- parseModuleIdentifier e -- Build the arguments parser let names = [ q | Ident q <- es_args ] ms = qualifierModules names flat <- flattenScope ms <$> getScope (parseSections, ops, p) <- buildParsers (getRange es_args) flat IsExpr names -- Parse -- TODO: not sure about forcing case {-force $-} parse (parseSections, pArgs p) es_args of [as] -> return (m, as) [] -> typeError $ OperatorInformation ops $ NoParseForApplication es ass -> do let f = fullParen . foldl (App noRange) (Ident m) typeError $ OperatorInformation ops $ AmbiguousParseForApplication es $ map f ass -- | Parse an expression into a module application -- (an identifier plus a list of arguments). parseModuleApplication :: Expr -> ScopeM (QName, [NamedArg Expr]) parseModuleApplication (RawApp _ es) = parseRawModuleApplication es parseModuleApplication (App r e1 e2) = do -- TODO: do we need this case? (m, args) <- parseModuleApplication e1 return (m, args ++ [e2]) parseModuleApplication e = do m <- parseModuleIdentifier e return (m, []) --------------------------------------------------------------------------- -- * Inserting parenthesis --------------------------------------------------------------------------- fullParen :: IsExpr e => e -> e fullParen e = case exprView $ fullParen' e of ParenV e -> e e' -> unExprView e' fullParen' :: IsExpr e => e -> e fullParen' e = case exprView e of LocalV _ -> e WildV _ -> e OtherV _ -> e HiddenArgV _ -> e InstanceArgV _ -> e ParenV _ -> e AppV e1 (Arg info e2) -> par $ unExprView $ AppV (fullParen' e1) (Arg info e2') where e2' = case argInfoHiding info of Hidden -> e2 Instance{} -> e2 NotHidden -> fullParen' <$> e2 OpAppV x ns es -> par $ unExprView $ OpAppV x ns $ (map . fmap . fmap . fmap . fmap) fullParen' es LamV bs e -> par $ unExprView $ LamV bs (fullParen e) where par = unExprView . ParenV Agda-2.5.3/src/full/Agda/Syntax/Concrete/Definitions.hs0000644000000000000000000022242513154613124020776 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GADTs #-} -- | Preprocess 'Agda.Syntax.Concrete.Declaration's, producing 'NiceDeclaration's. -- -- * Attach fixity and syntax declarations to the definition they refer to. -- -- * Distribute the following attributes to the individual definitions: -- @abstract@, -- @instance@, -- @postulate@, -- @primitive@, -- @private@, -- termination pragmas. -- -- * Gather the function clauses belonging to one function definition. -- -- * Expand ellipsis @...@ in function clauses following @with@. -- -- * Infer mutual blocks. -- A block starts when a lone signature is encountered, and ends when -- all lone signatures have seen their definition. -- -- * Report basic well-formedness error, -- when one of the above transformation fails. module Agda.Syntax.Concrete.Definitions ( NiceDeclaration(..) , NiceConstructor, NiceTypeSignature , Clause(..) , DeclarationException(..) , DeclarationWarning(..) , Nice, runNice , niceDeclarations , notSoNiceDeclarations , niceHasAbstract , Measure ) where import Prelude hiding (null) import Control.Arrow ((***), first, second) import Control.Applicative hiding (empty) import Control.Monad.State #if __GLASGOW_HASKELL__ <= 708 import Data.Foldable ( foldMap ) #endif import qualified Data.Map as Map import Data.Map (Map) import Data.Maybe import Data.Semigroup ( Semigroup, Monoid, (<>), mempty, mappend ) import qualified Data.List as List import qualified Data.Set as Set import Data.Traversable (Traversable, traverse) import qualified Data.Traversable as Trav import Data.Data (Data) import Data.Typeable (Typeable) import Agda.Syntax.Concrete import Agda.Syntax.Common hiding (TerminationCheck()) import qualified Agda.Syntax.Common as Common import Agda.Syntax.Position import Agda.Syntax.Fixity import Agda.Syntax.Notation import Agda.Syntax.Concrete.Pretty () import Agda.TypeChecking.Positivity.Occurrence import Agda.Utils.Except ( MonadError(throwError,catchError) ) import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List (caseList, headMaybe, isSublistOf) import Agda.Utils.Maybe import Agda.Utils.Monad import Agda.Utils.Null import qualified Agda.Utils.Pretty as Pretty import Agda.Utils.Pretty hiding ((<>)) import Agda.Utils.Singleton import Agda.Utils.Tuple import Agda.Utils.Update #include "undefined.h" import Agda.Utils.Impossible {-------------------------------------------------------------------------- Types --------------------------------------------------------------------------} {-| The nice declarations. No fixity declarations and function definitions are contained in a single constructor instead of spread out between type signatures and clauses. The @private@, @postulate@, @abstract@ and @instance@ modifiers have been distributed to the individual declarations. Observe the order of components: Range Fixity' Access IsAbstract IsInstance TerminationCheck PositivityCheck further attributes (Q)Name content (Expr, Declaration ...) -} data NiceDeclaration = Axiom Range Fixity' Access IsAbstract IsInstance ArgInfo (Maybe [Occurrence]) Name Expr -- ^ 'IsAbstract' argument: We record whether a declaration was made in an @abstract@ block. -- -- 'ArgInfo' argument: Axioms and functions can be declared irrelevant. -- ('Hiding' should be 'NotHidden'.) -- -- @Maybe [Occurrence]@ argument: Polarities can be assigned to identifiers. | NiceField Range Fixity' Access IsAbstract IsInstance Name (Arg Expr) | PrimitiveFunction Range Fixity' Access IsAbstract Name Expr | NiceMutual Range TerminationCheck PositivityCheck [NiceDeclaration] | NiceModule Range Access IsAbstract QName Telescope [Declaration] | NiceModuleMacro Range Access Name ModuleApplication OpenShortHand ImportDirective | NiceOpen Range QName ImportDirective | NiceImport Range QName (Maybe AsName) OpenShortHand ImportDirective | NicePragma Range Pragma | NiceRecSig Range Fixity' Access IsAbstract PositivityCheck Name [LamBinding] Expr | NiceDataSig Range Fixity' Access IsAbstract PositivityCheck Name [LamBinding] Expr | NiceFunClause Range Access IsAbstract TerminationCheck Catchall Declaration -- ^ An uncategorized function clause, could be a function clause -- without type signature or a pattern lhs (e.g. for irrefutable let). -- The 'Declaration' is the actual 'FunClause'. | FunSig Range Fixity' Access IsAbstract IsInstance IsMacro ArgInfo TerminationCheck Name Expr | FunDef Range [Declaration] Fixity' IsAbstract IsInstance TerminationCheck Name [Clause] -- ^ Block of function clauses (we have seen the type signature before). -- The 'Declaration's are the original declarations that were processed -- into this 'FunDef' and are only used in 'notSoNiceDeclaration'. -- Andreas, 2017-01-01: Because of issue #2372, we add 'IsInstance' here. -- An alias should know that it is an instance. | DataDef Range Fixity' IsAbstract PositivityCheck Name [LamBinding] [NiceConstructor] | RecDef Range Fixity' IsAbstract PositivityCheck Name (Maybe (Ranged Induction)) (Maybe Bool) (Maybe (ThingWithFixity Name, IsInstance)) [LamBinding] [NiceDeclaration] | NicePatternSyn Range Fixity' Name [Arg Name] Pattern | NiceUnquoteDecl Range [Fixity'] Access IsAbstract IsInstance TerminationCheck [Name] Expr | NiceUnquoteDef Range [Fixity'] Access IsAbstract TerminationCheck [Name] Expr deriving (Typeable, Data, Show) type TerminationCheck = Common.TerminationCheck Measure -- | Termination measure is, for now, a variable name. type Measure = Name type Catchall = Bool -- | Only 'Axiom's. type NiceConstructor = NiceTypeSignature -- | Only 'Axiom's. type NiceTypeSignature = NiceDeclaration -- | One clause in a function definition. There is no guarantee that the 'LHS' -- actually declares the 'Name'. We will have to check that later. data Clause = Clause Name Catchall LHS RHS WhereClause [Clause] deriving (Typeable, Data, Show) -- | The exception type. data DeclarationException = MultipleFixityDecls [(Name, [Fixity'])] | MultiplePolarityPragmas [Name] | InvalidName Name | DuplicateDefinition Name | MissingDefinition Name | MissingWithClauses Name | MissingTypeSignature LHS -- Andreas 2012-06-02: currently unused, remove after a while -- Fredrik 2012-09-20: now used, can we keep it? | MissingDataSignature Name | WrongDefinition Name DataRecOrFun DataRecOrFun | WrongParameters Name Params Params -- ^ 'Name' of symbol, 'Params' of signature, 'Params' of definition. | NotAllowedInMutual NiceDeclaration | Codata Range | DeclarationPanic String | WrongContentBlock KindOfBlock Range | AmbiguousFunClauses LHS [Name] -- ^ in a mutual block, a clause could belong to any of the @[Name]@ type signatures | InvalidTerminationCheckPragma Range | InvalidMeasureMutual Range -- ^ In a mutual block, all or none need a MEASURE pragma. -- Range is of mutual block. | PragmaNoTerminationCheck Range -- ^ Pragma @{-# NO_TERMINATION_CHECK #-}@ has been replaced -- by {-# TERMINATING #-} and {-# NON_TERMINATING #-}. | InvalidCatchallPragma Range | UnquoteDefRequiresSignature [Name] | BadMacroDef NiceDeclaration | InvalidNoPositivityCheckPragma Range deriving (Typeable, Data, Show) -- | Non-fatal errors encountered in the Nicifier data DeclarationWarning = UnknownNamesInFixityDecl [Name] | UnknownNamesInPolarityPragmas [Name] | PolarityPragmasButNotPostulates [Name] | UselessPrivate Range | UselessAbstract Range | UselessInstance Range deriving (Typeable, Data, Show) -- | Several declarations expect only type signatures as sub-declarations. These are: data KindOfBlock = PostulateBlock -- ^ @postulate@ | PrimitiveBlock -- ^ @primitive@. Ensured by parser. | InstanceBlock -- ^ @instance@. Actually, here all kinds of sub-declarations are allowed a priori. | FieldBlock -- ^ @field@. Ensured by parser. | DataBlock -- ^ @data ... where@. Here we got a bad error message for Agda-2.5 (Issue 1698). deriving (Typeable, Data, Eq, Ord, Show) instance HasRange DeclarationException where getRange (MultipleFixityDecls xs) = getRange (fst $ head xs) getRange (MultiplePolarityPragmas xs) = getRange (head xs) getRange (InvalidName x) = getRange x getRange (DuplicateDefinition x) = getRange x getRange (MissingDefinition x) = getRange x getRange (MissingWithClauses x) = getRange x getRange (MissingTypeSignature x) = getRange x getRange (MissingDataSignature x) = getRange x getRange (WrongDefinition x k k') = getRange x getRange (WrongParameters x _ _) = getRange x getRange (AmbiguousFunClauses lhs xs) = getRange lhs getRange (NotAllowedInMutual x) = getRange x getRange (Codata r) = r getRange (DeclarationPanic _) = noRange getRange (WrongContentBlock _ r) = r getRange (InvalidTerminationCheckPragma r) = r getRange (InvalidMeasureMutual r) = r getRange (PragmaNoTerminationCheck r) = r getRange (InvalidCatchallPragma r) = r getRange (UnquoteDefRequiresSignature x) = getRange x getRange (BadMacroDef d) = getRange d getRange (InvalidNoPositivityCheckPragma r) = r instance HasRange DeclarationWarning where getRange (UnknownNamesInFixityDecl xs) = getRange . head $ xs getRange (UnknownNamesInPolarityPragmas xs) = getRange . head $ xs getRange (PolarityPragmasButNotPostulates xs) = getRange . head $ xs getRange (UselessPrivate r) = r getRange (UselessAbstract r) = r getRange (UselessInstance r) = r instance HasRange NiceDeclaration where getRange (Axiom r _ _ _ _ _ _ _ _) = r getRange (NiceField r _ _ _ _ _ _) = r getRange (NiceMutual r _ _ _) = r getRange (NiceModule r _ _ _ _ _ ) = r getRange (NiceModuleMacro r _ _ _ _ _) = r getRange (NiceOpen r _ _) = r getRange (NiceImport r _ _ _ _) = r getRange (NicePragma r _) = r getRange (PrimitiveFunction r _ _ _ _ _) = r getRange (FunSig r _ _ _ _ _ _ _ _ _) = r getRange (FunDef r _ _ _ _ _ _ _) = r getRange (DataDef r _ _ _ _ _ _) = r getRange (RecDef r _ _ _ _ _ _ _ _ _) = r getRange (NiceRecSig r _ _ _ _ _ _ _) = r getRange (NiceDataSig r _ _ _ _ _ _ _) = r getRange (NicePatternSyn r _ _ _ _) = r getRange (NiceFunClause r _ _ _ _ _) = r getRange (NiceUnquoteDecl r _ _ _ _ _ _ _) = r getRange (NiceUnquoteDef r _ _ _ _ _ _) = r -- These error messages can (should) be terminated by a dot ".", -- there is no error context printed after them. instance Pretty DeclarationException where pretty (MultipleFixityDecls xs) = sep [ fsep $ pwords "Multiple fixity or syntax declarations for" , vcat $ map f xs ] where f (x, fs) = pretty x Pretty.<> text ": " <+> fsep (map pretty fs) pretty (MultiplePolarityPragmas xs) = fsep $ pwords "Multiple polarity pragmas for" ++ map pretty xs pretty (InvalidName x) = fsep $ pwords "Invalid name:" ++ [pretty x] pretty (DuplicateDefinition x) = fsep $ pwords "Duplicate definition of" ++ [pretty x] pretty (MissingDefinition x) = fsep $ pwords "Missing definition for" ++ [pretty x] pretty (MissingWithClauses x) = fsep $ pwords "Missing with-clauses for function" ++ [pretty x] pretty (MissingTypeSignature x) = fsep $ pwords "Missing type signature for left hand side" ++ [pretty x] pretty (MissingDataSignature x) = fsep $ pwords "Missing type signature for " ++ [pretty x] pretty (WrongDefinition x k k') = fsep $ pretty x : pwords ("has been declared as a " ++ show k ++ ", but is being defined as a " ++ show k') pretty (WrongParameters x sig def) = fsep $ pwords "List of parameters " ++ map pretty def ++ pwords " does not match parameters " ++ map pretty sig ++ pwords " of previous signature for " ++ [pretty x] pretty (AmbiguousFunClauses lhs xs) = sep [ fsep $ pwords "More than one matching type signature for left hand side " ++ [pretty lhs] ++ pwords "it could belong to any of:" , vcat $ map (pretty . PrintRange) xs ] pretty (WrongContentBlock b _) = fsep . pwords $ case b of PostulateBlock -> "A postulate block can only contain type signatures, possibly under keyword instance" DataBlock -> "A data definition can only contain type signatures, possibly under keyword instance" _ -> "Unexpected declaration" pretty (PragmaNoTerminationCheck _) = fsep $ pwords "Pragma {-# NO_TERMINATION_CHECK #-} has been removed. To skip the termination check, label your definitions either as {-# TERMINATING #-} or {-# NON_TERMINATING #-}." pretty (InvalidTerminationCheckPragma _) = fsep $ pwords "Termination checking pragmas can only precede a mutual block or a function definition." pretty (InvalidMeasureMutual _) = fsep $ pwords "In a mutual block, either all functions must have the same (or no) termination checking pragma." pretty (InvalidCatchallPragma _) = fsep $ pwords "The CATCHALL pragma can only preceed a function clause." pretty (UnquoteDefRequiresSignature xs) = fsep $ pwords "Missing type signatures for unquoteDef" ++ map pretty xs pretty (BadMacroDef nd) = fsep $ [text $ declName nd] ++ pwords "are not allowed in macro blocks" pretty (NotAllowedInMutual nd) = fsep $ [text $ declName nd] ++ pwords "are not allowed in mutual blocks" pretty (Codata _) = text $ "The codata construction has been removed. " ++ "Use the INFINITY builtin instead." pretty (DeclarationPanic s) = text s pretty (InvalidNoPositivityCheckPragma _) = fsep $ pwords "No positivity checking pragmas can only precede a mutual block or a data/record definition." instance Pretty DeclarationWarning where pretty (UnknownNamesInFixityDecl xs) = fsep $ pwords "The following names are not declared in the same scope as their syntax or fixity declaration (i.e., either not in scope at all, imported from another module, or declared in a super module):" ++ map pretty xs pretty (UnknownNamesInPolarityPragmas xs) = fsep $ pwords "The following names are not declared in the same scope as their polarity pragmas (they could for instance be out of scope, imported from another module, or declared in a super module):" ++ map pretty xs pretty (PolarityPragmasButNotPostulates xs) = fsep $ pwords "Polarity pragmas have been given for the following identifiers which are not postulates:" ++ map pretty xs pretty (UselessPrivate _) = fsep $ pwords "Using private here has no effect. Private applies only to declarations that introduce new identifiers into the module, like type signatures and data, record, and module declarations." pretty (UselessAbstract _) = fsep $ pwords "Using abstract here has no effect. Abstract applies to only definitions like data definitions, record type definitions and function clauses." pretty (UselessInstance _) = fsep $ pwords "Using instance here has no effect. Instance applies only to declarations that introduce new identifiers into the module, like type signatures and axioms." declName :: NiceDeclaration -> String declName Axiom{} = "Postulates" declName NiceField{} = "Fields" declName NiceMutual{} = "Mutual blocks" declName NiceModule{} = "Modules" declName NiceModuleMacro{} = "Modules" declName NiceOpen{} = "Open declarations" declName NiceImport{} = "Import statements" declName NicePragma{} = "Pragmas" declName PrimitiveFunction{} = "Primitive declarations" declName NicePatternSyn{} = "Pattern synonyms" declName NiceUnquoteDecl{} = "Unquoted declarations" declName NiceUnquoteDef{} = "Unquoted definitions" declName NiceRecSig{} = "Records" declName NiceDataSig{} = "Data types" declName NiceFunClause{} = "Functions without a type signature" declName FunSig{} = "Type signatures" declName FunDef{} = "Function definitions" declName RecDef{} = "Records" declName DataDef{} = "Data types" {-------------------------------------------------------------------------- The niceifier --------------------------------------------------------------------------} data InMutual = InMutual -- ^ we are nicifying a mutual block | NotInMutual -- ^ we are nicifying decls not in a mutual block deriving (Eq, Show) -- | The kind of the forward declaration, remembering the parameters. data DataRecOrFun = DataName { kindPosCheck :: PositivityCheck, kindParams :: Params } -- ^ Name of a data type with parameters. | RecName { kindPosCheck :: PositivityCheck, kindParams :: Params } -- ^ Name of a record type with parameters. | FunName TerminationCheck -- ^ Name of a function. deriving (Typeable, Data) -- Ignore pragmas when checking equality instance Eq DataRecOrFun where DataName _ p == DataName _ q = p == q RecName _ p == RecName _ q = p == q FunName _ == FunName _ = True _ == _ = False type Params = [Arg BoundName] instance Show DataRecOrFun where show (DataName _ n) = "data type" -- "with " ++ show n ++ " visible parameters" show (RecName _ n) = "record type" -- "with " ++ show n ++ " visible parameters" show (FunName{}) = "function" isFunName :: DataRecOrFun -> Bool isFunName (FunName{}) = True isFunName _ = False sameKind :: DataRecOrFun -> DataRecOrFun -> Bool sameKind DataName{} DataName{} = True sameKind RecName{} RecName{} = True sameKind FunName{} FunName{} = True sameKind _ _ = False terminationCheck :: DataRecOrFun -> TerminationCheck terminationCheck (FunName tc) = tc terminationCheck _ = TerminationCheck positivityCheck :: DataRecOrFun -> PositivityCheck positivityCheck (DataName pc _) = pc positivityCheck (RecName pc _) = pc positivityCheck _ = True -- | Check that declarations in a mutual block are consistently -- equipped with MEASURE pragmas, or whether there is a -- NO_TERMINATION_CHECK pragma. combineTermChecks :: Range -> [TerminationCheck] -> Nice TerminationCheck combineTermChecks r tcs = loop tcs where loop :: [TerminationCheck] -> Nice TerminationCheck loop [] = return TerminationCheck loop (tc : tcs) = do let failure r = throwError $ InvalidMeasureMutual r tc' <- loop tcs case (tc, tc') of (TerminationCheck , tc' ) -> return tc' (tc , TerminationCheck ) -> return tc (NonTerminating , NonTerminating ) -> return NonTerminating (NoTerminationCheck , NoTerminationCheck ) -> return NoTerminationCheck (NoTerminationCheck , Terminating ) -> return Terminating (Terminating , NoTerminationCheck ) -> return Terminating (Terminating , Terminating ) -> return Terminating (TerminationMeasure{} , TerminationMeasure{} ) -> return tc (TerminationMeasure r _, NoTerminationCheck ) -> failure r (TerminationMeasure r _, Terminating ) -> failure r (NoTerminationCheck , TerminationMeasure r _) -> failure r (Terminating , TerminationMeasure r _) -> failure r (TerminationMeasure r _, NonTerminating ) -> failure r (NonTerminating , TerminationMeasure r _) -> failure r (NoTerminationCheck , NonTerminating ) -> failure r (Terminating , NonTerminating ) -> failure r (NonTerminating , NoTerminationCheck ) -> failure r (NonTerminating , Terminating ) -> failure r -- | Check that the parameters of the data/record definition -- match the parameters of the corresponding signature. -- -- The definition may omit some hidden parameters. -- The names need to match. -- The types are not checked here. -- -- Precondition: the signature and definition have the same kind (data/record/fun). -- matchParameters :: Name -- ^ The data/record name. -> DataRecOrFun -- ^ The data/record signature. -> DataRecOrFun -- ^ The parameters as given in the data/record definition. -> Nice () matchParameters _ FunName{} FunName{} = return () matchParameters x sig def = loop (kindParams sig) (kindParams def) where failure = throwError $ WrongParameters x (kindParams sig) (kindParams def) loop hs [] = unless (all notVisible hs) failure loop [] (_:_) = failure loop (h:hs) (j:js) | h == j = loop hs js | notVisible h, visible j = loop hs (j:js) | otherwise = failure -- | Nicifier monad. -- Preserve the state when throwing an exception. newtype Nice a = Nice { unNice :: NiceEnv -> (Either DeclarationException a, NiceEnv) } -- We have to hand-roll the instances ourselves, since the automagic does not -- work for @Nice a = State s (Except e a)@, only for the usual -- @Nice a = StateT s (Except e) a@. instance Functor Nice where fmap f m = Nice $ \ s -> let (r, s') = unNice m s in case r of Left e -> (Left e, s') Right a -> (Right (f a), s') instance Applicative Nice where pure a = Nice $ \ s -> (Right a, s) (<*>) = ap instance Monad Nice where return = pure m >>= k = Nice $ \ s -> let (r, s') = unNice m s in case r of Left e -> (Left e, s') Right a -> unNice (k a) s' instance MonadState NiceEnv Nice where state f = Nice $ \ s -> first Right $ f s -- get = Nice $ \ s -> (Right s, s) -- Subsumed by state instance MonadError DeclarationException Nice where throwError e = Nice $ \ s -> (Left e, s) catchError m h = Nice $ \ s -> let (r, s') = unNice m s in case r of Left e -> unNice (h e) s' Right a -> (Right a, s') -- | Run a Nicifier computation, return result and warnings -- (in chronological order). runNice :: Nice a -> (Either DeclarationException a, NiceWarnings) runNice m = second (reverse . niceWarn) $ unNice m initNiceEnv -- | Nicifier state. data NiceEnv = NiceEnv { _loneSigs :: LoneSigs -- ^ Lone type signatures that wait for their definition. , _termChk :: TerminationCheck -- ^ Termination checking pragma waiting for a definition. , _posChk :: PositivityCheck -- ^ Positivity checking pragma waiting for a definition. , _catchall :: Catchall -- ^ Catchall pragma waiting for a function clause. , fixs :: Fixities , pols :: Polarities , niceWarn :: NiceWarnings -- ^ Stack of warnings. Head is last warning. } type LoneSigs = Map Name DataRecOrFun type Fixities = Map Name Fixity' type Polarities = Map Name [Occurrence] type NiceWarnings = [DeclarationWarning] -- ^ Stack of warnings. Head is last warning. -- | Initial nicifier state. initNiceEnv :: NiceEnv initNiceEnv = NiceEnv { _loneSigs = empty , _termChk = TerminationCheck , _posChk = True , _catchall = False , fixs = empty , pols = empty , niceWarn = [] } -- * Handling the lone signatures, stored to infer mutual blocks. -- | Lens for field '_loneSigs'. loneSigs :: Lens' LoneSigs NiceEnv loneSigs f e = f (_loneSigs e) <&> \ s -> e { _loneSigs = s } -- | Adding a lone signature to the state. addLoneSig :: Name -> DataRecOrFun -> Nice () addLoneSig x k = loneSigs %== \ s -> do let (mr, s') = Map.insertLookupWithKey (\ k new old -> new) x k s case mr of Nothing -> return s' Just{} -> throwError $ DuplicateDefinition x -- | Remove a lone signature from the state. removeLoneSig :: Name -> Nice () removeLoneSig x = loneSigs %= Map.delete x -- | Search for forward type signature. getSig :: Name -> Nice (Maybe DataRecOrFun) getSig x = Map.lookup x <$> use loneSigs -- | Check that no lone signatures are left in the state. noLoneSigs :: Nice Bool noLoneSigs = null <$> use loneSigs -- | Ensure that all forward declarations have been given a definition. checkLoneSigs :: [(Name, a)] -> Nice () checkLoneSigs xs = case xs of [] -> return () (x, _):_ -> throwError $ MissingDefinition x -- | Lens for field '_termChk'. terminationCheckPragma :: Lens' TerminationCheck NiceEnv terminationCheckPragma f e = f (_termChk e) <&> \ s -> e { _termChk = s } withTerminationCheckPragma :: TerminationCheck -> Nice a -> Nice a withTerminationCheckPragma tc f = do tc_old <- use terminationCheckPragma terminationCheckPragma .= tc result <- f terminationCheckPragma .= tc_old return result -- | Lens for field '_posChk'. positivityCheckPragma :: Lens' PositivityCheck NiceEnv positivityCheckPragma f e = f (_posChk e) <&> \ s -> e { _posChk = s } withPositivityCheckPragma :: PositivityCheck -> Nice a -> Nice a withPositivityCheckPragma pc f = do pc_old <- use positivityCheckPragma positivityCheckPragma .= pc result <- f positivityCheckPragma .= pc_old return result -- | Lens for field '_catchall'. catchallPragma :: Lens' Catchall NiceEnv catchallPragma f e = f (_catchall e) <&> \ s -> e { _catchall = s } -- | Get current catchall pragma, and reset it for the next clause. popCatchallPragma :: Nice Catchall popCatchallPragma = do ca <- use catchallPragma catchallPragma .= False return ca withCatchallPragma :: Catchall -> Nice a -> Nice a withCatchallPragma ca f = do ca_old <- use catchallPragma catchallPragma .= ca result <- f catchallPragma .= ca_old return result -- | Add a new warning. niceWarning :: DeclarationWarning -> Nice () niceWarning w = modify $ \ st -> st { niceWarn = w : niceWarn st } -- | Check whether name is not "_" and return its fixity. getFixity :: Name -> Nice Fixity' getFixity x = Map.findWithDefault noFixity' x <$> gets fixs -- WAS: defaultFixity' -- | Fail if the name is @_@. Otherwise the name's polarity, if any, -- is removed from the state and returned. getPolarity :: Name -> Nice (Maybe [Occurrence]) getPolarity x = do p <- gets (Map.lookup x . pols) modify (\s -> s { pols = Map.delete x (pols s) }) return p data DeclKind = LoneSig DataRecOrFun Name | LoneDefs DataRecOrFun [Name] | OtherDecl deriving (Eq, Show) declKind :: NiceDeclaration -> DeclKind declKind (FunSig _ _ _ _ _ _ _ tc x _) = LoneSig (FunName tc) x declKind (NiceRecSig _ _ _ _ pc x pars _) = LoneSig (RecName pc $ parameters pars) x declKind (NiceDataSig _ _ _ _ pc x pars _)= LoneSig (DataName pc $ parameters pars) x declKind (FunDef _ _ _ _ _ tc x _) = LoneDefs (FunName tc) [x] declKind (DataDef _ _ _ pc x pars _) = LoneDefs (DataName pc $ parameters pars) [x] declKind (RecDef _ _ _ pc x _ _ _ pars _) = LoneDefs (RecName pc $ parameters pars) [x] declKind (NiceUnquoteDef _ _ _ _ tc xs _) = LoneDefs (FunName tc) xs declKind Axiom{} = OtherDecl declKind NiceField{} = OtherDecl declKind PrimitiveFunction{} = OtherDecl declKind NiceMutual{} = OtherDecl declKind NiceModule{} = OtherDecl declKind NiceModuleMacro{} = OtherDecl declKind NiceOpen{} = OtherDecl declKind NiceImport{} = OtherDecl declKind NicePragma{} = OtherDecl declKind NiceFunClause{} = OtherDecl declKind NicePatternSyn{} = OtherDecl declKind NiceUnquoteDecl{} = OtherDecl -- | Compute parameters of a data or record signature or definition. parameters :: [LamBinding] -> Params parameters = List.concatMap $ \case DomainFree i x -> [Arg i x] DomainFull (TypedBindings _ (Arg _ TLet{})) -> [] DomainFull (TypedBindings _ (Arg i (TBind _ xs _))) -> for xs $ \ (WithHiding h x) -> mergeHiding $ WithHiding h $ Arg i x -- | Main. niceDeclarations :: [Declaration] -> Nice [NiceDeclaration] niceDeclarations ds = do -- Get fixity and syntax declarations. (fixs, polarities) <- fixitiesAndPolarities ds let declared = Set.fromList (concatMap declaredNames ds) -- If we have names in fixity declarations -- which are not defined in the appropriate scope, -- raise a warning and delete them from fixs. fixs <- ifNull (Map.keysSet fixs Set.\\ declared) (return fixs) $ \ unknownFixs -> do niceWarning $ UnknownNamesInFixityDecl $ Set.toList unknownFixs -- Note: Data.Map.restrictKeys requires containers >= 0.5.8.2 -- return $ Map.restrictKeys fixs declared return $ Map.filterWithKey (\ k _ -> Set.member k declared) fixs -- Same for undefined names in polarity declarations. polarities <- ifNull (Map.keysSet polarities Set.\\ declared) (return polarities) $ \ unknownPols -> do niceWarning $ UnknownNamesInPolarityPragmas $ Set.toList unknownPols -- Note: Data.Map.restrictKeys requires containers >= 0.5.8.2 -- return $ Map.restrictKeys polarities declared return $ Map.filterWithKey (\ k _ -> Set.member k declared) polarities -- Run the nicifier in an initial environment of fixity decls -- and polarities. But keep the warnings. st <- get put $ initNiceEnv { fixs = fixs, pols = polarities, niceWarn = niceWarn st } ds <- nice ds -- Check that every polarity pragma was used. unlessNullM (Map.keys <$> gets pols) $ \ unusedPolarities -> do niceWarning $ PolarityPragmasButNotPostulates unusedPolarities -- Check that every signature got its definition. checkLoneSigs . Map.toList =<< use loneSigs -- Note that loneSigs is ensured to be empty. -- (Important, since inferMutualBlocks also uses loneSigs state). res <- inferMutualBlocks ds -- Restore the old state, but keep the warnings. warns <- gets niceWarn put $ st { niceWarn = warns } return res where -- Compute the names defined in a declaration. -- We stay in the current scope, i.e., do not go into modules. declaredNames :: Declaration -> [Name] declaredNames d = case d of TypeSig _ x _ -> [x] Field _ x _ -> [x] FunClause (LHS p [] [] []) _ _ _ | IdentP (QName x) <- removeSingletonRawAppP p -> [x] FunClause{} -> [] DataSig _ _ x _ _ -> [x] Data _ _ x _ _ cs -> x : concatMap declaredNames cs RecordSig _ x _ _ -> [x] Record _ x _ _ c _ _ _ -> x : foldMap (:[]) (fst <$> c) Infix _ _ -> [] Syntax _ _ -> [] PatternSyn _ x _ _ -> [x] Mutual _ ds -> concatMap declaredNames ds Abstract _ ds -> concatMap declaredNames ds Private _ _ ds -> concatMap declaredNames ds InstanceB _ ds -> concatMap declaredNames ds Macro _ ds -> concatMap declaredNames ds Postulate _ ds -> concatMap declaredNames ds Primitive _ ds -> concatMap declaredNames ds Open{} -> [] Import{} -> [] ModuleMacro{} -> [] Module{} -> [] UnquoteDecl _ xs _ -> xs UnquoteDef{} -> [] Pragma{} -> [] inferMutualBlocks :: [NiceDeclaration] -> Nice [NiceDeclaration] inferMutualBlocks [] = return [] inferMutualBlocks (d : ds) = case declKind d of OtherDecl -> (d :) <$> inferMutualBlocks ds LoneDefs _ xs -> __IMPOSSIBLE__ LoneSig k x -> do addLoneSig x k ((tcs, pcs), (ds0, ds1)) <- untilAllDefined ([terminationCheck k], [positivityCheck k]) ds tc <- combineTermChecks (getRange d) tcs -- Record modules are, for performance reasons, not always -- placed in mutual blocks. -- ASR (01 January 2016): If the record module has a -- NO_POSITIVITY_CHECK pragma, it is placed in a mutual -- block. See Issue 1760. let prefix :: [NiceDeclaration] -> [NiceDeclaration] prefix = case (d, ds0) of (NiceRecSig{}, [r@(RecDef _ _ _ True _ _ _ _ _ _)]) -> ([d, r] ++) _ -> (NiceMutual (getRange (d : ds0)) tc (and pcs) (d : ds0) :) prefix <$> inferMutualBlocks ds1 where untilAllDefined :: ([TerminationCheck], [PositivityCheck]) -> [NiceDeclaration] -> Nice (([TerminationCheck], [PositivityCheck]), ([NiceDeclaration], [NiceDeclaration])) untilAllDefined (tc, pc) ds = do done <- noLoneSigs if done then return ((tc, pc), ([], ds)) else case ds of [] -> __IMPOSSIBLE__ <$ (checkLoneSigs . Map.toList =<< use loneSigs) d : ds -> case declKind d of LoneSig k x -> addLoneSig x k >> cons d (untilAllDefined (terminationCheck k : tc, positivityCheck k : pc) ds) LoneDefs k xs -> do mapM_ removeLoneSig xs cons d (untilAllDefined (terminationCheck k : tc, positivityCheck k : pc) ds) OtherDecl -> cons d (untilAllDefined (tc, pc) ds) where -- ASR (26 December 2015): Type annotated version of the @cons@ function. -- cons d = fmap $ -- (id :: (([TerminationCheck], [PositivityCheck]) -> ([TerminationCheck], [PositivityCheck]))) -- *** (d :) -- *** (id :: [NiceDeclaration] -> [NiceDeclaration]) cons d = fmap (id *** (d :) *** id) notMeasure TerminationMeasure{} = False notMeasure _ = True nice :: [Declaration] -> Nice [NiceDeclaration] nice [] = return [] nice ds = do (xs , ys) <- nice1 ds (xs ++) <$> nice ys nice1 :: [Declaration] -> Nice ([NiceDeclaration], [Declaration]) nice1 [] = __IMPOSSIBLE__ nice1 (d:ds) = case d of (TypeSig info x t) -> do termCheck <- use terminationCheckPragma fx <- getFixity x -- register x as lone type signature, to recognize clauses later addLoneSig x (FunName termCheck) return ([FunSig (getRange d) fx PublicAccess ConcreteDef NotInstanceDef NotMacroDef info termCheck x t] , ds) (FunClause lhs _ _ _) -> do termCheck <- use terminationCheckPragma catchall <- popCatchallPragma xs <- map fst . filter (isFunName . snd) . Map.toList <$> use loneSigs -- for each type signature 'x' waiting for clauses, we try -- if we have some clauses for 'x' fixs <- gets fixs case [ (x, (fits, rest)) | x <- xs , let (fits, rest) = -- Anonymous declarations only have 1 clause each! if isNoName x then ([d], ds) else span (couldBeFunClauseOf (Map.lookup x fixs) x) (d : ds) , not (null fits) ] of -- case: clauses match none of the sigs [] -> case lhs of -- Subcase: The lhs is single identifier (potentially anonymous). -- Treat it as a function clause without a type signature. LHS p [] [] [] | Just x <- isSingleIdentifierP p -> do d <- mkFunDef defaultArgInfo termCheck x Nothing [d] -- fun def without type signature is relevant return (d , ds) -- Subcase: The lhs is a proper pattern. -- This could be a let-pattern binding. Pass it on. -- A missing type signature error might be raise in ConcreteToAbstract _ -> do return ([NiceFunClause (getRange d) PublicAccess ConcreteDef termCheck catchall d] , ds) -- case: clauses match exactly one of the sigs [(x,(fits,rest))] -> do removeLoneSig x cs <- mkClauses x (expandEllipsis fits) False fx <- getFixity x return ([FunDef (getRange fits) fits fx ConcreteDef NotInstanceDef termCheck x cs] , rest) -- case: clauses match more than one sigs (ambiguity) l -> throwError $ AmbiguousFunClauses lhs $ reverse $ map fst l -- "ambiguous function clause; cannot assign it uniquely to one type signature" Field{} -> (,ds) <$> niceAxioms FieldBlock [ d ] DataSig r CoInductive _ _ _ -> throwError (Codata r) Data r CoInductive _ _ _ _ -> throwError (Codata r) (DataSig r Inductive x tel t) -> do pc <- use positivityCheckPragma addLoneSig x (DataName pc $ parameters tel) (,) <$> dataOrRec pc DataDef NiceDataSig (niceAxioms DataBlock) r x (Just (tel, t)) Nothing <*> return ds (Data r Inductive x tel mt cs) -> do pc <- use positivityCheckPragma mt <- defaultTypeSig (DataName pc $ parameters tel) x mt (,) <$> dataOrRec pc DataDef NiceDataSig (niceAxioms DataBlock) r x ((tel,) <$> mt) (Just (tel, cs)) <*> return ds (RecordSig r x tel t) -> do pc <- use positivityCheckPragma addLoneSig x (RecName pc $ parameters tel) fx <- getFixity x return ([NiceRecSig r fx PublicAccess ConcreteDef pc x tel t] , ds) (Record r x i e c tel mt cs) -> do pc <- use positivityCheckPragma mt <- defaultTypeSig (RecName pc $ parameters tel) x mt c <- traverse (\(cname, cinst) -> do fix <- getFixity cname; return (ThingWithFixity cname fix, cinst)) c (,) <$> dataOrRec pc (\ r f a pc x tel cs -> RecDef r f a pc x i e c tel cs) NiceRecSig niceDeclarations r x ((tel,) <$> mt) (Just (tel, cs)) <*> return ds Mutual r ds' -> (,ds) <$> (singleton <$> (mkOldMutual r =<< nice ds')) Abstract r ds' -> (,ds) <$> (abstractBlock r =<< nice ds') Private r o ds' -> (,ds) <$> (privateBlock r o =<< nice ds') InstanceB r ds' -> (,ds) <$> (instanceBlock r =<< nice ds') Macro r ds' -> (,ds) <$> (macroBlock r =<< nice ds') Postulate _ ds' -> (,ds) <$> (mapM setPolarity =<< niceAxioms PostulateBlock ds') where setPolarity (Axiom r f acc a i arg Nothing x e) = do mp <- getPolarity x return (Axiom r f acc a i arg mp x e) setPolarity (Axiom _ _ _ _ _ _ (Just _) _ _) = __IMPOSSIBLE__ setPolarity d = return d Primitive _ ds' -> (,ds) <$> (map toPrim <$> niceAxioms PrimitiveBlock ds') Module r x tel ds' -> return $ ([NiceModule r PublicAccess ConcreteDef x tel ds'] , ds) ModuleMacro r x modapp op is -> return $ ([NiceModuleMacro r PublicAccess x modapp op is] , ds) -- Fixity and syntax declarations and polarity pragmas have -- already been processed. Infix _ _ -> return ([], ds) Syntax _ _ -> return ([], ds) PatternSyn r n as p -> do fx <- getFixity n return ([NicePatternSyn r fx n as p] , ds) Open r x is -> return ([NiceOpen r x is] , ds) Import r x as op is -> return ([NiceImport r x as op is] , ds) UnquoteDecl r xs e -> do fxs <- mapM getFixity xs tc <- use terminationCheckPragma return ([NiceUnquoteDecl r fxs PublicAccess ConcreteDef NotInstanceDef tc xs e] , ds) UnquoteDef r xs e -> do fxs <- mapM getFixity xs sigs <- map fst . filter (isFunName . snd) . Map.toList <$> use loneSigs let missing = filter (`notElem` sigs) xs if null missing then do mapM_ removeLoneSig xs return ([NiceUnquoteDef r fxs PublicAccess ConcreteDef TerminationCheck xs e] , ds) else throwError $ UnquoteDefRequiresSignature missing Pragma p -> nicePragma p ds nicePragma :: Pragma -> [Declaration] -> Nice ([NiceDeclaration], [Declaration]) nicePragma (TerminationCheckPragma r (TerminationMeasure _ x)) ds = if canHaveTerminationMeasure ds then withTerminationCheckPragma (TerminationMeasure r x) $ nice1 ds else throwError $ InvalidTerminationCheckPragma r nicePragma (TerminationCheckPragma r NoTerminationCheck) ds = throwError $ PragmaNoTerminationCheck r nicePragma (TerminationCheckPragma r tc) ds = if canHaveTerminationCheckPragma ds then withTerminationCheckPragma tc $ nice1 ds else throwError $ InvalidTerminationCheckPragma r nicePragma (CatchallPragma r) ds = if canHaveCatchallPragma ds then withCatchallPragma True $ nice1 ds else throwError $ InvalidCatchallPragma r nicePragma (NoPositivityCheckPragma r) ds = if canHaveNoPositivityCheckPragma ds then withPositivityCheckPragma False $ nice1 ds else throwError $ InvalidNoPositivityCheckPragma r nicePragma (PolarityPragma{}) ds = return ([], ds) nicePragma p ds = return ([NicePragma (getRange p) p], ds) canHaveTerminationMeasure :: [Declaration] -> Bool canHaveTerminationMeasure [] = False canHaveTerminationMeasure (d:ds) = case d of TypeSig{} -> True (Pragma p) | isAttachedPragma p -> canHaveTerminationMeasure ds _ -> False canHaveTerminationCheckPragma :: [Declaration] -> Bool canHaveTerminationCheckPragma [] = False canHaveTerminationCheckPragma (d:ds) = case d of Mutual{} -> True TypeSig{} -> True FunClause{} -> True UnquoteDecl{} -> True (Pragma p) | isAttachedPragma p -> canHaveTerminationCheckPragma ds _ -> False canHaveCatchallPragma :: [Declaration] -> Bool canHaveCatchallPragma [] = False canHaveCatchallPragma (d:ds) = case d of FunClause{} -> True (Pragma p) | isAttachedPragma p -> canHaveCatchallPragma ds _ -> False canHaveNoPositivityCheckPragma :: [Declaration] -> Bool canHaveNoPositivityCheckPragma [] = False canHaveNoPositivityCheckPragma (d:ds) = case d of Mutual{} -> True (Data _ Inductive _ _ _ _) -> True (DataSig _ Inductive _ _ _) -> True Record{} -> True RecordSig{} -> True (Pragma p) | isAttachedPragma p -> canHaveNoPositivityCheckPragma ds _ -> False isAttachedPragma :: Pragma -> Bool isAttachedPragma p = case p of TerminationCheckPragma{} -> True CatchallPragma{} -> True NoPositivityCheckPragma{} -> True _ -> False -- We could add a default type signature here, but at the moment we can't -- infer the type of a record or datatype, so better to just fail here. defaultTypeSig :: DataRecOrFun -> Name -> Maybe Expr -> Nice (Maybe Expr) defaultTypeSig k x t@Just{} = return t defaultTypeSig k x Nothing = do caseMaybeM (getSig x) (throwError $ MissingDataSignature x) $ \ k' -> do unless (sameKind k k') $ throwError $ WrongDefinition x k' k unless (k == k') $ matchParameters x k' k Nothing <$ removeLoneSig x dataOrRec :: forall a . PositivityCheck -> (Range -> Fixity' -> IsAbstract -> PositivityCheck -> Name -> [LamBinding] -> [NiceConstructor] -> NiceDeclaration) -- ^ Construct definition. -> (Range -> Fixity' -> Access -> IsAbstract -> PositivityCheck -> Name -> [LamBinding] -> Expr -> NiceDeclaration) -- ^ Construct signature. -> ([a] -> Nice [NiceDeclaration]) -- ^ Constructor checking. -> Range -> Name -- ^ Data/record type name. -> Maybe ([LamBinding], Expr) -- ^ Parameters and type. If not @Nothing@ a signature is created. -> Maybe ([LamBinding], [a]) -- ^ Parameters and constructors. If not @Nothing@, a definition body is created. -> Nice [NiceDeclaration] dataOrRec pc mkDef mkSig niceD r x mt mcs = do mds <- Trav.forM mcs $ \ (tel, cs) -> (tel,) <$> niceD cs f <- getFixity x return $ catMaybes $ [ mt <&> \ (tel, t) -> mkSig (fuseRange x t) f PublicAccess ConcreteDef pc x tel t , mds <&> \ (tel, ds) -> mkDef r f ConcreteDef pc x (caseMaybe mt tel $ const $ concatMap dropType tel) ds -- If a type is given (mt /= Nothing), we have to delete the types in @tel@ -- for the data definition, lest we duplicate them. ] where -- | Drop type annotations and lets from bindings. dropType :: LamBinding -> [LamBinding] dropType (DomainFull (TypedBindings _r (Arg ai (TBind _ xs _)))) = map (mergeHiding . fmap (DomainFree ai)) xs dropType (DomainFull (TypedBindings _r (Arg _ TLet{}))) = [] dropType b@DomainFree{} = [b] -- Translate axioms niceAxioms :: KindOfBlock -> [TypeSignatureOrInstanceBlock] -> Nice [NiceDeclaration] niceAxioms b ds = liftM List.concat $ mapM (niceAxiom b) ds niceAxiom :: KindOfBlock -> TypeSignatureOrInstanceBlock -> Nice [NiceDeclaration] niceAxiom b d = case d of TypeSig rel x t -> do fx <- getFixity x return [ Axiom (getRange d) fx PublicAccess ConcreteDef NotInstanceDef rel Nothing x t ] Field i x argt | b == FieldBlock -> do fx <- getFixity x return [ NiceField (getRange d) fx PublicAccess ConcreteDef i x argt ] InstanceB r decls -> do instanceBlock r =<< niceAxioms InstanceBlock decls Pragma p@(RewritePragma r _) -> do return [ NicePragma r p ] _ -> throwError $ WrongContentBlock b $ getRange d toPrim :: NiceDeclaration -> NiceDeclaration toPrim (Axiom r f p a i rel Nothing x t) = PrimitiveFunction r f p a x t toPrim _ = __IMPOSSIBLE__ -- Create a function definition. mkFunDef info termCheck x mt ds0 = do cs <- mkClauses x (expandEllipsis ds0) False f <- getFixity x return [ FunSig (fuseRange x t) f PublicAccess ConcreteDef NotInstanceDef NotMacroDef info termCheck x t , FunDef (getRange ds0) ds0 f ConcreteDef NotInstanceDef termCheck x cs ] where t = case mt of Just t -> t Nothing -> underscore (getRange x) underscore r = Underscore r Nothing expandEllipsis :: [Declaration] -> [Declaration] expandEllipsis [] = [] expandEllipsis (d@(FunClause Ellipsis{} _ _ _) : ds) = d : expandEllipsis ds expandEllipsis (d@(FunClause lhs@(LHS p ps _ _) _ _ _) : ds) = d : expand (wipe p) (map wipe ps) ds where expand _ _ [] = [] expand p ps (d@(Pragma (CatchallPragma r)) : ds) = d : expand p ps ds expand p ps (FunClause (Ellipsis r ps' eqs es) rhs wh ca : ds) = FunClause (LHS (setRange r p) ((setRange r ps) ++ ps') eqs es) rhs wh ca : expand p (applyUnless (null es) (++ (map wipe ps')) ps) ds -- If we have with-expressions (es /= []) then the following -- ellipses also get the additional with patterns ps' -- We can have ellipses after a fun clause. -- They refer to the last clause that introduced new with-expressions. expand p ps (d@(FunClause (LHS _ _ _ []) _ _ _) : ds) = d : expand p ps ds -- Same here: If we have new with-expressions, the next ellipses will -- refer to us. expand _ _ (d@(FunClause (LHS p' ps' _ (_ : _)) _ _ _) : ds) = d : expand (wipe p') (map wipe ps') ds -- Andreas, Jesper, 2017-05-13, issue #2578 -- Need to update the range also on the next with-patterns. expand _ _ (_ : ds) = __IMPOSSIBLE__ expandEllipsis (_ : ds) = __IMPOSSIBLE__ -- Before copying a pattern, remove traces to its origin. wipe :: Pattern -> Pattern wipe = killRange . setInserted setInserted :: Pattern -> Pattern setInserted p = case p of IdentP{} -> p QuoteP{} -> p AppP p q -> AppP (setInserted p) (fmap (fmap setInserted) q) RawAppP r ps -> RawAppP r (map setInserted ps) OpAppP r c ns ps -> OpAppP r c ns (map (fmap $ fmap setInserted) ps) HiddenP r p -> HiddenP r (fmap setInserted p) InstanceP r p -> InstanceP r (fmap setInserted p) ParenP r p -> ParenP r (setInserted p) WildP{} -> p AbsurdP{} -> p AsP r n p -> AsP r n (setInserted p) DotP r _ e -> DotP r Inserted e LitP{} -> p RecP r fs -> RecP r (map (fmap setInserted) fs) -- Turn function clauses into nice function clauses. mkClauses :: Name -> [Declaration] -> Catchall -> Nice [Clause] mkClauses _ [] _ = return [] mkClauses x (Pragma (CatchallPragma r) : cs) True = throwError $ InvalidCatchallPragma r mkClauses x (Pragma (CatchallPragma r) : cs) False = do when (null cs) $ throwError $ InvalidCatchallPragma r mkClauses x cs True mkClauses x (FunClause lhs@(LHS _ _ _ []) rhs wh ca : cs) catchall = (Clause x (ca || catchall) lhs rhs wh [] :) <$> mkClauses x cs False mkClauses x (FunClause lhs@(LHS _ ps _ es) rhs wh ca : cs) catchall = do when (null with) $ throwError $ MissingWithClauses x wcs <- mkClauses x with False (Clause x (ca || catchall) lhs rhs wh wcs :) <$> mkClauses x cs' False where (with, cs') = subClauses cs -- A clause is a subclause if the number of with-patterns is -- greater or equal to the current number of with-patterns plus the -- number of with arguments. subClauses :: [Declaration] -> ([Declaration],[Declaration]) subClauses (c@(FunClause (LHS _ ps' _ _) _ _ _) : cs) | length ps' >= length ps + length es = mapFst (c:) (subClauses cs) | otherwise = ([], c:cs) subClauses (c@(FunClause (Ellipsis _ ps' _ _) _ _ _) : cs) = mapFst (c:) (subClauses cs) subClauses (c@(Pragma (CatchallPragma r)) : cs) = case subClauses cs of ([], cs') -> ([], c:cs') (cs, cs') -> (c:cs, cs') subClauses [] = ([],[]) subClauses _ = __IMPOSSIBLE__ mkClauses x (FunClause lhs@Ellipsis{} rhs wh ca : cs) catchall = (Clause x (ca || catchall) lhs rhs wh [] :) <$> mkClauses x cs False -- Will result in an error later. mkClauses _ _ _ = __IMPOSSIBLE__ -- for finding clauses for a type sig in mutual blocks couldBeFunClauseOf :: Maybe Fixity' -> Name -> Declaration -> Bool couldBeFunClauseOf mFixity x (Pragma (CatchallPragma{})) = True couldBeFunClauseOf mFixity x (FunClause Ellipsis{} _ _ _) = True couldBeFunClauseOf mFixity x (FunClause (LHS p _ _ _) _ _ _) = let pns = patternNames p xStrings = nameStringParts x patStrings = concatMap nameStringParts pns in -- trace ("x = " ++ prettyShow x) $ -- trace ("pns = " ++ show pns) $ -- trace ("xStrings = " ++ show xStrings) $ -- trace ("patStrings = " ++ show patStrings) $ -- trace ("mFixity = " ++ show mFixity) $ case (headMaybe pns, mFixity) of -- first identifier in the patterns is the fun.symbol? (Just y, _) | x == y -> True -- trace ("couldBe since y = " ++ prettyShow y) $ True -- are the parts of x contained in p _ | xStrings `isSublistOf` patStrings -> True -- trace ("couldBe since isSublistOf") $ True -- looking for a mixfix fun.symb (_, Just fix) -> -- also matches in case of a postfix let notStrings = stringParts (theNotation fix) in -- trace ("notStrings = " ++ show notStrings) $ -- trace ("patStrings = " ++ show patStrings) $ (not $ null notStrings) && (notStrings `isSublistOf` patStrings) -- not a notation, not first id: give up _ -> False -- trace ("couldBe not (case default)") $ False couldBeFunClauseOf _ _ _ = False -- trace ("couldBe not (fun default)") $ False -- ASR (27 May 2014). Commented out unused code. -- @isFunClauseOf@ is for non-mutual blocks where clauses must follow the -- type sig immediately -- isFunClauseOf :: Name -> Declaration -> Bool -- isFunClauseOf x (FunClause Ellipsis{} _ _) = True -- isFunClauseOf x (FunClause (LHS p _ _ _) _ _) = -- -- p is the whole left hand side, excluding "with" patterns and clauses -- case removeSingletonRawAppP p of -- IdentP (QName q) -> x == q -- lhs is just an identifier -- _ -> True -- -- more complicated lhss must come with type signatures, so we just assume -- -- it's part of the current definition -- isFunClauseOf _ _ = False isSingleIdentifierP :: Pattern -> Maybe Name isSingleIdentifierP p = case removeSingletonRawAppP p of IdentP (QName x) -> Just x WildP r -> Just $ noName r _ -> Nothing removeSingletonRawAppP :: Pattern -> Pattern removeSingletonRawAppP p = case p of RawAppP _ [p'] -> removeSingletonRawAppP p' ParenP _ p' -> removeSingletonRawAppP p' _ -> p -- Make an old style mutual block from a list of mutual declarations mkOldMutual :: Range -> [NiceDeclaration] -> Nice NiceDeclaration mkOldMutual r ds = do -- Check that there aren't any missing definitions checkLoneSigs loneNames -- Check that there are no declarations that aren't allowed in old style mutual blocks case filter notAllowedInMutual ds of [] -> return () (NiceFunClause _ _ _ _ s_ (FunClause lhs _ _ _)):_ -> throwError $ MissingTypeSignature lhs d:_ -> throwError $ NotAllowedInMutual d tc0 <- use terminationCheckPragma let tcs = map termCheck ds tc <- combineTermChecks r (tc0:tcs) pc0 <- use positivityCheckPragma let pc :: PositivityCheck pc = pc0 && all positivityCheckOldMutual ds return $ NiceMutual r tc pc $ sigs ++ other where -- Andreas, 2013-11-23 allow postulates in mutual blocks notAllowedInMutual Axiom{} = False notAllowedInMutual d = declKind d == OtherDecl -- Pull type signatures to the top (sigs, other) = List.partition isTypeSig ds isTypeSig Axiom{} = True isTypeSig d | LoneSig{} <- declKind d = True isTypeSig _ = False sigNames = [ (x, k) | LoneSig k x <- map declKind ds ] defNames = [ (x, k) | LoneDefs k xs <- map declKind ds, x <- xs ] -- compute the set difference with equality just on names loneNames = [ (x, k) | (x, k) <- sigNames, List.all ((x /=) . fst) defNames ] -- Andreas, 2013-02-28 (issue 804): -- do not termination check a mutual block if any of its -- inner declarations comes with a {-# NO_TERMINATION_CHECK #-} termCheck (FunSig _ _ _ _ _ _ _ tc _ _) = tc termCheck (FunDef _ _ _ _ _ tc _ _) = tc -- ASR (28 December 2015): Is this equation necessary? termCheck (NiceMutual _ tc _ _) = __IMPOSSIBLE__ termCheck (NiceUnquoteDecl _ _ _ _ _ tc _ _) = tc termCheck (NiceUnquoteDef _ _ _ _ tc _ _) = tc termCheck Axiom{} = TerminationCheck termCheck NiceField{} = TerminationCheck termCheck PrimitiveFunction{} = TerminationCheck termCheck NiceModule{} = TerminationCheck termCheck NiceModuleMacro{} = TerminationCheck termCheck NiceOpen{} = TerminationCheck termCheck NiceImport{} = TerminationCheck termCheck NicePragma{} = TerminationCheck termCheck NiceRecSig{} = TerminationCheck termCheck NiceDataSig{} = TerminationCheck termCheck NiceFunClause{} = TerminationCheck termCheck DataDef{} = TerminationCheck termCheck RecDef{} = TerminationCheck termCheck NicePatternSyn{} = TerminationCheck -- ASR (26 December 2015): Do not positivity check a mutual -- block if any of its inner declarations comes with a -- NO_POSITIVITY_CHECK pragma. See Issue 1614. positivityCheckOldMutual :: NiceDeclaration -> PositivityCheck positivityCheckOldMutual (DataDef _ _ _ pc _ _ _) = pc positivityCheckOldMutual (NiceDataSig _ _ _ _ pc _ _ _)= pc positivityCheckOldMutual (NiceMutual _ _ pc _) = __IMPOSSIBLE__ positivityCheckOldMutual (NiceRecSig _ _ _ _ pc _ _ _) = pc positivityCheckOldMutual (RecDef _ _ _ pc _ _ _ _ _ _) = pc positivityCheckOldMutual _ = True -- A mutual block cannot have a measure, -- but it can skip termination check. abstractBlock _ [] = return [] abstractBlock r ds = do let (ds', anyChange) = runChange $ mkAbstract ds inherited = r == noRange if anyChange then return ds' else do -- hack to avoid failing on inherited abstract blocks in where clauses unless inherited $ niceWarning $ UselessAbstract r return ds -- no change! privateBlock _ _ [] = return [] privateBlock r o ds = do let (ds', anyChange) = runChange $ mkPrivate o ds if anyChange then return ds' else do when (o == UserWritten) $ niceWarning $ UselessPrivate r return ds -- no change! instanceBlock _ [] = return [] instanceBlock r ds = do let (ds', anyChange) = runChange $ mapM mkInstance ds if anyChange then return ds' else do niceWarning $ UselessInstance r return ds -- no change! -- Make a declaration eligible for instance search. mkInstance :: Updater NiceDeclaration mkInstance d = case d of Axiom r f p a i rel mp x e -> (\ i -> Axiom r f p a i rel mp x e) <$> setInstance i FunSig r f p a i m rel tc x e -> (\ i -> FunSig r f p a i m rel tc x e) <$> setInstance i NiceUnquoteDecl r f p a i tc x e -> (\ i -> NiceUnquoteDecl r f p a i tc x e) <$> setInstance i NiceMutual{} -> return d NiceFunClause{} -> return d FunDef r ds f a i tc x cs -> (\ i -> FunDef r ds f a i tc x cs) <$> setInstance i NiceField{} -> return d -- Field instance are handled by the parser PrimitiveFunction{} -> return d NiceUnquoteDef{} -> return d NiceRecSig{} -> return d NiceDataSig{} -> return d NiceModuleMacro{} -> return d NiceModule{} -> return d NicePragma{} -> return d NiceOpen{} -> return d NiceImport{} -> return d DataDef{} -> return d RecDef{} -> return d NicePatternSyn{} -> return d setInstance :: Updater IsInstance setInstance i = case i of InstanceDef -> return i _ -> dirty $ InstanceDef macroBlock r ds = mapM mkMacro ds mkMacro :: NiceDeclaration -> Nice NiceDeclaration mkMacro d = case d of FunSig r f p a i _ rel tc x e -> return $ FunSig r f p a i MacroDef rel tc x e FunDef{} -> return d _ -> throwError (BadMacroDef d) -- | Make a declaration abstract. -- -- Mark computation as 'dirty' if there was a declaration that could be made abstract. -- If no abstraction is taking place, we want to complain about 'UselessAbstract'. -- -- Alternatively, we could only flag 'dirty' if a non-abstract thing was abstracted. -- Then, nested @abstract@s would sometimes also be complained about. class MakeAbstract a where mkAbstract :: Updater a default mkAbstract :: (Traversable f, MakeAbstract a', a ~ f a') => Updater a mkAbstract = traverse mkAbstract instance MakeAbstract a => MakeAbstract [a] where -- Default definition kicks in here! -- But note that we still have to declare the instance! -- Leads to overlap with 'WhereClause': -- instance (Traversable f, MakeAbstract a) => MakeAbstract (f a) where -- mkAbstract = traverse mkAbstract instance MakeAbstract IsAbstract where mkAbstract a = case a of AbstractDef -> return a ConcreteDef -> dirty $ AbstractDef instance MakeAbstract NiceDeclaration where mkAbstract d = case d of NiceMutual r termCheck pc ds -> NiceMutual r termCheck pc <$> mkAbstract ds FunDef r ds f a i tc x cs -> (\ a -> FunDef r ds f a i tc x) <$> mkAbstract a <*> mkAbstract cs DataDef r f a pc x ps cs -> (\ a -> DataDef r f a pc x ps) <$> mkAbstract a <*> mkAbstract cs RecDef r f a pc x i e c ps cs -> (\ a -> RecDef r f a pc x i e c ps) <$> mkAbstract a <*> mkAbstract cs NiceFunClause r p a termCheck catchall d -> (\ a -> NiceFunClause r p a termCheck catchall d) <$> mkAbstract a -- The following declarations have an @InAbstract@ field -- but are not really definitions, so we do count them into -- the declarations which can be made abstract -- (thus, do not notify progress with @dirty@). Axiom r f p a i rel mp x e -> return $ Axiom r f p AbstractDef i rel mp x e FunSig r f p a i m rel tc x e -> return $ FunSig r f p AbstractDef i m rel tc x e NiceRecSig r f p a pc x ls t -> return $ NiceRecSig r f p AbstractDef pc x ls t NiceDataSig r f p a pc x ls t -> return $ NiceDataSig r f p AbstractDef pc x ls t NiceField r f p _ i x e -> return $ NiceField r f p AbstractDef i x e PrimitiveFunction r f p _ x e -> return $ PrimitiveFunction r f p AbstractDef x e -- Andreas, 2016-07-17 it does have effect on unquoted defs. -- Need to set updater state to dirty! NiceUnquoteDecl r f p _ i t x e -> dirty $ NiceUnquoteDecl r f p AbstractDef i t x e NiceUnquoteDef r f p _ t x e -> dirty $ NiceUnquoteDef r f p AbstractDef t x e NiceModule{} -> return d NiceModuleMacro{} -> return d NicePragma{} -> return d NiceOpen{} -> return d NiceImport{} -> return d NicePatternSyn{} -> return d instance MakeAbstract Clause where mkAbstract (Clause x catchall lhs rhs wh with) = do Clause x catchall lhs rhs <$> mkAbstract wh <*> mkAbstract with -- | Contents of a @where@ clause are abstract if the parent is. instance MakeAbstract WhereClause where mkAbstract NoWhere = return $ NoWhere mkAbstract (AnyWhere ds) = dirty $ AnyWhere [Abstract noRange ds] mkAbstract (SomeWhere m a ds) = dirty $ SomeWhere m a [Abstract noRange ds] -- | Make a declaration private. -- -- Andreas, 2012-11-17: -- Mark computation as 'dirty' if there was a declaration that could be privatized. -- If no privatization is taking place, we want to complain about 'UselessPrivate'. -- -- Alternatively, we could only flag 'dirty' if a non-private thing was privatized. -- Then, nested @private@s would sometimes also be complained about. class MakePrivate a where mkPrivate :: Origin -> Updater a default mkPrivate :: (Traversable f, MakePrivate a', a ~ f a') => Origin -> Updater a mkPrivate o = traverse $ mkPrivate o instance MakePrivate a => MakePrivate [a] where -- Default definition kicks in here! -- But note that we still have to declare the instance! -- Leads to overlap with 'WhereClause': -- instance (Traversable f, MakePrivate a) => MakePrivate (f a) where -- mkPrivate = traverse mkPrivate instance MakePrivate Access where mkPrivate o p = case p of PrivateAccess{} -> return p -- OR? return $ PrivateAccess o _ -> dirty $ PrivateAccess o instance MakePrivate NiceDeclaration where mkPrivate o d = case d of Axiom r f p a i rel mp x e -> (\ p -> Axiom r f p a i rel mp x e) <$> mkPrivate o p NiceField r f p a i x e -> (\ p -> NiceField r f p a i x e) <$> mkPrivate o p PrimitiveFunction r f p a x e -> (\ p -> PrimitiveFunction r f p a x e) <$> mkPrivate o p NiceMutual r termCheck pc ds -> (\ p -> NiceMutual r termCheck pc p) <$> mkPrivate o ds NiceModule r p a x tel ds -> (\ p -> NiceModule r p a x tel ds) <$> mkPrivate o p NiceModuleMacro r p x ma op is -> (\ p -> NiceModuleMacro r p x ma op is) <$> mkPrivate o p FunSig r f p a i m rel tc x e -> (\ p -> FunSig r f p a i m rel tc x e) <$> mkPrivate o p NiceRecSig r f p a pc x ls t -> (\ p -> NiceRecSig r f p a pc x ls t) <$> mkPrivate o p NiceDataSig r f p a pc x ls t -> (\ p -> NiceDataSig r f p a pc x ls t) <$> mkPrivate o p NiceFunClause r p a termCheck catchall d -> (\ p -> NiceFunClause r p a termCheck catchall d) <$> mkPrivate o p NiceUnquoteDecl r f p a i t x e -> (\ p -> NiceUnquoteDecl r f p a i t x e) <$> mkPrivate o p NiceUnquoteDef r f p a t x e -> (\ p -> NiceUnquoteDef r f p a t x e) <$> mkPrivate o p NicePragma _ _ -> return $ d NiceOpen _ _ _ -> return $ d NiceImport _ _ _ _ _ -> return $ d -- Andreas, 2016-07-08, issue #2089 -- we need to propagate 'private' to the named where modules FunDef r ds f a i tc x cls -> FunDef r ds f a i tc x <$> mkPrivate o cls DataDef{} -> return $ d RecDef{} -> return $ d NicePatternSyn _ _ _ _ _ -> return $ d instance MakePrivate Clause where mkPrivate o (Clause x catchall lhs rhs wh with) = do Clause x catchall lhs rhs <$> mkPrivate o wh <*> mkPrivate o with instance MakePrivate WhereClause where mkPrivate o NoWhere = return $ NoWhere -- @where@-declarations are protected behind an anonymous module, -- thus, they are effectively private by default. mkPrivate o (AnyWhere ds) = return $ AnyWhere ds -- Andreas, 2016-07-08 -- A @where@-module is private if the parent function is private. -- The contents of this module are not private, unless declared so! -- Thus, we do not recurse into the @ds@ (could not anyway). mkPrivate o (SomeWhere m a ds) = mkPrivate o a <&> \ a' -> SomeWhere m a' ds -- | Add more fixities. Throw an exception for multiple fixity declarations. -- OR: Disjoint union of fixity maps. Throws exception if not disjoint. plusFixities :: Fixities -> Fixities -> Nice Fixities plusFixities m1 m2 -- If maps are not disjoint, report conflicts as exception. | not (null isect) = throwError $ MultipleFixityDecls isect -- Otherwise, do the union. | otherwise = return $ Map.unionWithKey mergeFixites m1 m2 where -- Merge two fixities, assuming there is no conflict mergeFixites name (Fixity' f1 s1 r1) (Fixity' f2 s2 r2) = Fixity' f s $ fuseRange r1 r2 where f | f1 == noFixity = f2 | f2 == noFixity = f1 | otherwise = __IMPOSSIBLE__ s | s1 == noNotation = s2 | s2 == noNotation = s1 | otherwise = __IMPOSSIBLE__ -- Compute a list of conflicts in a format suitable for error reporting. isect = [ (x, map (Map.findWithDefault __IMPOSSIBLE__ x) [m1,m2]) | (x, False) <- Map.assocs $ Map.intersectionWith compatible m1 m2 ] -- Check for no conflict. compatible (Fixity' f1 s1 _) (Fixity' f2 s2 _) = (f1 == noFixity || f2 == noFixity ) && (s1 == noNotation || s2 == noNotation) -- | While 'Fixities' and Polarities are not semigroups under disjoint -- union (which might fail), we get a semigroup instance for the -- monadic @Nice (Fixities, Polarities)@ which propagates the first -- error. instance Semigroup (Nice (Fixities, Polarities)) where c1 <> c2 = do (f1, p1) <- c1 (f2, p2) <- c2 f <- plusFixities f1 f2 p <- mergePolarities p1 p2 return (f, p) where mergePolarities p1 p2 | Set.null i = return (Map.union p1 p2) | otherwise = throwError $ MultiplePolarityPragmas (Set.toList i) where i = Set.intersection (Map.keysSet p1) (Map.keysSet p2) instance Monoid (Nice (Fixities, Polarities)) where mempty = return (Map.empty, Map.empty) mappend = (<>) -- | Get the fixities and polarity pragmas from the current block. -- Doesn't go inside modules and where blocks. -- The reason for this is that these declarations have to appear at the same -- level (or possibly outside an abstract or mutual block) as their target -- declaration. fixitiesAndPolarities :: [Declaration] -> Nice (Fixities, Polarities) fixitiesAndPolarities = foldMap $ \ d -> case d of -- These declarations define polarities: Pragma (PolarityPragma _ x occs) -> return (Map.empty, Map.singleton x occs) -- These declarations define fixities: Syntax x syn -> return ( Map.singleton x (Fixity' noFixity syn $ getRange x) , Map.empty ) Infix f xs -> return ( Map.fromList $ for xs $ \ x -> (x, Fixity' f noNotation$ getRange x) , Map.empty ) -- We look into these blocks: Mutual _ ds' -> fixitiesAndPolarities ds' Abstract _ ds' -> fixitiesAndPolarities ds' Private _ _ ds' -> fixitiesAndPolarities ds' InstanceB _ ds' -> fixitiesAndPolarities ds' Macro _ ds' -> fixitiesAndPolarities ds' -- All other declarations are ignored. -- We expand these boring cases to trigger a revisit -- in case the @Declaration@ type is extended in the future. TypeSig {} -> mempty Field {} -> mempty FunClause {} -> mempty DataSig {} -> mempty Data {} -> mempty RecordSig {} -> mempty Record {} -> mempty PatternSyn {} -> mempty Postulate {} -> mempty Primitive {} -> mempty Open {} -> mempty Import {} -> mempty ModuleMacro {} -> mempty Module {} -> mempty UnquoteDecl {} -> mempty UnquoteDef {} -> mempty Pragma {} -> mempty -- The following function is (at the time of writing) only used three -- times: for building Lets, and for printing error messages. -- | (Approximately) convert a 'NiceDeclaration' back to a list of -- 'Declaration's. notSoNiceDeclarations :: NiceDeclaration -> [Declaration] notSoNiceDeclarations d = case d of Axiom _ _ _ _ i rel mp x e -> (case mp of Nothing -> [] Just occs -> [Pragma (PolarityPragma noRange x occs)]) ++ inst i [TypeSig rel x e] NiceField _ _ _ _ i x argt -> [Field i x argt] PrimitiveFunction r _ _ _ x e -> [Primitive r [TypeSig defaultArgInfo x e]] NiceMutual r _ _ ds -> [Mutual r $ concatMap notSoNiceDeclarations ds] NiceModule r _ _ x tel ds -> [Module r x tel ds] NiceModuleMacro r _ x ma o dir -> [ModuleMacro r x ma o dir] NiceOpen r x dir -> [Open r x dir] NiceImport r x as o dir -> [Import r x as o dir] NicePragma _ p -> [Pragma p] NiceRecSig r _ _ _ _ x bs e -> [RecordSig r x bs e] NiceDataSig r _ _ _ _ x bs e -> [DataSig r Inductive x bs e] NiceFunClause _ _ _ _ _ d -> [d] FunSig _ _ _ _ i _ rel tc x e -> inst i [TypeSig rel x e] FunDef _r ds _ _ _ _ _ _ -> ds DataDef r _ _ _ x bs cs -> [Data r Inductive x bs Nothing $ concatMap notSoNiceDeclarations cs] RecDef r _ _ _ x i e c bs ds -> [Record r x i e (unThing <$> c) bs Nothing $ concatMap notSoNiceDeclarations ds] where unThing (ThingWithFixity c _, inst) = (c, inst) NicePatternSyn r _ n as p -> [PatternSyn r n as p] NiceUnquoteDecl r _ _ _ i _ x e -> inst i [UnquoteDecl r x e] NiceUnquoteDef r _ _ _ _ x e -> [UnquoteDef r x e] where inst InstanceDef ds = [InstanceB (getRange ds) ds] inst NotInstanceDef ds = ds -- | Has the 'NiceDeclaration' a field of type 'IsAbstract'? niceHasAbstract :: NiceDeclaration -> Maybe IsAbstract niceHasAbstract d = case d of Axiom{} -> Nothing NiceField _ _ _ a _ _ _ -> Just a PrimitiveFunction _ _ _ a _ _ -> Just a NiceMutual{} -> Nothing NiceModule _ _ a _ _ _ -> Just a NiceModuleMacro{} -> Nothing NiceOpen{} -> Nothing NiceImport{} -> Nothing NicePragma{} -> Nothing NiceRecSig{} -> Nothing NiceDataSig{} -> Nothing NiceFunClause _ _ a _ _ _ -> Just a FunSig{} -> Nothing FunDef _ _ _ a _ _ _ _ -> Just a DataDef _ _ a _ _ _ _ -> Just a RecDef _ _ a _ _ _ _ _ _ _ -> Just a NicePatternSyn{} -> Nothing NiceUnquoteDecl _ _ _ a _ _ _ _ -> Just a NiceUnquoteDef _ _ _ a _ _ _ -> Just a Agda-2.5.3/src/full/Agda/Syntax/Concrete/Generic.hs0000644000000000000000000002161413154613124020074 0ustar0000000000000000{-# LANGUAGE CPP #-} -- | Generic traversal and reduce for concrete syntax, -- in the style of "Agda.Syntax.Internal.Generic". -- -- However, here we use the terminology of 'Data.Traversable'. module Agda.Syntax.Concrete.Generic where import Control.Applicative import Data.Traversable import Data.Monoid import Data.Foldable import Agda.Syntax.Common import Agda.Syntax.Concrete import Agda.Utils.Either #include "undefined.h" import Agda.Utils.Impossible -- | Generic traversals for concrete expressions. -- -- Note: does not go into patterns! class ExprLike a where mapExpr :: (Expr -> Expr) -> a -> a -- ^ This corresponds to 'map'. traverseExpr #if __GLASGOW_HASKELL__ <= 708 :: (Applicative m, Monad m) #else :: Monad m #endif => (Expr -> m Expr) -> a -> m a -- ^ This corresponds to 'mapM'. foldExpr :: Monoid m => (Expr -> m) -> a -> m -- ^ This is a reduce. traverseExpr = __IMPOSSIBLE__ -- TODO: implement! foldExpr = __IMPOSSIBLE__ -- TODO: implement! -- * Instances for things that do not contain expressions. instance ExprLike Name where mapExpr f = id instance ExprLike QName where mapExpr f = id instance ExprLike Bool where mapExpr f = id -- * Instances for functors. instance ExprLike a => ExprLike (Named name a) where mapExpr = fmap . mapExpr traverseExpr = traverse . traverseExpr foldExpr = foldMap . foldExpr instance ExprLike a => ExprLike (Arg a) where -- TODO guilhem mapExpr = fmap . mapExpr traverseExpr = traverse . traverseExpr foldExpr = foldMap . foldExpr instance ExprLike a => ExprLike [a] where mapExpr = fmap . mapExpr traverseExpr = traverse . traverseExpr foldExpr = foldMap . foldExpr instance ExprLike a => ExprLike (Maybe a) where mapExpr = fmap . mapExpr traverseExpr = traverse . traverseExpr foldExpr = foldMap . foldExpr instance ExprLike a => ExprLike (MaybePlaceholder a) where mapExpr = fmap . mapExpr traverseExpr = traverse . traverseExpr foldExpr = foldMap . foldExpr instance (ExprLike a, ExprLike b) => ExprLike (Either a b) where mapExpr f = mapEither (mapExpr f) (mapExpr f) traverseExpr f = traverseEither (traverseExpr f) (traverseExpr f) foldExpr f = either (foldExpr f) (foldExpr f) instance ExprLike a => ExprLike (TypedBinding' a) where mapExpr = fmap . mapExpr traverseExpr = traverse . traverseExpr foldExpr = foldMap . foldExpr instance ExprLike a => ExprLike (RHS' a) where mapExpr = fmap . mapExpr traverseExpr = traverse . traverseExpr foldExpr = foldMap . foldExpr instance ExprLike a => ExprLike (WhereClause' a) where mapExpr = fmap . mapExpr traverseExpr = traverse . traverseExpr foldExpr = foldMap . foldExpr instance (ExprLike a, ExprLike b) => ExprLike (a, b) where mapExpr f (x, y) = (mapExpr f x, mapExpr f y) traverseExpr f (x, y) = (,) <$> traverseExpr f x <*> traverseExpr f y foldExpr f (x, y) = foldExpr f x `mappend` foldExpr f y instance (ExprLike a, ExprLike b, ExprLike c) => ExprLike (a, b, c) where mapExpr f (x, y, z) = (mapExpr f x, mapExpr f y, mapExpr f z) traverseExpr f (x, y, z) = (,,) <$> traverseExpr f x <*> traverseExpr f y <*> traverseExpr f z foldExpr f (x, y, z) = foldExpr f x `mappend` foldExpr f y `mappend` foldExpr f z instance (ExprLike a, ExprLike b, ExprLike c, ExprLike d) => ExprLike (a, b, c, d) where mapExpr f (x, y, z, w) = (mapExpr f x, mapExpr f y, mapExpr f z, mapExpr f w) traverseExpr f (x, y, z, w) = (,,,) <$> traverseExpr f x <*> traverseExpr f y <*> traverseExpr f z <*> traverseExpr f w foldExpr f (x, y, z, w) = foldExpr f x `mappend` foldExpr f y `mappend` foldExpr f z `mappend` foldExpr f w -- * Interesting instances instance ExprLike Expr where mapExpr f e0 = case e0 of Ident{} -> f $ e0 Lit{} -> f $ e0 QuestionMark{} -> f $ e0 Underscore{} -> f $ e0 RawApp r es -> f $ RawApp r $ mapE es App r e es -> f $ App r (mapE e) $ mapE es OpApp r q ns es -> f $ OpApp r q ns $ mapE es WithApp r e es -> f $ WithApp r (mapE e) $ mapE es HiddenArg r e -> f $ HiddenArg r $ mapE e InstanceArg r e -> f $ InstanceArg r $ mapE e Lam r bs e -> f $ Lam r (mapE bs) $ mapE e AbsurdLam{} -> f $ e0 ExtendedLam r cs -> f $ ExtendedLam r $ mapE cs Fun r a b -> f $ Fun r (mapE a) $ mapE b Pi tel e -> f $ Pi (mapE tel) $ mapE e Set{} -> f $ e0 Prop{} -> f $ e0 SetN{} -> f $ e0 Rec r es -> f $ Rec r $ mapE es RecUpdate r e es -> f $ RecUpdate r (mapE e) $ mapE es Let r ds e -> f $ Let r (mapE ds) $ mapE e Paren r e -> f $ Paren r $ mapE e IdiomBrackets r e -> f $ IdiomBrackets r $ mapE e Absurd{} -> f $ e0 As r x e -> f $ As r x $ mapE e Dot r e -> f $ Dot r $ mapE e ETel tel -> f $ ETel $ mapE tel QuoteGoal r x e -> f $ QuoteGoal r x $ mapE e QuoteContext r -> f $ e0 Tactic r e es -> f $ Tactic r (mapE e) $ mapE es Quote{} -> f $ e0 QuoteTerm{} -> f $ e0 Unquote{} -> f $ e0 DontCare e -> f $ DontCare $ mapE e Equal{} -> f $ e0 where mapE e = mapExpr f e instance ExprLike FieldAssignment where mapExpr f (FieldAssignment x e) = FieldAssignment x (mapExpr f e) traverseExpr f (FieldAssignment x e) = (\e' -> FieldAssignment x e') <$> traverseExpr f e foldExpr f (FieldAssignment _ e) = foldExpr f e instance ExprLike ModuleAssignment where mapExpr f (ModuleAssignment m es i) = ModuleAssignment m (mapExpr f es) i traverseExpr f (ModuleAssignment m es i) = (\es' -> ModuleAssignment m es' i) <$> traverseExpr f es foldExpr f (ModuleAssignment m es i) = foldExpr f es instance ExprLike a => ExprLike (OpApp a) where mapExpr f e0 = case e0 of SyntaxBindingLambda r bs e -> SyntaxBindingLambda r (mapE bs) $ mapE e Ordinary e -> Ordinary $ mapE e where mapE e = mapExpr f e instance ExprLike LamBinding where mapExpr f e0 = case e0 of DomainFree{} -> e0 DomainFull bs -> DomainFull $ mapE bs where mapE e = mapExpr f e instance ExprLike TypedBindings where mapExpr f e0 = case e0 of TypedBindings r b -> TypedBindings r $ mapE b where mapE e = mapExpr f e instance ExprLike LHS where mapExpr f e0 = case e0 of LHS ps wps res wes -> LHS ps wps (mapE res) $ mapE wes Ellipsis r ps res wes -> Ellipsis r ps (mapE res) $ mapE wes where mapE e = mapExpr f e instance ExprLike ModuleApplication where mapExpr f e0 = case e0 of SectionApp r bs e -> SectionApp r (mapE bs) $ mapE e RecordModuleIFS{} -> e0 where mapE e = mapExpr f e instance ExprLike Declaration where mapExpr f e0 = case e0 of TypeSig ai x e -> TypeSig ai x $ mapE e Field i x e -> Field i x $ mapE e FunClause lhs rhs wh ca -> FunClause (mapE lhs) (mapE rhs) (mapE wh) (mapE ca) DataSig r ind x bs e -> DataSig r ind x (mapE bs) $ mapE e Data r ind n bs e cs -> Data r ind n (mapE bs) (mapE e) $ mapE cs RecordSig r ind bs e -> RecordSig r ind (mapE bs) $ mapE e Record r n ind eta c tel e ds -> Record r n ind eta c (mapE tel) (mapE e) $ mapE ds Infix{} -> e0 Syntax{} -> e0 PatternSyn{} -> e0 Mutual r ds -> Mutual r $ mapE ds Abstract r ds -> Abstract r $ mapE ds Private r o ds -> Private r o $ mapE ds InstanceB r ds -> InstanceB r $ mapE ds Macro r ds -> Macro r $ mapE ds Postulate r ds -> Postulate r $ mapE ds Primitive r ds -> Primitive r $ mapE ds Open{} -> e0 Import{} -> e0 ModuleMacro r n es op dir -> ModuleMacro r n (mapE es) op dir Module r n tel ds -> Module r n (mapE tel) $ mapE ds UnquoteDecl r x e -> UnquoteDecl r x (mapE e) UnquoteDef r x e -> UnquoteDef r x (mapE e) Pragma{} -> e0 where mapE e = mapExpr f e {- Template instance ExprLike a where mapExpr f e0 = case e0 of where mapE e = mapExpr f e -} Agda-2.5.3/src/full/Agda/Syntax/Concrete/Name.hs0000644000000000000000000002734613154613124017410 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-| Names in the concrete syntax are just strings (or lists of strings for qualified names). -} module Agda.Syntax.Concrete.Name where import Control.DeepSeq import Control.Applicative import Data.ByteString.Char8 (ByteString) import Data.Function import qualified Data.List as List import Data.Data (Data) import Data.Typeable (Typeable) import GHC.Generics (Generic) import System.FilePath import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Utils.FileName import Agda.Utils.Pretty import Agda.Utils.Size #include "undefined.h" import Agda.Utils.Impossible {-| A name is a non-empty list of alternating 'Id's and 'Hole's. A normal name is represented by a singleton list, and operators are represented by a list with 'Hole's where the arguments should go. For instance: @[Hole,Id "+",Hole]@ is infix addition. Equality and ordering on @Name@s are defined to ignore range so same names in different locations are equal. -} data Name = Name Range [NamePart] -- ^ A (mixfix) identifier. | NoName Range NameId -- ^ @_@. deriving (Typeable, Data) instance Underscore Name where underscore = NoName noRange __IMPOSSIBLE__ isUnderscore NoName{} = True isUnderscore (Name _ [Id x]) = isUnderscore x isUnderscore _ = False -- | Mixfix identifiers are composed of words and holes, -- e.g. @_+_@ or @if_then_else_@ or @[_/_]@. data NamePart = Hole -- ^ @_@ part. | Id RawName -- ^ Identifier part. deriving (Typeable, Data, Generic) -- | Define equality on @Name@ to ignore range so same names in different -- locations are equal. -- -- Is there a reason not to do this? -Jeff -- -- No. But there are tons of reasons to do it. For instance, when using -- names as keys in maps you really don't want to have to get the range -- right to be able to do a lookup. -Ulf instance Eq Name where Name _ xs == Name _ ys = xs == ys NoName _ i == NoName _ j = i == j _ == _ = False instance Ord Name where compare (Name _ xs) (Name _ ys) = compare xs ys compare (NoName _ i) (NoName _ j) = compare i j compare (NoName {}) (Name {}) = LT compare (Name {}) (NoName {}) = GT instance Eq NamePart where Hole == Hole = True Id s1 == Id s2 = s1 == s2 _ == _ = False instance Ord NamePart where compare Hole Hole = EQ compare Hole (Id {}) = LT compare (Id {}) Hole = GT compare (Id s1) (Id s2) = compare s1 s2 -- | @QName@ is a list of namespaces and the name of the constant. -- For the moment assumes namespaces are just @Name@s and not -- explicitly applied modules. -- Also assumes namespaces are generative by just using derived -- equality. We will have to define an equality instance to -- non-generative namespaces (as well as having some sort of -- lookup table for namespace names). data QName = Qual Name QName -- ^ @A.rest@. | QName Name -- ^ @x@. deriving (Typeable, Data, Eq, Ord) instance Underscore QName where underscore = QName underscore isUnderscore (QName x) = isUnderscore x isUnderscore Qual{} = False -- | Top-level module names. Used in connection with the file system. -- -- Invariant: The list must not be empty. data TopLevelModuleName = TopLevelModuleName { moduleNameRange :: Range , moduleNameParts :: [String] } deriving (Show, Typeable, Data) instance Eq TopLevelModuleName where (==) = (==) `on` moduleNameParts instance Ord TopLevelModuleName where compare = compare `on` moduleNameParts instance Sized TopLevelModuleName where size = size . moduleNameParts ------------------------------------------------------------------------ -- * Operations on 'Name' and 'NamePart' ------------------------------------------------------------------------ nameToRawName :: Name -> RawName nameToRawName = prettyShow nameParts :: Name -> [NamePart] nameParts (Name _ ps) = ps nameParts (NoName _ _) = [Id "_"] -- To not return an empty list nameStringParts :: Name -> [RawName] nameStringParts n = [ s | Id s <- nameParts n ] -- | Parse a string to parts of a concrete name. -- -- Note: @stringNameParts "_" == [Id "_"] == nameParts NoName{}@ stringNameParts :: String -> [NamePart] stringNameParts "_" = [Id "_"] -- NoName stringNameParts s = loop s where loop "" = [] loop ('_':s) = Hole : loop s loop s | (x, s') <- break (== '_') s = Id (stringToRawName x) : loop s' -- | Number of holes in a 'Name' (i.e., arity of a mixfix-operator). class NumHoles a where numHoles :: a -> Int instance NumHoles [NamePart] where numHoles = length . filter (== Hole) instance NumHoles Name where numHoles NoName{} = 0 numHoles (Name _ parts) = numHoles parts instance NumHoles QName where numHoles (QName x) = numHoles x numHoles (Qual _ x) = numHoles x -- | Is the name an operator? isOperator :: Name -> Bool isOperator (NoName {}) = False isOperator (Name _ ps) = length ps > 1 isHole :: NamePart -> Bool isHole Hole = True isHole _ = False isPrefix, isPostfix, isInfix, isNonfix :: Name -> Bool isPrefix x = not (isHole (head xs)) && isHole (last xs) where xs = nameParts x isPostfix x = isHole (head xs) && not (isHole (last xs)) where xs = nameParts x isInfix x = isHole (head xs) && isHole (last xs) where xs = nameParts x isNonfix x = not (isHole (head xs)) && not (isHole (last xs)) where xs = nameParts x ------------------------------------------------------------------------ -- * Operations on qualified names ------------------------------------------------------------------------ -- | @qualify A.B x == A.B.x@ qualify :: QName -> Name -> QName qualify (QName m) x = Qual m (QName x) qualify (Qual m m') x = Qual m $ qualify m' x -- | @unqualify A.B.x == x@ -- -- The range is preserved. unqualify :: QName -> Name unqualify q = unqualify' q `withRangeOf` q where unqualify' (QName x) = x unqualify' (Qual _ x) = unqualify' x -- | @qnameParts A.B.x = [A, B, x]@ qnameParts :: QName -> [Name] qnameParts (Qual x q) = x : qnameParts q qnameParts (QName x) = [x] -- | Is the name qualified? isQualified :: QName -> Bool isQualified Qual{} = True isQualified QName{} = False ------------------------------------------------------------------------ -- * Operations on 'TopLevelModuleName' ------------------------------------------------------------------------ -- | Turns a qualified name into a 'TopLevelModuleName'. The qualified -- name is assumed to represent a top-level module name. toTopLevelModuleName :: QName -> TopLevelModuleName toTopLevelModuleName q = TopLevelModuleName (getRange q) $ map prettyShow $ qnameParts q -- UNUSED -- -- | Turns a top level module into a qualified name with 'noRange'. -- fromTopLevelModuleName :: TopLevelModuleName -> QName -- fromTopLevelModuleName (TopLevelModuleName _ []) = __IMPOSSIBLE__ -- fromTopLevelModuleName (TopLevelModuleName _ (x:xs)) = loop x xs -- where -- loop x [] = QName (mk x) -- loop x (y : ys) = Qual (mk x) $ loop y ys -- mk :: String -> Name -- mk x = Name noRange [Id x] -- | Turns a top-level module name into a file name with the given -- suffix. moduleNameToFileName :: TopLevelModuleName -> String -> FilePath moduleNameToFileName (TopLevelModuleName _ []) ext = __IMPOSSIBLE__ moduleNameToFileName (TopLevelModuleName _ ms) ext = joinPath (init ms) last ms <.> ext -- | Finds the current project's \"root\" directory, given a project -- file and the corresponding top-level module name. -- -- Example: If the module \"A.B.C\" is located in the file -- \"/foo/A/B/C.agda\", then the root is \"/foo/\". -- -- Precondition: The module name must be well-formed. projectRoot :: AbsolutePath -> TopLevelModuleName -> AbsolutePath projectRoot file (TopLevelModuleName _ m) = mkAbsolute $ foldr (.) id (replicate (length m - 1) takeDirectory) $ takeDirectory $ filePath file ------------------------------------------------------------------------ -- * No name stuff ------------------------------------------------------------------------ -- | @noName_ = 'noName' 'noRange'@ noName_ :: Name noName_ = noName noRange noName :: Range -> Name noName r = NoName r (NameId 0 0) -- | Check whether a name is the empty name "_". class IsNoName a where isNoName :: a -> Bool instance IsNoName String where isNoName = isUnderscore instance IsNoName ByteString where isNoName = isUnderscore instance IsNoName Name where isNoName (NoName _ _) = True isNoName (Name _ [Hole]) = True -- TODO: Track down where these come from isNoName (Name _ []) = True isNoName (Name _ [Id x]) = isNoName x isNoName _ = False instance IsNoName QName where isNoName (QName x) = isNoName x isNoName Qual{} = False -- M.A._ does not qualify as empty name -- no instance for TopLevelModuleName ------------------------------------------------------------------------ -- * Showing names ------------------------------------------------------------------------ -- deriving instance Show Name -- deriving instance Show NamePart -- deriving instance Show QName -- TODO: 'Show' should output Haskell-parseable representations. -- The following instances are deprecated, and Pretty should be used -- instead. Later, simply derive Show for these types: instance Show Name where show = prettyShow instance Show NamePart where show = prettyShow instance Show QName where show = prettyShow ------------------------------------------------------------------------ -- * Printing names ------------------------------------------------------------------------ instance Pretty Name where pretty (Name _ xs) = hcat $ map pretty xs pretty (NoName _ _) = text $ "_" instance Pretty NamePart where pretty Hole = text $ "_" pretty (Id s) = text $ rawNameToString s instance Pretty QName where pretty (Qual m x) | isUnderscore m = pretty x -- don't print anonymous modules | otherwise = pretty m <> pretty "." <> pretty x pretty (QName x) = pretty x instance Pretty TopLevelModuleName where pretty (TopLevelModuleName _ ms) = text $ List.intercalate "." ms ------------------------------------------------------------------------ -- * Range instances ------------------------------------------------------------------------ instance HasRange Name where getRange (Name r ps) = r getRange (NoName r _) = r instance HasRange QName where getRange (QName x) = getRange x getRange (Qual n x) = fuseRange n x instance HasRange TopLevelModuleName where getRange = moduleNameRange instance SetRange Name where setRange r (Name _ ps) = Name r ps setRange r (NoName _ i) = NoName r i instance SetRange QName where setRange r (QName x) = QName (setRange r x) setRange r (Qual n x) = Qual (setRange r n) (setRange r x) instance SetRange TopLevelModuleName where setRange r (TopLevelModuleName _ x) = TopLevelModuleName r x instance KillRange QName where killRange (QName x) = QName $ killRange x killRange (Qual n x) = killRange n `Qual` killRange x instance KillRange Name where killRange (Name r ps) = Name (killRange r) ps killRange (NoName r i) = NoName (killRange r) i instance KillRange TopLevelModuleName where killRange (TopLevelModuleName _ x) = TopLevelModuleName noRange x ------------------------------------------------------------------------ -- * NFData instances ------------------------------------------------------------------------ -- | Ranges are not forced. instance NFData Name where rnf (Name _ ns) = rnf ns rnf (NoName _ n) = rnf n instance NFData NamePart where rnf Hole = () rnf (Id s) = rnf s instance NFData QName where rnf (Qual a b) = rnf a `seq` rnf b rnf (QName a) = rnf a Agda-2.5.3/src/full/Agda/Syntax/Concrete/Operators/0000755000000000000000000000000013154613124020136 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Syntax/Concrete/Operators/Parser.hs0000644000000000000000000003070213154613124021730 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE TypeFamilies #-} module Agda.Syntax.Concrete.Operators.Parser where import Control.Applicative import Data.Either import Data.Hashable import Data.Maybe import qualified Data.Strict.Maybe as Strict import Data.Set (Set) import Agda.Syntax.Position import qualified Agda.Syntax.Abstract.Name as A import Agda.Syntax.Common import Agda.Syntax.Fixity import Agda.Syntax.Notation import Agda.Syntax.Concrete import Agda.Syntax.Concrete.Operators.Parser.Monad hiding (parse) import qualified Agda.Syntax.Concrete.Operators.Parser.Monad as P import Agda.Utils.Pretty #include "undefined.h" import Agda.Utils.Impossible placeholder :: PositionInName -> Parser e (MaybePlaceholder e) placeholder p = doc (text ("_" ++ show p)) $ sat $ \t -> case t of Placeholder p' | p' == p -> True _ -> False maybePlaceholder :: Maybe PositionInName -> Parser e e -> Parser e (MaybePlaceholder e) maybePlaceholder mp p = case mp of Nothing -> p' Just h -> placeholder h <|> p' where p' = noPlaceholder <$> p satNoPlaceholder :: (e -> Maybe a) -> Parser e a satNoPlaceholder p = sat' $ \tok -> case tok of NoPlaceholder _ e -> p e Placeholder _ -> Nothing data ExprView e = LocalV QName | WildV e | OtherV e | AppV e (NamedArg e) | OpAppV QName (Set A.Name) [NamedArg (MaybePlaceholder (OpApp e))] -- ^ The 'QName' is possibly ambiguous, but it must correspond -- to one of the names in the set. | HiddenArgV (Named_ e) | InstanceArgV (Named_ e) | LamV [LamBinding] e | ParenV e -- deriving (Show) class HasRange e => IsExpr e where exprView :: e -> ExprView e unExprView :: ExprView e -> e instance IsExpr e => HasRange (ExprView e) where getRange = getRange . unExprView instance IsExpr Expr where exprView e = case e of Ident x -> LocalV x App _ e1 e2 -> AppV e1 e2 OpApp r d ns es -> OpAppV d ns es HiddenArg _ e -> HiddenArgV e InstanceArg _ e -> InstanceArgV e Paren _ e -> ParenV e Lam _ bs e -> LamV bs e Underscore{} -> WildV e _ -> OtherV e unExprView e = case e of LocalV x -> Ident x AppV e1 e2 -> App (fuseRange e1 e2) e1 e2 OpAppV d ns es -> OpApp (fuseRange d es) d ns es HiddenArgV e -> HiddenArg (getRange e) e InstanceArgV e -> InstanceArg (getRange e) e ParenV e -> Paren (getRange e) e LamV bs e -> Lam (fuseRange bs e) bs e WildV e -> e OtherV e -> e instance IsExpr Pattern where exprView e = case e of IdentP x -> LocalV x AppP e1 e2 -> AppV e1 e2 OpAppP r d ns es -> OpAppV d ns ((map . fmap . fmap) (noPlaceholder . Ordinary) es) HiddenP _ e -> HiddenArgV e InstanceP _ e -> InstanceArgV e ParenP _ e -> ParenV e WildP{} -> WildV e _ -> OtherV e unExprView e = case e of LocalV x -> IdentP x AppV e1 e2 -> AppP e1 e2 OpAppV d ns es -> let ess :: [NamedArg Pattern] ess = (map . fmap . fmap) (\x -> case x of Placeholder{} -> __IMPOSSIBLE__ NoPlaceholder _ x -> fromOrdinary __IMPOSSIBLE__ x) es in OpAppP (fuseRange d ess) d ns ess HiddenArgV e -> HiddenP (getRange e) e InstanceArgV e -> InstanceP (getRange e) e ParenV e -> ParenP (getRange e) e LamV _ _ -> __IMPOSSIBLE__ WildV e -> e OtherV e -> e -- | Should sections be parsed? data ParseSections = ParseSections | DoNotParseSections deriving (Eq, Show) -- | Runs a parser. If sections should be parsed, then identifiers -- with at least two name parts are split up into multiple tokens, -- using 'PositionInName' to record the tokens' original positions -- within their respective identifiers. parse :: IsExpr e => (ParseSections, Parser e a) -> [e] -> [a] parse (DoNotParseSections, p) es = P.parse p (map noPlaceholder es) parse (ParseSections, p) es = P.parse p (concat $ map splitExpr es) where splitExpr :: IsExpr e => e -> [MaybePlaceholder e] splitExpr e = case exprView e of LocalV n -> splitName n _ -> noSplit where noSplit = [noPlaceholder e] splitName n = case last ns of Name r ps@(_ : _ : _) -> splitParts r (init ns) Beginning ps _ -> noSplit where ns = qnameParts n -- Note that the same range is used for every name part. This is -- not entirely correct, but will hopefully not lead to any -- problems. -- Note also that the module qualifier, if any, is only applied -- to the first name part. splitParts _ _ _ [] = __IMPOSSIBLE__ splitParts _ _ _ (Hole : []) = [Placeholder End] splitParts r m _ (Id s : []) = [part r m End s] splitParts r m w (Hole : ps) = Placeholder w : splitParts r m Middle ps splitParts r m w (Id s : ps) = part r m w s : splitParts r [] Middle ps part r m w s = NoPlaceholder (Strict.Just w) (unExprView $ LocalV $ foldr Qual (QName (Name r [Id s])) m) --------------------------------------------------------------------------- -- * Parser combinators --------------------------------------------------------------------------- ---------------------------- -- Specific combinators -- | Parse a specific identifier as a NamePart partP :: IsExpr e => [Name] -> RawName -> Parser e Range partP ms s = doc (text (show str)) $ satNoPlaceholder isLocal where str = prettyShow $ foldr Qual (QName $ Name noRange [Id s]) ms isLocal e = case exprView e of LocalV y | str == prettyShow y -> Just $ getRange y _ -> Nothing -- | Parses a split-up, unqualified name consisting of at least two -- name parts. -- -- The parser does not check that underscores and other name parts -- alternate. The range of the resulting name is the range of the -- first name part that is not an underscore. atLeastTwoParts :: IsExpr e => Parser e Name atLeastTwoParts = (\p1 ps p2 -> let all = p1 : ps ++ [p2] in case catMaybes (map fst all) of r : _ -> Name r (map snd all) [] -> __IMPOSSIBLE__) <$> part Beginning <*> many (part Middle) <*> part End where part pos = sat' $ \tok -> case tok of Placeholder pos' | pos == pos' -> Just ( Nothing , Hole ) NoPlaceholder (Strict.Just pos') e | pos == pos' -> case exprView e of LocalV (QName (Name r [Id s])) -> Just (Just r, Id s) _ -> Nothing _ -> Nothing -- | Either a wildcard (@_@), or an unqualified name (possibly -- containing multiple name parts). wildOrUnqualifiedName :: IsExpr e => Parser e (Maybe Name) wildOrUnqualifiedName = (Nothing <$ partP [] "_") <|> (satNoPlaceholder $ \e -> case exprView e of LocalV (QName n) -> Just (Just n) WildV _ -> Just Nothing _ -> Nothing) <|> Just <$> atLeastTwoParts -- | Used to define the return type of 'opP'. type family OperatorType (k :: NotationKind) (e :: *) :: * type instance OperatorType 'InfixNotation e = MaybePlaceholder e -> MaybePlaceholder e -> e type instance OperatorType 'PrefixNotation e = MaybePlaceholder e -> e type instance OperatorType 'PostfixNotation e = MaybePlaceholder e -> e type instance OperatorType 'NonfixNotation e = e -- | A singleton type for 'NotationKind' (except for the constructor -- 'NoNotation'). data NK (k :: NotationKind) :: * where In :: NK 'InfixNotation Pre :: NK 'PrefixNotation Post :: NK 'PostfixNotation Non :: NK 'NonfixNotation -- | Parse the \"operator part\" of the given notation. -- -- Normal holes (but not binders) at the beginning and end are -- ignored. -- -- If the notation does not contain any binders, then a section -- notation is allowed. opP :: forall e k. IsExpr e => ParseSections -> Parser e e -> NewNotation -> NK k -> Parser e (OperatorType k e) opP parseSections p (NewNotation q names _ syn isOp) kind = flip fmap (worker (init $ qnameParts q) withoutExternalHoles) $ \(range, hs) -> let (normal, binders) = partitionEithers hs lastHole = maximum $ mapMaybe holeTarget syn app :: ([(MaybePlaceholder e, NamedArg Int)] -> [(MaybePlaceholder e, NamedArg Int)]) -> e app f = -- If we have an operator and there is exactly one -- placeholder for every hole, then we only return -- the operator. if isOp && noPlaceholders args == lastHole + 1 then -- Note that the information in the set "names" is thrown -- away here. unExprView (LocalV q') else unExprView (OpAppV q' names args) where args = map (findExprFor (f normal) binders) [0..lastHole] q' = setRange range q in case kind of In -> \x y -> app (\es -> (x, leadingHole) : es ++ [(y, trailingHole)]) Pre -> \ y -> app (\es -> es ++ [(y, trailingHole)]) Post -> \x -> app (\es -> (x, leadingHole) : es) Non -> app (\es -> es) where (leadingHoles, syn1) = span isNormalHole syn (trailingHoles, syn2) = span isNormalHole (reverse syn1) withoutExternalHoles = reverse syn2 leadingHole = case leadingHoles of [NormalHole h] -> h _ -> __IMPOSSIBLE__ trailingHole = case trailingHoles of [NormalHole h] -> h _ -> __IMPOSSIBLE__ worker :: [Name] -> Notation -> Parser e (Range, [Either (MaybePlaceholder e, NamedArg Int) (LamBinding, Int)]) worker ms [] = pure (noRange, []) worker ms (IdPart x : xs) = (\r1 (r2, es) -> (fuseRanges r1 r2, es)) <$> partP ms x <*> worker [] xs -- Only the first part is qualified. worker ms (NormalHole h : xs) = (\e (r, es) -> (r, Left (e, h) : es)) <$> maybePlaceholder (if isOp && parseSections == ParseSections then Just Middle else Nothing) p <*> worker ms xs worker ms (WildHole h : xs) = (\(r, es) -> (r, Right (mkBinding h $ Name noRange [Hole]) : es)) <$> worker ms xs worker ms (BindHole h : xs) = do (\e (r, es) -> let x = case e of Just name -> name Nothing -> Name noRange [Hole] in (r, Right (mkBinding h x) : es)) -- Andreas, 2011-04-07 put just 'Relevant' here, is this -- correct? <$> wildOrUnqualifiedName <*> worker ms xs mkBinding h x = (DomainFree defaultArgInfo $ mkBoundName_ x, h) set x arg = fmap (fmap (const x)) arg findExprFor :: [(MaybePlaceholder e, NamedArg Int)] -> [(LamBinding, Int)] -> Int -> NamedArg (MaybePlaceholder (OpApp e)) findExprFor normalHoles binders n = case [ h | h@(_, m) <- normalHoles, namedArg m == n ] of [(Placeholder p, arg)] -> set (Placeholder p) arg [(NoPlaceholder _ e, arg)] -> case [b | (b, m) <- binders, m == n] of [] -> set (noPlaceholder (Ordinary e)) arg -- no variable to bind bs -> set (noPlaceholder (SyntaxBindingLambda (fuseRange bs e) bs e)) arg _ -> __IMPOSSIBLE__ noPlaceholders :: [NamedArg (MaybePlaceholder (OpApp e))] -> Int noPlaceholders = sum . map (isPlaceholder . namedArg) where isPlaceholder NoPlaceholder{} = 0 isPlaceholder Placeholder{} = 1 argsP :: IsExpr e => Parser e e -> Parser e [NamedArg e] argsP p = many (mkArg <$> p) where mkArg e = case exprView e of HiddenArgV e -> hide (defaultArg e) InstanceArgV e -> makeInstance (defaultArg e) _ -> defaultArg (unnamed e) appP :: IsExpr e => Parser e e -> Parser e [NamedArg e] -> Parser e e appP p pa = foldl app <$> p <*> pa where app e = unExprView . AppV e atomP :: IsExpr e => (QName -> Bool) -> Parser e e atomP p = doc (text "") $ satNoPlaceholder $ \e -> case exprView e of LocalV x | not (p x) -> Nothing _ -> Just e Agda-2.5.3/src/full/Agda/Syntax/Concrete/Operators/Parser/0000755000000000000000000000000013154613124021372 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Syntax/Concrete/Operators/Parser/Monad.hs0000644000000000000000000000463613154613124022775 0ustar0000000000000000------------------------------------------------------------------------ -- | The parser monad used by the operator parser ------------------------------------------------------------------------ {-# LANGUAGE CPP #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE Rank2Types #-} module Agda.Syntax.Concrete.Operators.Parser.Monad ( MemoKey(..) , Parser , parse , sat' , sat , doc , memoise , memoiseIfPrinting , grammar ) where import Data.Hashable import GHC.Generics (Generic) import Text.PrettyPrint.HughesPJ import Agda.Syntax.Common import qualified Agda.Utils.Parser.MemoisedCPS as Parser -- | Memoisation keys. data MemoKey = NodeK (Either Integer Integer) | PostLeftsK (Either Integer Integer) | PreRightsK (Either Integer Integer) | TopK | AppK | NonfixK deriving (Eq, Show, Generic) instance Hashable MemoKey -- | The parser monad. type Parser tok a = #ifdef DEBUG Parser.ParserWithGrammar #else Parser.Parser #endif MemoKey tok (MaybePlaceholder tok) a -- | Runs the parser. parse :: forall tok a. Parser tok a -> [MaybePlaceholder tok] -> [a] parse = Parser.parse -- | Parses a token satisfying the given predicate. The computed value -- is returned. sat' :: (MaybePlaceholder tok -> Maybe a) -> Parser tok a sat' = Parser.sat' -- | Parses a token satisfying the given predicate. sat :: (MaybePlaceholder tok -> Bool) -> Parser tok (MaybePlaceholder tok) sat = Parser.sat -- | Uses the given document as the printed representation of the -- given parser. The document's precedence is taken to be 'atomP'. doc :: Doc -> Parser tok a -> Parser tok a doc = Parser.doc -- | Memoises the given parser. -- -- Every memoised parser must be annotated with a /unique/ key. -- (Parametrised parsers must use distinct keys for distinct inputs.) memoise :: MemoKey -> Parser tok tok -> Parser tok tok memoise = Parser.memoise -- | Memoises the given parser, but only if printing, not if parsing. -- -- Every memoised parser must be annotated with a /unique/ key. -- (Parametrised parsers must use distinct keys for distinct inputs.) memoiseIfPrinting :: MemoKey -> Parser tok tok -> Parser tok tok memoiseIfPrinting = Parser.memoiseIfPrinting -- | Tries to print the parser, or returns 'empty', depending on the -- implementation. This function might not terminate. grammar :: Parser tok a -> Doc grammar = Parser.grammar Agda-2.5.3/src/full/Agda/Syntax/Scope/0000755000000000000000000000000013154613124015467 5ustar0000000000000000Agda-2.5.3/src/full/Agda/Syntax/Scope/Base.hs0000644000000000000000000011211113154613124016672 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GADTs #-} {-| This module defines the notion of a scope and operations on scopes. -} module Agda.Syntax.Scope.Base where import Prelude hiding (null) import Control.Arrow (first, second, (***)) import Control.Applicative hiding (empty) import Control.DeepSeq import Control.Monad import Data.Either (partitionEithers) import Data.Function import qualified Data.List as List import Data.Map (Map) import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set import Data.Maybe import Data.Typeable (Typeable) import Data.Data (Data) import Agda.Benchmarking import Agda.Syntax.Position import Agda.Syntax.Common import Agda.Syntax.Fixity import Agda.Syntax.Abstract.Name as A import Agda.Syntax.Concrete.Name as C import qualified Agda.Syntax.Concrete as C import Agda.Utils.AssocList (AssocList) import qualified Agda.Utils.AssocList as AssocList import Agda.Utils.Functor import Agda.Utils.Lens import Agda.Utils.List import Agda.Utils.Null import Agda.Utils.Pretty import qualified Agda.Utils.Map as Map #include "undefined.h" import Agda.Utils.Impossible -- * Scope representation -- | A scope is a named collection of names partitioned into public and private -- names. data Scope = Scope { scopeName :: A.ModuleName , scopeParents :: [A.ModuleName] , scopeNameSpaces :: ScopeNameSpaces , scopeImports :: Map C.QName A.ModuleName , scopeDatatypeModule :: Maybe DataOrRecord } deriving (Typeable, Data, Eq, Show) -- | See 'Agda.Syntax.Common.Access'. data NameSpaceId = PrivateNS -- ^ Things not exported by this module. | PublicNS -- ^ Things defined and exported by this module. | ImportedNS -- ^ Things from open public, exported by this module. | OnlyQualifiedNS -- ^ Visible (as qualified) from outside, -- but not exported when opening the module. -- Used for qualified constructors. deriving (Typeable, Data, Eq, Bounded, Enum, Show) type ScopeNameSpaces = [(NameSpaceId, NameSpace)] localNameSpace :: Access -> NameSpaceId localNameSpace PublicAccess = PublicNS localNameSpace PrivateAccess{} = PrivateNS localNameSpace OnlyQualified = OnlyQualifiedNS nameSpaceAccess :: NameSpaceId -> Access nameSpaceAccess PrivateNS = PrivateAccess Inserted nameSpaceAccess _ = PublicAccess -- | Get a 'NameSpace' from 'Scope'. scopeNameSpace :: NameSpaceId -> Scope -> NameSpace scopeNameSpace ns = fromMaybe __IMPOSSIBLE__ . lookup ns . scopeNameSpaces -- | A lens for 'scopeNameSpaces' updateScopeNameSpaces :: (ScopeNameSpaces -> ScopeNameSpaces) -> Scope -> Scope updateScopeNameSpaces f s = s { scopeNameSpaces = f (scopeNameSpaces s) } -- | ``Monadic'' lens (Functor sufficient). updateScopeNameSpacesM :: (Functor m) => (ScopeNameSpaces -> m ScopeNameSpaces) -> Scope -> m Scope updateScopeNameSpacesM f s = for (f $ scopeNameSpaces s) $ \ x -> s { scopeNameSpaces = x } -- | The complete information about the scope at a particular program point -- includes the scope stack, the local variables, and the context precedence. data ScopeInfo = ScopeInfo { scopeCurrent :: A.ModuleName , scopeModules :: Map A.ModuleName Scope , scopeLocals :: LocalVars , scopePrecedence :: PrecedenceStack , scopeInverseName :: Map A.QName [C.QName] , scopeInverseModule :: Map A.ModuleName [C.QName] , scopeInScope :: InScopeSet } deriving (Typeable, Data, Show) instance Eq ScopeInfo where ScopeInfo c1 m1 l1 p1 _ _ _ == ScopeInfo c2 m2 l2 p2 _ _ _ = c1 == c2 && m1 == m2 && l1 == l2 && p1 == p2 -- | Local variables. type LocalVars = AssocList C.Name LocalVar -- | A local variable can be shadowed by an import. -- In case of reference to a shadowed variable, we want to report -- a scope error. data LocalVar = LocalVar { localVar :: A.Name -- ^ Unique ID of local variable. , localLetBound :: Bool -- ^ Flag whether the variable is introduced by a @let@. , localShadowedBy :: [AbstractName] -- ^ If this list is not empty, the local variable is -- shadowed by one or more imports. } deriving (Typeable, Data, Show) instance Eq LocalVar where (==) = (==) `on` localVar instance Ord LocalVar where compare = compare `on` localVar -- | We show shadowed variables as prefixed by a ".", as not in scope. instance Pretty LocalVar where pretty (LocalVar x _ []) = pretty x pretty (LocalVar x _ xs) = text "." <> pretty x -- | Shadow a local name by a non-empty list of imports. shadowLocal :: [AbstractName] -> LocalVar -> LocalVar shadowLocal [] _ = __IMPOSSIBLE__ shadowLocal ys (LocalVar x b zs) = LocalVar x b (ys ++ zs) -- | Project name of unshadowed local variable. notShadowedLocal :: LocalVar -> Maybe A.Name notShadowedLocal (LocalVar x _ []) = Just x notShadowedLocal _ = Nothing -- | Get all locals that are not shadowed __by imports__. notShadowedLocals :: LocalVars -> AssocList C.Name A.Name notShadowedLocals = mapMaybe $ \ (c,x) -> (c,) <$> notShadowedLocal x -- | Lens for 'scopeLocals'. updateScopeLocals :: (LocalVars -> LocalVars) -> ScopeInfo -> ScopeInfo updateScopeLocals f sc = sc { scopeLocals = f (scopeLocals sc) } setScopeLocals :: LocalVars -> ScopeInfo -> ScopeInfo setScopeLocals vars = updateScopeLocals (const vars) mapScopeInfo :: (Scope -> Scope) -> ScopeInfo -> ScopeInfo mapScopeInfo f i = i{ scopeModules = f <$> scopeModules i } ------------------------------------------------------------------------ -- * Name spaces -- -- Map concrete names to lists of abstract names. ------------------------------------------------------------------------ -- | A @NameSpace@ contains the mappings from concrete names that the user can -- write to the abstract fully qualified names that the type checker wants to -- read. data NameSpace = NameSpace { nsNames :: NamesInScope -- ^ Maps concrete names to a list of abstract names. , nsModules :: ModulesInScope -- ^ Maps concrete module names to a list of abstract module names. , nsInScope :: InScopeSet } deriving (Typeable, Data, Eq, Show) type ThingsInScope a = Map C.Name [a] type NamesInScope = ThingsInScope AbstractName type ModulesInScope = ThingsInScope AbstractModule type InScopeSet = Set A.QName -- | Set of types consisting of exactly 'AbstractName' and 'AbstractModule'. -- -- A GADT just for some dependent-types trickery. data InScopeTag a where NameTag :: InScopeTag AbstractName ModuleTag :: InScopeTag AbstractModule -- | Type class for some dependent-types trickery. class Eq a => InScope a where inScopeTag :: InScopeTag a instance InScope AbstractName where inScopeTag = NameTag instance InScope AbstractModule where inScopeTag = ModuleTag -- | @inNameSpace@ selects either the name map or the module name map from -- a 'NameSpace'. What is selected is determined by result type -- (using the dependent-type trickery). inNameSpace :: forall a. InScope a => NameSpace -> ThingsInScope a inNameSpace = case inScopeTag :: InScopeTag a of NameTag -> nsNames ModuleTag -> nsModules ------------------------------------------------------------------------ -- * Decorated names -- -- - What kind of name? (defined, constructor...) -- - Where does the name come from? (to explain to user) ------------------------------------------------------------------------ -- | For the sake of parsing left-hand sides, we distinguish -- constructor and record field names from defined names. data KindOfName = ConName -- ^ Constructor name. | FldName -- ^ Record field name. | DefName -- ^ Ordinary defined name. | PatternSynName -- ^ Name of a pattern synonym. | MacroName -- ^ Name of a macro | QuotableName -- ^ A name that can only be quoted. deriving (Eq, Show, Typeable, Data, Enum, Bounded) -- | A list containing all name kinds. allKindsOfNames :: [KindOfName] allKindsOfNames = [minBound..maxBound] -- | Where does a name come from? -- -- This information is solely for reporting to the user, -- see 'Agda.Interaction.InteractionTop.whyInScope'. data WhyInScope = Defined -- ^ Defined in this module. | Opened C.QName WhyInScope -- ^ Imported from another module. | Applied C.QName WhyInScope -- ^ Imported by a module application. deriving (Typeable, Data, Show) -- | A decoration of 'Agda.Syntax.Abstract.Name.QName'. data AbstractName = AbsName { anameName :: A.QName -- ^ The resolved qualified name. , anameKind :: KindOfName -- ^ The kind (definition, constructor, record field etc.). , anameLineage :: WhyInScope -- ^ Explanation where this name came from. } deriving (Typeable, Data, Show) -- | A decoration of abstract syntax module names. data AbstractModule = AbsModule { amodName :: A.ModuleName -- ^ The resolved module name. , amodLineage :: WhyInScope -- ^ Explanation where this name came from. } deriving (Typeable, Data, Show) instance Eq AbstractName where (==) = (==) `on` anameName instance Ord AbstractName where compare = compare `on` anameName -- | Van Laarhoven lens on 'anameName'. lensAnameName :: Functor m => (A.QName -> m A.QName) -> AbstractName -> m AbstractName lensAnameName f am = f (anameName am) <&> \ m -> am { anameName = m } instance Eq AbstractModule where (==) = (==) `on` amodName instance Ord AbstractModule where compare = compare `on` amodName -- | Van Laarhoven lens on 'amodName'. lensAmodName :: Functor m => (A.ModuleName -> m A.ModuleName) -> AbstractModule -> m AbstractModule lensAmodName f am = f (amodName am) <&> \ m -> am { amodName = m } -- * Operations on name and module maps. mergeNames :: Eq a => ThingsInScope a -> ThingsInScope a -> ThingsInScope a mergeNames = Map.unionWith List.union ------------------------------------------------------------------------ -- * Operations on name spaces ------------------------------------------------------------------------ -- | The empty name space. emptyNameSpace :: NameSpace emptyNameSpace = NameSpace Map.empty Map.empty Set.empty -- | Map functions over the names and modules in a name space. mapNameSpace :: (NamesInScope -> NamesInScope ) -> (ModulesInScope -> ModulesInScope) -> (InScopeSet -> InScopeSet ) -> NameSpace -> NameSpace mapNameSpace fd fm fs ns = ns { nsNames = fd $ nsNames ns , nsModules = fm $ nsModules ns , nsInScope = fs $ nsInScope ns } -- | Zip together two name spaces. zipNameSpace :: (NamesInScope -> NamesInScope -> NamesInScope ) -> (ModulesInScope -> ModulesInScope -> ModulesInScope) -> (InScopeSet -> InScopeSet -> InScopeSet ) -> NameSpace -> NameSpace -> NameSpace zipNameSpace fd fm fs ns1 ns2 = ns1 { nsNames = nsNames ns1 `fd` nsNames ns2 , nsModules = nsModules ns1 `fm` nsModules ns2 , nsInScope = nsInScope ns1 `fs` nsInScope ns2 } -- | Map monadic function over a namespace. mapNameSpaceM :: Applicative m => (NamesInScope -> m NamesInScope ) -> (ModulesInScope -> m ModulesInScope) -> (InScopeSet -> m InScopeSet ) -> NameSpace -> m NameSpace mapNameSpaceM fd fm fs ns = update ns <$> fd (nsNames ns) <*> fm (nsModules ns) <*> fs (nsInScope ns) where update ns ds ms is = ns { nsNames = ds, nsModules = ms, nsInScope = is } ------------------------------------------------------------------------ -- * General operations on scopes ------------------------------------------------------------------------ -- | The empty scope. emptyScope :: Scope emptyScope = Scope { scopeName = noModuleName , scopeParents = [] , scopeNameSpaces = [ (nsid, emptyNameSpace) | nsid <- [minBound..maxBound] ] , scopeImports = Map.empty , scopeDatatypeModule = Nothing } -- | The empty scope info. emptyScopeInfo :: ScopeInfo emptyScopeInfo = ScopeInfo { scopeCurrent = noModuleName , scopeModules = Map.singleton noModuleName emptyScope , scopeLocals = [] , scopePrecedence = [] , scopeInverseName = Map.empty , scopeInverseModule = Map.empty , scopeInScope = Set.empty } -- | Map functions over the names and modules in a scope. mapScope :: (NameSpaceId -> NamesInScope -> NamesInScope ) -> (NameSpaceId -> ModulesInScope -> ModulesInScope) -> (NameSpaceId -> InScopeSet -> InScopeSet ) -> Scope -> Scope mapScope fd fm fs = updateScopeNameSpaces $ AssocList.mapWithKey mapNS where mapNS acc = mapNameSpace (fd acc) (fm acc) (fs acc) -- | Same as 'mapScope' but applies the same function to all name spaces. mapScope_ :: (NamesInScope -> NamesInScope ) -> (ModulesInScope -> ModulesInScope) -> (InScopeSet -> InScopeSet ) -> Scope -> Scope mapScope_ fd fm fs = mapScope (const fd) (const fm) (const fs) -- | Same as 'mapScope' but applies the function only on the given name space. mapScope' :: NameSpaceId -> (NamesInScope -> NamesInScope ) -> (ModulesInScope -> ModulesInScope) -> (InScopeSet -> InScopeSet ) -> Scope -> Scope mapScope' i fd fm fs = mapScope (\ j -> if i == j then fd else id) (\ j -> if i == j then fm else id) (\ j -> if i == j then fs else id) -- | Map monadic functions over the names and modules in a scope. mapScopeM :: Applicative m => (NameSpaceId -> NamesInScope -> m NamesInScope ) -> (NameSpaceId -> ModulesInScope -> m ModulesInScope) -> (NameSpaceId -> InScopeSet -> m InScopeSet ) -> Scope -> m Scope mapScopeM fd fm fs = updateScopeNameSpacesM $ AssocList.mapWithKeyM mapNS where mapNS acc = mapNameSpaceM (fd acc) (fm acc) (fs acc) -- | Same as 'mapScopeM' but applies the same function to both the public and -- private name spaces. mapScopeM_ :: Applicative m => (NamesInScope -> m NamesInScope ) -> (ModulesInScope -> m ModulesInScope) -> (InScopeSet -> m InScopeSet ) -> Scope -> m Scope mapScopeM_ fd fm fs = mapScopeM (const fd) (const fm) (const fs) -- | Zip together two scopes. The resulting scope has the same name as the -- first scope. zipScope :: (NameSpaceId -> NamesInScope -> NamesInScope -> NamesInScope ) -> (NameSpaceId -> ModulesInScope -> ModulesInScope -> ModulesInScope) -> (NameSpaceId -> InScopeSet -> InScopeSet -> InScopeSet ) -> Scope -> Scope -> Scope zipScope fd fm fs s1 s2 = s1 { scopeNameSpaces = [ (nsid, zipNS nsid ns1 ns2) | ((nsid, ns1), (nsid', ns2)) <- fromMaybe __IMPOSSIBLE__ $ zipWith' (,) (scopeNameSpaces s1) (scopeNameSpaces s2) , assert (nsid == nsid') ] , scopeImports = Map.union (scopeImports s1) (scopeImports s2) } where assert True = True assert False = __IMPOSSIBLE__ zipNS acc = zipNameSpace (fd acc) (fm acc) (fs acc) -- | Same as 'zipScope' but applies the same function to both the public and -- private name spaces. zipScope_ :: (NamesInScope -> NamesInScope -> NamesInScope ) -> (ModulesInScope -> ModulesInScope -> ModulesInScope) -> (InScopeSet -> InScopeSet -> InScopeSet ) -> Scope -> Scope -> Scope zipScope_ fd fm fs = zipScope (const fd) (const fm) (const fs) -- | Recompute the inScope sets of a scope. recomputeInScopeSets :: Scope -> Scope recomputeInScopeSets = updateScopeNameSpaces (map $ second recomputeInScope) where recomputeInScope ns = ns { nsInScope = Set.unions $ map (Set.fromList . map anameName) $ Map.elems $ nsNames ns } -- | Filter a scope keeping only concrete names matching the predicates. -- The first predicate is applied to the names and the second to the modules. filterScope :: (C.Name -> Bool) -> (C.Name -> Bool) -> Scope -> Scope filterScope pd pm = recomputeInScopeSets . mapScope_ (Map.filterKeys pd) (Map.filterKeys pm) id -- We don't have enough information in the in scope set to do an -- incremental update here, so just recompute it from the name map. -- | Return all names in a scope. allNamesInScope :: InScope a => Scope -> ThingsInScope a allNamesInScope = namesInScope [minBound..maxBound] allNamesInScope' :: InScope a => Scope -> ThingsInScope (a, Access) allNamesInScope' s = foldr1 mergeNames [ map (, nameSpaceAccess ns) <$> namesInScope [ns] s | ns <- [minBound..maxBound] ] -- | Returns the scope's non-private names. exportedNamesInScope :: InScope a => Scope -> ThingsInScope a exportedNamesInScope = namesInScope [PublicNS, ImportedNS, OnlyQualifiedNS] namesInScope :: InScope a => [NameSpaceId] -> Scope -> ThingsInScope a namesInScope ids s = foldr1 mergeNames [ inNameSpace (scopeNameSpace nsid s) | nsid <- ids ] allThingsInScope :: Scope -> NameSpace allThingsInScope = thingsInScope [minBound..maxBound] thingsInScope :: [NameSpaceId] -> Scope -> NameSpace thingsInScope fs s = NameSpace { nsNames = namesInScope fs s , nsModules = namesInScope fs s , nsInScope = Set.unions [ nsInScope $ scopeNameSpace nsid s | nsid <- fs ] } -- | Merge two scopes. The result has the name of the first scope. mergeScope :: Scope -> Scope -> Scope mergeScope = zipScope_ mergeNames mergeNames Set.union -- | Merge a non-empty list of scopes. The result has the name of the first -- scope in the list. mergeScopes :: [Scope] -> Scope mergeScopes [] = __IMPOSSIBLE__ mergeScopes ss = foldr1 mergeScope ss -- * Specific operations on scopes -- | Move all names in a scope to the given name space (except never move from -- Imported to Public). setScopeAccess :: NameSpaceId -> Scope -> Scope setScopeAccess a s = (`updateScopeNameSpaces` s) $ AssocList.mapWithKey $ const . ns where zero = emptyNameSpace one = allThingsInScope s imp = thingsInScope [ImportedNS] s noimp = thingsInScope [PublicNS, PrivateNS, OnlyQualifiedNS] s ns b = case (a, b) of (PublicNS, PublicNS) -> noimp (PublicNS, ImportedNS) -> imp _ | a == b -> one | otherwise -> zero -- | Update a particular name space. setNameSpace :: NameSpaceId -> NameSpace -> Scope -> Scope setNameSpace nsid ns = modifyNameSpace nsid $ const ns -- | Modify a particular name space. modifyNameSpace :: NameSpaceId -> (NameSpace -> NameSpace) -> Scope -> Scope modifyNameSpace nsid f = updateScopeNameSpaces $ AssocList.updateAt nsid f -- | Add names to a scope. addNamesToScope :: NameSpaceId -> C.Name -> [AbstractName] -> Scope -> Scope addNamesToScope acc x ys s = mergeScope s s1 where s1 = setScopeAccess acc $ setNameSpace PublicNS ns emptyScope ns = emptyNameSpace { nsNames = Map.singleton x ys , nsInScope = Set.fromList (map anameName ys) } -- | Add a name to a scope. addNameToScope :: NameSpaceId -> C.Name -> AbstractName -> Scope -> Scope addNameToScope acc x y s = addNamesToScope acc x [y] s -- | Remove a name from a scope. Caution: does not update the nsInScope set. -- This is only used by rebindName and in that case we add the name right -- back (but with a different kind). removeNameFromScope :: NameSpaceId -> C.Name -> Scope -> Scope removeNameFromScope ns x s = mapScope remove (const id) (const id) s where remove ns' | ns' /= ns = id | otherwise = Map.delete x -- | Add a module to a scope. addModuleToScope :: NameSpaceId -> C.Name -> AbstractModule -> Scope -> Scope addModuleToScope acc x m s = mergeScope s s1 where s1 = setScopeAccess acc $ setNameSpace PublicNS ns emptyScope ns = emptyNameSpace { nsModules = Map.singleton x [m] } -- When we get here we cannot have both using and hiding type UsingOrHiding = Either C.Using [C.ImportedName] usingOrHiding :: C.ImportDirective -> UsingOrHiding usingOrHiding i = case (using i, hiding i) of (UseEverything, xs) -> Right xs (u, []) -> Left u _ -> __IMPOSSIBLE__ -- | Apply an 'ImportDirective' to a scope. applyImportDirective :: C.ImportDirective -> Scope -> Scope applyImportDirective dir s = mergeScope usedOrHidden renamed where usedOrHidden = useOrHide (hideLHS (impRenaming dir) $ usingOrHiding dir) s renamed = rename (impRenaming dir) $ useOrHide useRenamedThings s useRenamedThings = Left $ Using $ map renFrom $ impRenaming dir hideLHS :: [C.Renaming] -> UsingOrHiding -> UsingOrHiding hideLHS _ i@(Left _) = i hideLHS ren (Right xs) = Right $ xs ++ map renFrom ren useOrHide :: UsingOrHiding -> Scope -> Scope useOrHide (Right xs) s = filterNames notElem notElem xs s useOrHide (Left (Using xs)) s = filterNames elem elem xs s useOrHide _ _ = __IMPOSSIBLE__ filterNames :: (C.Name -> [C.Name] -> Bool) -> (C.Name -> [C.Name] -> Bool) -> [C.ImportedName] -> Scope -> Scope filterNames pd pm xs = filterScope (`pd` ds) (`pm` ms) where ds = [ x | ImportedName x <- xs ] ms = [ m | ImportedModule m <- xs ] -- Renaming rename :: [C.Renaming] -> Scope -> Scope rename rho = mapScope_ (Map.mapKeys $ ren drho) (Map.mapKeys $ ren mrho) id where (drho, mrho) = partitionEithers $ for rho $ \case Renaming (ImportedName x) (ImportedName y) _ -> Left (x,y) Renaming (ImportedModule x) (ImportedModule y) _ -> Right (x,y) _ -> __IMPOSSIBLE__ ren r x = fromMaybe x $ lookup x r -- | Rename the abstract names in a scope. renameCanonicalNames :: Map A.QName A.QName -> Map A.ModuleName A.ModuleName -> Scope -> Scope renameCanonicalNames renD renM = mapScope_ renameD renameM (Set.map newName) where newName x = Map.findWithDefault x x renD newMod x = Map.findWithDefault x x renM renameD = Map.map $ map $ over lensAnameName newName renameM = Map.map $ map $ over lensAmodName newMod -- | Remove private name space of a scope. -- -- Should be a right identity for 'exportedNamesInScope'. -- @exportedNamesInScope . restrictPrivate == exportedNamesInScope@. restrictPrivate :: Scope -> Scope restrictPrivate s = setNameSpace PrivateNS emptyNameSpace $ s { scopeImports = Map.empty } -- | Remove private things from the given module from a scope. restrictLocalPrivate :: ModuleName -> Scope -> Scope restrictLocalPrivate m = mapScope' PrivateNS (Map.mapMaybe rName) (Map.mapMaybe rMod) (Set.filter (not . (`isInModule` m))) where check p x = x <$ guard (p x) rName as = check (not . null) $ filter (not . (`isInModule` m) . anameName) as rMod as = check (not . null) $ filter (not . (`isSubModuleOf` m) . amodName) as -- | Remove names that can only be used qualified (when opening a scope) removeOnlyQualified :: Scope -> Scope removeOnlyQualified s = setNameSpace OnlyQualifiedNS emptyNameSpace s -- | Add an explanation to why things are in scope. inScopeBecause :: (WhyInScope -> WhyInScope) -> Scope -> Scope inScopeBecause f = mapScope_ mapName mapMod id where mapName = fmap . map $ \a -> a { anameLineage = f $ anameLineage a } mapMod = fmap . map $ \a -> a { amodLineage = f $ amodLineage a } -- | Get the public parts of the public modules of a scope publicModules :: ScopeInfo -> Map A.ModuleName Scope publicModules scope = Map.filterWithKey (\ m _ -> reachable m) allMods where -- Get all modules in the ScopeInfo. allMods = Map.map restrictPrivate $ scopeModules scope root = scopeCurrent scope modules s = map amodName $ concat $ Map.elems $ allNamesInScope s chase m = m : concatMap chase ms where ms = maybe __IMPOSSIBLE__ modules $ Map.lookup m allMods reachable = (`elem` chase root) publicNames :: ScopeInfo -> Set AbstractName publicNames scope = Set.fromList $ concat $ Map.elems $ exportedNamesInScope $ mergeScopes $ Map.elems $ publicModules scope everythingInScope :: ScopeInfo -> NameSpace everythingInScope scope = allThingsInScope $ mergeScopes $ (s0 :) $ map look $ scopeParents s0 where look m = fromMaybe __IMPOSSIBLE__ $ Map.lookup m $ scopeModules scope s0 = look $ scopeCurrent scope -- | Compute a flattened scope. Only include unqualified names or names -- qualified by modules in the first argument. flattenScope :: [[C.Name]] -> ScopeInfo -> Map C.QName [AbstractName] flattenScope ms scope = Map.unionWith (++) (build ms allNamesInScope root) imported where current = moduleScope $ scopeCurrent scope root = mergeScopes $ current : map moduleScope (scopeParents current) imported = Map.unionsWith (++) [ qual c (build ms' exportedNamesInScope $ moduleScope a) | (c, a) <- Map.toList $ scopeImports root , let -- get the suffixes of c in ms ms' = mapMaybe (List.stripPrefix $ C.qnameParts c) ms , not $ null ms' ] qual c = Map.mapKeys (q c) where q (C.QName x) = C.Qual x q (C.Qual m x) = C.Qual m . q x build :: [[C.Name]] -> (forall a. InScope a => Scope -> ThingsInScope a) -> Scope -> Map C.QName [AbstractName] build ms getNames s = Map.unionsWith (++) $ (Map.mapKeysMonotonic C.QName $ getNames s) : [ Map.mapKeysMonotonic (\ y -> C.Qual x y) $ build ms' exportedNamesInScope $ moduleScope m | (x, mods) <- Map.toList (getNames s) , let ms' = [ tl | hd:tl <- ms, hd == x ] , not $ null ms' , AbsModule m _ <- mods ] moduleScope :: A.ModuleName -> Scope moduleScope m = fromMaybe __IMPOSSIBLE__ $ Map.lookup m $ scopeModules scope -- | Get all concrete names in scope. Includes bound variables. concreteNamesInScope :: ScopeInfo -> Set C.QName concreteNamesInScope scope = Set.unions [ build allNamesInScope root, imported, locals ] where current = moduleScope $ scopeCurrent scope root = mergeScopes $ current : map moduleScope (scopeParents current) locals = Set.fromList [ C.QName x | (x, _) <- scopeLocals scope ] imported = Set.unions [ qual c (build exportedNamesInScope $ moduleScope a) | (c, a) <- Map.toList $ scopeImports root ] qual c = Set.map (q c) where q (C.QName x) = C.Qual x q (C.Qual m x) = C.Qual m . q x build :: (forall a. InScope a => Scope -> ThingsInScope a) -> Scope -> Set C.QName build getNames s = Set.unions $ (Set.fromList $ map C.QName $ Map.keys (getNames s :: ThingsInScope AbstractName)) : [ Set.mapMonotonic (\ y -> C.Qual x y) $ build exportedNamesInScope $ moduleScope m | (x, mods) <- Map.toList (getNames s) , prettyShow x /= "_" , AbsModule m _ <- mods ] moduleScope :: A.ModuleName -> Scope moduleScope m = fromMaybe __IMPOSSIBLE__ $ Map.lookup m $ scopeModules scope -- | Look up a name in the scope scopeLookup :: InScope a => C.QName -> ScopeInfo -> [a] scopeLookup q scope = map fst $ scopeLookup' q scope scopeLookup' :: forall a. InScope a => C.QName -> ScopeInfo -> [(a, Access)] scopeLookup' q scope = List.nubBy ((==) `on` fst) $ findName q root ++ maybeToList topImports ++ imports where -- 1. Finding a name in the current scope and its parents. moduleScope :: A.ModuleName -> Scope moduleScope m = fromMaybe __IMPOSSIBLE__ $ Map.lookup m $ scopeModules scope current :: Scope current = moduleScope $ scopeCurrent scope root :: Scope root = mergeScopes $ current : map moduleScope (scopeParents current) -- | Find a concrete, possibly qualified name in scope @s@. findName :: forall a. InScope a => C.QName -> Scope -> [(a, Access)] findName q0 s = case q0 of C.QName x -> lookupName x s C.Qual x q -> do let -- | Get the modules named @x@ in scope @s@. mods :: [A.ModuleName] mods = amodName . fst <$> lookupName x s -- | Get the definitions named @x@ in scope @s@ and interpret them as modules. -- Andreas, 2013-05-01: Issue 836 debates this feature: -- Qualified constructors are qualified by their datatype rather than a module defs :: [A.ModuleName] defs = mnameFromList . qnameToList . anameName . fst <$> lookupName x s -- Andreas, 2013-05-01: Issue 836 complains about the feature -- that constructors can also be qualified by their datatype -- and projections by their record type. This feature is off -- if we just consider the modules: m <- mods -- The feature is on if we consider also the data and record types: -- trace ("mods ++ defs = " ++ show (mods ++ defs)) $ do -- m <- nub $ mods ++ defs -- record types will appear both as a mod and a def -- Get the scope of module m, if any, and remove its private definitions. let ss = Map.lookup m $ scopeModules scope ss' = restrictPrivate <$> ss -- trace ("ss = " ++ show ss ) $ do -- trace ("ss' = " ++ show ss') $ do s' <- maybeToList ss' findName q s' where lookupName :: forall a. InScope a => C.Name -> Scope -> [(a, Access)] lookupName x s = fromMaybe [] $ Map.lookup x $ allNamesInScope' s -- 2. Finding a name in the top imports. topImports :: Maybe (a, Access) topImports = case (inScopeTag :: InScopeTag a) of NameTag -> Nothing ModuleTag -> first (`AbsModule` Defined) <$> imported q imported :: C.QName -> Maybe (A.ModuleName, Access) imported q = fmap (,PublicAccess) $ Map.lookup q $ scopeImports root -- 3. Finding a name in the imports belonging to an initial part of the qualifier. imports :: [(a, Access)] imports = do (m, x) <- splitName q m <- maybeToList $ fst <$> imported m findName x $ restrictPrivate $ moduleScope m -- return all possible splittings, e.g. -- splitName X.Y.Z = [(X, Y.Z), (X.Y, Z)] splitName :: C.QName -> [(C.QName, C.QName)] splitName (C.QName x) = [] splitName (C.Qual x q) = (C.QName x, q) : [ (C.Qual x m, r) | (m, r) <- splitName q ] -- * Inverse look-up data AllowAmbiguousNames = AmbiguousAnything -- ^ Used for instance arguments to check whether a name is in scope, -- but we do not care whether is is ambiguous | AmbiguousConProjs -- ^ Ambiguous constructors or projections. | AmbiguousNothing deriving (Eq) isNameInScope :: A.QName -> ScopeInfo -> Bool isNameInScope q scope = billToPure [ Scoping, InverseScopeLookup ] $ Set.member q (scopeInScope scope) -- | Find the concrete names that map (uniquely) to a given abstract name. -- Sort by length, shortest first. inverseScopeLookup :: Either A.ModuleName A.QName -> ScopeInfo -> [C.QName] inverseScopeLookup = inverseScopeLookup' AmbiguousConProjs inverseScopeLookup' :: AllowAmbiguousNames -> Either A.ModuleName A.QName -> ScopeInfo -> [C.QName] inverseScopeLookup' amb name scope = billToPure [ Scoping , InverseScopeLookup ] $ -- trace ("importMap = " ++ show importMap) $ -- trace ("moduleMap = " ++ show moduleMap) $ case name of Left m -> best $ filter unambiguousModule $ findModule m Right q -> best $ filter unambiguousName $ findName q where findName x = fromMaybe [] $ Map.lookup x (scopeInverseName scope) findModule x = fromMaybe [] $ Map.lookup x (scopeInverseModule scope) len :: C.QName -> Int len (C.QName _) = 1 len (C.Qual _ x) = 1 + len x best :: [C.QName] -> [C.QName] best = List.sortBy (compare `on` len) unique :: forall a . [a] -> Bool unique [] = __IMPOSSIBLE__ unique [_] = True unique (_:_:_) = False unambiguousModule q = amb == AmbiguousAnything || unique (scopeLookup q scope :: [AbstractModule]) unambiguousName q = amb == AmbiguousAnything || unique xs || amb == AmbiguousConProjs && (all ((ConName ==) . anameKind) xs || all ((FldName ==) . anameKind) xs) where xs = scopeLookup q scope recomputeInverseScopeMaps :: ScopeInfo -> ScopeInfo recomputeInverseScopeMaps scope = billToPure [ Scoping , InverseScopeLookup ] $ scope { scopeInverseName = nameMap , scopeInverseModule = Map.fromList [ (x, findModule x) | x <- Map.keys moduleMap ++ Map.keys importMap ] , scopeInScope = Set.unions $ map (scopeSet . snd) scopes } where this = scopeCurrent scope current = this : scopeParents (moduleScope this) scopes = [ (m, restrict m s) | (m, s) <- Map.toList (scopeModules scope) ] moduleScope :: A.ModuleName -> Scope moduleScope m = fromMaybe __IMPOSSIBLE__ $ Map.lookup m $ scopeModules scope restrict m s | m `elem` current = s | otherwise = restrictPrivate s scopeSet s = Set.unions $ map (namespaceSet . snd) $ scopeNameSpaces s namespaceSet s = nsInScope s internalName :: C.QName -> Bool internalName C.QName{} = False internalName (C.Qual m n) = intern m || internalName n where -- Recognize fresh names created Parser.y intern (C.Name _ [ C.Id ('.' : '#' : _) ]) = True intern _ = False findName :: Ord a => Map a [(A.ModuleName, C.Name)] -> a -> [C.QName] findName table q = do (m, x) <- fromMaybe [] $ Map.lookup q table if m `elem` current then return (C.QName x) else do y <- findModule m let z = C.qualify y x guard $ not $ internalName z return z findModule :: A.ModuleName -> [C.QName] findModule q = findName moduleMap q ++ fromMaybe [] (Map.lookup q importMap) importMap = Map.fromListWith (++) $ do (m, s) <- scopes (x, y) <- Map.toList $ scopeImports s return (y, [x]) moduleMap = Map.fromListWith (++) $ do (m, s) <- scopes (x, ms) <- Map.toList (allNamesInScope s) q <- amodName <$> ms return (q, [(m, x)]) nameMap = Map.fromListWith (++) $ do (m, s) <- scopes (x, ms) <- Map.toList (allNamesInScope s) q <- anameName <$> ms if elem m current then return (q, [C.QName x]) else do y <- findModule m let z = C.qualify y x guard $ not $ internalName z return (q, [z]) -- | Find the concrete names that map (uniquely) to a given abstract qualified name. -- Sort by length, shortest first. inverseScopeLookupName :: A.QName -> ScopeInfo -> [C.QName] inverseScopeLookupName x = inverseScopeLookup (Right x) inverseScopeLookupName' :: AllowAmbiguousNames -> A.QName -> ScopeInfo -> [C.QName] inverseScopeLookupName' ambCon x = inverseScopeLookup' ambCon (Right x) -- | Find the concrete names that map (uniquely) to a given abstract module name. -- Sort by length, shortest first. inverseScopeLookupModule :: A.ModuleName -> ScopeInfo -> [C.QName] inverseScopeLookupModule x = inverseScopeLookup (Left x) ------------------------------------------------------------------------ -- * (Debug) printing ------------------------------------------------------------------------ instance Pretty AbstractName where pretty = pretty . anameName instance Pretty AbstractModule where pretty = pretty . amodName instance Pretty NameSpaceId where pretty = text . \case PublicNS -> "public" PrivateNS -> "private" ImportedNS -> "imported" OnlyQualifiedNS -> "only-qualified" instance Pretty NameSpace where pretty = vcat . prettyNameSpace prettyNameSpace :: NameSpace -> [Doc] prettyNameSpace (NameSpace names mods _) = blockOfLines (text "names") (map pr $ Map.toList names) ++ blockOfLines (text "modules") (map pr $ Map.toList mods) where pr :: (Pretty a, Pretty b) => (a,b) -> Doc pr (x, y) = pretty x <+> text "-->" <+> pretty y instance Pretty Scope where pretty (scope@Scope{ scopeName = name, scopeParents = parents, scopeImports = imps }) = vcat $ [ text "scope" <+> pretty name ] ++ ind ( concat [ blockOfLines (pretty nsid) $ prettyNameSpace $ scopeNameSpace nsid scope | nsid <- [minBound..maxBound] ] ++ blockOfLines (text "imports") (case Map.keys imps of [] -> []; ks -> [ prettyList ks ]) ) where ind = map $ nest 2 -- | Add first string only if list is non-empty. blockOfLines :: Doc -> [Doc] -> [Doc] blockOfLines _ [] = [] blockOfLines hd ss = hd : map (nest 2) ss instance Pretty ScopeInfo where pretty (ScopeInfo this mods locals ctx _ _ _) = vcat $ [ text "ScopeInfo" , text " current = " <> pretty this ] ++ (if null locals then [] else [ text " locals = " <> pretty locals ]) ++ [ text " context = " <> pretty ctx , text " modules" ] ++ map (nest 4) (List.filter (not . null) $ map pretty $ Map.elems mods) ------------------------------------------------------------------------ -- * Boring instances ------------------------------------------------------------------------ instance KillRange ScopeInfo where killRange m = m instance HasRange AbstractName where getRange = getRange . anameName instance SetRange AbstractName where setRange r x = x { anameName = setRange r $ anameName x } Agda-2.5.3/src/full/Agda/Syntax/Scope/Monad.hs0000644000000000000000000007116013154613124017066 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE NondecreasingIndentation #-} {-| The scope monad with operations. -} module Agda.Syntax.Scope.Monad where import Prelude hiding (mapM) import Control.Arrow (first, second, (***)) import Control.Applicative import Control.Monad hiding (mapM, forM) import Control.Monad.Writer hiding (mapM, forM) import Control.Monad.State hiding (mapM, forM) import qualified Data.List as List import Data.Map (Map) import qualified Data.Map as Map import Data.Maybe import Data.Set (Set) import qualified Data.Set as Set import Data.Traversable hiding (for) import Agda.Syntax.Common import Agda.Syntax.Position import Agda.Syntax.Fixity import Agda.Syntax.Abstract.Name as A import qualified Agda.Syntax.Abstract as A import Agda.Syntax.Abstract (ScopeCopyInfo(..), initCopyInfo) import Agda.Syntax.Concrete as C import Agda.Syntax.Scope.Base import Agda.TypeChecking.Monad.Base import Agda.TypeChecking.Monad.Debug import Agda.TypeChecking.Monad.State import Agda.TypeChecking.Monad.Options import qualified Agda.Utils.AssocList as AssocList import Agda.Utils.Function import Agda.Utils.Functor import Agda.Utils.List import Agda.Utils.Maybe import Agda.Utils.Null (unlessNull) import Agda.Utils.Pretty import Agda.Utils.Size import Agda.Utils.Tuple #include "undefined.h" import Agda.Utils.Impossible -- * The scope checking monad -- | To simplify interaction between scope checking and type checking (in -- particular when chasing imports), we use the same monad. type ScopeM = TCM -- * Errors isDatatypeModule :: A.ModuleName -> ScopeM (Maybe DataOrRecord) isDatatypeModule m = do scopeDatatypeModule . Map.findWithDefault __IMPOSSIBLE__ m . scopeModules <$> getScope -- * General operations getCurrentModule :: ScopeM A.ModuleName getCurrentModule = setRange noRange . scopeCurrent <$> getScope setCurrentModule :: A.ModuleName -> ScopeM () setCurrentModule m = modifyScope $ \s -> s { scopeCurrent = m } withCurrentModule :: A.ModuleName -> ScopeM a -> ScopeM a withCurrentModule new action = do old <- getCurrentModule setCurrentModule new x <- action setCurrentModule old return x withCurrentModule' :: (MonadTrans t, Monad (t ScopeM)) => A.ModuleName -> t ScopeM a -> t ScopeM a withCurrentModule' new action = do old <- lift getCurrentModule lift $ setCurrentModule new x <- action lift $ setCurrentModule old return x getNamedScope :: A.ModuleName -> ScopeM Scope getNamedScope m = do scope <- getScope case Map.lookup m (scopeModules scope) of Just s -> return s Nothing -> do reportSLn "" 0 $ "ERROR: In scope\n" ++ prettyShow scope ++ "\nNO SUCH SCOPE " ++ prettyShow m __IMPOSSIBLE__ getCurrentScope :: ScopeM Scope getCurrentScope = getNamedScope =<< getCurrentModule -- | Create a new module with an empty scope. -- (@Just@ if it is a datatype or record module.) createModule :: Maybe DataOrRecord -> A.ModuleName -> ScopeM () createModule b m = do reportSLn "scope.createModule" 10 $ "createModule " ++ prettyShow m s <- getCurrentScope let parents = scopeName s : scopeParents s sm = emptyScope { scopeName = m , scopeParents = parents , scopeDatatypeModule = b } -- Andreas, 2015-07-02: internal error if module is not new. -- Ulf, 2016-02-15: It's not new if multiple imports (#1770). modifyScopes $ Map.insertWith const m sm -- | Apply a function to the scope map. modifyScopes :: (Map A.ModuleName Scope -> Map A.ModuleName Scope) -> ScopeM () modifyScopes f = modifyScope $ \s -> s { scopeModules = f $ scopeModules s } -- | Apply a function to the given scope. modifyNamedScope :: A.ModuleName -> (Scope -> Scope) -> ScopeM () modifyNamedScope m f = modifyScopes $ Map.adjust f m setNamedScope :: A.ModuleName -> Scope -> ScopeM () setNamedScope m s = modifyNamedScope m $ const s -- | Apply a monadic function to the top scope. modifyNamedScopeM :: A.ModuleName -> (Scope -> ScopeM (a, Scope)) -> ScopeM a modifyNamedScopeM m f = do (a, s) <- f =<< getNamedScope m setNamedScope m s return a -- | Apply a function to the current scope. modifyCurrentScope :: (Scope -> Scope) -> ScopeM () modifyCurrentScope f = getCurrentModule >>= (`modifyNamedScope` f) modifyCurrentScopeM :: (Scope -> ScopeM (a, Scope)) -> ScopeM a modifyCurrentScopeM f = getCurrentModule >>= (`modifyNamedScopeM` f) -- | Apply a function to the public or private name space. modifyCurrentNameSpace :: NameSpaceId -> (NameSpace -> NameSpace) -> ScopeM () modifyCurrentNameSpace acc f = modifyCurrentScope $ updateScopeNameSpaces $ AssocList.updateAt acc f pushContextPrecedence :: Precedence -> ScopeM () pushContextPrecedence p = modifyScope_ $ \ s -> s { scopePrecedence = pushPrecedence p $ scopePrecedence s } popContextPrecedence :: ScopeM () popContextPrecedence = modifyScope_ $ \ s -> s { scopePrecedence = drop 1 $ scopePrecedence s } withContextPrecedence :: Precedence -> ScopeM a -> ScopeM a withContextPrecedence p m = do pushContextPrecedence p x <- m popContextPrecedence return x getLocalVars :: ScopeM LocalVars getLocalVars = scopeLocals <$> getScope modifyLocalVars :: (LocalVars -> LocalVars) -> ScopeM () modifyLocalVars = modifyScope_ . updateScopeLocals setLocalVars :: LocalVars -> ScopeM () setLocalVars vars = modifyLocalVars $ const vars -- | Run a computation without changing the local variables. withLocalVars :: ScopeM a -> ScopeM a withLocalVars m = do vars <- getLocalVars x <- m setLocalVars vars return x -- * Names -- | Create a fresh abstract name from a concrete name. -- -- This function is used when we translate a concrete name -- in a binder. The 'Range' of the concrete name is -- saved as the 'nameBindingSite' of the abstract name. freshAbstractName :: Fixity' -> C.Name -> ScopeM A.Name freshAbstractName fx x = do i <- fresh return $ A.Name { nameId = i , nameConcrete = x , nameBindingSite = getRange x , nameFixity = fx } -- | @freshAbstractName_ = freshAbstractName noFixity'@ freshAbstractName_ :: C.Name -> ScopeM A.Name freshAbstractName_ = freshAbstractName noFixity' -- | Create a fresh abstract qualified name. freshAbstractQName :: Fixity' -> C.Name -> ScopeM A.QName freshAbstractQName fx x = do y <- freshAbstractName fx x m <- getCurrentModule return $ A.qualify m y -- * Resolving names data ResolvedName = -- | Local variable bound by λ, Π, module telescope, pattern, @let@. VarName { resolvedVar :: A.Name , resolvedLetBound :: Bool -- ^ Variable bound by @let@? } | -- | Function, data/record type, postulate. DefinedName Access AbstractName | -- | Record field name. Needs to be distinguished to parse copatterns. FieldName [AbstractName] | -- | Data or record constructor name. ConstructorName [AbstractName] | -- | Name of pattern synonym. PatternSynResName AbstractName | -- | Unbound name. UnknownName deriving (Show, Eq) instance Pretty ResolvedName where pretty = \case VarName x _ -> text "variable" <+> pretty x DefinedName a x -> pretty a <+> pretty x FieldName xs -> text "field" <+> pretty xs ConstructorName xs -> text "constructor" <+> pretty xs PatternSynResName x -> text "pattern" <+> pretty x UnknownName -> text "" -- | Look up the abstract name referred to by a given concrete name. resolveName :: C.QName -> ScopeM ResolvedName resolveName = resolveName' allKindsOfNames Nothing -- | Look up the abstract name corresponding to a concrete name of -- a certain kind and/or from a given set of names. -- Sometimes we know already that we are dealing with a constructor -- or pattern synonym (e.g. when we have parsed a pattern). -- Then, we can ignore conflicting definitions of that name -- of a different kind. (See issue 822.) resolveName' :: [KindOfName] -> Maybe (Set A.Name) -> C.QName -> ScopeM ResolvedName resolveName' kinds names x = do scope <- getScope let vars = AssocList.mapKeysMonotonic C.QName $ scopeLocals scope retVar y = return . VarName y{ nameConcrete = unqualify x } aName = A.qnameName . anameName case lookup x vars of -- Case: we have a local variable x. Just (LocalVar y b []) -> retVar y b -- Case: ... but is (perhaps) shadowed by some imports. Just (LocalVar y b ys) -> case names of Nothing -> shadowed ys Just ns -> case filter (\ y -> aName y `Set.member` ns) ys of [] -> retVar y b ys -> shadowed ys where shadowed ys = typeError $ AmbiguousName x $ A.qualify_ y : map anameName ys -- Case: we do not have a local variable x. Nothing -> do -- Consider only names of one of the given kinds let filtKind = filter $ \ y -> anameKind (fst y) `elem` kinds -- Consider only names in the given set of names filtName = filter $ \ y -> maybe True (Set.member (aName (fst y))) names case filtKind $ filtName $ scopeLookup' x scope of [] -> return UnknownName ds | all ((==ConName) . anameKind . fst) ds -> return $ ConstructorName $ map (upd . fst) ds ds | all ((==FldName) . anameKind . fst) ds -> return $ FieldName $ map (upd . fst) ds [(d, a)] | anameKind d == PatternSynName -> return $ PatternSynResName $ upd d [(d, a)] -> return $ DefinedName a $ upd d ds -> typeError $ AmbiguousName x (map (anameName . fst) ds) where upd d = updateConcreteName d $ unqualify x updateConcreteName :: AbstractName -> C.Name -> AbstractName updateConcreteName d@(AbsName { anameName = A.QName qm qn }) x = d { anameName = A.QName (setRange (getRange x) qm) (qn { nameConcrete = x }) } -- | Look up a module in the scope. resolveModule :: C.QName -> ScopeM AbstractModule resolveModule x = do ms <- scopeLookup x <$> getScope case ms of [AbsModule m why] -> return $ AbsModule (m `withRangeOf` x) why [] -> typeError $ NoSuchModule x ms -> typeError $ AmbiguousModule x (map amodName ms) -- | Get the notation of a name. The name is assumed to be in scope. getNotation :: C.QName -> Set A.Name -- ^ The name must correspond to one of the names in this set. -> ScopeM NewNotation getNotation x ns = do r <- resolveName' allKindsOfNames (Just ns) x case r of VarName y _ -> return $ namesToNotation x y DefinedName _ d -> return $ notation d FieldName ds -> return $ oneNotation ds ConstructorName ds -> return $ oneNotation ds PatternSynResName n -> return $ notation n UnknownName -> __IMPOSSIBLE__ where notation = namesToNotation x . qnameName . anameName oneNotation ds = case mergeNotations $ map notation ds of [n] -> n _ -> __IMPOSSIBLE__ -- * Binding names -- | Bind a variable. bindVariable :: Bool -- ^ Let-bound variable? -> C.Name -- ^ Concrete name. -> A.Name -- ^ Abstract name. -> ScopeM () bindVariable b x y = modifyScope_ $ updateScopeLocals $ AssocList.insert x $ LocalVar y b [] -- | Bind a defined name. Must not shadow anything. bindName :: Access -> KindOfName -> C.Name -> A.QName -> ScopeM () bindName acc kind x y = do r <- resolveName (C.QName x) ys <- case r of -- Binding an anonymous declaration always succeeds. -- In case it's not the first one, we simply remove the one that came before UnknownName | isNoName x -> success DefinedName{} | isNoName x -> success <* modifyCurrentScope (removeNameFromScope PrivateNS x) DefinedName _ d -> clash $ anameName d VarName z _ -> clash $ A.qualify (mnameFromList []) z FieldName ds -> ambiguous FldName ds ConstructorName ds -> ambiguous ConName ds PatternSynResName n -> clash $ anameName n UnknownName -> success let ns = if isNoName x then PrivateNS else localNameSpace acc modifyCurrentScope $ addNamesToScope ns x ys where success = return [ AbsName y kind Defined ] clash = typeError . ClashingDefinition (C.QName x) ambiguous k ds@(d:_) = if kind == k && all ((==k) . anameKind) ds then success else clash $ anameName d ambiguous k [] = __IMPOSSIBLE__ -- | Rebind a name. Use with care! -- Ulf, 2014-06-29: Currently used to rebind the name defined by an -- unquoteDecl, which is a 'QuotableName' in the body, but a 'DefinedName' -- later on. rebindName :: Access -> KindOfName -> C.Name -> A.QName -> ScopeM () rebindName acc kind x y = do modifyCurrentScope $ removeNameFromScope (localNameSpace acc) x bindName acc kind x y -- | Bind a module name. bindModule :: Access -> C.Name -> A.ModuleName -> ScopeM () bindModule acc x m = modifyCurrentScope $ addModuleToScope (localNameSpace acc) x (AbsModule m Defined) -- | Bind a qualified module name. Adds it to the imports field of the scope. bindQModule :: Access -> C.QName -> A.ModuleName -> ScopeM () bindQModule acc q m = modifyCurrentScope $ \s -> s { scopeImports = Map.insert q m (scopeImports s) } -- * Module manipulation operations -- | Clear the scope of any no names. stripNoNames :: ScopeM () stripNoNames = modifyScopes $ Map.map $ mapScope_ stripN stripN id where stripN = Map.filterWithKey $ const . not . isNoName type WSM = StateT ScopeMemo ScopeM data ScopeMemo = ScopeMemo { memoNames :: A.Ren A.QName , memoModules :: [(ModuleName, (ModuleName, Bool))] -- ^ Bool: did we copy recursively? We need to track this because we don't -- copy recursively when creating new modules for reexported functions -- (issue1985), but we might need to copy recursively later. } memoToScopeInfo :: ScopeMemo -> ScopeCopyInfo memoToScopeInfo (ScopeMemo names mods) = ScopeCopyInfo { renNames = names , renModules = [ (x, y) | (x, (y, _)) <- mods ] } -- | Create a new scope with the given name from an old scope. Renames -- public names in the old scope to match the new name and returns the -- renamings. copyScope :: C.QName -> A.ModuleName -> Scope -> ScopeM (Scope, ScopeCopyInfo) copyScope oldc new0 s = (inScopeBecause (Applied oldc) *** memoToScopeInfo) <$> runStateT (copy new0 s) (ScopeMemo [] []) where copy :: A.ModuleName -> Scope -> WSM Scope copy new s = do lift $ reportSLn "scope.copy" 20 $ "Copying scope " ++ prettyShow old ++ " to " ++ prettyShow new lift $ reportSLn "scope.copy" 50 $ prettyShow s s0 <- lift $ getNamedScope new -- Delete private names, then copy names and modules. Recompute inScope -- set rather than trying to copy it. s' <- recomputeInScopeSets <$> mapScopeM_ copyD copyM return (setNameSpace PrivateNS emptyNameSpace s) -- Fix name and parent. return $ s' { scopeName = scopeName s0 , scopeParents = scopeParents s0 } where rnew = getRange new new' = killRange new newL = A.mnameToList new' old = scopeName s copyD :: NamesInScope -> WSM NamesInScope copyD = traverse $ mapM $ onName renName copyM :: ModulesInScope -> WSM ModulesInScope copyM = traverse $ mapM $ lensAmodName renMod onName :: (A.QName -> WSM A.QName) -> AbstractName -> WSM AbstractName onName f d = case anameKind d of PatternSynName -> return d -- Pattern synonyms are simply aliased, not renamed _ -> lensAnameName f d -- Adding to memo structure. addName x y = modify $ \ i -> i { memoNames = (x, y) : memoNames i } addMod x y rec = modify $ \ i -> i { memoModules = (x, (y, rec)) : filter ((/= x) . fst) (memoModules i) } -- Querying the memo structure. findName x = lookup x <$> gets memoNames findMod x = lookup x <$> gets memoModules refresh :: A.Name -> WSM A.Name refresh x = do i <- lift fresh return $ x { nameId = i } -- Change a binding M.x -> old.M'.y to M.x -> new.M'.y renName :: A.QName -> WSM A.QName renName x = do -- Issue 1985: For re-exported names we can't use new' as the -- module, since it has the wrong telescope. Example: -- -- module M1 (A : Set) where -- module M2 (B : Set) where -- postulate X : Set -- module M3 (C : Set) where -- module M4 (D E : Set) where -- open M2 public -- -- module M = M1.M3 A C -- -- Here we can't copy M1.M2.X to M.M4.X since we need -- X : (B : Set) → Set, but M.M4 has telescope (D E : Set). Thus, we -- would break the invariant that all functions in a module share the -- module telescope. Instead we copy M1.M2.X to M.M2.X for a fresh -- module M2 that gets the right telescope. m <- case x `isInModule` old of True -> return new' False -> renMod' False (qnameModule x) -- Don't copy recursively here, we only know that the -- current name x should be copied. -- Generate a fresh name for the target. -- Andreas, 2015-08-11 Issue 1619: -- Names copied by a module macro should get the module macro's -- range as declaration range -- (maybe rather the one of the open statement). -- For now, we just set their range -- to the new module name's one, which fixes issue 1619. y <- setRange rnew . A.qualify m <$> refresh (qnameName x) lift $ reportSLn "scope.copy" 50 $ " Copying " ++ prettyShow x ++ " to " ++ prettyShow y addName x y return y -- Change a binding M.x -> old.M'.y to M.x -> new.M'.y renMod :: A.ModuleName -> WSM A.ModuleName renMod = renMod' True renMod' rec x = do -- Andreas, issue 1607: -- If we have already copied this module, return the copy. z <- findMod x case z of Just (y, False) | rec -> y <$ copyRec x y Just (y, _) -> return y Nothing -> do -- Ulf (issue 1985): If copying a reexported module we put it at the -- top-level, to make sure we don't mess up the invariant that all -- (abstract) names M.f share the argument telescope of M. let newM | x `isSubModuleOf` old = newL | otherwise = mnameToList new0 y <- do -- Andreas, Jesper, 2015-07-02: Issue 1597 -- Don't blindly drop a prefix of length of the old qualifier. -- If things are imported by open public they do not have the old qualifier -- as prefix. Those need just to be linked, not copied. -- return $ A.mnameFromList $ (newL ++) $ drop (size old) $ A.mnameToList x -- caseMaybe (stripPrefix (A.mnameToList old) (A.mnameToList x)) (return x) $ \ suffix -> do -- return $ A.mnameFromList $ newL ++ suffix -- Ulf, 2016-02-22: #1726 -- We still need to copy modules from 'open public'. Same as in renName. y <- refresh (last $ A.mnameToList x) return $ A.mnameFromList $ newM ++ [y] -- Andreas, Jesper, 2015-07-02: Issue 1597 -- Don't copy a module over itself, it will just be emptied of its contents. if (x == y) then return x else do lift $ reportSLn "scope.copy" 50 $ " Copying module " ++ prettyShow x ++ " to " ++ prettyShow y addMod x y rec lift $ createModule Nothing y -- We need to copy the contents of included modules recursively (only when 'rec') when rec $ copyRec x y return y where copyRec x y = do s0 <- lift $ getNamedScope x s <- withCurrentModule' y $ copy y s0 lift $ modifyNamedScope y (const s) -- | Apply an import directive and check that all the names mentioned actually -- exist. applyImportDirectiveM :: C.QName -> C.ImportDirective -> Scope -> ScopeM (A.ImportDirective, Scope) applyImportDirectiveM m dir@ImportDirective{ impRenaming = ren, using = u, hiding = h } scope = do -- Translate exported names to abstract syntax. -- Raise error if unsuccessful. caseMaybe mNamesA doesntExport $ \ namesA -> do let extraModules = [ x | ImportedName x <- names, let mx = ImportedModule x, not $ doesntExist mx, notElem mx names ] dir' = addExtraModules extraModules dir dir' <- sanityCheck dir' -- Check for duplicate imports in a single import directive. -- @dup@ : To be imported names that are mentioned more than once. let dup = targetNames List.\\ List.nub targetNames unless (null dup) $ typeError $ DuplicateImports m dup -- Apply the import directive. let scope' = applyImportDirective dir' scope -- Look up the defined names in the new scope. let namesInScope' = (allNamesInScope scope' :: ThingsInScope AbstractName) let modulesInScope' = (allNamesInScope scope' :: ThingsInScope AbstractModule) let look x = head . Map.findWithDefault __IMPOSSIBLE__ x -- We set the ranges to the ranges of the concrete names in order to get -- highlighting for the names in the import directive. let definedA = for definedNames $ \case ImportedName x -> ImportedName . (x,) . setRange (getRange x) . anameName $ look x namesInScope' ImportedModule x -> ImportedModule . (x,) . setRange (getRange x) . amodName $ look x modulesInScope' let adir = mapImportDir namesA definedA dir return (adir, scope') -- TODO Issue 1714: adir where -- | Names in the @using@ directive. usingNames :: [ImportedName] usingNames = case u of Using xs -> xs UseEverything -> [] -- | All names from the imported module mentioned in the import directive. names :: [ImportedName] names = map renFrom ren ++ h ++ usingNames -- | Names defined by the import (targets of renaming). definedNames :: [ImportedName] definedNames = map renTo ren -- | Names to be in scope after import. targetNames :: [ImportedName] targetNames = definedNames ++ usingNames sanityCheck dir = case (using dir, hiding dir) of (Using xs, ys) -> do let uselessHiding = [ x | x@ImportedName{} <- ys ] ++ [ x | x@(ImportedModule y) <- ys, ImportedName y `notElem` names ] unless (null uselessHiding) $ typeError $ GenericError $ "Hiding " ++ List.intercalate ", " (map prettyShow uselessHiding) ++ " has no effect" return dir{ hiding = [] } _ -> return dir addExtraModules :: [C.Name] -> C.ImportDirective -> C.ImportDirective addExtraModules extra dir = dir{ using = case using dir of Using xs -> Using $ concatMap addExtra xs UseEverything -> UseEverything , hiding = concatMap addExtra (hiding dir) , impRenaming = concatMap extraRenaming (impRenaming dir) } where addExtra f@(ImportedName y) | elem y extra = [f, ImportedModule y] addExtra m = [m] extraRenaming r@(Renaming from to rng) = case (from, to) of (ImportedName y, ImportedName z) | elem y extra -> [r, Renaming (ImportedModule y) (ImportedModule z) rng] _ -> [r] -- | Names and modules (abstract) in scope before the import. namesInScope = (allNamesInScope scope :: ThingsInScope AbstractName) modulesInScope = (allNamesInScope scope :: ThingsInScope AbstractModule) -- | AST versions of the concrete names from the imported module. -- @Nothing@ is one of the names is not exported. mNamesA = forM names $ \case ImportedName x -> ImportedName . (x,) . setRange (getRange x) . anameName . head <$> Map.lookup x namesInScope ImportedModule x -> ImportedModule . (x,) . setRange (getRange x) . amodName . head <$> Map.lookup x modulesInScope head = headWithDefault __IMPOSSIBLE__ -- For the sake of the error message, we (re)compute the list of unresolved names. doesntExport = do -- Names @xs@ mentioned in the import directive @dir@ but not in the @scope@. let xs = filter doesntExist names reportSLn "scope.import.apply" 20 $ "non existing names: " ++ prettyShow xs typeError $ ModuleDoesntExport m xs doesntExist (ImportedName x) = isNothing $ Map.lookup x namesInScope doesntExist (ImportedModule x) = isNothing $ Map.lookup x modulesInScope -- | A finite map for @ImportedName@s. lookupImportedName :: (Eq a, Eq b) => ImportedName' a b -> [ImportedName' (a,c) (b,d)] -> ImportedName' c d lookupImportedName (ImportedName x) = loop where loop [] = __IMPOSSIBLE__ loop (ImportedName (y,z) : _) | x == y = ImportedName z loop (_ : ns) = loop ns lookupImportedName (ImportedModule x) = loop where loop [] = __IMPOSSIBLE__ loop (ImportedModule (y,z) : _) | x == y = ImportedModule z loop (_ : ns) = loop ns -- | Translation of @ImportDirective@. mapImportDir :: (Eq a, Eq b) => [ImportedName' (a,c) (b,d)] -- ^ Translation of imported names. -> [ImportedName' (a,c) (b,d)] -- ^ Translation of names defined by this import. -> ImportDirective' a b -> ImportDirective' c d mapImportDir src tgt (ImportDirective r u h ren open) = ImportDirective r (mapUsing src u) (map (`lookupImportedName` src) h) (map (mapRenaming src tgt) ren) open -- | Translation of @Using or Hiding@. mapUsing :: (Eq a, Eq b) => [ImportedName' (a,c) (b,d)] -- ^ Translation of names in @using@ or @hiding@ list. -> Using' a b -> Using' c d mapUsing src UseEverything = UseEverything mapUsing src (Using xs) = Using $ map (`lookupImportedName` src) xs -- | Translation of @Renaming@. mapRenaming :: (Eq a, Eq b) => [ImportedName' (a,c) (b,d)] -- ^ Translation of 'renFrom' names. -> [ImportedName' (a,c) (b,d)] -- ^ Translation of 'rento' names. -> Renaming' a b -> Renaming' c d mapRenaming src tgt (Renaming from to r) = Renaming (lookupImportedName from src) (lookupImportedName to tgt) r -- | Open a module. openModule_ :: C.QName -> C.ImportDirective -> ScopeM A.ImportDirective openModule_ cm dir = do current <- getCurrentModule m <- amodName <$> resolveModule cm let acc | not (publicOpen dir) = PrivateNS | m `isSubModuleOf` current = PublicNS | otherwise = ImportedNS -- Get the scope exported by module to be opened. (adir, s') <- applyImportDirectiveM cm dir . inScopeBecause (Opened cm) . removeOnlyQualified . restrictPrivate =<< getNamedScope m let s = setScopeAccess acc s' let ns = scopeNameSpace acc s checkForClashes ns modifyCurrentScope (`mergeScope` s) -- Importing names might shadow existing locals. verboseS "scope.locals" 10 $ do locals <- mapMaybe (\ (c,x) -> c <$ notShadowedLocal x) <$> getLocalVars let newdefs = Map.keys $ nsNames ns shadowed = List.intersect locals newdefs reportSLn "scope.locals" 10 $ "opening module shadows the following locals vars: " ++ prettyShow shadowed -- Andreas, 2014-09-03, issue 1266: shadow local variables by imported defs. modifyLocalVars $ AssocList.mapWithKey $ \ c x -> case Map.lookup c $ nsNames ns of Nothing -> x Just ys -> shadowLocal ys x return adir where -- Only checks for clashes that would lead to the same -- name being exported twice from the module. checkForClashes new = when (publicOpen dir) $ do old <- allThingsInScope . restrictPrivate <$> (getNamedScope =<< getCurrentModule) let defClashes = Map.toList $ Map.intersectionWith (,) (nsNames new) (nsNames old) modClashes = Map.toList $ Map.intersectionWith (,) (nsModules new) (nsModules old) -- No ambiguity if concrete identifier is mapped to -- single, identical abstract identifiers. realClash (_, ([x],[y])) = x /= y realClash _ = True -- No ambiguity if concrete identifier is only mapped to -- constructor names or only to projection names. defClash (_, (qs0, qs1)) = not $ all (== ConName) ks || all (==FldName) ks where ks = map anameKind $ qs0 ++ qs1 -- We report the first clashing exported identifier. unlessNull (filter (\ x -> realClash x && defClash x) defClashes) $ \ ((x, (_, q:_)) : _) -> typeError $ ClashingDefinition (C.QName x) (anameName q) unlessNull (filter realClash modClashes) $ \ ((_, (m0:_, m1:_)) : _) -> typeError $ ClashingModule (amodName m0) (amodName m1)