(****************************************************************************** * Citadelle * * Copyright (c) 2011-2018 Université Paris-Saclay, Univ. Paris-Sud, France * 2013-2017 IRT SystemX, France * 2011-2015 Achim D. Brucker, Germany * 2016-2018 The University of Sheffield, UK * 2016-2017 Nanyang Technological University, Singapore * 2017-2018 Virginia Tech, USA * * All rights reserved. * * 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 the name of the copyright holders 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 COPYRIGHT HOLDERS AND 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 COPYRIGHT * OWNER 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. ******************************************************************************) theory Init imports "isabelle_home/src/HOL/Isabelle_Main0" begin section‹Optimization on the String Datatype› text‹The following types will allow to delay all concatenations on @{typ "integer list"}, until we reach the end. As optimization, we also consider the use of @{typ String.literal} besides @{typ "integer list"}.› type_notation natural ("nat") definition "Succ x = x + 1" datatype string⇩b⇩a⇩s⇩e = ST String.literal | ST' "integer list" (* NOTE one can further optimize here by adding another constructor for representing "nat" (oid management) *) datatype abr_string = (* NOTE operations in this datatype must not decrease the size of the string *) SS_base string⇩b⇩a⇩s⇩e | String_concatWith abr_string "abr_string list" syntax "_string1" :: "_ ⇒ abr_string" ("⟨(_)⟩") translations "⟨x⟩" ⇌ "CONST SS_base (CONST ST x)" syntax "_string3" :: "_ ⇒ abr_string" ("≪(_)≫") translations "≪x≫" ⇌ "CONST SS_base (CONST ST' x)" syntax "_integer1" :: "_ ⇒ abr_string" ("°(_)°") translations "°x°" ⇌ "CONST SS_base (CONST ST' ((CONST Cons) x (CONST Nil)))" type_notation abr_string ("string") section‹Basic Extension of the Standard Library› subsection‹Polymorphic Cartouches› text‹We generalize the construction of cartouches for them to be used ``polymorphically'', however the type inference is not automatic: types of all cartouche expressions will need to be specified earlier before their use (we will however provide a default type).› ML‹ structure Cartouche_Grammar = struct fun list_comb_mk cst n c = list_comb (Syntax.const cst, String_Syntax.mk_bits_syntax n c) val nil1 = Syntax.const @{const_syntax String.empty_literal} fun cons1 c l = list_comb_mk @{const_syntax String.Literal} 7 c $ l val default = [ ( "char list" , ( Const (@{const_syntax Nil}, @{typ "char list"}) , fn c => fn l => Syntax.const @{const_syntax Cons} $ list_comb_mk @{const_syntax Char} 8 c $ l , snd)) , ( "String.literal", (nil1, cons1, snd)) , ( "abr_string" , ( nil1 , cons1 , fn (_, x) => Syntax.const @{const_syntax SS_base} $ (Syntax.const @{const_syntax ST} $ x)))] end › ML‹ fun parse_translation_cartouche binding l f_integer accu = let val cartouche_type = Attrib.setup_config_string binding (K (fst (hd l))) (* if there is no type specified, by default we set the first element to be the default type of cartouches *) in fn ctxt => let val cart_type = Config.get ctxt cartouche_type in case List.find (fn (s, _) => s = cart_type) l of NONE => error ("Unregistered return type for the cartouche: \"" ^ cart_type ^ "\"") | SOME (_, (nil0, cons, f)) => string_tr f (f_integer, cons, nil0) accu (Symbol_Pos.cartouche_content o Symbol_Pos.explode) end end › parse_translation ‹ [( @{syntax_const "_cartouche_string"} , parse_translation_cartouche @{binding cartouche_type} Cartouche_Grammar.default (K I) ())] › text‹This is the special command which sets the type of subsequent cartouches. Note: here the given type is currently parsed as a string, one should extend it to be a truly ``typed'' type...› declare[[cartouche_type = "abr_string"]] subsection‹Operations on List›